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));
}
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));
}
Aggregations