|
|
@@ -10,7 +10,8 @@ const (
|
|
|
)
|
|
|
|
|
|
func TestTickMsgHub(t *testing.T) {
|
|
|
- n := New(0, []int{0, 1, 2}, defaultHeartbeat, defaultElection)
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.sm = newStateMachine(0, []int{0, 1, 2})
|
|
|
|
|
|
for i := 0; i < defaultElection+1; i++ {
|
|
|
n.Tick()
|
|
|
@@ -30,7 +31,8 @@ func TestTickMsgHub(t *testing.T) {
|
|
|
|
|
|
func TestTickMsgBeat(t *testing.T) {
|
|
|
k := 3
|
|
|
- n := New(0, []int{0, 1, 2}, defaultHeartbeat, defaultElection)
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.sm = newStateMachine(0, []int{0, 1, 2})
|
|
|
|
|
|
n.Step(Message{Type: msgHup}) // become leader please
|
|
|
for _, m := range n.Msgs() {
|
|
|
@@ -70,7 +72,8 @@ func TestResetElapse(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
- n := New(0, []int{0, 1, 2}, defaultHeartbeat, defaultElection)
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.sm = newStateMachine(0, []int{0, 1, 2})
|
|
|
n.sm.term = 2
|
|
|
|
|
|
n.Tick()
|
|
|
@@ -85,24 +88,38 @@ func TestResetElapse(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func TestAdd(t *testing.T) {
|
|
|
- n := New(0, []int{0}, defaultHeartbeat, defaultElection)
|
|
|
+func TestStartCluster(t *testing.T) {
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.StartCluster()
|
|
|
|
|
|
- n.sm.becomeCandidate()
|
|
|
- n.sm.becomeLeader()
|
|
|
+ if len(n.sm.ins) != 1 {
|
|
|
+ t.Errorf("k = %d, want 1", len(n.sm.ins))
|
|
|
+ }
|
|
|
+ if n.sm.addr != 0 {
|
|
|
+ t.Errorf("addr = %d, want 0", n.sm.addr)
|
|
|
+ }
|
|
|
+ if n.sm.state != stateLeader {
|
|
|
+ t.Errorf("state = %s, want %s", n.sm.state, stateLeader)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestAdd(t *testing.T) {
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.StartCluster()
|
|
|
n.Add(1)
|
|
|
n.Next()
|
|
|
|
|
|
if len(n.sm.ins) != 2 {
|
|
|
t.Errorf("k = %d, want 2", len(n.sm.ins))
|
|
|
}
|
|
|
+ if n.sm.addr != 0 {
|
|
|
+ t.Errorf("addr = %d, want 0", n.sm.addr)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
- n := New(0, []int{0}, defaultHeartbeat, defaultElection)
|
|
|
-
|
|
|
- n.sm.becomeCandidate()
|
|
|
- n.sm.becomeLeader()
|
|
|
+ n := New(0, defaultHeartbeat, defaultElection)
|
|
|
+ n.StartCluster()
|
|
|
n.Add(1)
|
|
|
n.Next()
|
|
|
n.Remove(0)
|