Search in sources :

Example 61 with EntryEvent

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

the class QueryCacheIMapEventHandlingTest 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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 62 with EntryEvent

use of com.hazelcast.core.EntryEvent 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);
    final CountDownLatch entryCountingLatch = new CountDownLatch(populationCount);
    QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, Predicates.alwaysTrue(), true);
    UUID listener = cache.addEntryListener(new EntryAddedListener() {

        @Override
        public void entryAdded(EntryEvent event) {
            entryCountingLatch.countDown();
        }
    }, false);
    for (int i = 0; i < populationCount; i++) {
        map.put(i, i);
    }
    assertOpenEventually("Cache size is " + cache.size(), entryCountingLatch);
    // expecting at most populationCount - maxSize - 5 entries
    // 5 states an error margin since eviction does not sweep precise number of entries.
    assertQueryCacheEvicted(maxSize, 5, cache);
    assertTrue(cache.removeEntryListener(listener));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) MapConfig(com.hazelcast.config.MapConfig) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 63 with EntryEvent

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

the class QueryCacheCoalescingTest method testCoalescingModeWorks.

@Test
public void testCoalescingModeWorks() {
    String mapName = randomString();
    String cacheName = randomString();
    Config config = getConfig(mapName, cacheName);
    HazelcastInstance node = createHazelcastInstance(config);
    IMap<Integer, Integer> map = getMap(node, mapName);
    final CountDownLatch updateEventCount = new CountDownLatch(1);
    final QueryCache<Integer, Integer> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, true);
    cache.addEntryListener(new EntryUpdatedListener() {

        @Override
        public void entryUpdated(EntryEvent event) {
            updateEventCount.countDown();
        }
    }, false);
    for (int i = 0; i < 100; i++) {
        map.put(i, i);
    }
    // update same key to control whether coalescing kicks in.
    for (int i = 0; i < 500; i++) {
        map.put(0, i);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(100, cache.size());
        }
    });
    assertOpenEventually(updateEventCount);
}
Also used : Config(com.hazelcast.config.Config) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) CountDownLatch(java.util.concurrent.CountDownLatch) EntryUpdatedListener(com.hazelcast.map.listener.EntryUpdatedListener) HazelcastInstance(com.hazelcast.core.HazelcastInstance) 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 64 with EntryEvent

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

the class EventPublishOrderWithEvictionTest method assertEmittedEventsOrder.

private void assertEmittedEventsOrder(EventOrderAwareEntryListener entryListener) {
    Map<Integer, List<EntryEventType>> eventsPerKey = new HashMap<Integer, List<EntryEventType>>();
    List<EntryEvent> events = entryListener.getOrderedEvents();
    for (EntryEvent event : events) {
        Integer key = (Integer) event.getKey();
        List<EntryEventType> eventTypes = eventsPerKey.get(key);
        if (eventTypes == null) {
            eventTypes = new ArrayList<EntryEventType>();
            eventsPerKey.put(key, eventTypes);
        }
        EntryEventType eventType = event.getEventType();
        eventTypes.add(eventType);
    }
    Set<Map.Entry<Integer, List<EntryEventType>>> entries = eventsPerKey.entrySet();
    for (Map.Entry<Integer, List<EntryEventType>> entry : entries) {
        List<EntryEventType> eventTypes = entry.getValue();
        EntryEventType prev = null;
        for (int i = 0; i < eventTypes.size(); i++) {
            final EntryEventType eventType = eventTypes.get(i);
            if (i == 0) {
                assertEquals(EntryEventType.ADDED, eventType);
                prev = eventType;
                continue;
            }
            if (prev.equals(EntryEventType.ADDED)) {
                assertEquals(EntryEventType.EVICTED, eventType);
            } else if (prev.equals(EntryEventType.EVICTED)) {
                assertEquals(EntryEventType.ADDED, eventType);
            }
            prev = eventType;
        }
    }
}
Also used : HashMap(java.util.HashMap) EntryEventType(com.hazelcast.core.EntryEventType) EntryEvent(com.hazelcast.core.EntryEvent) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with EntryEvent

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

the class EvictionTest method expired_entries_removed_after_migration.

@Test
@Category(NightlyTest.class)
public void expired_entries_removed_after_migration() {
    int numOfEntries = 10;
    String name = "expired_entries_removed_after_migration";
    MapConfig mapConfig = newMapConfig(name).setMaxIdleSeconds(20);
    Config config = getConfig().setProperty(PROP_TASK_PERIOD_SECONDS, "1").setProperty(ClusterProperty.PARTITION_COUNT.getName(), "2").addMapConfig(mapConfig);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance node1 = factory.newHazelcastInstance(config);
    IMap<Integer, Integer> map = node1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(numOfEntries);
    map.addEntryListener(new EntryAdapter() {

        @Override
        public void entryExpired(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    for (int i = 0; i < numOfEntries; ++i) {
        map.put(i, i);
    }
    // data migration will be done to new node
    factory.newHazelcastInstance(config);
    assertOpenEventually(latch);
    assertSizeEventually(0, map);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.hazelcast.config.Config) 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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

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