use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testListener_withEmptyPoll.
@Test
public void testListener_withEmptyPoll() {
HazelcastInstance hz = createHazelcastInstance();
String name = randomName();
IQueue<VersionedObject<String>> queue = hz.getQueue(name);
EventCountingItemListener listener = new EventCountingItemListener();
queue.addItemListener(listener, true);
VersionedObject<String> item = hz.executeTransaction(ctx -> {
TransactionalQueue<VersionedObject<String>> queue1 = ctx.getQueue(name);
return queue1.poll();
});
assertNull(item);
assertTrueAllTheTime(() -> assertEquals(0, listener.removes.get()), 5);
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testPollWithTimeout_WithAnotherThreadOffering.
@Test
public void testPollWithTimeout_WithAnotherThreadOffering() throws InterruptedException {
HazelcastInstance instance = createHazelcastInstance();
String name = randomString();
CountDownLatch offerReserveLatch = new CountDownLatch(1);
spawn(() -> {
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
context.getQueue(name).offer(new VersionedObject<>(randomString()));
offerReserveLatch.countDown();
sleepAtLeastSeconds(2);
context.commitTransaction();
});
assertOpenEventually(offerReserveLatch, 10);
TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue = context.getQueue(name);
VersionedObject<String> item = queue.poll(30, SECONDS);
assertNotNull(item);
context.commitTransaction();
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testRollbackQueue.
@Test
public void testRollbackQueue() {
HazelcastInstance h1 = createHazelcastInstance();
TransactionContext transactionContext = h1.newTransactionContext();
transactionContext.beginTransaction();
TransactionalQueue<VersionedObject<String>> queue = transactionContext.getQueue("testq");
queue.offer(new VersionedObject<>("offered-val"));
transactionContext.rollbackTransaction();
assertNull(h1.getQueue("testq").poll());
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testPromotionFromBackup.
@Test
public void testPromotionFromBackup() {
Config config = getConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance owner = factory.newHazelcastInstance(config);
HazelcastInstance backup = factory.newHazelcastInstance(config);
String name = generateKeyOwnedBy(owner);
TransactionContext context = backup.newTransactionContext();
context.beginTransaction();
TransactionalQueue<VersionedObject<Integer>> queue = context.getQueue(name);
queue.offer(new VersionedObject<>(1, 1));
owner.getLifecycleService().terminate();
queue.offer(new VersionedObject<>(2, 2));
context.commitTransaction();
}
use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.
the class TransactionQueueTest method testIssue859And863.
// https://github.com/hazelcast/hazelcast/issues/3796
private void testIssue859And863(HazelcastInstance instance1, HazelcastInstance instance2, String inQueueName, String outQueueName) {
int numberOfMessages = 3000;
AtomicInteger counter = new AtomicInteger();
IQueue<VersionedObject<Integer>> inQueue = instance1.getQueue(inQueueName);
for (int i = 0; i < numberOfMessages; i++) {
if (!inQueue.offer(new VersionedObject<>(i, i))) {
throw new RuntimeException("initial put did not work");
}
}
Thread[] instance1Threads = createThreads(instance1, 3, inQueueName, outQueueName, counter);
Thread[] instance2Threads = createThreads(instance2, 3, inQueueName, outQueueName, counter);
try {
startThreads(instance1Threads);
startThreads(instance2Threads);
while (counter.get() < numberOfMessages / 2) {
LockSupport.parkNanos(1000);
}
instance2.getLifecycleService().shutdown();
interruptThreads(instance2Threads);
assertJoinable(instance2Threads);
// When a node goes down, backup of the transaction commits all prepared stated transactions
// Since it relies on 'memberRemoved' event, it is async. That's why we should assert eventually
assertTrueEventually(() -> {
assertEquals(numberOfMessages, instance1.getQueue(outQueueName).size());
assertTrue(instance1.getQueue(inQueueName).isEmpty());
});
} finally {
interruptThreads(instance1Threads);
interruptThreads(instance2Threads);
assertJoinable(instance1Threads);
}
}
Aggregations