Skip to content

Commit 44e5f18

Browse files
Removed best-effort while-loops
Replaces executor shutdowns with more reliable ThreadPool#terminate calls Added assertBusy around queue latency checks, to avoid races with ThreadPool clock not moving forward
1 parent 99a0637 commit 44e5f18

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

server/src/test/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutorTests.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public void testExecutionEWMACalculation() throws Exception {
8989
assertThat(executor.getTotalTaskExecutionTime(), equalTo(500L));
9090
});
9191
assertThat(executor.getOngoingTasks().toString(), executor.getOngoingTasks().size(), equalTo(0));
92-
executor.shutdown();
93-
executor.awaitTermination(10, TimeUnit.SECONDS);
92+
ThreadPool.terminate(executor, 10, TimeUnit.SECONDS);
9493
}
9594

9695
/**
@@ -143,28 +142,27 @@ public void testFrontOfQueueLatency() throws Exception {
143142
executor.execute(() -> {});
144143

145144
var frontOfQueueDuration = executor.peekMaxQueueLatencyInQueue();
146-
assertThat("Expected a task to be queued", frontOfQueueDuration, greaterThan(0L));
145+
assertBusy(
146+
// Wrap this call in an assertBusy because it's feasible for the thread pool's clock to see no time pass.
147+
() -> assertThat("Expected a task to be queued", frontOfQueueDuration, greaterThan(0L))
148+
);
147149
safeSleep(10);
148150
var updatedFrontOfQueueDuration = executor.peekMaxQueueLatencyInQueue();
149-
assertThat(
150-
"Expected a second peek to report a longer duration",
151-
updatedFrontOfQueueDuration,
152-
greaterThan(frontOfQueueDuration)
151+
assertBusy(
152+
// Again add an assertBusy to ensure time passes on the thread pool's clock and there are no races.
153+
() -> assertThat(
154+
"Expected a second peek to report a longer duration",
155+
updatedFrontOfQueueDuration,
156+
greaterThan(frontOfQueueDuration)
157+
)
153158
);
154159

155160
// Release the first task that's running, and wait for the second to start -- then it is ensured that the queue will be empty.
156161
safeAwait(barrier);
157162
safeAwait(barrier);
158-
assertBusy(() -> { assertEquals("Queue should be emptied", 0, executor.peekMaxQueueLatencyInQueue()); });
163+
assertEquals("Queue should be emptied", 0, executor.peekMaxQueueLatencyInQueue());
159164
} finally {
160-
// Clean up.
161-
while (barrier.getNumberWaiting() > 0) {
162-
// Release any potentially running task. This could be racy (a task may start executing and hit the barrier afterward) and
163-
// is best-effort.
164-
safeAwait(barrier);
165-
}
166-
executor.shutdown();
167-
executor.awaitTermination(10, TimeUnit.SECONDS);
165+
ThreadPool.terminate(executor, 10, TimeUnit.SECONDS);
168166
}
169167
}
170168

@@ -224,14 +222,7 @@ public void testMaxDequeuedQueueLatency() throws Exception {
224222
assertEquals("Max should not be the last task", 5, executor.getMaxQueueLatencyMillisSinceLastPollAndReset());
225223
assertEquals("The max was just reset, should be zero", 0, executor.getMaxQueueLatencyMillisSinceLastPollAndReset());
226224
} finally {
227-
// Clean up.
228-
while (barrier.getNumberWaiting() > 0) {
229-
// Release any potentially running task. This could be racy (a task may start executing and hit the barrier afterward) and
230-
// is best-effort.
231-
safeAwait(barrier);
232-
}
233-
executor.shutdown();
234-
executor.awaitTermination(10, TimeUnit.SECONDS);
225+
ThreadPool.terminate(executor, 10, TimeUnit.SECONDS);
235226
}
236227
}
237228

@@ -268,8 +259,7 @@ public void testExceptionThrowingTask() throws Exception {
268259
assertThat(executor.getTotalTaskExecutionTime(), equalTo(0L));
269260
assertThat(executor.getActiveCount(), equalTo(0));
270261
assertThat(executor.getOngoingTasks().toString(), executor.getOngoingTasks().size(), equalTo(0));
271-
executor.shutdown();
272-
executor.awaitTermination(10, TimeUnit.SECONDS);
262+
ThreadPool.terminate(executor, 10, TimeUnit.SECONDS);
273263
}
274264

275265
public void testGetOngoingTasks() throws Exception {
@@ -306,8 +296,7 @@ public void testGetOngoingTasks() throws Exception {
306296
exitTaskLatch.countDown();
307297
assertBusy(() -> assertThat(executor.getOngoingTasks().toString(), executor.getOngoingTasks().size(), equalTo(0)));
308298
assertThat(executor.getTotalTaskExecutionTime(), greaterThan(0L));
309-
executor.shutdown();
310-
executor.awaitTermination(10, TimeUnit.SECONDS);
299+
ThreadPool.terminate(executor, 10, TimeUnit.SECONDS);
311300
}
312301

313302
public void testQueueLatencyHistogramMetrics() {

0 commit comments

Comments
 (0)