IndexEventHandlerTest: fix flakiness in concurrent indexing tests The usage of CyclicBarrier to synchronize two concurrent tasks was not coded correctly. The assumption that when both concurrent tasks pass the `await()` point, which occurred before attempting to acquire a lock, ensures concurrency was wrong: it can happen that one task acquires a lock and releases it before the other task even tries to acquire the lock. In the end both tasks succeed in acquiring the lock. To fix the flakiness, the `await()` on the CyclicBarrier has to be called after a lock was successfully acquired but before it is released and also when failing to acquire the lock. This way the two `await()` calls from two concurrent tasks ensure real concurrency. Change-Id: I4dd4faec5e94eecdefed4729e241ef174428bba1
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexEventHandlerTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexEventHandlerTest.java index 75c8a39..7c6a4f6 100644 --- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexEventHandlerTest.java +++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/index/IndexEventHandlerTest.java
@@ -586,15 +586,20 @@ @Override public void run() { try { - testBarrier.await(); idLocks .withLock( task, () -> runLater( INDEX_WAIT_TIMEOUT_MS * 2, - () -> CompletableFuture.completedFuture(successFunc.get())), - failureFunc) + () -> { + await(); + return CompletableFuture.completedFuture(successFunc.get()); + }), + () -> { + await(); + failureFunc.invoke(); + }) .whenComplete( (v, t) -> { if (t == null) { @@ -609,6 +614,14 @@ } } + private void await() { + try { + testBarrier.await(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public void join() { try { future.join();