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);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAbstractTest method testNewCondition_whenNullName.
@Test(timeout = 60000, expected = NullPointerException.class)
public void testNewCondition_whenNullName() {
ILock lock = callerInstance.getLock(newName());
lock.newCondition(null);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwait_whenNegativeTimeout.
// https://github.com/hazelcast/hazelcast/issues/3262
@Test(timeout = 60000)
@Ignore
public void testAwait_whenNegativeTimeout() throws InterruptedException {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition(newName());
lock.lock();
assertFalse(condition.await(-1, TimeUnit.MILLISECONDS));
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitExpiration_whenLockIsAcquiredByAnotherThread.
@Test
public void testAwaitExpiration_whenLockIsAcquiredByAnotherThread() throws InterruptedException {
String lockName = newName();
String conditionName = newName();
final ILock lock = callerInstance.getLock(lockName);
final ICondition condition = lock.newCondition(conditionName);
final AtomicInteger expires = new AtomicInteger(0);
final int awaitCount = 10;
final CountDownLatch awaitLatch = new CountDownLatch(awaitCount);
for (int i = 0; i < awaitCount; i++) {
new Thread(new Runnable() {
public void run() {
try {
lock.lock();
awaitLatch.countDown();
boolean signalled = condition.await(1, TimeUnit.SECONDS);
assertFalse(signalled);
expires.incrementAndGet();
} catch (InterruptedException ignored) {
} finally {
lock.unlock();
}
}
}).start();
}
awaitLatch.await(2, TimeUnit.MINUTES);
lock.lock();
sleepSeconds(2);
lock.unlock();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(awaitCount, expires.get());
}
});
}
Aggregations