Search in sources :

Example 1 with BaseFuture

use of org.elasticsearch.common.util.concurrent.BaseFuture in project elasticsearch by elastic.

the class ScheduleWithFixedDelayTests method testBlockingCallOnSchedulerThreadFails.

public void testBlockingCallOnSchedulerThreadFails() throws Exception {
    final BaseFuture<Object> future = new BaseFuture<Object>() {
    };
    final TestFuture resultsFuture = new TestFuture();
    final boolean getWithTimeout = randomBoolean();
    final Runnable runnable = () -> {
        try {
            Object obj;
            if (getWithTimeout) {
                obj = future.get(1L, TimeUnit.SECONDS);
            } else {
                obj = future.get();
            }
            resultsFuture.futureDone(obj);
        } catch (Throwable t) {
            resultsFuture.futureDone(t);
        }
    };
    Cancellable cancellable = threadPool.scheduleWithFixedDelay(runnable, TimeValue.timeValueMillis(10L), Names.SAME);
    Object resultingObject = resultsFuture.get();
    assertNotNull(resultingObject);
    assertThat(resultingObject, instanceOf(Throwable.class));
    Throwable t = (Throwable) resultingObject;
    assertThat(t, instanceOf(AssertionError.class));
    assertThat(t.getMessage(), containsString("Blocking"));
    assertFalse(cancellable.isCancelled());
}
Also used : BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) Cancellable(org.elasticsearch.threadpool.ThreadPool.Cancellable) ReschedulingRunnable(org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable)

Example 2 with BaseFuture

use of org.elasticsearch.common.util.concurrent.BaseFuture in project elasticsearch by elastic.

the class ClusterServiceTests method testBlockingCallInClusterStateTaskListenerFails.

public void testBlockingCallInClusterStateTaskListenerFails() throws InterruptedException {
    assumeTrue("assertions must be enabled for this test to work", BaseFuture.class.desiredAssertionStatus());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AssertionError> assertionRef = new AtomicReference<>();
    clusterService.submitStateUpdateTask("testBlockingCallInClusterStateTaskListenerFails", new Object(), ClusterStateTaskConfig.build(Priority.NORMAL), new ClusterStateTaskExecutor<Object>() {

        @Override
        public ClusterTasksResult<Object> execute(ClusterState currentState, List<Object> tasks) throws Exception {
            ClusterState newClusterState = ClusterState.builder(currentState).build();
            return ClusterTasksResult.builder().successes(tasks).build(newClusterState);
        }
    }, new ClusterStateTaskListener() {

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            BaseFuture<Void> future = new BaseFuture<Void>() {
            };
            try {
                if (randomBoolean()) {
                    future.get(1L, TimeUnit.SECONDS);
                } else {
                    future.get();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } catch (AssertionError e) {
                assertionRef.set(e);
                latch.countDown();
            }
        }

        @Override
        public void onFailure(String source, Exception e) {
        }
    });
    latch.await();
    assertNotNull(assertionRef.get());
    assertThat(assertionRef.get().getMessage(), containsString("not be the cluster state update thread. Reason: [Blocking operation]"));
}
Also used : BaseFuture(org.elasticsearch.common.util.concurrent.BaseFuture) ClusterState(org.elasticsearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ClusterStateTaskListener(org.elasticsearch.cluster.ClusterStateTaskListener)

Aggregations

BaseFuture (org.elasticsearch.common.util.concurrent.BaseFuture)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ClusterStateTaskListener (org.elasticsearch.cluster.ClusterStateTaskListener)1 Cancellable (org.elasticsearch.threadpool.ThreadPool.Cancellable)1 ReschedulingRunnable (org.elasticsearch.threadpool.ThreadPool.ReschedulingRunnable)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1