Search in sources :

Example 1 with VersionedObject

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);
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with VersionedObject

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();
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with VersionedObject

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());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with VersionedObject

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();
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with VersionedObject

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);
    }
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)78 Test (org.junit.Test)77 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)76 QuickTest (com.hazelcast.test.annotation.QuickTest)76 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 TransactionContext (com.hazelcast.transaction.TransactionContext)25 Config (com.hazelcast.config.Config)23 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)19 CountDownLatch (java.util.concurrent.CountDownLatch)16 QueueStoreConfig (com.hazelcast.config.QueueStoreConfig)10 LocalQueueStats (com.hazelcast.collection.LocalQueueStats)8 ArrayList (java.util.ArrayList)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 TestThread (com.hazelcast.test.TestThread)5 TransactionException (com.hazelcast.transaction.TransactionException)4 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)3 ListenerConfig (com.hazelcast.config.ListenerConfig)3 QueueConfig (com.hazelcast.config.QueueConfig)3 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 ExecutorService (java.util.concurrent.ExecutorService)3