use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testTxnOwnerDies.
@Test
@Category(NightlyTest.class)
public void testTxnOwnerDies() throws TransactionException, InterruptedException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final HazelcastInstance h3 = factory.newHazelcastInstance(config);
final int size = 50;
final AtomicBoolean result = new AtomicBoolean(false);
Runnable runnable = new Runnable() {
public void run() {
try {
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
for (int i = 0; i < size; i++) {
txMap.put(i, i);
sleepSeconds(1);
}
return true;
}
});
result.set(b);
} catch (HazelcastInstanceNotActiveException ignored) {
} catch (TransactionException ignored) {
}
}
};
Thread thread = new Thread(runnable);
thread.start();
sleepSeconds(1);
h1.shutdown();
// wait till thread finishes.
thread.join(30 * 1000);
assertFalse(result.get());
final IMap map2 = h2.getMap("default");
for (int i = 0; i < size; i++) {
assertNull(map2.get(i));
}
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testSet_whenNullKey.
@Test(expected = NullPointerException.class)
public void testSet_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.set(null, "value");
return true;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testUpdate_thenGetForUpdate.
@Test
public void testUpdate_thenGetForUpdate() 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");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
try {
final TransactionalMap<String, String> txMap = context.getMap("default");
assertEquals("value0", txMap.put("var", "value1"));
assertEquals("value1", txMap.getForUpdate("var"));
} catch (Exception e) {
}
return true;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnReplace_whenReplaceFails_keyShouldRemainUnlocked.
@Test
public void testTxnReplace_whenReplaceFails_keyShouldRemainUnlocked() throws InterruptedException {
HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
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.replace(key, value);
return null;
}
});
assertFalse("Key remains locked!", map.isLocked(key));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnPutIfAbsent_whenPutFails_keyShouldRemainLocked_whenExplicitlyLocked.
@Test
public void testTxnPutIfAbsent_whenPutFails_keyShouldRemainLocked_whenExplicitlyLocked() throws InterruptedException {
final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
final 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.getForUpdate(key);
transactionalMap.putIfAbsent(key, "t");
assertTrue("Key remains unlocked!", map.isLocked(key));
return null;
}
});
}
Aggregations