use of com.hazelcast.transaction.TransactionException 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));
}
use of com.hazelcast.transaction.TransactionException 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));
}
use of com.hazelcast.transaction.TransactionException 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;
}
});
}
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;
}
});
}
use of com.hazelcast.transaction.TransactionException 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"));
}
Aggregations