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);
}
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());
}
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());
}
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);
}
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();
}
Aggregations