Search in sources :

Example 16 with ILock

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

Example 17 with ILock

use of com.hazelcast.core.ILock 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);
}
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 18 with ILock

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

Example 19 with ILock

use of com.hazelcast.core.ILock 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 20 with ILock

use of com.hazelcast.core.ILock 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)

Aggregations

ILock (com.hazelcast.core.ILock)77 Test (org.junit.Test)71 ParallelTest (com.hazelcast.test.annotation.ParallelTest)44 QuickTest (com.hazelcast.test.annotation.QuickTest)44 HazelcastInstance (com.hazelcast.core.HazelcastInstance)43 CountDownLatch (java.util.concurrent.CountDownLatch)37 ICondition (com.hazelcast.core.ICondition)33 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)20 Config (com.hazelcast.config.Config)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AssertTask (com.hazelcast.test.AssertTask)7 TestThread (com.hazelcast.test.TestThread)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 IOException (java.io.IOException)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)2 Data (com.hazelcast.nio.serialization.Data)2 TimeoutException (java.util.concurrent.TimeoutException)2 InternalLockNamespace (com.hazelcast.concurrent.lock.InternalLockNamespace)1