Search in sources :

Example 56 with IScheduledExecutorService

use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method getScheduled_whenTaskImplementingNamedTask.

@Test
public void getScheduled_whenTaskImplementingNamedTask() {
    int delay = 1;
    String taskName = NamedCallable.NAME;
    NamedCallable task = new NamedCallable();
    HazelcastInstance[] instances = createClusterWithCount(2);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    IScheduledFuture<Boolean> first = executorService.schedule(task, delay, SECONDS);
    ScheduledTaskHandler handler = first.getHandler();
    IScheduledFuture<Double> copy = executorService.getScheduledFuture(handler);
    assertEquals(first.getHandler(), copy.getHandler());
    assertEquals(copy.getHandler().getTaskName(), taskName);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 57 with IScheduledExecutorService

use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method handlerTaskAndSchedulerNames_withRunnable.

@Test
public void handlerTaskAndSchedulerNames_withRunnable() {
    int delay = 0;
    String schedulerName = ANY_EXECUTOR_NAME;
    String taskName = "TestRunnable";
    HazelcastInstance[] instances = createClusterWithCount(2);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    IScheduledExecutorService executorService = instances[0].getScheduledExecutorService(schedulerName);
    IScheduledFuture future = executorService.schedule(named(taskName, new ICountdownLatchRunnableTask("latch")), delay, SECONDS);
    assertOpenEventually(latch);
    ScheduledTaskHandler handler = future.getHandler();
    assertEquals(schedulerName, handler.getSchedulerName());
    assertEquals(taskName, handler.getTaskName());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 58 with IScheduledExecutorService

use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method cancelledAndDone_durable.

@Test
public void cancelledAndDone_durable() {
    HazelcastInstance[] instances = createClusterWithCount(3);
    Object key = generateKeyOwnedBy(instances[1]);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    IScheduledFuture future = executorService.scheduleOnKeyOwnerAtFixedRate(new ICountdownLatchRunnableTask("latch"), key, 0, 1, SECONDS);
    assertOpenEventually(latch);
    assertFalse(future.isCancelled());
    assertFalse(future.isDone());
    future.cancel(false);
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());
    instances[1].getLifecycleService().shutdown();
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 59 with IScheduledExecutorService

use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method scheduleOnKeyOwner_withNotPeriodicRunnable.

@Test
public void scheduleOnKeyOwner_withNotPeriodicRunnable() throws Exception {
    HazelcastInstance[] instances = createClusterWithCount(2);
    String key = generateKeyOwnedBy(instances[0]);
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    s.scheduleOnKeyOwner(new ICountdownLatchRunnableTask("latch"), key, 2, SECONDS).get();
    assertEquals(0, latch.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 60 with IScheduledExecutorService

use of com.hazelcast.scheduledexecutor.IScheduledExecutorService in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method scheduledAtFixedRate_generates_statistics_when_stats_enabled.

@Test
public void scheduledAtFixedRate_generates_statistics_when_stats_enabled() {
    // run task
    Config config = smallInstanceConfig();
    config.getScheduledExecutorConfig(ANY_EXECUTOR_NAME).setStatisticsEnabled(true);
    long now = System.currentTimeMillis();
    HazelcastInstance[] instances = createClusterWithCount(1, config);
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    CountDownLatch progress = new CountDownLatch(3);
    Semaphore suspend = new Semaphore(0);
    s.scheduleAtFixedRate(new CountableRunTask(progress, suspend), 1, 1, SECONDS);
    assertOpenEventually(progress);
    try {
        assertTrueEventually(() -> {
            // collect metrics
            Map<String, List<Long>> metrics = collectMetrics(SCHEDULED_EXECUTOR_PREFIX, instances);
            // check results
            assertMetricsCollected(metrics, 0, 0, 3, 2, 0, now, 0);
        });
    } finally {
        suspend.release();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) List(java.util.List) ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)94 HazelcastInstance (com.hazelcast.core.HazelcastInstance)83 Test (org.junit.Test)83 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)81 QuickTest (com.hazelcast.test.annotation.QuickTest)70 IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)38 ICountDownLatch (com.hazelcast.cp.ICountDownLatch)29 Config (com.hazelcast.config.Config)21 ScheduledExecutorConfig (com.hazelcast.config.ScheduledExecutorConfig)21 Member (com.hazelcast.cluster.Member)18 SlowTest (com.hazelcast.test.annotation.SlowTest)15 ArrayList (java.util.ArrayList)11 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)10 HashMap (java.util.HashMap)6 List (java.util.List)6 RootCauseMatcher (com.hazelcast.internal.util.RootCauseMatcher)4 ScheduledTaskStatistics (com.hazelcast.scheduledexecutor.ScheduledTaskStatistics)4 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 Address (com.hazelcast.cluster.Address)2 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)2