Search in sources :

Example 21 with TransactionalTaskContext

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

the class MapTransactionTest method testFailingMapStore.

@Test
public // TODO: @mm - Review following case...
void testFailingMapStore() throws TransactionException {
    final String map = "map";
    final String anotherMap = "anotherMap";
    Config config = getConfig();
    config.getMapConfig(map).setMapStoreConfig(new MapStoreConfig().setEnabled(true).setImplementation(new MapStoreAdapter() {

        public void store(Object key, Object value) {
            throw new ExpectedRuntimeException("Map store intentionally failed :) ");
        }
    }));
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    try {
        h1.executeTransaction(options, new TransactionalTask<Boolean>() {

            public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                assertNull(context.getMap(map).put("1", "value1"));
                assertNull(context.getMap(anotherMap).put("1", "value1"));
                return true;
            }
        });
        fail();
    } catch (ExpectedRuntimeException expected) {
    }
    assertNull(h2.getMap(map).get("1"));
    assertEquals("value1", h2.getMap(anotherMap).get("1"));
}
Also used : ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.core.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) 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)

Example 22 with TransactionalTaskContext

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

the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb.

@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
    final String mapName = randomMapName();
    final String valueFromDB = randomString();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(valueFromDB);
    Config config = new Config();
    MapStoreConfig storeConfig = new MapStoreConfig();
    storeConfig.setEnabled(true).setImplementation(mock);
    config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(mapName);
            Object value = map.getForUpdate(1);
            assertEquals(valueFromDB, value);
            verify(mock, times(1)).load(anyObject());
            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) MapStoreAdapter(com.hazelcast.core.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 23 with TransactionalTaskContext

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

the class MapTransactionTest method testRemove_whenSame.

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

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.put("3", "3");
            map2.put("4", "4");
            assertEquals(true, txMap.remove("1", "1"));
            assertEquals(false, txMap.remove("2", "1"));
            assertEquals("1", map1.get("1"));
            assertEquals(null, txMap.get("1"));
            assertEquals(true, txMap.remove("2", "2"));
            assertEquals(false, txMap.remove("5", "2"));
            assertEquals(2, txMap.size());
            return true;
        }
    });
    assertTrue(b);
    assertEquals(null, map1.get("1"));
    assertEquals(null, map2.get("1"));
    assertEquals(null, map1.get("2"));
    assertEquals(null, map2.get("2"));
    assertEquals("3", map1.get("3"));
    assertEquals("3", map2.get("3"));
    assertEquals("4", map1.get("4"));
    assertEquals("4", map2.get("4"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.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) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with TransactionalTaskContext

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

the class MapTransactionTest method testRemove_whenNullKey.

@Test(expected = NullPointerException.class)
public void testRemove_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.remove(null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 25 with TransactionalTaskContext

use of com.hazelcast.transaction.TransactionalTaskContext 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.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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