summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/common
diff options
context:
space:
mode:
authorJason Tedor <jason@tedor.me>2017-05-08 09:49:14 -0400
committerGitHub <noreply@github.com>2017-05-08 09:49:14 -0400
commit0ec30eb8e03220140127616ab254ecd54d3b8026 (patch)
tree4408c97453e1a98e18eaa375757b7ed17acfc363 /core/src/test/java/org/elasticsearch/common
parent7e437f23663d66d4592b86a001a45eebff17b66f (diff)
Fix cache expire after access
This commit fixes a bug in the cache expire after access implementation. The bug is this: if you construct a cache with an expire after access of T, put a key, and then touch the key at some time t > T, the act of getting the key would update the access time for the entry before checking if the entry was expired. There are situations in which expire after access would be honored (e.g., if the cache needs to prune the LRU list to keep the cache under a certain weight, or a manual refresh was called) but this behavior is otherwise broken. Relates #24546
Diffstat (limited to 'core/src/test/java/org/elasticsearch/common')
-rw-r--r--core/src/test/java/org/elasticsearch/common/cache/CacheTests.java22
1 files changed, 22 insertions, 0 deletions
diff --git a/core/src/test/java/org/elasticsearch/common/cache/CacheTests.java b/core/src/test/java/org/elasticsearch/common/cache/CacheTests.java
index 71fe7b262a..7dbaba0289 100644
--- a/core/src/test/java/org/elasticsearch/common/cache/CacheTests.java
+++ b/core/src/test/java/org/elasticsearch/common/cache/CacheTests.java
@@ -257,6 +257,28 @@ public class CacheTests extends ESTestCase {
}
}
+ public void testSimpleExpireAfterAccess() {
+ AtomicLong now = new AtomicLong();
+ Cache<Integer, String> cache = new Cache<Integer, String>() {
+ @Override
+ protected long now() {
+ return now.get();
+ }
+ };
+ cache.setExpireAfterAccessNanos(1);
+ now.set(0);
+ for (int i = 0; i < numberOfEntries; i++) {
+ cache.put(i, Integer.toString(i));
+ }
+ for (int i = 0; i < numberOfEntries; i++) {
+ assertEquals(cache.get(i), Integer.toString(i));
+ }
+ now.set(2);
+ for(int i = 0; i < numberOfEntries; i++) {
+ assertNull(cache.get(i));
+ }
+ }
+
public void testExpirationAfterWrite() {
AtomicLong now = new AtomicLong();
Cache<Integer, String> cache = new Cache<Integer, String>() {