Skip to content

Commit e3d7d0e

Browse files
peek at the queue twice, ensure the queue duration reported the second peek is longer than the first
1 parent a14d8c2 commit e3d7d0e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ public void testFrontOfQueueLatency() throws Exception {
104104
// waiting for task execution to begin via the supplied barrier.
105105
var adjustableTimedRunnable = new AdjustableQueueTimeWithExecutionBarrierTimedRunnable(
106106
barrier,
107-
TimeUnit.NANOSECONDS.toNanos(1000000) // Until changed, queue latencies will always be 1 millisecond.
107+
// This won't actually be used, because it is reported when a task is taken off the queue. This test peeks at the still queued
108+
// tasks.
109+
TimeUnit.NANOSECONDS.toNanos(1_000_000)
108110
);
109111
TaskExecutionTimeTrackingEsThreadPoolExecutor executor = new TaskExecutionTimeTrackingEsThreadPoolExecutor(
110112
"test-threadpool",
111113
1,
112114
1,
113-
1000,
115+
1_000,
114116
TimeUnit.MILLISECONDS,
115117
ConcurrentCollections.newBlockingQueue(),
116118
(runnable) -> adjustableTimedRunnable,
@@ -139,14 +141,24 @@ public void testFrontOfQueueLatency() throws Exception {
139141
// one thread) and can be peeked at.
140142
executor.execute(() -> {});
141143
executor.execute(() -> {});
144+
142145
var frontOfQueueDuration = executor.peekMaxQueueLatencyInQueue();
143146
assertThat("Expected a task to be queued", frontOfQueueDuration, greaterThan(0L));
144-
safeAwait(barrier); // release the first task to finish
147+
safeSleep(10);
148+
var updatedFrontOfQueueDuration = executor.peekMaxQueueLatencyInQueue();
149+
assertThat(
150+
"Expected a second peek to report a longer duration",
151+
updatedFrontOfQueueDuration,
152+
greaterThan(frontOfQueueDuration)
153+
);
154+
155+
// Release the first task that's running, and wait for the second to start -- then it is ensured that the queue will be empty.
156+
safeAwait(barrier);
157+
safeAwait(barrier);
145158
assertBusy(() -> { assertEquals("Queue should be emptied", 0, executor.peekMaxQueueLatencyInQueue()); });
146-
safeAwait(barrier); // release the second task to finish.
147159
} finally {
148160
// Clean up.
149-
if (barrier.getNumberWaiting() > 0) {
161+
while (barrier.getNumberWaiting() > 0) {
150162
// Release any potentially running task. This could be racy (a task may start executing and hit the barrier afterward) and
151163
// is best-effort.
152164
safeAwait(barrier);
@@ -213,7 +225,7 @@ public void testMaxDequeuedQueueLatency() throws Exception {
213225
assertEquals("The max was just reset, should be zero", 0, executor.getMaxQueueLatencyMillisSinceLastPollAndReset());
214226
} finally {
215227
// Clean up.
216-
if (barrier.getNumberWaiting() > 0) {
228+
while (barrier.getNumberWaiting() > 0) {
217229
// Release any potentially running task. This could be racy (a task may start executing and hit the barrier afterward) and
218230
// is best-effort.
219231
safeAwait(barrier);

0 commit comments

Comments
 (0)