Search in sources :

Example 1 with ReschedulingRunnable

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

Example 2 with 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());
}
Also used : ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable) Matchers.containsString(org.hamcrest.Matchers.containsString) TimeValue(org.elasticsearch.common.unit.TimeValue) ScheduledFuture(java.util.concurrent.ScheduledFuture) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

TimeValue (org.elasticsearch.common.unit.TimeValue)2 ReschedulingRunnable (org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1