Search in sources :

Example 36 with TransactionContext

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

the class TransactionListTest method testOrder_WhenMultipleConcurrentTransactionRollback.

@Test
public void testOrder_WhenMultipleConcurrentTransactionRollback() throws InterruptedException {
    final HazelcastInstance instance = createHazelcastInstance();
    final String name = randomString();
    IList<Integer> list = instance.getList(name);
    list.add(1);
    list.add(2);
    list.add(3);
    TransactionContext firstContext = instance.newTransactionContext();
    firstContext.beginTransaction();
    firstContext.getList(name).remove(1);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread() {

        @Override
        public void run() {
            TransactionContext secondContext = instance.newTransactionContext();
            secondContext.beginTransaction();
            secondContext.getList(name).remove(2);
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            secondContext.rollbackTransaction();
        }
    };
    thread.start();
    firstContext.rollbackTransaction();
    latch.countDown();
    thread.join();
    assertEquals(1, list.get(0).intValue());
    assertEquals(2, list.get(1).intValue());
    assertEquals(3, list.get(2).intValue());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 37 with TransactionContext

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

the class TransactionListTest method testAdd.

@Test
public void testAdd() {
    HazelcastInstance instance = createHazelcastInstance();
    String name = randomString();
    String item = randomString();
    IList<Object> list = instance.getList(name);
    TransactionContext context = instance.newTransactionContext();
    try {
        context.beginTransaction();
        TransactionalList<Object> txnList = context.getList(name);
        assertTrue(txnList.add(item));
        context.commitTransaction();
    } catch (Exception e) {
        fail(e.getMessage());
        context.rollbackTransaction();
    }
    assertEquals(1, list.size());
    assertEquals(item, list.get(0));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 38 with TransactionContext

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

the class TransactionListTest method testMigrationSerializationNotFails_whenTransactionsAreUsed.

@Test
public void testMigrationSerializationNotFails_whenTransactionsAreUsed() throws Exception {
    Config config = new Config();
    config.setProperty("hazelcast.partition.count", "2");
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = factory.newHazelcastInstance(config);
    String listName = randomString();
    TransactionContext tr = instance1.newTransactionContext();
    tr.beginTransaction();
    TransactionalList<Object> list = tr.getList(listName);
    for (int i = 0; i < 10; i++) {
        list.add(i);
    }
    tr.commitTransaction();
    HazelcastInstance instance2 = factory.newHazelcastInstance(config);
    Member owner = instance1.getPartitionService().getPartition(listName).getOwner();
    HazelcastInstance aliveInstance;
    if (instance1.getCluster().getLocalMember().equals(owner)) {
        instance1.shutdown();
        aliveInstance = instance2;
    } else {
        instance2.shutdown();
        aliveInstance = instance1;
    }
    IList<Object> l = aliveInstance.getList(listName);
    for (int i = 0; i < 10; i++) {
        assertEquals(i, l.get(i));
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 39 with TransactionContext

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

the class TransactionQueueTest method testPeekWithTimeout.

@Test
public void testPeekWithTimeout() {
    final String name = randomString();
    final String item = randomString();
    HazelcastInstance instance = createHazelcastInstance();
    final IQueue<String> queue = instance.getQueue(name);
    spawn(new Runnable() {

        @Override
        public void run() {
            sleepSeconds(1);
            queue.offer(item);
        }
    });
    TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    try {
        TransactionalQueue<String> txnQueue = context.getQueue(name);
        String peeked = txnQueue.peek(10, SECONDS);
        assertEquals(item, peeked);
        context.commitTransaction();
    } catch (Exception e) {
        context.rollbackTransaction();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TransactionException(com.hazelcast.transaction.TransactionException) ExecutionException(java.util.concurrent.ExecutionException) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 40 with TransactionContext

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

the class TransactionQueueTest method testPollWithTimeout_WithAnotherThreadOffering.

@Test
public void testPollWithTimeout_WithAnotherThreadOffering() throws InterruptedException {
    final HazelcastInstance instance = createHazelcastInstance();
    final String name = randomString();
    final CountDownLatch offerReserveLatch = new CountDownLatch(1);
    spawn(new Runnable() {

        @Override
        public void run() {
            TransactionContext context = instance.newTransactionContext();
            context.beginTransaction();
            context.getQueue(name).offer(randomString());
            offerReserveLatch.countDown();
            sleepAtLeastSeconds(2);
            context.commitTransaction();
        }
    });
    assertOpenEventually(offerReserveLatch, 10);
    TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<Object> queue = context.getQueue(name);
    Object item = queue.poll(30, SECONDS);
    assertNotNull(item);
    context.commitTransaction();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionContext (com.hazelcast.transaction.TransactionContext)280 Test (org.junit.Test)209 QuickTest (com.hazelcast.test.annotation.QuickTest)189 ParallelTest (com.hazelcast.test.annotation.ParallelTest)184 HazelcastInstance (com.hazelcast.core.HazelcastInstance)58 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)48 TransactionalMap (com.hazelcast.core.TransactionalMap)33 TransactionException (com.hazelcast.transaction.TransactionException)22 IMap (com.hazelcast.core.IMap)21 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)19 Config (com.hazelcast.config.Config)17 NightlyTest (com.hazelcast.test.annotation.NightlyTest)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 XAException (javax.transaction.xa.XAException)14 TransactionalQueue (com.hazelcast.core.TransactionalQueue)13 TransactionalMultiMap (com.hazelcast.core.TransactionalMultiMap)9 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)9 ExecutionException (java.util.concurrent.ExecutionException)9 ISet (com.hazelcast.core.ISet)8 MultiMap (com.hazelcast.core.MultiMap)8