use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class NodeQueryCacheEventServiceTest method no_left_over_listener_after_concurrent_addition_and_removal_on_same_queryCache.
@Test
public void no_left_over_listener_after_concurrent_addition_and_removal_on_same_queryCache() throws InterruptedException {
final String mapName = "test";
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
final HazelcastInstance node = factory.newHazelcastInstance();
SubscriberContext subscriberContext = getSubscriberContext(node);
final NodeQueryCacheEventService nodeQueryCacheEventService = (NodeQueryCacheEventService) subscriberContext.getEventService();
final AtomicBoolean stop = new AtomicBoolean(false);
ArrayList<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < 5; i++) {
Thread thread = new Thread() {
@Override
public void run() {
while (!stop.get()) {
nodeQueryCacheEventService.addListener(mapName, "a", new EntryAddedListener() {
@Override
public void entryAdded(EntryEvent event) {
}
});
nodeQueryCacheEventService.removeAllListeners(mapName, "a");
}
}
};
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
sleepSeconds(5);
stop.set(true);
for (Thread thread : threads) {
thread.join();
}
assertNoUserListenerLeft(node);
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class QueryListenerTest method testMapQueryListener.
@Test
public void testMapQueryListener() throws InterruptedException {
Config config = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
HazelcastInstance instance3 = nodeFactory.newHazelcastInstance(config);
final IMap<Object, Object> map = instance1.getMap("testMapQueryListener");
final Object[] addedKey = new Object[1];
final Object[] addedValue = new Object[1];
final Object[] updatedKey = new Object[1];
final Object[] oldValue = new Object[1];
final Object[] newValue = new Object[1];
final Object[] removedKey = new Object[1];
final Object[] removedValue = new Object[1];
EntryListener<Object, Object> listener = new EntryAdapter<Object, Object>() {
public void entryAdded(EntryEvent<Object, Object> event) {
addedKey[0] = event.getKey();
addedValue[0] = event.getValue();
}
public void entryRemoved(EntryEvent<Object, Object> event) {
removedKey[0] = event.getKey();
removedValue[0] = event.getOldValue();
}
public void entryUpdated(EntryEvent<Object, Object> event) {
updatedKey[0] = event.getKey();
oldValue[0] = event.getOldValue();
newValue[0] = event.getValue();
}
public void entryEvicted(EntryEvent<Object, Object> event) {
}
@Override
public void mapEvicted(MapEvent event) {
}
@Override
public void mapCleared(MapEvent event) {
}
};
map.addEntryListener(listener, new StartsWithPredicate("a"), null, true);
map.put("key1", "abc");
map.put("key2", "bcd");
map.put("key2", "axyz");
map.remove("key1");
Thread.sleep(1000);
assertEquals(addedKey[0], "key1");
assertEquals(addedValue[0], "abc");
assertEquals(updatedKey[0], "key2");
assertEquals(oldValue[0], "bcd");
assertEquals(newValue[0], "axyz");
assertEquals(removedKey[0], "key1");
assertEquals(removedValue[0], "abc");
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class QueryListenerTest method testMapQueryListener2.
@Test
public void testMapQueryListener2() throws InterruptedException {
Config cfg = getConfig();
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(cfg);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(cfg);
HazelcastInstance instance3 = nodeFactory.newHazelcastInstance(cfg);
final IMap<Object, Object> map = instance1.getMap("testMapQueryListener2");
final AtomicInteger addCount = new AtomicInteger(0);
EntryListener<Object, Object> listener = new EntryAdapter<Object, Object>() {
public void entryAdded(EntryEvent<Object, Object> event) {
addCount.incrementAndGet();
}
};
Predicate<Object, Object> predicate = Predicates.sql("age >= 50");
map.addEntryListener(listener, predicate, null, false);
int size = 100;
for (int i = 0; i < size; i++) {
Person person = new Person("name", i);
map.put(i, person);
}
Thread.sleep(1000);
assertEquals(50, addCount.get());
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class MapPreconditionsTest method testAddEntryListenerWithMapListenerAndPredicateAndKey_NullPredicate.
@Test(expected = NullPointerException.class)
public void testAddEntryListenerWithMapListenerAndPredicateAndKey_NullPredicate() {
MapListener mapListener = new MapListenerAdapter() {
public void onEntryEvent(EntryEvent event) {
System.out.println("-");
}
};
Predicate predicate = null;
map.addEntryListener(mapListener, predicate, null, true);
}
use of com.hazelcast.core.EntryEvent in project hazelcast by hazelcast.
the class BasicMapTest method testMapEntryListener.
@Test
public void testMapEntryListener() {
IMap<String, String> map = getInstance().getMap("testMapEntryListener");
final CountDownLatch latchAdded = new CountDownLatch(1);
final CountDownLatch latchRemoved = new CountDownLatch(1);
final CountDownLatch latchUpdated = new CountDownLatch(1);
final CountDownLatch latchCleared = new CountDownLatch(1);
final CountDownLatch latchEvicted = new CountDownLatch(1);
map.addEntryListener(new EntryListener<String, String>() {
@Override
public void entryExpired(EntryEvent<String, String> event) {
}
@Override
public void entryAdded(EntryEvent event) {
latchAdded.countDown();
}
@Override
public void entryRemoved(EntryEvent event) {
assertEquals("hello", event.getKey());
assertEquals("new world", event.getOldValue());
latchRemoved.countDown();
}
@Override
public void entryUpdated(EntryEvent event) {
assertEquals("world", event.getOldValue());
assertEquals("new world", event.getValue());
assertEquals("hello", event.getKey());
latchUpdated.countDown();
}
@Override
public void entryEvicted(EntryEvent event) {
entryRemoved(event);
}
@Override
public void mapEvicted(MapEvent event) {
latchEvicted.countDown();
}
@Override
public void mapCleared(MapEvent event) {
latchCleared.countDown();
}
}, true);
map.put("hello", "world");
map.put("hello", "new world");
map.remove("hello");
map.put("hi", "new world");
map.evictAll();
map.put("hello", "world");
map.clear();
try {
assertTrue(latchAdded.await(5, SECONDS));
assertTrue(latchUpdated.await(5, SECONDS));
assertTrue(latchRemoved.await(5, SECONDS));
assertTrue(latchEvicted.await(5, SECONDS));
assertTrue(latchCleared.await(5, SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
assertFalse(e.getMessage(), true);
}
}
Aggregations