Search in sources :

Example 81 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project hazelcast by hazelcast.

the class ExecutionDelayTest method testExecutorRetriesTask_whenOneNodeShutdowns.

@Test
public void testExecutorRetriesTask_whenOneNodeShutdowns() throws InterruptedException, ExecutionException {
    final int taskCount = 20;
    ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
    try {
        ex.schedule(new Runnable() {

            @Override
            public void run() {
                instances.get(1).shutdown();
            }
        }, 1000, TimeUnit.MILLISECONDS);
        Task task = new Task();
        runClient(task, taskCount);
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                final int taskExecutions = COUNTER.get();
                assertTrue(taskExecutions >= taskCount);
            }
        });
    } finally {
        ex.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AssertTask(com.hazelcast.test.AssertTask) AssertTask(com.hazelcast.test.AssertTask) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 82 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project hazelcast by hazelcast.

the class ExecutionDelayTest method testExecutorRetriesTask_whenOneNodeTerminates.

@Test
public void testExecutorRetriesTask_whenOneNodeTerminates() throws InterruptedException, ExecutionException {
    final int taskCount = 20;
    ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
    try {
        ex.schedule(new Runnable() {

            @Override
            public void run() {
                instances.get(1).getLifecycleService().terminate();
            }
        }, 1000, TimeUnit.MILLISECONDS);
        Task task = new Task();
        runClient(task, taskCount);
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                final int taskExecutions = COUNTER.get();
                assertTrue(taskExecutions >= taskCount);
            }
        });
    } finally {
        ex.shutdown();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AssertTask(com.hazelcast.test.AssertTask) AssertTask(com.hazelcast.test.AssertTask) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 83 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project musiccabinet by hakko.

the class UpdateNowPlayingService method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    taskExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                receive();
            } catch (Throwable t) {
                LOG.error("Unexpected error caught while receiving scrobbles!", t);
            }
        }
    });
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                scrobbleTracks();
            } catch (Throwable t) {
                LOG.error("Unexpected error caught while scrobbling!", t);
            }
        }
    }, 1, 1, TimeUnit.MINUTES);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 84 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project guava by google.

the class MoreExecutorsTest method testGetScheduledExitingExcutorService_shutdownHookRegistered.

public void testGetScheduledExitingExcutorService_shutdownHookRegistered() throws InterruptedException {
    TestApplication application = new TestApplication();
    ScheduledThreadPoolExecutor executor = mock(ScheduledThreadPoolExecutor.class);
    ThreadFactory threadFactory = mock(ThreadFactory.class);
    when(executor.getThreadFactory()).thenReturn(threadFactory);
    ScheduledExecutorService unused = application.getExitingScheduledExecutorService(executor);
    application.shutdown();
    verify(executor).shutdown();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 85 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project guava by google.

the class AbstractScheduledServiceTest method testTimeout.

public void testTimeout() {
    // Create a service whose executor will never run its commands
    Service service = new AbstractScheduledService() {

        @Override
        protected Scheduler scheduler() {
            return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.NANOSECONDS);
        }

        @Override
        protected ScheduledExecutorService executor() {
            return TestingExecutors.noOpScheduledExecutor();
        }

        @Override
        protected void runOneIteration() throws Exception {
        }

        @Override
        protected String serviceName() {
            return "Foo";
        }
    };
    try {
        service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS);
        fail("Expected timeout");
    } catch (TimeoutException e) {
        assertThat(e).hasMessage("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)285 Test (org.junit.Test)81 ExecutorService (java.util.concurrent.ExecutorService)37 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)28 IOException (java.io.IOException)27 ArrayList (java.util.ArrayList)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 HashMap (java.util.HashMap)23 Map (java.util.Map)20 List (java.util.List)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 HashSet (java.util.HashSet)13 ThreadFactory (java.util.concurrent.ThreadFactory)13 TimeUnit (java.util.concurrent.TimeUnit)13