Search in sources :

Example 51 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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 52 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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 53 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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 54 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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 55 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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

TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)63 Test (org.junit.Test)63 TransactionException (com.hazelcast.transaction.TransactionException)62 HazelcastInstance (com.hazelcast.core.HazelcastInstance)61 TransactionalMap (com.hazelcast.core.TransactionalMap)59 ParallelTest (com.hazelcast.test.annotation.ParallelTest)58 QuickTest (com.hazelcast.test.annotation.QuickTest)58 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 Config (com.hazelcast.config.Config)36 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)32 IMap (com.hazelcast.core.IMap)18 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 ExecutionException (java.util.concurrent.ExecutionException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5 Employee (com.hazelcast.query.SampleObjects.Employee)5