Commit c053eb3e authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

util: ensure that cache entry lifetime is bigger than idle time

Motivation:
'document' the behavior, avoid miss configurations

Modification:
Add precondition into Cache constructor

Result:
Invalid cache objects can be constructed

Target: master
Acked-by: Albert Rossi
Acked-by: Lea Morschel
parent a045ca7c
Pipeline #23646 passed with stage
in 2 minutes and 3 seconds
/*
* Copyright (c) 2009 - 2020 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
......@@ -32,6 +32,8 @@ import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkArgument;
/**
* A Dictionary where value associated with the key may become unavailable due
* to validity timeout.
......@@ -146,6 +148,9 @@ public class Cache<K, V> {
*/
public Cache(final String name, int size, long entryLifeTime, long entryIdleTime,
CacheEventListener<K, V> eventListener, Clock clock) {
checkArgument(entryLifeTime >= entryIdleTime, "Entry life time cant be smaller that idle time");
_name = name;
_size = size;
_defaultEntryMaxLifeTime = entryLifeTime;
......
......@@ -42,6 +42,13 @@ public class CacheTest {
new NopCacheEventListener(), _clock);
}
@Test(expected = IllegalArgumentException.class)
public void testCacheIdleBiggerThanMax() {
new Cache<>("test cache", 10,
5, 7,
new NopCacheEventListener(), _clock);
}
@Test
public void testPutGet() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment