Skip to content

Commit a6dd6a9

Browse files
committed
Reinstate testRunnableRunsAtMostOnceAfterCancellation (#99525)
This test was failing in #34004 due to a race, and although #34296 made the failures rarer they did not actually fix the race. Then in #99201 we fixed the race but the resulting test over-synchronizes and no longer meaningfully verifies the concurrent behaviour we were originally trying to check. It also fails for other reasons. This commit reverts back to the original test showing that we might run the action at most once after cancellation without any further synchronization, but fixes the assertion to use the value of the counter observed immediately after the cancellation since we cannot be sure that no extra iterations execute before the cancellation completes.
1 parent 6392a7f commit a6dd6a9

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

server/src/test/java/org/elasticsearch/threadpool/ScheduleWithFixedDelayTests.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import java.util.concurrent.atomic.AtomicReference;
2929

3030
import static org.hamcrest.Matchers.containsString;
31-
import static org.hamcrest.Matchers.equalTo;
3231
import static org.hamcrest.Matchers.instanceOf;
32+
import static org.hamcrest.Matchers.oneOf;
3333
import static org.hamcrest.Matchers.sameInstance;
3434
import static org.mockito.ArgumentMatchers.any;
3535
import static org.mockito.Mockito.mock;
@@ -273,25 +273,28 @@ public ScheduledCancellable schedule(Runnable command, TimeValue delay, String e
273273
assertTrue(reschedulingRunnable.isCancelled());
274274
}
275275

276-
public void testRunnableDoesNotRunAfterCancellation() throws Exception {
277-
final int iterations = scaledRandomIntBetween(2, 12);
276+
public void testRunnableRunsAtMostOnceAfterCancellation() throws Exception {
277+
final var intervalMillis = randomLongBetween(1, 50);
278278
final AtomicInteger counter = new AtomicInteger();
279-
final CountDownLatch doneLatch = new CountDownLatch(iterations);
279+
final CountDownLatch doneLatch = new CountDownLatch(scaledRandomIntBetween(1, 12));
280280
final Runnable countingRunnable = () -> {
281281
counter.incrementAndGet();
282282
doneLatch.countDown();
283283
};
284284

285-
final TimeValue interval = TimeValue.timeValueMillis(50L);
286-
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, interval, Names.GENERIC);
287-
doneLatch.await();
288-
cancellable.cancel();
289-
290-
final int counterValue = counter.get();
291-
assertThat(counterValue, equalTo(iterations));
292-
285+
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(
286+
countingRunnable,
287+
TimeValue.timeValueMillis(intervalMillis),
288+
Names.GENERIC
289+
);
290+
safeAwait(doneLatch);
291+
assertTrue(cancellable.cancel());
292+
final var iterations = counter.get();
293293
if (rarely()) {
294-
assertBusy(() -> assertThat(counter.get(), equalTo(iterations)), 5 * interval.millis(), TimeUnit.MILLISECONDS);
294+
Thread.sleep(randomLongBetween(0, intervalMillis * 5));
295+
} else if (randomBoolean()) {
296+
Thread.yield();
295297
}
298+
assertThat(counter.get(), oneOf(iterations, iterations + 1));
296299
}
297300
}

0 commit comments

Comments
 (0)