use of com.hazelcast.transaction.TransactionalMap 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.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));
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, String> 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.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.transaction.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.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace.
@Test
public void testReplace() 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");
assertNull(txMap.replace("1", "value"));
txMap.put("1", "value2");
assertEquals("value2", txMap.replace("1", "value3"));
assertEquals("value3", txMap.get("1"));
assertNull(map1.get("1"));
assertNull(map2.get("2"));
return true;
}
});
assertTrue(b);
assertEquals("value3", map1.get("1"));
assertEquals("value3", map2.get("1"));
}
Aggregations