Skip to content

Commit

Permalink
add test that pre-vote should not reject leader
Browse files Browse the repository at this point in the history
  • Loading branch information
k-jingyang committed Aug 17, 2024
1 parent 6187abb commit 5dca3d1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3270,3 +3270,45 @@ func TestRaft_runFollower_ReloadTimeoutConfigs(t *testing.T) {
// Check the follower loop set the right state
require.Equal(t, Candidate, env.raft.getState())
}

func TestRaft_PreVote_ShouldNotRejectLeader(t *testing.T) {
// Make a cluster
c := MakeCluster(3, t, nil)
defer c.Close()
err := waitForLeader(c)
require.NoError(t, err)
leader := c.Leader()

// Wait until we have 2 followers
limit := time.Now().Add(c.longstopTimeout)
var followers []*Raft
for time.Now().Before(limit) && len(followers) != 2 {
c.WaitEvent(nil, c.conf.CommitTimeout)
followers = c.GetInState(Follower)
}
if len(followers) != 2 {
t.Fatalf("expected two followers: %v", followers)
}

// A follower who thinks that x is the leader should not reject x's pre-vote
follower := followers[0]
require.Equal(t, leader.localAddr, follower.leaderAddr)

reqPrevote := RequestPreVoteRequest{
RPCHeader: leader.getRPCHeader(),
Term: leader.getCurrentTerm() + 1,
LastLogIndex: leader.lastLogIndex,
LastLogTerm: leader.getCurrentTerm(),
}

var resp RequestPreVoteResponse
leaderT := c.trans[c.IndexOf(leader)]
if err := leaderT.RequestPreVote(follower.localID, follower.localAddr, &reqPrevote, &resp); err != nil {
t.Fatalf("RequestPreVote RPC failed %v", err)
}

// the pre-vote should be granted
if !resp.Granted {
t.Fatalf("expected pre-vote to be granted, but it wasn't %+v", resp)
}
}

0 comments on commit 5dca3d1

Please sign in to comment.