use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LegacyClientConditionTest method testLockConditionSimpleUsage.
@Test
public void testLockConditionSimpleUsage() throws InterruptedException {
final String name = randomString();
final ILock lock = client.getLock(name);
final ICondition condition = lock.newCondition(randomString());
final AtomicInteger count = new AtomicInteger(0);
final CountDownLatch threadGetTheLock = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
lock.lock();
threadGetTheLock.countDown();
try {
if (lock.isLockedByCurrentThread()) {
count.incrementAndGet();
}
condition.await();
if (lock.isLockedByCurrentThread()) {
count.incrementAndGet();
}
} catch (InterruptedException ignored) {
} finally {
lock.unlock();
}
}
});
t.start();
assertOpenEventually(threadGetTheLock);
lock.lock();
assertEquals(true, lock.isLocked());
condition.signal();
lock.unlock();
t.join();
assertEquals(2, count.get());
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ClientLockTest method testObtainLock_FromDifferentClients.
@Test
public void testObtainLock_FromDifferentClients() throws InterruptedException {
factory.newHazelcastInstance();
String name = randomName();
HazelcastInstance clientA = factory.newHazelcastClient();
ILock lockA = clientA.getLock(name);
lockA.lock();
HazelcastInstance clientB = factory.newHazelcastClient();
ILock lockB = clientB.getLock(name);
boolean lockObtained = lockB.tryLock();
assertFalse("Lock obtained by 2 client ", lockObtained);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ClientLockTest method testTryLockLeaseTime_whenLockFree.
@Test(timeout = 60000)
public void testTryLockLeaseTime_whenLockFree() throws InterruptedException {
factory.newHazelcastInstance();
HazelcastInstance hz = factory.newHazelcastClient();
final ILock lock = hz.getLock(randomName());
boolean isLocked = lock.tryLock(1000, TimeUnit.MILLISECONDS, 1000, TimeUnit.MILLISECONDS);
assertTrue(isLocked);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ClientLockTest method testTryLock.
@Test
public void testTryLock() throws Exception {
factory.newHazelcastInstance();
HazelcastInstance hz = factory.newHazelcastClient();
final ILock lock = hz.getLock(randomName());
assertTrue(lock.tryLock(2, TimeUnit.SECONDS));
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
public void run() {
try {
if (!lock.tryLock(2, TimeUnit.SECONDS)) {
latch.countDown();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
assertTrue(latch.await(100, TimeUnit.SECONDS));
assertTrue(lock.isLocked());
final CountDownLatch latch2 = new CountDownLatch(1);
new Thread() {
public void run() {
try {
if (lock.tryLock(20, TimeUnit.SECONDS)) {
latch2.countDown();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Thread.sleep(1000);
lock.unlock();
assertTrue(latch2.await(100, TimeUnit.SECONDS));
assertTrue(lock.isLocked());
lock.forceUnlock();
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ClientLockTest method testLockWithZeroTTL.
@Test(expected = IllegalArgumentException.class)
public void testLockWithZeroTTL() {
factory.newHazelcastInstance();
HazelcastInstance hz = factory.newHazelcastClient();
final ILock lock = hz.getLock(randomName());
final long ttl = 0;
lock.lock(ttl, TimeUnit.MILLISECONDS);
}
Aggregations