Search in sources :

Example 56 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class TransactionQueueTest method testTxnQueueOuterTransaction.

@Test(expected = TransactionNotActiveException.class)
public void testTxnQueueOuterTransaction() {
    HazelcastInstance h1 = createHazelcastInstance();
    TransactionContext transactionContext = h1.newTransactionContext();
    transactionContext.beginTransaction();
    TransactionalQueue<VersionedObject<Object>> queue = transactionContext.getQueue("testTxnQueueOuterTransaction");
    queue.offer(new VersionedObject<>("item"));
    transactionContext.commitTransaction();
    queue.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 57 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class TransactionQueueTest method testOfferTake.

@Test
public void testOfferTake() throws InterruptedException {
    Config config = getConfig();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance owner = factory.newHazelcastInstance(config);
    HazelcastInstance backup = factory.newHazelcastInstance(config);
    String name = generateKeyOwnedBy(owner);
    for (int i = 0; i < 1000; i++) {
        TransactionContext ctx = owner.newTransactionContext();
        ctx.beginTransaction();
        TransactionalQueue<VersionedObject<Integer>> queue = ctx.getQueue(name);
        queue.offer(new VersionedObject<>(1, 1));
        queue.take();
        ctx.commitTransaction();
    }
    assertEquals(0, owner.getQueue(name).size());
    assertTransactionMapSize(owner, name, 0);
    assertTransactionMapSize(backup, name, 0);
}
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 58 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class TransactionQueueTest method transactionShouldBeRolledBack_whenInitiatorTerminatesBeforeCommit.

@Test
public void transactionShouldBeRolledBack_whenInitiatorTerminatesBeforeCommit() {
    Config config = getConfig();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    HazelcastInstance master = factory.newHazelcastInstance(config);
    HazelcastInstance instance = factory.newHazelcastInstance(config);
    warmUpPartitions(instance);
    String name = generateKeyOwnedBy(master);
    IQueue<VersionedObject<Integer>> queue = master.getQueue(name);
    queue.offer(new VersionedObject<>(1));
    waitAllForSafeState(master, instance);
    TransactionOptions options = new TransactionOptions().setTransactionType(TransactionOptions.TransactionType.TWO_PHASE);
    TransactionContext context = master.newTransactionContext(options);
    context.beginTransaction();
    TransactionalQueue<VersionedObject<Integer>> txQueue = context.getQueue(name);
    txQueue.poll();
    master.getLifecycleService().terminate();
    IQueue<Integer> queue2 = instance.getQueue(name);
    assertTrueEventually(() -> assertEquals(1, queue2.size()));
    assertTrueAllTheTime(() -> assertEquals(1, queue2.size()), 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) TransactionOptions(com.hazelcast.transaction.TransactionOptions) 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 59 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class TransactionQueueTest method testListener_withOffer.

@Test
public void testListener_withOffer() {
    HazelcastInstance hz = createHazelcastInstance();
    String name = randomName();
    IQueue<VersionedObject<String>> queue = hz.getQueue(name);
    EventCountingItemListener listener = new EventCountingItemListener();
    queue.addItemListener(listener, true);
    hz.executeTransaction((TransactionalTask<Object>) ctx -> {
        TransactionalQueue<VersionedObject<Object>> queue1 = ctx.getQueue(name);
        return queue1.offer(new VersionedObject<>("item"));
    });
    assertTrueEventually(() -> assertEquals(1, listener.adds.get()));
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Arrays(java.util.Arrays) QuickTest(com.hazelcast.test.annotation.QuickTest) TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) RunWith(org.junit.runner.RunWith) QueueService(com.hazelcast.collection.impl.queue.QueueService) Random(java.util.Random) TransactionException(com.hazelcast.transaction.TransactionException) ItemEvent(com.hazelcast.collection.ItemEvent) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Assert.fail(org.junit.Assert.fail) IQueue(com.hazelcast.collection.IQueue) Parameterized(org.junit.runners.Parameterized) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Assert.assertNotNull(org.junit.Assert.assertNotNull) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) TransactionOptions(com.hazelcast.transaction.TransactionOptions) Category(org.junit.experimental.categories.Category) ExecutionException(java.util.concurrent.ExecutionException) LockSupport(java.util.concurrent.locks.LockSupport) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) ItemListener(com.hazelcast.collection.ItemListener) Ignore(org.junit.Ignore) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) VersionedObjectComparator(com.hazelcast.collection.impl.queue.model.VersionedObjectComparator) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionalTask(com.hazelcast.transaction.TransactionalTask) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionalQueue(com.hazelcast.transaction.TransactionalQueue) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 60 with VersionedObject

use of com.hazelcast.collection.impl.queue.model.VersionedObject in project hazelcast by hazelcast.

the class QueueStatisticsTest method testPollOperationCount.

@Test
public void testPollOperationCount() throws Exception {
    IQueue<VersionedObject<String>> queue = newQueue();
    for (int i = 0; i < 30; i++) {
        queue.offer(new VersionedObject<>("item" + i, i));
    }
    for (int i = 0; i < 10; i++) {
        queue.remove();
    }
    for (int i = 0; i < 10; i++) {
        queue.take();
    }
    for (int i = 0; i < 10; i++) {
        queue.poll();
    }
    final LocalQueueStats stats = queue.getLocalQueueStats();
    assertTrueEventually(() -> assertEquals(30, stats.getPollOperationCount()));
}
Also used : LocalQueueStats(com.hazelcast.collection.LocalQueueStats) VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) 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