Skip to content

Commit 9bc3d22

Browse files
committed
Use safeAwait in indexRandom
No need to implement the safe wait on the latch by hand here, we can use the other `safeAwait` variant that takes a timeout. Also adds a comment discouraging its use elsewhere without good reason.
1 parent f8ef784 commit 9bc3d22

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
214214
import static org.elasticsearch.common.util.CollectionUtils.eagerPartition;
215215
import static org.elasticsearch.core.TimeValue.timeValueMillis;
216+
import static org.elasticsearch.core.TimeValue.timeValueSeconds;
216217
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING;
217218
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
218219
import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING;
@@ -1887,16 +1888,8 @@ private void postIndexAsyncActions(String[] indices, List<CountDownLatch> inFlig
18871888
}
18881889
}
18891890
while (inFlightAsyncOperations.size() > MAX_IN_FLIGHT_ASYNC_INDEXES) {
1890-
int waitFor = between(0, inFlightAsyncOperations.size() - 1);
1891-
try {
1892-
assertTrue(
1893-
"operation did not complete within timeout",
1894-
inFlightAsyncOperations.remove(waitFor).await(60, TimeUnit.SECONDS)
1895-
);
1896-
} catch (InterruptedException e) {
1897-
Thread.currentThread().interrupt();
1898-
fail(e, "interrupted while waiting for operation to complete");
1899-
}
1891+
// longer-than-usual timeout, see #112908
1892+
safeAwait(inFlightAsyncOperations.remove(between(0, inFlightAsyncOperations.size() - 1)), timeValueSeconds(60));
19001893
}
19011894
}
19021895

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,10 @@ public static void safeAwait(CountDownLatch countDownLatch) {
24292429
/**
24302430
* Await on the given {@link CountDownLatch} with a supplied timeout, preserving the thread's interrupt status
24312431
* flag and asserting that the latch is indeed completed before the timeout.
2432+
* <p>
2433+
* Prefer {@link #safeAwait(CountDownLatch)} (with the default 10s timeout) wherever possible. It's very unusual to need to block a
2434+
* test for more than 10s, and such slow tests are a big problem for overall test suite performance. In almost all cases it's possible
2435+
* to find a different way to write the test which doesn't need such a long wait.
24322436
*/
24332437
public static void safeAwait(CountDownLatch countDownLatch, TimeValue timeout) {
24342438
try {

0 commit comments

Comments
 (0)