Search in sources :

Example 21 with ICondition

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

the class AbstractLockQuorumTest method testCondition.

void testCondition(ILock lock) {
    CountDownLatch signalArrived = new CountDownLatch(1);
    ICondition cond = lock.newCondition("condition");
    await(lock, cond, signalArrived);
    sleepSeconds(1);
    lock.lock();
    cond.signal();
    lock.unlock();
    assertOpenEventually(signalArrived);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition)

Example 22 with ICondition

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

the class LegacyClientConditionTest method testLockConditionSignalAll.

@Test
public void testLockConditionSignalAll() throws InterruptedException {
    final String name = "testLockConditionSimpleUsage";
    final ILock lock = client.getLock(name);
    final ICondition condition = lock.newCondition(name + "c");
    final AtomicInteger count = new AtomicInteger(0);
    final int k = 50;
    final CountDownLatch awaitLatch = new CountDownLatch(k);
    final CountDownLatch finalLatch = new CountDownLatch(k);
    for (int i = 0; i < k; i++) {
        new Thread(new Runnable() {

            public void run() {
                lock.lock();
                try {
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                    awaitLatch.countDown();
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                } catch (InterruptedException ignored) {
                } finally {
                    lock.unlock();
                    finalLatch.countDown();
                }
            }
        }).start();
    }
    awaitLatch.await(1, TimeUnit.MINUTES);
    lock.lock();
    condition.signalAll();
    lock.unlock();
    finalLatch.await(1, TimeUnit.MINUTES);
    assertEquals(k * 2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 23 with ICondition

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

the class ConditionAbstractTest method testAwaitTime_whenTimeout.

@Test
public void testAwaitTime_whenTimeout() throws InterruptedException {
    ILock lock = callerInstance.getLock(newName());
    ICondition condition = lock.newCondition(newName());
    lock.lock();
    boolean success = condition.await(1, TimeUnit.MILLISECONDS);
    assertFalse(success);
    assertTrue(lock.isLockedByCurrentThread());
}
Also used : ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 24 with ICondition

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

the class ConditionAbstractTest method testConditionsWithSameNameButDifferentLocksAreIndependent.

@Test(timeout = 60000)
public void testConditionsWithSameNameButDifferentLocksAreIndependent() throws InterruptedException {
    String name = newName();
    ILock lock0 = callerInstance.getLock(newName());
    ICondition condition0 = lock0.newCondition(name);
    ILock lock1 = callerInstance.getLock(newName());
    ICondition condition1 = lock1.newCondition(name);
    CountDownLatch allAwaited = new CountDownLatch(2);
    CountDownLatch allSignalled = new CountDownLatch(2);
    startThreadWaitingOnCondition(lock0, condition0, allAwaited, allSignalled);
    startThreadWaitingOnCondition(lock1, condition1, allAwaited, allSignalled);
    assertOpenEventually("All threads should have been reached await", allAwaited);
    signalAll(lock0, condition0);
    signalAll(lock1, condition1);
    assertOpenEventually(allSignalled);
}
Also used : ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 25 with ICondition

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

the class ConditionAbstractTest method testAwaitOnConditionOfFreeLock.

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

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