Skip to content

Commit 97f6bd4

Browse files
committedMar 25, 2024
add a partitioned node prevote test
1 parent 62bcfe7 commit 97f6bd4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

Diff for: ‎raft_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,37 @@ func TestRaft_PreVoteMixedCluster(t *testing.T) {
21192119

21202120
}
21212121

2122+
func TestRaft_PreVoteAvoidElectionWithPartition(t *testing.T) {
2123+
// Make a prevote cluster.
2124+
conf := inmemConfig(t)
2125+
conf.PreVote = true
2126+
c := MakeCluster(5, t, conf)
2127+
defer c.Close()
2128+
2129+
oldLeaderTerm := c.Leader().getCurrentTerm()
2130+
followers := c.Followers()
2131+
require.Len(t, followers, 4)
2132+
2133+
//Partition a node and wait enough for it to increase its term
2134+
c.Partition([]ServerAddress{followers[0].localAddr})
2135+
time.Sleep(10 * c.propagateTimeout)
2136+
2137+
// Check the leader is stable and the followers are as expected
2138+
leaderTerm := c.Leader().getCurrentTerm()
2139+
require.Equal(t, leaderTerm, oldLeaderTerm)
2140+
require.Len(t, c.WaitForFollowers(3), 3)
2141+
2142+
// reconnect the partitioned node
2143+
c.FullyConnect()
2144+
time.Sleep(3 * c.propagateTimeout)
2145+
2146+
// Check that the number of followers increase and
2147+
require.Len(t, c.Followers(), 4)
2148+
leaderTerm = c.Leader().getCurrentTerm()
2149+
require.Equal(t, leaderTerm, oldLeaderTerm)
2150+
2151+
}
2152+
21222153
func TestRaft_VotingGrant_WhenLeaderAvailable(t *testing.T) {
21232154
conf := inmemConfig(t)
21242155
conf.ProtocolVersion = 3

Diff for: ‎testing.go

+6
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,12 @@ func (c *cluster) Leader() *Raft {
501501
// state.
502502
func (c *cluster) Followers() []*Raft {
503503
expFollowers := len(c.rafts) - 1
504+
return c.WaitForFollowers(expFollowers)
505+
}
506+
507+
// WaitForFollowers waits for the cluster to have a given number of followers and stay in a stable
508+
// state.
509+
func (c *cluster) WaitForFollowers(expFollowers int) []*Raft {
504510
followers := c.GetInState(Follower)
505511
if len(followers) != expFollowers {
506512
c.t.Fatalf("timeout waiting for %d followers (followers are %v)", expFollowers, followers)

0 commit comments

Comments
 (0)