Skip to content

Commit abfbe1d

Browse files
authored
Use TimeValue for timeouts in safeAwait etc. (elastic#126509) (elastic#126711)
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 58c5a3f commit abfbe1d

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

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

@@ -2424,7 +2425,7 @@ public static void safeAcquire(int permits, Semaphore semaphore) {
24242425
* @return The value with which the {@code listener} was completed.
24252426
*/
24262427
public static <T> T safeAwait(SubscribableListener<T> listener) {
2427-
return safeAwait(listener, SAFE_AWAIT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS);
2428+
return safeAwait(listener, SAFE_AWAIT_TIMEOUT);
24282429
}
24292430

24302431
/**
@@ -2433,10 +2434,10 @@ public static <T> T safeAwait(SubscribableListener<T> listener) {
24332434
*
24342435
* @return The value with which the {@code listener} was completed.
24352436
*/
2436-
public static <T> T safeAwait(SubscribableListener<T> listener, long timeout, TimeUnit unit) {
2437+
public static <T> T safeAwait(SubscribableListener<T> listener, TimeValue timeout) {
24372438
final var future = new TestPlainActionFuture<T>();
24382439
listener.addListener(future);
2439-
return safeGet(future, timeout, unit);
2440+
return safeGet(future, timeout);
24402441
}
24412442

24422443
/**
@@ -2466,7 +2467,7 @@ public static <T extends ActionResponse> T safeExecute(ElasticsearchClient clien
24662467
* @return The value with which the {@code future} was completed.
24672468
*/
24682469
public static <T> T safeGet(Future<T> future) {
2469-
return safeGet(future, SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS);
2470+
return safeGet(future, SAFE_AWAIT_TIMEOUT);
24702471
}
24712472

24722473
/**
@@ -2475,9 +2476,10 @@ public static <T> T safeGet(Future<T> future) {
24752476
*
24762477
* @return The value with which the {@code future} was completed.
24772478
*/
2478-
public static <T> T safeGet(Future<T> future, long timeout, TimeUnit unit) {
2479+
// NB private because tests should be designed not to need to wait for longer than SAFE_AWAIT_TIMEOUT.
2480+
private static <T> T safeGet(Future<T> future, TimeValue timeout) {
24792481
try {
2480-
return future.get(timeout, unit);
2482+
return future.get(timeout.millis(), TimeUnit.MILLISECONDS);
24812483
} catch (InterruptedException e) {
24822484
Thread.currentThread().interrupt();
24832485
throw new AssertionError("safeGet: interrupted waiting for SubscribableListener", e);

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

Lines changed: 1 addition & 2 deletions
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)