Search in sources :

Example 6 with TransactionalQueue

use of com.hazelcast.core.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 (Exception 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.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with TransactionalQueue

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

the class ClientTxnQueueTest method testTransactionalOfferPoll.

@Test
public void testTransactionalOfferPoll() {
    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);
    assertEquals(item, txnQueue.poll());
    context.commitTransaction();
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) IQueue(com.hazelcast.core.IQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with TransactionalQueue

use of com.hazelcast.core.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.core.TransactionalQueue) IQueue(com.hazelcast.core.IQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with TransactionalQueue

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

the class ClientTxnQueueTest method testTransactionalQueueGetsOfferedItems_whenBlockedOnPoll.

@Test
public void testTransactionalQueueGetsOfferedItems_whenBlockedOnPoll() throws InterruptedException {
    final String item = "offered1";
    final String queueName = randomString();
    final IQueue queue1 = client.getQueue(queueName);
    final CountDownLatch justBeforeBlocked = new CountDownLatch(1);
    new Thread() {

        public void run() {
            try {
                justBeforeBlocked.await();
                sleepSeconds(1);
                queue1.offer(item);
            } catch (InterruptedException e) {
                fail("failed" + e);
            }
        }
    }.start();
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue1 = context.getQueue(queueName);
    justBeforeBlocked.countDown();
    Object result = txnQueue1.poll(5, TimeUnit.SECONDS);
    assertEquals("TransactionalQueue while blocked in pol should get item offered from client queue", item, result);
    context.commitTransaction();
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) IQueue(com.hazelcast.core.IQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with TransactionalQueue

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

the class ClientTxnQueueTest method testTransactionalPeek.

@Test
public void testTransactionalPeek() {
    final String item = "offered";
    final String queunName = randomString();
    final IQueue queue = client.getQueue(queunName);
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue = context.getQueue(queunName);
    txnQueue.offer(item);
    assertEquals(item, txnQueue.peek());
    assertEquals(item, txnQueue.peek());
    context.commitTransaction();
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) IQueue(com.hazelcast.core.IQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionalQueue (com.hazelcast.core.TransactionalQueue)13 TransactionContext (com.hazelcast.transaction.TransactionContext)13 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 IQueue (com.hazelcast.core.IQueue)4 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 MembershipAdapter (com.hazelcast.core.MembershipAdapter)2 MembershipEvent (com.hazelcast.core.MembershipEvent)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 TransactionException (com.hazelcast.transaction.TransactionException)1 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)1