Skip to content

Commit 37988dd

Browse files
authored
Ensure cached time elapses in ClusterServiceIT (#91986)
Rather than just checking `System.nanoTime()` we should verify that each thread pool's cached time has elapsed here.
1 parent 75de8f8 commit 37988dd

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/service/ClusterServiceIT.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.test.ESIntegTestCase;
1818
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
1919
import org.elasticsearch.test.ESIntegTestCase.Scope;
20+
import org.elasticsearch.threadpool.ThreadPool;
2021

2122
import java.util.Arrays;
2223
import java.util.HashSet;
@@ -25,6 +26,7 @@
2526
import java.util.concurrent.CountDownLatch;
2627
import java.util.concurrent.TimeUnit;
2728
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.stream.StreamSupport;
2830

2931
import static org.hamcrest.Matchers.equalTo;
3032
import static org.hamcrest.Matchers.greaterThan;
@@ -414,11 +416,7 @@ public void onFailure(Exception e) {
414416
});
415417
}
416418

417-
final var startNanoTime = System.nanoTime();
418-
while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startNanoTime, TimeUnit.NANOSECONDS) <= 0) {
419-
// noinspection BusyWait
420-
Thread.sleep(100);
421-
}
419+
waitForTimeToElapse();
422420

423421
pendingClusterTasks = clusterService.getMasterService().pendingTasks();
424422
assertThat(pendingClusterTasks.size(), greaterThanOrEqualTo(5));
@@ -441,4 +439,28 @@ public void onFailure(Exception e) {
441439
block2.countDown();
442440
}
443441
}
442+
443+
private static void waitForTimeToElapse() throws InterruptedException {
444+
final ThreadPool[] threadPools = StreamSupport.stream(internalCluster().getInstances(ClusterService.class).spliterator(), false)
445+
.map(ClusterService::threadPool)
446+
.toArray(ThreadPool[]::new);
447+
final long[] startTimes = Arrays.stream(threadPools).mapToLong(ThreadPool::relativeTimeInMillis).toArray();
448+
449+
final var startNanoTime = System.nanoTime();
450+
while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startNanoTime, TimeUnit.NANOSECONDS) <= 100) {
451+
// noinspection BusyWait
452+
Thread.sleep(100);
453+
}
454+
455+
outer: do {
456+
for (int i = 0; i < threadPools.length; i++) {
457+
if (threadPools[i].relativeTimeInMillis() <= startTimes[i]) {
458+
// noinspection BusyWait
459+
Thread.sleep(100);
460+
continue outer;
461+
}
462+
}
463+
return;
464+
} while (true);
465+
}
444466
}

0 commit comments

Comments
 (0)