Search in sources :

Example 66 with VersionedObject

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

the class QueueTestsFrom2X method testQueueRemove.

@Test
public void testQueueRemove() {
    HazelcastInstance instance = createHazelcastInstance();
    IQueue<VersionedObject<String>> q = instance.getQueue("testQueueRemove");
    for (int i = 0; i < 10; i++) {
        q.offer(new VersionedObject<>("item" + i, i));
    }
    for (int i = 0; i < 5; i++) {
        assertNotNull(q.poll());
    }
    assertEquals(new VersionedObject<>("item5", 5), q.peek());
    boolean removed = q.remove(new VersionedObject<>("item5", 5));
    assertTrue(removed);
    Iterator<VersionedObject<String>> it = q.iterator();
    int i = 6;
    while (it.hasNext()) {
        int itemId = i++;
        assertEquals(new VersionedObject<>("item" + itemId, itemId), it.next());
    }
    assertEquals(4, q.size());
}
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 67 with VersionedObject

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

the class QueueTestsFrom2X method issue391.

@Test
public void issue391() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    int total = 10;
    Collection<VersionedObject<Integer>> results = new ArrayList<>(5);
    HazelcastInstance hz1 = factory.newHazelcastInstance(getConfig());
    CountDownLatch latchOffer = new CountDownLatch(1);
    CountDownLatch latchTake = new CountDownLatch(1);
    spawn(() -> {
        try {
            IQueue<VersionedObject<Integer>> q = hz1.getQueue("q");
            for (int i = 0; i < total; i++) {
                results.add(q.take());
            }
            latchTake.countDown();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    });
    HazelcastInstance hz2 = factory.newHazelcastInstance(getConfig());
    waitAllForSafeState(hz1, hz2);
    spawn(() -> {
        for (int i = 0; i < total; i++) {
            hz2.getQueue("q").offer(new VersionedObject<>(i, i));
        }
        latchOffer.countDown();
    });
    assertTrue(latchOffer.await(100, TimeUnit.SECONDS));
    assertTrue(latchTake.await(10, TimeUnit.SECONDS));
    assertTrue(hz1.getQueue("q").isEmpty());
    hz1.shutdown();
    assertTrue(hz2.getQueue("q").isEmpty());
    @SuppressWarnings("unchecked") VersionedObject<Integer>[] objects = new VersionedObject[total];
    for (int i = 0; i < total; i++) {
        objects[i] = new VersionedObject<>(i, i);
    }
    assertArrayEquals(objects, results.toArray());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 68 with VersionedObject

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

the class QueueTestsFrom2X method issue427QOfferIncorrectWithinTransaction.

@Test
public void issue427QOfferIncorrectWithinTransaction() {
    Config config = getConfig();
    config.getQueueConfig("default").setMaxSize(100);
    HazelcastInstance instance = createHazelcastInstance(config);
    TransactionContext transactionContext = instance.newTransactionContext();
    transactionContext.beginTransaction();
    TransactionalQueue<VersionedObject<Integer>> queue = transactionContext.getQueue("default");
    for (int i = 0; i < 100; i++) {
        queue.offer(new VersionedObject<>(i, i));
    }
    boolean result = queue.offer(new VersionedObject<>(100, 100));
    assertEquals(100, queue.size());
    transactionContext.commitTransaction();
    assertEquals(100, instance.getQueue("default").size());
    assertFalse(result);
    instance.shutdown();
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 69 with VersionedObject

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

the class QueueTestsFrom2X method testQueueContains.

@Test
public void testQueueContains() {
    HazelcastInstance instance = createHazelcastInstance();
    IQueue<VersionedObject<String>> queue = instance.getQueue("testQueueContains");
    @SuppressWarnings("unchecked") VersionedObject<String>[] items = new VersionedObject[] { new VersionedObject<>("one"), new VersionedObject<>("two"), new VersionedObject<>("three"), new VersionedObject<>("four") };
    queue.addAll(asList(items));
    assertContains(queue, new VersionedObject<>("one"));
    assertContains(queue, new VersionedObject<>("two"));
    assertContains(queue, new VersionedObject<>("three"));
    assertContains(queue, new VersionedObject<>("four"));
}
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 70 with VersionedObject

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

the class QueueTestsFrom2X method issue114TestQueueListenersUnderTransaction.

/**
 * Github issue #114
 */
@Test
public void issue114TestQueueListenersUnderTransaction() throws Exception {
    String name = "issue99TestQueueTakeAndDuringRollback";
    HazelcastInstance hz = createHazelcastInstance();
    IQueue<VersionedObject<String>> testQueue = hz.getQueue(name);
    CountDownLatch offerLatch = new CountDownLatch(2);
    CountDownLatch pollLatch = new CountDownLatch(2);
    testQueue.addItemListener(new ItemListener<VersionedObject<String>>() {

        public void itemAdded(ItemEvent<VersionedObject<String>> item) {
            offerLatch.countDown();
        }

        public void itemRemoved(ItemEvent<VersionedObject<String>> item) {
            pollLatch.countDown();
        }
    }, true);
    TransactionContext context = hz.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<VersionedObject<String>> queue = context.getQueue(name);
    queue.offer(new VersionedObject<>("tx Hello"));
    queue.offer(new VersionedObject<>("tx World"));
    context.commitTransaction();
    TransactionContext context2 = hz.newTransactionContext();
    context2.beginTransaction();
    TransactionalQueue<VersionedObject<String>> queue2 = context2.getQueue(name);
    assertEquals(new VersionedObject<>("tx Hello"), queue2.poll());
    assertEquals(new VersionedObject<>("tx World"), queue2.poll());
    context2.commitTransaction();
    assertTrue("Remaining offer listener count: " + offerLatch.getCount(), offerLatch.await(2, TimeUnit.SECONDS));
    assertTrue("Remaining poll listener count: " + pollLatch.getCount(), pollLatch.await(2, TimeUnit.SECONDS));
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) CountDownLatch(java.util.concurrent.CountDownLatch) 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