Search in sources :

Example 36 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject 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(getConfig());
    IQueue<VersionedObject<String>> queue = instance.getQueue("testInterruption");
    CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread(() -> {
        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(new VersionedObject<>("message"));
    assertEquals(1, queue.size());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Ignore(org.junit.Ignore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 37 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class QueueTestsFrom2X method testListenerLifecycle.

@Test
public void testListenerLifecycle() throws Exception {
    long sleep = 2000;
    String name = "listenerLifecycle";
    HazelcastInstance instance = createHazelcastInstance();
    IQueue<VersionedObject<Integer>> queue = instance.getQueue(name);
    try {
        CountDownLatch latch = new CountDownLatch(3);
        ItemListener<VersionedObject<Integer>> listener = new ItemListener<VersionedObject<Integer>>() {

            public void itemAdded(ItemEvent<VersionedObject<Integer>> itemEvent) {
                latch.countDown();
            }

            public void itemRemoved(ItemEvent<VersionedObject<Integer>> itemEvent) {
            }
        };
        queue.addItemListener(listener, false);
        queue.offer(new VersionedObject<>(1));
        sleep(sleep);
        queue.destroy();
        queue = instance.getQueue(name);
        UUID id = queue.addItemListener(listener, false);
        queue.offer(new VersionedObject<>(2));
        sleep(sleep);
        queue.removeItemListener(id);
        queue.offer(new VersionedObject<>(3));
        sleep(sleep);
        assertEquals(1, latch.getCount());
        latch.countDown();
        assertTrue("Remaining: " + latch.getCount(), latch.await(3, TimeUnit.SECONDS));
    } finally {
        queue.destroy();
    }
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ItemEvent(com.hazelcast.collection.ItemEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ItemListener(com.hazelcast.collection.ItemListener) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 38 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class QueueAdvancedTest method testOfferLong.

@Test
public void testOfferLong() throws InterruptedException {
    Config config = getConfig();
    config.getQueueConfig("default").setMaxSize(200);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(config);
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    IQueue<VersionedObject<String>> q1 = h1.getQueue("default");
    IQueue<VersionedObject<String>> q2 = h2.getQueue("default");
    for (int i = 0; i < 100; i++) {
        assertTrue("Expected q1.offer() to succeed", q1.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
        assertTrue("Expected q2.offer() to succeed", q2.offer(new VersionedObject<>("item" + i, i), 100, SECONDS));
    }
    assertFalse(q1.offer(new VersionedObject<>("item")));
    assertFalse(q2.offer(new VersionedObject<>("item")));
    assertFalse(q1.offer(new VersionedObject<>("item"), 2, SECONDS));
    assertFalse(q2.offer(new VersionedObject<>("item"), 2, SECONDS));
    CountDownLatch pollLatch = new CountDownLatch(200);
    new Thread(() -> {
        try {
            Thread.sleep(3000);
            for (int i = 0; i < 100; i++) {
                if (new VersionedObject<>("item" + i, i).equals(q1.poll(2, SECONDS))) {
                    pollLatch.countDown();
                }
                if (new VersionedObject<>("item" + i, i).equals(q2.poll(2, SECONDS))) {
                    pollLatch.countDown();
                }
            }
        } catch (InterruptedException e) {
            LOG.info(e);
        }
    }).start();
    assertOpenEventually(pollLatch);
    ExecutorService es = Executors.newFixedThreadPool(50);
    CountDownLatch latch = new CountDownLatch(200);
    for (int i = 0; i < 100; i++) {
        es.execute(() -> {
            try {
                if (q1.offer(new VersionedObject<>("item"), 30, SECONDS)) {
                    latch.countDown();
                }
            } catch (InterruptedException e) {
                LOG.info(e);
            }
        });
        es.execute(() -> {
            try {
                if (q2.offer(new VersionedObject<>("item"), 30, SECONDS)) {
                    latch.countDown();
                }
            } catch (InterruptedException e) {
                LOG.info(e);
            }
        });
    }
    assertOpenEventually(latch);
    es.shutdown();
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TestThread(com.hazelcast.test.TestThread) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 39 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class QueueAdvancedTest method testDrainBackup.

@Test
public void testDrainBackup() {
    HazelcastInstance[] instances = createHazelcastInstances();
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];
    String name = generateKeyOwnedBy(instance1);
    IQueue<VersionedObject<String>> queue1 = instance1.getQueue(name);
    IQueue<VersionedObject<String>> queue2 = instance2.getQueue(name);
    for (int i = 0; i < 4; i++) {
        queue1.offer(new VersionedObject<>("item" + i, i));
    }
    assertSizeEventually(4, queue2);
    assertIterableEquals(queue2, new VersionedObject<>("item0", 0), new VersionedObject<>("item1", 1), new VersionedObject<>("item2", 2), new VersionedObject<>("item3", 3));
    List<VersionedObject<String>> list = new ArrayList<>();
    queue1.drainTo(list, 2);
    instance1.shutdown();
    assertSizeEventually(2, queue2);
    assertIterableEquals(queue2, new VersionedObject<>("item2", 2), new VersionedObject<>("item3", 3));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ArrayList(java.util.ArrayList) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 40 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class QueueAdvancedTest method queueEntriesShouldBeConsistentAfterShutdown.

@Test
public void queueEntriesShouldBeConsistentAfterShutdown() {
    HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(getConfig());
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    Queue<VersionedObject<String>> q1 = h1.getQueue("q");
    Queue<VersionedObject<String>> q2 = h2.getQueue("q");
    for (int i = 0; i < 5; i++) {
        q1.offer(new VersionedObject<>("item" + i, i));
    }
    assertEquals(5, q1.size());
    assertEquals(5, q2.size());
    assertEquals(new VersionedObject<>("item0", 0), q2.poll());
    assertEquals(new VersionedObject<>("item1", 1), q2.poll());
    assertEquals(new VersionedObject<>("item2", 2), q2.poll());
    assertSizeEventually(2, q1);
    assertSizeEventually(2, q2);
    h1.getLifecycleService().shutdown();
    assertSizeEventually(2, q2);
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)78 Test (org.junit.Test)77 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)76 QuickTest (com.hazelcast.test.annotation.QuickTest)76 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 TransactionContext (com.hazelcast.transaction.TransactionContext)25 Config (com.hazelcast.config.Config)23 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)19 CountDownLatch (java.util.concurrent.CountDownLatch)16 QueueStoreConfig (com.hazelcast.config.QueueStoreConfig)10 LocalQueueStats (com.hazelcast.collection.LocalQueueStats)8 ArrayList (java.util.ArrayList)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 TestThread (com.hazelcast.test.TestThread)5 TransactionException (com.hazelcast.transaction.TransactionException)4 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)3 ListenerConfig (com.hazelcast.config.ListenerConfig)3 QueueConfig (com.hazelcast.config.QueueConfig)3 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 ExecutorService (java.util.concurrent.ExecutorService)3