Search in sources :

Example 61 with TransactionalTaskContext

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

the class MapTransactionTest method testReplace.

@Test
public void testReplace() 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");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            assertNull(txMap.replace("1", "value"));
            txMap.put("1", "value2");
            assertEquals("value2", txMap.replace("1", "value3"));
            assertEquals("value3", txMap.get("1"));
            assertNull(map1.get("1"));
            assertNull(map2.get("2"));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value3", map1.get("1"));
    assertEquals("value3", map2.get("1"));
}
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 62 with TransactionalTaskContext

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

the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb.

@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb() {
    final String mapName = randomMapName();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(null);
    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);
            assertNull("value should be null", 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 63 with TransactionalTaskContext

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

the class MapTransactionTest method testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate.

@Test
public void testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName();
    final Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance node = factory.newHazelcastInstance(config);
    final IMap map = node.getMap(mapName);
    final Employee emp = new Employee("name", 77, true, 10D);
    map.put(1, emp);
    node.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
            txMap.put(2, emp);
            Collection<Employee> coll = txMap.values(new SqlPredicate("age = 77"));
            assertEquals(2, coll.size());
            return true;
        }
    });
    node.shutdown();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) SqlPredicate(com.hazelcast.query.SqlPredicate) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) Collection(java.util.Collection) 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