Search in sources :

Example 1 with EntryExpiredListener

use of com.hazelcast.map.listener.EntryExpiredListener in project hazelcast by hazelcast.

the class ExpirationManagerTest method stops_running_when_clusterState_turns_passive.

@Test
public void stops_running_when_clusterState_turns_passive() throws Exception {
    Config config = new Config();
    config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
    HazelcastInstance node = createHazelcastInstance(config);
    final AtomicInteger expirationCounter = new AtomicInteger();
    IMap map = node.getMap("test");
    map.addEntryListener(new EntryExpiredListener() {

        @Override
        public void entryExpired(EntryEvent event) {
            expirationCounter.incrementAndGet();
        }
    }, true);
    map.put(1, 1, 3, TimeUnit.SECONDS);
    node.getCluster().changeClusterState(PASSIVE);
    // wait a little to see if any expiration is occurring
    sleepSeconds(3);
    int expirationCount = expirationCounter.get();
    assertEquals(format("Expecting no expiration but found:%d", expirationCount), 0, expirationCount);
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryExpiredListener(com.hazelcast.map.listener.EntryExpiredListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(com.hazelcast.config.Config) EntryEvent(com.hazelcast.core.EntryEvent) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with EntryExpiredListener

use of com.hazelcast.map.listener.EntryExpiredListener in project hazelcast by hazelcast.

the class ExpirationManagerTest method starts_running_when_clusterState_turns_active.

@Test
public void starts_running_when_clusterState_turns_active() throws Exception {
    Config config = new Config();
    config.setProperty(SYS_PROP_EXPIRATION_TASK_PERIOD_SECONDS, "1");
    HazelcastInstance node = createHazelcastInstance(config);
    final AtomicInteger expirationCounter = new AtomicInteger();
    IMap map = node.getMap("test");
    map.addEntryListener(new EntryExpiredListener() {

        @Override
        public void entryExpired(EntryEvent event) {
            expirationCounter.incrementAndGet();
        }
    }, true);
    map.put(1, 1, 3, SECONDS);
    node.getCluster().changeClusterState(PASSIVE);
    node.getCluster().changeClusterState(ACTIVE);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            int expirationCount = expirationCounter.get();
            assertEquals(format("Expecting 1 expiration but found:%d", expirationCount), 1, expirationCount);
        }
    });
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryExpiredListener(com.hazelcast.map.listener.EntryExpiredListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(com.hazelcast.config.Config) EntryEvent(com.hazelcast.core.EntryEvent) AssertTask(com.hazelcast.test.AssertTask) ExpectedException(org.junit.rules.ExpectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

Config (com.hazelcast.config.Config)2 EntryEvent (com.hazelcast.core.EntryEvent)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 IMap (com.hazelcast.core.IMap)2 EntryExpiredListener (com.hazelcast.map.listener.EntryExpiredListener)2 ParallelTest (com.hazelcast.test.annotation.ParallelTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 AssertTask (com.hazelcast.test.AssertTask)1 ExpectedException (org.junit.rules.ExpectedException)1