use of com.hazelcast.core.TransactionalMap 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.core.TransactionalMap 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.core.TransactionalMap 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"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate.
@Test
public void testGetForUpdate() 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");
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicBoolean pass = new AtomicBoolean(true);
map.put("var", "value0");
Runnable updater = new Runnable() {
public void run() {
try {
latch1.await(100, TimeUnit.SECONDS);
pass.set(map.tryPut("var", "value1", 0, TimeUnit.SECONDS) == false);
latch2.countDown();
} catch (Exception e) {
}
}
};
new Thread(updater).start();
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
try {
final TransactionalMap<String, Integer> txMap = context.getMap("default");
assertEquals("value0", txMap.getForUpdate("var"));
latch1.countDown();
latch2.await(100, TimeUnit.SECONDS);
} catch (Exception e) {
}
return true;
}
});
assertTrue(b);
assertTrue(pass.get());
assertTrue(map.tryPut("var", "value2", 0, TimeUnit.SECONDS));
}
use of com.hazelcast.core.TransactionalMap 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"));
}
Aggregations