-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test.go
121 lines (94 loc) · 2.28 KB
/
raft_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
package raft
import (
"testing"
"time"
)
func GetRaft(t *testing.T) *Raft {
conf := DefaultConf()
conf.Name = "Machine 1"
conf.ElectionTime = 300
for i := 0; i < 100; i++ {
r, err := Init(conf)
if err == nil {
return r
}
conf.BindTCPPort++
conf.BindUDPPort++
}
t.Fatalf("Couldn't create the raft node")
return nil
}
func JoinRaft(t *testing.T, name string, port int) *Raft {
conf := DefaultConf()
conf.Name = name
conf.ElectionTime = 300
conf.BindTCPPort = 2222
conf.BindUDPPort = 2222
for i := 0; i < 100; i++ {
r, err := Join(conf, "127.0.0.1", port)
if err == nil {
return r
}
conf.BindTCPPort++
conf.BindUDPPort++
}
t.Fatalf("Couldn't create the raft node")
return nil
}
func TestRaft_CreateShutdown(t *testing.T) {
r := GetRaft(t)
// defer r.Shutdown()
if r.self.state != Follower {
t.Fatalf("On init the state should be 'Follower' = 0, instead: %d", r.self.state)
}
if r.self.term != 0 {
t.Fatalf("On init the term state should be 0, instead: %d", r.self.term)
}
time.Sleep(time.Duration(r.config.ElectionTime)*time.Millisecond + 50*time.Millisecond)
if r.self.state != Candidate {
t.Fatalf("Election timeout fired, the state should be 'Candidate' = 1, instead: %d", r.self.state)
}
}
func TestRaft_Join(t *testing.T) {
r1 := GetRaft(t)
// defer r1.Shutdown()
r2 := JoinRaft(t, "Machine 2", r1.config.BindUDPPort)
// defer r2.Shutdown()
t.Log("Successfully created the instances")
time.Sleep(2000 * time.Millisecond)
node1 := r1.nodeMap[r2.config.Name]
node2 := r2.nodeMap[r1.config.Name]
if node1.Name != r2.config.Name {
t.Fatalf("Failed to join nodes")
}
if node2.Name != r1.config.Name {
t.Fatalf("Failed to join nodes")
}
if r2.self.leaderID != r1.config.Name {
t.Fatalf("Failed to achieve the leadership: %s vs %s", r2.self.leaderID, r1.config.Name)
}
}
func TestRaft_Nodes(t *testing.T) {
r := GetRaft(t)
n1 := &Node{
Addr: nil,
Name: "Machine 2",
VoteStatus: Unknown,
}
n2 := &Node{
Addr: nil,
Name: "Machine 3",
VoteStatus: Unknown,
}
n3 := &Node{
Addr: nil,
Name: "Machine 4",
VoteStatus: Unknown,
}
r.addNode(n1)
r.addNode(n2)
r.addNode(n3)
if len(r.Nodes()) != 3 {
t.Fatalf("The length of Nodes should be 3, but instead: %d", len(r.Nodes()))
}
}