Search in sources :

Example 51 with VersionedObject

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

the class TransactionQueueTest method testSingleQueueAtomicity.

@Test
public void testSingleQueueAtomicity() throws ExecutionException, InterruptedException {
    String name = randomString();
    int itemCount = 200;
    HazelcastInstance instance = createHazelcastInstance();
    Future<Integer> f = spawn(() -> {
        IQueue<VersionedObject<String>> queue = instance.getQueue(name);
        queue.take();
        return queue.size();
    });
    TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<VersionedObject<String>> queue = context.getQueue(name);
    for (int i = 0; i < itemCount; i++) {
        queue.offer(new VersionedObject<>("item-" + i, i));
    }
    context.commitTransaction();
    int size = f.get();
    assertEquals(itemCount - 1, size);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 52 with VersionedObject

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

the class TransactionQueueTest method testTransactionalOfferPoll2.

@Test
public void testTransactionalOfferPoll2() {
    int insCount = 4;
    String name0 = "defQueue0";
    String name1 = "defQueue1";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    HazelcastInstance[] instances = factory.newInstances(getConfig());
    CountDownLatch latch = new CountDownLatch(1);
    new Thread(() -> {
        try {
            latch.await(5, SECONDS);
            sleepMillis(3000);
            getQueue(instances, name0).offer(new VersionedObject<>("item0"));
        } catch (InterruptedException | HazelcastInstanceNotActiveException ignored) {
        }
    }).start();
    TransactionContext context = instances[0].newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<VersionedObject<String>> q0 = context.getQueue(name0);
    TransactionalQueue<VersionedObject<String>> q1 = context.getQueue(name1);
    VersionedObject<String> s = null;
    latch.countDown();
    try {
        s = q0.poll(10, SECONDS);
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    assertEquals(new VersionedObject<>("item0"), s);
    q1.offer(s);
    context.commitTransaction();
    assertEquals(0, getQueue(instances, name0).size());
    assertEquals(new VersionedObject<>("item0"), getQueue(instances, name1).poll());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 53 with VersionedObject

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

the class TransactionQueueTest method testTransactionalOfferPoll1.

@Test
public void testTransactionalOfferPoll1() {
    int insCount = 4;
    String name = "defQueue";
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
    HazelcastInstance[] instances = factory.newInstances(getConfig());
    TransactionContext context = instances[0].newTransactionContext();
    context.beginTransaction();
    try {
        TransactionalQueue<VersionedObject<String>> q = context.getQueue(name);
        assertTrue(q.offer(new VersionedObject<>("ali")));
        VersionedObject<String> s = q.poll();
        assertEquals(new VersionedObject<>("ali"), s);
        context.commitTransaction();
    } catch (TransactionException e) {
        context.rollbackTransaction();
        throw e;
    }
    assertEquals(0, getQueue(instances, name).size());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 54 with VersionedObject

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

the class TransactionQueueTest method nestedTransactionTest.

@Test(expected = IllegalStateException.class)
public void nestedTransactionTest() {
    HazelcastInstance instance = createHazelcastInstance();
    TransactionContext outerTnx = instance.newTransactionContext();
    outerTnx.beginTransaction();
    String name = randomString();
    VersionedObject<String> item = new VersionedObject<>(randomString());
    outerTnx.getQueue(name).offer(item);
    try {
        TransactionContext innerTnx = instance.newTransactionContext();
        innerTnx.beginTransaction();
        innerTnx.getQueue(name).offer(item);
        innerTnx.commitTransaction();
    } finally {
        outerTnx.rollbackTransaction();
    }
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with VersionedObject

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

the class TransactionQueueTest method testTransactionalOfferAndPollWithTimeout.

@Test
public void testTransactionalOfferAndPollWithTimeout() throws InterruptedException {
    HazelcastInstance instance = createHazelcastInstance();
    VersionedObject<String> item = new VersionedObject<>("offered");
    String queueName = "testTransactionalOfferAndPollWithTimeout";
    TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<VersionedObject<String>> txnQueue = context.getQueue(queueName);
    assertTrue(txnQueue.offer(item));
    assertEquals(1, txnQueue.size());
    assertEquals(item, txnQueue.poll(5, SECONDS));
    context.commitTransaction();
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) 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