Search in sources :

Example 21 with EntryEvent

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);
}
Also used : ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryEvent(com.hazelcast.core.EntryEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with EntryEvent

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");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 23 with EntryEvent

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 24 with EntryEvent

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);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapListenerAdapter(com.hazelcast.map.impl.MapListenerAdapter) EntryEvent(com.hazelcast.core.EntryEvent) Predicate(com.hazelcast.query.Predicate) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 25 with EntryEvent

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);
    }
}
Also used : EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) 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