Skip to content

Commit 800cf72

Browse files
authored
Use TimeValue for timeouts in safeAwait etc. (#126509)
There's no need to force callers to deconstruct the `TimeValue` in their possession into a `long` and a `TimeUnit`, we can do it ourselves.
1 parent f57be54 commit 800cf72

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -2398,8 +2398,9 @@ protected static SecureRandom secureRandomFips(final byte[] seed) throws NoSuchA
23982398
* complete are a big drag on CI times which slows everyone down.
23992399
* <p>
24002400
* For instance, tests which verify things that require the passage of time ought to simulate this (e.g. using a {@link
2401-
* org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits (e.g.
2402-
* using a {@link CountDownLatch}) which release as soon as the condition is satisfied.
2401+
* org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits. For
2402+
* instance, use a {@link CountDownLatch} or {@link CyclicBarrier} or similar to continue execution as soon as a condition is satisfied.
2403+
* To wait for a particular cluster state, use {@link ClusterServiceUtils#addTemporaryStateListener} rather than busy-waiting on an API.
24032404
*/
24042405
public static final TimeValue SAFE_AWAIT_TIMEOUT = TimeValue.timeValueSeconds(10);
24052406

@@ -2477,7 +2478,7 @@ public static void safeAcquire(int permits, Semaphore semaphore) {
24772478
* @return The value with which the {@code listener} was completed.
24782479
*/
24792480
public static <T> T safeAwait(SubscribableListener<T> listener) {
2480-
return safeAwait(listener, SAFE_AWAIT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS);
2481+
return safeAwait(listener, SAFE_AWAIT_TIMEOUT);
24812482
}
24822483

24832484
/**
@@ -2486,10 +2487,10 @@ public static <T> T safeAwait(SubscribableListener<T> listener) {
24862487
*
24872488
* @return The value with which the {@code listener} was completed.
24882489
*/
2489-
public static <T> T safeAwait(SubscribableListener<T> listener, long timeout, TimeUnit unit) {
2490+
public static <T> T safeAwait(SubscribableListener<T> listener, TimeValue timeout) {
24902491
final var future = new TestPlainActionFuture<T>();
24912492
listener.addListener(future);
2492-
return safeGet(future, timeout, unit);
2493+
return safeGet(future, timeout);
24932494
}
24942495

24952496
/**
@@ -2519,7 +2520,7 @@ public static <T extends ActionResponse> T safeExecute(ElasticsearchClient clien
25192520
* @return The value with which the {@code future} was completed.
25202521
*/
25212522
public static <T> T safeGet(Future<T> future) {
2522-
return safeGet(future, SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS);
2523+
return safeGet(future, SAFE_AWAIT_TIMEOUT);
25232524
}
25242525

25252526
/**
@@ -2528,9 +2529,10 @@ public static <T> T safeGet(Future<T> future) {
25282529
*
25292530
* @return The value with which the {@code future} was completed.
25302531
*/
2531-
public static <T> T safeGet(Future<T> future, long timeout, TimeUnit unit) {
2532+
// NB private because tests should be designed not to need to wait for longer than SAFE_AWAIT_TIMEOUT.
2533+
private static <T> T safeGet(Future<T> future, TimeValue timeout) {
25322534
try {
2533-
return future.get(timeout, unit);
2535+
return future.get(timeout.millis(), TimeUnit.MILLISECONDS);
25342536
} catch (InterruptedException e) {
25352537
Thread.currentThread().interrupt();
25362538
throw new AssertionError("safeGet: interrupted waiting for SubscribableListener", e);

x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DataStreamLifecycleDownsampleDisruptionIT.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import java.util.Collection;
3131
import java.util.List;
32-
import java.util.concurrent.TimeUnit;
3332

3433
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_DOWNSAMPLE_STATUS;
3534
import static org.elasticsearch.xpack.downsample.DataStreamLifecycleDriver.getBackingIndices;
@@ -116,6 +115,6 @@ private void ensureDownsamplingStatus(String downsampledIndex, IndexMetadata.Dow
116115
}
117116
return false;
118117
});
119-
safeAwait(listener, timeout.millis(), TimeUnit.MILLISECONDS);
118+
safeAwait(listener, timeout);
120119
}
121120
}

0 commit comments

Comments
 (0)