Search in sources :

Example 11 with EntryAdapter

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

the class MapTransactionRegressionTest method test_Issue1076.

@Test
public void test_Issue1076() {
    Config config = getConfig();
    final HazelcastInstance inst = createHazelcastInstance(config);
    IMap map = inst.getMap("default");
    EntryListener<String, Integer> l = new EntryAdapter<String, Integer>() {
    };
    EntryObject e = new PredicateBuilder().getEntryObject();
    Predicate<String, Integer> p = e.equal(1);
    map.addEntryListener(l, p, null, false);
    for (Integer i = 0; i < 100; i++) {
        TransactionContext context = inst.newTransactionContext();
        context.beginTransaction();
        TransactionalMap<String, Integer> txnMap = context.getMap("default");
        txnMap.remove(i.toString());
        context.commitTransaction();
    }
    assertEquals(0, map.size());
    inst.shutdown();
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) EntryAdapter(com.hazelcast.core.EntryAdapter) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 12 with EntryAdapter

use of com.hazelcast.core.EntryAdapter 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 = new SqlPredicate("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 : Config(com.hazelcast.config.Config) EntryAdapter(com.hazelcast.core.EntryAdapter) SqlPredicate(com.hazelcast.query.SqlPredicate) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryEvent(com.hazelcast.core.EntryEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 13 with EntryAdapter

use of com.hazelcast.core.EntryAdapter 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) MapEvent(com.hazelcast.core.MapEvent) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 14 with EntryAdapter

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

the class QueryCacheListenerTest method listenerShouldReceive_CLEAR_ALL_Event_whenIMapCleared.

@Test
public void listenerShouldReceive_CLEAR_ALL_Event_whenIMapCleared() {
    String cacheName = randomString();
    int entryCount = 1000;
    final AtomicInteger clearAllEventCount = new AtomicInteger();
    final QueryCache<Integer, Employee> queryCache = map.getQueryCache(cacheName, new EntryAdapter() {

        @Override
        public void mapCleared(MapEvent e) {
            clearAllEventCount.incrementAndGet();
        }
    }, TRUE_PREDICATE, false);
    populateMap(map, entryCount);
    assertQueryCacheSizeEventually(entryCount, queryCache);
    map.clear();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            // expecting at least 1 event
            assertTrue(clearAllEventCount.get() >= 1);
            assertEquals(0, queryCache.size());
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Employee(com.hazelcast.mapreduce.helpers.Employee) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) 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)

Example 15 with EntryAdapter

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

the class EvictionTest method testMapPutTTLWithListener.

/**
     * Background task {@link com.hazelcast.map.impl.eviction.ExpirationManager.ClearExpiredRecordsTask}
     * should sweep expired records eventually.
     */
@Test
@Category(NightlyTest.class)
public void testMapPutTTLWithListener() throws InterruptedException {
    final int putCount = 100;
    final CountDownLatch latch = new CountDownLatch(putCount);
    IMap<Integer, Integer> map = createSimpleMap();
    map.addEntryListener(new EntryAdapter() {

        public void entryEvicted(final EntryEvent event) {
            latch.countDown();
        }
    }, true);
    final int ttl = (int) (Math.random() * 5000);
    for (int j = 0; j < putCount; j++) {
        map.put(j, j, ttl, TimeUnit.MILLISECONDS);
    }
    // wait until eviction is complete.
    assertOpenEventually(latch, TimeUnit.MINUTES.toSeconds(10));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) 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