From ca6949976defbbcaba0db143f2882acef5648435 Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Sun, 9 Jun 2024 09:50:12 -0700 Subject: [PATCH] delete redundant tests and add missing ones --- .../benmanes/caffeine/cache/EvictionTest.java | 79 +++++++++++-------- .../caffeine/cache/ExpireAfterVarTest.java | 50 ++++++++++++ 2 files changed, 94 insertions(+), 35 deletions(-) diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/EvictionTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/EvictionTest.java index a8c2043c2f..4aac036b00 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/EvictionTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/EvictionTest.java @@ -69,6 +69,7 @@ import com.github.benmanes.caffeine.cache.testing.CheckNoStats; import com.github.benmanes.caffeine.cache.testing.RemovalListeners.RejectingRemovalListener; import com.github.benmanes.caffeine.testing.Int; +import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import com.google.common.collect.Range; @@ -325,6 +326,46 @@ public void getAll_weigherFails_async(AsyncCache cache, CacheContext c .hasSize(context.absentKeys().size()); } + @Test(dataProvider = "caches") + @CacheSpec(population = Population.FULL, + maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO) + public void getAll_weigherFails_newEntries(Cache cache, CacheContext context) { + when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); + assertThrows(IllegalStateException.class, () -> + cache.getAll(context.absentKeys(), keys -> HashBiMap.create(context.original()).inverse())); + assertThat(cache).containsExactlyEntriesIn(context.original()); + if (context.isCaffeine()) { + assertThat(logEvents() + .withMessage("Exception thrown during asynchronous load") + .withThrowable(IllegalStateException.class) + .withLevel(WARN) + .exclusively()) + .hasSize(context.isAsync() ? context.original().size() : 0); + } + } + + @Test(dataProvider = "caches") + @CacheSpec(population = Population.FULL, + maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO) + public void getAll_weigherFails_newEntries_async( + AsyncCache cache, CacheContext context) { + when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); + var future = new CompletableFuture>(); + var result = cache.getAll(context.absentKeys(), (keys, executor) -> future); + future.complete(HashBiMap.create(context.original()).inverse()); + + assertThat(context).stats().failures(1); + assertThat(result).failsWith(CompletionException.class) + .hasCauseThat().isInstanceOf(IllegalStateException.class); + assertThat(cache).containsExactlyEntriesIn(context.original()); + assertThat(logEvents() + .withMessage("Exception thrown during asynchronous load") + .withThrowable(IllegalStateException.class) + .withLevel(WARN) + .exclusively()) + .hasSize(context.original().size()); + } + @Test(dataProvider = "caches") @CacheSpec(population = Population.EMPTY, maximumSize = Maximum.FULL, weigher = CacheWeigher.NEGATIVE) @@ -563,41 +604,9 @@ public void replaceConditionally_weigherFails_absent( public void replaceConditionally_weigherFails_presentKey( Cache cache, CacheContext context, Eviction eviction) { when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); - assertThrows(IllegalStateException.class, () -> cache.asMap().replace( - context.firstKey(), context.original().get(context.firstKey()), context.absentValue())); - assertThat(cache).containsExactlyEntriesIn(context.original()); - assertThat(eviction.weightOf(context.firstKey())).hasValue(1); - } - - @Test(dataProvider = "caches") - @CacheSpec(population = Population.FULL, - maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO) - public void replaceConditionally_weigherFails_presentKey_async( - AsyncCache cache, CacheContext context) { - when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); - var future = new CompletableFuture(); - cache.asMap().replace(context.firstKey(), cache.getIfPresent(context.firstKey()), future); - future.complete(context.absentValue()); - assertThat(context).stats().failures(1); - assertThat(cache).doesNotContainKey(context.absentKey()); - assertThat(logEvents() - .withMessage("Exception thrown during asynchronous load") - .withThrowable(IllegalStateException.class) - .withLevel(WARN) - .exclusively()) - .hasSize(1); - } - - @Test(dataProvider = "caches") - @CacheSpec(population = Population.FULL, - maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO) - public void replaceConditionally_weigherFails_presentKeyAndValue( - Cache cache, CacheContext context, Eviction eviction) { - when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); - assertThrows(IllegalStateException.class, () -> { + assertThrows(IllegalStateException.class, () -> cache.asMap().replace(context.firstKey(), - context.original().get(context.firstKey()), context.absentValue()); - }); + context.original().get(context.firstKey()), context.absentValue())); assertThat(cache).containsExactlyEntriesIn(context.original()); assertThat(eviction.weightOf(context.firstKey())).hasValue(1); } @@ -605,7 +614,7 @@ public void replaceConditionally_weigherFails_presentKeyAndValue( @Test(dataProvider = "caches") @CacheSpec(population = Population.FULL, maximumSize = Maximum.FULL, weigher = CacheWeigher.MOCKITO) - public void replaceConditionally_weigherFails_presentKeyAndValue_async( + public void replaceConditionally_weigherFails_presentKey_async( AsyncCache cache, CacheContext context) { when(context.weigher().weigh(any(), any())).thenThrow(IllegalStateException.class); var future = new CompletableFuture(); diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterVarTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterVarTest.java index acfe70b48a..e6e1fcce5f 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterVarTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterVarTest.java @@ -78,6 +78,7 @@ import com.github.benmanes.caffeine.cache.testing.CheckNoStats; import com.github.benmanes.caffeine.testing.ConcurrentTestHarness; import com.github.benmanes.caffeine.testing.Int; +import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import com.google.common.testing.SerializableTester; @@ -412,6 +413,14 @@ public void getAll_expiryFails(Cache cache, CacheContext context) { cache.getAll(context.absentKeys(), keys -> Maps.toMap(keys, Int::negate))); context.ticker().advance(Duration.ofHours(-1)); assertThat(cache).containsExactlyEntriesIn(context.original()); + if (context.isCaffeine()) { + assertThat(logEvents() + .withMessage("Exception thrown during asynchronous load") + .withThrowable(ExpirationException.class) + .withLevel(WARN) + .exclusively()) + .hasSize(context.isAsync() ? context.absentKeys().size() : 0); + } } @Test(dataProvider = "caches") @@ -434,6 +443,47 @@ public void getAll_expiryFails_async(AsyncCache cache, CacheContext co .hasSize(context.absentKeys().size()); } + @Test(dataProvider = "caches") + @CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO) + public void getAll_expiryFails_newEntries(Cache cache, CacheContext context) { + context.ticker().advance(Duration.ofHours(1)); + when(context.expiry().expireAfterCreate(any(), any(), anyLong())) + .thenThrow(ExpirationException.class); + assertThrows(ExpirationException.class, () -> + cache.getAll(context.absentKeys(), keys -> HashBiMap.create(context.original()).inverse())); + context.ticker().advance(Duration.ofHours(-1)); + assertThat(cache).containsExactlyEntriesIn(context.original()); + if (context.isCaffeine()) { + assertThat(logEvents() + .withMessage("Exception thrown during asynchronous load") + .withThrowable(ExpirationException.class) + .withLevel(WARN) + .exclusively()) + .hasSize(context.isAsync() ? context.original().size() : 0); + } + } + + @Test(dataProvider = "caches") + @CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO) + public void getAll_expiryFails_newEntries_async( + AsyncCache cache, CacheContext context) { + context.ticker().advance(Duration.ofHours(1)); + when(context.expiry().expireAfterCreate(any(), any(), anyLong())) + .thenThrow(ExpirationException.class); + var future = new CompletableFuture>(); + var result = cache.getAll(context.absentKeys(), (keys, executor) -> future); + future.complete(HashBiMap.create(context.original()).inverse()); + assertThat(result).failsWith(CompletionException.class) + .hasCauseThat().isInstanceOf(ExpirationException.class); + assertThat(cache).containsExactlyEntriesIn(context.original()); + assertThat(logEvents() + .withMessage("Exception thrown during asynchronous load") + .withThrowable(ExpirationException.class) + .withLevel(WARN) + .exclusively()) + .hasSize(context.original().size()); + } + @Test(dataProvider = "caches") @CacheSpec(population = Population.FULL, expiry = CacheExpiry.MOCKITO) public void getAllPresent_expiryFails(Cache cache, CacheContext context) {