Search in sources :

Example 31 with EntryEvent

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);
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 32 with EntryEvent

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));
}
Also used : QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) EntryEvictedListener(com.hazelcast.map.listener.EntryEvictedListener) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryEvent(com.hazelcast.core.EntryEvent) ClientConfig(com.hazelcast.client.config.ClientConfig) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 33 with EntryEvent

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);
}
Also used : InternalQueryCache(com.hazelcast.map.impl.querycache.subscriber.InternalQueryCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 34 with EntryEvent

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Portable(com.hazelcast.nio.serialization.Portable) NamedPortable(com.hazelcast.internal.serialization.impl.portable.NamedPortable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListener(com.hazelcast.core.EntryListener) NamedPortable(com.hazelcast.internal.serialization.impl.portable.NamedPortable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 35 with EntryEvent

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);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapListenerAdapter(com.hazelcast.map.impl.MapListenerAdapter) EntryEvent(com.hazelcast.core.EntryEvent) Predicate(com.hazelcast.query.Predicate) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

EntryEvent (com.hazelcast.core.EntryEvent)71 Test (org.junit.Test)62 QuickTest (com.hazelcast.test.annotation.QuickTest)47 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)39 EntryAdapter (com.hazelcast.core.EntryAdapter)22 HazelcastInstance (com.hazelcast.core.HazelcastInstance)22 Config (com.hazelcast.config.Config)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 AssertTask (com.hazelcast.test.AssertTask)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 MapListener (com.hazelcast.map.listener.MapListener)11 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)10 MapConfig (com.hazelcast.config.MapConfig)9 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)8 Predicate (com.hazelcast.query.Predicate)8 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)8 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 EntryAddedListener (com.hazelcast.map.listener.EntryAddedListener)7 QueryCacheConfig (com.hazelcast.config.QueryCacheConfig)6