Search in sources :

Example 6 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method schedule_withMapChanges_durable.

@Test
public void schedule_withMapChanges_durable() {
    HazelcastInstance[] instances = createClusterWithCount(2);
    IMap<String, Integer> map = instances[1].getMap("map");
    map.put("foo", 1);
    Object key = generateKeyOwnedBy(instances[0]);
    ICountDownLatch startedLatch = instances[1].getCPSubsystem().getCountDownLatch("startedLatch");
    ICountDownLatch finishedLatch = instances[1].getCPSubsystem().getCountDownLatch("finishedLatch");
    ICountDownLatch waitAfterStartLatch = instances[1].getCPSubsystem().getCountDownLatch("waitAfterStartLatch");
    startedLatch.trySetCount(1);
    finishedLatch.trySetCount(1);
    waitAfterStartLatch.trySetCount(1);
    IAtomicLong runEntryCounter = instances[1].getCPSubsystem().getAtomicLong("runEntryCounterName");
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    executorService.scheduleOnKeyOwner(new ICountdownLatchMapIncrementCallableTask("map", "runEntryCounterName", "startedLatch", "finishedLatch", "waitAfterStartLatch"), key, 0, SECONDS);
    assertOpenEventually(startedLatch);
    instances[0].getLifecycleService().shutdown();
    waitAfterStartLatch.countDown();
    assertOpenEventually(finishedLatch);
    assertEquals(2, (int) map.get("foo"));
    assertEquals(2, runEntryCounter.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) IAtomicLong(com.hazelcast.cp.IAtomicLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method scheduleOnKeyOwnerWithRepetition.

@Test
public void scheduleOnKeyOwnerWithRepetition() {
    String key = "TestKey";
    HazelcastInstance[] instances = createClusterWithCount(2);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(5);
    IScheduledFuture future = executorService.scheduleOnKeyOwnerAtFixedRate(new ICountdownLatchRunnableTask("latch"), key, 0, 1, SECONDS);
    ScheduledTaskHandler handler = future.getHandler();
    int expectedPartition = instances[0].getPartitionService().getPartition(key).getPartitionId();
    assertEquals(expectedPartition, handler.getPartitionId());
    assertOpenEventually(latch);
    assertEquals(0, latch.getCount());
}
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 8 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method scheduleWithRepetition.

@Test
public void scheduleWithRepetition() {
    HazelcastInstance[] instances = createClusterWithCount(2);
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(3);
    IScheduledFuture future = s.scheduleAtFixedRate(new ICountdownLatchRunnableTask("latch"), 0, 1, SECONDS);
    assertOpenEventually(latch);
    future.cancel(false);
    assertEquals(0, latch.getCount());
}
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 9 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method schedule_withLongSleepingCallable_cancelledAndGet.

@Test
public void schedule_withLongSleepingCallable_cancelledAndGet() {
    int delay = 0;
    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, ANY_EXECUTOR_NAME);
    IScheduledFuture<Double> future = executorService.schedule(new ICountdownLatchCallableTask(initCountLatch.getName(), waitCountLatch.getName(), doneCountLatch.getName()), delay, SECONDS);
    assertOpenEventually(initCountLatch);
    assertTrue(future.cancel(false));
    waitCountLatch.countDown();
    assertOpenEventually(doneCountLatch);
    assertTrue(future.isDone());
    assertTrue(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) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method schedule_withNamedInstanceAware_whenLocalRun.

@Test
public void schedule_withNamedInstanceAware_whenLocalRun() {
    HazelcastInstance[] instances = createClusterWithCount(1);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    IScheduledExecutorService s = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    s.schedule(TaskUtils.named("blah", new PlainInstanceAwareRunnableTask("latch")), 1, TimeUnit.SECONDS);
    assertOpenEventually(latch);
    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)

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