This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleader_test.go
More file actions
118 lines (93 loc) · 2.77 KB
/
leader_test.go
File metadata and controls
118 lines (93 loc) · 2.77 KB
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
package raftify
import (
"testing"
"time"
)
func TestToLeader(t *testing.T) {
// Reserve ports for this test
ports := reservePorts(1)
// Initialize and start dummy node
node := initDummyNode("TestNode", 1, 1, ports[0])
node.createMemberlist()
// Check state transitions constraints
node.toFollower(0)
node.toLeader()
node.messageTicker.Stop()
if node.state == Leader {
t.Log("Expected node to remain in the Follower state, instead it switched into the Leader state")
t.FailNow()
}
// Switch into Leader state
node.toFollower(0)
node.toPreCandidate()
node.toCandidate()
node.toLeader()
node.messageTicker.Stop()
if node.state != Leader {
t.Logf("Expected node to be in the Leader state, instead got %v", node.state.toString())
t.FailNow()
}
if node.votedFor != "" {
t.Logf("Expected node to not have voted for anyone, instead got %v", node.votedFor)
t.FailNow()
}
// Shutdown node
node.memberlist.Shutdown()
}
func TestRunLeaderTickerCase(t *testing.T) {
// Reserve ports for this test
ports := reservePorts(1)
// Initialize and start dummy node
node := initDummyNode("TestNode", 1, 1, ports[0])
node.quorum = 1
node.heartbeatIDList.received = 1
node.createMemberlist()
defer node.memberlist.Shutdown()
done := make(chan bool)
// Check sub quorum cycles after having reached the quorum
go func() {
node.runLeader()
done <- true
}()
node.messageTicker = time.NewTicker(10 * time.Millisecond)
<-done
if node.heartbeatIDList.subQuorumCycles != 0 {
t.Logf("Expected subQuorumCycles to be reset to 0, instead got %v", node.heartbeatIDList.subQuorumCycles)
t.FailNow()
}
node.messageTicker.Stop()
// Check sub quorum cycles after failing to reach the quorum
node.heartbeatIDList.received = 0
go func() {
node.runLeader()
done <- true
}()
node.messageTicker = time.NewTicker(10 * time.Millisecond)
<-done
if node.heartbeatIDList.subQuorumCycles != 1 {
t.Logf("Expected subQuorumCycles to be 1, instead got %v", node.heartbeatIDList.subQuorumCycles)
t.FailNow()
}
node.messageTicker.Stop()
// Check if maximum sub quorum cycles have been reached
node.heartbeatIDList.received = 0
node.heartbeatIDList.subQuorumCycles = MaxSubQuorumCycles
go func() {
node.runLeader()
done <- true
}()
node.messageTicker = time.NewTicker(10 * time.Millisecond)
<-done
if node.heartbeatIDList.currentHeartbeatID != 0 {
t.Logf("Expected current heartbeat id to be 0, instead got %v", node.heartbeatIDList.currentHeartbeatID)
t.FailNow()
}
if node.heartbeatIDList.subQuorumCycles != 0 {
t.Logf("Expected subQuorumCycles to be reset to 0, instead got %v", node.heartbeatIDList.subQuorumCycles)
t.FailNow()
}
if node.state != Rejoin {
t.Logf("Expected node to be in the Rejoin state, instead got %v", node.state.toString())
t.FailNow()
}
}