Search in sources :

Example 56 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ReplicatedMapWriteOrderTest method testDataIntegrity.

@Test
public void testDataIntegrity() throws InterruptedException {
    System.out.println("nodeCount = " + nodeCount);
    System.out.println("operations = " + operations);
    System.out.println("keyCount = " + keyCount);
    Config config = new Config();
    config.getReplicatedMapConfig("test").setReplicationDelayMillis(0);
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    String replicatedMapName = "test";
    final List<ReplicatedMap> maps = createMapOnEachInstance(instances, replicatedMapName);
    ArrayList<Integer> keys = generateRandomIntegerList(keyCount);
    Thread[] threads = createThreads(nodeCount, maps, keys, operations);
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    for (int i = 0; i < keyCount; i++) {
        final String key = "foo-" + keys.get(i);
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                System.out.println("---------------------");
                System.out.println("key = " + key);
                printValues();
                assertValuesAreEqual();
            }

            private void printValues() throws Exception {
                for (int j = 0; j < maps.size(); j++) {
                    ReplicatedMap map = maps.get(j);
                    System.out.println("value[" + j + "] = " + map.get(key) + " , store version : " + getStore(map, key).getVersion());
                }
            }

            private void assertValuesAreEqual() {
                for (int i = 0; i < maps.size() - 1; i++) {
                    ReplicatedMap map1 = maps.get(i);
                    ReplicatedMap map2 = maps.get(i + 1);
                    Object v1 = map1.get(key);
                    Object v2 = map2.get(key);
                    assertNotNull(v1);
                    assertNotNull(v2);
                    assertEquals(v1, v2);
                }
            }
        }, 120);
    }
}
Also used : Config(com.hazelcast.config.Config) ReplicatedMap(com.hazelcast.core.ReplicatedMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Test(org.junit.Test)

Example 57 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ReplicatedMapStatsTest method testHitsGenerated_updatedConcurrently.

@Test
public void testHitsGenerated_updatedConcurrently() throws Exception {
    final ReplicatedMap<Integer, Integer> replicatedMap = getReplicatedMap();
    final int actionCount = 100;
    for (int i = 0; i < actionCount; i++) {
        replicatedMap.put(i, i);
        replicatedMap.get(i);
    }
    final LocalReplicatedMapStats stats = replicatedMap.getReplicatedMapStats();
    final long initialHits = stats.getHits();
    new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < actionCount; i++) {
                replicatedMap.get(i);
            }
            // causes the local stats object to update
            replicatedMap.getReplicatedMapStats();
        }
    }).start();
    assertEquals(actionCount, initialHits);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(actionCount * 2, stats.getHits());
        }
    });
}
Also used : AssertTask(com.hazelcast.test.AssertTask) LocalReplicatedMapStats(com.hazelcast.monitor.LocalReplicatedMapStats) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 58 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ReplicatedMapStatsTest method testLastUpdateTime_updatedConcurrently.

@Test
public void testLastUpdateTime_updatedConcurrently() throws InterruptedException {
    final long startTime = Clock.currentTimeMillis();
    final ReplicatedMap<String, String> map = getReplicatedMap();
    final String key = "key";
    map.put(key, "value");
    final LocalReplicatedMapStats stats = map.getReplicatedMapStats();
    final long lastUpdateTime = stats.getLastUpdateTime();
    new Thread(new Runnable() {

        @Override
        public void run() {
            sleepAtLeastMillis(1);
            map.put(key, "value2");
            // causes the local stats object to update
            map.getReplicatedMapStats();
        }
    }).start();
    assertTrue(lastUpdateTime >= startTime);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(stats.getLastUpdateTime() >= lastUpdateTime);
        }
    });
}
Also used : AssertTask(com.hazelcast.test.AssertTask) LocalReplicatedMapStats(com.hazelcast.monitor.LocalReplicatedMapStats) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 59 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ReplicatedMapTest method testContainsValue.

private void testContainsValue(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");
    final int partitionCount = getPartitionService(instance1).getPartitionCount();
    final Set<String> keys = generateRandomKeys(instance1, partitionCount);
    int half = keys.size() / 2, i = 0;
    for (String key : keys) {
        final ReplicatedMap<String, String> map = i++ < half ? map1 : map2;
        map.put(key, key);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (String key : keys) {
                assertTrue(map1.containsValue(key));
                assertTrue(map2.containsValue(key));
            }
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TimeoutException(java.util.concurrent.TimeoutException)

Example 60 with AssertTask

use of com.hazelcast.test.AssertTask in project hazelcast by hazelcast.

the class ReplicatedMapTest method testPutAll.

private void testPutAll(Config config) throws TimeoutException {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");
    final int partitionCount = getPartitionService(instance1).getPartitionCount();
    final Set<String> keys = generateRandomKeys(instance1, partitionCount);
    final Map<String, String> mapTest = new HashMap<String, String>();
    for (String key : keys) {
        mapTest.put(key, "bar");
    }
    map1.putAll(mapTest);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (String key : keys) {
                assertEquals("bar", map1.get(key));
                assertEquals("bar", map2.get(key));
            }
        }
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HashMap(java.util.HashMap) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

AssertTask (com.hazelcast.test.AssertTask)575 Test (org.junit.Test)489 QuickTest (com.hazelcast.test.annotation.QuickTest)428 ParallelTest (com.hazelcast.test.annotation.ParallelTest)347 HazelcastInstance (com.hazelcast.core.HazelcastInstance)263 Config (com.hazelcast.config.Config)113 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)94 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)75 ExecutionException (java.util.concurrent.ExecutionException)57 MapConfig (com.hazelcast.config.MapConfig)49 NightlyTest (com.hazelcast.test.annotation.NightlyTest)48 IOException (java.io.IOException)46 CountDownLatch (java.util.concurrent.CountDownLatch)42 IMap (com.hazelcast.core.IMap)39 NearCacheConfig (com.hazelcast.config.NearCacheConfig)38 TimeoutException (java.util.concurrent.TimeoutException)33 ClientConfig (com.hazelcast.client.config.ClientConfig)32 MapStoreConfig (com.hazelcast.config.MapStoreConfig)29 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)23 AtomicReference (java.util.concurrent.atomic.AtomicReference)20