use of org.opensearch.threadpool.Scheduler.ReschedulingRunnable in project OpenSearch by opensearch-project.
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 ScheduledCancellable schedule(Runnable command, TimeValue delay, String executor) {
if (command instanceof ReschedulingRunnable) {
((ReschedulingRunnable) command).onRejection(new OpenSearchRejectedExecutionException());
} 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, (e) -> {
}, (e) -> {
});
assertTrue(reschedulingRunnable.isCancelled());
}
use of org.opensearch.threadpool.Scheduler.ReschedulingRunnable in project OpenSearch by opensearch-project.
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, (e) -> {
}, (e) -> {
});
// this call was made during construction of the runnable
verify(threadPool, times(1)).schedule(reschedulingRunnable, delay, Names.GENERIC);
// 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(reschedulingRunnable, delay, Names.GENERIC);
}
Aggregations