Search in sources :

Example 66 with ILock

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

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

Example 68 with ILock

use of com.hazelcast.core.ILock in project hazelcast by hazelcast.

the class ConditionAbstractTest method testSignalAll_whenMultipleConditions.

@Test(timeout = 60000)
public void testSignalAll_whenMultipleConditions() throws InterruptedException {
    ILock lock = callerInstance.getLock(newName());
    ICondition condition0 = lock.newCondition(newName());
    ICondition condition1 = lock.newCondition(newName());
    CountDownLatch allAwaited = new CountDownLatch(2);
    CountDownLatch allSignalled = new CountDownLatch(10);
    startThreadWaitingOnCondition(lock, condition0, allAwaited, allSignalled);
    startThreadWaitingOnCondition(lock, condition1, allAwaited, allSignalled);
    assertOpenEventually("All threads should have been reached await", allAwaited);
    signalAll(lock, condition0);
    assertCountEventually("Condition has not been signalled", 9, allSignalled, THIRTY_SECONDS);
}
Also used : ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) Test(org.junit.Test)

Example 69 with ILock

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

Example 70 with ILock

use of com.hazelcast.core.ILock in project hazelcast by hazelcast.

the class ClientConcurrentLockTest method concurrent_LockTest.

private void concurrent_LockTest(boolean tryLockWithTimeOut) throws InterruptedException {
    final ILock lock = client.getLock(randomString());
    final AtomicInteger upTotal = new AtomicInteger(0);
    final AtomicInteger downTotal = new AtomicInteger(0);
    LockTestThread[] threads = new LockTestThread[8];
    for (int i = 0; i < threads.length; i++) {
        LockTestThread t;
        if (tryLockWithTimeOut) {
            t = new TryLockWithTimeOutThread(lock, upTotal, downTotal);
        } else {
            t = new TryLockThread(lock, upTotal, downTotal);
        }
        t.start();
        threads[i] = t;
    }
    assertJoinable(threads);
    assertEquals("concurrent access to locked code caused wrong total", 0, upTotal.get() + downTotal.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ILock(com.hazelcast.core.ILock)

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