Search in sources :

Example 6 with TransactionalTaskContext

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

the class MapTransactionTest method testGetForUpdate_whenNullKey.

// =================== getForUpdate ===============================
@Test(expected = NullPointerException.class)
public void testGetForUpdate_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.getForUpdate(null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with TransactionalTaskContext

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

the class MapTransactionTest method testGetForUpdate_releasesBackupLock.

@Test
public void testGetForUpdate_releasesBackupLock() {
    Config config = getConfig();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = factory.newHazelcastInstance(config);
    HazelcastInstance instance2 = factory.newHazelcastInstance(config);
    final String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    instance1.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(randomString());
            map.getForUpdate(keyOwnedByInstance2);
            return null;
        }
    });
    Node node = TestUtil.getNode(instance1);
    Data keyData = node.nodeEngine.toData(keyOwnedByInstance2);
    LockService lockService = node.nodeEngine.getService(LockService.SERVICE_NAME);
    for (LockResource lockResource : lockService.getAllLocks()) {
        if (keyData.equals(lockResource.getKey())) {
            assertEquals(0, lockResource.getLockCount());
        }
    }
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) LockService(com.hazelcast.concurrent.lock.LockService) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) Node(com.hazelcast.instance.Node) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Data(com.hazelcast.nio.serialization.Data) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) LockResource(com.hazelcast.concurrent.lock.LockResource) Mockito.anyObject(org.mockito.Mockito.anyObject) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with TransactionalTaskContext

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

the class MapTransactionTest method testTxnCommit.

@Test
public void testTxnCommit() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final String map1 = "map1";
    final String map2 = "map2";
    final String key = "1";
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap1 = context.getMap(map1);
            final TransactionalMap<Object, Object> txMap2 = context.getMap(map2);
            txMap1.put(key, "value");
            assertEquals("value", txMap1.put(key, "value1"));
            assertEquals("value1", txMap1.get(key));
            txMap2.put(key, "value");
            assertEquals("value", txMap2.put(key, "value2"));
            assertEquals("value2", txMap2.get(key));
            assertEquals(true, txMap1.containsKey(key));
            assertEquals(true, txMap2.containsKey(key));
            assertNull(h1.getMap(map1).get(key));
            assertNull(h1.getMap(map2).get(key));
            return true;
        }
    });
    assertTrue(b);
    assertEquals("value1", h1.getMap(map1).get(key));
    assertEquals("value1", h2.getMap(map1).get(key));
    assertEquals("value2", h1.getMap(map2).get(key));
    assertEquals("value2", h2.getMap(map2).get(key));
    assertFalse(h1.getMap(map1).isLocked(key));
    assertFalse(h1.getMap(map2).isLocked(key));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with TransactionalTaskContext

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

the class MapTransactionTest method testContainsKey_whenNullKey.

// ========================= containsKey =====================
@Test(expected = NullPointerException.class)
public void testContainsKey_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.containsKey(null);
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with TransactionalTaskContext

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

the class MapTransactionTest method tesPutIfAbsent_whenNullKey.

// ========================= putIfAbsent =====================
@Test(expected = NullPointerException.class)
public void tesPutIfAbsent_whenNullKey() throws TransactionException {
    final HazelcastInstance hz = createHazelcastInstance();
    hz.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.putIfAbsent(null, "value");
            return true;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)63 Test (org.junit.Test)63 TransactionException (com.hazelcast.transaction.TransactionException)62 HazelcastInstance (com.hazelcast.core.HazelcastInstance)61 TransactionalMap (com.hazelcast.core.TransactionalMap)59 ParallelTest (com.hazelcast.test.annotation.ParallelTest)58 QuickTest (com.hazelcast.test.annotation.QuickTest)58 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 Config (com.hazelcast.config.Config)36 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)32 IMap (com.hazelcast.core.IMap)18 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 ExecutionException (java.util.concurrent.ExecutionException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5 Employee (com.hazelcast.query.SampleObjects.Employee)5