Skip to content

Commit

Permalink
Add configurable timeout safe await method (elastic#117296)
Browse files Browse the repository at this point in the history
Add a method for a configurable timeout with countdown latches.
  • Loading branch information
Tim-Brooks authored and alexey-ivanov-es committed Nov 28, 2024
1 parent 6c84f0a commit d390fd4
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2331,10 +2331,18 @@ public static void safeAwait(CyclicBarrier barrier) {
* flag and asserting that the latch is indeed completed before the timeout.
*/
public static void safeAwait(CountDownLatch countDownLatch) {
safeAwait(countDownLatch, SAFE_AWAIT_TIMEOUT);
}

/**
* Await on the given {@link CountDownLatch} with a supplied timeout, preserving the thread's interrupt status
* flag and asserting that the latch is indeed completed before the timeout.
*/
public static void safeAwait(CountDownLatch countDownLatch, TimeValue timeout) {
try {
assertTrue(
"safeAwait: CountDownLatch did not reach zero within the timeout",
countDownLatch.await(SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS)
countDownLatch.await(timeout.millis(), TimeUnit.MILLISECONDS)
);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down

0 comments on commit d390fd4

Please sign in to comment.