use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAdvancedTest method testDestroyLock_whenOtherWaitingOnConditionAwait.
@Test(timeout = 60000, expected = DistributedObjectDestroyedException.class)
public void testDestroyLock_whenOtherWaitingOnConditionAwait() throws InterruptedException {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
final ILock lock = instance.getLock(randomString());
final ICondition condition = lock.newCondition("condition");
final CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {
public void run() {
try {
latch.await(30, TimeUnit.SECONDS);
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.destroy();
}
}).start();
lock.lock();
latch.countDown();
condition.await();
lock.unlock();
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAdvancedTest method testInterruptionDuringWaiting.
@Test(timeout = 60000)
public void testInterruptionDuringWaiting() throws InterruptedException {
Config config = new Config();
// the system should wait at most 5000 ms in order to determine the operation status
config.setProperty(GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "5000");
HazelcastInstance instance = createHazelcastInstance(config);
final ILock lock = instance.getLock(randomString());
final ICondition condition0 = lock.newCondition(randomString());
final CountDownLatch latch = new CountDownLatch(1);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
condition0.await();
} catch (InterruptedException e) {
latch.countDown();
}
}
});
thread.start();
sleepSeconds(2);
thread.interrupt();
assertOpenEventually(latch);
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitExpiration_whenLockIsNotAcquired.
@Test
public void testAwaitExpiration_whenLockIsNotAcquired() throws InterruptedException {
String lockName = newName();
String conditionName = newName();
final ILock lock = callerInstance.getLock(lockName);
final ICondition condition = lock.newCondition(conditionName);
final AtomicInteger expires = new AtomicInteger(0);
final int awaitCount = 10;
final CountDownLatch awaitLatch = new CountDownLatch(awaitCount);
for (int i = 0; i < awaitCount; i++) {
new Thread(new Runnable() {
public void run() {
try {
lock.lock();
awaitLatch.countDown();
boolean signalled = condition.await(1, TimeUnit.SECONDS);
assertFalse(signalled);
expires.incrementAndGet();
} catch (InterruptedException ignored) {
} finally {
lock.unlock();
}
}
}).start();
}
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(awaitCount, expires.get());
}
});
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitTimeout_whenSuccess.
@Test(timeout = 60000)
public void testAwaitTimeout_whenSuccess() throws InterruptedException {
final ILock lock = callerInstance.getLock(newName());
final ICondition condition = lock.newCondition(newName());
final CountDownLatch locked = new CountDownLatch(1);
final AtomicBoolean signalledCorrectly = new AtomicBoolean(false);
new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
locked.countDown();
try {
if (condition.await(10, TimeUnit.SECONDS)) {
signalledCorrectly.set(true);
}
} catch (InterruptedException e) {
ignore(e);
}
}
}).start();
locked.await();
signal(lock, condition);
assertAtomicEventually("awaiting thread should have been signalled", true, signalledCorrectly, THIRTY_SECONDS);
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testSignalIsNotStored.
// A signal send to wake up threads is not a flag set on the condition
// Threads calling await() after signal() has been called will hand on waiting.
@Test(timeout = 60000)
public void testSignalIsNotStored() throws InterruptedException {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition(newName());
CountDownLatch signalled = new CountDownLatch(1);
signal(lock, condition);
startThreadWaitingOnCondition(lock, condition, new CountDownLatch(0), signalled);
assertFalse("The time should elapse but the latch reached zero unexpectedly", signalled.await(3000, TimeUnit.MILLISECONDS));
}
Aggregations