Search in sources :

Example 1 with PROP_CLEANUP_ENABLED

use of com.hazelcast.map.impl.eviction.MapClearExpiredRecordsTask.PROP_CLEANUP_ENABLED in project hazelcast by hazelcast.

the class QueryAdvancedTest method testQueryWithTTL.

@Test
public void testQueryWithTTL() {
    Config config = getConfig();
    // disable background expiration task to keep test safe from jitter
    config.setProperty(PROP_CLEANUP_ENABLED, "false");
    String mapName = "default";
    config.getMapConfig(mapName).setTimeToLiveSeconds(3);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<String, Employee> map = instance.getMap(mapName);
    map.addIndex(IndexType.HASH, "name");
    map.addIndex(IndexType.SORTED, "age");
    map.addIndex(IndexType.HASH, "active");
    int passiveEmployees = 5;
    int activeEmployees = 5;
    int allEmployees = passiveEmployees + activeEmployees;
    final CountDownLatch latch = new CountDownLatch(allEmployees);
    map.addEntryListener((EntryExpiredListener) 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
    final Collection values = map.values(Predicates.sql("active"));
    assertTrueEventually(() -> assertEquals(String.format("Expected %s results but got %s." + " Number of evicted entries: %s.", activeEmployees, values.size(), allEmployees - latch.getCount()), activeEmployees, values.size()));
    // delete expire keys by explicitly calling `map#get`
    assertTrueEventually(() -> {
        for (int i = 0; i < activeEmployees; i++) {
            assertNull(map.get("activeEmployee" + i));
        }
        for (int i = 0; i < passiveEmployees; i++) {
            assertNull(map.get("passiveEmployee" + i));
        }
    });
    // wait until eviction is completed
    assertOpenEventually(latch);
    // check the query result after eviction
    assertEquals(0, map.values(Predicates.sql("active")).size());
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) PortableEmployee(com.hazelcast.query.SampleTestObjects.PortableEmployee) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ValueType(com.hazelcast.query.SampleTestObjects.ValueType) IndexType(com.hazelcast.config.IndexType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) Employee(com.hazelcast.query.SampleTestObjects.Employee) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) Predicate(com.hazelcast.query.Predicate) ExpectedException(org.junit.rules.ExpectedException) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastException(com.hazelcast.core.HazelcastException) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Category(org.junit.experimental.categories.Category) EntryExpiredListener(com.hazelcast.map.listener.EntryExpiredListener) IndexConfig(com.hazelcast.config.IndexConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) RootCauseMatcher(com.hazelcast.internal.util.RootCauseMatcher) Predicates(com.hazelcast.query.Predicates) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) PROP_CLEANUP_ENABLED(com.hazelcast.map.impl.eviction.MapClearExpiredRecordsTask.PROP_CLEANUP_ENABLED) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PortableEmployee(com.hazelcast.query.SampleTestObjects.PortableEmployee) Employee(com.hazelcast.query.SampleTestObjects.Employee) Config(com.hazelcast.config.Config) IndexConfig(com.hazelcast.config.IndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Collection(java.util.Collection) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

Config (com.hazelcast.config.Config)1 IndexConfig (com.hazelcast.config.IndexConfig)1 IndexType (com.hazelcast.config.IndexType)1 MapStoreConfig (com.hazelcast.config.MapStoreConfig)1 HazelcastException (com.hazelcast.core.HazelcastException)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 RootCauseMatcher (com.hazelcast.internal.util.RootCauseMatcher)1 IMap (com.hazelcast.map.IMap)1 MapStoreAdapter (com.hazelcast.map.MapStoreAdapter)1 PROP_CLEANUP_ENABLED (com.hazelcast.map.impl.eviction.MapClearExpiredRecordsTask.PROP_CLEANUP_ENABLED)1 EntryExpiredListener (com.hazelcast.map.listener.EntryExpiredListener)1 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)1 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)1 DataSerializable (com.hazelcast.nio.serialization.DataSerializable)1 Predicate (com.hazelcast.query.Predicate)1 Predicates (com.hazelcast.query.Predicates)1 Employee (com.hazelcast.query.SampleTestObjects.Employee)1 PortableEmployee (com.hazelcast.query.SampleTestObjects.PortableEmployee)1 ValueType (com.hazelcast.query.SampleTestObjects.ValueType)1 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)1