Search in sources :

Example 11 with TransactionalQueue

use of com.hazelcast.transaction.TransactionalQueue in project hazelcast by hazelcast.

the class ClientTxnQueueTest method testQueueSizeAfterTxnOfferPoll.

@Test
public void testQueueSizeAfterTxnOfferPoll() {
    final String item = "offered";
    final String queueName = randomString();
    final IQueue queue = client.getQueue(queueName);
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue = context.getQueue(queueName);
    txnQueue.offer(item);
    txnQueue.poll();
    context.commitTransaction();
    assertEquals(0, queue.size());
}
Also used : TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) IQueue(com.hazelcast.collection.IQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with TransactionalQueue

use of com.hazelcast.transaction.TransactionalQueue in project hazelcast by hazelcast.

the class ClientTxnTest method testTxnRollback.

@Test
public void testTxnRollback() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    try {
        context.beginTransaction();
        assertNotNull(context.getTxnId());
        final TransactionalQueue queue = context.getQueue(queueName);
        queue.offer(randomString());
        server.shutdown();
        context.commitTransaction();
        fail("commit should throw exception!!!");
    } catch (TransactionException e) {
        context.rollbackTransaction();
        txnRollbackLatch.countDown();
    }
    assertOpenEventually(txnRollbackLatch);
    assertOpenEventually(memberRemovedLatch);
    final IQueue<Object> q = client.getQueue(queueName);
    assertNull(q.poll());
    assertEquals(0, q.size());
}
Also used : TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.cluster.MembershipEvent) MembershipAdapter(com.hazelcast.cluster.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with TransactionalQueue

use of com.hazelcast.transaction.TransactionalQueue in project hazelcast by hazelcast.

the class TransactionQueueTest method testListener_withOffer.

@Test
public void testListener_withOffer() {
    HazelcastInstance hz = createHazelcastInstance();
    String name = randomName();
    IQueue<VersionedObject<String>> queue = hz.getQueue(name);
    EventCountingItemListener listener = new EventCountingItemListener();
    queue.addItemListener(listener, true);
    hz.executeTransaction((TransactionalTask<Object>) ctx -> {
        TransactionalQueue<VersionedObject<Object>> queue1 = ctx.getQueue(name);
        return queue1.offer(new VersionedObject<>("item"));
    });
    assertTrueEventually(() -> assertEquals(1, listener.adds.get()));
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Arrays(java.util.Arrays) QuickTest(com.hazelcast.test.annotation.QuickTest) TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) RunWith(org.junit.runner.RunWith) QueueService(com.hazelcast.collection.impl.queue.QueueService) Random(java.util.Random) TransactionException(com.hazelcast.transaction.TransactionException) ItemEvent(com.hazelcast.collection.ItemEvent) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Assert.fail(org.junit.Assert.fail) IQueue(com.hazelcast.collection.IQueue) Parameterized(org.junit.runners.Parameterized) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Assert.assertNotNull(org.junit.Assert.assertNotNull) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) TransactionOptions(com.hazelcast.transaction.TransactionOptions) Category(org.junit.experimental.categories.Category) ExecutionException(java.util.concurrent.ExecutionException) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) ItemListener(com.hazelcast.collection.ItemListener) Ignore(org.junit.Ignore) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) VersionedObjectComparator(com.hazelcast.collection.impl.queue.model.VersionedObjectComparator) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionalTask(com.hazelcast.transaction.TransactionalTask) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionContext (com.hazelcast.transaction.TransactionContext)13 TransactionalQueue (com.hazelcast.transaction.TransactionalQueue)13 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 IQueue (com.hazelcast.collection.IQueue)5 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)4 TransactionException (com.hazelcast.transaction.TransactionException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 MembershipAdapter (com.hazelcast.cluster.MembershipAdapter)2 MembershipEvent (com.hazelcast.cluster.MembershipEvent)2 Config (com.hazelcast.config.Config)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 ItemEvent (com.hazelcast.collection.ItemEvent)1 ItemListener (com.hazelcast.collection.ItemListener)1 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 VersionedObjectComparator (com.hazelcast.collection.impl.queue.model.VersionedObjectComparator)1 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 Accessors.getNode (com.hazelcast.test.Accessors.getNode)1