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 committed Nov 22, 2024
1 parent 45d5990 commit 3748667
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2301,10 +2301,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 3748667

Please sign in to comment.