Search in sources :

Example 16 with ICondition

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

the class ConditionAbstractTest method testAwaitUntil_whenFail.

@Test(timeout = 60000)
public void testAwaitUntil_whenFail() throws InterruptedException {
    ILock lock = callerInstance.getLock(newName());
    ICondition condition = lock.newCondition(newName());
    lock.lock();
    assertFalse(condition.awaitUntil(currentTimeAfterGivenMillis(1000)));
}
Also used : ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 17 with ICondition

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

the class ConditionAbstractTest method testAwaitTimeout_whenFail.

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

Example 18 with ICondition

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

the class ConditionAbstractTest method testSameConditionRetrievedMultipleTimesForSameLock.

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

Example 19 with ICondition

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

the class ConditionAbstractTest method testMultipleConditionsForSameLock.

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

Example 20 with ICondition

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

the class ConditionAbstractTest method testSignalAllWithSingleWaiter.

@Test(timeout = 60000)
public void testSignalAllWithSingleWaiter() throws InterruptedException {
    String lockName = newName();
    String conditionName = newName();
    final ILock lock = callerInstance.getLock(lockName);
    final ICondition condition = lock.newCondition(conditionName);
    final AtomicInteger count = new AtomicInteger(0);
    final int k = 50;
    final CountDownLatch allAwaited = new CountDownLatch(k);
    final CountDownLatch allFinished = new CountDownLatch(k);
    for (int i = 0; i < k; i++) {
        new Thread(new Runnable() {

            public void run() {
                try {
                    lock.lock();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                    allAwaited.countDown();
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                } catch (InterruptedException ignored) {
                } finally {
                    lock.unlock();
                    allFinished.countDown();
                }
            }
        }).start();
    }
    allAwaited.await(1, TimeUnit.MINUTES);
    assertUnlockedEventually(lock, THIRTY_SECONDS);
    // Make sure that all threads are waiting on condition await call
    Thread.sleep(3000);
    signalAll(lock, condition);
    allFinished.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) TestThread(com.hazelcast.test.TestThread) 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