[CI] Fix bogus ScheduleWithFixedDelayTests.testRunnableRunsAtMostOnceAfterCancellation

Closes #34004
This commit is contained in:
Vladimir Dolzhenko 2018-10-04 16:31:56 +02:00 committed by GitHub
parent f1f0687b57
commit dcfe64e0e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,8 +40,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -266,8 +266,8 @@ public class ScheduleWithFixedDelayTests extends ESTestCase {
assertTrue(reschedulingRunnable.isCancelled()); assertTrue(reschedulingRunnable.isCancelled());
} }
public void testRunnableRunsAtMostOnceAfterCancellation() throws Exception { public void testRunnableDoesNotRunAfterCancellation() throws Exception {
final int iterations = scaledRandomIntBetween(1, 12); final int iterations = scaledRandomIntBetween(2, 12);
final AtomicInteger counter = new AtomicInteger(); final AtomicInteger counter = new AtomicInteger();
final CountDownLatch doneLatch = new CountDownLatch(iterations); final CountDownLatch doneLatch = new CountDownLatch(iterations);
final Runnable countingRunnable = () -> { final Runnable countingRunnable = () -> {
@ -275,17 +275,19 @@ public class ScheduleWithFixedDelayTests extends ESTestCase {
doneLatch.countDown(); doneLatch.countDown();
}; };
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, TimeValue.timeValueMillis(10L), Names.GENERIC); final TimeValue interval = TimeValue.timeValueMillis(50L);
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, interval, Names.GENERIC);
doneLatch.await(); doneLatch.await();
cancellable.cancel(); cancellable.cancel();
final int counterValue = counter.get(); final int counterValue = counter.get();
assertThat(counterValue, isOneOf(iterations, iterations + 1)); assertThat(counterValue, equalTo(iterations));
if (rarely()) { if (rarely()) {
awaitBusy(() -> { awaitBusy(() -> {
final int value = counter.get(); final int value = counter.get();
return value == iterations || value == iterations + 1; return value == iterations;
}, 50L, TimeUnit.MILLISECONDS); }, 5 * interval.millis(), TimeUnit.MILLISECONDS);
} }
} }