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);
}
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);
}
Aggregations