use of org.elasticsearch.threadpool.ThreadPool.Cancellable in project elasticsearch by elastic.
the class ScheduleWithFixedDelayTests method testThatRunnableIsRescheduled.
public void testThatRunnableIsRescheduled() throws Exception {
final CountDownLatch latch = new CountDownLatch(scaledRandomIntBetween(2, 16));
final Runnable countingRunnable = () -> {
if (rarely()) {
throw new ElasticsearchException("sometimes we throw before counting down");
}
latch.countDown();
if (randomBoolean()) {
throw new ElasticsearchException("this shouldn't cause the test to fail!");
}
};
Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, TimeValue.timeValueMillis(10L), Names.GENERIC);
assertNotNull(cancellable);
// wait for the number of successful count down operations
latch.await();
// cancel
cancellable.cancel();
assertTrue(cancellable.isCancelled());
}
use of org.elasticsearch.threadpool.ThreadPool.Cancellable in project elasticsearch by elastic.
the class ScheduleWithFixedDelayTests method testRunnableRunsAtMostOnceAfterCancellation.
public void testRunnableRunsAtMostOnceAfterCancellation() throws Exception {
final int iterations = scaledRandomIntBetween(1, 12);
final AtomicInteger counter = new AtomicInteger();
final CountDownLatch doneLatch = new CountDownLatch(iterations);
final Runnable countingRunnable = () -> {
counter.incrementAndGet();
doneLatch.countDown();
};
final Cancellable cancellable = threadPool.scheduleWithFixedDelay(countingRunnable, TimeValue.timeValueMillis(10L), Names.GENERIC);
doneLatch.await();
cancellable.cancel();
final int counterValue = counter.get();
assertThat(counterValue, isOneOf(iterations, iterations + 1));
if (rarely()) {
awaitBusy(() -> {
final int value = counter.get();
return value == iterations || value == iterations + 1;
}, 50L, TimeUnit.MILLISECONDS);
}
}
Aggregations