Search in sources :

Example 21 with AssertTask

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

the class EvictionTest method testLastAddedKey_canBeEvicted_whenFreeHeapNeeded.

/**
     * Eviction of last added key can only be triggered with one of heap based max-size-policies.
     */
@Test
public void testLastAddedKey_canBeEvicted_whenFreeHeapNeeded() {
    // don't use getConfig(), this test is OSS specific
    Config config = new Config();
    config.setProperty(GroupProperty.PARTITION_COUNT.getName(), "1");
    config.getMapConfig("test").setEvictionPolicy(LFU).getMaxSizeConfig().setSize(90).setMaxSizePolicy(FREE_HEAP_PERCENTAGE);
    HazelcastInstance node = createHazelcastInstance(config);
    IMap<Integer, Integer> map = node.getMap("test");
    final AtomicReference<Integer> evictedKey = new AtomicReference<Integer>(null);
    map.addEntryListener(new EntryEvictedListener<Integer, Integer>() {

        @Override
        public void entryEvicted(EntryEvent<Integer, Integer> event) {
            evictedKey.set(event.getKey());
        }
    }, false);
    // 1. Make available free-heap-percentage 10. availableFree = maxMemoryMB - (totalMemoryMB - freeMemoryMB)
    // free-heap-percentage = availableFree/maxMemoryMB;
    int totalMemoryMB = 90;
    int freeMemoryMB = 0;
    int maxMemoryMB = 100;
    setMockRuntimeMemoryInfoAccessor(map, totalMemoryMB, freeMemoryMB, maxMemoryMB);
    // 2. This `put` should trigger eviction because we used 90% heap already.
    // And max used-heap-percentage was set 10% in map-config.
    map.put(1, 1);
    final Integer expected = 1;
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Eviction impl. should evict latest added key when heap based max-size-policy is used", expected, evictedKey.get());
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) AssertTask(com.hazelcast.test.AssertTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 22 with AssertTask

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

the class LocalMapStatsTest method testPutAsync.

@Test
public void testPutAsync() throws Exception {
    IMap<Integer, Integer> map = getMap();
    for (int i = 0; i < 100; i++) {
        map.putAsync(i, i);
    }
    final LocalMapStats localMapStats = map.getLocalMapStats();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(100, localMapStats.getPutOperationCount());
        }
    });
}
Also used : LocalMapStats(com.hazelcast.monitor.LocalMapStats) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 23 with AssertTask

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

the class LocalMapStatsTest method testRemoveAsync.

@Test
public void testRemoveAsync() throws Exception {
    IMap<Integer, Integer> map = getMap();
    for (int i = 0; i < 100; i++) {
        map.put(i, i);
        map.removeAsync(i);
    }
    final LocalMapStats localMapStats = map.getLocalMapStats();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(100, localMapStats.getRemoveOperationCount());
        }
    });
}
Also used : LocalMapStats(com.hazelcast.monitor.LocalMapStats) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 24 with AssertTask

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

the class LocalMapStatsTest method testHitsGenerated_updatedConcurrently.

@Test
public void testHitsGenerated_updatedConcurrently() throws Exception {
    final IMap<Integer, Integer> map = getMap();
    final int actionCount = 100;
    for (int i = 0; i < actionCount; i++) {
        map.put(i, i);
        map.get(i);
    }
    final LocalMapStats localMapStats = map.getLocalMapStats();
    final long initialHits = localMapStats.getHits();
    new Thread(new Runnable() {

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

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

Example 25 with AssertTask

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

the class QueryAdvancedTest method testMapWithIndexAfterShutDown.

@Test
public void testMapWithIndexAfterShutDown() {
    Config config = getConfig();
    String mapName = "default";
    config.getMapConfig(mapName).addMapIndexConfig(new MapIndexConfig("typeName", false));
    HazelcastInstance[] instances = createHazelcastInstanceFactory(3).newInstances(config);
    final IMap<Integer, ValueType> map = instances[0].getMap(mapName);
    final int sampleSize1 = 100;
    final int sampleSize2 = 30;
    int totalSize = sampleSize1 + sampleSize2;
    for (int i = 0; i < sampleSize1; i++) {
        map.put(i, new ValueType("type" + i));
    }
    for (int i = sampleSize1; i < totalSize; i++) {
        map.put(i, new ValueType("typex"));
    }
    Collection typexValues = map.values(new SqlPredicate("typeName = typex"));
    assertEquals(sampleSize2, typexValues.size());
    instances[1].shutdown();
    assertEquals(totalSize, map.size());
    assertTrueEventually(new AssertTask() {

        public void run() {
            final Collection values = map.values(new SqlPredicate("typeName = typex"));
            assertEquals(sampleSize2, values.size());
        }
    });
    instances[2].shutdown();
    assertEquals(totalSize, map.size());
    assertTrueEventually(new AssertTask() {

        public void run() {
            final Collection values = map.values(new SqlPredicate("typeName = typex"));
            assertEquals(sampleSize2, values.size());
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapIndexConfig(com.hazelcast.config.MapIndexConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ValueType(com.hazelcast.query.SampleObjects.ValueType) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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