From 8a004e019a2fe269431b12ec82824fb372e5380f Mon Sep 17 00:00:00 2001 From: thePatmanMI Date: Wed, 19 Sep 2018 09:57:06 -0400 Subject: [PATCH 1/2] updating containsKey method in PassiveExpiringMap correcting the containsKey method to remove all expired entries to match the class and method javadoc statements --- .../org/apache/commons/collections4/map/PassiveExpiringMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/collections4/map/PassiveExpiringMap.java b/src/main/java/org/apache/commons/collections4/map/PassiveExpiringMap.java index d8565e67e0..5ff6479486 100644 --- a/src/main/java/org/apache/commons/collections4/map/PassiveExpiringMap.java +++ b/src/main/java/org/apache/commons/collections4/map/PassiveExpiringMap.java @@ -341,7 +341,7 @@ public void clear() { */ @Override public boolean containsKey(final Object key) { - removeIfExpired(key, now()); + removeAllExpired(now()); return super.containsKey(key); } From 9ad68b0658ad888629cdd1cd6cd2f31ca839bbcf Mon Sep 17 00:00:00 2001 From: thePatmanMI Date: Sat, 22 Sep 2018 08:39:16 -0400 Subject: [PATCH 2/2] amending the containsKey unit test to fail with the old approach and pass with the new changes --- .../map/PassiveExpiringMapTest.java | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java b/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java index f09105c475..f7a7327af7 100644 --- a/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/PassiveExpiringMapTest.java @@ -52,6 +52,22 @@ public long expirationTime(final Integer key, final String value) { } } + /** + * alternate policy that will cause multiples of 3 to expire after 250 milliseconds, all others never expire. + */ + private static class AlternateExpirationPolicy implements ExpirationPolicy { + private static final long serialVersionUID = 1L; + + @Override + public long expirationTime(Integer key, String value) { + if (key.intValue() % 3 != 0) { + return -1; + } + + return 250; + } + } + public static Test suite() { return BulkTest.makeSuite(PassiveExpiringMapTest.class); } @@ -101,6 +117,18 @@ private Map makeTestMap() { return m; } + private PassiveExpiringMap makePassiveExpiringMap() { + final PassiveExpiringMap m = + new PassiveExpiringMap<>(new AlternateExpirationPolicy()); + m.put(Integer.valueOf(1), "one"); + m.put(Integer.valueOf(2), "two"); + m.put(Integer.valueOf(3), "three"); + m.put(Integer.valueOf(4), "four"); + m.put(Integer.valueOf(5), "five"); + m.put(Integer.valueOf(6), "six"); + return m; + } + public void testConstructors() { try { final Map map = null; @@ -128,13 +156,20 @@ public void testConstructors() { } public void testContainsKey() { - final Map m = makeTestMap(); - assertFalse(m.containsKey(Integer.valueOf(1))); - assertFalse(m.containsKey(Integer.valueOf(3))); - assertFalse(m.containsKey(Integer.valueOf(5))); - assertTrue(m.containsKey(Integer.valueOf(2))); - assertTrue(m.containsKey(Integer.valueOf(4))); - assertTrue(m.containsKey(Integer.valueOf(6))); + final PassiveExpiringMap m = makePassiveExpiringMap(); + try { + Thread.sleep(500); + } catch (InterruptedException ignored) { + } + assertEquals(6, m.decorated().size()); + assertTrue(m.containsKey(1)); + assertEquals(4, m.decorated().size()); + assertTrue(m.containsKey(2)); + assertTrue(m.containsKey(4)); + assertTrue(m.containsKey(5)); + assertFalse(m.containsKey(3)); + assertFalse(m.containsKey(6)); + assertEquals(4, m.decorated().size()); } public void testContainsValue() {