diff options
author | jonmv <venstad@gmail.com> | 2022-10-26 11:31:42 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-10-26 11:31:42 +0200 |
commit | 5246967ce8b8ac4a95c944be21a0f473175c4b2a (patch) | |
tree | 50ad2cbec35ab32d725f94473020d2a578ab5f0f /vespajlib | |
parent | 73ca78767d08828859f112bc6f23997c113617cf (diff) |
Test caching negative results, and invoke callback for those too
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/collections/MethodCache.java | 2 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java | 13 |
2 files changed, 13 insertions, 2 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java index ead1d109fba..d0da7617b58 100644 --- a/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java +++ b/vespajlib/src/main/java/com/yahoo/collections/MethodCache.java @@ -47,7 +47,7 @@ public final class MethodCache { method = lookupMethod(object); pair = new Pair<>(object.getClass(), method); cache.put(object.getClass().getName(), new WeakReference<>(pair)); - if (method != null && onPut != null) + if (onPut != null) onPut.accept(object.getClass().getName()); } return method; diff --git a/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java index 7b3d3e98cec..02efd10e91d 100644 --- a/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java +++ b/vespajlib/src/test/java/com/yahoo/collections/MethodCacheTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -60,11 +61,21 @@ class MethodCacheTest { cachedCustomMethod.invoke(customDummy); assertThrows(IllegalArgumentException.class, () -> applicationMethod.invoke(customDummy)); assertThrows(IllegalArgumentException.class, () -> customMethod.invoke(applicationDummy)); + + Object noDummy = new NoDummy(); + Method noMethod = methods.get(noDummy, __ -> updatedCache.set(true)); + assertTrue(updatedCache.getAndSet(false), "cache was updated"); + assertNull(noMethod); + + Method cachedNoMethod = methods.get(noDummy, __ -> updatedCache.set(true)); + assertFalse(updatedCache.getAndSet(false), "cache was updated"); + assertNull(cachedNoMethod); } } + public static class NoDummy implements Cloneable { } + public static class Dummy implements Cloneable { - public Dummy() { } public Object clone() throws CloneNotSupportedException { return super.clone(); } } |