diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index c5a49f77e4fd8..fa77aebb80b53 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -2398,8 +2398,9 @@ protected static SecureRandom secureRandomFips(final byte[] seed) throws NoSuchA * complete are a big drag on CI times which slows everyone down. *

* For instance, tests which verify things that require the passage of time ought to simulate this (e.g. using a {@link - * org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits (e.g. - * using a {@link CountDownLatch}) which release as soon as the condition is satisfied. + * org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits. For + * instance, use a {@link CountDownLatch} or {@link CyclicBarrier} or similar to continue execution as soon as a condition is satisfied. + * To wait for a particular cluster state, use {@link ClusterServiceUtils#addTemporaryStateListener} rather than busy-waiting on an API. */ public static final TimeValue SAFE_AWAIT_TIMEOUT = TimeValue.timeValueSeconds(10); @@ -2477,7 +2478,7 @@ public static void safeAcquire(int permits, Semaphore semaphore) { * @return The value with which the {@code listener} was completed. */ public static T safeAwait(SubscribableListener listener) { - return safeAwait(listener, SAFE_AWAIT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS); + return safeAwait(listener, SAFE_AWAIT_TIMEOUT); } /** @@ -2486,10 +2487,10 @@ public static T safeAwait(SubscribableListener listener) { * * @return The value with which the {@code listener} was completed. */ - public static T safeAwait(SubscribableListener listener, long timeout, TimeUnit unit) { + public static T safeAwait(SubscribableListener listener, TimeValue timeout) { final var future = new TestPlainActionFuture(); listener.addListener(future); - return safeGet(future, timeout, unit); + return safeGet(future, timeout); } /** @@ -2519,7 +2520,7 @@ public static T safeExecute(ElasticsearchClient clien * @return The value with which the {@code future} was completed. */ public static T safeGet(Future future) { - return safeGet(future, SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS); + return safeGet(future, SAFE_AWAIT_TIMEOUT); } /** @@ -2528,9 +2529,10 @@ public static T safeGet(Future future) { * * @return The value with which the {@code future} was completed. */ - public static T safeGet(Future future, long timeout, TimeUnit unit) { + // NB private because tests should be designed not to need to wait for longer than SAFE_AWAIT_TIMEOUT. + private static T safeGet(Future future, TimeValue timeout) { try { - return future.get(timeout, unit); + return future.get(timeout.millis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AssertionError("safeGet: interrupted waiting for SubscribableListener", e); diff --git a/x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DataStreamLifecycleDownsampleDisruptionIT.java b/x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DataStreamLifecycleDownsampleDisruptionIT.java index a96a7f20a815f..e8fb278bb570a 100644 --- a/x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DataStreamLifecycleDownsampleDisruptionIT.java +++ b/x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DataStreamLifecycleDownsampleDisruptionIT.java @@ -29,7 +29,6 @@ import java.util.Collection; import java.util.List; -import java.util.concurrent.TimeUnit; import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_DOWNSAMPLE_STATUS; import static org.elasticsearch.xpack.downsample.DataStreamLifecycleDriver.getBackingIndices; @@ -116,6 +115,6 @@ private void ensureDownsamplingStatus(String downsampledIndex, IndexMetadata.Dow } return false; }); - safeAwait(listener, timeout.millis(), TimeUnit.MILLISECONDS); + safeAwait(listener, timeout); } }