use of org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable in project elasticsearch by elastic.
the class ScheduleWithFixedDelayTests method testDoesNotRescheduleUntilExecutionFinished.
public void testDoesNotRescheduleUntilExecutionFinished() throws Exception {
final TimeValue delay = TimeValue.timeValueMillis(100L);
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch pauseLatch = new CountDownLatch(1);
ThreadPool threadPool = mock(ThreadPool.class);
final Runnable runnable = () -> {
// notify that the runnable is started
startLatch.countDown();
try {
// wait for other thread to un-pause
pauseLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
ReschedulingRunnable reschedulingRunnable = new ReschedulingRunnable(runnable, delay, Names.GENERIC, threadPool);
// this call was made during construction of the runnable
verify(threadPool, times(1)).schedule(delay, Names.GENERIC, reschedulingRunnable);
// create a thread and start the runnable
Thread runThread = new Thread() {
@Override
public void run() {
reschedulingRunnable.run();
}
};
runThread.start();
// wait for the runnable to be started and ensure the runnable hasn't used the threadpool again
startLatch.await();
verifyNoMoreInteractions(threadPool);
// un-pause the runnable and allow it to complete execution
pauseLatch.countDown();
runThread.join();
// validate schedule was called again
verify(threadPool, times(2)).schedule(delay, Names.GENERIC, reschedulingRunnable);
}
use of org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable in project elasticsearch by elastic.
the class ScheduleWithFixedDelayTests method testOnRejectionCausesCancellation.
public void testOnRejectionCausesCancellation() throws Exception {
final TimeValue delay = TimeValue.timeValueMillis(10L);
terminate(threadPool);
threadPool = new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "fixed delay tests").build()) {
@Override
public ScheduledFuture<?> schedule(TimeValue delay, String executor, Runnable command) {
if (command instanceof ReschedulingRunnable) {
((ReschedulingRunnable) command).onRejection(new EsRejectedExecutionException());
} else {
fail("this should only be called with a rescheduling runnable in this test");
}
return null;
}
};
Runnable runnable = () -> {
};
ReschedulingRunnable reschedulingRunnable = new ReschedulingRunnable(runnable, delay, Names.GENERIC, threadPool);
assertTrue(reschedulingRunnable.isCancelled());
}
Aggregations