github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/memberlist/state_test.go (about) 1 package memberlist 2 3 import ( 4 "fmt" 5 "log" 6 "net" 7 "os" 8 "sync" 9 "sync/atomic" 10 "testing" 11 "time" 12 ) 13 14 var bindLock sync.Mutex 15 var bindNum byte = 10 16 17 func getBindAddrNet(network byte) net.IP { 18 bindLock.Lock() 19 defer bindLock.Unlock() 20 21 //result := net.IPv4(127, 0, network, bindNum) 22 //bindNum++ 23 //if bindNum > 255 { 24 // bindNum = 10 25 //} 26 27 result := net.IPv4(127, 0, 0, 1) 28 29 return result 30 } 31 32 func getBindAddr() net.IP { 33 return getBindAddrNet(0) 34 } 35 36 func HostMemberlist(host string, t *testing.T, f func(*Config)) *Memberlist { 37 t.Helper() 38 39 c := DefaultLANConfig() 40 c.Name = host 41 c.BindAddr = host 42 c.BindPort = 0 // choose a free port 43 c.Logger = log.New(os.Stderr, host, log.LstdFlags) 44 if f != nil { 45 f(c) 46 } 47 48 m, err := NewMemberlist(c) 49 if err != nil { 50 t.Fatalf("failed to get memberlist: %s", err) 51 } 52 return m 53 } 54 55 func TestMemberList_Probe(t *testing.T) { 56 addr1 := getBindAddr() 57 addr2 := getBindAddr() 58 59 m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 60 c.ProbeTimeout = time.Millisecond 61 c.ProbeInterval = 10 * time.Millisecond 62 c.BindPort = 56102 63 c.AdvertisePort = 56102 64 c.Name = "c1" 65 }) 66 defer m1.Shutdown() 67 68 m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 69 c.BindPort = 56101 70 c.AdvertisePort = 56101 71 c.Name = "c2" 72 }) 73 defer m2.Shutdown() 74 75 a1 := alive{ 76 Node: m1.config.Name, 77 Addr: addr1.String(), 78 Port: uint16(m1.config.BindPort), 79 Incarnation: 1, 80 Vsn: m1.config.BuildVsnArray(), 81 } 82 m1.aliveNode(&a1, nil, true) 83 a2 := alive{ 84 Node: m2.config.Name, 85 Addr: addr2.String(), 86 Port: uint16(m2.config.BindPort), 87 Incarnation: 1, 88 Vsn: m2.config.BuildVsnArray(), 89 } 90 m1.aliveNode(&a2, nil, false) 91 92 // should ping addr2 93 m1.probe() 94 95 // Should not be marked suspect 96 //n := m1.nodeMap[m2.config.Name] 97 //if n.State != StateAlive { 98 // t.Fatalf("Expect node to be alive") 99 //} 100 // 101 //// Should increment seqno 102 //if m1.sequenceNum != 1 { 103 // t.Fatalf("bad seqno %v", m2.sequenceNum) 104 //} 105 } 106 107 func TestMemberList_ProbeNode_Suspect(t *testing.T) { 108 addr1 := getBindAddr() 109 addr2 := getBindAddr() 110 addr3 := getBindAddr() 111 addr4 := getBindAddr() 112 ip1 := addr1 113 ip2 := addr2 114 ip3 := addr3 115 ip4 := addr4 116 117 m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 118 c.ProbeTimeout = time.Millisecond 119 c.ProbeInterval = 10 * time.Millisecond 120 c.BindPort = 56100 121 c.AdvertisePort = 56100 122 c.Name = "c1" 123 }) 124 defer m1.Shutdown() 125 126 m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 127 c.BindPort = 56099 128 c.AdvertisePort = 56099 129 c.Name = "c2" 130 }) 131 defer m2.Shutdown() 132 m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 133 c.BindPort = 56098 134 c.AdvertisePort = 56098 135 c.Name = "c3" 136 }) 137 defer m3.Shutdown() 138 a1 := alive{Node: m1.config.Name, Addr: ip1.String(), Port: m1.advertisePort, Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 139 m1.aliveNode(&a1, nil, true) 140 a2 := alive{Node: m2.config.Name, Addr: ip2.String(), Port: m2.advertisePort, Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 141 m1.aliveNode(&a2, nil, false) 142 a3 := alive{Node: m3.config.Name, Addr: ip3.String(), Port: m3.advertisePort, Incarnation: 1, Vsn: m3.config.BuildVsnArray()} 143 m1.aliveNode(&a3, nil, false) 144 a4 := alive{Node: "c4", Addr: ip4.String(), Port: uint16(56097), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 145 m1.aliveNode(&a4, nil, false) 146 147 n := m1.nodeMap["c4"] 148 m1.probeNode(n) 149 150 // Should be marked suspect. 151 if n.State != StateSuspect { 152 t.Fatalf("Expect node to be suspect") 153 } 154 time.Sleep(10 * time.Millisecond) 155 156 // One of the peers should have attempted an indirect probe. 157 if s2, s3 := atomic.LoadUint32(&m2.sequenceNum), atomic.LoadUint32(&m3.sequenceNum); s2 != 1 && s3 != 1 { 158 t.Fatalf("bad seqnos, expected both to be 1: %v, %v", s2, s3) 159 } 160 } 161 162 func TestMemberList_ProbeNode_Suspect_Dogpile(t *testing.T) { 163 freePorts := []int{ 164 56096, 165 56095, 166 56094, 167 56093, 168 56092, 169 56091, 170 56090, 171 56089, 172 56088, 173 } 174 nodes := []string{ 175 "c1", 176 "c2", 177 "c3", 178 "c4", 179 "c5", 180 "c6", 181 "c7", 182 "c8", 183 "c9", 184 } 185 cases := []struct { 186 name string 187 numPeers int 188 confirmations int 189 expected time.Duration 190 nodeName string 191 bindPort int 192 }{ 193 {"n=2, k=3 (max timeout disabled)", 1, 0, 500 * time.Millisecond, "c1", 56096}, 194 {"n=3, k=3", 2, 0, 500 * time.Millisecond, "c2", 56095}, 195 {"n=4, k=3", 3, 0, 500 * time.Millisecond, "c3", 56094}, 196 {"n=5, k=3 (max timeout starts to take effect)", 4, 0, 1000 * time.Millisecond, "c4", 56093}, 197 {"n=6, k=3", 5, 0, 1000 * time.Millisecond, "c5", 56092}, 198 {"n=6, k=3 (confirmations start to lower timeout)", 5, 1, 750 * time.Millisecond, "c6", 56091}, 199 {"n=6, k=3", 5, 2, 604 * time.Millisecond, "c7", 56090}, 200 {"n=6, k=3 (timeout driven to nominal value)", 5, 3, 500 * time.Millisecond, "c8", 56089}, 201 {"n=6, k=3", 5, 4, 500 * time.Millisecond, "c9", 56088}, 202 } 203 204 for i, c := range cases { 205 var ports []int 206 ports = append(ports, freePorts[:i]...) 207 ports = append(ports, freePorts[i+1:]...) 208 var peernames []string 209 peernames = append(peernames, nodes[:i]...) 210 peernames = append(peernames, nodes[i+1:]...) 211 t.Run(c.name, func(t *testing.T) { 212 // Create the main memberlist under test. 213 addr := getBindAddr() 214 215 m := HostMemberlist(addr.String(), t, func(conf *Config) { 216 conf.ProbeTimeout = time.Millisecond 217 conf.ProbeInterval = 100 * time.Millisecond 218 conf.SuspicionMult = 5 219 conf.SuspicionMaxTimeoutMult = 2 220 conf.BindPort = c.bindPort 221 conf.AdvertisePort = c.bindPort 222 conf.Name = c.nodeName 223 }) 224 defer m.Shutdown() 225 226 a := alive{Node: addr.String(), Addr: addr.String(), Port: uint16(m.config.BindPort), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 227 m.aliveNode(&a, nil, true) 228 229 // Make all but one peer be an real, alive instance. 230 var peers []*Memberlist 231 for j := 0; j < c.numPeers-1; j++ { 232 peerAddr := getBindAddr() 233 234 peer := HostMemberlist(peerAddr.String(), t, func(c *Config) { 235 c.BindPort = ports[j] 236 c.AdvertisePort = ports[j] 237 c.Name = peernames[j] 238 }) 239 defer peer.Shutdown() 240 241 peers = append(peers, peer) 242 243 a = alive{Node: peer.config.Name, Addr: peerAddr.String(), Port: peer.advertisePort, Incarnation: 1, Vsn: m.config.BuildVsnArray()} 244 m.aliveNode(&a, nil, false) 245 } 246 247 // Just use a bogus address for the last peer so it doesn't respond 248 // to pings, but tell the memberlist it's alive. 249 badPeerAddr := getBindAddr() 250 badPeerName := "c10" 251 a = alive{Node: badPeerName, Addr: badPeerAddr.String(), Port: uint16(56087), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 252 m.aliveNode(&a, nil, false) 253 254 // Force a probe, which should start us into the suspect state. 255 m.probeNodeByAddr(badPeerName) 256 257 if m.getNodeState(badPeerName) != StateSuspect { 258 t.Fatalf("case %d: expected node to be suspect", i) 259 } 260 261 // Add the requested number of confirmations. 262 for j := 0; j < c.confirmations; j++ { 263 from := fmt.Sprintf("peer%d", j) 264 s := suspect{Node: badPeerName, Incarnation: 1, From: from} 265 m.suspectNode(&s) 266 } 267 268 // Wait until right before the timeout and make sure the timer 269 // hasn't fired. 270 fudge := 25 * time.Millisecond 271 time.Sleep(c.expected - fudge) 272 273 if m.getNodeState(badPeerName) != StateSuspect { 274 t.Fatalf("case %d: expected node to still be suspect", i) 275 } 276 277 // Wait through the timeout and a little after to make sure the 278 // timer fires. 279 time.Sleep(2 * fudge) 280 281 if m.getNodeState(badPeerName) != StateDead { 282 t.Fatalf("case %d: expected node to be dead", i) 283 } 284 }) 285 } 286 } 287 288 /* 289 func TestMemberList_ProbeNode_FallbackTCP(t *testing.T) { 290 addr1 := getBindAddr() 291 addr2 := getBindAddr() 292 addr3 := getBindAddr() 293 addr4 := getBindAddr() 294 ip1 := addr1 295 ip2 := addr2 296 ip3 := addr3 297 ip4 := addr4 298 299 var probeTimeMax time.Duration 300 m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 301 c.ProbeTimeout = 10 * time.Millisecond 302 c.ProbeInterval = 200 * time.Millisecond 303 probeTimeMax = c.ProbeInterval + 20*time.Millisecond 304 }) 305 defer m1.Shutdown() 306 307 bindPort := m1.config.BindPort 308 309 m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 310 c.BindPort = bindPort 311 }) 312 defer m2.Shutdown() 313 314 m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 315 c.BindPort = bindPort 316 }) 317 defer m3.Shutdown() 318 319 m4 := HostMemberlist(addr4.String(), t, func(c *Config) { 320 c.BindPort = bindPort 321 }) 322 defer m4.Shutdown() 323 324 a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 325 m1.aliveNode(&a1, nil, true) 326 a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 327 m1.aliveNode(&a2, nil, false) 328 a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1} 329 m1.aliveNode(&a3, nil, false) 330 331 // Make sure m4 is configured with the same protocol version as m1 so 332 // the TCP fallback behavior is enabled. 333 a4 := alive{ 334 Node: addr4.String(), 335 Addr: ip4.String(), 336 Port: uint16(bindPort), 337 Incarnation: 1, 338 Vsn: []uint8{ 339 ProtocolVersionMin, 340 ProtocolVersionMax, 341 m1.config.ProtocolVersion, 342 m1.config.DelegateProtocolMin, 343 m1.config.DelegateProtocolMax, 344 m1.config.DelegateProtocolVersion, 345 }, 346 } 347 m1.aliveNode(&a4, nil, false) 348 349 // Isolate m4 from UDP traffic by re-opening its listener on the wrong 350 // port. This should force the TCP fallback path to be used. 351 var err error 352 if err = m4.udpListener.Close(); err != nil { 353 t.Fatalf("err: %v", err) 354 } 355 udpAddr := &net.UDPAddr{IP: ip4.String(), Port: 9999} 356 if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil { 357 t.Fatalf("err: %v", err) 358 } 359 360 // Have node m1 probe m4. 361 n := m1.nodeMap[addr4.String()] 362 startProbe := time.Now() 363 m1.probeNode(n) 364 probeTime := time.Now().Sub(startProbe) 365 366 // Should be marked alive because of the TCP fallback ping. 367 if n.State != stateAlive { 368 t.Fatalf("expect node to be alive") 369 } 370 371 // Make sure TCP activity completed in a timely manner. 372 if probeTime > probeTimeMax { 373 t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 374 } 375 376 // Confirm at least one of the peers attempted an indirect probe. 377 time.Sleep(probeTimeMax) 378 if m2.sequenceNum != 1 && m3.sequenceNum != 1 { 379 t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 380 } 381 382 // Now shutdown all inbound TCP traffic to make sure the TCP fallback 383 // path properly fails when the node is really unreachable. 384 if err = m4.tcpListener.Close(); err != nil { 385 t.Fatalf("err: %v", err) 386 } 387 tcpAddr := &net.TCPAddr{IP: ip4.String(), Port: 9999} 388 if m4.tcpListener, err = net.ListenTCP("tcp", tcpAddr); err != nil { 389 t.Fatalf("err: %v", err) 390 } 391 392 // Probe again, this time there should be no contact. 393 startProbe = time.Now() 394 m1.probeNode(n) 395 probeTime = time.Now().Sub(startProbe) 396 397 // Node should be reported suspect. 398 if n.State != stateSuspect { 399 t.Fatalf("expect node to be suspect") 400 } 401 402 // Make sure TCP activity didn't cause us to wait too long before 403 // timing out. 404 if probeTime > probeTimeMax { 405 t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 406 } 407 408 // Confirm at least one of the peers attempted an indirect probe. 409 time.Sleep(probeTimeMax) 410 if m2.sequenceNum != 2 && m3.sequenceNum != 2 { 411 t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 412 } 413 } 414 415 func TestMemberList_ProbeNode_FallbackTCP_Disabled(t *testing.T) { 416 addr1 := getBindAddr() 417 addr2 := getBindAddr() 418 addr3 := getBindAddr() 419 addr4 := getBindAddr() 420 ip1 := addr1 421 ip2 := addr2 422 ip3 := addr3 423 ip4 := addr4 424 425 var probeTimeMax time.Duration 426 m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 427 c.ProbeTimeout = 10 * time.Millisecond 428 c.ProbeInterval = 200 * time.Millisecond 429 probeTimeMax = c.ProbeInterval + 20*time.Millisecond 430 }) 431 defer m1.Shutdown() 432 433 bindPort := m1.config.BindPort 434 435 m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 436 c.BindPort = bindPort 437 }) 438 defer m2.Shutdown() 439 440 m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 441 c.BindPort = bindPort 442 }) 443 defer m3.Shutdown() 444 445 m4 := HostMemberlist(addr4.String(), t, func(c *Config) { 446 c.BindPort = bindPort 447 }) 448 defer m4.Shutdown() 449 450 a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 451 m1.aliveNode(&a1, nil, true) 452 a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 453 m1.aliveNode(&a2, nil, false) 454 a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1} 455 m1.aliveNode(&a3, nil, false) 456 457 // Make sure m4 is configured with the same protocol version as m1 so 458 // the TCP fallback behavior is enabled. 459 a4 := alive{ 460 Node: addr4.String(), 461 Addr: ip4.String(), 462 Port: uint16(bindPort), 463 Incarnation: 1, 464 Vsn: []uint8{ 465 ProtocolVersionMin, 466 ProtocolVersionMax, 467 m1.config.ProtocolVersion, 468 m1.config.DelegateProtocolMin, 469 m1.config.DelegateProtocolMax, 470 m1.config.DelegateProtocolVersion, 471 }, 472 } 473 m1.aliveNode(&a4, nil, false) 474 475 // Isolate m4 from UDP traffic by re-opening its listener on the wrong 476 // port. This should force the TCP fallback path to be used. 477 var err error 478 if err = m4.udpListener.Close(); err != nil { 479 t.Fatalf("err: %v", err) 480 } 481 udpAddr := &net.UDPAddr{IP: ip4.String(), Port: 9999} 482 if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil { 483 t.Fatalf("err: %v", err) 484 } 485 486 // Disable the TCP pings using the config mechanism. 487 m1.config.DisableTcpPings = true 488 489 // Have node m1 probe m4. 490 n := m1.nodeMap[addr4.String()] 491 startProbe := time.Now() 492 m1.probeNode(n) 493 probeTime := time.Now().Sub(startProbe) 494 495 // Node should be reported suspect. 496 if n.State != stateSuspect { 497 t.Fatalf("expect node to be suspect") 498 } 499 500 // Make sure TCP activity didn't cause us to wait too long before 501 // timing out. 502 if probeTime > probeTimeMax { 503 t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 504 } 505 506 // Confirm at least one of the peers attempted an indirect probe. 507 time.Sleep(probeTimeMax) 508 if m2.sequenceNum != 1 && m3.sequenceNum != 1 { 509 t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 510 } 511 } 512 513 func TestMemberList_ProbeNode_FallbackTCP_OldProtocol(t *testing.T) { 514 addr1 := getBindAddr() 515 addr2 := getBindAddr() 516 addr3 := getBindAddr() 517 addr4 := getBindAddr() 518 ip1 := addr1 519 ip2 := addr2 520 ip3 := addr3 521 ip4 := addr4 522 523 var probeTimeMax time.Duration 524 m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 525 c.ProbeTimeout = 10 * time.Millisecond 526 c.ProbeInterval = 200 * time.Millisecond 527 probeTimeMax = c.ProbeInterval + 20*time.Millisecond 528 }) 529 defer m1.Shutdown() 530 531 bindPort := m1.config.BindPort 532 533 m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 534 c.BindPort = bindPort 535 }) 536 defer m2.Shutdown() 537 538 m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 539 c.BindPort = bindPort 540 }) 541 defer m3.Shutdown() 542 543 m4 := HostMemberlist(addr4.String(), t, func(c *Config) { 544 c.BindPort = bindPort 545 }) 546 defer m4.Shutdown() 547 548 a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 549 m1.aliveNode(&a1, nil, true) 550 a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 551 m1.aliveNode(&a2, nil, false) 552 a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1} 553 m1.aliveNode(&a3, nil, false) 554 555 // Set up m4 so that it doesn't understand a version of the protocol 556 // that supports TCP pings. 557 a4 := alive{ 558 Node: addr4.String(), 559 Addr: ip4.String(), 560 Port: uint16(bindPort), 561 Incarnation: 1, 562 Vsn: []uint8{ 563 ProtocolVersionMin, 564 ProtocolVersion2Compatible, 565 ProtocolVersion2Compatible, 566 m1.config.DelegateProtocolMin, 567 m1.config.DelegateProtocolMax, 568 m1.config.DelegateProtocolVersion, 569 }, 570 } 571 m1.aliveNode(&a4, nil, false) 572 573 // Isolate m4 from UDP traffic by re-opening its listener on the wrong 574 // port. This should force the TCP fallback path to be used. 575 var err error 576 if err = m4.udpListener.Close(); err != nil { 577 t.Fatalf("err: %v", err) 578 } 579 udpAddr := &net.UDPAddr{IP: ip4.String(), Port: 9999} 580 if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil { 581 t.Fatalf("err: %v", err) 582 } 583 584 // Have node m1 probe m4. 585 n := m1.nodeMap[addr4.String()] 586 startProbe := time.Now() 587 m1.probeNode(n) 588 probeTime := time.Now().Sub(startProbe) 589 590 // Node should be reported suspect. 591 if n.State != stateSuspect { 592 t.Fatalf("expect node to be suspect") 593 } 594 595 // Make sure TCP activity didn't cause us to wait too long before 596 // timing out. 597 if probeTime > probeTimeMax { 598 t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 599 } 600 601 // Confirm at least one of the peers attempted an indirect probe. 602 time.Sleep(probeTimeMax) 603 if m2.sequenceNum != 1 && m3.sequenceNum != 1 { 604 t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 605 } 606 } 607 */ 608 // 609 //func TestMemberList_ProbeNode_Awareness_Degraded(t *testing.T) { 610 // addr1 := getBindAddr() 611 // addr2 := getBindAddr() 612 // addr3 := getBindAddr() 613 // addr4 := getBindAddr() 614 // ip1 := addr1 615 // ip2 := addr2 616 // ip3 := addr3 617 // ip4 := addr4 618 // 619 // var probeTimeMin time.Duration 620 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 621 // c.ProbeTimeout = 10 * time.Millisecond 622 // c.ProbeInterval = 200 * time.Millisecond 623 // probeTimeMin = 2*c.ProbeInterval - 50*time.Millisecond 624 // }) 625 // defer m1.Shutdown() 626 // 627 // bindPort := m1.config.BindPort 628 // 629 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 630 // c.BindPort = bindPort 631 // c.ProbeTimeout = 10 * time.Millisecond 632 // c.ProbeInterval = 200 * time.Millisecond 633 // }) 634 // defer m2.Shutdown() 635 // 636 // m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 637 // c.BindPort = bindPort 638 // c.ProbeTimeout = 10 * time.Millisecond 639 // c.ProbeInterval = 200 * time.Millisecond 640 // }) 641 // defer m3.Shutdown() 642 // 643 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 644 // m1.aliveNode(&a1, nil, true) 645 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 646 // m1.aliveNode(&a2, nil, false) 647 // a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m3.config.BuildVsnArray()} 648 // m1.aliveNode(&a3, nil, false) 649 // 650 // vsn4 := []uint8{ 651 // ProtocolVersionMin, ProtocolVersionMax, ProtocolVersionMin, 652 // 1, 1, 1, 653 // } 654 // // Node 4 never gets started. 655 // a4 := alive{Node: addr4.String(), Addr: ip4.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: vsn4} 656 // m1.aliveNode(&a4, nil, false) 657 // 658 // // Start the health in a degraded state. 659 // m1.awareness.ApplyDelta(1) 660 // if score := m1.GetHealthScore(); score != 1 { 661 // t.Fatalf("bad: %d", score) 662 // } 663 // 664 // // Have node m1 probe m4. 665 // n := m1.nodeMap[addr4.String()] 666 // startProbe := time.Now() 667 // m1.probeNode(n) 668 // probeTime := time.Now().Sub(startProbe) 669 // 670 // // Node should be reported suspect. 671 // if n.State != StateSuspect { 672 // t.Fatalf("expect node to be suspect") 673 // } 674 // 675 // // Make sure we timed out approximately on time (note that we accounted 676 // // for the slowed-down failure detector in the probeTimeMin calculation. 677 // if probeTime < probeTimeMin { 678 // t.Fatalf("probed too quickly, %9.6f", probeTime.Seconds()) 679 // } 680 // 681 // // Confirm at least one of the peers attempted an indirect probe. 682 // if m2.sequenceNum != 1 && m3.sequenceNum != 1 { 683 // t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 684 // } 685 // 686 // // We should have gotten all the nacks, so our score should remain the 687 // // same, since we didn't get a successful probe. 688 // if score := m1.GetHealthScore(); score != 1 { 689 // t.Fatalf("bad: %d", score) 690 // } 691 //} 692 // 693 //func TestMemberList_ProbeNode_Wrong_VSN(t *testing.T) { 694 // addr1 := getBindAddr() 695 // addr2 := getBindAddr() 696 // addr3 := getBindAddr() 697 // addr4 := getBindAddr() 698 // ip1 := addr1 699 // ip2 := addr2 700 // ip3 := addr3 701 // ip4 := addr4 702 // 703 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 704 // c.ProbeTimeout = 10 * time.Millisecond 705 // c.ProbeInterval = 200 * time.Millisecond 706 // }) 707 // defer m1.Shutdown() 708 // 709 // bindPort := m1.config.BindPort 710 // 711 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 712 // c.BindPort = bindPort 713 // c.ProbeTimeout = 10 * time.Millisecond 714 // c.ProbeInterval = 200 * time.Millisecond 715 // }) 716 // defer m2.Shutdown() 717 // 718 // m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 719 // c.BindPort = bindPort 720 // c.ProbeTimeout = 10 * time.Millisecond 721 // c.ProbeInterval = 200 * time.Millisecond 722 // }) 723 // defer m3.Shutdown() 724 // 725 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 726 // m1.aliveNode(&a1, nil, true) 727 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 728 // m1.aliveNode(&a2, nil, false) 729 // a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m3.config.BuildVsnArray()} 730 // m1.aliveNode(&a3, nil, false) 731 // 732 // vsn4 := []uint8{ 733 // 0, 0, 0, 734 // 0, 0, 0, 735 // } 736 // // Node 4 never gets started. 737 // a4 := alive{Node: addr4.String(), Addr: ip4.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: vsn4} 738 // m1.aliveNode(&a4, nil, false) 739 // 740 // // Start the health in a degraded state. 741 // m1.awareness.ApplyDelta(1) 742 // if score := m1.GetHealthScore(); score != 1 { 743 // t.Fatalf("bad: %d", score) 744 // } 745 // 746 // // Have node m1 probe m4. 747 // n, ok := m1.nodeMap[addr4.String()] 748 // if ok || n != nil { 749 // t.Fatalf("expect node a4 to be not taken into account, because of its wrong version") 750 // } 751 //} 752 // 753 //func TestMemberList_ProbeNode_Awareness_Improved(t *testing.T) { 754 // addr1 := getBindAddr() 755 // addr2 := getBindAddr() 756 // ip1 := addr1 757 // ip2 := addr2 758 // 759 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 760 // c.ProbeTimeout = 10 * time.Millisecond 761 // c.ProbeInterval = 200 * time.Millisecond 762 // }) 763 // defer m1.Shutdown() 764 // 765 // bindPort := m1.config.BindPort 766 // 767 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 768 // c.BindPort = bindPort 769 // }) 770 // defer m2.Shutdown() 771 // 772 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 773 // m1.aliveNode(&a1, nil, true) 774 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 775 // m1.aliveNode(&a2, nil, false) 776 // 777 // // Start the health in a degraded state. 778 // m1.awareness.ApplyDelta(1) 779 // if score := m1.GetHealthScore(); score != 1 { 780 // t.Fatalf("bad: %d", score) 781 // } 782 // 783 // // Have node m1 probe m2. 784 // n := m1.nodeMap[addr2.String()] 785 // m1.probeNode(n) 786 // 787 // // Node should be reported alive. 788 // if n.State != StateAlive { 789 // t.Fatalf("expect node to be suspect") 790 // } 791 // 792 // // Our score should have improved since we did a good probe. 793 // if score := m1.GetHealthScore(); score != 0 { 794 // t.Fatalf("bad: %d", score) 795 // } 796 //} 797 // 798 //func TestMemberList_ProbeNode_Awareness_MissedNack(t *testing.T) { 799 // addr1 := getBindAddr() 800 // addr2 := getBindAddr() 801 // addr3 := getBindAddr() 802 // addr4 := getBindAddr() 803 // ip1 := addr1 804 // ip2 := addr2 805 // ip3 := addr3 806 // ip4 := addr4 807 // 808 // var probeTimeMax time.Duration 809 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 810 // c.ProbeTimeout = 10 * time.Millisecond 811 // c.ProbeInterval = 200 * time.Millisecond 812 // probeTimeMax = c.ProbeInterval + 50*time.Millisecond 813 // }) 814 // defer m1.Shutdown() 815 // 816 // bindPort := m1.config.BindPort 817 // 818 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 819 // c.BindPort = bindPort 820 // c.ProbeTimeout = 10 * time.Millisecond 821 // c.ProbeInterval = 200 * time.Millisecond 822 // }) 823 // defer m2.Shutdown() 824 // 825 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 826 // m1.aliveNode(&a1, nil, true) 827 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 828 // m1.aliveNode(&a2, nil, false) 829 // 830 // vsn := m1.config.BuildVsnArray() 831 // // Node 3 and node 4 never get started. 832 // a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: vsn} 833 // m1.aliveNode(&a3, nil, false) 834 // a4 := alive{Node: addr4.String(), Addr: ip4.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: vsn} 835 // m1.aliveNode(&a4, nil, false) 836 // 837 // // Make sure health looks good. 838 // if score := m1.GetHealthScore(); score != 0 { 839 // t.Fatalf("bad: %d", score) 840 // } 841 // 842 // // Have node m1 probe m4. 843 // n := m1.nodeMap[addr4.String()] 844 // startProbe := time.Now() 845 // m1.probeNode(n) 846 // probeTime := time.Now().Sub(startProbe) 847 // 848 // // Node should be reported suspect. 849 // 850 // m1.nodeLock.Lock() 851 // if n.State != StateSuspect { 852 // t.Fatalf("expect node to be suspect") 853 // } 854 // m1.nodeLock.Unlock() 855 // 856 // // Make sure we timed out approximately on time. 857 // if probeTime > probeTimeMax { 858 // t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 859 // } 860 // 861 // // We should have gotten dinged for the missed nack. Note that the code under 862 // // test is waiting for probeTimeMax and then doing some other work before it 863 // // updates the awareness, so we need to wait some extra time. Rather than just 864 // // add longer and longer sleeps, we'll retry a few times. 865 // iretry.Run(t, func(r *iretry.R) { 866 // if score := m1.GetHealthScore(); score != 1 { 867 // r.Fatalf("expected health score to decrement on missed nack. want %d, "+ 868 // "got: %d", 1, score) 869 // } 870 // }) 871 //} 872 // 873 //func TestMemberList_ProbeNode_Awareness_OldProtocol(t *testing.T) { 874 // addr1 := getBindAddr() 875 // addr2 := getBindAddr() 876 // addr3 := getBindAddr() 877 // addr4 := getBindAddr() 878 // ip1 := addr1 879 // ip2 := addr2 880 // ip3 := addr3 881 // ip4 := addr4 882 // 883 // var probeTimeMax time.Duration 884 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 885 // c.ProbeTimeout = 10 * time.Millisecond 886 // c.ProbeInterval = 200 * time.Millisecond 887 // probeTimeMax = c.ProbeInterval + 20*time.Millisecond 888 // }) 889 // defer m1.Shutdown() 890 // 891 // bindPort := m1.config.BindPort 892 // 893 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 894 // c.BindPort = bindPort 895 // }) 896 // defer m2.Shutdown() 897 // 898 // m3 := HostMemberlist(addr3.String(), t, func(c *Config) { 899 // c.BindPort = bindPort 900 // }) 901 // defer m3.Shutdown() 902 // 903 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 904 // m1.aliveNode(&a1, nil, true) 905 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 906 // m1.aliveNode(&a2, nil, false) 907 // a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1} 908 // m1.aliveNode(&a3, nil, false) 909 // 910 // // Node 4 never gets started. 911 // a4 := alive{Node: addr4.String(), Addr: ip4.String(), Port: uint16(bindPort), Incarnation: 1} 912 // m1.aliveNode(&a4, nil, false) 913 // 914 // // Make sure health looks good. 915 // if score := m1.GetHealthScore(); score != 0 { 916 // t.Fatalf("bad: %d", score) 917 // } 918 // 919 // // Have node m1 probe m4. 920 // n := m1.nodeMap[addr4.String()] 921 // startProbe := time.Now() 922 // m1.probeNode(n) 923 // probeTime := time.Now().Sub(startProbe) 924 // 925 // // Node should be reported suspect. 926 // if n.State != StateSuspect { 927 // t.Fatalf("expect node to be suspect") 928 // } 929 // 930 // // Make sure we timed out approximately on time. 931 // if probeTime > probeTimeMax { 932 // t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds()) 933 // } 934 // 935 // // Confirm at least one of the peers attempted an indirect probe. 936 // time.Sleep(probeTimeMax) 937 // if m2.sequenceNum != 1 && m3.sequenceNum != 1 { 938 // t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum) 939 // } 940 // 941 // // Since we are using the old protocol here, we should have gotten dinged 942 // // for a failed health check. 943 // if score := m1.GetHealthScore(); score != 1 { 944 // t.Fatalf("bad: %d", score) 945 // } 946 //} 947 // 948 //func TestMemberList_ProbeNode_Buddy(t *testing.T) { 949 // addr1 := getBindAddr() 950 // addr2 := getBindAddr() 951 // ip1 := addr1 952 // ip2 := addr2 953 // 954 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 955 // c.ProbeTimeout = time.Millisecond 956 // c.ProbeInterval = 10 * time.Millisecond 957 // }) 958 // defer m1.Shutdown() 959 // 960 // bindPort := m1.config.BindPort 961 // 962 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 963 // c.BindPort = bindPort 964 // }) 965 // defer m2.Shutdown() 966 // 967 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 968 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 969 // 970 // m1.aliveNode(&a1, nil, true) 971 // m1.aliveNode(&a2, nil, false) 972 // m2.aliveNode(&a2, nil, true) 973 // 974 // // Force the state to suspect so we piggyback a suspect message with the ping. 975 // // We should see this get refuted later, and the ping will succeed. 976 // n := m1.nodeMap[addr2.String()] 977 // n.State = StateSuspect 978 // m1.probeNode(n) 979 // 980 // // Make sure a ping was sent. 981 // if m1.sequenceNum != 1 { 982 // t.Fatalf("bad seqno %v", m1.sequenceNum) 983 // } 984 // 985 // // Check a broadcast is queued. 986 // if num := m2.broadcasts.NumQueued(); num != 1 { 987 // t.Fatalf("expected only one queued message: %d", num) 988 // } 989 // 990 // // Should be alive msg. 991 // if messageType(m2.broadcasts.orderedView(true)[0].b.Message()[0]) != aliveMsg { 992 // t.Fatalf("expected queued alive msg") 993 // } 994 //} 995 // 996 //func TestMemberList_ProbeNode(t *testing.T) { 997 // addr1 := getBindAddr() 998 // addr2 := getBindAddr() 999 // ip1 := addr1 1000 // ip2 := addr2 1001 // 1002 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 1003 // c.ProbeTimeout = time.Millisecond 1004 // c.ProbeInterval = 10 * time.Millisecond 1005 // }) 1006 // defer m1.Shutdown() 1007 // 1008 // bindPort := m1.config.BindPort 1009 // 1010 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 1011 // c.BindPort = bindPort 1012 // }) 1013 // defer m2.Shutdown() 1014 // 1015 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 1016 // m1.aliveNode(&a1, nil, true) 1017 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 1018 // m1.aliveNode(&a2, nil, false) 1019 // 1020 // n := m1.nodeMap[addr2.String()] 1021 // m1.probeNode(n) 1022 // 1023 // // Should be marked alive 1024 // if n.State != StateAlive { 1025 // t.Fatalf("Expect node to be alive") 1026 // } 1027 // 1028 // // Should increment seqno 1029 // if m1.sequenceNum != 1 { 1030 // t.Fatalf("bad seqno %v", m1.sequenceNum) 1031 // } 1032 //} 1033 // 1034 //func TestMemberList_Ping(t *testing.T) { 1035 // addr1 := getBindAddr() 1036 // addr2 := getBindAddr() 1037 // ip1 := addr1 1038 // ip2 := addr2 1039 // 1040 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 1041 // c.ProbeTimeout = 1 * time.Second 1042 // c.ProbeInterval = 10 * time.Second 1043 // }) 1044 // defer m1.Shutdown() 1045 // 1046 // bindPort := m1.config.BindPort 1047 // 1048 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 1049 // c.BindPort = bindPort 1050 // }) 1051 // defer m2.Shutdown() 1052 // 1053 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1} 1054 // m1.aliveNode(&a1, nil, true) 1055 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1} 1056 // m1.aliveNode(&a2, nil, false) 1057 // 1058 // // Do a legit ping. 1059 // n := m1.nodeMap[addr2.String()] 1060 // addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(addr2.String(), strconv.Itoa(bindPort))) 1061 // if err != nil { 1062 // t.Fatalf("err: %v", err) 1063 // } 1064 // rtt, err := m1.Ping(n.Name, addr) 1065 // if err != nil { 1066 // t.Fatalf("err: %v", err) 1067 // } 1068 // if !(rtt > 0) { 1069 // t.Fatalf("bad: %v", rtt) 1070 // } 1071 // 1072 // // This ping has a bad node name so should timeout. 1073 // _, err = m1.Ping("bad", addr) 1074 // if _, ok := err.(NoPingResponseError); !ok || err == nil { 1075 // t.Fatalf("bad: %v", err) 1076 // } 1077 //} 1078 // 1079 //func TestMemberList_ResetNodes(t *testing.T) { 1080 // m := GetMemberlist(t, func(c *Config) { 1081 // c.GossipToTheDeadTime = 100 * time.Millisecond 1082 // }) 1083 // defer m.Shutdown() 1084 // 1085 // a1 := alive{Node: "test1", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1086 // m.aliveNode(&a1, nil, false) 1087 // a2 := alive{Node: "test2", Addr: string([]byte{127, 0, 0, 2}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1088 // m.aliveNode(&a2, nil, false) 1089 // a3 := alive{Node: "test3", Addr: string([]byte{127, 0, 0, 3}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1090 // m.aliveNode(&a3, nil, false) 1091 // d := dead{Node: "test2", Incarnation: 1} 1092 // m.deadNode(&d) 1093 // 1094 // m.resetNodes() 1095 // if len(m.nodes) != 3 { 1096 // t.Fatalf("Bad length") 1097 // } 1098 // if _, ok := m.nodeMap["test2"]; !ok { 1099 // t.Fatalf("test2 should not be unmapped") 1100 // } 1101 // 1102 // time.Sleep(200 * time.Millisecond) 1103 // m.resetNodes() 1104 // if len(m.nodes) != 2 { 1105 // t.Fatalf("Bad length") 1106 // } 1107 // if _, ok := m.nodeMap["test2"]; ok { 1108 // t.Fatalf("test2 should be unmapped") 1109 // } 1110 //} 1111 // 1112 //func TestMemberList_NextSeq(t *testing.T) { 1113 // m := &Memberlist{} 1114 // if m.nextSeqNo() != 1 { 1115 // t.Fatalf("bad sequence no") 1116 // } 1117 // if m.nextSeqNo() != 2 { 1118 // t.Fatalf("bad sequence no") 1119 // } 1120 //} 1121 // 1122 //func ackHandlerExists(t *testing.T, m *Memberlist, idx uint32) bool { 1123 // t.Helper() 1124 // 1125 // m.ackLock.Lock() 1126 // _, ok := m.ackHandlers[idx] 1127 // m.ackLock.Unlock() 1128 // 1129 // return ok 1130 //} 1131 // 1132 //func TestMemberList_setProbeChannels(t *testing.T) { 1133 // m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)} 1134 // 1135 // ch := make(chan ackMessage, 1) 1136 // m.setProbeChannels(0, ch, nil, 10*time.Millisecond) 1137 // 1138 // require.True(t, ackHandlerExists(t, m, 0), "missing handler") 1139 // 1140 // time.Sleep(20 * time.Millisecond) 1141 // 1142 // require.False(t, ackHandlerExists(t, m, 0), "non-reaped handler") 1143 //} 1144 // 1145 //func TestMemberList_setAckHandler(t *testing.T) { 1146 // m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)} 1147 // 1148 // f := func([]byte, time.Time) {} 1149 // m.setAckHandler(0, f, 10*time.Millisecond) 1150 // 1151 // require.True(t, ackHandlerExists(t, m, 0), "missing handler") 1152 // 1153 // time.Sleep(20 * time.Millisecond) 1154 // 1155 // require.False(t, ackHandlerExists(t, m, 0), "non-reaped handler") 1156 //} 1157 // 1158 //func TestMemberList_invokeAckHandler(t *testing.T) { 1159 // m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)} 1160 // 1161 // // Does nothing 1162 // m.invokeAckHandler(ackResp{}, time.Now()) 1163 // 1164 // var b bool 1165 // f := func(payload []byte, timestamp time.Time) { b = true } 1166 // m.setAckHandler(0, f, 10*time.Millisecond) 1167 // 1168 // // Should set b 1169 // m.invokeAckHandler(ackResp{0, nil}, time.Now()) 1170 // if !b { 1171 // t.Fatalf("b not set") 1172 // } 1173 // 1174 // require.False(t, ackHandlerExists(t, m, 0), "non-reaped handler") 1175 //} 1176 // 1177 //func TestMemberList_invokeAckHandler_Channel_Ack(t *testing.T) { 1178 // m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)} 1179 // 1180 // ack := ackResp{0, []byte{0, 0, 0}} 1181 // 1182 // // Does nothing 1183 // m.invokeAckHandler(ack, time.Now()) 1184 // 1185 // ackCh := make(chan ackMessage, 1) 1186 // nackCh := make(chan struct{}, 1) 1187 // m.setProbeChannels(0, ackCh, nackCh, 10*time.Millisecond) 1188 // 1189 // // Should send message 1190 // m.invokeAckHandler(ack, time.Now()) 1191 // 1192 // select { 1193 // case v := <-ackCh: 1194 // if v.Complete != true { 1195 // t.Fatalf("Bad value") 1196 // } 1197 // if bytes.Compare(v.Payload, ack.Payload) != 0 { 1198 // t.Fatalf("wrong payload. expected: %v; actual: %v", ack.Payload, v.Payload) 1199 // } 1200 // 1201 // case <-nackCh: 1202 // t.Fatalf("should not get a nack") 1203 // 1204 // default: 1205 // t.Fatalf("message not sent") 1206 // } 1207 // 1208 // require.False(t, ackHandlerExists(t, m, 0), "non-reaped handler") 1209 //} 1210 // 1211 //func TestMemberList_invokeAckHandler_Channel_Nack(t *testing.T) { 1212 // m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)} 1213 // 1214 // nack := nackResp{0} 1215 // 1216 // // Does nothing. 1217 // m.invokeNackHandler(nack) 1218 // 1219 // ackCh := make(chan ackMessage, 1) 1220 // nackCh := make(chan struct{}, 1) 1221 // m.setProbeChannels(0, ackCh, nackCh, 10*time.Millisecond) 1222 // 1223 // // Should send message. 1224 // m.invokeNackHandler(nack) 1225 // 1226 // select { 1227 // case <-ackCh: 1228 // t.Fatalf("should not get an ack") 1229 // 1230 // case <-nackCh: 1231 // // Good. 1232 // 1233 // default: 1234 // t.Fatalf("message not sent") 1235 // } 1236 // 1237 // // Getting a nack doesn't reap the handler so that we can still forward 1238 // // an ack up to the reap time, if we get one. 1239 // require.True(t, ackHandlerExists(t, m, 0), "handler should not be reaped") 1240 // 1241 // ack := ackResp{0, []byte{0, 0, 0}} 1242 // m.invokeAckHandler(ack, time.Now()) 1243 // 1244 // select { 1245 // case v := <-ackCh: 1246 // if v.Complete != true { 1247 // t.Fatalf("Bad value") 1248 // } 1249 // if bytes.Compare(v.Payload, ack.Payload) != 0 { 1250 // t.Fatalf("wrong payload. expected: %v; actual: %v", ack.Payload, v.Payload) 1251 // } 1252 // 1253 // case <-nackCh: 1254 // t.Fatalf("should not get a nack") 1255 // 1256 // default: 1257 // t.Fatalf("message not sent") 1258 // } 1259 // 1260 // require.False(t, ackHandlerExists(t, m, 0), "non-reaped handler") 1261 //} 1262 // 1263 //func TestMemberList_AliveNode_NewNode(t *testing.T) { 1264 // ch := make(chan NodeEvent, 1) 1265 // m := GetMemberlist(t, func(c *Config) { 1266 // c.Events = &ChannelEventDelegate{ch} 1267 // }) 1268 // defer m.Shutdown() 1269 // 1270 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1271 // m.aliveNode(&a, nil, false) 1272 // 1273 // if len(m.nodes) != 1 { 1274 // t.Fatalf("should add node") 1275 // } 1276 // 1277 // state, ok := m.nodeMap["test"] 1278 // if !ok { 1279 // t.Fatalf("should map node") 1280 // } 1281 // 1282 // if state.Incarnation != 1 { 1283 // t.Fatalf("bad incarnation") 1284 // } 1285 // if state.State != StateAlive { 1286 // t.Fatalf("bad state") 1287 // } 1288 // if time.Now().Sub(state.StateChange) > time.Second { 1289 // t.Fatalf("bad change delta") 1290 // } 1291 // 1292 // // Check for a join message 1293 // select { 1294 // case e := <-ch: 1295 // if e.Node.Name != "test" { 1296 // t.Fatalf("bad node name") 1297 // } 1298 // default: 1299 // t.Fatalf("no join message") 1300 // } 1301 // 1302 // // Check a broad cast is queued 1303 // if m.broadcasts.NumQueued() != 1 { 1304 // t.Fatalf("expected queued message") 1305 // } 1306 //} 1307 // 1308 //func TestMemberList_AliveNode_SuspectNode(t *testing.T) { 1309 // ch := make(chan NodeEvent, 1) 1310 // ted := &toggledEventDelegate{ 1311 // real: &ChannelEventDelegate{ch}, 1312 // } 1313 // m := GetMemberlist(t, func(c *Config) { 1314 // c.Events = ted 1315 // }) 1316 // defer m.Shutdown() 1317 // 1318 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1319 // m.aliveNode(&a, nil, false) 1320 // 1321 // // Listen only after first join 1322 // ted.Toggle(true) 1323 // 1324 // // Make suspect 1325 // state := m.nodeMap["test"] 1326 // state.State = StateSuspect 1327 // state.StateChange = state.StateChange.Add(-time.Hour) 1328 // 1329 // // Old incarnation number, should not change 1330 // m.aliveNode(&a, nil, false) 1331 // if state.State != StateSuspect { 1332 // t.Fatalf("update with old incarnation!") 1333 // } 1334 // 1335 // // Should reset to alive now 1336 // a.Incarnation = 2 1337 // m.aliveNode(&a, nil, false) 1338 // if state.State != StateAlive { 1339 // t.Fatalf("no update with new incarnation!") 1340 // } 1341 // 1342 // if time.Now().Sub(state.StateChange) > time.Second { 1343 // t.Fatalf("bad change delta") 1344 // } 1345 // 1346 // // Check for a no join message 1347 // select { 1348 // case <-ch: 1349 // t.Fatalf("got bad join message") 1350 // default: 1351 // } 1352 // 1353 // // Check a broad cast is queued 1354 // if m.broadcasts.NumQueued() != 1 { 1355 // t.Fatalf("expected queued message") 1356 // } 1357 //} 1358 // 1359 //func TestMemberList_AliveNode_Idempotent(t *testing.T) { 1360 // ch := make(chan NodeEvent, 1) 1361 // ted := &toggledEventDelegate{ 1362 // real: &ChannelEventDelegate{ch}, 1363 // } 1364 // m := GetMemberlist(t, func(c *Config) { 1365 // c.Events = ted 1366 // }) 1367 // defer m.Shutdown() 1368 // 1369 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1370 // m.aliveNode(&a, nil, false) 1371 // 1372 // // Listen only after first join 1373 // ted.Toggle(true) 1374 // 1375 // // Make suspect 1376 // state := m.nodeMap["test"] 1377 // stateTime := state.StateChange 1378 // 1379 // // Should reset to alive now 1380 // a.Incarnation = 2 1381 // m.aliveNode(&a, nil, false) 1382 // if state.State != StateAlive { 1383 // t.Fatalf("non idempotent") 1384 // } 1385 // 1386 // if stateTime != state.StateChange { 1387 // t.Fatalf("should not change state") 1388 // } 1389 // 1390 // // Check for a no join message 1391 // select { 1392 // case <-ch: 1393 // t.Fatalf("got bad join message") 1394 // default: 1395 // } 1396 // 1397 // // Check a broad cast is queued 1398 // if m.broadcasts.NumQueued() != 1 { 1399 // t.Fatalf("expected only one queued message") 1400 // } 1401 //} 1402 // 1403 //type toggledEventDelegate struct { 1404 // mu sync.Mutex 1405 // real EventDelegate 1406 // enabled bool 1407 //} 1408 // 1409 //func (d *toggledEventDelegate) Toggle(enabled bool) { 1410 // d.mu.Lock() 1411 // defer d.mu.Unlock() 1412 // d.enabled = enabled 1413 //} 1414 // 1415 //// NotifyJoin is invoked when a node is detected to have joined. 1416 //// The Node argument must not be modified. 1417 //func (d *toggledEventDelegate) NotifyJoin(n *Node) { 1418 // d.mu.Lock() 1419 // defer d.mu.Unlock() 1420 // if d.enabled { 1421 // d.real.NotifyJoin(n) 1422 // } 1423 //} 1424 // 1425 //// NotifyLeave is invoked when a node is detected to have left. 1426 //// The Node argument must not be modified. 1427 //func (d *toggledEventDelegate) NotifyLeave(n *Node) { 1428 // d.mu.Lock() 1429 // defer d.mu.Unlock() 1430 // if d.enabled { 1431 // d.real.NotifyLeave(n) 1432 // } 1433 //} 1434 // 1435 //// NotifyUpdate is invoked when a node is detected to have 1436 //// updated, usually involving the meta data. The Node argument 1437 //// must not be modified. 1438 //func (d *toggledEventDelegate) NotifyUpdate(n *Node) { 1439 // d.mu.Lock() 1440 // defer d.mu.Unlock() 1441 // if d.enabled { 1442 // d.real.NotifyUpdate(n) 1443 // } 1444 //} 1445 // 1446 //func (d *toggledEventDelegate) NotifyWeight(n *Node) { 1447 // d.mu.Lock() 1448 // defer d.mu.Unlock() 1449 // if d.enabled { 1450 // d.real.NotifyWeight(n) 1451 // } 1452 //} 1453 // 1454 //func (d *toggledEventDelegate) NotifySuspectSateChange(n *Node) { 1455 // d.mu.Lock() 1456 // defer d.mu.Unlock() 1457 // if d.enabled { 1458 // d.real.NotifySuspectSateChange(n) 1459 // } 1460 //} 1461 // 1462 //// Serf Bug: GH-58, Meta data does not update 1463 //func TestMemberList_AliveNode_ChangeMeta(t *testing.T) { 1464 // ch := make(chan NodeEvent, 1) 1465 // ted := &toggledEventDelegate{ 1466 // real: &ChannelEventDelegate{ch}, 1467 // } 1468 // 1469 // m := GetMemberlist(t, func(c *Config) { 1470 // c.Events = ted 1471 // }) 1472 // defer m.Shutdown() 1473 // 1474 // a := alive{ 1475 // Node: "test", 1476 // Addr: string([]byte{127, 0, 0, 1}), 1477 // Meta: []byte("val1"), 1478 // Incarnation: 1, 1479 // Vsn: m.config.BuildVsnArray()} 1480 // m.aliveNode(&a, nil, false) 1481 // 1482 // // Listen only after first join 1483 // ted.Toggle(true) 1484 // 1485 // // Make suspect 1486 // state := m.nodeMap["test"] 1487 // 1488 // // Should reset to alive now 1489 // a.Incarnation = 2 1490 // a.Meta = []byte("val2") 1491 // m.aliveNode(&a, nil, false) 1492 // 1493 // // Check updates 1494 // if bytes.Compare(state.Meta, a.Meta) != 0 { 1495 // t.Fatalf("meta did not update") 1496 // } 1497 // 1498 // // Check for a NotifyUpdate 1499 // select { 1500 // case e := <-ch: 1501 // if e.Event != NodeUpdate { 1502 // t.Fatalf("bad event: %v", e) 1503 // } 1504 // if !reflect.DeepEqual(*e.Node, state.Node) { 1505 // t.Fatalf("expected %v, got %v", *e.Node, state.Node) 1506 // } 1507 // if bytes.Compare(e.Node.Meta, a.Meta) != 0 { 1508 // t.Fatalf("meta did not update") 1509 // } 1510 // default: 1511 // t.Fatalf("missing event!") 1512 // } 1513 // 1514 //} 1515 // 1516 //func TestMemberList_AliveNode_Refute(t *testing.T) { 1517 // m := GetMemberlist(t, nil) 1518 // defer m.Shutdown() 1519 // 1520 // a := alive{Node: m.config.Name, Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1521 // m.aliveNode(&a, nil, true) 1522 // 1523 // // Clear queue 1524 // m.broadcasts.Reset() 1525 // 1526 // // Conflicting alive 1527 // s := alive{ 1528 // Node: m.config.Name, 1529 // Addr: string([]byte{127, 0, 0, 1}), 1530 // Incarnation: 2, 1531 // Meta: []byte("foo"), 1532 // Vsn: m.config.BuildVsnArray(), 1533 // } 1534 // m.aliveNode(&s, nil, false) 1535 // 1536 // state := m.nodeMap[m.config.Name] 1537 // if state.State != StateAlive { 1538 // t.Fatalf("should still be alive") 1539 // } 1540 // if state.Meta != nil { 1541 // t.Fatalf("meta should still be nil") 1542 // } 1543 // 1544 // // Check a broad cast is queued 1545 // if num := m.broadcasts.NumQueued(); num != 1 { 1546 // t.Fatalf("expected only one queued message: %d", 1547 // num) 1548 // } 1549 // 1550 // // Should be alive mesg 1551 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != aliveMsg { 1552 // t.Fatalf("expected queued alive msg") 1553 // } 1554 //} 1555 // 1556 //func TestMemberList_AliveNode_Conflict(t *testing.T) { 1557 // m := GetMemberlist(t, func(c *Config) { 1558 // c.DeadNodeReclaimTime = 10 * time.Millisecond 1559 // }) 1560 // defer m.Shutdown() 1561 // 1562 // nodeName := "test" 1563 // a := alive{Node: nodeName, Addr: string([]byte{127, 0, 0, 1}), Port: 8000, Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1564 // m.aliveNode(&a, nil, true) 1565 // 1566 // // Clear queue 1567 // m.broadcasts.Reset() 1568 // 1569 // // Conflicting alive 1570 // s := alive{ 1571 // Node: nodeName, 1572 // Addr: string([]byte{127, 0, 0, 2}), 1573 // Port: 9000, 1574 // Incarnation: 2, 1575 // Meta: []byte("foo"), 1576 // Vsn: m.config.BuildVsnArray(), 1577 // } 1578 // m.aliveNode(&s, nil, false) 1579 // 1580 // state := m.nodeMap[nodeName] 1581 // if state.State != StateAlive { 1582 // t.Fatalf("should still be alive") 1583 // } 1584 // if state.Meta != nil { 1585 // t.Fatalf("meta should still be nil") 1586 // } 1587 // if state.Addr == string([]byte{127, 0, 0, 2}) { 1588 // t.Fatalf("address should not be updated") 1589 // } 1590 // if state.Port == 9000 { 1591 // t.Fatalf("port should not be updated") 1592 // } 1593 // 1594 // // Check a broad cast is queued 1595 // if num := m.broadcasts.NumQueued(); num != 0 { 1596 // t.Fatalf("expected 0 queued messages: %d", num) 1597 // } 1598 // 1599 // // Change the node to dead 1600 // d := dead{Node: nodeName, Incarnation: 2} 1601 // m.deadNode(&d) 1602 // m.broadcasts.Reset() 1603 // 1604 // state = m.nodeMap[nodeName] 1605 // if state.State != StateDead { 1606 // t.Fatalf("should be dead") 1607 // } 1608 // 1609 // time.Sleep(m.config.DeadNodeReclaimTime) 1610 // 1611 // // New alive node 1612 // s2 := alive{ 1613 // Node: nodeName, 1614 // Addr: string([]byte{127, 0, 0, 2}), 1615 // Port: 9000, 1616 // Incarnation: 3, 1617 // Meta: []byte("foo"), 1618 // Vsn: m.config.BuildVsnArray(), 1619 // } 1620 // m.aliveNode(&s2, nil, false) 1621 // 1622 // state = m.nodeMap[nodeName] 1623 // if state.State != StateAlive { 1624 // t.Fatalf("should still be alive") 1625 // } 1626 // if !bytes.Equal(state.Meta, []byte("foo")) { 1627 // t.Fatalf("meta should be updated") 1628 // } 1629 // if state.Addr != string([]byte{127, 0, 0, 2}) { 1630 // t.Fatalf("address should be updated") 1631 // } 1632 // if state.Port != 9000 { 1633 // t.Fatalf("port should be updated") 1634 // } 1635 //} 1636 // 1637 //func TestMemberList_SuspectNode_NoNode(t *testing.T) { 1638 // m := GetMemberlist(t, nil) 1639 // defer m.Shutdown() 1640 // 1641 // s := suspect{Node: "test", Incarnation: 1} 1642 // m.suspectNode(&s) 1643 // if len(m.nodes) != 0 { 1644 // t.Fatalf("don't expect nodes") 1645 // } 1646 //} 1647 // 1648 //func TestMemberList_SuspectNode(t *testing.T) { 1649 // m := GetMemberlist(t, func(c *Config) { 1650 // c.ProbeInterval = time.Millisecond 1651 // c.SuspicionMult = 1 1652 // }) 1653 // defer m.Shutdown() 1654 // 1655 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1656 // m.aliveNode(&a, nil, false) 1657 // 1658 // m.changeNode("test", func(state *nodeState) { 1659 // state.StateChange = state.StateChange.Add(-time.Hour) 1660 // }) 1661 // 1662 // s := suspect{Node: "test", Incarnation: 1} 1663 // m.suspectNode(&s) 1664 // 1665 // if m.getNodeState("test") != StateSuspect { 1666 // t.Fatalf("Bad state") 1667 // } 1668 // 1669 // change := m.getNodeStateChange("test") 1670 // if time.Now().Sub(change) > time.Second { 1671 // t.Fatalf("bad change delta") 1672 // } 1673 // 1674 // // Check a broad cast is queued 1675 // if m.broadcasts.NumQueued() != 1 { 1676 // t.Fatalf("expected only one queued message") 1677 // } 1678 // 1679 // // Check its a suspect message 1680 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != suspectMsg { 1681 // t.Fatalf("expected queued suspect msg") 1682 // } 1683 // 1684 // // Wait for the timeout 1685 // time.Sleep(10 * time.Millisecond) 1686 // 1687 // if m.getNodeState("test") != StateDead { 1688 // t.Fatalf("Bad state") 1689 // } 1690 // 1691 // newChange := m.getNodeStateChange("test") 1692 // if time.Now().Sub(newChange) > time.Second { 1693 // t.Fatalf("bad change delta") 1694 // } 1695 // if !newChange.After(change) { 1696 // t.Fatalf("should increment time") 1697 // } 1698 // 1699 // // Check a broad cast is queued 1700 // if m.broadcasts.NumQueued() != 1 { 1701 // t.Fatalf("expected only one queued message") 1702 // } 1703 // 1704 // // Check its a suspect message 1705 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != deadMsg { 1706 // t.Fatalf("expected queued dead msg") 1707 // } 1708 //} 1709 // 1710 //func TestMemberList_SuspectNode_DoubleSuspect(t *testing.T) { 1711 // m := GetMemberlist(t, nil) 1712 // defer m.Shutdown() 1713 // 1714 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1715 // m.aliveNode(&a, nil, false) 1716 // 1717 // state := m.nodeMap["test"] 1718 // state.StateChange = state.StateChange.Add(-time.Hour) 1719 // 1720 // s := suspect{Node: "test", Incarnation: 1} 1721 // m.suspectNode(&s) 1722 // 1723 // if state.State != StateSuspect { 1724 // t.Fatalf("Bad state") 1725 // } 1726 // 1727 // change := state.StateChange 1728 // if time.Now().Sub(change) > time.Second { 1729 // t.Fatalf("bad change delta") 1730 // } 1731 // 1732 // // clear the broadcast queue 1733 // m.broadcasts.Reset() 1734 // 1735 // // Suspect again 1736 // m.suspectNode(&s) 1737 // 1738 // if state.StateChange != change { 1739 // t.Fatalf("unexpected state change") 1740 // } 1741 // 1742 // // Check a broad cast is queued 1743 // if m.broadcasts.NumQueued() != 0 { 1744 // t.Fatalf("expected only one queued message") 1745 // } 1746 // 1747 //} 1748 // 1749 //func TestMemberList_SuspectNode_OldSuspect(t *testing.T) { 1750 // m := GetMemberlist(t, nil) 1751 // defer m.Shutdown() 1752 // 1753 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 10, Vsn: m.config.BuildVsnArray()} 1754 // m.aliveNode(&a, nil, false) 1755 // 1756 // state := m.nodeMap["test"] 1757 // state.StateChange = state.StateChange.Add(-time.Hour) 1758 // 1759 // // Clear queue 1760 // m.broadcasts.Reset() 1761 // 1762 // s := suspect{Node: "test", Incarnation: 1} 1763 // m.suspectNode(&s) 1764 // 1765 // if state.State != StateAlive { 1766 // t.Fatalf("Bad state") 1767 // } 1768 // 1769 // // Check a broad cast is queued 1770 // if m.broadcasts.NumQueued() != 0 { 1771 // t.Fatalf("expected only one queued message") 1772 // } 1773 //} 1774 // 1775 //func TestMemberList_SuspectNode_Refute(t *testing.T) { 1776 // m := GetMemberlist(t, nil) 1777 // defer m.Shutdown() 1778 // 1779 // a := alive{Node: m.config.Name, Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1780 // m.aliveNode(&a, nil, true) 1781 // 1782 // // Clear queue 1783 // m.broadcasts.Reset() 1784 // 1785 // // Make sure health is in a good state 1786 // if score := m.GetHealthScore(); score != 0 { 1787 // t.Fatalf("bad: %d", score) 1788 // } 1789 // 1790 // s := suspect{Node: m.config.Name, Incarnation: 1} 1791 // m.suspectNode(&s) 1792 // 1793 // state := m.nodeMap[m.config.Name] 1794 // if state.State != StateAlive { 1795 // t.Fatalf("should still be alive") 1796 // } 1797 // 1798 // // Check a broad cast is queued 1799 // if m.broadcasts.NumQueued() != 1 { 1800 // t.Fatalf("expected only one queued message") 1801 // } 1802 // 1803 // // Should be alive mesg 1804 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != aliveMsg { 1805 // t.Fatalf("expected queued alive msg") 1806 // } 1807 // 1808 // // Health should have been dinged 1809 // if score := m.GetHealthScore(); score != 1 { 1810 // t.Fatalf("bad: %d", score) 1811 // } 1812 //} 1813 // 1814 //func TestMemberList_DeadNode_NoNode(t *testing.T) { 1815 // m := GetMemberlist(t, nil) 1816 // defer m.Shutdown() 1817 // 1818 // d := dead{Node: "test", Incarnation: 1} 1819 // m.deadNode(&d) 1820 // if len(m.nodes) != 0 { 1821 // t.Fatalf("don't expect nodes") 1822 // } 1823 //} 1824 // 1825 //func TestMemberList_DeadNodeLeft(t *testing.T) { 1826 // ch := make(chan NodeEvent, 1) 1827 // 1828 // m := GetMemberlist(t, func(c *Config) { 1829 // c.Events = &ChannelEventDelegate{ch} 1830 // }) 1831 // defer m.Shutdown() 1832 // 1833 // nodeName := "node1" 1834 // s1 := alive{ 1835 // Node: nodeName, 1836 // Addr: string([]byte{127, 0, 0, 1}), 1837 // Port: 8000, 1838 // Incarnation: 1, 1839 // Vsn: m.config.BuildVsnArray(), 1840 // } 1841 // m.aliveNode(&s1, nil, false) 1842 // 1843 // // Read the join event 1844 // <-ch 1845 // 1846 // d := dead{Node: nodeName, From: nodeName, Incarnation: 1} 1847 // m.deadNode(&d) 1848 // 1849 // // Read the dead event 1850 // <-ch 1851 // 1852 // state := m.nodeMap[nodeName] 1853 // if state.State != StateLeft { 1854 // t.Fatalf("Bad state") 1855 // } 1856 // 1857 // // Check a broad cast is queued 1858 // if m.broadcasts.NumQueued() != 1 { 1859 // t.Fatalf("expected only one queued message") 1860 // } 1861 // 1862 // // Check its a dead message 1863 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != deadMsg { 1864 // t.Fatalf("expected queued dead msg") 1865 // } 1866 // 1867 // // Clear queue 1868 // // m.broadcasts.Reset() 1869 // 1870 // // New alive node 1871 // s2 := alive{ 1872 // Node: nodeName, 1873 // Addr: string([]byte{127, 0, 0, 2}), 1874 // Port: 9000, 1875 // Incarnation: 3, 1876 // Meta: []byte("foo"), 1877 // Vsn: m.config.BuildVsnArray(), 1878 // } 1879 // m.aliveNode(&s2, nil, false) 1880 // 1881 // // Read the join event 1882 // <-ch 1883 // 1884 // state = m.nodeMap[nodeName] 1885 // if state.State != StateAlive { 1886 // t.Fatalf("should still be alive") 1887 // } 1888 // if !bytes.Equal(state.Meta, []byte("foo")) { 1889 // t.Fatalf("meta should be updated") 1890 // } 1891 // if state.Addr != string([]byte{127, 0, 0, 2}) { 1892 // t.Fatalf("address should be updated") 1893 // } 1894 // if state.Port != 9000 { 1895 // t.Fatalf("port should be updated") 1896 // } 1897 //} 1898 // 1899 //func TestMemberList_DeadNode(t *testing.T) { 1900 // ch := make(chan NodeEvent, 1) 1901 // 1902 // m := GetMemberlist(t, func(c *Config) { 1903 // c.Events = &ChannelEventDelegate{ch} 1904 // }) 1905 // defer m.Shutdown() 1906 // 1907 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1908 // m.aliveNode(&a, nil, false) 1909 // 1910 // // Read the join event 1911 // <-ch 1912 // 1913 // state := m.nodeMap["test"] 1914 // state.StateChange = state.StateChange.Add(-time.Hour) 1915 // 1916 // d := dead{Node: "test", Incarnation: 1} 1917 // m.deadNode(&d) 1918 // 1919 // if state.State != StateDead { 1920 // t.Fatalf("Bad state") 1921 // } 1922 // 1923 // change := state.StateChange 1924 // if time.Now().Sub(change) > time.Second { 1925 // t.Fatalf("bad change delta") 1926 // } 1927 // 1928 // select { 1929 // case leave := <-ch: 1930 // if leave.Event != NodeLeave || leave.Node.Name != "test" { 1931 // t.Fatalf("bad node name") 1932 // } 1933 // default: 1934 // t.Fatalf("no leave message") 1935 // } 1936 // 1937 // // Check a broad cast is queued 1938 // if m.broadcasts.NumQueued() != 1 { 1939 // t.Fatalf("expected only one queued message") 1940 // } 1941 // 1942 // // Check its a dead message 1943 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != deadMsg { 1944 // t.Fatalf("expected queued dead msg") 1945 // } 1946 //} 1947 // 1948 //func TestMemberList_DeadNode_Double(t *testing.T) { 1949 // ch := make(chan NodeEvent, 1) 1950 // m := GetMemberlist(t, nil) 1951 // defer m.Shutdown() 1952 // 1953 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 1954 // m.aliveNode(&a, nil, false) 1955 // 1956 // state := m.nodeMap["test"] 1957 // state.StateChange = state.StateChange.Add(-time.Hour) 1958 // 1959 // d := dead{Node: "test", Incarnation: 1} 1960 // m.deadNode(&d) 1961 // 1962 // // Clear queue 1963 // m.broadcasts.Reset() 1964 // 1965 // // Notify after the first dead 1966 // m.config.Events = &ChannelEventDelegate{ch} 1967 // 1968 // // Should do nothing 1969 // d.Incarnation = 2 1970 // m.deadNode(&d) 1971 // 1972 // select { 1973 // case <-ch: 1974 // t.Fatalf("should not get leave") 1975 // default: 1976 // } 1977 // 1978 // // Check a broad cast is queued 1979 // if m.broadcasts.NumQueued() != 0 { 1980 // t.Fatalf("expected only one queued message") 1981 // } 1982 //} 1983 // 1984 //func TestMemberList_DeadNode_OldDead(t *testing.T) { 1985 // m := GetMemberlist(t, nil) 1986 // defer m.Shutdown() 1987 // 1988 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 10, Vsn: m.config.BuildVsnArray()} 1989 // m.aliveNode(&a, nil, false) 1990 // 1991 // state := m.nodeMap["test"] 1992 // state.StateChange = state.StateChange.Add(-time.Hour) 1993 // 1994 // d := dead{Node: "test", Incarnation: 1} 1995 // m.deadNode(&d) 1996 // 1997 // if state.State != StateAlive { 1998 // t.Fatalf("Bad state") 1999 // } 2000 //} 2001 // 2002 //func TestMemberList_DeadNode_AliveReplay(t *testing.T) { 2003 // m := GetMemberlist(t, nil) 2004 // defer m.Shutdown() 2005 // 2006 // a := alive{Node: "test", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 10, Vsn: m.config.BuildVsnArray()} 2007 // m.aliveNode(&a, nil, false) 2008 // 2009 // d := dead{Node: "test", Incarnation: 10} 2010 // m.deadNode(&d) 2011 // 2012 // // Replay alive at same incarnation 2013 // m.aliveNode(&a, nil, false) 2014 // 2015 // // Should remain dead 2016 // state, ok := m.nodeMap["test"] 2017 // if ok && state.State != StateDead { 2018 // t.Fatalf("Bad state") 2019 // } 2020 //} 2021 // 2022 //func TestMemberList_DeadNode_Refute(t *testing.T) { 2023 // m := GetMemberlist(t, nil) 2024 // defer m.Shutdown() 2025 // 2026 // a := alive{Node: m.config.Name, Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 2027 // m.aliveNode(&a, nil, true) 2028 // 2029 // // Clear queue 2030 // m.broadcasts.Reset() 2031 // 2032 // // Make sure health is in a good state 2033 // if score := m.GetHealthScore(); score != 0 { 2034 // t.Fatalf("bad: %d", score) 2035 // } 2036 // 2037 // d := dead{Node: m.config.Name, Incarnation: 1} 2038 // m.deadNode(&d) 2039 // 2040 // state := m.nodeMap[m.config.Name] 2041 // if state.State != StateAlive { 2042 // t.Fatalf("should still be alive") 2043 // } 2044 // 2045 // // Check a broad cast is queued 2046 // if m.broadcasts.NumQueued() != 1 { 2047 // t.Fatalf("expected only one queued message") 2048 // } 2049 // 2050 // // Should be alive mesg 2051 // if messageType(m.broadcasts.orderedView(true)[0].b.Message()[0]) != aliveMsg { 2052 // t.Fatalf("expected queued alive msg") 2053 // } 2054 // 2055 // // We should have been dinged 2056 // if score := m.GetHealthScore(); score != 1 { 2057 // t.Fatalf("bad: %d", score) 2058 // } 2059 //} 2060 // 2061 //func TestMemberList_MergeState(t *testing.T) { 2062 // m := GetMemberlist(t, nil) 2063 // defer m.Shutdown() 2064 // 2065 // a1 := alive{Node: "test1", Addr: string([]byte{127, 0, 0, 1}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 2066 // m.aliveNode(&a1, nil, false) 2067 // a2 := alive{Node: "test2", Addr: string([]byte{127, 0, 0, 2}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 2068 // m.aliveNode(&a2, nil, false) 2069 // a3 := alive{Node: "test3", Addr: string([]byte{127, 0, 0, 3}), Incarnation: 1, Vsn: m.config.BuildVsnArray()} 2070 // m.aliveNode(&a3, nil, false) 2071 // 2072 // s := suspect{Node: "test1", Incarnation: 1} 2073 // m.suspectNode(&s) 2074 // 2075 // remote := []pushNodeState{ 2076 // pushNodeState{ 2077 // Name: "test1", 2078 // Addr: string([]byte{127, 0, 0, 1}), 2079 // Incarnation: 2, 2080 // State: StateAlive, 2081 // }, 2082 // pushNodeState{ 2083 // Name: "test2", 2084 // Addr: string([]byte{127, 0, 0, 2}), 2085 // Incarnation: 1, 2086 // State: StateSuspect, 2087 // }, 2088 // pushNodeState{ 2089 // Name: "test3", 2090 // Addr: string([]byte{127, 0, 0, 3}), 2091 // Incarnation: 1, 2092 // State: StateDead, 2093 // }, 2094 // pushNodeState{ 2095 // Name: "test4", 2096 // Addr: string([]byte{127, 0, 0, 4}), 2097 // Incarnation: 2, 2098 // State: StateAlive, 2099 // }, 2100 // } 2101 // 2102 // // Listen for changes 2103 // eventCh := make(chan NodeEvent, 1) 2104 // m.config.Events = &ChannelEventDelegate{eventCh} 2105 // 2106 // // Merge remote state 2107 // m.mergeState(remote) 2108 // 2109 // // Check the states 2110 // state := m.nodeMap["test1"] 2111 // if state.State != StateAlive || state.Incarnation != 2 { 2112 // t.Fatalf("Bad state %v", state) 2113 // } 2114 // 2115 // state = m.nodeMap["test2"] 2116 // if state.State != StateSuspect || state.Incarnation != 1 { 2117 // t.Fatalf("Bad state %v", state) 2118 // } 2119 // 2120 // state = m.nodeMap["test3"] 2121 // if state.State != StateSuspect { 2122 // t.Fatalf("Bad state %v", state) 2123 // } 2124 // 2125 // state = m.nodeMap["test4"] 2126 // if state.State != StateAlive || state.Incarnation != 2 { 2127 // t.Fatalf("Bad state %v", state) 2128 // } 2129 // 2130 // // Check the channels 2131 // select { 2132 // case e := <-eventCh: 2133 // if e.Event != NodeJoin || e.Node.Name != "test4" { 2134 // t.Fatalf("bad node %v", e) 2135 // } 2136 // default: 2137 // t.Fatalf("Expect join") 2138 // } 2139 // 2140 // select { 2141 // case e := <-eventCh: 2142 // t.Fatalf("Unexpect event: %v", e) 2143 // default: 2144 // } 2145 //} 2146 // 2147 //func TestMemberlist_Gossip(t *testing.T) { 2148 // ch := make(chan NodeEvent, 3) 2149 // 2150 // addr1 := getBindAddr() 2151 // addr2 := getBindAddr() 2152 // addr3 := getBindAddr() 2153 // ip1 := addr1 2154 // ip2 := addr2 2155 // ip3 := addr3 2156 // 2157 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 2158 // // Set the gossip interval fast enough to get a reasonable test, 2159 // // but slow enough to avoid "sendto: operation not permitted" 2160 // c.GossipInterval = 10 * time.Millisecond 2161 // }) 2162 // defer m1.Shutdown() 2163 // 2164 // bindPort := m1.config.BindPort 2165 // 2166 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 2167 // c.BindPort = bindPort 2168 // c.Events = &ChannelEventDelegate{ch} 2169 // // Set the gossip interval fast enough to get a reasonable test, 2170 // // but slow enough to avoid "sendto: operation not permitted" 2171 // c.GossipInterval = 10 * time.Millisecond 2172 // }) 2173 // defer m2.Shutdown() 2174 // 2175 // m3 := HostMemberlist(addr2.String(), t, func(c *Config) { 2176 // }) 2177 // defer m3.Shutdown() 2178 // 2179 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 2180 // m1.aliveNode(&a1, nil, true) 2181 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 2182 // m1.aliveNode(&a2, nil, false) 2183 // a3 := alive{Node: addr3.String(), Addr: ip3.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m3.config.BuildVsnArray()} 2184 // m1.aliveNode(&a3, nil, false) 2185 // 2186 // // Gossip should send all this to m2. Retry a few times because it's UDP and 2187 // // timing and stuff makes this flaky without. 2188 // retry(t, 15, 250*time.Millisecond, func(failf func(string, ...interface{})) { 2189 // m1.gossip() 2190 // 2191 // time.Sleep(3 * time.Millisecond) 2192 // 2193 // if len(ch) < 3 { 2194 // failf("expected 3 messages from gossip but only got %d", len(ch)) 2195 // } 2196 // }) 2197 //} 2198 // 2199 //func TestMemberlist_GossipToDead(t *testing.T) { 2200 // ch := make(chan NodeEvent, 2) 2201 // 2202 // addr1 := getBindAddr() 2203 // addr2 := getBindAddr() 2204 // ip1 := addr1 2205 // ip2 := addr2 2206 // 2207 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 2208 // c.GossipInterval = time.Millisecond 2209 // c.GossipToTheDeadTime = 100 * time.Millisecond 2210 // }) 2211 // defer m1.Shutdown() 2212 // 2213 // bindPort := m1.config.BindPort 2214 // 2215 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 2216 // c.BindPort = bindPort 2217 // c.Events = &ChannelEventDelegate{ch} 2218 // }) 2219 // 2220 // defer m2.Shutdown() 2221 // 2222 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 2223 // m1.aliveNode(&a1, nil, true) 2224 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 2225 // m1.aliveNode(&a2, nil, false) 2226 // 2227 // // Shouldn't send anything to m2 here, node has been dead for 2x the GossipToTheDeadTime 2228 // m1.nodeMap[addr2.String()].State = StateDead 2229 // m1.nodeMap[addr2.String()].StateChange = time.Now().Add(-200 * time.Millisecond) 2230 // m1.gossip() 2231 // 2232 // select { 2233 // case <-ch: 2234 // t.Fatalf("shouldn't get gossip") 2235 // case <-time.After(50 * time.Millisecond): 2236 // } 2237 // 2238 // // Should gossip to m2 because its state has changed within GossipToTheDeadTime 2239 // m1.nodeMap[addr2.String()].StateChange = time.Now().Add(-20 * time.Millisecond) 2240 // 2241 // retry(t, 5, 10*time.Millisecond, func(failf func(string, ...interface{})) { 2242 // m1.gossip() 2243 // 2244 // time.Sleep(3 * time.Millisecond) 2245 // 2246 // if len(ch) < 2 { 2247 // failf("expected 2 messages from gossip") 2248 // } 2249 // }) 2250 //} 2251 // 2252 //func TestMemberlist_FailedRemote(t *testing.T) { 2253 // type test struct { 2254 // name string 2255 // err error 2256 // expected bool 2257 // } 2258 // tests := []test{ 2259 // {"nil error", nil, false}, 2260 // {"normal error", fmt.Errorf(""), false}, 2261 // {"net.OpError for file", &net.OpError{Net: "file"}, false}, 2262 // {"net.OpError for udp", &net.OpError{Net: "udp"}, false}, 2263 // {"net.OpError for udp4", &net.OpError{Net: "udp4"}, false}, 2264 // {"net.OpError for udp6", &net.OpError{Net: "udp6"}, false}, 2265 // {"net.OpError for tcp", &net.OpError{Net: "tcp"}, false}, 2266 // {"net.OpError for tcp4", &net.OpError{Net: "tcp4"}, false}, 2267 // {"net.OpError for tcp6", &net.OpError{Net: "tcp6"}, false}, 2268 // {"net.OpError for tcp with dial", &net.OpError{Net: "tcp", Op: "dial"}, true}, 2269 // {"net.OpError for tcp with write", &net.OpError{Net: "tcp", Op: "write"}, true}, 2270 // {"net.OpError for tcp with read", &net.OpError{Net: "tcp", Op: "read"}, true}, 2271 // } 2272 // 2273 // for _, test := range tests { 2274 // t.Run(test.name, func(t *testing.T) { 2275 // actual := failedRemote(test.err) 2276 // if actual != test.expected { 2277 // t.Fatalf("expected %t, got %t", test.expected, actual) 2278 // } 2279 // }) 2280 // } 2281 //} 2282 // 2283 //func TestMemberlist_PushPull(t *testing.T) { 2284 // addr1 := getBindAddr() 2285 // addr2 := getBindAddr() 2286 // ip1 := addr1 2287 // ip2 := addr2 2288 // 2289 // ch := make(chan NodeEvent, 3) 2290 // 2291 // m1 := HostMemberlist(addr1.String(), t, func(c *Config) { 2292 // c.GossipInterval = 10 * time.Second 2293 // c.PushPullInterval = time.Millisecond 2294 // }) 2295 // defer m1.Shutdown() 2296 // 2297 // bindPort := m1.config.BindPort 2298 // 2299 // m2 := HostMemberlist(addr2.String(), t, func(c *Config) { 2300 // c.BindPort = bindPort 2301 // c.GossipInterval = 10 * time.Second 2302 // c.Events = &ChannelEventDelegate{ch} 2303 // }) 2304 // defer m2.Shutdown() 2305 // 2306 // a1 := alive{Node: addr1.String(), Addr: ip1.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m1.config.BuildVsnArray()} 2307 // m1.aliveNode(&a1, nil, true) 2308 // a2 := alive{Node: addr2.String(), Addr: ip2.String(), Port: uint16(bindPort), Incarnation: 1, Vsn: m2.config.BuildVsnArray()} 2309 // m1.aliveNode(&a2, nil, false) 2310 // 2311 // // Gossip should send all this to m2. It's UDP though so retry a few times 2312 // retry(t, 5, 10*time.Millisecond, func(failf func(string, ...interface{})) { 2313 // m1.pushPull() 2314 // 2315 // time.Sleep(3 * time.Millisecond) 2316 // 2317 // if len(ch) < 2 { 2318 // failf("expected 2 messages from pushPull") 2319 // } 2320 // }) 2321 //} 2322 // 2323 //func TestVerifyProtocol(t *testing.T) { 2324 // cases := []struct { 2325 // Anodes [][3]uint8 2326 // Bnodes [][3]uint8 2327 // expected bool 2328 // }{ 2329 // // Both running identical everything 2330 // { 2331 // Anodes: [][3]uint8{ 2332 // {0, 0, 0}, 2333 // }, 2334 // Bnodes: [][3]uint8{ 2335 // {0, 0, 0}, 2336 // }, 2337 // expected: true, 2338 // }, 2339 // 2340 // // One can understand newer, but speaking same protocol 2341 // { 2342 // Anodes: [][3]uint8{ 2343 // {0, 0, 0}, 2344 // }, 2345 // Bnodes: [][3]uint8{ 2346 // {0, 1, 0}, 2347 // }, 2348 // expected: true, 2349 // }, 2350 // 2351 // // One is speaking outside the range 2352 // { 2353 // Anodes: [][3]uint8{ 2354 // {0, 0, 0}, 2355 // }, 2356 // Bnodes: [][3]uint8{ 2357 // {1, 1, 1}, 2358 // }, 2359 // expected: false, 2360 // }, 2361 // 2362 // // Transitively outside the range 2363 // { 2364 // Anodes: [][3]uint8{ 2365 // {0, 1, 0}, 2366 // {0, 2, 1}, 2367 // }, 2368 // Bnodes: [][3]uint8{ 2369 // {1, 3, 1}, 2370 // }, 2371 // expected: false, 2372 // }, 2373 // 2374 // // Multi-node 2375 // { 2376 // Anodes: [][3]uint8{ 2377 // {0, 3, 2}, 2378 // {0, 2, 0}, 2379 // }, 2380 // Bnodes: [][3]uint8{ 2381 // {0, 2, 1}, 2382 // {0, 5, 0}, 2383 // }, 2384 // expected: true, 2385 // }, 2386 // } 2387 // 2388 // for _, tc := range cases { 2389 // aCore := make([][6]uint8, len(tc.Anodes)) 2390 // aApp := make([][6]uint8, len(tc.Anodes)) 2391 // for i, n := range tc.Anodes { 2392 // aCore[i] = [6]uint8{n[0], n[1], n[2], 0, 0, 0} 2393 // aApp[i] = [6]uint8{0, 0, 0, n[0], n[1], n[2]} 2394 // } 2395 // 2396 // bCore := make([][6]uint8, len(tc.Bnodes)) 2397 // bApp := make([][6]uint8, len(tc.Bnodes)) 2398 // for i, n := range tc.Bnodes { 2399 // bCore[i] = [6]uint8{n[0], n[1], n[2], 0, 0, 0} 2400 // bApp[i] = [6]uint8{0, 0, 0, n[0], n[1], n[2]} 2401 // } 2402 // 2403 // // Test core protocol verification 2404 // testVerifyProtocolSingle(t, aCore, bCore, tc.expected) 2405 // testVerifyProtocolSingle(t, bCore, aCore, tc.expected) 2406 // 2407 // // Test app protocol verification 2408 // testVerifyProtocolSingle(t, aApp, bApp, tc.expected) 2409 // testVerifyProtocolSingle(t, bApp, aApp, tc.expected) 2410 // } 2411 //} 2412 // 2413 //func testVerifyProtocolSingle(t *testing.T, A [][6]uint8, B [][6]uint8, expect bool) { 2414 // m := GetMemberlist(t, nil) 2415 // defer m.Shutdown() 2416 // 2417 // m.nodes = make([]*nodeState, len(A)) 2418 // for i, n := range A { 2419 // m.nodes[i] = &nodeState{ 2420 // Node: Node{ 2421 // PMin: n[0], 2422 // PMax: n[1], 2423 // PCur: n[2], 2424 // DMin: n[3], 2425 // DMax: n[4], 2426 // DCur: n[5], 2427 // }, 2428 // } 2429 // } 2430 // 2431 // remote := make([]pushNodeState, len(B)) 2432 // for i, n := range B { 2433 // remote[i] = pushNodeState{ 2434 // Name: fmt.Sprintf("node %d", i), 2435 // Vsn: []uint8{n[0], n[1], n[2], n[3], n[4], n[5]}, 2436 // } 2437 // } 2438 // 2439 // err := m.verifyProtocol(remote) 2440 // if (err == nil) != expect { 2441 // t.Fatalf("bad:\nA: %v\nB: %v\nErr: %s", A, B, err) 2442 // } 2443 //}