raft: Add unit test TestAddNodeCheckQuorum

This test verifies that adding a node does not cause the leader to step
down until at least one full ElectionTick cycle elapses.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2017-05-04 14:58:05 -07:00
parent 52613b262b
commit 9451fa1f9c

View File

@ -2565,6 +2565,41 @@ func TestAddNode(t *testing.T) {
}
}
// TestAddNodeCheckQuorum tests that addNode does not trigger a leader election
// immediately when checkQuorum is set.
func TestAddNodeCheckQuorum(t *testing.T) {
r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage())
r.pendingConf = true
r.checkQuorum = true
r.becomeCandidate()
r.becomeLeader()
for i := 0; i < r.electionTimeout-1; i++ {
r.tick()
}
r.addNode(2)
// This tick will reach electionTimeout, which triggers a quorum check.
r.tick()
// Node 1 should still be the leader after a single tick.
if r.state != StateLeader {
t.Errorf("state = %v, want %v", r.state, StateLeader)
}
// After another electionTimeout ticks without hearing from node 2,
// node 1 should step down.
for i := 0; i < r.electionTimeout; i++ {
r.tick()
}
if r.state != StateFollower {
t.Errorf("state = %v, want %v", r.state, StateFollower)
}
}
// TestRemoveNode tests that removeNode could update pendingConf, nodes and
// and removed list correctly.
func TestRemoveNode(t *testing.T) {