use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitUntil_whenSuccess.
@Test(timeout = 60000)
public void testAwaitUntil_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.awaitUntil(currentTimeAfterGivenMillis(10000))) {
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 testSignalOnConditionOfFreeLock.
@Test(timeout = 60000, expected = IllegalMonitorStateException.class)
public void testSignalOnConditionOfFreeLock() {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition("condition");
condition.signal();
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testContendedLockUnlockWithVeryShortAwait.
/**
* Testcase for #3025. Tests that an await with short duration in a highly-contended lock does not generate an
* IllegalStateException (previously due to a race condition in the waiter list for the condition).
*/
@Test(timeout = 60000)
public void testContendedLockUnlockWithVeryShortAwait() throws InterruptedException {
String lockName = newName();
String conditionName = newName();
final ILock lock = callerInstance.getLock(lockName);
final ICondition condition = lock.newCondition(conditionName);
final AtomicBoolean running = new AtomicBoolean(true);
final AtomicReference<Exception> errorRef = new AtomicReference<Exception>();
final int numberOfThreads = 8;
final CountDownLatch allFinished = new CountDownLatch(numberOfThreads);
ExecutorService ex = Executors.newCachedThreadPool();
for (int i = 0; i < numberOfThreads; i++) {
ex.execute(new Runnable() {
@Override
public void run() {
try {
while (running.get()) {
lock.lock();
try {
condition.await(1, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignored) {
} catch (IllegalStateException e) {
errorRef.set(e);
running.set(false);
} finally {
lock.unlock();
}
}
} finally {
allFinished.countDown();
}
}
});
}
ex.execute(new Runnable() {
@Override
public void run() {
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
running.set(false);
}
});
try {
allFinished.await(30, TimeUnit.SECONDS);
assertNull("await() on condition threw IllegalStateException!", errorRef.get());
} finally {
ex.shutdownNow();
}
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testSignalAll.
@Test(timeout = 60000)
public void testSignalAll() throws InterruptedException {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition(newName());
CountDownLatch allAwaited = new CountDownLatch(2);
CountDownLatch allSignalled = new CountDownLatch(2);
startThreadWaitingOnCondition(lock, condition, allAwaited, allSignalled);
startThreadWaitingOnCondition(lock, condition, allAwaited, allSignalled);
assertOpenEventually("All threads should have been reached await", allAwaited);
signalAll(lock, condition);
assertOpenEventually("All threads should have been signalled", allSignalled);
}
use of com.hazelcast.core.ICondition in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitUntil_whenDeadLineInThePast.
@Test(timeout = 60000)
public void testAwaitUntil_whenDeadLineInThePast() throws InterruptedException {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition(newName());
lock.lock();
assertFalse(condition.awaitUntil(currentTimeAfterGivenMillis(-1000)));
}
Aggregations