@@ -188,7 +188,7 @@ public void testJoinWithHigherAcceptedTerm() {
188
188
containsString ("higher than current last accepted term" ));
189
189
}
190
190
191
- public void testJoinWithHigherVersion () {
191
+ public void testJoinWithSameAcceptedTermButHigherVersion () {
192
192
VotingConfiguration initialConfig = new VotingConfiguration (Collections .singleton (node1 .getId ()));
193
193
ClusterState state1 = clusterState (0L , 1L , node1 , initialConfig , initialConfig , 42L );
194
194
cs1 .setInitialState (state1 );
@@ -206,7 +206,7 @@ public void testJoinWithHigherVersion() {
206
206
containsString ("higher than current last accepted version" ));
207
207
}
208
208
209
- public void testJoinWinsElection () {
209
+ public void testJoinWithLowerLastAcceptedTermWinsElection () {
210
210
VotingConfiguration initialConfig = new VotingConfiguration (Collections .singleton (node1 .getId ()));
211
211
ClusterState state1 = clusterState (0L , 1L , node1 , initialConfig , initialConfig , 42L );
212
212
cs1 .setInitialState (state1 );
@@ -218,9 +218,28 @@ public void testJoinWinsElection() {
218
218
StartJoinRequest startJoinRequest2 = new StartJoinRequest (node2 , randomLongBetween (startJoinRequest1 .getTerm () + 1 , 10 ));
219
219
Join v1 = cs1 .handleStartJoin (startJoinRequest2 );
220
220
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 ()));
224
243
assertTrue (cs1 .handleJoin (join ));
225
244
assertTrue (cs1 .electionWon ());
226
245
assertTrue (cs1 .containsJoinVoteFor (node1 ));
0 commit comments