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