-
Notifications
You must be signed in to change notification settings - Fork 38
/
heartbeat_test.go
309 lines (248 loc) · 8.92 KB
/
heartbeat_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
// Copyright 2013-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graft
import (
"os"
"testing"
"time"
"github.com/nats-io/graft/pb"
)
// Test HeartBeat RPC in different states.
func hbNode(t *testing.T, expected int) *Node {
ci := ClusterInfo{Name: "ae", Size: expected}
hand, rpc, log := genNodeArgs(t)
node, err := New(ci, hand, rpc, log)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
// Delay elections
node.electTimer.Reset(10 * time.Second)
return node
}
func sendAndWait(n *Node, hb *pb.Heartbeat) {
// Any Heartbeat will set the Leader if none given.
n.HeartBeats <- hb
// Wait for AE to be processed.
for len(n.HeartBeats) > 0 {
time.Sleep(10 * time.Millisecond)
}
}
func TestHeartBeatAsFollower(t *testing.T) {
node := hbNode(t, 3)
defer node.Close()
if state := node.State(); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := node.Leader(); curLeader != NO_LEADER {
t.Fatalf("Expected no leader, got: %s\n", curLeader)
}
leader := "leader123"
term := uint64(2)
// Any HeartBeat will set the Leader if none given.
sendAndWait(node, &pb.Heartbeat{Term: term, Leader: leader})
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := waitForLeader(node, leader); curLeader != leader {
t.Fatalf("Expected leader to be %s, got: %s\n", leader, curLeader)
}
// Test persistent state
testStateOfNode(t, node)
// Ignore lower terms when we have a leader already.
// any HeartBeat will set the Leader if none given.
oldLeader := "oldleader"
sendAndWait(node, &pb.Heartbeat{Term: 1, Leader: oldLeader})
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := waitForLeader(node, leader); curLeader != leader {
t.Fatalf("Expected leader to be %s, got: %s\n", leader, curLeader)
}
// Test persistent state
testStateOfNode(t, node)
// A newer term will reset.
newLeader := "newLeader"
newTerm := uint64(10)
sendAndWait(node, &pb.Heartbeat{Term: newTerm, Leader: newLeader})
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := waitForLeader(node, newLeader); curLeader != newLeader {
t.Fatalf("Expected leader to be %s, got: %s\n", newLeader, curLeader)
}
if node.CurrentTerm() != newTerm {
t.Fatalf("Expected CurrentTerm of %d, got: %d\n",
newTerm, node.CurrentTerm())
}
// Test persistent state
testStateOfNode(t, node)
}
func TestHeartBeatAsCandidate(t *testing.T) {
node := hbNode(t, 3)
defer node.Close()
// Speed up switch to Candidate state by shortening the timer.
node.electTimer.Reset(1 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
// Verify new state
if state := node.State(); state != CANDIDATE {
t.Fatalf("Expected Node to be in Candidate state, got: %s", state)
}
if curLeader := node.Leader(); curLeader != NO_LEADER {
t.Fatalf("Expected no leader, got: %s\n", curLeader)
}
// Test persistent state
testStateOfNode(t, node)
// Set term to artificial higher value.
newTerm := uint64(8)
node.setTerm(newTerm)
// Ignore HeartBeat with lower terms.
leader := "leader123"
term := uint64(2)
// Any HeartBeat will set the Leader if none given.
sendAndWait(node, &pb.Heartbeat{Term: term, Leader: leader})
if state := waitForState(node, CANDIDATE); state != CANDIDATE {
t.Fatalf("Expected Node to be in Candidate state, got: %s", state)
}
if curLeader := waitForLeader(node, NO_LEADER); curLeader != NO_LEADER {
t.Fatalf("Expected no leader, got: %s\n", curLeader)
}
// A newer term will reset us to a follower.
newLeader := "newLeader"
newTerm++
sendAndWait(node, &pb.Heartbeat{Term: newTerm, Leader: newLeader})
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := waitForLeader(node, newLeader); curLeader != newLeader {
t.Fatalf("Expected leader to be %s, got: %s\n", newLeader, curLeader)
}
if node.CurrentTerm() != newTerm {
t.Fatalf("Expected CurrentTerm of %d, got: %d\n",
newTerm, node.CurrentTerm())
}
if vote := node.CurrentVote(); vote != NO_VOTE {
t.Fatalf("Expected to have no vote at this point, got %s", node.vote)
}
// Test persistent state
testStateOfNode(t, node)
}
func TestHeartBeatAsLeader(t *testing.T) {
node := hbNode(t, 3)
defer node.Close()
// Set term to artificial higher value.
newTerm := uint64(8)
node.setTerm(newTerm)
// Create fake node to elect the Leader.
fake := fakeNode("fake")
// Hook up to MockRPC layer
mockRegisterPeer(fake)
defer mockUnregisterPeer(fake.id)
node.electTimer.Reset(1 * time.Millisecond)
// Verify new state
if state := waitForState(node, CANDIDATE); state != CANDIDATE {
t.Fatalf("Expected Node to be in Candidate state, got: %s", state)
}
if curLeader := waitForLeader(node, NO_LEADER); curLeader != NO_LEADER {
t.Fatalf("Expected no leader, got: %s\n", curLeader)
}
vreq := <-fake.VoteRequests
// Send Fake VoteResponse to promote node to Leader
node.VoteResponses <- &pb.VoteResponse{Term: vreq.Term, Granted: true}
if state := waitForState(node, LEADER); state != LEADER {
t.Fatalf("Expected Node to be in Leader state, got: %s", state)
}
if curLeader := waitForLeader(node, node.Id()); curLeader != node.Id() {
t.Fatalf("Expected us to be leader, got: %s\n", curLeader)
}
if node.CurrentTerm() != vreq.Term {
t.Fatalf("Expected CurrentTerm of %d, got: %d\n",
vreq.Term, node.CurrentTerm())
}
// Test persistent state
testStateOfNode(t, node)
// Ignore HeartBeat with lower terms.
leader := "leader123"
term := uint64(2)
// Any HeartBeat will set the Leader if none given.
sendAndWait(node, &pb.Heartbeat{Term: term, Leader: leader})
if state := waitForState(node, LEADER); state != LEADER {
t.Fatalf("Expected Node to be in Leader state, got: %s", state)
}
if curLeader := waitForLeader(node, node.Id()); curLeader != node.Id() {
t.Fatalf("Expected us to be leader, got: %s\n", curLeader)
}
// Test persistent state
testStateOfNode(t, node)
// A newer term will reset us to a follower.
newLeader := "newLeader"
newTerm = 20
sendAndWait(node, &pb.Heartbeat{Term: newTerm, Leader: newLeader})
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got: %s", state)
}
if curLeader := waitForLeader(node, newLeader); curLeader != newLeader {
t.Fatalf("Expected leader to be %s, got: %s\n", newLeader, curLeader)
}
if node.CurrentTerm() != newTerm {
t.Fatalf("Expected CurrentTerm of %d, got: %d\n",
newTerm, node.CurrentTerm())
}
if vote := node.CurrentVote(); vote != NO_VOTE {
t.Fatalf("Expected to have no vote at this point, got %s", node.vote)
}
// Test persistent state
testStateOfNode(t, node)
}
func TestHeartBeatAsLeaderErrorOnWrite(t *testing.T) {
node := hbNode(t, 3)
defer node.Close()
// Set term to artificial higher value.
newTerm := uint64(8)
node.setTerm(newTerm)
// Create fake node to elect the Leader.
fake := fakeNode("fake")
// Hook up to MockRPC layer
mockRegisterPeer(fake)
defer mockUnregisterPeer(fake.id)
node.mu.Lock()
node.electTimer.Reset(1 * time.Millisecond)
node.mu.Unlock()
vreq := <-fake.VoteRequests
// Send Fake VoteResponse to promote node to Leader
node.VoteResponses <- &pb.VoteResponse{Term: vreq.Term, Granted: true}
if state := waitForState(node, LEADER); state != LEADER {
t.Fatalf("Expected Node to be in Leader state, got: %s", state)
}
if curLeader := waitForLeader(node, node.Id()); curLeader != node.Id() {
t.Fatalf("Expected us to be leader, got: %s\n", curLeader)
}
if node.CurrentTerm() != vreq.Term {
t.Fatalf("Expected CurrentTerm of %d, got: %d\n",
vreq.Term, node.CurrentTerm())
}
// Test persistent state
testStateOfNode(t, node)
// Change log permissions to cause error
if err := os.Chmod(node.logPath, 0400); err != nil {
t.Fatalf("Unable to change log permissions: %v", err)
}
// Send an heartbeat with higher term, which should cause the current leader
// to step down.
sendAndWait(node, &pb.Heartbeat{Term: newTerm + 10, Leader: "other"})
// Server should stepdown
if state := waitForState(node, FOLLOWER); state != FOLLOWER {
t.Fatalf("Expected Node to be in Follower state, got %s", state)
}
// Reset the permission
os.Chmod(node.logPath, 0660)
}