-
Notifications
You must be signed in to change notification settings - Fork 1
/
election_test.go
93 lines (82 loc) · 2.32 KB
/
election_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
package election
import (
"fmt"
"testing"
"time"
)
func TestSeungbae(t *testing.T) {
e1, _ := New(&Config{"127.0.0.1:8551", []string{"127.0.0.1:8552"}, nil})
e2, _ := New(&Config{"127.0.0.1:8552", []string{"127.0.0.1:8551"}, nil})
e3, _ := New(&Config{"127.0.0.1:8553", []string{"127.0.0.1:8551", "127.0.0.1:8552"}, nil})
e4, _ := New(&Config{"127.0.0.1:8554", []string{"127.0.0.1:8551", "127.0.0.1:8552", "127.0.0.1:8553"}, nil})
e5, _ := New(&Config{"127.0.0.1:8555", []string{"127.0.0.1:8551", "127.0.0.1:8552", "127.0.0.1:8553", "127.0.0.1:8554"}, nil})
e1.Run()
e2.Run()
go func() {
time.Sleep(5 * time.Second)
fmt.Println("** register node 3 **")
e1.memberlist["127.0.0.1:8553"] = struct{}{}
e2.memberlist["127.0.0.1:8553"] = struct{}{}
e3.Run()
}()
go func() {
time.Sleep(10 * time.Second)
fmt.Println("** register node 4 **")
e1.memberlist["127.0.0.1:8554"] = struct{}{}
e2.memberlist["127.0.0.1:8554"] = struct{}{}
e3.memberlist["127.0.0.1:8554"] = struct{}{}
e4.Run()
}()
go func() {
time.Sleep(15 * time.Second)
fmt.Println("** shutdown Leader **")
if e1.state.role() == Leader {
e1.Stop()
}
if e2.state.role() == Leader {
e2.Stop()
}
}()
go func() {
time.Sleep(20 * time.Second)
fmt.Println("** register node 5 **")
e1.memberlist["127.0.0.1:8555"] = struct{}{}
e2.memberlist["127.0.0.1:8555"] = struct{}{}
e3.memberlist["127.0.0.1:8555"] = struct{}{}
e4.memberlist["127.0.0.1:8555"] = struct{}{}
e5.Run()
}()
go func() {
time.Sleep(25 * time.Second)
fmt.Println("Split brain - node 4")
delete(e4.memberlist, "127.0.0.1:8551")
delete(e4.memberlist, "127.0.0.1:8552")
delete(e4.memberlist, "127.0.0.1:8553")
delete(e4.memberlist, "127.0.0.1:8555")
}()
done := make(chan struct{})
go func() {
time.Sleep(35 * time.Second)
close(done)
}()
for {
select {
case <-done:
e1.Stop()
e2.Stop()
e3.Stop()
e4.Stop()
e5.Stop()
fmt.Println("done")
return
default:
time.Sleep(500 * time.Millisecond)
fmt.Println("e1", e1.state.role().String(), e1.state.term())
fmt.Println("e2", e2.state.role().String(), e2.state.term())
fmt.Println("e3", e3.state.role().String(), e3.state.term())
fmt.Println("e4", e4.state.role().String(), e4.state.term())
fmt.Println("e5", e5.state.role().String(), e5.state.term())
fmt.Println()
}
}
}