use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testScheduledLockActionForDeadMember.
@Test(timeout = 100000)
public void testScheduledLockActionForDeadMember() throws Exception {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
final ILock lock1 = h1.getLock("default");
final HazelcastInstance h2 = nodeFactory.newHazelcastInstance();
final ILock lock2 = h2.getLock("default");
assertTrue(lock1.tryLock());
final AtomicBoolean error = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
public void run() {
try {
lock2.lock();
error.set(true);
} catch (Throwable ignored) {
}
}
});
thread.start();
Thread.sleep(5000);
assertTrue(lock1.isLocked());
h2.shutdown();
thread.join(10000);
assertFalse(thread.isAlive());
assertFalse(error.get());
assertTrue(lock1.isLocked());
lock1.unlock();
assertFalse(lock1.isLocked());
assertTrue(lock1.tryLock());
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest 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");
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
HazelcastInstance hz = factory.newHazelcastInstance(config);
ILock lock = hz.getLock(randomName());
lock.lock(10, TimeUnit.SECONDS);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testIsLocked.
/**
* Test for issue #39
*/
@Test
public void testIsLocked() throws InterruptedException {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
final HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
final HazelcastInstance h2 = nodeFactory.newHazelcastInstance();
final HazelcastInstance h3 = nodeFactory.newHazelcastInstance();
final String key = "testLockIsLocked";
final ILock lock = h1.getLock(key);
final ILock lock2 = h2.getLock(key);
assertFalse(lock.isLocked());
assertFalse(lock2.isLocked());
lock.lock();
assertTrue(lock.isLocked());
assertTrue(lock2.isLocked());
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
Thread thread = new Thread(new Runnable() {
public void run() {
ILock lock3 = h3.getLock(key);
assertTrue(lock3.isLocked());
try {
latch2.countDown();
while (lock3.isLocked()) {
Thread.sleep(100);
}
latch.countDown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
latch2.await(3, TimeUnit.SECONDS);
Thread.sleep(500);
lock.unlock();
assertOpenEventually(latch, 5);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testLockLeaseTime_whenKeyOwnerMemberDies.
@Test
public void testLockLeaseTime_whenKeyOwnerMemberDies() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance hz1 = factory.newHazelcastInstance();
HazelcastInstance hz2 = factory.newHazelcastInstance();
warmUpPartitions(hz1, hz2);
String key = generateKeyOwnedBy(hz1);
final ILock lock = hz2.getLock(key);
lock.lock(3, TimeUnit.SECONDS);
terminateInstance(hz1);
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 LockAdvancedTest method testLockInterruptibly.
@Test
public void testLockInterruptibly() throws Exception {
Config config = new Config();
config.setProperty(GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "5000");
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
final HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
final ILock lock = h1.getLock(randomString());
final CountDownLatch latch = new CountDownLatch(1);
lock.lock();
Thread t = new Thread() {
public void run() {
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
latch.countDown();
}
}
};
t.start();
sleepMillis(5000);
t.interrupt();
assertOpenEventually(latch, 30);
}
Aggregations