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);
}
}
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"));
}
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();
}
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;
}
});
}
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;
}
});
}
Aggregations