Search in sources :

Example 66 with TestHazelcastInstanceFactory

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 67 with TestHazelcastInstanceFactory

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) ILock(com.hazelcast.core.ILock) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) IOException(java.io.IOException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 68 with TestHazelcastInstanceFactory

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 69 with TestHazelcastInstanceFactory

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();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 70 with TestHazelcastInstanceFactory

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();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)948 HazelcastInstance (com.hazelcast.core.HazelcastInstance)827 Test (org.junit.Test)774 QuickTest (com.hazelcast.test.annotation.QuickTest)726 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)575 Config (com.hazelcast.config.Config)488 MapConfig (com.hazelcast.config.MapConfig)185 ParallelTest (com.hazelcast.test.annotation.ParallelTest)145 MapStoreConfig (com.hazelcast.config.MapStoreConfig)121 CountDownLatch (java.util.concurrent.CountDownLatch)108 AssertTask (com.hazelcast.test.AssertTask)97 NightlyTest (com.hazelcast.test.annotation.NightlyTest)86 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)81 Before (org.junit.Before)75 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 NearCacheConfig (com.hazelcast.config.NearCacheConfig)53 SlowTest (com.hazelcast.test.annotation.SlowTest)53 IndexConfig (com.hazelcast.config.IndexConfig)50 ArrayList (java.util.ArrayList)45 Map (java.util.Map)43