elements not evicted if new elements are added

This commit is contained in:
2018-12-20 16:13:55 +01:00
parent d52bfa0916
commit afba3b6f77
3 changed files with 250 additions and 54 deletions

View File

@@ -1,6 +1,7 @@
package org.lucares.utils.cache;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -12,6 +13,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.lucares.utils.cache.HotEntryCache.EventType;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -32,6 +35,26 @@ public class HotEntryCacheTest {
final String cachedValue2 = cache.get("key");
Assert.assertEquals(cachedValue2, "value2");
cache.checkInvariants();
}
public void testPutTouches() throws InterruptedException, ExecutionException, TimeoutException {
final ModifiableFixedTimeClock clock = new ModifiableFixedTimeClock();
final Duration timeToLive = Duration.ofSeconds(10);
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive, clock);
cache.put("key", "value1");
final Instant oldestLastAccessTime = cache.getLastAccessMap().firstKey();
Assert.assertEquals(oldestLastAccessTime, Instant.now(clock));
clock.plusSeconds(1);
cache.put("key", "value2");
Assert.assertEquals(cache.getLastAccessMap().size(), 1);
final Instant oldestLastAccessTimeAfterTouch = cache.getLastAccessMap().firstKey();
Assert.assertEquals(oldestLastAccessTimeAfterTouch, Instant.now(clock));
cache.checkInvariants();
}
public void testEvictOnGet() throws InterruptedException, ExecutionException, TimeoutException {
@@ -42,10 +65,11 @@ public class HotEntryCacheTest {
cache.put("key", "value1");
clock.plus(timeToLive.plusMillis(1));
cache.triggerEviction();
// cache.triggerEviction();
final String cachedValue1_evicted = cache.get("key");
Assert.assertEquals(cachedValue1_evicted, null);
cache.checkInvariants();
}
public void testEvictionByBackgroundThread() throws InterruptedException, ExecutionException, TimeoutException {
@@ -60,11 +84,15 @@ public class HotEntryCacheTest {
cache.put("key", "value1");
clock.plus(timeToLive.plusMillis(1));
clock.plus(timeToLive.minusSeconds(1));
cache.put("key2", "value2");
clock.plus(Duration.ofSeconds(1).plusMillis(1));
cache.triggerEviction();
final String evictedValue1 = evictionEventFuture.get(5, TimeUnit.MINUTES); // enough time for debugging
Assert.assertEquals(evictedValue1, "value1");
cache.checkInvariants();
}
public void testRemove() throws InterruptedException, ExecutionException, TimeoutException {
@@ -81,6 +109,7 @@ public class HotEntryCacheTest {
Assert.assertEquals(removedValues, Arrays.asList("value1"));
Assert.assertEquals(cache.get("key"), null);
cache.checkInvariants();
}
public void testClear() throws InterruptedException, ExecutionException, TimeoutException {
@@ -98,6 +127,7 @@ public class HotEntryCacheTest {
Assert.assertEquals(cache.get("key2"), null);
Assert.assertEquals(removedValues, Arrays.asList("value1", "value2"));
cache.checkInvariants();
}
public void testForEachTouches() throws InterruptedException, ExecutionException, TimeoutException {
@@ -130,6 +160,7 @@ public class HotEntryCacheTest {
clock.plus(timeToLive.minusMillis(1));
Assert.assertEquals(cache.get("key"), null);
cache.checkInvariants();
}
/**
@@ -175,6 +206,7 @@ public class HotEntryCacheTest {
} finally {
pool.shutdownNow();
}
cache.checkInvariants();
}
public void testPutIfAbsentReturnsExistingValue() throws Exception {
@@ -191,6 +223,7 @@ public class HotEntryCacheTest {
final String actualInCache = cache.get(key);
Assert.assertEquals(actualInCache, valueA);
cache.checkInvariants();
}
public void testPutIfAbsentDoesNotAddNull() throws Exception {
@@ -202,6 +235,7 @@ public class HotEntryCacheTest {
final String actualInCache = cache.get(key);
Assert.assertEquals(actualInCache, null);
cache.checkInvariants();
}
private void sleep(final TimeUnit timeUnit, final long timeout) {
@@ -219,4 +253,40 @@ public class HotEntryCacheTest {
throw new IllegalStateException(e);
}
}
public static void main(final String[] args) throws InterruptedException {
Configurator.setRootLevel(Level.TRACE);
final Duration timeToLive = Duration.ofSeconds(1);
final HotEntryCache<String, String> cache = new HotEntryCache<>(timeToLive);
cache.addListener(event -> {
System.out.println(Instant.now() + " evicting: " + event);
}, EventType.EVICTED);
cache.put("key", "value that is touched");
for (int i = 0; i < 20; i++) {
System.out.println(Instant.now() + " putting value" + i);
cache.put("key" + i, "value" + i);
cache.put("key", "value that is touched" + i);
TimeUnit.MILLISECONDS.sleep(450);
}
for (int i = 20; i < 23; i++) {
System.out.println(Instant.now() + " putting value" + i);
cache.put("key" + i, "value" + i);
TimeUnit.MILLISECONDS.sleep(Duration.ofSeconds(5).plusMillis(10).toMillis());
}
TimeUnit.MILLISECONDS.sleep(Duration.ofSeconds(5).plusMillis(10).toMillis());
for (int i = 23; i < 27; i++) {
System.out.println(Instant.now() + " putting value" + i);
cache.put("key" + i, "value" + i);
TimeUnit.MILLISECONDS.sleep(Duration.ofSeconds(5).plusMillis(10).toMillis());
}
TimeUnit.SECONDS.sleep(300);
}
}