Search in sources :

Example 1 with TransactionalTask

use of com.hazelcast.transaction.TransactionalTask in project hazelcast by hazelcast.

the class SetAbstractTest method testNameBasedAffinity.

@Test
public void testNameBasedAffinity() {
    // creates more instances to increase a chance 'foo' will not be owned by
    // the same member as 'foo@1'
    newInstances(config);
    newInstances(config);
    int numberOfSets = 100;
    ISet[] localSets = new ISet[numberOfSets];
    ISet[] targetSets = new ISet[numberOfSets];
    for (int i = 0; i < numberOfSets; i++) {
        String name = randomName() + "@" + i;
        localSets[i] = local.getSet(name);
        targetSets[i] = target.getSet(name);
    }
    for (final ISet set : localSets) {
        TransactionalTask task = new TransactionalTask() {

            @Override
            public Object execute(TransactionalTaskContext context) throws TransactionException {
                TransactionalSet<String> txSet = context.getSet(set.getName());
                txSet.add("Hello");
                return null;
            }
        };
        local.executeTransaction(task);
    }
    for (ISet set : localSets) {
        assertEquals(1, set.size());
    }
}
Also used : TransactionalTask(com.hazelcast.transaction.TransactionalTask) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ISet(com.hazelcast.collection.ISet) Test(org.junit.Test)

Example 2 with TransactionalTask

use of com.hazelcast.transaction.TransactionalTask in project hazelcast by hazelcast.

the class HazelcastOSGiInstanceTest method executeTransactionCalledSuccessfullyOverOSGiInstance.

@Test
public void executeTransactionCalledSuccessfullyOverOSGiInstance() {
    Object result = new Object();
    TransactionalTask mockTransactionalTask = mock(TransactionalTask.class);
    HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
    HazelcastOSGiInstance hazelcastOSGiInstance = createHazelcastOSGiInstance(mockHazelcastInstance);
    when(mockHazelcastInstance.executeTransaction(mockTransactionalTask)).thenReturn(result);
    assertEquals(result, hazelcastOSGiInstance.executeTransaction(mockTransactionalTask));
    verify(mockHazelcastInstance).executeTransaction(mockTransactionalTask);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastOSGiTestUtil.createHazelcastOSGiInstance(com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance) TransactionalTask(com.hazelcast.transaction.TransactionalTask) DistributedObject(com.hazelcast.core.DistributedObject) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with TransactionalTask

use of com.hazelcast.transaction.TransactionalTask in project hazelcast by hazelcast.

the class HazelcastOSGiInstanceTest method executeTransactionWithOptionsCalledSuccessfullyOverOSGiInstance.

@Test
public void executeTransactionWithOptionsCalledSuccessfullyOverOSGiInstance() {
    Object result = new Object();
    TransactionOptions transactionOptions = new TransactionOptions();
    TransactionalTask mockTransactionalTask = mock(TransactionalTask.class);
    HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
    HazelcastOSGiInstance hazelcastOSGiInstance = createHazelcastOSGiInstance(mockHazelcastInstance);
    when(mockHazelcastInstance.executeTransaction(transactionOptions, mockTransactionalTask)).thenReturn(result);
    assertEquals(result, hazelcastOSGiInstance.executeTransaction(transactionOptions, mockTransactionalTask));
    verify(mockHazelcastInstance).executeTransaction(transactionOptions, mockTransactionalTask);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastOSGiTestUtil.createHazelcastOSGiInstance(com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance) TransactionOptions(com.hazelcast.transaction.TransactionOptions) TransactionalTask(com.hazelcast.transaction.TransactionalTask) DistributedObject(com.hazelcast.core.DistributedObject) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with TransactionalTask

use of com.hazelcast.transaction.TransactionalTask 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 5 with TransactionalTask

use of com.hazelcast.transaction.TransactionalTask in project hazelcast by hazelcast.

the class TxnMapNearCacheInvalidationTest method txn_map_contains_newly_put_key_even_it_is_null_cached_after_addition.

@Test
public void txn_map_contains_newly_put_key_even_it_is_null_cached_after_addition() {
    final String mapName = "test";
    final int key = 1;
    NearCacheConfig nearCacheConfig = createNearCacheConfig(mapName).setInvalidateOnChange(true);
    Config config = smallInstanceConfig();
    config.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);
    final HazelcastInstance instance = createHazelcastInstance(config);
    final CountDownLatch waitTxnPut = new CountDownLatch(1);
    final CountDownLatch waitNullCaching = new CountDownLatch(1);
    final AtomicBoolean keyExistInTxnMap = new AtomicBoolean(false);
    Thread txnPutThread = new Thread(new Runnable() {

        @Override
        public void run() {
            Boolean result = instance.executeTransaction(new TransactionalTask<Boolean>() {

                @Override
                public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                    TransactionalMap<Integer, Integer> map = context.getMap(mapName);
                    // 1. first put key into txn map inside txn
                    map.put(key, 1);
                    waitTxnPut.countDown();
                    assertOpenEventually(waitNullCaching);
                    // 3. after caching key as null inside Near Cache, check that key exist for txn
                    return map.containsKey(key);
                }
            });
            keyExistInTxnMap.set(result);
        }
    });
    txnPutThread.start();
    assertOpenEventually(waitTxnPut);
    // 2. cache key as null into Near Cache (we know we didn't commit the above txn and key is null inside map)
    instance.getMap(mapName).get(key);
    waitNullCaching.countDown();
    assertJoinable(txnPutThread);
    assertTrue("Key should be exist in txn map but we didn't find it", keyExistInTxnMap.get());
}
Also used : MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TransactionalTask(com.hazelcast.transaction.TransactionalTask) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionalTask (com.hazelcast.transaction.TransactionalTask)5 Test (org.junit.Test)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Config (com.hazelcast.config.Config)2 DistributedObject (com.hazelcast.core.DistributedObject)2 HazelcastOSGiTestUtil.createHazelcastOSGiInstance (com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance)2 TransactionOptions (com.hazelcast.transaction.TransactionOptions)2 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IQueue (com.hazelcast.collection.IQueue)1 ISet (com.hazelcast.collection.ISet)1 ItemEvent (com.hazelcast.collection.ItemEvent)1 ItemListener (com.hazelcast.collection.ItemListener)1 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 VersionedObjectComparator (com.hazelcast.collection.impl.queue.model.VersionedObjectComparator)1 MapConfig (com.hazelcast.config.MapConfig)1 NearCacheConfig (com.hazelcast.config.NearCacheConfig)1