use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testTxnBackupDies.
@Test
public void testTxnBackupDies() throws TransactionException, InterruptedException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final String mapName = randomMapName();
final IMap map = h1.getMap(mapName);
final int size = 100;
final CountDownLatch transactionCompletedLatch = new CountDownLatch(size + 1);
final CountDownLatch thresholdReached = new CountDownLatch(1);
Runnable runnable = new Runnable() {
public void run() {
try {
final int oneThird = size / 3;
final int threshold = new Random().nextInt(oneThird) + oneThird;
h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap(mapName);
for (int i = 0; i < size; i++) {
if (i == threshold) {
thresholdReached.countDown();
}
txMap.put(i, i);
sleepMillis(100);
transactionCompletedLatch.countDown();
}
return true;
}
});
fail();
} catch (Exception ignored) {
}
transactionCompletedLatch.countDown();
}
};
new Thread(runnable).start();
assertOpenEventually(thresholdReached);
terminateInstance(h2);
assertOpenEventually(transactionCompletedLatch);
for (int i = 0; i < size; i++) {
assertNull(map.get(i));
}
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testContainsKey.
@Test
public void testContainsKey() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final IMap<String, String> map = h1.getMap("default");
map.put("1", "1");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.delete("1");
assertEquals(false, txMap.containsKey("1"));
assertEquals(true, map.containsKey("1"));
return true;
}
});
assertTrue(b);
}
use of com.hazelcast.transaction.TransactionalTaskContext 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(new SqlPredicate("age > 70 "));
assertEquals(0, coll.size());
return true;
}
});
node.shutdown();
}
use of com.hazelcast.transaction.TransactionalTaskContext 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.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_ThenUpdate.
@Test
public void testGetForUpdate_ThenUpdate() 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.getForUpdate("var"));
assertEquals("value0", txMap.put("var", "value1"));
assertEquals("value1", txMap.getForUpdate("var"));
assertEquals("value1", txMap.get("var"));
} catch (Exception e) {
}
return true;
}
});
}
Aggregations