Search in sources :

Example 6 with ICondition

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

the class ConditionAbstractTest method testSignal_whenOwnedByOtherThread.

@Test(timeout = 60000, expected = IllegalMonitorStateException.class)
public void testSignal_whenOwnedByOtherThread() throws InterruptedException {
    final ILock lock = callerInstance.getLock(newName());
    final ICondition condition = lock.newCondition(newName());
    releaseLockInSeparateThread(lock);
    condition.signal();
}
Also used : ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 7 with ICondition

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

the class ProducerConsumerConditionStressTest method test.

@Test
public void test() {
    HazelcastInstance[] instances = createHazelcastInstanceFactory(INSTANCE_COUNT).newInstances();
    HazelcastInstance hz = instances[0];
    ILock lock = hz.getLock(randomString());
    ICondition condition = lock.newCondition(randomString());
    ConsumerThread[] consumers = new ConsumerThread[CONSUMER_COUNT];
    for (int k = 0; k < consumers.length; k++) {
        ConsumerThread thread = new ConsumerThread(k, lock, condition);
        thread.start();
        consumers[k] = thread;
    }
    ProducerThread[] producers = new ProducerThread[PRODUCER_COUNT];
    for (int k = 0; k < producers.length; k++) {
        ProducerThread thread = new ProducerThread(k, lock, condition);
        thread.start();
        producers[k] = thread;
    }
    assertJoinable(600, producers);
    assertJoinable(600, consumers);
    for (TestThread consumer : consumers) {
        assertNull(consumer.throwable);
    }
    for (TestThread producer : producers) {
        assertNull(producer.throwable);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 8 with ICondition

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

the class ConditionAbstractTest method testSignalWithMultipleWaiters.

//if there are multiple waiters, then only 1 waiter should be notified.
@Test(timeout = 60000)
public void testSignalWithMultipleWaiters() throws InterruptedException {
    ILock lock = callerInstance.getLock(newName());
    ICondition condition = lock.newCondition(newName());
    CountDownLatch allAwaited = new CountDownLatch(3);
    CountDownLatch allSignalled = new CountDownLatch(10);
    startThreadWaitingOnCondition(lock, condition, allAwaited, allSignalled);
    startThreadWaitingOnCondition(lock, condition, allAwaited, allSignalled);
    startThreadWaitingOnCondition(lock, condition, allAwaited, allSignalled);
    assertOpenEventually("All threads should have been reached await", allAwaited);
    signal(lock, condition);
    assertCountEventually("Condition has not been signalled", 9, allSignalled, THIRTY_SECONDS);
    assertFalse(lock.isLocked());
}
Also used : ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 9 with ICondition

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

the class ConditionAbstractTest method testAwait_whenOwnedByOtherThread.

@Test(timeout = 60000, expected = IllegalMonitorStateException.class)
public void testAwait_whenOwnedByOtherThread() throws InterruptedException {
    final ILock lock = callerInstance.getLock(newName());
    final ICondition condition = lock.newCondition(newName());
    releaseLockInSeparateThread(lock);
    condition.await();
}
Also used : ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 10 with ICondition

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

the class ConditionAdvancedTest method testShutDownNode_whenOtherWaitingOnConditionAwait.

@Test(timeout = 60000, expected = HazelcastInstanceNotActiveException.class)
public void testShutDownNode_whenOtherWaitingOnConditionAwait() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
    nodeFactory.newHazelcastInstance();
    final String name = randomString();
    final ILock lock = instance.getLock(name);
    final ICondition condition = lock.newCondition("condition");
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        public void run() {
            try {
                latch.await(1, TimeUnit.MINUTES);
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance.shutdown();
        }
    }).start();
    lock.lock();
    try {
        latch.countDown();
        condition.await();
    } catch (InterruptedException e) {
    }
    lock.unlock();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ICondition (com.hazelcast.core.ICondition)37 Test (org.junit.Test)36 ILock (com.hazelcast.core.ILock)33 CountDownLatch (java.util.concurrent.CountDownLatch)22 ParallelTest (com.hazelcast.test.annotation.ParallelTest)11 QuickTest (com.hazelcast.test.annotation.QuickTest)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 TestThread (com.hazelcast.test.TestThread)6 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AssertTask (com.hazelcast.test.AssertTask)2 Config (com.hazelcast.config.Config)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Ignore (org.junit.Ignore)1