Search in sources :

Example 66 with TransactionException

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

the class MapTransactionLockingTest method testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlocked.

@Test
public void testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlocked() throws InterruptedException {
    HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
    IMap<String, Object> map = hazelcastInstance.getMap(mapName);
    map.put(key, value);
    hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
            TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
            transactionalMap.putIfAbsent(key, "t");
            return null;
        }
    });
    assertFalse("Key remains locked!", map.isLocked(key));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 67 with TransactionException

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

the class MapTransactionLockingTest method testTxnRemoveIfSame_whenRemoveFails_keyShouldRemainUnlocked.

@Test
public void testTxnRemoveIfSame_whenRemoveFails_keyShouldRemainUnlocked() throws InterruptedException {
    HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
    IMap<String, Object> map = hazelcastInstance.getMap(mapName);
    map.put(key, value);
    hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
            TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
            transactionalMap.remove(key, value + "other");
            return null;
        }
    });
    assertFalse("Key remains locked!", map.isLocked(key));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 68 with TransactionException

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

the class MapTransactionLockingTest method testTxnReplace_whenReplaceIfSameFails_keyShouldRemainLocked_whenExplicitlyLocked.

@Test
public void testTxnReplace_whenReplaceIfSameFails_keyShouldRemainLocked_whenExplicitlyLocked() throws InterruptedException {
    final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
    final IMap<String, Object> map = hazelcastInstance.getMap(mapName);
    hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
            TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
            transactionalMap.getForUpdate(key);
            boolean replace = transactionalMap.replace(key, value + "other", value);
            assertFalse(replace);
            assertTrue("Key remains unlocked!", map.isLocked(key));
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 69 with TransactionException

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

the class MapTransactionTest method transactionalMap_shouldNotHaveNegativeSize.

@Test
public void transactionalMap_shouldNotHaveNegativeSize() throws Exception {
    Config config = getConfig();
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            String mapName = randomString();
            String key = randomString();
            String val = randomString();
            TransactionalMap<String, String> map = context.getMap(mapName);
            map.put(key, val);
            map.remove(key);
            assertEquals(0, map.size());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) 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) Mockito.anyObject(org.mockito.Mockito.anyObject) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 70 with TransactionException

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

the class MapTransactionTest method testGetForUpdate_whenTimeout.

@Test
public void testGetForUpdate_whenTimeout() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap<String, String> map = h2.getMap("default");
    map.put("var", "value0");
    map.lock("var");
    TransactionOptions opts = new TransactionOptions();
    opts.setTimeout(1, TimeUnit.SECONDS);
    try {
        boolean b = h1.executeTransaction(opts, new TransactionalTask<Boolean>() {

            public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                final TransactionalMap<String, String> txMap = context.getMap("default");
                txMap.getForUpdate("var");
                fail();
                return true;
            }
        });
    } catch (TransactionException e) {
    }
    assertTrue(map.isLocked("var"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)91 Test (org.junit.Test)74 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 QuickTest (com.hazelcast.test.annotation.QuickTest)66 ParallelTest (com.hazelcast.test.annotation.ParallelTest)62 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)62 TransactionalMap (com.hazelcast.core.TransactionalMap)60 NightlyTest (com.hazelcast.test.annotation.NightlyTest)51 Config (com.hazelcast.config.Config)39 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)39 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)35 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.core.IMap)18 TransactionContext (com.hazelcast.transaction.TransactionContext)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionOptions (com.hazelcast.transaction.TransactionOptions)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5