Search in sources :

Example 26 with TestHazelcastInstanceFactory

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

the class QueueAdvancedTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances();
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    warmUpPartitions(h2, h1);
    IQueue<String> q1 = h1.getQueue("default");
    IQueue<String> q2 = h2.getQueue("default");
    for (int i = 0; i < 40; i++) {
        assertTrue("Expected q1.offer() to succeed", q1.offer("item" + i, 100, SECONDS));
    }
    h1.getLifecycleService().shutdown();
    for (int i = 40; i < 100; i++) {
        assertTrue("Expected q2.offer() to succeed", q2.offer("item" + i, 100, SECONDS));
    }
    for (int i = 0; i < 100; i++) {
        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 27 with TestHazelcastInstanceFactory

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

the class QueueAdvancedTest method testTake.

@Test
public void testTake() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances();
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    final IQueue<String> q1 = h1.getQueue("default");
    final IQueue<String> q2 = h2.getQueue("default");
    final CountDownLatch offerLatch = new CountDownLatch(2 * 100);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                for (int i = 0; i < 100; i++) {
                    if (q1.offer("item")) {
                        offerLatch.countDown();
                    }
                    if (q2.offer("item")) {
                        offerLatch.countDown();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    assertOpenEventually(offerLatch);
    ExecutorService es = Executors.newFixedThreadPool(50);
    final CountDownLatch latch = new CountDownLatch(200);
    for (int i = 0; i < 100; i++) {
        es.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if ("item".equals(q1.take())) {
                        latch.countDown();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        es.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if ("item".equals(q2.take())) {
                        latch.countDown();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    assertOpenEventually(latch);
    es.shutdown();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TestThread(com.hazelcast.test.TestThread) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 28 with TestHazelcastInstanceFactory

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

the class QueueTestsFrom2X method testQueueOfferCommitSize.

@Test
public void testQueueOfferCommitSize() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = factory.newHazelcastInstance();
    HazelcastInstance instance2 = factory.newHazelcastInstance();
    TransactionContext context = instance1.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<String> txnQ1 = context.getQueue("testQueueOfferCommitSize");
    TransactionalQueue<String> txnQ2 = context.getQueue("testQueueOfferCommitSize");
    txnQ1.offer("item");
    assertEquals(1, txnQ1.size());
    assertEquals(1, txnQ2.size());
    context.commitTransaction();
    assertEquals(1, instance1.getQueue("testQueueOfferCommitSize").size());
    assertEquals(1, instance2.getQueue("testQueueOfferCommitSize").size());
    assertEquals("item", instance2.getQueue("testQueueOfferCommitSize").poll());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 29 with TestHazelcastInstanceFactory

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

the class QueueTestsFrom2X method testInterruption.

@Test
@Ignore
public void testInterruption() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
    HazelcastInstance instance = factory.newHazelcastInstance(new Config());
    final IQueue<String> queue = instance.getQueue("testInterruption");
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                queue.poll(5, TimeUnit.SECONDS);
                fail();
            } catch (InterruptedException e) {
                latch.countDown();
            }
        }
    });
    thread.start();
    sleep(2000);
    thread.interrupt();
    assertTrue(latch.await(100, TimeUnit.SECONDS));
    queue.offer("message");
    assertEquals(1, queue.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Ignore(org.junit.Ignore) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 30 with TestHazelcastInstanceFactory

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

the class QueueTestsFrom2X method testQueuePollRollbackSize.

@Test
public void testQueuePollRollbackSize() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance = factory.newHazelcastInstance();
    factory.newHazelcastInstance();
    TransactionContext context = instance.newTransactionContext();
    IQueue<Object> queue = instance.getQueue("testQueuePollRollbackSize");
    queue.offer("item1");
    queue.offer("item2");
    assertEquals(2, queue.size());
    context.beginTransaction();
    TransactionalQueue txnQ1 = context.getQueue("testQueuePollRollbackSize");
    assertEquals("item1", txnQ1.poll());
    assertEquals(1, txnQ1.size());
    assertEquals(1, queue.size());
    context.rollbackTransaction();
    assertEquals(2, queue.size());
    assertEquals("item1", queue.poll());
    assertEquals("item2", queue.poll());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) 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