Search in sources :

Example 71 with ILock

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

the class ClientLockTest method testMaxLockLeaseTime.

@Test
public void testMaxLockLeaseTime() {
    Config config = new Config();
    config.setProperty(GroupProperty.LOCK_MAX_LEASE_TIME_SECONDS.getName(), "1");
    factory.newHazelcastInstance(config);
    HazelcastInstance hz = factory.newHazelcastClient();
    final ILock lock = hz.getLock(randomName());
    lock.lock();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertFalse("Lock should be released after lease expires!", lock.isLocked());
        }
    }, 30);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) AssertTask(com.hazelcast.test.AssertTask) ILock(com.hazelcast.core.ILock) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 72 with ILock

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

the class ClientLockTest method testTryLockLeaseTime_lockIsReleasedEventually.

@Test
public void testTryLockLeaseTime_lockIsReleasedEventually() throws InterruptedException {
    factory.newHazelcastInstance();
    HazelcastInstance hz = factory.newHazelcastClient();
    final ILock lock = hz.getLock(randomName());
    lock.tryLock(1000, TimeUnit.MILLISECONDS, 1000, TimeUnit.MILLISECONDS);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertFalse(lock.isLocked());
        }
    }, 30);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) ILock(com.hazelcast.core.ILock) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 73 with ILock

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

the class ClientLockTest method testTryLockLeaseTime_whenLockAcquiredByOther.

@Test(timeout = 60000)
public void testTryLockLeaseTime_whenLockAcquiredByOther() throws InterruptedException {
    factory.newHazelcastInstance();
    HazelcastInstance hz = factory.newHazelcastClient();
    final ILock lock = hz.getLock(randomName());
    Thread thread = new Thread() {

        public void run() {
            lock.lock();
        }
    };
    thread.start();
    thread.join();
    boolean isLocked = lock.tryLock(1000, TimeUnit.MILLISECONDS, 1000, TimeUnit.MILLISECONDS);
    assertFalse(isLocked);
}
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 74 with ILock

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

the class ClientLockTest method testStats.

@Test
public void testStats() throws InterruptedException {
    factory.newHazelcastInstance();
    HazelcastInstance hz = factory.newHazelcastClient();
    final ILock lock = hz.getLock(randomName());
    lock.lock();
    assertTrue(lock.isLocked());
    assertTrue(lock.isLockedByCurrentThread());
    assertEquals(1, lock.getLockCount());
    lock.unlock();
    assertFalse(lock.isLocked());
    assertEquals(0, lock.getLockCount());
    assertEquals(-1L, lock.getRemainingLeaseTime());
    lock.lock(1, TimeUnit.MINUTES);
    assertTrue(lock.isLocked());
    assertTrue(lock.isLockedByCurrentThread());
    assertEquals(1, lock.getLockCount());
    assertTrue(lock.getRemainingLeaseTime() > 1000 * 30);
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {

        public void run() {
            assertTrue(lock.isLocked());
            assertFalse(lock.isLockedByCurrentThread());
            assertEquals(1, lock.getLockCount());
            assertTrue(lock.getRemainingLeaseTime() > 1000 * 30);
            latch.countDown();
        }
    }.start();
    assertTrue(latch.await(1, TimeUnit.MINUTES));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 75 with ILock

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

the class ClientLockTest method testLockFail_whenGreaterThanMaxLeaseTimeUsed.

@Test(expected = IllegalArgumentException.class)
public void testLockFail_whenGreaterThanMaxLeaseTimeUsed() {
    Config config = new Config();
    config.setProperty(GroupProperty.LOCK_MAX_LEASE_TIME_SECONDS.getName(), "1");
    factory.newHazelcastInstance(config);
    HazelcastInstance hz = factory.newHazelcastClient();
    ILock lock = hz.getLock(randomName());
    lock.lock(10, TimeUnit.SECONDS);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ILock(com.hazelcast.core.ILock) 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