Search in sources :

Example 21 with TestHazelcastInstanceFactory

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

the class QueueAdvancedTest method testDeadTaker.

/**
     * Test for issue 730 (Google).
     */
@Test
public void testDeadTaker() throws Exception {
    Config config = new Config();
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            shutdownLatch.countDown();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    }));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(config);
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h1, h2);
    final IQueue<String> q1 = h1.getQueue("default");
    final IQueue<String> q2 = h2.getQueue("default");
    final CountDownLatch startLatch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
                Thread.sleep(5000);
                h2.getLifecycleService().terminate();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.countDown();
                String value = q2.take();
                fail("Should not be able to take value from queue, but got: " + value);
            } catch (HazelcastInstanceNotActiveException e) {
                EmptyStatement.ignore(e);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
    q1.offer("item");
    assertEquals(1, q1.size());
    assertEquals("item", q1.poll());
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MembershipEvent(com.hazelcast.core.MembershipEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.hazelcast.test.TestThread) ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with TestHazelcastInstanceFactory

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

the class QueueAdvancedTest method testOffer.

@Test
public void testOffer() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances();
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    IQueue<String> q1 = h1.getQueue("default");
    IQueue<String> q2 = h2.getQueue("default");
    for (int i = 0; i < 100; i++) {
        assertTrue("Expected q1.offer() to succeed", q1.offer("item" + i, 100, SECONDS));
        assertTrue("Expected q2.offer() to succeed", q2.offer("item" + i, 100, SECONDS));
    }
    assertEquals("item0", q1.peek());
    assertEquals("item0", q2.peek());
    for (int i = 0; i < 100; i++) {
        assertEquals("item" + i, q1.poll());
        assertEquals("item" + i, q2.poll());
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 23 with TestHazelcastInstanceFactory

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

the class QueueListenerTest method testItemListener_addedToQueueConfig_Issue366.

@Test
public void testItemListener_addedToQueueConfig_Issue366() throws Exception {
    itemListenerConfig.setImplementation(countdownItemListener);
    itemListenerConfig.setIncludeValue(true);
    queueConfig.setName(QUEUE_NAME);
    queueConfig.addItemListenerConfig(itemListenerConfig);
    config.addQueueConfig(queueConfig);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance = factory.newHazelcastInstance(config);
    IQueue<Integer> queue = instance.getQueue(QUEUE_NAME);
    for (int i = 0; i < TOTAL_QUEUE_PUT / 2; i++) {
        queue.put(i);
    }
    factory.newHazelcastInstance(config);
    for (int i = 0; i < TOTAL_QUEUE_PUT / 4; i++) {
        queue.put(i);
    }
    assertTrue(countdownItemListener.added.await(3, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with TestHazelcastInstanceFactory

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

the class ListListenerTest method testListenerRemove.

@Test
public void testListenerRemove() throws Exception {
    final String name = randomString();
    final int count = 10;
    final int insCount = 4;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    final HazelcastInstance[] instances = factory.newInstances();
    ListenerTest listener = new ListenerTest(count);
    IList list = getList(instances, name);
    list.addItemListener(listener, true);
    for (int i = 0; i < count; i++) {
        list.add("item" + i);
    }
    for (int i = count - 1; i >= 0; i--) {
        list.remove(i);
    }
    assertTrue(listener.latchAdd.await(5, TimeUnit.SECONDS));
    assertTrue(listener.latchRemove.await(5, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) IList(com.hazelcast.core.IList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 25 with TestHazelcastInstanceFactory

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

the class QueueAdvancedTest method testQueueAfterShutdown.

/**
     * Test case for issue 289.
     * <p/>
     * 1. Create HazelcastInstance h1 and h2, then get a queue from each (same queue name)
     * 2. Put a message on queue from h2
     * 3. Take a message off on queue from h1
     * 4. Shutdown h1, then check if queue is still empty on h2
     */
@Test
public void testQueueAfterShutdown() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances();
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    IQueue<String> q1 = h1.getQueue("default");
    IQueue<String> q2 = h2.getQueue("default");
    q2.offer("item");
    assertEquals(1, q1.size());
    assertEquals(1, q2.size());
    assertEquals("item", q1.take());
    assertEquals(0, q1.size());
    assertEquals(0, q2.size());
    h1.getLifecycleService().shutdown();
    assertEquals(0, q2.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) 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)743 HazelcastInstance (com.hazelcast.core.HazelcastInstance)665 Test (org.junit.Test)632 QuickTest (com.hazelcast.test.annotation.QuickTest)618 ParallelTest (com.hazelcast.test.annotation.ParallelTest)598 Config (com.hazelcast.config.Config)361 MapConfig (com.hazelcast.config.MapConfig)146 MapStoreConfig (com.hazelcast.config.MapStoreConfig)101 CountDownLatch (java.util.concurrent.CountDownLatch)99 AssertTask (com.hazelcast.test.AssertTask)94 NightlyTest (com.hazelcast.test.annotation.NightlyTest)70 IMap (com.hazelcast.core.IMap)65 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)62 MapIndexConfig (com.hazelcast.config.MapIndexConfig)51 TransactionException (com.hazelcast.transaction.TransactionException)46 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)43 Member (com.hazelcast.core.Member)41 NearCacheConfig (com.hazelcast.config.NearCacheConfig)40 Map (java.util.Map)38 Before (org.junit.Before)35