Search in sources :

Example 1 with EntryAdapter

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

the class ClientRegressionWithMockNetworkTest method testListenerReconnect.

public void testListenerReconnect() throws InterruptedException {
    final HazelcastInstance instance1 = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch latch = new CountDownLatch(2);
    final IMap<Object, Object> m = client.getMap("m");
    final String id = m.addEntryListener(new EntryAdapter() {

        public void entryAdded(EntryEvent event) {
            latch.countDown();
        }

        @Override
        public void entryUpdated(EntryEvent event) {
            latch.countDown();
        }
    }, true);
    m.put("key1", "value1");
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
    instance1.shutdown();
    final Thread thread = new Thread() {

        @Override
        public void run() {
            while (!isInterrupted()) {
                m.put("key2", "value2");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ignored) {
                }
            }
        }
    };
    thread.start();
    assertOpenEventually(latch, 10);
    thread.interrupt();
    assertTrue(m.removeEntryListener(id));
    assertFalse(m.removeEntryListener("foo"));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) DistributedObject(com.hazelcast.core.DistributedObject) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with EntryAdapter

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

the class ClientMapTest method testIssue537.

@Test
@SuppressWarnings("deprecation")
public void testIssue537() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch nullLatch = new CountDownLatch(2);
    EntryListener<String, GenericEvent> listener = new EntryAdapter<String, GenericEvent>() {

        @Override
        public void entryAdded(EntryEvent<String, GenericEvent> event) {
            latch.countDown();
        }

        @Override
        public void entryEvicted(EntryEvent<String, GenericEvent> event) {
            GenericEvent value = event.getValue();
            GenericEvent oldValue = event.getOldValue();
            if (value == null) {
                nullLatch.countDown();
            }
            if (oldValue != null) {
                nullLatch.countDown();
            }
            latch.countDown();
        }
    };
    IMap<String, GenericEvent> map = createMap();
    String id = map.addEntryListener(listener, true);
    map.put("key1", new GenericEvent("value1"), 2, TimeUnit.SECONDS);
    assertOpenEventually(latch);
    assertOpenEventually(nullLatch);
    map.removeEntryListener(id);
    map.put("key2", new GenericEvent("value2"));
    assertEquals(1, map.size());
}
Also used : GenericEvent(com.hazelcast.client.map.helpers.GenericEvent) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with EntryAdapter

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

the class ClientMapTest method testListener.

@Test
@SuppressWarnings("deprecation")
public void testListener() throws InterruptedException {
    IMap<String, String> map = createMap();
    final CountDownLatch latch1Add = new CountDownLatch(5);
    final CountDownLatch latch1Remove = new CountDownLatch(2);
    EntryListener<String, String> listener1 = new EntryAdapter<String, String>() {

        @Override
        public void entryAdded(EntryEvent<String, String> event) {
            latch1Add.countDown();
        }

        @Override
        public void entryRemoved(EntryEvent<String, String> event) {
            latch1Remove.countDown();
        }
    };
    final CountDownLatch latch2Add = new CountDownLatch(1);
    final CountDownLatch latch2Remove = new CountDownLatch(1);
    EntryListener<String, String> listener2 = new EntryAdapter<String, String>() {

        @Override
        public void entryAdded(EntryEvent<String, String> event) {
            latch2Add.countDown();
        }

        @Override
        public void entryRemoved(EntryEvent<String, String> event) {
            latch2Remove.countDown();
        }
    };
    map.addEntryListener(listener1, false);
    map.addEntryListener(listener2, "key3", true);
    Thread.sleep(1000);
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.put("key4", "value4");
    map.put("key5", "value5");
    map.remove("key1");
    map.remove("key3");
    assertTrue(latch1Add.await(10, TimeUnit.SECONDS));
    assertTrue(latch1Remove.await(10, TimeUnit.SECONDS));
    assertTrue(latch2Add.await(5, TimeUnit.SECONDS));
    assertTrue(latch2Remove.await(5, TimeUnit.SECONDS));
}
Also used : EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with EntryAdapter

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

the class ClientMapTest method testPredicateListenerWithPortableKey.

@Test
@SuppressWarnings("deprecation")
public void testPredicateListenerWithPortableKey() throws InterruptedException {
    IMap<Portable, Integer> tradeMap = createMap();
    final AtomicInteger atomicInteger = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    EntryListener listener = new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            atomicInteger.incrementAndGet();
            countDownLatch.countDown();
        }
    };
    NamedPortable key = new NamedPortable("a", 1);
    tradeMap.addEntryListener(listener, key, true);
    NamedPortable key2 = new NamedPortable("b", 2);
    tradeMap.put(key2, 1);
    assertFalse(countDownLatch.await(5, TimeUnit.SECONDS));
    assertEquals(0, atomicInteger.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Portable(com.hazelcast.nio.serialization.Portable) NamedPortable(com.hazelcast.nio.serialization.NamedPortable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListener(com.hazelcast.core.EntryListener) NamedPortable(com.hazelcast.nio.serialization.NamedPortable) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with EntryAdapter

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

the class ClientMultiMapListenersTest method testListenerOnKey_whenOtherKeysAdded.

@Test
public void testListenerOnKey_whenOtherKeysAdded() throws InterruptedException {
    final MultiMap mm = client.getMultiMap(randomString());
    final List<EntryEvent> events = new ArrayList<EntryEvent>();
    mm.addEntryListener(new EntryAdapter() {

        @Override
        public void entryAdded(EntryEvent event) {
            events.add(event);
        }
    }, "key", true);
    mm.put("key2", "value");
    mm.put("key", "value");
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, events.size());
            assertEquals("key", events.get(0).getKey());
        }
    });
}
Also used : MultiMap(com.hazelcast.core.MultiMap) EntryEvent(com.hazelcast.core.EntryEvent) EntryAdapter(com.hazelcast.core.EntryAdapter) ArrayList(java.util.ArrayList) AssertTask(com.hazelcast.test.AssertTask) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

EntryAdapter (com.hazelcast.core.EntryAdapter)26 Test (org.junit.Test)23 EntryEvent (com.hazelcast.core.EntryEvent)21 QuickTest (com.hazelcast.test.annotation.QuickTest)21 ParallelTest (com.hazelcast.test.annotation.ParallelTest)20 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Config (com.hazelcast.config.Config)12 AssertTask (com.hazelcast.test.AssertTask)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)6 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)6 NightlyTest (com.hazelcast.test.annotation.NightlyTest)6 MapConfig (com.hazelcast.config.MapConfig)5 IMap (com.hazelcast.core.IMap)4 MapEvent (com.hazelcast.core.MapEvent)4 SqlPredicate (com.hazelcast.query.SqlPredicate)4 MaxSizeConfig (com.hazelcast.config.MaxSizeConfig)3 ArrayList (java.util.ArrayList)3 MultiMapConfig (com.hazelcast.config.MultiMapConfig)2