Search in sources :

Example 1 with MapEvent

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

the class MultiMapListenerTest method testListeners.

@Test
public void testListeners() throws Exception {
    int count = 4;
    String name = randomMapName();
    Config config = new Config();
    config.getMultiMapConfig(name).setValueCollectionType(MultiMapConfig.ValueCollectionType.LIST);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(count);
    HazelcastInstance[] instances = factory.newInstances(config);
    final Set<String> keys = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
    EntryListener<String, String> listener = new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            keys.add(event.getKey());
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            keys.remove(event.getKey());
        }

        @Override
        public void mapCleared(MapEvent event) {
            keys.clear();
        }
    };
    final MultiMap<String, String> multiMap = instances[0].getMultiMap(name);
    final String id = multiMap.addLocalEntryListener(listener);
    multiMap.put("key1", "val1");
    multiMap.put("key2", "val2");
    multiMap.put("key3", "val3");
    multiMap.put("key4", "val4");
    multiMap.put("key5", "val5");
    multiMap.put("key6", "val6");
    multiMap.put("key7", "val7");
    multiMap.put("key8", "val8");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    if (keys.size() != 0) {
        multiMap.remove(keys.iterator().next());
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertContainsAll(multiMap.localKeySet(), keys);
        }
    });
    multiMap.removeEntryListener(id);
    getMultiMap(instances, name).clear();
    keys.clear();
    final String id2 = multiMap.addEntryListener(listener, true);
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key3", "val33");
    getMultiMap(instances, name).put("key4", "val4");
    getMultiMap(instances, name).remove("key3", "val33");
    assertSizeEventually(1, keys);
    getMultiMap(instances, name).clear();
    assertSizeEventually(0, keys);
    multiMap.removeEntryListener(id2);
    multiMap.addEntryListener(listener, "key7", true);
    getMultiMap(instances, name).put("key2", "val2");
    getMultiMap(instances, name).put("key3", "val3");
    getMultiMap(instances, name).put("key7", "val7");
    assertSizeEventually(1, keys);
}
Also used : MultiMapConfig(com.hazelcast.config.MultiMapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) MapEvent(com.hazelcast.core.MapEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with MapEvent

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

the class MultiMapTestsFrom2X method testMultiMapEntryListener.

@Test
public void testMultiMapEntryListener() {
    final HazelcastInstance instance = createHazelcastInstance();
    MultiMap<String, String> map = instance.getMultiMap("testMultiMapEntryListener");
    final CountDownLatch latchAdded = new CountDownLatch(3);
    final CountDownLatch latchRemoved = new CountDownLatch(1);
    final CountDownLatch latchCleared = new CountDownLatch(1);
    final Set<String> expectedValues = new CopyOnWriteArraySet<String>();
    expectedValues.add("hello");
    expectedValues.add("world");
    expectedValues.add("again");
    map.addEntryListener(new EntryAdapter<String, String>() {

        public void entryAdded(EntryEvent<String, String> event) {
            String key = event.getKey();
            String value = event.getValue();
            if ("2".equals(key)) {
                assertEquals("again", value);
            } else {
                assertEquals("1", key);
            }
            assertContains(expectedValues, value);
            expectedValues.remove(value);
            latchAdded.countDown();
        }

        public void entryRemoved(EntryEvent<String, String> event) {
            assertEquals("2", event.getKey());
            assertEquals("again", event.getOldValue());
            latchRemoved.countDown();
        }

        public void entryUpdated(EntryEvent<String, String> event) {
            throw new AssertionError("MultiMap cannot get update event!");
        }

        public void entryEvicted(EntryEvent<String, String> event) {
            entryRemoved(event);
        }

        @Override
        public void mapEvicted(MapEvent event) {
        }

        @Override
        public void mapCleared(MapEvent event) {
            latchCleared.countDown();
        }
    }, true);
    map.put("1", "hello");
    map.put("1", "world");
    map.put("2", "again");
    Collection<String> values = map.get("1");
    assertEquals(2, values.size());
    assertContains(values, "hello");
    assertContains(values, "world");
    assertEquals(1, map.get("2").size());
    assertEquals(3, map.size());
    map.remove("2");
    assertEquals(2, map.size());
    map.clear();
    try {
        assertTrue(latchAdded.await(5, TimeUnit.SECONDS));
        assertTrue(latchRemoved.await(5, TimeUnit.SECONDS));
        assertTrue(latchCleared.await(5, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MapEvent(com.hazelcast.core.MapEvent) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) HazelcastInstance(com.hazelcast.core.HazelcastInstance) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with MapEvent

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

the class BasicMapTest method testMapQueryListener.

@Test
public void testMapQueryListener() {
    IMap<Object, Object> map = getInstance().getMap(randomMapName());
    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>() {

        @Override
        public void entryAdded(EntryEvent<Object, Object> event) {
            addedKey[0] = event.getKey();
            addedValue[0] = event.getValue();
        }

        @Override
        public void entryRemoved(EntryEvent<Object, Object> event) {
            removedKey[0] = event.getKey();
            removedValue[0] = event.getOldValue();
        }

        @Override
        public void entryUpdated(EntryEvent<Object, Object> event) {
            updatedKey[0] = event.getKey();
            oldValue[0] = event.getOldValue();
            newValue[0] = event.getValue();
        }

        @Override
        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");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals("key1", addedKey[0]);
            assertEquals("abc", addedValue[0]);
            assertEquals("key2", updatedKey[0]);
            assertEquals("bcd", oldValue[0]);
            assertEquals("axyz", newValue[0]);
            assertEquals("key1", removedKey[0]);
            assertEquals("abc", removedValue[0]);
        }
    });
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapEvent(com.hazelcast.core.MapEvent) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MapEvent (com.hazelcast.core.MapEvent)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 EntryAdapter (com.hazelcast.core.EntryAdapter)2 EntryEvent (com.hazelcast.core.EntryEvent)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 AssertTask (com.hazelcast.test.AssertTask)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 Config (com.hazelcast.config.Config)1 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)1 MultiMapConfig (com.hazelcast.config.MultiMapConfig)1 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1