use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testSingleQueueAtomicity.
@Test
public void testSingleQueueAtomicity() throws ExecutionException, InterruptedException {
String name = randomString();
int itemCount = 200;
HazelcastInstance instance = createHazelcastInstance();
Future<Integer> f = spawn(() -> {
IQueue<VersionedObject<String>> queue = instance.getQueue(name);
queue.take();
return queue.size();
});
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue = context.getQueue(name);
for (int i = 0; i < itemCount; i++) {
queue.offer(new VersionedObject<>("item-" + i, i));
}
context.commitTransaction();
int size = f.get();
assertEquals(itemCount - 1, size);
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testTransactionalOfferPoll2.
@Test
public void testTransactionalOfferPoll2() {
int insCount = 4;
String name0 = "defQueue0";
String name1 = "defQueue1";
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
HazelcastInstance[] instances = factory.newInstances(getConfig());
CountDownLatch latch = new CountDownLatch(1);
new Thread(() -> {
try {
latch.await(5, SECONDS);
sleepMillis(3000);
getQueue(instances, name0).offer(new VersionedObject<>("item0"));
} catch (InterruptedException | HazelcastInstanceNotActiveException ignored) {
}
}).start();
TransactionContext context = instances[0].newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<String>> q0 = context.getQueue(name0);
TransactionalQueue<VersionedObject<String>> q1 = context.getQueue(name1);
VersionedObject<String> s = null;
latch.countDown();
try {
s = q0.poll(10, SECONDS);
} catch (InterruptedException e) {
fail(e.getMessage());
}
assertEquals(new VersionedObject<>("item0"), s);
q1.offer(s);
context.commitTransaction();
assertEquals(0, getQueue(instances, name0).size());
assertEquals(new VersionedObject<>("item0"), getQueue(instances, name1).poll());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testTransactionalOfferPoll1.
@Test
public void testTransactionalOfferPoll1() {
int insCount = 4;
String name = "defQueue";
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
HazelcastInstance[] instances = factory.newInstances(getConfig());
TransactionContext context = instances[0].newTransactionContext();
context.beginTransaction();
try {
TransactionalQueue<VersionedObject<String>> q = context.getQueue(name);
assertTrue(q.offer(new VersionedObject<>("ali")));
VersionedObject<String> s = q.poll();
assertEquals(new VersionedObject<>("ali"), s);
context.commitTransaction();
} catch (TransactionException e) {
context.rollbackTransaction();
throw e;
}
assertEquals(0, getQueue(instances, name).size());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method nestedTransactionTest.
@Test(expected = IllegalStateException.class)
public void nestedTransactionTest() {
HazelcastInstance instance = createHazelcastInstance();
TransactionContext outerTnx = instance.newTransactionContext();
outerTnx.beginTransaction();
String name = randomString();
VersionedObject<String> item = new VersionedObject<>(randomString());
outerTnx.getQueue(name).offer(item);
try {
TransactionContext innerTnx = instance.newTransactionContext();
innerTnx.beginTransaction();
innerTnx.getQueue(name).offer(item);
innerTnx.commitTransaction();
} finally {
outerTnx.rollbackTransaction();
}
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testTransactionalOfferAndPollWithTimeout.
@Test
public void testTransactionalOfferAndPollWithTimeout() throws InterruptedException {
HazelcastInstance instance = createHazelcastInstance();
VersionedObject<String> item = new VersionedObject<>("offered");
String queueName = "testTransactionalOfferAndPollWithTimeout";
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<String>> txnQueue = context.getQueue(queueName);
assertTrue(txnQueue.offer(item));
assertEquals(1, txnQueue.size());
assertEquals(item, txnQueue.poll(5, SECONDS));
context.commitTransaction();
}
Aggregations