use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAdvancedTest method testLockConditionSignalAllShutDownKeyOwner.
@Test
public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
final String name = randomString();
final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
final AtomicInteger count = new AtomicInteger(0);
final int size = 50;
final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance();
warmUpPartitions(instance, keyOwner);
final String key = generateKeyOwnedBy(keyOwner);
final ILock lock = instance.getLock(key);
final ICondition condition = lock.newCondition(name);
final CountDownLatch awaitLatch = new CountDownLatch(size);
final CountDownLatch finalLatch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
new Thread(new Runnable() {
public void run() {
lock.lock();
try {
awaitLatch.countDown();
condition.await();
if (lock.isLockedByCurrentThread()) {
count.incrementAndGet();
}
} catch (InterruptedException ignored) {
} finally {
lock.unlock();
finalLatch.countDown();
}
}
}).start();
}
ILock lock1 = keyOwner.getLock(key);
ICondition condition1 = lock1.newCondition(name);
awaitLatch.await(1, TimeUnit.MINUTES);
lock1.lock();
condition1.signalAll();
lock1.unlock();
keyOwner.shutdown();
finalLatch.await(2, TimeUnit.MINUTES);
assertEquals(size, count.get());
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAdvancedTest method testKeyOwnerDiesOnCondition.
// ====================== tests to make sure the condition can deal with cluster member failure ====================
@Test(timeout = 100000)
public void testKeyOwnerDiesOnCondition() throws Exception {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
final AtomicInteger signalCounter = new AtomicInteger(0);
final String key = generateKeyOwnedBy(instance1);
final ILock lock1 = instance1.getLock(key);
final String conditionName = randomString();
final ICondition condition1 = lock1.newCondition(conditionName);
Thread t = new Thread(new Runnable() {
public void run() {
ILock lock = instance2.getLock(key);
ICondition condition = lock.newCondition(conditionName);
lock.lock();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
signalCounter.incrementAndGet();
}
});
t.start();
Thread.sleep(1000);
lock1.lock();
keyOwner.shutdown();
condition1.signal();
lock1.unlock();
Thread.sleep(1000);
t.join();
assertEquals(1, signalCounter.get());
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testLockOwnerDies.
// ====================== tests to make sure the lock can deal with cluster member failure ====================
@Test(timeout = 100000)
public void testLockOwnerDies() throws Exception {
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance lockOwner = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
final String name = randomString();
final ILock lock = lockOwner.getLock(name);
lock.lock();
assertTrue(lock.isLocked());
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
final ILock lock = instance1.getLock(name);
lock.lock();
latch.countDown();
}
});
t.start();
lockOwner.shutdown();
assertOpenEventually(latch, 10);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testLockOwnerDies_withMultipleLocks.
@Test(timeout = 100000)
public void testLockOwnerDies_withMultipleLocks() throws Exception {
int lockCount = 10;
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
HazelcastInstance lockOwner = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
final String[] names = generateKeysBelongingToSamePartitionsOwnedBy(instance1, lockCount);
final ILock[] locks = getLocks(lockOwner, names);
lockAll(locks);
assertAllLocked(locks);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
final ILock[] locks = getLocks(instance1, names);
lockAll(locks);
latch.countDown();
}
});
t.start();
lockOwner.shutdown();
assertOpenEventually(latch, 10);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testLockEviction.
private void testLockEviction(boolean localKey) throws Exception {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
warmUpPartitions(instance2, instance1);
final String key;
if (localKey) {
key = generateKeyOwnedBy(instance1);
} else {
key = generateKeyNotOwnedBy(instance1);
}
final ILock lock = instance1.getLock(key);
lock.lock(10, TimeUnit.SECONDS);
assertTrue(lock.getRemainingLeaseTime() > 0);
assertTrue(lock.isLocked());
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
final ILock lock = instance2.getLock(key);
lock.lock();
latch.countDown();
}
});
t.start();
assertOpenEventually(latch, 30);
}
Aggregations