Search in sources :

Example 21 with VersionedObject

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

the class QueueAdvancedTest method testShutdown.

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

Example 22 with VersionedObject

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

the class QueueAdvancedTest method testOffer.

@Test
public void testOffer() 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");
    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));
    }
    assertEquals(new VersionedObject<>("item0", 0), q1.peek());
    assertEquals(new VersionedObject<>("item0", 0), q2.peek());
    for (int i = 0; i < 100; i++) {
        assertEquals(new VersionedObject<>("item" + i, i), q1.poll());
        assertEquals(new VersionedObject<>("item" + i, i), q2.poll());
    }
}
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)

Example 23 with VersionedObject

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

the class QueueAdvancedTest method testTake.

@Test
public void testTake() {
    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);
    new Thread(() -> {
        try {
            Thread.sleep(3000);
            for (int i = 0; i < 100; i++) {
                if (q1.offer(new VersionedObject<>("item"))) {
                    offerLatch.countDown();
                }
                if (q2.offer(new VersionedObject<>("item"))) {
                    offerLatch.countDown();
                }
            }
        } catch (InterruptedException e) {
            LOG.info(e);
        }
    }).start();
    assertOpenEventually(offerLatch);
    ExecutorService es = Executors.newFixedThreadPool(50);
    CountDownLatch latch = new CountDownLatch(200);
    for (int i = 0; i < 100; i++) {
        es.execute(() -> {
            try {
                if (new VersionedObject<>("item").equals(q1.take())) {
                    latch.countDown();
                }
            } catch (InterruptedException e) {
                LOG.info(e);
            }
        });
        es.execute(() -> {
            try {
                if (new VersionedObject<>("item").equals(q2.take())) {
                    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 24 with VersionedObject

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

the class QueueEvictionTest method testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted.

@Test
public void testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted() throws Exception {
    String queueName = randomString();
    Config config = smallInstanceConfig();
    config.getQueueConfig("default").setPriorityComparatorClassName(comparatorClassName);
    config.getQueueConfig(queueName).setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

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

        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<VersionedObject<String>> queue = hz.getQueue(queueName);
    assertTrue(queue.offer(new VersionedObject<>("item")));
    assertEquals(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) Config(com.hazelcast.config.Config) 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 25 with VersionedObject

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

the class QueueEvictionTest method testQueueEviction_whenTtlIsSet_thenTakeThrowsException.

@Test
public void testQueueEviction_whenTtlIsSet_thenTakeThrowsException() throws Exception {
    String queueName = randomString();
    Config config = smallInstanceConfig();
    config.getQueueConfig(queueName).setPriorityComparatorClassName(comparatorClassName).setEmptyQueueTtl(2);
    HazelcastInstance hz = createHazelcastInstance(config);
    IQueue<VersionedObject<String>> queue = hz.getQueue(queueName);
    try {
        assertTrue(queue.offer(new VersionedObject<>("item")));
        assertEquals(new VersionedObject<>("item"), queue.poll());
        queue.take();
        fail();
    } catch (DistributedObjectDestroyedException expected) {
        ignore(expected);
    }
    assertEquals(0, queue.size());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) DistributedObjectDestroyedException(com.hazelcast.spi.exception.DistributedObjectDestroyedException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) 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