use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ClientConcurrentLockTest method concurrent_LockTest.
private void concurrent_LockTest(boolean tryLockWithTimeOut) throws InterruptedException {
final ILock lock = client.getLock(randomString());
final AtomicInteger upTotal = new AtomicInteger(0);
final AtomicInteger downTotal = new AtomicInteger(0);
LockTestThread[] threads = new LockTestThread[8];
for (int i = 0; i < threads.length; i++) {
LockTestThread t;
if (tryLockWithTimeOut) {
t = new TryLockWithTimeOutThread(lock, upTotal, downTotal);
} else {
t = new TryLockThread(lock, upTotal, downTotal);
}
t.start();
threads[i] = t;
}
assertJoinable(threads);
assertEquals("concurrent access to locked code caused wrong total", 0, upTotal.get() + downTotal.get());
}
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);
}
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);
}
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);
}
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));
}
Aggregations