use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockSplitBrainTest method testLockSplitBrain_acquireSameLock.
@Test
public void testLockSplitBrain_acquireSameLock() throws InterruptedException {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
Config config = newConfig();
HazelcastInstance h1 = factory.newHazelcastInstance(config);
HazelcastInstance h2 = factory.newHazelcastInstance(config);
HazelcastInstance h3 = factory.newHazelcastInstance(config);
warmUpPartitions(h1, h2, h3);
String key = generateKeyOwnedBy(h3);
ILock lock1 = h1.getLock(key);
ILock lock3 = h3.getLock(key);
lock3.lock();
waitAllForSafeState(h1, h2, h3);
// create split: [h1, h2] & [h3]
closeConnectionBetween(h1, h3);
closeConnectionBetween(h2, h3);
assertClusterSizeEventually(2, h1);
assertClusterSizeEventually(2, h2);
assertClusterSizeEventually(1, h3);
// acquire lock on [h1, h2] side
lock1.lock();
// release lock on h3 side
lock3.forceUnlock();
// merge back
getNode(h3).getClusterService().merge(getAddress(h1));
assertClusterSizeEventually(3, h1);
assertClusterSizeEventually(3, h2);
assertClusterSizeEventually(3, h3);
// h3 observes lock as acquired
assertTrue(lock3.isLocked());
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testLockCleanup_whenInvokingMemberDies.
@Test
public void testLockCleanup_whenInvokingMemberDies() {
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance hz = factory.newHazelcastInstance();
HazelcastInstance hz2 = factory.newHazelcastInstance();
NodeEngineImpl nodeEngine = getNodeEngineImpl(hz2);
InternalOperationService operationService = getOperationService(hz2);
warmUpPartitions(hz2);
String name = randomNameOwnedBy(hz);
Data key = nodeEngine.toData(name);
int partitionId = nodeEngine.getPartitionService().getPartitionId(key);
operationService.invokeOnPartition(LockService.SERVICE_NAME, new SlowLockOperation(name, key, 2000), partitionId);
sleepMillis(500);
terminateInstance(hz2);
final ILock lock = hz.getLock(name);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertFalse("Lock owned by dead member should have been released!", lock.isLocked());
}
}, 30);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class LockAdvancedTest method testMaxLockLeaseTime.
@Test
public void testMaxLockLeaseTime() {
Config config = new Config();
config.setProperty(GroupProperty.LOCK_MAX_LEASE_TIME_SECONDS.getName(), "1");
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
HazelcastInstance hz = factory.newHazelcastInstance(config);
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 LockAdvancedTest method testKeyOwnerDies.
@Test(timeout = 100000)
public void testKeyOwnerDies() throws Exception {
final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
warmUpPartitions(keyOwner, instance1, instance2);
final String key = generateKeyOwnedBy(keyOwner);
final ILock lock1 = instance1.getLock(key);
lock1.lock();
final CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {
public void run() {
final ILock lock = instance2.getLock(key);
lock.lock();
latch.countDown();
}
}).start();
Thread.sleep(1000);
keyOwner.shutdown();
assertTrue(lock1.isLocked());
assertTrue(lock1.isLockedByCurrentThread());
assertTrue(lock1.tryLock());
lock1.unlock();
lock1.unlock();
assertOpenEventually(latch, 10);
}
use of com.hazelcast.core.ILock in project hazelcast by hazelcast.
the class ConditionAbstractTest method testAwaitTime_whenTimeout.
@Test
public void testAwaitTime_whenTimeout() throws InterruptedException {
ILock lock = callerInstance.getLock(newName());
ICondition condition = lock.newCondition(newName());
lock.lock();
boolean success = condition.await(1, TimeUnit.MILLISECONDS);
assertFalse(success);
assertTrue(lock.isLockedByCurrentThread());
}
Aggregations