use of com.hazelcast.transaction.TransactionalTaskContext 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.transaction.TransactionalTaskContext 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.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testRemove.
@Test
public void testRemove() 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");
map2.put("1", "1");
map2.put("2", "2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.put("3", "3");
map2.put("4", "4");
assertEquals("1", txMap.remove("1"));
assertEquals("2", map1.remove("2"));
assertEquals("1", map1.get("1"));
assertEquals(null, txMap.get("1"));
assertEquals(null, txMap.remove("2"));
assertEquals(2, txMap.size());
return true;
}
});
assertTrue(b);
assertEquals(null, map1.get("1"));
assertEquals(null, map2.get("1"));
assertEquals(null, map1.get("2"));
assertEquals(null, map2.get("2"));
assertEquals("3", map1.get("3"));
assertEquals("3", map2.get("3"));
assertEquals("4", map1.get("4"));
assertEquals("4", map2.get("4"));
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class ClientTxnMapTest method testGetForUpdate.
@Test
public void testGetForUpdate() throws TransactionException {
final String mapName = randomString();
final String key = "key";
final int initialValue = 111;
final int value = 888;
final CountDownLatch getKeyForUpdateLatch = new CountDownLatch(1);
final CountDownLatch afterTryPutResult = new CountDownLatch(1);
final IMap<String, Integer> map = client.getMap(mapName);
map.put(key, initialValue);
final AtomicBoolean tryPutResult = new AtomicBoolean(true);
Runnable incrementor = new Runnable() {
public void run() {
try {
getKeyForUpdateLatch.await(30, TimeUnit.SECONDS);
boolean result = map.tryPut(key, value, 0, TimeUnit.SECONDS);
tryPutResult.set(result);
afterTryPutResult.countDown();
} catch (Exception e) {
}
}
};
new Thread(incrementor).start();
client.executeTransaction(new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
try {
final TransactionalMap<String, Integer> txMap = context.getMap(mapName);
txMap.getForUpdate(key);
getKeyForUpdateLatch.countDown();
afterTryPutResult.await(30, TimeUnit.SECONDS);
} catch (Exception e) {
}
return true;
}
});
assertFalse(tryPutResult.get());
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testValues_withPagingPredicate.
@Test(expected = IllegalArgumentException.class)
public void testValues_withPagingPredicate() throws TransactionException {
final int nodeCount = 1;
final String mapName = randomMapName("testValuesWithPagingPredicate");
final Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
final HazelcastInstance node = factory.newHazelcastInstance(config);
final IMap<Integer, Employee> 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<Integer, Employee> txMap = context.getMap(mapName);
PagingPredicate<Integer, Employee> predicate = new PagingPredicate<Integer, Employee>(5);
txMap.values(predicate);
return true;
}
});
}
Aggregations