Search in sources :

Example 1 with ICountDownLatch

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

the class CountDownLatchAdvancedTest method testCountDown_whenReachZero_thenLatchRemoved.

@Test
public void testCountDown_whenReachZero_thenLatchRemoved() {
    HazelcastInstance instance = createHazelcastInstance();
    ICountDownLatch latch = instance.getCountDownLatch(randomString());
    CountDownLatchService service = getNode(instance).getNodeEngine().getService(CountDownLatchService.SERVICE_NAME);
    latch.trySetCount(1);
    assertTrue(service.containsLatch(latch.getName()));
    latch.countDown();
    assertFalse(service.containsLatch(latch.getName()));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICountDownLatch(com.hazelcast.core.ICountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with ICountDownLatch

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

the class CountDownLatchMigrationTest method testLatchMigration.

@Test
public void testLatchMigration() throws InterruptedException {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(5);
    HazelcastInstance hz1 = factory.newHazelcastInstance();
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    warmUpPartitions(hz2, hz1);
    ICountDownLatch latch1 = hz1.getCountDownLatch("test");
    latch1.trySetCount(10);
    Thread.sleep(500);
    ICountDownLatch latch2 = hz2.getCountDownLatch("test");
    assertEquals(10, latch2.getCount());
    latch2.countDown();
    assertEquals(9, latch1.getCount());
    hz1.shutdown();
    assertEquals(9, latch2.getCount());
    HazelcastInstance hz3 = factory.newHazelcastInstance();
    warmUpPartitions(hz3);
    ICountDownLatch latch3 = hz3.getCountDownLatch("test");
    latch3.countDown();
    assertEquals(8, latch3.getCount());
    hz2.shutdown();
    latch3.countDown();
    assertEquals(7, latch3.getCount());
    HazelcastInstance hz4 = factory.newHazelcastInstance();
    HazelcastInstance hz5 = factory.newHazelcastInstance();
    warmUpPartitions(hz5, hz4);
    Thread.sleep(250);
    hz3.shutdown();
    ICountDownLatch latch4 = hz4.getCountDownLatch("test");
    assertEquals(7, latch4.getCount());
    ICountDownLatch latch5 = hz5.getCountDownLatch("test");
    latch5.countDown();
    assertEquals(6, latch5.getCount());
    latch5.countDown();
    assertEquals(5, latch4.getCount());
    assertEquals(5, latch5.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ICountDownLatch

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

the class CountDownLatchSplitBrainTest method testCountDownLatchSplitBrain.

@Test
public void testCountDownLatchSplitBrain() throws InterruptedException {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    Config config = newConfig();
    HazelcastInstance h1 = factory.newHazelcastInstance(config);
    HazelcastInstance h2 = factory.newHazelcastInstance(config);
    HazelcastInstance h3 = factory.newHazelcastInstance(config);
    warmUpPartitions(h1, h2, h3);
    String name = generateKeyOwnedBy(h3);
    ICountDownLatch countDownLatch1 = h1.getCountDownLatch(name);
    ICountDownLatch countDownLatch3 = h3.getCountDownLatch(name);
    countDownLatch3.trySetCount(5);
    waitAllForSafeState(h1, h2, h3);
    // create split: [h1, h2] & [h3]
    closeConnectionBetween(h1, h3);
    closeConnectionBetween(h2, h3);
    assertClusterSizeEventually(2, h1);
    assertClusterSizeEventually(2, h2);
    assertClusterSizeEventually(1, h3);
    // modify both latches after split with different counts
    // count of h1 & h2 = 4
    countDownLatch1.countDown();
    // count of h3 = 0
    while (countDownLatch3.getCount() > 0) {
        countDownLatch3.countDown();
    }
    // merge back
    getNode(h3).getClusterService().merge(getAddress(h1));
    assertClusterSizeEventually(3, h1);
    assertClusterSizeEventually(3, h2);
    assertClusterSizeEventually(3, h3);
    // latch count should be equal to the count of larger cluster
    assertEquals(4, countDownLatch3.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with ICountDownLatch

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

the class CountDownLatchAdvancedTest method testSimpleUsage.

@Test
public void testSimpleUsage() throws InterruptedException {
    final int k = 5;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances();
    ICountDownLatch latch = instances[0].getCountDownLatch("test");
    latch.trySetCount(k - 1);
    assertEquals(k - 1, latch.getCount());
    new Thread() {

        public void run() {
            for (int i = 1; i < k; i++) {
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                final ICountDownLatch l = instances[i].getCountDownLatch("test");
                l.countDown();
                assertEquals(k - 1 - i, l.getCount());
            }
        }
    }.start();
    assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
    assertEquals(0, latch.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TestThread(com.hazelcast.test.TestThread) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ICountDownLatch

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

the class ScheduledExecutorServiceBasicTest method getErroneous.

@Test
public void getErroneous() throws InterruptedException, ExecutionException {
    int delay = 2;
    String taskName = "Test";
    String completionLatchName = "completionLatch";
    HazelcastInstance[] instances = createClusterWithCount(2);
    String key = generateKeyOwnedBy(instances[1]);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, "s");
    ICountDownLatch latch = instances[1].getCountDownLatch(completionLatchName);
    latch.trySetCount(1);
    IScheduledFuture<Double> future = executorService.scheduleOnKeyOwner(named(taskName, new ErroneousCallableTask(completionLatchName)), key, delay, SECONDS);
    latch.await(10, SECONDS);
    expected.expect(ExecutionException.class);
    expected.expectCause(new RootCauseMatcher(IllegalStateException.class, "Erroneous task"));
    future.get();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICountDownLatch(com.hazelcast.core.ICountDownLatch) RootCauseMatcher(com.hazelcast.util.RootCauseMatcher) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ICountDownLatch (com.hazelcast.core.ICountDownLatch)37 ParallelTest (com.hazelcast.test.annotation.ParallelTest)37 Test (org.junit.Test)37 HazelcastInstance (com.hazelcast.core.HazelcastInstance)35 QuickTest (com.hazelcast.test.annotation.QuickTest)27 SlowTest (com.hazelcast.test.annotation.SlowTest)10 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)5 TestThread (com.hazelcast.test.TestThread)3 IAtomicLong (com.hazelcast.core.IAtomicLong)2 RootCauseMatcher (com.hazelcast.util.RootCauseMatcher)2 CountDownLatchService (com.hazelcast.concurrent.countdownlatch.CountDownLatchService)1 Config (com.hazelcast.config.Config)1 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 Member (com.hazelcast.core.Member)1 DistributedObjectDestroyedException (com.hazelcast.spi.exception.DistributedObjectDestroyedException)1 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)1