Search in sources :

Example 11 with ICondition

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

the class ConditionAdvancedTest method testDestroyLock_whenOtherWaitingOnConditionAwait.

@Test(timeout = 60000, expected = DistributedObjectDestroyedException.class)
public void testDestroyLock_whenOtherWaitingOnConditionAwait() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
    final ILock lock = instance.getLock(randomString());
    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();
    latch.countDown();
    condition.await();
    lock.unlock();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 12 with ICondition

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

the class ConditionAdvancedTest method testInterruptionDuringWaiting.

@Test(timeout = 60000)
public void testInterruptionDuringWaiting() throws InterruptedException {
    Config config = new Config();
    // the system should wait at most 5000 ms in order to determine the operation status
    config.setProperty(GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "5000");
    HazelcastInstance instance = createHazelcastInstance(config);
    final ILock lock = instance.getLock(randomString());
    final ICondition condition0 = lock.newCondition(randomString());
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                lock.lock();
                condition0.await();
            } catch (InterruptedException e) {
                latch.countDown();
            }
        }
    });
    thread.start();
    sleepSeconds(2);
    thread.interrupt();
    assertOpenEventually(latch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) 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 13 with ICondition

use of com.hazelcast.core.ICondition 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 14 with ICondition

use of com.hazelcast.core.ICondition 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 15 with ICondition

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

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