Search in sources :

Example 26 with ICondition

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

the class ConditionAdvancedTest method testLockConditionSignalAllShutDownKeyOwner.

@Test
public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final String name = randomString();
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
    final AtomicInteger count = new AtomicInteger(0);
    final int size = 50;
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance();
    warmUpPartitions(instance, keyOwner);
    final String key = generateKeyOwnedBy(keyOwner);
    final ILock lock = instance.getLock(key);
    final ICondition condition = lock.newCondition(name);
    final CountDownLatch awaitLatch = new CountDownLatch(size);
    final CountDownLatch finalLatch = new CountDownLatch(size);
    for (int i = 0; i < size; i++) {
        new Thread(new Runnable() {

            public void run() {
                lock.lock();
                try {
                    awaitLatch.countDown();
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                } catch (InterruptedException ignored) {
                } finally {
                    lock.unlock();
                    finalLatch.countDown();
                }
            }
        }).start();
    }
    ILock lock1 = keyOwner.getLock(key);
    ICondition condition1 = lock1.newCondition(name);
    awaitLatch.await(1, TimeUnit.MINUTES);
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    keyOwner.shutdown();
    finalLatch.await(2, TimeUnit.MINUTES);
    assertEquals(size, count.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 27 with ICondition

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

the class ConditionAdvancedTest method testKeyOwnerDiesOnCondition.

// ====================== tests to make sure the condition can deal with cluster member failure ====================
@Test(timeout = 100000)
public void testKeyOwnerDiesOnCondition() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
    final AtomicInteger signalCounter = new AtomicInteger(0);
    final String key = generateKeyOwnedBy(instance1);
    final ILock lock1 = instance1.getLock(key);
    final String conditionName = randomString();
    final ICondition condition1 = lock1.newCondition(conditionName);
    Thread t = new Thread(new Runnable() {

        public void run() {
            ILock lock = instance2.getLock(key);
            ICondition condition = lock.newCondition(conditionName);
            lock.lock();
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            signalCounter.incrementAndGet();
        }
    });
    t.start();
    Thread.sleep(1000);
    lock1.lock();
    keyOwner.shutdown();
    condition1.signal();
    lock1.unlock();
    Thread.sleep(1000);
    t.join();
    assertEquals(1, signalCounter.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ILock(com.hazelcast.core.ILock) 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 28 with ICondition

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

the class ConditionAbstractTest method testAwait_whenNegativeTimeout.

// https://github.com/hazelcast/hazelcast/issues/3262
@Test(timeout = 60000)
@Ignore
public void testAwait_whenNegativeTimeout() 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 29 with ICondition

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

the class ConditionAbstractTest method testAwaitExpiration_whenLockIsAcquiredByAnotherThread.

@Test
public void testAwaitExpiration_whenLockIsAcquiredByAnotherThread() 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();
    }
    awaitLatch.await(2, TimeUnit.MINUTES);
    lock.lock();
    sleepSeconds(2);
    lock.unlock();
    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 30 with ICondition

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

the class ConditionAbstractTest method testAwaitNanos_remainingTime.

@Test
public void testAwaitNanos_remainingTime() throws InterruptedException {
    String name = newName();
    ILock lock = callerInstance.getLock(name);
    ICondition condition = lock.newCondition(name);
    lock.lock();
    long remainingTimeout = condition.awaitNanos(1000L);
    assertTrue("Remaining timeout should be <= 0, but it's = " + remainingTimeout, remainingTimeout <= 0);
}
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