@@ -188,7 +188,7 @@ public void testJoinWithHigherAcceptedTerm() {
188188 containsString ("higher than current last accepted term" ));
189189 }
190190
191- public void testJoinWithHigherVersion () {
191+ public void testJoinWithSameAcceptedTermButHigherVersion () {
192192 VotingConfiguration initialConfig = new VotingConfiguration (Collections .singleton (node1 .getId ()));
193193 ClusterState state1 = clusterState (0L , 1L , node1 , initialConfig , initialConfig , 42L );
194194 cs1 .setInitialState (state1 );
@@ -206,7 +206,7 @@ public void testJoinWithHigherVersion() {
206206 containsString ("higher than current last accepted version" ));
207207 }
208208
209- public void testJoinWinsElection () {
209+ public void testJoinWithLowerLastAcceptedTermWinsElection () {
210210 VotingConfiguration initialConfig = new VotingConfiguration (Collections .singleton (node1 .getId ()));
211211 ClusterState state1 = clusterState (0L , 1L , node1 , initialConfig , initialConfig , 42L );
212212 cs1 .setInitialState (state1 );
@@ -218,9 +218,28 @@ public void testJoinWinsElection() {
218218 StartJoinRequest startJoinRequest2 = new StartJoinRequest (node2 , randomLongBetween (startJoinRequest1 .getTerm () + 1 , 10 ));
219219 Join v1 = cs1 .handleStartJoin (startJoinRequest2 );
220220
221- boolean sameTermButBetterVersion = randomBoolean ();
222- Join join = new Join (node1 , node1 , v1 .getTerm (), sameTermButBetterVersion ? state2 .term () : randomLongBetween (0 , state2 .term ()),
223- sameTermButBetterVersion ? randomLongBetween (0 , state2 .version ()) : randomLongBetween (0 , 20 ));
221+ Join join = new Join (node1 , node1 , v1 .getTerm (), randomLongBetween (0 , state2 .term () - 1 ), randomLongBetween (0 , 20 ));
222+ assertTrue (cs1 .handleJoin (join ));
223+ assertTrue (cs1 .electionWon ());
224+ assertTrue (cs1 .containsJoinVoteFor (node1 ));
225+ assertFalse (cs1 .containsJoinVoteFor (node2 ));
226+ assertEquals (cs1 .getLastPublishedVersion (), cs1 .getLastAcceptedVersion ());
227+ assertFalse (cs1 .handleJoin (join ));
228+ }
229+
230+ public void testJoinWithSameLastAcceptedTermButLowerOrSameVersionWinsElection () {
231+ VotingConfiguration initialConfig = new VotingConfiguration (Collections .singleton (node1 .getId ()));
232+ ClusterState state1 = clusterState (0L , 1L , node1 , initialConfig , initialConfig , 42L );
233+ cs1 .setInitialState (state1 );
234+
235+ StartJoinRequest startJoinRequest1 = new StartJoinRequest (node2 , randomLongBetween (1 , 5 ));
236+ cs1 .handleStartJoin (startJoinRequest1 );
237+ ClusterState state2 = clusterState (startJoinRequest1 .getTerm (), randomLongBetween (2 , 20 ), node1 , initialConfig , initialConfig , 42L );
238+ cs1 .handlePublishRequest (new PublishRequest (state2 ));
239+ StartJoinRequest startJoinRequest2 = new StartJoinRequest (node2 , randomLongBetween (startJoinRequest1 .getTerm () + 1 , 10 ));
240+ Join v1 = cs1 .handleStartJoin (startJoinRequest2 );
241+
242+ Join join = new Join (node1 , node1 , v1 .getTerm (), state2 .term (), randomLongBetween (0 , state2 .version ()));
224243 assertTrue (cs1 .handleJoin (join ));
225244 assertTrue (cs1 .electionWon ());
226245 assertTrue (cs1 .containsJoinVoteFor (node1 ));
0 commit comments