use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testPeekMethod.
@Test
public void testPeekMethod() throws Exception {
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);
VersionedObject<String> response1 = q.peek(10, SECONDS);
assertNull(response1);
assertTrue(q.offer(new VersionedObject<>("ali")));
VersionedObject<String> response2 = q.peek();
assertEquals(new VersionedObject<>("ali"), response2);
context.commitTransaction();
} catch (TransactionException e) {
context.rollbackTransaction();
throw e;
}
assertEquals(1, getQueue(instances, name).size());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testPeekWithTimeout.
@Test
public void testPeekWithTimeout() {
String name = randomString();
VersionedObject<String> item = new VersionedObject<>(randomString());
HazelcastInstance instance = createHazelcastInstance();
IQueue<VersionedObject<String>> queue = instance.getQueue(name);
spawn(() -> {
sleepSeconds(1);
queue.offer(item);
});
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
try {
TransactionalQueue<VersionedObject<String>> txnQueue = context.getQueue(name);
VersionedObject<String> peeked = txnQueue.peek(10, SECONDS);
assertEquals(item, peeked);
context.commitTransaction();
} catch (Exception e) {
context.rollbackTransaction();
}
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method issue_6259_backupNotRollingBackCorrectly.
// https://github.com/hazelcast/hazelcast/issues/6259
@Test
public void issue_6259_backupNotRollingBackCorrectly() {
HazelcastInstance[] cluster = createHazelcastInstanceFactory(2).newInstances(getConfig());
HazelcastInstance local = cluster[0];
HazelcastInstance remote = cluster[1];
String queueName = generateKeyOwnedBy(remote);
// first we add an item
local.executeTransaction(context -> {
TransactionalQueue<VersionedObject<String>> queue = context.getQueue(queueName);
queue.offer(new VersionedObject<>("item"));
return null;
});
// we remove the item and then do a rollback. This causes the local (backup) to become out
// of sync with the remote (primary)
TransactionContext firstCtxt = local.newTransactionContext();
firstCtxt.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue = firstCtxt.getQueue(queueName);
queue.poll();
firstCtxt.rollbackTransaction();
// we kill the remote. Now the local (which was the backup) is going to become primary
remote.shutdown();
// if we take the item, we should get an error
TransactionContext secondCtxt = local.newTransactionContext();
secondCtxt.beginTransaction();
queue = secondCtxt.getQueue(queueName);
VersionedObject<String> found = queue.poll();
assertEquals(new VersionedObject<>("item"), found);
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testOrder_WhenMultipleConcurrentTransactionRollback.
@Test
public void testOrder_WhenMultipleConcurrentTransactionRollback() throws InterruptedException {
HazelcastInstance instance = createHazelcastInstance();
String name = randomString();
IQueue<VersionedObject<Integer>> queue = instance.getQueue(name);
queue.offer(new VersionedObject<>(1, 1));
queue.offer(new VersionedObject<>(2, 2));
queue.offer(new VersionedObject<>(3, 3));
TransactionContext firstContext = instance.newTransactionContext();
firstContext.beginTransaction();
firstContext.getQueue(name).poll();
CountDownLatch latch = new CountDownLatch(1);
Thread thread = new Thread(() -> {
TransactionContext secondContext = instance.newTransactionContext();
secondContext.beginTransaction();
secondContext.getQueue(name).poll();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
secondContext.rollbackTransaction();
});
thread.start();
firstContext.rollbackTransaction();
latch.countDown();
thread.join();
assertEquals(new VersionedObject<>(1, 1), queue.poll());
assertEquals(new VersionedObject<>(2, 2), queue.poll());
assertEquals(new VersionedObject<>(3, 3), queue.poll());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testListener_withPoll.
@Test
public void testListener_withPoll() {
HazelcastInstance hz = createHazelcastInstance();
String name = randomName();
IQueue<VersionedObject<String>> queue = hz.getQueue(name);
queue.offer(new VersionedObject<>("item"));
EventCountingItemListener listener = new EventCountingItemListener();
queue.addItemListener(listener, true);
VersionedObject<String> item = hz.executeTransaction(ctx -> {
TransactionalQueue<VersionedObject<String>> queue1 = ctx.getQueue(name);
return queue1.poll();
});
assertEquals(new VersionedObject<>("item"), item);
assertTrueEventually(() -> assertEquals(1, listener.removes.get()));
}
Aggregations