raft: merge test cases of pre-candidate with the normal one
So result checking just compares the expected with output and becomes more readable.release-3.4
parent
02e2e3def7
commit
3d12e36c7e
|
@ -303,8 +303,15 @@ func TestLeaderElectionPreVote(t *testing.T) {
|
||||||
|
|
||||||
func testLeaderElection(t *testing.T, preVote bool) {
|
func testLeaderElection(t *testing.T, preVote bool) {
|
||||||
var cfg func(*Config)
|
var cfg func(*Config)
|
||||||
|
candState := StateType(StateCandidate)
|
||||||
|
candTerm := uint64(1)
|
||||||
if preVote {
|
if preVote {
|
||||||
cfg = preVoteConfig
|
cfg = preVoteConfig
|
||||||
|
// In pre-vote mode, an election that fails to complete
|
||||||
|
// leaves the node in pre-candidate state without advancing
|
||||||
|
// the term.
|
||||||
|
candState = StatePreCandidate
|
||||||
|
candTerm = 0
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
*network
|
*network
|
||||||
|
@ -313,8 +320,8 @@ func testLeaderElection(t *testing.T, preVote bool) {
|
||||||
}{
|
}{
|
||||||
{newNetworkWithConfig(cfg, nil, nil, nil), StateLeader, 1},
|
{newNetworkWithConfig(cfg, nil, nil, nil), StateLeader, 1},
|
||||||
{newNetworkWithConfig(cfg, nil, nil, nopStepper), StateLeader, 1},
|
{newNetworkWithConfig(cfg, nil, nil, nopStepper), StateLeader, 1},
|
||||||
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper), StateCandidate, 1},
|
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper), candState, candTerm},
|
||||||
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper, nil), StateCandidate, 1},
|
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper, nil), candState, candTerm},
|
||||||
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper, nil, nil), StateLeader, 1},
|
{newNetworkWithConfig(cfg, nil, nopStepper, nopStepper, nil, nil), StateLeader, 1},
|
||||||
|
|
||||||
// three logs further along than 0, but in the same term so rejections
|
// three logs further along than 0, but in the same term so rejections
|
||||||
|
@ -327,23 +334,11 @@ func testLeaderElection(t *testing.T, preVote bool) {
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
|
tt.send(pb.Message{From: 1, To: 1, Type: pb.MsgHup})
|
||||||
sm := tt.network.peers[1].(*raft)
|
sm := tt.network.peers[1].(*raft)
|
||||||
var expState StateType
|
if sm.state != tt.state {
|
||||||
var expTerm uint64
|
t.Errorf("#%d: state = %s, want %s", i, sm.state, tt.state)
|
||||||
if tt.state == StateCandidate && preVote {
|
|
||||||
// In pre-vote mode, an election that fails to complete
|
|
||||||
// leaves the node in pre-candidate state without advancing
|
|
||||||
// the term.
|
|
||||||
expState = StatePreCandidate
|
|
||||||
expTerm = 0
|
|
||||||
} else {
|
|
||||||
expState = tt.state
|
|
||||||
expTerm = tt.expTerm
|
|
||||||
}
|
}
|
||||||
if sm.state != expState {
|
if g := sm.Term; g != tt.expTerm {
|
||||||
t.Errorf("#%d: state = %s, want %s", i, sm.state, expState)
|
t.Errorf("#%d: term = %d, want %d", i, g, tt.expTerm)
|
||||||
}
|
|
||||||
if g := sm.Term; g != expTerm {
|
|
||||||
t.Errorf("#%d: term = %d, want %d", i, g, expTerm)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue