[COLLECTIONS-703] The PassiveExpiringMap#put() method should return the

previous record only if not expired.
This commit is contained in:
Tomas Tulka 2018-11-23 00:51:19 -07:00 committed by Gary Gregory
parent c15be5398f
commit 26bebdafe4
3 changed files with 26 additions and 10 deletions

View File

@ -39,6 +39,9 @@
<action issue="COLLECTIONS-701" dev="ggregory" type="fix" due-to="Shin Hong, Don Jeba">
StackOverflowError in SetUniqueList.add() when it receives itself.
</action>
<action issue="COLLECTIONS-703" dev="ggregory" type="fix" due-to="Tomas Tulka">
The PassiveExpiringMap#put() method should return the previous record only if not expired.
</action>
</release>
<release version="4.2" date="2018-07-11" description="Update from Java 6 to Java 7, bug fixes, and small changes.">
<action issue="COLLECTIONS-681" dev="kinow" type="add" due-to="Stephan Fuhrmann">

View File

@ -430,6 +430,9 @@ public class PassiveExpiringMap<K, V>
*/
@Override
public V put(final K key, final V value) {
// remove the previous record
removeIfExpired(key, now());
// record expiration time of new entry
final long expirationTime = expiringPolicy.expirationTime(key, value);
expirationMap.put(key, Long.valueOf(expirationTime));

View File

@ -181,6 +181,15 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
assertEquals(3, m.entrySet().size());
}
public void testExpiration() {
validateExpiration(new PassiveExpiringMap<String, String>(500), 500);
validateExpiration(new PassiveExpiringMap<String, String>(1000), 1000);
validateExpiration(new PassiveExpiringMap<>(
new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, String>(500)), 500);
validateExpiration(new PassiveExpiringMap<>(
new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, String>(1, TimeUnit.SECONDS)), 1000);
}
public void testGet() {
final Map<Integer, String> m = makeTestMap();
assertNull(m.get(Integer.valueOf(1)));
@ -208,6 +217,16 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
assertEquals(3, m.keySet().size());
}
public void testPut() {
final Map<Integer, String> m = makeTestMap();
assertNull(m.put(Integer.valueOf(1), "ONE"));
assertEquals("two", m.put(Integer.valueOf(2), "TWO"));
assertNull(m.put(Integer.valueOf(3), "THREE"));
assertEquals("four", m.put(Integer.valueOf(4), "FOUR"));
assertNull(m.put(Integer.valueOf(5), "FIVE"));
assertEquals("six", m.put(Integer.valueOf(6), "SIX"));
}
public void testSize() {
final Map<Integer, String> m = makeTestMap();
assertEquals(3, m.size());
@ -225,15 +244,6 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
assertNull(m.get("a"));
}
public void testExpiration() {
validateExpiration(new PassiveExpiringMap<String, String>(500), 500);
validateExpiration(new PassiveExpiringMap<String, String>(1000), 1000);
validateExpiration(new PassiveExpiringMap<>(
new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, String>(500)), 500);
validateExpiration(new PassiveExpiringMap<>(
new PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<String, String>(1, TimeUnit.SECONDS)), 1000);
}
private void validateExpiration(final Map<String, String> map, final long timeout) {
map.put("a", "b");
@ -247,5 +257,5 @@ public class PassiveExpiringMapTest<K, V> extends AbstractMapTest<K, V> {
assertNull(map.get("a"));
}
}