@@ -201,24 +201,29 @@ public void testLeaderElectionWithRenewDeadline() throws InterruptedException {
201
201
List <String > electionHistory = new ArrayList <>();
202
202
List <String > leadershipHistory = new ArrayList <>();
203
203
204
+ CountDownLatch testLockAccessLatch = new CountDownLatch (9 );
204
205
MockResourceLock mockLock = new MockResourceLock ("mock" );
205
206
mockLock .renewCountMax = 3 ;
206
207
mockLock .onCreate =
207
208
record -> {
208
209
electionHistory .add ("create record" );
209
210
leadershipHistory .add ("get leadership" );
211
+ testLockAccessLatch .countDown ();
210
212
};
211
213
mockLock .onUpdate =
212
214
record -> {
213
215
electionHistory .add ("update record" );
216
+ testLockAccessLatch .countDown ();
214
217
};
215
218
mockLock .onChange =
216
219
record -> {
217
220
electionHistory .add ("change record" );
221
+ testLockAccessLatch .countDown ();
218
222
};
219
223
mockLock .onTryUpdate =
220
224
record -> {
221
225
electionHistory .add ("try update record" );
226
+ testLockAccessLatch .countDown ();
222
227
};
223
228
224
229
LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig ();
@@ -244,18 +249,16 @@ record -> {
244
249
});
245
250
246
251
testLeaderElectionLatch .await (10 , SECONDS );
252
+ testLockAccessLatch .await (10 , SECONDS );
247
253
248
- assertHistory (
254
+ assertWildcardHistory (
249
255
electionHistory ,
250
256
"create record" ,
251
- "try update record" ,
257
+ "try update record+ " ,
252
258
"update record" ,
253
- "try update record" ,
259
+ "try update record+ " ,
254
260
"update record" ,
255
- "try update record" ,
256
- "try update record" ,
257
- "try update record" ,
258
- "try update record" );
261
+ "try update record+" );
259
262
assertHistory (leadershipHistory , "get leadership" , "start leading" , "stop leading" );
260
263
}
261
264
@@ -274,6 +277,36 @@ private void assertHistory(List<String> history, String... expected) {
274
277
}
275
278
}
276
279
280
+ // assertWildcardHistory allows for an arbitrary number of repeated entries for an
281
+ // comparison with a '+' suffix. This allows for a semantic rather than literal
282
+ // comparison to avoid issues of timing.
283
+ private void assertWildcardHistory (List <String > history , String ... expected ) {
284
+ Assert .assertNotNull (expected );
285
+ Assert .assertNotNull (history );
286
+
287
+ // TODO: This code is too complicated and a little bit buggy, but it works
288
+ // for the current limited use case. Clean this up!
289
+ int expectedIx = 0 ;
290
+ for (int index = 0 ; index < history .size (); ++index ) {
291
+ String compare = expected [expectedIx ];
292
+ if (compare .endsWith ("+" )) {
293
+ compare = compare .substring (0 , compare .length () - 1 );
294
+ if (!history .get (index ).equals (compare )) {
295
+ expectedIx ++;
296
+ compare = expected [expectedIx ];
297
+ expectedIx ++;
298
+ }
299
+ } else {
300
+ expectedIx ++;
301
+ }
302
+ Assert .assertEquals (
303
+ String .format (
304
+ "Not equal at index %d, expected %s, got %s" , index , compare , history .get (index )),
305
+ compare ,
306
+ history .get (index ));
307
+ }
308
+ }
309
+
277
310
@ Test
278
311
public void testLeaderElectionCaptureException () throws ApiException , InterruptedException {
279
312
RuntimeException expectedException = new RuntimeException ("noxu" );
0 commit comments