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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations