Search in sources :

Example 6 with Cancellable

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

Example 7 with Cancellable

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

Aggregations

Cancellable (org.elasticsearch.threadpool.ThreadPool.Cancellable)7 ReschedulingRunnable (org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Term (org.apache.lucene.index.Term)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 TimeValue (org.elasticsearch.common.unit.TimeValue)1 BaseFuture (org.elasticsearch.common.util.concurrent.BaseFuture)1 Engine (org.elasticsearch.index.engine.Engine)1 InternalEngine (org.elasticsearch.index.engine.InternalEngine)1 SingleFieldsVisitor (org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor)1 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)1 ThreadPool (org.elasticsearch.threadpool.ThreadPool)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1