Search in sources :

Example 1 with CacheEventListener

use of com.hazelcast.cache.impl.CacheEventListener in project hazelcast by hazelcast.

the class CacheClearTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    Map<String, String> entries = createAndFillEntries();
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        cache.put(entry.getKey(), entry.getValue());
    }
    // Verify that put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new CacheEventListener() {

        @Override
        public void handleEvent(Object eventObject) {
            if (eventObject instanceof Invalidation) {
                Invalidation event = (Invalidation) eventObject;
                if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                    counter.incrementAndGet();
                }
            }
        }
    }, config.getNameWithPrefix());
    cache.clear();
    // Make sure that one event is received
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, counter.get());
        }
    }, 5);
    // Make sure that the callback is not called for a while
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= 1);
        }
    }, 3);
}
Also used : CacheEventListener(com.hazelcast.cache.impl.CacheEventListener) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) HashMap(java.util.HashMap) Map(java.util.Map) CacheConfig(com.hazelcast.config.CacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with CacheEventListener

use of com.hazelcast.cache.impl.CacheEventListener in project hazelcast by hazelcast.

the class CacheDestroyTest method testInvalidationListenerCallCount.

@Test
public void testInvalidationListenerCallCount() {
    final ICache<String, String> cache = createCache();
    final AtomicInteger counter = new AtomicInteger(0);
    final CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new CacheEventListener() {

        @Override
        public void handleEvent(Object eventObject) {
            if (eventObject instanceof Invalidation) {
                Invalidation event = (Invalidation) eventObject;
                if (null == event.getKey() && config.getNameWithPrefix().equals(event.getName())) {
                    counter.incrementAndGet();
                }
            }
        }
    }, config.getNameWithPrefix());
    cache.destroy();
    // Make sure that at least 1 invalidation event has been received
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() >= 1);
        }
    }, 2);
    // Make sure that no more than INSTNACE_COUNT events are received ever
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= INSTANCE_COUNT);
        }
    }, 3);
}
Also used : CacheEventListener(com.hazelcast.cache.impl.CacheEventListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) Invalidation(com.hazelcast.internal.nearcache.impl.invalidation.Invalidation) CacheConfig(com.hazelcast.config.CacheConfig) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CacheEventListener (com.hazelcast.cache.impl.CacheEventListener)2 CacheConfig (com.hazelcast.config.CacheConfig)2 Invalidation (com.hazelcast.internal.nearcache.impl.invalidation.Invalidation)2 AssertTask (com.hazelcast.test.AssertTask)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1