use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class ClientMapTest method testListener.
@Test
public void testListener() {
IMap<String, String> map = createMap();
final CountDownLatch latch1Add = new CountDownLatch(5);
final CountDownLatch latch1Remove = new CountDownLatch(2);
EntryListener<String, String> listener1 = new EntryAdapter<String, String>() {
@Override
public void entryAdded(EntryEvent<String, String> event) {
latch1Add.countDown();
}
@Override
public void entryRemoved(EntryEvent<String, String> event) {
latch1Remove.countDown();
}
};
final CountDownLatch latch2Add = new CountDownLatch(1);
final CountDownLatch latch2Remove = new CountDownLatch(1);
EntryListener<String, String> listener2 = new EntryAdapter<String, String>() {
@Override
public void entryAdded(EntryEvent<String, String> event) {
latch2Add.countDown();
}
@Override
public void entryRemoved(EntryEvent<String, String> event) {
latch2Remove.countDown();
}
};
map.addEntryListener(listener1, false);
map.addEntryListener(listener2, "key3", true);
sleepSeconds(1);
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
map.put("key5", "value5");
map.remove("key1");
map.remove("key3");
assertOpenEventually(latch1Add);
assertOpenEventually(latch1Remove);
assertOpenEventually(latch2Add);
assertOpenEventually(latch2Remove);
}
use of com.hazelcast.core.EntryEvent 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).setMaxSizePolicy(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, Predicates.alwaysTrue(), true);
UUID 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.core.EntryEvent in project hazelcast by hazelcast.
the class ClientQueryCacheRecreationTest method listeners_still_works_after_query_cache_recreation.
@Test
public void listeners_still_works_after_query_cache_recreation() {
IMap<Object, Object> map = client.getMap(mapName);
QueryCache<Object, Object> queryCache = map.getQueryCache(queryCacheName, Predicates.alwaysTrue(), true);
final AtomicInteger entryAddedCounter = new AtomicInteger();
queryCache.addEntryListener(new EntryAdapter() {
@Override
public void entryAdded(EntryEvent event) {
entryAddedCounter.incrementAndGet();
}
}, Predicates.sql("__key >= 10"), true);
// Restart server
server.shutdown();
server = factory.newHazelcastInstance(getConfig());
// Recreate query cache on same reference
// Recreation empties query cache.
((InternalQueryCache) queryCache).recreate();
for (int i = 0; i < 100; i++) {
map.put(i, i);
}
AssertTask assertTask = () -> assertEquals(90, entryAddedCounter.get());
assertTrueEventually(assertTask);
assertTrueAllTheTime(assertTask, 3);
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class ClientMapTest method testPredicateListenerWithPortableKey.
@Test
public void testPredicateListenerWithPortableKey() throws Exception {
IMap<Portable, Integer> tradeMap = createMap();
final AtomicInteger atomicInteger = new AtomicInteger(0);
final CountDownLatch countDownLatch = new CountDownLatch(1);
EntryListener listener = new EntryAdapter() {
@Override
public void entryAdded(EntryEvent event) {
atomicInteger.incrementAndGet();
countDownLatch.countDown();
}
};
NamedPortable key = new NamedPortable("a", 1);
tradeMap.addEntryListener(listener, key, true);
NamedPortable key2 = new NamedPortable("b", 2);
tradeMap.put(key2, 1);
assertFalse(countDownLatch.await(5, TimeUnit.SECONDS));
assertEquals(0, atomicInteger.get());
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class MapPreconditionsTest method testAddEntryListenerWithMapListenerAndPredicate_NullPredicate.
@Test(expected = NullPointerException.class)
public void testAddEntryListenerWithMapListenerAndPredicate_NullPredicate() {
MapListener mapListener = new MapListenerAdapter() {
public void onEntryEvent(EntryEvent event) {
System.out.println("-");
}
};
Predicate predicate = null;
map.addEntryListener(mapListener, predicate, true);
}
Aggregations