Search in sources :

Example 46 with EntryEvent

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

the class EvictionTest method testMapRecordIdleEvictionOnMigration.

@Test
@Category(NightlyTest.class)
public void testMapRecordIdleEvictionOnMigration() {
    final String name = "testMapRecordIdleEvictionOnMigration";
    Config cfg = getConfig();
    cfg.setProperty(GroupProperty.PARTITION_COUNT.getName(), "1");
    MapConfig mapConfig = cfg.getMapConfig(name);
    int maxIdleSeconds = 30;
    int size = 100;
    final int nsize = size / 5;
    mapConfig.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
    final IMap<Integer, Integer> map = instance1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(new EntryAdapter() {

        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    // put sample data
    for (int i = 0; i < size; i++) {
        map.put(i, i);
    }
    // wait until some time that is close to eviction
    sleepSeconds(maxIdleSeconds - 5);
    // touch the ones you dont want to be evicted.
    for (int i = 0; i < nsize; i++) {
        map.get(i);
    }
    factory.newHazelcastInstance(cfg);
    factory.newHazelcastInstance(cfg);
    //wait until eviction is complete
    assertOpenEventually(latch, 240);
    assertSizeEventually(nsize, map);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) 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)

Example 47 with EntryEvent

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

the class QueryAdvancedTest method testQueryWithTTL.

@Test
@SuppressWarnings("deprecation")
public void testQueryWithTTL() throws Exception {
    Config config = getConfig();
    String mapName = "default";
    config.getMapConfig(mapName).setTimeToLiveSeconds(5);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<String, Employee> map = instance.getMap(mapName);
    map.addIndex("name", false);
    map.addIndex("age", false);
    map.addIndex("active", true);
    int passiveEmployees = 5;
    int activeEmployees = 5;
    int allEmployees = passiveEmployees + activeEmployees;
    final CountDownLatch latch = new CountDownLatch(allEmployees);
    map.addEntryListener(new EntryAdapter() {

        @Override
        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    for (int i = 0; i < activeEmployees; i++) {
        Employee employee = new Employee("activeEmployee" + i, 60, true, i);
        map.put("activeEmployee" + i, employee);
    }
    for (int i = 0; i < passiveEmployees; i++) {
        Employee employee = new Employee("passiveEmployee" + i, 60, false, i);
        map.put("passiveEmployee" + i, employee);
    }
    // check the query result before eviction
    Collection values = map.values(new SqlPredicate("active"));
    assertEquals(activeEmployees, values.size());
    // wait until eviction is completed
    assertOpenEventually(latch);
    // check the query result after eviction
    values = map.values(new SqlPredicate("active"));
    assertEquals(0, values.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 48 with EntryEvent

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

the class MapPreconditionsTest method testAddLocalEntryListenerWithMapListenerAndPredicate_NullPredicate.

@Test(expected = NullPointerException.class)
public void testAddLocalEntryListenerWithMapListenerAndPredicate_NullPredicate() {
    MapListener mapListener = new MapListenerAdapter() {

        public void onEntryEvent(EntryEvent event) {
            System.out.println("-");
        }
    };
    Predicate predicate = null;
    map.addLocalEntryListener(mapListener, predicate, true);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapListenerAdapter(com.hazelcast.map.impl.MapListenerAdapter) EntryEvent(com.hazelcast.core.EntryEvent) TruePredicate(com.hazelcast.query.TruePredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 49 with EntryEvent

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

the class ListenerTest method testConfigListenerRegistration.

@Test
public void testConfigListenerRegistration() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    String name = randomString();
    Config config = getConfig();
    MapConfig mapConfig = config.getMapConfig(name);
    EntryListenerConfig entryListenerConfig = new EntryListenerConfig();
    entryListenerConfig.setImplementation(new EntryAdapter() {

        public void entryAdded(EntryEvent event) {
            latch.countDown();
        }
    });
    mapConfig.addEntryListenerConfig(entryListenerConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<Object, Object> map = instance.getMap(name);
    map.put(1, 1);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 50 with EntryEvent

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

the class MapPreconditionsTest method testAddEntryListenerWithMapListenerAndPredicate_NullPredicate.

@Test(expected = NullPointerException.class)
public void testAddEntryListenerWithMapListenerAndPredicate_NullPredicate() {
    MapListener mapListener = new MapListenerAdapter() {

        public void onEntryEvent(EntryEvent event) {
            System.out.println("-");
        }
    };
    Predicate predicate = null;
    map.addEntryListener(mapListener, predicate, true);
}
Also used : MapListener(com.hazelcast.map.listener.MapListener) MapListenerAdapter(com.hazelcast.map.impl.MapListenerAdapter) EntryEvent(com.hazelcast.core.EntryEvent) TruePredicate(com.hazelcast.query.TruePredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

EntryEvent (com.hazelcast.core.EntryEvent)57 Test (org.junit.Test)49 QuickTest (com.hazelcast.test.annotation.QuickTest)38 ParallelTest (com.hazelcast.test.annotation.ParallelTest)37 EntryAdapter (com.hazelcast.core.EntryAdapter)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 HazelcastInstance (com.hazelcast.core.HazelcastInstance)17 Config (com.hazelcast.config.Config)15 AssertTask (com.hazelcast.test.AssertTask)11 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)10 MapListener (com.hazelcast.map.listener.MapListener)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Predicate (com.hazelcast.query.Predicate)8 TruePredicate (com.hazelcast.query.TruePredicate)8 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)7 MapConfig (com.hazelcast.config.MapConfig)7 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)6 MapEvent (com.hazelcast.core.MapEvent)5 MaxSizeConfig (com.hazelcast.config.MaxSizeConfig)4