Search in sources :

Example 1 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project camel by apache.

the class AmazonSQSClientMock method deleteMessage.

@Override
public DeleteMessageResult deleteMessage(DeleteMessageRequest deleteMessageRequest) throws AmazonClientException {
    String receiptHandle = deleteMessageRequest.getReceiptHandle();
    if (inFlight.containsKey(receiptHandle)) {
        ScheduledFuture inFlightTask = inFlight.get(receiptHandle);
        inFlightTask.cancel(true);
    }
    return new DeleteMessageResult();
}
Also used : DeleteMessageResult(com.amazonaws.services.sqs.model.DeleteMessageResult) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 2 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project flink by apache.

the class TimerServiceTest method testUnregisterAllTimeouts.

/**
	 * Test all timeouts registered can be unregistered
	 * @throws Exception
   */
@Test
@SuppressWarnings("unchecked")
public void testUnregisterAllTimeouts() throws Exception {
    // Prepare all instances.
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    ScheduledFuture scheduledFuture = mock(ScheduledFuture.class);
    when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class))).thenReturn(scheduledFuture);
    TimerService<AllocationID> timerService = new TimerService<>(scheduledExecutorService, 100L);
    TimeoutListener<AllocationID> listener = mock(TimeoutListener.class);
    timerService.start(listener);
    // Invoke register and unregister.
    timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
    timerService.registerTimeout(new AllocationID(), 10, TimeUnit.SECONDS);
    timerService.unregisterAllTimeouts();
    // Verify.
    Map<?, ?> timeouts = (Map<?, ?>) Whitebox.getInternalState(timerService, "timeouts");
    assertTrue(timeouts.isEmpty());
    verify(scheduledFuture, times(2)).cancel(true);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AllocationID(org.apache.flink.runtime.clusterframework.types.AllocationID) TimeUnit(java.util.concurrent.TimeUnit) Map(java.util.Map) ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.Test)

Example 3 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project hbase by apache.

the class JmxCacheBuster method stop.

/**
   * Stops the clearing of JMX metrics and restarting the Hadoop metrics system. This is needed for
   * some test environments where we manually inject sources or sinks dynamically.
   */
@VisibleForTesting
public static void stop() {
    stopped.set(true);
    ScheduledFuture future = fut.get();
    future.cancel(false);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project spring-boot-admin by codecentric.

the class StatusUpdateApplicationListenerTest method test_start_stop.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test_start_stop() throws Exception {
    StatusUpdater statusUpdater = mock(StatusUpdater.class);
    ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
    StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(statusUpdater, scheduler);
    ScheduledFuture task = mock(ScheduledFuture.class);
    when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
    listener.onApplicationReady(new ApplicationReadyEvent(mock(SpringApplication.class), null, mock(ConfigurableWebApplicationContext.class)));
    verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
    listener.onContextClosed(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
    verify(task).cancel(true);
}
Also used : ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ScheduledFuture(java.util.concurrent.ScheduledFuture) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Example 5 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project elasticsearch by elastic.

the class AsyncBulkByScrollActionTests method testScrollDelay.

public void testScrollDelay() throws Exception {
    /*
         * Replace the thread pool with one that will save the delay sent for the command. We'll use that to check that we used a proper
         * delay for throttling.
         */
    AtomicReference<TimeValue> capturedDelay = new AtomicReference<>();
    AtomicReference<Runnable> capturedCommand = new AtomicReference<>();
    setupClient(new TestThreadPool(getTestName()) {

        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String name, Runnable command) {
            capturedDelay.set(delay);
            capturedCommand.set(command);
            return null;
        }
    });
    DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction();
    action.setScroll(scrollId());
    // Set the base for the scroll to wait - this is added to the figure we calculate below
    firstSearchRequest.scroll(timeValueSeconds(10));
    // Set throttle to 1 request per second to make the math simpler
    testTask.rethrottle(1f);
    // Make the last batch look nearly instant but have 100 documents
    action.startNextScroll(timeValueNanos(System.nanoTime()), 100);
    // So the next request is going to have to wait an extra 100 seconds or so (base was 10 seconds, so 110ish)
    assertThat(client.lastScroll.get().request.scroll().keepAlive().seconds(), either(equalTo(110L)).or(equalTo(109L)));
    // Now we can simulate a response and check the delay that we used for the task
    SearchHit hit = new SearchHit(0, "id", new Text("type"), emptyMap());
    SearchHits hits = new SearchHits(new SearchHit[] { hit }, 0, 0);
    InternalSearchResponse internalResponse = new InternalSearchResponse(hits, null, null, null, false, false, 1);
    SearchResponse searchResponse = new SearchResponse(internalResponse, scrollId(), 5, 4, randomLong(), null);
    if (randomBoolean()) {
        client.lastScroll.get().listener.onResponse(searchResponse);
        // The delay is still 100ish seconds because there hasn't been much time between when we requested the bulk and when we got it.
        assertThat(capturedDelay.get().seconds(), either(equalTo(100L)).or(equalTo(99L)));
    } else {
        // Let's rethrottle between the starting the scroll and getting the response
        testTask.rethrottle(10f);
        client.lastScroll.get().listener.onResponse(searchResponse);
        // The delay uses the new throttle
        assertThat(capturedDelay.get().seconds(), either(equalTo(10L)).or(equalTo(9L)));
    }
    // Running the command ought to increment the delay counter on the task.
    capturedCommand.get().run();
    assertEquals(capturedDelay.get(), testTask.getStatus().getThrottled());
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) AtomicReference(java.util.concurrent.atomic.AtomicReference) Text(org.elasticsearch.common.text.Text) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ScheduledFuture(java.util.concurrent.ScheduledFuture) SearchResponse(org.elasticsearch.action.search.SearchResponse) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) SearchHits(org.elasticsearch.search.SearchHits) TimeValue(org.elasticsearch.common.unit.TimeValue) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)83 Test (org.junit.Test)27 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Date (java.util.Date)9 IOException (java.io.IOException)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 Map (java.util.Map)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 None (com.linkedin.common.util.None)5 D2Client (com.linkedin.d2.balancer.D2Client)5 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 JSONObject (org.json.simple.JSONObject)5 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4