forked from hashicorp/memberlist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_test.go
1374 lines (1142 loc) · 32.5 KB
/
state_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package memberlist
import (
"bytes"
"fmt"
"net"
"testing"
"time"
)
func HostMemberlist(host string, t *testing.T, f func(*Config)) *Memberlist {
c := DefaultLANConfig()
c.Name = host
c.BindAddr = host
if f != nil {
f(c)
}
m, err := newMemberlist(c)
if err != nil {
t.Fatalf("failed to get memberlist: %s", err)
}
return m
}
func TestMemberList_Probe(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = time.Millisecond
c.ProbeInterval = 10 * time.Millisecond
})
m2 := HostMemberlist(addr2.String(), t, nil)
a1 := alive{
Node: addr1.String(),
Addr: []byte(addr1),
Port: uint16(m1.config.BindPort),
Incarnation: 1,
}
m1.aliveNode(&a1, nil, true)
a2 := alive{
Node: addr2.String(),
Addr: []byte(addr2),
Port: uint16(m2.config.BindPort),
Incarnation: 1,
}
m1.aliveNode(&a2, nil, false)
// should ping addr2
m1.probe()
// Should not be marked suspect
n := m1.nodeMap[addr2.String()]
if n.State != stateAlive {
t.Fatalf("Expect node to be alive")
}
// Should increment seqno
if m1.sequenceNum != 1 {
t.Fatalf("bad seqno %v", m2.sequenceNum)
}
}
func TestMemberList_ProbeNode_Suspect(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
addr3 := getBindAddr()
addr4 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
ip3 := []byte(addr3)
ip4 := []byte(addr4)
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = time.Millisecond
c.ProbeInterval = 10 * time.Millisecond
})
m2 := HostMemberlist(addr2.String(), t, nil)
m3 := HostMemberlist(addr3.String(), t, nil)
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
a3 := alive{Node: addr3.String(), Addr: ip3, Port: 7946, Incarnation: 1}
m1.aliveNode(&a3, nil, false)
a4 := alive{Node: addr4.String(), Addr: ip4, Port: 7946, Incarnation: 1}
m1.aliveNode(&a4, nil, false)
n := m1.nodeMap[addr4.String()]
m1.probeNode(n)
// Should be marked suspect.
if n.State != stateSuspect {
t.Fatalf("Expect node to be suspect")
}
time.Sleep(10 * time.Millisecond)
// One of the peers should have attempted an indirect probe.
if m2.sequenceNum != 1 && m3.sequenceNum != 1 {
t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum)
}
}
func TestMemberList_ProbeNode_FallbackTCP(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
addr3 := getBindAddr()
addr4 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
ip3 := []byte(addr3)
ip4 := []byte(addr4)
var probeTimeMax time.Duration
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = 10 * time.Millisecond
c.ProbeInterval = 200 * time.Millisecond
probeTimeMax = c.ProbeInterval + 20*time.Millisecond
})
defer m1.Shutdown()
m2 := HostMemberlist(addr2.String(), t, nil)
defer m2.Shutdown()
m3 := HostMemberlist(addr3.String(), t, nil)
defer m3.Shutdown()
m4 := HostMemberlist(addr4.String(), t, nil)
defer m4.Shutdown()
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
a3 := alive{Node: addr3.String(), Addr: ip3, Port: 7946, Incarnation: 1}
m1.aliveNode(&a3, nil, false)
// Make sure m4 is configured with the same protocol version as m1 so
// the TCP fallback behavior is enabled.
a4 := alive{
Node: addr4.String(),
Addr: ip4,
Port: 7946,
Incarnation: 1,
Vsn: []uint8{
ProtocolVersionMin,
ProtocolVersionMax,
m1.config.ProtocolVersion,
m1.config.DelegateProtocolMin,
m1.config.DelegateProtocolMax,
m1.config.DelegateProtocolVersion,
},
}
m1.aliveNode(&a4, nil, false)
// Isolate m4 from UDP traffic by re-opening its listener on the wrong
// port. This should force the TCP fallback path to be used.
var err error
if err = m4.udpListener.Close(); err != nil {
t.Fatalf("err: %v", err)
}
udpAddr := &net.UDPAddr{IP: ip4, Port: 9999}
if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil {
t.Fatalf("err: %v", err)
}
// Have node m1 probe m4.
n := m1.nodeMap[addr4.String()]
startProbe := time.Now()
m1.probeNode(n)
probeTime := time.Now().Sub(startProbe)
// Should be marked alive because of the TCP fallback ping.
if n.State != stateAlive {
t.Fatalf("expect node to be alive")
}
// Make sure TCP activity completed in a timely manner.
if probeTime > probeTimeMax {
t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds())
}
// Confirm at least one of the peers attempted an indirect probe.
time.Sleep(probeTimeMax)
if m2.sequenceNum != 1 && m3.sequenceNum != 1 {
t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum)
}
// Now shutdown all inbound TCP traffic to make sure the TCP fallback
// path properly fails when the node is really unreachable.
if err = m4.tcpListener.Close(); err != nil {
t.Fatalf("err: %v", err)
}
tcpAddr := &net.TCPAddr{IP: ip4, Port: 9999}
if m4.tcpListener, err = net.ListenTCP("tcp", tcpAddr); err != nil {
t.Fatalf("err: %v", err)
}
// Probe again, this time there should be no contact.
startProbe = time.Now()
m1.probeNode(n)
probeTime = time.Now().Sub(startProbe)
// Node should be reported suspect.
if n.State != stateSuspect {
t.Fatalf("expect node to be suspect")
}
// Make sure TCP activity didn't cause us to wait too long before
// timing out.
if probeTime > probeTimeMax {
t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds())
}
// Confirm at least one of the peers attempted an indirect probe.
time.Sleep(probeTimeMax)
if m2.sequenceNum != 2 && m3.sequenceNum != 2 {
t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum)
}
}
func TestMemberList_ProbeNode_FallbackTCP_Disabled(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
addr3 := getBindAddr()
addr4 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
ip3 := []byte(addr3)
ip4 := []byte(addr4)
var probeTimeMax time.Duration
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = 10 * time.Millisecond
c.ProbeInterval = 200 * time.Millisecond
probeTimeMax = c.ProbeInterval + 20*time.Millisecond
})
defer m1.Shutdown()
m2 := HostMemberlist(addr2.String(), t, nil)
defer m2.Shutdown()
m3 := HostMemberlist(addr3.String(), t, nil)
defer m3.Shutdown()
m4 := HostMemberlist(addr4.String(), t, nil)
defer m4.Shutdown()
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
a3 := alive{Node: addr3.String(), Addr: ip3, Port: 7946, Incarnation: 1}
m1.aliveNode(&a3, nil, false)
// Make sure m4 is configured with the same protocol version as m1 so
// the TCP fallback behavior is enabled.
a4 := alive{
Node: addr4.String(),
Addr: ip4,
Port: 7946,
Incarnation: 1,
Vsn: []uint8{
ProtocolVersionMin,
ProtocolVersionMax,
m1.config.ProtocolVersion,
m1.config.DelegateProtocolMin,
m1.config.DelegateProtocolMax,
m1.config.DelegateProtocolVersion,
},
}
m1.aliveNode(&a4, nil, false)
// Isolate m4 from UDP traffic by re-opening its listener on the wrong
// port. This should force the TCP fallback path to be used.
var err error
if err = m4.udpListener.Close(); err != nil {
t.Fatalf("err: %v", err)
}
udpAddr := &net.UDPAddr{IP: ip4, Port: 9999}
if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil {
t.Fatalf("err: %v", err)
}
// Disable the TCP pings using the config mechanism.
m1.config.DisableTcpPings = true
// Have node m1 probe m4.
n := m1.nodeMap[addr4.String()]
startProbe := time.Now()
m1.probeNode(n)
probeTime := time.Now().Sub(startProbe)
// Node should be reported suspect.
if n.State != stateSuspect {
t.Fatalf("expect node to be suspect")
}
// Make sure TCP activity didn't cause us to wait too long before
// timing out.
if probeTime > probeTimeMax {
t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds())
}
// Confirm at least one of the peers attempted an indirect probe.
time.Sleep(probeTimeMax)
if m2.sequenceNum != 1 && m3.sequenceNum != 1 {
t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum)
}
}
func TestMemberList_ProbeNode_FallbackTCP_OldProtocol(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
addr3 := getBindAddr()
addr4 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
ip3 := []byte(addr3)
ip4 := []byte(addr4)
var probeTimeMax time.Duration
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = 10 * time.Millisecond
c.ProbeInterval = 200 * time.Millisecond
probeTimeMax = c.ProbeInterval + 20*time.Millisecond
})
defer m1.Shutdown()
m2 := HostMemberlist(addr2.String(), t, nil)
defer m2.Shutdown()
m3 := HostMemberlist(addr3.String(), t, nil)
defer m3.Shutdown()
m4 := HostMemberlist(addr4.String(), t, nil)
defer m4.Shutdown()
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
a3 := alive{Node: addr3.String(), Addr: ip3, Port: 7946, Incarnation: 1}
m1.aliveNode(&a3, nil, false)
// Set up m4 so that it doesn't understand a version of the protocol
// that supports TCP pings.
a4 := alive{
Node: addr4.String(),
Addr: ip4,
Port: 7946,
Incarnation: 1,
Vsn: []uint8{
ProtocolVersionMin,
ProtocolVersion2Compatible,
ProtocolVersion2Compatible,
m1.config.DelegateProtocolMin,
m1.config.DelegateProtocolMax,
m1.config.DelegateProtocolVersion,
},
}
m1.aliveNode(&a4, nil, false)
// Isolate m4 from UDP traffic by re-opening its listener on the wrong
// port. This should force the TCP fallback path to be used.
var err error
if err = m4.udpListener.Close(); err != nil {
t.Fatalf("err: %v", err)
}
udpAddr := &net.UDPAddr{IP: ip4, Port: 9999}
if m4.udpListener, err = net.ListenUDP("udp", udpAddr); err != nil {
t.Fatalf("err: %v", err)
}
// Have node m1 probe m4.
n := m1.nodeMap[addr4.String()]
startProbe := time.Now()
m1.probeNode(n)
probeTime := time.Now().Sub(startProbe)
// Node should be reported suspect.
if n.State != stateSuspect {
t.Fatalf("expect node to be suspect")
}
// Make sure TCP activity didn't cause us to wait too long before
// timing out.
if probeTime > probeTimeMax {
t.Fatalf("took to long to probe, %9.6f", probeTime.Seconds())
}
// Confirm at least one of the peers attempted an indirect probe.
time.Sleep(probeTimeMax)
if m2.sequenceNum != 1 && m3.sequenceNum != 1 {
t.Fatalf("bad seqnos %v, %v", m2.sequenceNum, m3.sequenceNum)
}
}
func TestMemberList_ProbeNode(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = time.Millisecond
c.ProbeInterval = 10 * time.Millisecond
})
_ = HostMemberlist(addr2.String(), t, nil)
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
n := m1.nodeMap[addr2.String()]
m1.probeNode(n)
// Should be marked alive
if n.State != stateAlive {
t.Fatalf("Expect node to be alive")
}
// Should increment seqno
if m1.sequenceNum != 1 {
t.Fatalf("bad seqno %v", m1.sequenceNum)
}
}
func TestMemberList_Ping(t *testing.T) {
addr1 := getBindAddr()
addr2 := getBindAddr()
ip1 := []byte(addr1)
ip2 := []byte(addr2)
m1 := HostMemberlist(addr1.String(), t, func(c *Config) {
c.ProbeTimeout = time.Millisecond
c.ProbeInterval = 10 * time.Second
})
_ = HostMemberlist(addr2.String(), t, nil)
a1 := alive{Node: addr1.String(), Addr: ip1, Port: 7946, Incarnation: 1}
m1.aliveNode(&a1, nil, true)
a2 := alive{Node: addr2.String(), Addr: ip2, Port: 7946, Incarnation: 1}
m1.aliveNode(&a2, nil, false)
// Do a legit ping.
n := m1.nodeMap[addr2.String()]
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(addr2.String(), "7946"))
if err != nil {
t.Fatalf("err: %v", err)
}
rtt, err := m1.Ping(n.Name, addr)
if err != nil {
t.Fatalf("err: %v", err)
}
if !(rtt > 0) {
t.Fatalf("bad: %v", rtt)
}
// This ping has a bad node name so should timeout.
_, err = m1.Ping("bad", addr)
if _, ok := err.(NoPingResponseError); !ok || err == nil {
t.Fatalf("bad: %v", err)
}
}
func TestMemberList_ResetNodes(t *testing.T) {
m := GetMemberlist(t)
a1 := alive{Node: "test1", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a1, nil, false)
a2 := alive{Node: "test2", Addr: []byte{127, 0, 0, 2}, Incarnation: 1}
m.aliveNode(&a2, nil, false)
a3 := alive{Node: "test3", Addr: []byte{127, 0, 0, 3}, Incarnation: 1}
m.aliveNode(&a3, nil, false)
d := dead{Node: "test2", Incarnation: 1}
m.deadNode(&d)
m.resetNodes()
if len(m.nodes) != 2 {
t.Fatalf("Bad length")
}
if _, ok := m.nodeMap["test2"]; ok {
t.Fatalf("test2 should be unmapped")
}
}
func TestMemberList_NextSeq(t *testing.T) {
m := &Memberlist{}
if m.nextSeqNo() != 1 {
t.Fatalf("bad sequence no")
}
if m.nextSeqNo() != 2 {
t.Fatalf("bad sequence no")
}
}
func TestMemberList_SetAckChannel(t *testing.T) {
m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)}
ch := make(chan ackMessage, 1)
m.setAckChannel(0, ch, 10*time.Millisecond)
if _, ok := m.ackHandlers[0]; !ok {
t.Fatalf("missing handler")
}
time.Sleep(20 * time.Millisecond)
if _, ok := m.ackHandlers[0]; ok {
t.Fatalf("non-reaped handler")
}
}
func TestMemberList_SetAckHandler(t *testing.T) {
m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)}
f := func([]byte, time.Time) {}
m.setAckHandler(0, f, 10*time.Millisecond)
if _, ok := m.ackHandlers[0]; !ok {
t.Fatalf("missing handler")
}
time.Sleep(20 * time.Millisecond)
if _, ok := m.ackHandlers[0]; ok {
t.Fatalf("non-reaped handler")
}
}
func TestMemberList_InvokeAckHandler(t *testing.T) {
m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)}
// Does nothing
m.invokeAckHandler(ackResp{}, time.Now())
var b bool
f := func(payload []byte, timestamp time.Time) { b = true }
m.setAckHandler(0, f, 10*time.Millisecond)
// Should set b
m.invokeAckHandler(ackResp{0, nil}, time.Now())
if !b {
t.Fatalf("b not set")
}
if _, ok := m.ackHandlers[0]; ok {
t.Fatalf("non-reaped handler")
}
}
func TestMemberList_InvokeAckHandler_Channel(t *testing.T) {
m := &Memberlist{ackHandlers: make(map[uint32]*ackHandler)}
ack := ackResp{0, []byte{0, 0, 0}}
// Does nothing
m.invokeAckHandler(ack, time.Now())
ch := make(chan ackMessage, 1)
m.setAckChannel(0, ch, 10*time.Millisecond)
// Should send message
m.invokeAckHandler(ack, time.Now())
select {
case v := <-ch:
if v.Complete != true {
t.Fatalf("Bad value")
}
if bytes.Compare(v.Payload, ack.Payload) != 0 {
t.Fatalf("wrong payload. expected: %v; actual: %v", ack.Payload, v.Payload)
}
default:
t.Fatalf("message not sent")
}
if _, ok := m.ackHandlers[0]; ok {
t.Fatalf("non-reaped handler")
}
}
func TestMemberList_AliveNode_NewNode(t *testing.T) {
ch := make(chan NodeEvent, 1)
m := GetMemberlist(t)
m.config.Events = &ChannelEventDelegate{ch}
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
if len(m.nodes) != 1 {
t.Fatalf("should add node")
}
state, ok := m.nodeMap["test"]
if !ok {
t.Fatalf("should map node")
}
if state.Incarnation != 1 {
t.Fatalf("bad incarnation")
}
if state.State != stateAlive {
t.Fatalf("bad state")
}
if time.Now().Sub(state.StateChange) > time.Second {
t.Fatalf("bad change delta")
}
// Check for a join message
select {
case e := <-ch:
if e.Node.Name != "test" {
t.Fatalf("bad node name")
}
default:
t.Fatalf("no join message")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected queued message")
}
}
func TestMemberList_AliveNode_SuspectNode(t *testing.T) {
ch := make(chan NodeEvent, 1)
m := GetMemberlist(t)
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
// Listen only after first join
m.config.Events = &ChannelEventDelegate{ch}
// Make suspect
state := m.nodeMap["test"]
state.State = stateSuspect
state.StateChange = state.StateChange.Add(-time.Hour)
// Old incarnation number, should not change
m.aliveNode(&a, nil, false)
if state.State != stateSuspect {
t.Fatalf("update with old incarnation!")
}
// Should reset to alive now
a.Incarnation = 2
m.aliveNode(&a, nil, false)
if state.State != stateAlive {
t.Fatalf("no update with new incarnation!")
}
if time.Now().Sub(state.StateChange) > time.Second {
t.Fatalf("bad change delta")
}
// Check for a no join message
select {
case <-ch:
t.Fatalf("got bad join message")
default:
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected queued message")
}
}
func TestMemberList_AliveNode_Idempotent(t *testing.T) {
ch := make(chan NodeEvent, 1)
m := GetMemberlist(t)
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
// Listen only after first join
m.config.Events = &ChannelEventDelegate{ch}
// Make suspect
state := m.nodeMap["test"]
stateTime := state.StateChange
// Should reset to alive now
a.Incarnation = 2
m.aliveNode(&a, nil, false)
if state.State != stateAlive {
t.Fatalf("non idempotent")
}
if stateTime != state.StateChange {
t.Fatalf("should not change state")
}
// Check for a no join message
select {
case <-ch:
t.Fatalf("got bad join message")
default:
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected only one queued message")
}
}
// Serf Bug: GH-58, Meta data does not update
func TestMemberList_AliveNode_ChangeMeta(t *testing.T) {
ch := make(chan NodeEvent, 1)
m := GetMemberlist(t)
a := alive{
Node: "test",
Addr: []byte{127, 0, 0, 1},
Meta: []byte("val1"),
Incarnation: 1}
m.aliveNode(&a, nil, false)
// Listen only after first join
m.config.Events = &ChannelEventDelegate{ch}
// Make suspect
state := m.nodeMap["test"]
// Should reset to alive now
a.Incarnation = 2
a.Meta = []byte("val2")
m.aliveNode(&a, nil, false)
// Check updates
if bytes.Compare(state.Meta, a.Meta) != 0 {
t.Fatalf("meta did not update")
}
// Check for a NotifyUpdate
select {
case e := <-ch:
if e.Event != NodeUpdate {
t.Fatalf("bad event: %v", e)
}
if e.Node != &state.Node {
t.Fatalf("bad event: %v", e)
}
if bytes.Compare(e.Node.Meta, a.Meta) != 0 {
t.Fatalf("meta did not update")
}
default:
t.Fatalf("missing event!")
}
}
func TestMemberList_AliveNode_Refute(t *testing.T) {
m := GetMemberlist(t)
a := alive{Node: m.config.Name, Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, true)
// Clear queue
m.broadcasts.Reset()
// Conflicting alive
s := alive{
Node: m.config.Name,
Addr: []byte{127, 0, 0, 1},
Incarnation: 2,
Meta: []byte("foo"),
}
m.aliveNode(&s, nil, false)
state := m.nodeMap[m.config.Name]
if state.State != stateAlive {
t.Fatalf("should still be alive")
}
if state.Meta != nil {
t.Fatalf("meta should still be nil")
}
// Check a broad cast is queued
if num := m.broadcasts.NumQueued(); num != 1 {
t.Fatalf("expected only one queued message: %d",
num)
}
// Should be alive mesg
if messageType(m.broadcasts.bcQueue[0].b.Message()[0]) != aliveMsg {
t.Fatalf("expected queued alive msg")
}
}
func TestMemberList_SuspectNode_NoNode(t *testing.T) {
m := GetMemberlist(t)
s := suspect{Node: "test", Incarnation: 1}
m.suspectNode(&s)
if len(m.nodes) != 0 {
t.Fatalf("don't expect nodes")
}
}
func TestMemberList_SuspectNode(t *testing.T) {
m := GetMemberlist(t)
m.config.ProbeInterval = time.Millisecond
m.config.SuspicionMult = 1
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
state := m.nodeMap["test"]
state.StateChange = state.StateChange.Add(-time.Hour)
s := suspect{Node: "test", Incarnation: 1}
m.suspectNode(&s)
if state.State != stateSuspect {
t.Fatalf("Bad state")
}
change := state.StateChange
if time.Now().Sub(change) > time.Second {
t.Fatalf("bad change delta")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected only one queued message")
}
// Check its a suspect message
if messageType(m.broadcasts.bcQueue[0].b.Message()[0]) != suspectMsg {
t.Fatalf("expected queued suspect msg")
}
// Wait for the timeout
time.Sleep(10 * time.Millisecond)
if state.State != stateDead {
t.Fatalf("Bad state")
}
if time.Now().Sub(state.StateChange) > time.Second {
t.Fatalf("bad change delta")
}
if !state.StateChange.After(change) {
t.Fatalf("should increment time")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected only one queued message")
}
// Check its a suspect message
if messageType(m.broadcasts.bcQueue[0].b.Message()[0]) != deadMsg {
t.Fatalf("expected queued dead msg")
}
}
func TestMemberList_SuspectNode_DoubleSuspect(t *testing.T) {
m := GetMemberlist(t)
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
state := m.nodeMap["test"]
state.StateChange = state.StateChange.Add(-time.Hour)
s := suspect{Node: "test", Incarnation: 1}
m.suspectNode(&s)
if state.State != stateSuspect {
t.Fatalf("Bad state")
}
change := state.StateChange
if time.Now().Sub(change) > time.Second {
t.Fatalf("bad change delta")
}
// clear the broadcast queue
m.broadcasts.Reset()
// Suspect again
m.suspectNode(&s)
if state.StateChange != change {
t.Fatalf("unexpected state change")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 0 {
t.Fatalf("expected only one queued message")
}
}
func TestMemberList_SuspectNode_OldSuspect(t *testing.T) {
m := GetMemberlist(t)
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 10}
m.aliveNode(&a, nil, false)
state := m.nodeMap["test"]
state.StateChange = state.StateChange.Add(-time.Hour)
// Clear queue
m.broadcasts.Reset()
s := suspect{Node: "test", Incarnation: 1}
m.suspectNode(&s)
if state.State != stateAlive {
t.Fatalf("Bad state")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 0 {
t.Fatalf("expected only one queued message")
}
}
func TestMemberList_SuspectNode_Refute(t *testing.T) {
m := GetMemberlist(t)
a := alive{Node: m.config.Name, Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, true)
// Clear queue
m.broadcasts.Reset()
s := suspect{Node: m.config.Name, Incarnation: 1}
m.suspectNode(&s)
state := m.nodeMap[m.config.Name]
if state.State != stateAlive {
t.Fatalf("should still be alive")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected only one queued message")
}
// Should be alive mesg
if messageType(m.broadcasts.bcQueue[0].b.Message()[0]) != aliveMsg {
t.Fatalf("expected queued alive msg")
}
}
func TestMemberList_DeadNode_NoNode(t *testing.T) {
m := GetMemberlist(t)
d := dead{Node: "test", Incarnation: 1}
m.deadNode(&d)
if len(m.nodes) != 0 {
t.Fatalf("don't expect nodes")
}
}
func TestMemberList_DeadNode(t *testing.T) {
ch := make(chan NodeEvent, 1)
m := GetMemberlist(t)
m.config.Events = &ChannelEventDelegate{ch}
a := alive{Node: "test", Addr: []byte{127, 0, 0, 1}, Incarnation: 1}
m.aliveNode(&a, nil, false)
// Read the join event
<-ch
state := m.nodeMap["test"]
state.StateChange = state.StateChange.Add(-time.Hour)
d := dead{Node: "test", Incarnation: 1}
m.deadNode(&d)
if state.State != stateDead {
t.Fatalf("Bad state")
}
change := state.StateChange
if time.Now().Sub(change) > time.Second {
t.Fatalf("bad change delta")
}
select {
case leave := <-ch:
if leave.Event != NodeLeave || leave.Node.Name != "test" {
t.Fatalf("bad node name")
}
default:
t.Fatalf("no leave message")
}
// Check a broad cast is queued
if m.broadcasts.NumQueued() != 1 {
t.Fatalf("expected only one queued message")
}
// Check its a suspect message
if messageType(m.broadcasts.bcQueue[0].b.Message()[0]) != deadMsg {
t.Fatalf("expected queued dead msg")
}
}
func TestMemberList_DeadNode_Double(t *testing.T) {