Skip to content

Commit de2feac

Browse files
authored
Use safeAwait in indexRandom (#124362) (#124376)
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 0d7bd28 commit de2feac

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
@@ -200,6 +200,7 @@
200200
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
201201
import static org.elasticsearch.common.util.CollectionUtils.eagerPartition;
202202
import static org.elasticsearch.core.TimeValue.timeValueMillis;
203+
import static org.elasticsearch.core.TimeValue.timeValueSeconds;
203204
import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING;
204205
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
205206
import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING;
@@ -1819,16 +1820,8 @@ private void postIndexAsyncActions(String[] indices, List<CountDownLatch> inFlig
18191820
}
18201821
}
18211822
while (inFlightAsyncOperations.size() > MAX_IN_FLIGHT_ASYNC_INDEXES) {
1822-
int waitFor = between(0, inFlightAsyncOperations.size() - 1);
1823-
try {
1824-
assertTrue(
1825-
"operation did not complete within timeout",
1826-
inFlightAsyncOperations.remove(waitFor).await(60, TimeUnit.SECONDS)
1827-
);
1828-
} catch (InterruptedException e) {
1829-
Thread.currentThread().interrupt();
1830-
fail(e, "interrupted while waiting for operation to complete");
1831-
}
1823+
// longer-than-usual timeout, see #112908
1824+
safeAwait(inFlightAsyncOperations.remove(between(0, inFlightAsyncOperations.size() - 1)), timeValueSeconds(60));
18321825
}
18331826
}
18341827

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,10 @@ public static void safeAwait(CountDownLatch countDownLatch) {
23762376
/**
23772377
* Await on the given {@link CountDownLatch} with a supplied timeout, preserving the thread's interrupt status
23782378
* flag and asserting that the latch is indeed completed before the timeout.
2379+
* <p>
2380+
* Prefer {@link #safeAwait(CountDownLatch)} (with the default 10s timeout) wherever possible. It's very unusual to need to block a
2381+
* 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
2382+
* to find a different way to write the test which doesn't need such a long wait.
23792383
*/
23802384
public static void safeAwait(CountDownLatch countDownLatch, TimeValue timeout) {
23812385
try {

0 commit comments

Comments
 (0)