Search in sources :

Example 21 with TransactionException

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

the class MapTransactionTest method testReplace_whenNullNewValue.

@Test(expected = NullPointerException.class)
public void testReplace_whenNullNewValue() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.replace("key", "oldvalue", null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 22 with TransactionException

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

the class MapTransactionTest method tesPutIfAbsent_whenNullKey.

// ========================= putIfAbsent =====================
@Test(expected = NullPointerException.class)
public void tesPutIfAbsent_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.putIfAbsent(null, "value");
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 23 with TransactionException

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

the class MapTransactionTest method testTxnReplace2.

@Test
public void testTxnReplace2() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map2 = h2.getMap("default");
    map2.put("1", "value2");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            assertEquals("value2", txMap.replace("1", "value3"));
            assertEquals("value3", txMap.get("1"));
            assertNull(map2.get("2"));
            return true;
        }
    });
    assertTrue(b);
    IMap map1 = h1.getMap("default");
    assertEquals("value3", map1.get("1"));
    assertEquals("value3", map2.get("1"));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 24 with TransactionException

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

the class TransactionsWithWriteBehind_whenNoCoalescingQueueIsFullTest method rollback_successful_when_prepare_step_throws_exception_when_two_phase.

@Test
public void rollback_successful_when_prepare_step_throws_exception_when_two_phase() {
    String mapName = "map";
    String queueName = "queue";
    long maxWbqCapacity = 100;
    Config config = getConfig(mapName, maxWbqCapacity);
    HazelcastInstance node = createHazelcastInstance(config);
    TransactionContext context = newTransactionContext(node, TWO_PHASE);
    try {
        context.beginTransaction();
        TransactionalQueue queue = context.getQueue(queueName);
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        TransactionalMap map = context.getMap(mapName);
        for (int i = 0; i < 1000; i++) {
            map.put("item-" + i, "value");
        }
        context.commitTransaction();
    } catch (TransactionException e) {
        context.rollbackTransaction();
    }
    assertEquals(0, node.getQueue(queueName).size());
    assertEquals(0, node.getMap(mapName).size());
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) TransactionException(com.hazelcast.transaction.TransactionException) 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 25 with TransactionException

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

the class TransactionsWithWriteBehind_whenNoCoalescingQueueIsFullTest method name.

@Test
public void name() {
    String mapName = "map";
    Config config = getConfig(mapName, 50);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance node1 = factory.newHazelcastInstance(config);
    HazelcastInstance node2 = factory.newHazelcastInstance(config);
    for (int ii = 0; ii < 100; ii++) {
        TransactionContext context = newTransactionContext(node1, TWO_PHASE);
        context.beginTransaction();
        TransactionalMap map = context.getMap("map");
        for (int i = 0; i < 51; i++) {
            map.put("item-" + i, "value");
        }
        try {
            context.commitTransaction();
        } catch (TransactionException e) {
            context.rollbackTransaction();
        }
    }
    HazelcastInstance node3 = factory.newHazelcastInstance(config);
    assertTrueEventually(() -> {
        Collection<HazelcastInstance> instances = factory.getAllHazelcastInstances();
        for (HazelcastInstance node : instances) {
            assertWriteBehindQueuesEmpty(mapName, Collections.singletonList(node));
            assertEquals(0, getNodeWideUsedCapacity(node));
            assertEquals(0, getTotalNumOfTxnReservedCapacity(mapName, node));
        }
    }, 30);
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) Config(com.hazelcast.config.Config) 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)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)106 Test (org.junit.Test)82 QuickTest (com.hazelcast.test.annotation.QuickTest)76 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)72 HazelcastInstance (com.hazelcast.core.HazelcastInstance)70 TransactionalMap (com.hazelcast.transaction.TransactionalMap)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)57 NightlyTest (com.hazelcast.test.annotation.NightlyTest)48 Config (com.hazelcast.config.Config)38 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)34 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TransactionContext (com.hazelcast.transaction.TransactionContext)22 IMap (com.hazelcast.map.IMap)16 TransactionOptions (com.hazelcast.transaction.TransactionOptions)8 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)6 Data (com.hazelcast.internal.serialization.Data)5