Search in sources :

Example 1 with ICondition

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

the class LegacyClientConditionTest method testDestroyLockWhenOtherWaitingOnConditionAwait.

@Test(expected = DistributedObjectDestroyedException.class)
public void testDestroyLockWhenOtherWaitingOnConditionAwait() {
    final ILock lock = client.getLock("testDestroyLockWhenOtherWaitingOnConditionAwait");
    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();
    try {
        latch.countDown();
        condition.await();
    } catch (InterruptedException e) {
    }
    lock.unlock();
}
Also used : 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 2 with ICondition

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

the class LegacyClientConditionTest method testLockConditionSimpleUsage.

@Test
public void testLockConditionSimpleUsage() throws InterruptedException {
    final String name = randomString();
    final ILock lock = client.getLock(name);
    final ICondition condition = lock.newCondition(randomString());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch threadGetTheLock = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {

        public void run() {
            lock.lock();
            threadGetTheLock.countDown();
            try {
                if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                }
                condition.await();
                if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                }
            } catch (InterruptedException ignored) {
            } finally {
                lock.unlock();
            }
        }
    });
    t.start();
    assertOpenEventually(threadGetTheLock);
    lock.lock();
    assertEquals(true, lock.isLocked());
    condition.signal();
    lock.unlock();
    t.join();
    assertEquals(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 3 with ICondition

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

the class LegacyClientConditionTest method testIllegalConditionUsageSignalToNonAwaiter.

@Test(expected = IllegalMonitorStateException.class)
public void testIllegalConditionUsageSignalToNonAwaiter() {
    final ICondition condition = lock.newCondition("condition");
    condition.signal();
}
Also used : ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with ICondition

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

the class LegacyClientConditionTest method testConditionUsage.

@Test
public void testConditionUsage() throws InterruptedException {
    lock.lock();
    final ICondition condition = lock.newCondition("condition");
    condition.await(1, TimeUnit.SECONDS);
    lock.unlock();
}
Also used : ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ICondition

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

the class LegacyClientConditionTest method testAwaitNanos_remainingTime.

@Test
public void testAwaitNanos_remainingTime() throws InterruptedException {
    ICondition condition = lock.newCondition("condition");
    lock.lock();
    try {
        long timeout = 1000L;
        long remainingTimeout = condition.awaitNanos(timeout);
        assertTrue("Remaining timeout should be <= 0, but it's = " + remainingTimeout, remainingTimeout <= 0);
    } finally {
        lock.unlock();
    }
}
Also used : ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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