use of com.hazelcast.test.TestHazelcastInstanceFactory 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.test.TestHazelcastInstanceFactory 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.test.TestHazelcastInstanceFactory 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);
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class HazelcastInstanceFactoryTest method testTestHazelcastInstanceFactory_withTwoFactories.
@Test
public void testTestHazelcastInstanceFactory_withTwoFactories() {
TestHazelcastInstanceFactory instanceFactory1 = new TestHazelcastInstanceFactory();
TestHazelcastInstanceFactory instanceFactory2 = new TestHazelcastInstanceFactory();
try {
final HazelcastInstance instance11 = instanceFactory1.newHazelcastInstance();
final HazelcastInstance instance12 = instanceFactory1.newHazelcastInstance();
final HazelcastInstance instance13 = instanceFactory1.newHazelcastInstance();
final HazelcastInstance instance21 = instanceFactory2.newHazelcastInstance();
final HazelcastInstance instance22 = instanceFactory2.newHazelcastInstance();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(2, instance21.getCluster().getMembers().size());
assertEquals(2, instance22.getCluster().getMembers().size());
}
});
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(3, instance11.getCluster().getMembers().size());
assertEquals(3, instance12.getCluster().getMembers().size());
assertEquals(3, instance13.getCluster().getMembers().size());
}
});
} finally {
instanceFactory1.terminateAll();
instanceFactory2.terminateAll();
}
}
use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.
the class HazelcastInstanceFactoryTest method testTestHazelcastInstanceFactory.
@Test
public void testTestHazelcastInstanceFactory() {
TestHazelcastInstanceFactory instanceFactory = new TestHazelcastInstanceFactory();
try {
final HazelcastInstance instance1 = instanceFactory.newHazelcastInstance();
final HazelcastInstance instance2 = instanceFactory.newHazelcastInstance();
final HazelcastInstance instance3 = instanceFactory.newHazelcastInstance();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(3, instance1.getCluster().getMembers().size());
assertEquals(3, instance2.getCluster().getMembers().size());
assertEquals(3, instance3.getCluster().getMembers().size());
}
});
} finally {
instanceFactory.terminateAll();
}
}
Aggregations