Search in sources :

Example 36 with ILock

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

the class ConditionAbstractTest method testMultipleConditionsForSameLock.

@Test(timeout = 60000)
public void testMultipleConditionsForSameLock() 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(2);
    startThreadWaitingOnCondition(lock, condition0, allAwaited, allSignalled);
    startThreadWaitingOnCondition(lock, condition1, allAwaited, allSignalled);
    assertOpenEventually("All threads should have been reached await", allAwaited);
    signal(lock, condition0);
    signal(lock, condition1);
    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 37 with ILock

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

the class ConditionAbstractTest method testSignalAllWithSingleWaiter.

@Test(timeout = 60000)
public void testSignalAllWithSingleWaiter() throws InterruptedException {
    String lockName = newName();
    String conditionName = newName();
    final ILock lock = callerInstance.getLock(lockName);
    final ICondition condition = lock.newCondition(conditionName);
    final AtomicInteger count = new AtomicInteger(0);
    final int k = 50;
    final CountDownLatch allAwaited = new CountDownLatch(k);
    final CountDownLatch allFinished = new CountDownLatch(k);
    for (int i = 0; i < k; i++) {
        new Thread(new Runnable() {

            public void run() {
                try {
                    lock.lock();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                    allAwaited.countDown();
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                        count.incrementAndGet();
                    }
                } catch (InterruptedException ignored) {
                } finally {
                    lock.unlock();
                    allFinished.countDown();
                }
            }
        }).start();
    }
    allAwaited.await(1, TimeUnit.MINUTES);
    assertUnlockedEventually(lock, THIRTY_SECONDS);
    // Make sure that all threads are waiting on condition await call
    Thread.sleep(3000);
    signalAll(lock, condition);
    allFinished.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) TestThread(com.hazelcast.test.TestThread) Test(org.junit.Test)

Example 38 with ILock

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

the class HazelcastOSGiInstanceTest method getLockCalledSuccessfullyOverOSGiInstance.

@Test
public void getLockCalledSuccessfullyOverOSGiInstance() {
    ILock mockLock = mock(ILock.class);
    HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
    HazelcastOSGiInstance hazelcastOSGiInstance = HazelcastOSGiTestUtil.createHazelcastOSGiInstance(mockHazelcastInstance);
    when(mockHazelcastInstance.getLock("my-lock")).thenReturn(mockLock);
    assertEquals(mockLock, hazelcastOSGiInstance.getLock("my-lock"));
    verify(mockHazelcastInstance).getLock("my-lock");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 39 with ILock

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

the class LockQuorumListenerTest method testQuorumFailureEventFiredWhenNodeCountBelowThreshold.

@Test
public void testQuorumFailureEventFiredWhenNodeCountBelowThreshold() {
    CountDownLatch quorumNotPresent = new CountDownLatch(1);
    String lockName = randomString();
    Config config = addQuorum(new Config(), lockName, quorumListener(null, quorumNotPresent));
    HazelcastInstance instance = createHazelcastInstance(config);
    ILock q = instance.getLock(lockName);
    try {
        q.lock();
    } catch (Exception expected) {
        expected.printStackTrace();
    }
    assertOpenEventually(quorumNotPresent, 15);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) ILock(com.hazelcast.core.ILock) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest) BaseQuorumListenerTest(com.hazelcast.quorum.BaseQuorumListenerTest)

Example 40 with ILock

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

the class PartitionControlledIdTest method testLock.

@Test
public void testLock() throws Exception {
    String partitionKey = "hazelcast";
    HazelcastInstance hz = getHazelcastInstance(partitionKey);
    ILock lock = hz.getLock("lock@" + partitionKey);
    lock.lock();
    assertEquals("lock@" + partitionKey, lock.getName());
    assertEquals(partitionKey, lock.getPartitionKey());
    Node node = getNode(hz);
    LockServiceImpl lockService = node.nodeEngine.getService(LockServiceImpl.SERVICE_NAME);
    Partition partition = instances[0].getPartitionService().getPartition(partitionKey);
    LockStore lockStore = lockService.getLockStore(partition.getPartitionId(), new InternalLockNamespace(lock.getName()));
    Data key = node.getSerializationService().toData(lock.getName(), StringPartitioningStrategy.INSTANCE);
    assertTrue(lockStore.isLocked(key));
}
Also used : Partition(com.hazelcast.core.Partition) HazelcastInstance(com.hazelcast.core.HazelcastInstance) InternalLockNamespace(com.hazelcast.concurrent.lock.InternalLockNamespace) Node(com.hazelcast.instance.Node) LockServiceImpl(com.hazelcast.concurrent.lock.LockServiceImpl) Data(com.hazelcast.nio.serialization.Data) ILock(com.hazelcast.core.ILock) LockStore(com.hazelcast.concurrent.lock.LockStore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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