Search in sources :

Example 1 with ReschedulingRunnable

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());
}
Also used : ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with ReschedulingRunnable

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);
}
Also used : ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) ReschedulingRunnable(org.opensearch.threadpool.Scheduler.ReschedulingRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

TimeValue (org.opensearch.common.unit.TimeValue)2 ReschedulingRunnable (org.opensearch.threadpool.Scheduler.ReschedulingRunnable)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)1