Search in sources :

Example 6 with VersionedObject

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());
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) 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 7 with VersionedObject

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

Example 8 with VersionedObject

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);
}
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 9 with VersionedObject

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());
}
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 10 with VersionedObject

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()));
}
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)

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