Search in sources :

Example 31 with ICondition

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) TestThread(com.hazelcast.test.TestThread) Test(org.junit.Test)

Example 32 with ICondition

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();
}
Also used : ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 33 with ICondition

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();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) ILock(com.hazelcast.core.ILock) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 34 with ICondition

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);
}
Also used : ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 35 with ICondition

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