Search in sources :

Example 1 with CacheRecordHashMap

use of com.hazelcast.cache.impl.record.CacheRecordHashMap in project hazelcast by hazelcast.

the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.

@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
    final int recordCount = 100;
    final int expectedEvictedRecordValue = recordCount / 2;
    Node node = getNode(instance);
    SerializationService serializationService = node.getSerializationService();
    ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
    SamplingEvictionStrategy<K, V, S> evictionStrategy = SamplingEvictionStrategy.INSTANCE;
    CacheRecordHashMap cacheRecordMap = new CacheRecordHashMap(serializationService, 1000, cacheContext);
    CacheObjectRecord expectedEvictedRecord = null;
    Data expectedData = null;
    for (int i = 0; i < recordCount; i++) {
        CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
        Data data = serializationService.toData(i);
        cacheRecordMap.put(data, record);
        if (i == expectedEvictedRecordValue) {
            expectedEvictedRecord = record;
            expectedData = data;
        }
    }
    assertNotNull(expectedEvictedRecord);
    assertNotNull(expectedData);
    final SimpleEvictionCandidate evictionCandidate = new SimpleEvictionCandidate((K) expectedData, (V) expectedEvictedRecord);
    // we are testing "EvictionStrategy" in this test, so we mock "EvictionPolicyEvaluator" (it's tested in another test)
    EvictionPolicyEvaluator evictionPolicyEvaluator = mock(EvictionPolicyEvaluator.class);
    when(evictionPolicyEvaluator.evaluate(Matchers.any(Iterable.class))).thenReturn(evictionCandidate);
    when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
    assertEquals(recordCount, cacheRecordMap.size());
    assertTrue(cacheRecordMap.containsKey(expectedData));
    assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
    boolean evicted = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
    assertTrue(evicted);
    assertEquals(recordCount - 1, cacheRecordMap.size());
    assertFalse(cacheRecordMap.containsKey(expectedData));
    assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Node(com.hazelcast.instance.impl.Node) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheRecordHashMap(com.hazelcast.cache.impl.record.CacheRecordHashMap) EVICT_ALWAYS(com.hazelcast.internal.eviction.EvictionChecker.EVICT_ALWAYS) ICacheService(com.hazelcast.cache.impl.ICacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CacheContext (com.hazelcast.cache.impl.CacheContext)1 ICacheService (com.hazelcast.cache.impl.ICacheService)1 CacheObjectRecord (com.hazelcast.cache.impl.record.CacheObjectRecord)1 CacheRecordHashMap (com.hazelcast.cache.impl.record.CacheRecordHashMap)1 Node (com.hazelcast.instance.impl.Node)1 EVICT_ALWAYS (com.hazelcast.internal.eviction.EvictionChecker.EVICT_ALWAYS)1 EvictionPolicyEvaluator (com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator)1 Data (com.hazelcast.internal.serialization.Data)1 SerializationService (com.hazelcast.internal.serialization.SerializationService)1 Accessors.getNode (com.hazelcast.test.Accessors.getNode)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 Test (org.junit.Test)1