Search in sources :

Example 41 with VersionedObject

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

the class QueueAdvancedTest method testPollLong.

@Test
public void testPollLong() throws Exception {
    HazelcastInstance[] instances = createHazelcastInstanceFactory(2).newInstances(getConfig());
    HazelcastInstance h1 = instances[0];
    HazelcastInstance h2 = instances[1];
    IQueue<VersionedObject<String>> q1 = h1.getQueue("default");
    IQueue<VersionedObject<String>> q2 = h2.getQueue("default");
    CountDownLatch offerLatch = new CountDownLatch(2 * 100);
    Thread.sleep(1000);
    new Thread(() -> {
        for (int i = 0; i < 100; i++) {
            if (q1.offer(new VersionedObject<>("item"))) {
                offerLatch.countDown();
            }
            if (q2.offer(new VersionedObject<>("item"))) {
                offerLatch.countDown();
            }
        }
    }).start();
    assertOpenEventually(offerLatch);
    ExecutorService es = Executors.newFixedThreadPool(50);
    CountDownLatch latch = new CountDownLatch(200);
    Thread.sleep(3000);
    for (int i = 0; i < 100; i++) {
        es.execute(() -> {
            try {
                if (new VersionedObject<>("item").equals(q1.poll(5, SECONDS))) {
                    latch.countDown();
                }
            } catch (InterruptedException e) {
                LOG.info(e);
            }
        });
        es.execute(() -> {
            try {
                if (new VersionedObject<>("item").equals(q2.poll(5, 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) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.hazelcast.test.TestThread) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 42 with VersionedObject

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

the class QueueListenerTest method testConfigListenerRegistration.

@Test
public void testConfigListenerRegistration() throws Exception {
    Config config = getConfig();
    String name = "queue";
    QueueConfig queueConfig = config.getQueueConfig(name);
    CountdownItemListener listener = new CountdownItemListener(1, 1);
    ItemListenerConfig itemListenerConfig = new ItemListenerConfig(listener, true);
    queueConfig.addItemListenerConfig(itemListenerConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    IQueue<VersionedObject<String>> queue = instance.getQueue(name);
    queue.offer(new VersionedObject<>("item"));
    queue.poll();
    assertTrue(listener.added.await(10, TimeUnit.SECONDS));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) QueueConfig(com.hazelcast.config.QueueConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) QueueConfig(com.hazelcast.config.QueueConfig) Config(com.hazelcast.config.Config) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 43 with VersionedObject

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

the class QueueListenerTest method testListener_withEvictionViaTTL.

@Test
public void testListener_withEvictionViaTTL() throws Exception {
    Config config = getConfig();
    config.getQueueConfig("queueWithTTL").setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            latch.countDown();
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<VersionedObject<String>> queue = hz.getQueue("queueWithTTL");
    queue.offer(new VersionedObject<>("item"));
    queue.poll();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) QueueConfig(com.hazelcast.config.QueueConfig) Config(com.hazelcast.config.Config) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 44 with VersionedObject

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

the class QueueListenerTest method testListeners.

@Test
public void testListeners() throws Exception {
    int totalQueuePut = 2000;
    String queueName = randomString();
    HazelcastInstance instance = createHazelcastInstance();
    IQueue<VersionedObject<String>> queue = instance.getQueue(queueName);
    TestItemListener listener = new TestItemListener(totalQueuePut);
    UUID listenerId = queue.addItemListener(listener, true);
    for (int i = 0; i < totalQueuePut / 2; i++) {
        queue.offer(new VersionedObject<>("item-" + i, i));
    }
    for (int i = 0; i < totalQueuePut / 2; i++) {
        queue.poll();
    }
    assertTrue(listener.latch.await(5, TimeUnit.SECONDS));
    queue.removeItemListener(listenerId);
    queue.offer(new VersionedObject<>("item-a"));
    queue.poll();
    assertTrue(listener.notCalled.get());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 45 with VersionedObject

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

the class QueueAdvancedTest method testCompareAndRemoveBackup.

@Test
public void testCompareAndRemoveBackup() {
    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<>();
    list.add(new VersionedObject<>("item0", 0));
    list.add(new VersionedObject<>("item1", 1));
    list.add(new VersionedObject<>("item2", 2));
    assertTrue("Expected queue1.removeAll() to succeed", queue1.removeAll(list));
    instance1.shutdown();
    assertSizeEventually(1, queue2);
    assertIterableEquals(queue2, 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)

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