Skip to content

Commit

Permalink
[COLLECTIONS-703] The PassiveExpiringMap#put() method should return the
Browse files Browse the repository at this point in the history
previous record only if not expired.
  • Loading branch information
ttulka authored and garydgregory committed Nov 23, 2018
1 parent c15be53 commit 26bebda
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Expand Up @@ -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">
Expand Down
Expand Up @@ -430,6 +430,9 @@ private long now() {
*/
@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));
Expand Down
Expand Up @@ -181,6 +181,15 @@ public void testEntrySet() {
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)));
Expand Down Expand Up @@ -208,6 +217,16 @@ public void testKeySet() {
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());
Expand All @@ -225,15 +244,6 @@ public void testZeroTimeToLive() {
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");

Expand All @@ -247,5 +257,5 @@ private void validateExpiration(final Map<String, String> map, final long timeou

assertNull(map.get("a"));
}

}

0 comments on commit 26bebda

Please sign in to comment.