Search in sources :

Example 96 with TransactionException

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

the class MapTransactionStressTest method testTransactionAtomicity_whenMapGetIsUsed_withTransaction.

@Test
public void testTransactionAtomicity_whenMapGetIsUsed_withTransaction() throws InterruptedException {
    final HazelcastInstance hz = createHazelcastInstance(createConfigWithDummyTxService());
    final String name = HazelcastTestSupport.generateRandomString(5);
    Thread producerThread = startProducerThread(hz, name);
    try {
        IQueue<String> q = hz.getQueue(name);
        for (int i = 0; i < 1000; i++) {
            String id = q.poll();
            if (id != null) {
                TransactionContext tx = hz.newTransactionContext();
                try {
                    tx.beginTransaction();
                    TransactionalMap<String, Object> map = tx.getMap(name);
                    Object value = map.get(id);
                    Assert.assertNotNull(value);
                    map.delete(id);
                    tx.commitTransaction();
                } catch (TransactionException e) {
                    tx.rollbackTransaction();
                    e.printStackTrace();
                }
            } else {
                LockSupport.parkNanos(100);
            }
        }
    } finally {
        stopProducerThread(producerThread);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) DistributedObject(com.hazelcast.core.DistributedObject) TransactionalObject(com.hazelcast.transaction.TransactionalObject) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 97 with TransactionException

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

the class MapTransactionTest method testTxnPutIfAbsent.

@Test
public void testTxnPutIfAbsent() 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");
            txMap.putIfAbsent("1", "value");
            assertEquals("value", txMap.putIfAbsent("1", "value2"));
            assertEquals("value", txMap.get("1"));
            assertNull(map1.get("1"));
            assertNull(map2.get("2"));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value", map1.get("1"));
    assertEquals("value", 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 98 with TransactionException

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

the class MapTransactionTest method testValuesWithPredicate_removingExistentEntry.

@Test
public void testValuesWithPredicate_removingExistentEntry() throws TransactionException {
    final int nodeCount = 1;
    final String mapName = randomMapName("_testValuesWithPredicate_removingExistentEntry_");
    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<Object, Object> txMap = context.getMap(mapName);
            txMap.remove(1);
            Collection<Object> coll = txMap.values(Predicates.sql("age > 70 "));
            assertEquals(0, coll.size());
            return true;
        }
    });
    node.shutdown();
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleTestObjects.Employee) TransactionException(com.hazelcast.transaction.TransactionException) Collection(java.util.Collection) 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 99 with TransactionException

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

the class MapTransactionTest method testGet_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb.

@Test
public void testGet_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb() {
    final String mapName = randomMapName();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(null);
    Config config = getConfig();
    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.get(1);
            assertNull("value should be null", value);
            verify(mock, times(1)).load(anyObject());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 100 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.transaction.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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) 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