Search in sources :

Example 11 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 12 with ICountDownLatch

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

the class CountDownLatchAdvancedTest method testDestroy.

@Test
public void testDestroy() {
    HazelcastInstance instance = createHazelcastInstance();
    ICountDownLatch latch = instance.getCountDownLatch(randomString());
    NodeEngineImpl nodeEngine = getNode(instance).getNodeEngine();
    CountDownLatchService service = nodeEngine.getService(CountDownLatchService.SERVICE_NAME);
    latch.destroy();
    assertFalse(service.containsLatch(latch.getName()));
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) 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 13 with ICountDownLatch

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

the class CountDownLatchAdvancedTest method testAwait_whenInstanceShutdown_thenHazelcastInstanceNotActiveException.

@Test
public void testAwait_whenInstanceShutdown_thenHazelcastInstanceNotActiveException() throws InterruptedException {
    HazelcastInstance instance = createHazelcastInstance();
    final ICountDownLatch latch = instance.getCountDownLatch(randomString());
    latch.trySetCount(10);
    final TestThread awaitThread = new TestThread() {

        @Override
        public void doRun() throws Exception {
            latch.await(1, TimeUnit.HOURS);
        }
    };
    awaitThread.start();
    // give the awaitthread some time to get in the waiting state
    sleepSeconds(5);
    instance.shutdown();
    awaitThread.assertFailsEventually(HazelcastInstanceNotActiveException.class);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestThread(com.hazelcast.test.TestThread) ICountDownLatch(com.hazelcast.core.ICountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with ICountDownLatch

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

the class CountDownLatchAdvancedTest method testLatchDestroyed.

@Test(expected = DistributedObjectDestroyedException.class)
public void testLatchDestroyed() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance hz1 = factory.newHazelcastInstance();
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    final ICountDownLatch latch = hz1.getCountDownLatch("test");
    latch.trySetCount(2);
    new TestThread() {

        public void doRun() throws Exception {
            sleep(1000);
            latch.destroy();
        }
    }.start();
    hz2.getCountDownLatch("test").await(5, TimeUnit.SECONDS);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestThread(com.hazelcast.test.TestThread) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) DistributedObjectDestroyedException(com.hazelcast.spi.exception.DistributedObjectDestroyedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 15 with ICountDownLatch

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

the class MBeanDestroyTest method testCountDownLatch.

@Test
public void testCountDownLatch() throws Exception {
    ICountDownLatch countDownLatch = holder.getHz().getCountDownLatch("semaphore");
    countDownLatch.getCount();
    holder.assertMBeanExistEventually("ICountDownLatch", countDownLatch.getName());
    destroyObjectAndAssert(countDownLatch, "ICountDownLatch");
}
Also used : ICountDownLatch(com.hazelcast.core.ICountDownLatch) 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