Search in sources :

Example 46 with TransactionalMap

use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionRegressionTest method test_Issue615_ValuesWithPredicate.

@Test
public void test_Issue615_ValuesWithPredicate() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map2 = h2.getMap("default");
    final SampleObjects.Employee emp1 = new SampleObjects.Employee("abc-123-xvz", 34, true, 10D);
    map2.put(1, emp1);
    final SampleObjects.Employee emp2 = new SampleObjects.Employee("xvz", 4, true, 10D);
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            assertEquals(0, txMap.values(new SqlPredicate("age <= 10")).size());
            txMap.put(2, emp2);
            Collection coll = txMap.values(new SqlPredicate("age <= 10"));
            Iterator<Object> iterator = coll.iterator();
            while (iterator.hasNext()) {
                final SampleObjects.Employee e = (SampleObjects.Employee) iterator.next();
                assertEquals(emp2, e);
            }
            coll = txMap.values(new SqlPredicate("age > 30 "));
            iterator = coll.iterator();
            while (iterator.hasNext()) {
                final SampleObjects.Employee e = (SampleObjects.Employee) iterator.next();
                assertEquals(emp1, e);
            }
            txMap.remove(2);
            coll = txMap.values(new SqlPredicate("age <= 10 "));
            assertEquals(0, coll.size());
            return true;
        }
    });
    assertEquals(0, map2.values(new SqlPredicate("age <= 10")).size());
    assertEquals(1, map2.values(new SqlPredicate("age = 34")).size());
    h1.shutdown();
    h2.shutdown();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) SampleObjects(com.hazelcast.query.SampleObjects) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) SqlPredicate(com.hazelcast.query.SqlPredicate) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) Iterator(java.util.Iterator) Collection(java.util.Collection) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 47 with TransactionalMap

use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionRegressionTest method test_Issue615_values.

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

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.put("3", "3");
            assertEquals(3, txMap.values().size());
            map2.put("4", "4");
            assertEquals(4, txMap.values().size());
            txMap.remove("1");
            assertEquals(3, txMap.values().size());
            map2.remove("2");
            assertEquals(2, txMap.values().size());
            assertEquals(2, txMap.size());
            txMap.put("12", "32");
            assertEquals(2, map2.values().size());
            return true;
        }
    });
    assertEquals(3, map2.values().size());
    h1.shutdown();
    h2.shutdown();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 48 with TransactionalMap

use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionRegressionTest method test_Issue615_keySet.

@Test
public void test_Issue615_keySet() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final IMap map = h2.getMap("default");
    map.put("1", "1");
    map.put("2", "2");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.put("3", "3");
            assertEquals(3, txMap.keySet().size());
            map.put("4", "4");
            assertEquals(4, txMap.keySet().size());
            txMap.remove("1");
            assertEquals(3, txMap.keySet().size());
            map.remove("2");
            assertEquals(2, txMap.keySet().size());
            assertEquals(2, txMap.size());
            return true;
        }
    });
    assertEquals(2, map.keySet().size());
    // raise an exception and rollback changes.
    try {
        boolean b2 = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

            public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                final TransactionalMap<Object, Object> txMap = context.getMap("default");
                txMap.put("5", "5");
                assertEquals(3, txMap.keySet().size());
                assertEquals(2, map.keySet().size());
                throw new DummyUncheckedHazelcastTestException();
            }
        });
    } catch (Exception e) {
        if (!(e instanceof DummyUncheckedHazelcastTestException)) {
            throw new RuntimeException(e);
        }
    }
    assertEquals(2, map.keySet().size());
    h1.shutdown();
    h2.shutdown();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) TransactionException(com.hazelcast.transaction.TransactionException) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 49 with TransactionalMap

use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testTxnBackupDies.

@Test
public void testTxnBackupDies() throws TransactionException, InterruptedException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    final String mapName = randomMapName();
    final IMap map = h1.getMap(mapName);
    final int size = 100;
    final CountDownLatch transactionCompletedLatch = new CountDownLatch(size + 1);
    final CountDownLatch thresholdReached = new CountDownLatch(1);
    Runnable runnable = new Runnable() {

        public void run() {
            try {
                final int oneThird = size / 3;
                final int threshold = new Random().nextInt(oneThird) + oneThird;
                h1.executeTransaction(options, new TransactionalTask<Boolean>() {

                    public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                        final TransactionalMap<Object, Object> txMap = context.getMap(mapName);
                        for (int i = 0; i < size; i++) {
                            if (i == threshold) {
                                thresholdReached.countDown();
                            }
                            txMap.put(i, i);
                            sleepMillis(100);
                            transactionCompletedLatch.countDown();
                        }
                        return true;
                    }
                });
                fail();
            } catch (Exception ignored) {
            }
            transactionCompletedLatch.countDown();
        }
    };
    new Thread(runnable).start();
    assertOpenEventually(thresholdReached);
    terminateInstance(h2);
    assertOpenEventually(transactionCompletedLatch);
    for (int i = 0; i < size; i++) {
        assertNull(map.get(i));
    }
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) TransactionException(com.hazelcast.transaction.TransactionException) ExecutionException(java.util.concurrent.ExecutionException) ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) TransactionNotActiveException(com.hazelcast.transaction.TransactionNotActiveException) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) Random(java.util.Random) 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 50 with TransactionalMap

use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.

the class MapTransactionTest method testContainsKey.

@Test
public void testContainsKey() throws TransactionException {
    Config config = getConfig();
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final IMap<String, String> map = h1.getMap("default");
    map.put("1", "1");
    boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            final TransactionalMap<Object, Object> txMap = context.getMap("default");
            txMap.delete("1");
            assertEquals(false, txMap.containsKey("1"));
            assertEquals(true, map.containsKey("1"));
            return true;
        }
    });
    assertTrue(b);
}
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)

Aggregations

TransactionalMap (com.hazelcast.core.TransactionalMap)92 Test (org.junit.Test)73 QuickTest (com.hazelcast.test.annotation.QuickTest)68 ParallelTest (com.hazelcast.test.annotation.ParallelTest)67 HazelcastInstance (com.hazelcast.core.HazelcastInstance)61 TransactionException (com.hazelcast.transaction.TransactionException)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)59 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 Config (com.hazelcast.config.Config)36 TransactionContext (com.hazelcast.transaction.TransactionContext)33 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)30 IMap (com.hazelcast.core.IMap)29 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)12 SqlPredicate (com.hazelcast.query.SqlPredicate)8 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 Collection (java.util.Collection)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)6 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6