use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionListTest method testOrder_WhenMultipleConcurrentTransactionRollback.
@Test
public void testOrder_WhenMultipleConcurrentTransactionRollback() throws InterruptedException {
final HazelcastInstance instance = createHazelcastInstance();
final String name = randomString();
IList<Integer> list = instance.getList(name);
list.add(1);
list.add(2);
list.add(3);
TransactionContext firstContext = instance.newTransactionContext();
firstContext.beginTransaction();
firstContext.getList(name).remove(1);
final CountDownLatch latch = new CountDownLatch(1);
Thread thread = new Thread() {
@Override
public void run() {
TransactionContext secondContext = instance.newTransactionContext();
secondContext.beginTransaction();
secondContext.getList(name).remove(2);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
secondContext.rollbackTransaction();
}
};
thread.start();
firstContext.rollbackTransaction();
latch.countDown();
thread.join();
assertEquals(1, list.get(0).intValue());
assertEquals(2, list.get(1).intValue());
assertEquals(3, list.get(2).intValue());
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionListTest method testAdd.
@Test
public void testAdd() {
HazelcastInstance instance = createHazelcastInstance();
String name = randomString();
String item = randomString();
IList<Object> list = instance.getList(name);
TransactionContext context = instance.newTransactionContext();
try {
context.beginTransaction();
TransactionalList<Object> txnList = context.getList(name);
assertTrue(txnList.add(item));
context.commitTransaction();
} catch (Exception e) {
fail(e.getMessage());
context.rollbackTransaction();
}
assertEquals(1, list.size());
assertEquals(item, list.get(0));
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionListTest method testMigrationSerializationNotFails_whenTransactionsAreUsed.
@Test
public void testMigrationSerializationNotFails_whenTransactionsAreUsed() throws Exception {
Config config = new Config();
config.setProperty("hazelcast.partition.count", "2");
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
String listName = randomString();
TransactionContext tr = instance1.newTransactionContext();
tr.beginTransaction();
TransactionalList<Object> list = tr.getList(listName);
for (int i = 0; i < 10; i++) {
list.add(i);
}
tr.commitTransaction();
HazelcastInstance instance2 = factory.newHazelcastInstance(config);
Member owner = instance1.getPartitionService().getPartition(listName).getOwner();
HazelcastInstance aliveInstance;
if (instance1.getCluster().getLocalMember().equals(owner)) {
instance1.shutdown();
aliveInstance = instance2;
} else {
instance2.shutdown();
aliveInstance = instance1;
}
IList<Object> l = aliveInstance.getList(listName);
for (int i = 0; i < 10; i++) {
assertEquals(i, l.get(i));
}
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionQueueTest method testPeekWithTimeout.
@Test
public void testPeekWithTimeout() {
final String name = randomString();
final String item = randomString();
HazelcastInstance instance = createHazelcastInstance();
final IQueue<String> queue = instance.getQueue(name);
spawn(new Runnable() {
@Override
public void run() {
sleepSeconds(1);
queue.offer(item);
}
});
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
try {
TransactionalQueue<String> txnQueue = context.getQueue(name);
String peeked = txnQueue.peek(10, SECONDS);
assertEquals(item, peeked);
context.commitTransaction();
} catch (Exception e) {
context.rollbackTransaction();
}
}
use of com.hazelcast.transaction.TransactionContext in project hazelcast by hazelcast.
the class TransactionQueueTest method testPollWithTimeout_WithAnotherThreadOffering.
@Test
public void testPollWithTimeout_WithAnotherThreadOffering() throws InterruptedException {
final HazelcastInstance instance = createHazelcastInstance();
final String name = randomString();
final CountDownLatch offerReserveLatch = new CountDownLatch(1);
spawn(new Runnable() {
@Override
public void run() {
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
context.getQueue(name).offer(randomString());
offerReserveLatch.countDown();
sleepAtLeastSeconds(2);
context.commitTransaction();
}
});
assertOpenEventually(offerReserveLatch, 10);
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
TransactionalQueue<Object> queue = context.getQueue(name);
Object item = queue.poll(30, SECONDS);
assertNotNull(item);
context.commitTransaction();
}
Aggregations