Search in sources :

Example 36 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ClientMultiMapListenersTest method testListenerOnKey_whenOtherKeysAdded.

@Test
public void testListenerOnKey_whenOtherKeysAdded() throws InterruptedException {
    final MultiMap mm = client.getMultiMap(randomString());
    final List<EntryEvent> events = new ArrayList<EntryEvent>();
    mm.addEntryListener(new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            events.add(event);
        }
    }, "key", true);
    mm.put("key2", "value");
    mm.put("key", "value");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, events.size());
            assertEquals("key", events.get(0).getKey());
        }
    });
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) EntryEvent(com.hazelcast.core.EntryEvent) EntryAdapter(com.hazelcast.core.EntryAdapter) ArrayList(java.util.ArrayList) AssertTask(com.hazelcast.test.AssertTask) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 37 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ClientQueryCacheTest method testQueryCache_withLocalListener.

@Test
public void testQueryCache_withLocalListener() {
    String mapName = randomString();
    String queryCacheName = randomString();
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Integer> map = client.getMap(mapName);
    for (int i = 0; i < 30; i++) {
        map.put(i, i);
    }
    final AtomicInteger countAddEvent = new AtomicInteger();
    final AtomicInteger countRemoveEvent = new AtomicInteger();
    final QueryCache<Integer, Integer> queryCache = map.getQueryCache(queryCacheName, new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            countAddEvent.incrementAndGet();
        }

        @Override
        public void entryRemoved(EntryEvent event) {
            countRemoveEvent.incrementAndGet();
        }
    }, Predicates.sql("this > 20"), true);
    for (int i = 0; i < 30; i++) {
        map.remove(i);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, queryCache.size());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Count of add events wrong!", 9, countAddEvent.get());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Count of remove events wrong!", 9, countRemoveEvent.get());
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) 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 38 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ClientQueryCacheTest method testQueryCache_withLocalListener.

@Test
public void testQueryCache_withLocalListener() {
    String mapName = randomString();
    String queryCacheName = randomString();
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Integer> map = client.getMap(mapName);
    for (int i = 0; i < 30; i++) {
        map.put(i, i);
    }
    final AtomicInteger countAddEvent = new AtomicInteger();
    final AtomicInteger countRemoveEvent = new AtomicInteger();
    final QueryCache<Integer, Integer> queryCache = map.getQueryCache(queryCacheName, new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            countAddEvent.incrementAndGet();
        }

        @Override
        public void entryRemoved(EntryEvent event) {
            countRemoveEvent.incrementAndGet();
        }
    }, new SqlPredicate("this > 20"), true);
    for (int i = 0; i < 30; i++) {
        map.remove(i);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, queryCache.size());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Count of add events wrong!", 9, countAddEvent.get());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals("Count of remove events wrong!", 9, countRemoveEvent.get());
        }
    });
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) SqlPredicate(com.hazelcast.query.SqlPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 39 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class ClientQueryCacheEventHandlingTest method testEvent_EXPIRED.

@Test
public void testEvent_EXPIRED() throws Exception {
    int key = 1;
    int value = 1;
    final CountDownLatch latch = new CountDownLatch(1);
    queryCache.addEntryListener(new EntryAddedListener() {

        @Override
        public void entryAdded(EntryEvent event) {
            latch.countDown();
        }
    }, true);
    map.put(key, value, 1, SECONDS);
    latch.await();
    sleepSeconds(1);
    // map#get creates EXPIRED event
    map.get(key);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, queryCache.size());
        }
    });
}
Also used : EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 40 with EntryEvent

use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.

the class EvictionTest method testMapRecordIdleEvictionOnMigration.

@Test
@Category(NightlyTest.class)
public void testMapRecordIdleEvictionOnMigration() {
    final String name = "testMapRecordIdleEvictionOnMigration";
    Config cfg = getConfig();
    cfg.setProperty(GroupProperty.PARTITION_COUNT.getName(), "1");
    MapConfig mapConfig = cfg.getMapConfig(name);
    int maxIdleSeconds = 30;
    int size = 100;
    final int nsize = size / 5;
    mapConfig.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
    final IMap<Integer, Integer> map = instance1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(new EntryAdapter() {

        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    // put sample data
    for (int i = 0; i < size; i++) {
        map.put(i, i);
    }
    // wait until some time that is close to eviction
    sleepSeconds(maxIdleSeconds - 5);
    // touch the ones you dont want to be evicted.
    for (int i = 0; i < nsize; i++) {
        map.get(i);
    }
    factory.newHazelcastInstance(cfg);
    factory.newHazelcastInstance(cfg);
    //wait until eviction is complete
    assertOpenEventually(latch, 240);
    assertSizeEventually(nsize, map);
}
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) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

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