Search in sources :

Example 1 with EntryEvictedListener

use of com.hazelcast.map.listener.EntryEvictedListener in project hazelcast by hazelcast.

the class ClientEvictionTest method testMaxSizeEvictionWorks.

@Test
public void testMaxSizeEvictionWorks() throws Exception {
    final int maxSize = 1000;
    final int populationCount = 5000;
    String mapName = randomString();
    String cacheName = randomString();
    QueryCacheConfig cacheConfig = new QueryCacheConfig(cacheName);
    cacheConfig.getEvictionConfig().setSize(maxSize).setEvictionPolicy(EvictionPolicy.LFU).setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addQueryCacheConfig(mapName, cacheConfig);
    HazelcastInstance client = factory.newHazelcastClient(clientConfig);
    IMap<Integer, Integer> map = client.getMap(mapName);
    // expecting at least populationCount - maxSize + 10 evicted entries according to max size.
    // 10 states an error margin since eviction does not sweep precise number of entries.
    int margin = 10;
    final CountDownLatch evictedCount = new CountDownLatch(populationCount - maxSize - margin);
    final QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, TruePredicate.INSTANCE, true);
    String listener = cache.addEntryListener(new EntryEvictedListener() {

        @Override
        public void entryEvicted(EntryEvent event) {
            evictedCount.countDown();
        }
    }, false);
    for (int i = 0; i < populationCount; i++) {
        map.put(i, i);
    }
    assertOpenEventually(evictedCount);
    assertQueryCacheEvicted(maxSize, margin, cache);
    assertTrue(cache.removeEntryListener(listener));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) EntryEvent(com.hazelcast.core.EntryEvent) EntryEvictedListener(com.hazelcast.map.listener.EntryEvictedListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with EntryEvictedListener

use of com.hazelcast.map.listener.EntryEvictedListener in project hazelcast by hazelcast.

the class EvictionTest method testMaxSizeEvictionWorks.

@Test
public void testMaxSizeEvictionWorks() {
    int maxSize = 100;
    int populationCount = 500;
    String mapName = randomString();
    String cacheName = randomString();
    Config config = getConfig(maxSize, mapName, cacheName);
    HazelcastInstance node = createHazelcastInstance(config);
    IMap<Integer, Integer> map = getMap(node, mapName);
    // expecting at least populationCount - maxSize - 50 evicted entries according to max size.
    // 50 states an error margin since eviction does not sweep precise number of entries.
    int margin = 50;
    final CountDownLatch evictedCount = new CountDownLatch(populationCount - maxSize - margin);
    QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, TruePredicate.INSTANCE, true);
    String listener = cache.addEntryListener(new EntryEvictedListener() {

        @Override
        public void entryEvicted(EntryEvent event) {
            evictedCount.countDown();
        }
    }, false);
    for (int i = 0; i < populationCount; i++) {
        map.put(i, i);
    }
    assertOpenEventually("Cache size is " + cache.size(), evictedCount);
    assertQueryCacheEvicted(maxSize, margin, cache);
    assertTrue(cache.removeEntryListener(listener));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) MapConfig(com.hazelcast.config.MapConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) EntryEvent(com.hazelcast.core.EntryEvent) EntryEvictedListener(com.hazelcast.map.listener.EntryEvictedListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

QueryCacheConfig (com.hazelcast.config.QueryCacheConfig)2 EntryEvent (com.hazelcast.core.EntryEvent)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 EntryEvictedListener (com.hazelcast.map.listener.EntryEvictedListener)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 ClientConfig (com.hazelcast.client.config.ClientConfig)1 Config (com.hazelcast.config.Config)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 MapConfig (com.hazelcast.config.MapConfig)1