Search in sources :

Example 56 with ILock

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

the class LockAdvancedTest method testLockOwnerDies_withMultipleLocks.

@Test(timeout = 100000)
public void testLockOwnerDies_withMultipleLocks() throws Exception {
    int lockCount = 10;
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance lockOwner = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
    final String[] names = generateKeysBelongingToSamePartitionsOwnedBy(instance1, lockCount);
    final ILock[] locks = getLocks(lockOwner, names);
    lockAll(locks);
    assertAllLocked(locks);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {

        public void run() {
            final ILock[] locks = getLocks(instance1, names);
            lockAll(locks);
            latch.countDown();
        }
    });
    t.start();
    lockOwner.shutdown();
    assertOpenEventually(latch, 10);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 57 with ILock

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

the class LockAdvancedTest method testLockEviction.

private void testLockEviction(boolean localKey) throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
    warmUpPartitions(instance2, instance1);
    final String key;
    if (localKey) {
        key = generateKeyOwnedBy(instance1);
    } else {
        key = generateKeyNotOwnedBy(instance1);
    }
    final ILock lock = instance1.getLock(key);
    lock.lock(10, TimeUnit.SECONDS);
    assertTrue(lock.getRemainingLeaseTime() > 0);
    assertTrue(lock.isLocked());
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {

        public void run() {
            final ILock lock = instance2.getLock(key);
            lock.lock();
            latch.countDown();
        }
    });
    t.start();
    assertOpenEventually(latch, 30);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory)

Example 58 with ILock

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

the class ConditionAbstractTest method testNewCondition_whenNullName.

@Test(timeout = 60000, expected = NullPointerException.class)
public void testNewCondition_whenNullName() {
    ILock lock = callerInstance.getLock(newName());
    lock.newCondition(null);
}
Also used : ILock(com.hazelcast.core.ILock) Test(org.junit.Test)

Example 59 with ILock

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

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

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