This repository has been 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
/
bootstrap_test.go
78 lines (63 loc) · 2.06 KB
/
bootstrap_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
package raftify
import (
"fmt"
"testing"
"time"
)
func TestToBootstrap(t *testing.T) {
// Reserve ports for this test
ports := reservePorts(2)
// Initialize and start dummy node
node := initDummyNode("TestNode", 2, 2, ports[0])
node.createMemberlist()
defer node.memberlist.Shutdown()
// Switch into Bootstrap state, expecting another node to join
node.toBootstrap()
if node.state != Bootstrap {
t.Logf("Expected node to be in the Bootstrap state, instead got %v", node.state.toString())
t.FailNow()
}
// Switch into Bootstrap state, expecting a single node cluster with no peers
node.config.Expect = 1
node.toBootstrap()
if node.state != Leader {
t.Logf("Expected node to be in the Leader state, instead got %v", node.state.toString())
t.FailNow()
}
// Switch into Bootstrap state, expecting a single node cluster with peers
node.config.PeerList = []string{fmt.Sprintf("127.0.0.1:%v", ports[1])}
node.toBootstrap()
if node.state != Follower {
t.Logf("Expected node to be in the Follower state, instead got %v", node.state.toString())
t.FailNow()
}
}
func TestRunBootstrapJoinEventCase(t *testing.T) {
// Reserve ports for this test
ports := reservePorts(2)
// Initialize dummy nodes
node1 := initDummyNode("TestNode_1", 2, 2, ports[0])
node2 := initDummyNode("TestNode_2", 2, 2, ports[1])
node1.config.PeerList = []string{fmt.Sprintf("127.0.0.1:%v", node2.config.BindPort)}
node2.config.PeerList = []string{fmt.Sprintf("127.0.0.1:%v", node1.config.BindPort)}
// Start both nodes
node1.createMemberlist()
node2.createMemberlist()
defer node1.memberlist.Shutdown()
defer node2.memberlist.Shutdown()
// Run runBootstrap in goroutine, so the event case gets triggered on join
go node2.runBootstrap()
// Form a cluster
node2.tryJoin()
select {
case <-node2.bootstrapCh:
if node2.state != Follower {
t.Logf("Expected node2 to be in the Follower state, instead got %v", node2.state.toString())
t.FailNow()
}
break
case <-time.After(6 * time.Second):
t.Log("Expected join event to happen, instead nothing happened")
t.FailNow()
}
}