Add configurable timeout safe await method (#117296)

Add a method for a configurable timeout with countdown latches.
This commit is contained in:
Tim Brooks 2024-11-21 17:11:38 -07:00 committed by GitHub
parent bead24880b
commit de73397a05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2331,10 +2331,18 @@ public abstract class ESTestCase extends LuceneTestCase {
* 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();