-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test.go
67 lines (47 loc) · 1.77 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
package raft
import (
"testing"
)
func Test1(t *testing.T) { // Simple Leader Election
cluster := NewCluster(t, 5)
defer cluster.Shutdown()
sleepMs(3000) // Wait for a leader to be elected
firstLeaderId := cluster.getClusterLeader()
cluster.DisconnectPeer(firstLeaderId)
secondLeaderId := cluster.getClusterLeader()
cluster.DisconnectPeer(secondLeaderId)
thirdLeaderId := cluster.getClusterLeader()
cluster.DisconnectPeer(thirdLeaderId)
sleepMs(3000)
// Fails, no leader present
cluster.getClusterLeader()
sleepMs(3000)
}
func Test2(t *testing.T) {
/* Replication failure scenario: Leader drops after committing, comes back later*/
cluster := NewCluster(t, 5)
defer cluster.Shutdown()
// ReceiveClientCommand a couple of values to a fully connected nodes.
origLeaderId := cluster.getClusterLeader()
cluster.SubmitClientCommand(origLeaderId, "Set X = 5")
cluster.SubmitClientCommand(origLeaderId, "Set X = 1000")
sleepMs(3000)
// Leader disconnected...
cluster.DisconnectPeer(origLeaderId)
// ReceiveClientCommand 7 to original leader, even though it's disconnected. Should not reflect.
cluster.SubmitClientCommand(origLeaderId, "Set X = X-5")
newLeaderId := cluster.getClusterLeader()
// ReceiveClientCommand 8.. to new leader.
cluster.SubmitClientCommand(newLeaderId, "Set X = X+10")
cluster.SubmitClientCommand(newLeaderId, "Set X = X+1")
cluster.SubmitClientCommand(newLeaderId, "Set Y = 5")
cluster.SubmitClientCommand(newLeaderId, "Set Y = X+Y")
cluster.SubmitClientCommand(newLeaderId, "Set Y = Y+3")
cluster.SubmitClientCommand(newLeaderId, "Set Z = -1")
sleepMs(3000)
// ReceiveClientCommand 9 and check it's fully committed.
cluster.SubmitClientCommand(newLeaderId, "Set Z = 3")
sleepMs(3000)
cluster.ReconnectPeer(origLeaderId)
sleepMs(15000)
}