Search in sources :

Example 51 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class CountDownLatchMigrationTest method testLatchMigration.

@Test
public void testLatchMigration() throws InterruptedException {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(5);
    HazelcastInstance hz1 = factory.newHazelcastInstance();
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    warmUpPartitions(hz2, hz1);
    ICountDownLatch latch1 = hz1.getCountDownLatch("test");
    latch1.trySetCount(10);
    Thread.sleep(500);
    ICountDownLatch latch2 = hz2.getCountDownLatch("test");
    assertEquals(10, latch2.getCount());
    latch2.countDown();
    assertEquals(9, latch1.getCount());
    hz1.shutdown();
    assertEquals(9, latch2.getCount());
    HazelcastInstance hz3 = factory.newHazelcastInstance();
    warmUpPartitions(hz3);
    ICountDownLatch latch3 = hz3.getCountDownLatch("test");
    latch3.countDown();
    assertEquals(8, latch3.getCount());
    hz2.shutdown();
    latch3.countDown();
    assertEquals(7, latch3.getCount());
    HazelcastInstance hz4 = factory.newHazelcastInstance();
    HazelcastInstance hz5 = factory.newHazelcastInstance();
    warmUpPartitions(hz5, hz4);
    Thread.sleep(250);
    hz3.shutdown();
    ICountDownLatch latch4 = hz4.getCountDownLatch("test");
    assertEquals(7, latch4.getCount());
    ICountDownLatch latch5 = hz5.getCountDownLatch("test");
    latch5.countDown();
    assertEquals(6, latch5.getCount());
    latch5.countDown();
    assertEquals(5, latch4.getCount());
    assertEquals(5, latch5.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 52 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class CountDownLatchSplitBrainTest method testCountDownLatchSplitBrain.

@Test
public void testCountDownLatchSplitBrain() 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 name = generateKeyOwnedBy(h3);
    ICountDownLatch countDownLatch1 = h1.getCountDownLatch(name);
    ICountDownLatch countDownLatch3 = h3.getCountDownLatch(name);
    countDownLatch3.trySetCount(5);
    waitAllForSafeState(h1, h2, h3);
    // create split: [h1, h2] & [h3]
    closeConnectionBetween(h1, h3);
    closeConnectionBetween(h2, h3);
    assertClusterSizeEventually(2, h1);
    assertClusterSizeEventually(2, h2);
    assertClusterSizeEventually(1, h3);
    // modify both latches after split with different counts
    // count of h1 & h2 = 4
    countDownLatch1.countDown();
    // count of h3 = 0
    while (countDownLatch3.getCount() > 0) {
        countDownLatch3.countDown();
    }
    // merge back
    getNode(h3).getClusterService().merge(getAddress(h1));
    assertClusterSizeEventually(3, h1);
    assertClusterSizeEventually(3, h2);
    assertClusterSizeEventually(3, h3);
    // latch count should be equal to the count of larger cluster
    assertEquals(4, countDownLatch3.getCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ICountDownLatch(com.hazelcast.core.ICountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 53 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class IdGeneratorStressTest method setup.

@Before
public void setup() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(clusterSize);
    instances = factory.newInstances();
    name = randomString();
}
Also used : TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Before(org.junit.Before)

Example 54 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class SemaphoreAdvancedTest method testSemaphoreWithFailuresAndJoin.

@Test(timeout = 300000)
public void testSemaphoreWithFailuresAndJoin() {
    final String semaphoreName = randomString();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    final HazelcastInstance instance1 = factory.newHazelcastInstance();
    final HazelcastInstance instance2 = factory.newHazelcastInstance();
    final ISemaphore semaphore = instance1.getSemaphore(semaphoreName);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    assertTrue(semaphore.init(0));
    final Thread thread = new Thread() {

        public void run() {
            for (int i = 0; i < 2; i++) {
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            countDownLatch.countDown();
        }
    };
    thread.start();
    instance2.shutdown();
    semaphore.release();
    HazelcastInstance instance3 = factory.newHazelcastInstance();
    ISemaphore semaphore1 = instance3.getSemaphore(semaphoreName);
    semaphore1.release();
    try {
        assertTrue(countDownLatch.await(15, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        thread.interrupt();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ISemaphore(com.hazelcast.core.ISemaphore) 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 55 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class AtomicLongAdvancedTest method testAtomicLongSpawnNodeInParallel.

@Test
public void testAtomicLongSpawnNodeInParallel() throws InterruptedException {
    int total = 6;
    int parallel = 2;
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(total + 1);
    HazelcastInstance instance = nodeFactory.newHazelcastInstance();
    final String name = "testAtomicLongSpawnNodeInParallel";
    IAtomicLong atomicLong = instance.getAtomicLong(name);
    atomicLong.set(100);
    final ExecutorService ex = Executors.newFixedThreadPool(parallel);
    try {
        for (int i = 0; i < total / parallel; i++) {
            final HazelcastInstance[] instances = new HazelcastInstance[parallel];
            final CountDownLatch countDownLatch = new CountDownLatch(parallel);
            final AtomicInteger exceptionCount = new AtomicInteger(0);
            for (int j = 0; j < parallel; j++) {
                final int id = j;
                ex.execute(new Runnable() {

                    public void run() {
                        try {
                            instances[id] = nodeFactory.newHazelcastInstance();
                            instances[id].getAtomicLong(name).incrementAndGet();
                        } catch (Exception e) {
                            exceptionCount.incrementAndGet();
                            e.printStackTrace();
                        } finally {
                            countDownLatch.countDown();
                        }
                    }
                });
            }
            assertOpenEventually(countDownLatch);
            waitAllForSafeState(nodeFactory.getAllHazelcastInstances());
            // if there is an exception while incrementing in parallel threads, find number of exceptions
            // and subtract the number from expectedValue.
            final int thrownExceptionCount = exceptionCount.get();
            final long expectedValue = (long) 100 + (i + 1) * parallel - thrownExceptionCount;
            IAtomicLong newAtomicLong = instance.getAtomicLong(name);
            assertEquals(expectedValue, newAtomicLong.get());
            instance.shutdown();
            instance = instances[0];
        }
    } finally {
        ex.shutdownNow();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) IAtomicLong(com.hazelcast.core.IAtomicLong) 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)

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