Search in sources :

Example 21 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method scheduleOnMemberWithRepetition.

@Test
public void scheduleOnMemberWithRepetition() {
    HazelcastInstance[] instances = createClusterWithCount(4);
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(4);
    Map<Member, IScheduledFuture<Object>> futures = s.scheduleOnAllMembersAtFixedRate(new ICountdownLatchRunnableTask("latch"), 0, 3, SECONDS);
    assertOpenEventually(latch);
    assertEquals(0, latch.getCount());
    assertEquals(4, futures.size());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method schedule_partitionAware_runnable.

@Test
public void schedule_partitionAware_runnable() {
    int delay = 1;
    String completionLatchName = "completionLatch";
    HazelcastInstance[] instances = createClusterWithCount(2);
    ICountDownLatch completionLatch = instances[0].getCPSubsystem().getCountDownLatch(completionLatchName);
    completionLatch.trySetCount(1);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    Runnable task = new PlainPartitionAwareRunnableTask(completionLatchName);
    IScheduledFuture first = executorService.schedule(task, delay, SECONDS);
    assertOpenEventually(completionLatch);
    ScheduledTaskHandler handler = first.getHandler();
    int expectedPartition = getPartitionIdFromPartitionAwareTask(instances[0], (PartitionAware) task);
    assertEquals(expectedPartition, handler.getPartitionId());
}
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 23 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method schedule_andCancel_onMember.

@Test
public void schedule_andCancel_onMember() {
    HazelcastInstance[] instances = createClusterWithCount(2);
    Member localMember = instances[0].getCluster().getLocalMember();
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    IScheduledFuture future = executorService.scheduleOnMemberAtFixedRate(new ICountdownLatchRunnableTask("latch"), localMember, 1, 1, SECONDS);
    sleepSeconds(5);
    assertFalse(future.isCancelled());
    assertFalse(future.isDone());
    future.cancel(false);
    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) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceBasicTest method schedule_withNamedInstanceAware_whenRemoteRun.

@Test
public void schedule_withNamedInstanceAware_whenRemoteRun() {
    HazelcastInstance[] instances = createClusterWithCount(2);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    MemberImpl member = getNodeEngineImpl(instances[1]).getLocalMember();
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    s.scheduleOnMember(TaskUtils.named("blah", new PlainInstanceAwareRunnableTask("latch")), member, 1, TimeUnit.SECONDS);
    assertOpenEventually(latch);
    assertEquals(0, latch.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 25 with ICountDownLatch

use of com.hazelcast.cp.ICountDownLatch in project hazelcast by hazelcast.

the class ScheduledExecutorServiceSlowTest method schedule_withLongSleepingCallable_blockingOnGet.

@Test
public void schedule_withLongSleepingCallable_blockingOnGet() throws Exception {
    int delay = 0;
    double expectedResult = 169.4;
    HazelcastInstance[] instances = createClusterWithCount(2);
    ICountDownLatch initCountLatch = instances[0].getCPSubsystem().getCountDownLatch("initCountLatchName");
    initCountLatch.trySetCount(1);
    ICountDownLatch waitCountLatch = instances[0].getCPSubsystem().getCountDownLatch("waitCountLatchName");
    waitCountLatch.trySetCount(1);
    ICountDownLatch doneCountLatch = instances[0].getCPSubsystem().getCountDownLatch("doneCountLatchName");
    doneCountLatch.trySetCount(1);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
    IScheduledFuture<Double> future = executorService.schedule(new ICountdownLatchCallableTask(initCountLatch.getName(), waitCountLatch.getName(), doneCountLatch.getName()), delay, SECONDS);
    assertOpenEventually(initCountLatch);
    int sleepPeriod = 10000;
    long start = System.currentTimeMillis();
    new Thread(() -> {
        sleepAtLeastMillis(sleepPeriod);
        waitCountLatch.countDown();
    }).start();
    double result = future.get();
    assertTrue(System.currentTimeMillis() - start > sleepPeriod);
    assertTrue(doneCountLatch.await(0, SECONDS));
    assertEquals(expectedResult, result, 0);
    assertTrue(future.isDone());
    assertFalse(future.isCancelled());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Aggregations

ICountDownLatch (com.hazelcast.cp.ICountDownLatch)32 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)31 Test (org.junit.Test)31 HazelcastInstance (com.hazelcast.core.HazelcastInstance)30 IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)27 QuickTest (com.hazelcast.test.annotation.QuickTest)21 IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)14 SlowTest (com.hazelcast.test.annotation.SlowTest)11 IAtomicLong (com.hazelcast.cp.IAtomicLong)3 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)3 Member (com.hazelcast.cluster.Member)2 RootCauseMatcher (com.hazelcast.internal.util.RootCauseMatcher)2 ScheduledTaskStatistics (com.hazelcast.scheduledexecutor.ScheduledTaskStatistics)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 Address (com.hazelcast.cluster.Address)1 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)1 Config (com.hazelcast.config.Config)1 MapConfig (com.hazelcast.config.MapConfig)1 CPSubsystem (com.hazelcast.cp.CPSubsystem)1 CountDownLatchService (com.hazelcast.cp.internal.datastructures.countdownlatch.CountDownLatchService)1