|
40 | 40 | import java.util.concurrent.atomic.AtomicReference;
|
41 | 41 |
|
42 | 42 | import static org.hamcrest.Matchers.containsString;
|
| 43 | +import static org.hamcrest.Matchers.equalTo; |
43 | 44 | import static org.hamcrest.Matchers.instanceOf;
|
44 |
| -import static org.hamcrest.Matchers.isOneOf; |
45 | 45 | import static org.hamcrest.Matchers.sameInstance;
|
46 | 46 | import static org.mockito.Mockito.mock;
|
47 | 47 | import static org.mockito.Mockito.times;
|
@@ -266,26 +266,28 @@ public ScheduledFuture<?> schedule(TimeValue delay, String executor, Runnable co
|
266 | 266 | assertTrue(reschedulingRunnable.isCancelled());
|
267 | 267 | }
|
268 | 268 |
|
269 |
| - public void testRunnableRunsAtMostOnceAfterCancellation() throws Exception { |
270 |
| - final int iterations = scaledRandomIntBetween(1, 12); |
| 269 | + public void testRunnableDoesNotRunAfterCancellation() throws Exception { |
| 270 | + final int iterations = scaledRandomIntBetween(2, 12); |
271 | 271 | final AtomicInteger counter = new AtomicInteger();
|
272 | 272 | final CountDownLatch doneLatch = new CountDownLatch(iterations);
|
273 | 273 | final Runnable countingRunnable = () -> {
|
274 | 274 | counter.incrementAndGet();
|
275 | 275 | doneLatch.countDown();
|
276 | 276 | };
|
277 | 277 |
|
278 |
| - final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, TimeValue.timeValueMillis(10L), Names.GENERIC); |
| 278 | + final TimeValue interval = TimeValue.timeValueMillis(50L); |
| 279 | + final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, interval, Names.GENERIC); |
279 | 280 | doneLatch.await();
|
280 | 281 | cancellable.cancel();
|
| 282 | + |
281 | 283 | final int counterValue = counter.get();
|
282 |
| - assertThat(counterValue, isOneOf(iterations, iterations + 1)); |
| 284 | + assertThat(counterValue, equalTo(iterations)); |
283 | 285 |
|
284 | 286 | if (rarely()) {
|
285 | 287 | awaitBusy(() -> {
|
286 | 288 | final int value = counter.get();
|
287 |
| - return value == iterations || value == iterations + 1; |
288 |
| - }, 50L, TimeUnit.MILLISECONDS); |
| 289 | + return value == iterations; |
| 290 | + }, 5 * interval.millis(), TimeUnit.MILLISECONDS); |
289 | 291 | }
|
290 | 292 | }
|
291 | 293 |
|
|
0 commit comments