diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsMapTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsMapTest.java index 19b30cd400..25f3d9d1d6 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsMapTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsMapTest.java @@ -58,6 +58,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -1095,31 +1096,32 @@ public void computeIfAbsent_nullValue(Map map, CacheContext co assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = IllegalStateException.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void computeIfAbsent_recursive(Map map, CacheContext context) { Function mappingFunction = new Function() { @Override public Integer apply(Integer key) { return map.computeIfAbsent(key, this); } }; - map.computeIfAbsent(context.absentKey(), mappingFunction); + try { + map.computeIfAbsent(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = IllegalStateException.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void computeIfAbsent_pingpong(Map map, CacheContext context) { Function mappingFunction = new Function() { @Override public Integer apply(Integer key) { - Integer value = context.original().get(key); - return map.computeIfAbsent(value, this); + return map.computeIfAbsent(-key, this); } }; - map.computeIfAbsent(context.absentKey(), mappingFunction); + try { + map.computeIfAbsent(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } @Test(dataProvider = "caches") @@ -1358,10 +1360,8 @@ public void compute_remove(Map map, CacheContext context) { assertThat(map, hasRemovalNotifications(context, count, RemovalCause.EXPLICIT)); } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = StackOverflowError.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void compute_recursive(Map map, CacheContext context) { BiFunction mappingFunction = new BiFunction() { @@ -1369,34 +1369,40 @@ public void compute_recursive(Map map, CacheContext context) { return map.compute(key, this); } }; - map.compute(context.absentKey(), mappingFunction); + try { + map.compute(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, - removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = StackOverflowError.class) + @Test(dataProvider = "caches") + @CacheSpec(population = Population.EMPTY, implementation = Implementation.Caffeine) public void compute_pingpong(Map map, CacheContext context) { + var key1 = 1; + var key2 = 2; BiFunction mappingFunction = new BiFunction() { @Override public Integer apply(Integer key, Integer value) { - return map.computeIfPresent(context.lastKey(), this); + return map.compute((key == key1) ? key2 : key1, this); } }; - map.computeIfPresent(context.firstKey(), mappingFunction); + try { + map.compute(key1, mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } @CacheSpec @Test(dataProvider = "caches") public void compute_error(Map map, CacheContext context) { try { - map.compute(context.absentKey(), (key, value) -> { throw new Error(); }); - } catch (Error e) {} + map.compute(context.absentKey(), (key, value) -> { throw new IllegalStateException(); }); + Assert.fail(); + } catch (IllegalStateException e) { /* ignored */ } assertThat(map, is(equalTo(context.original()))); assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1))); - assertThat(map.computeIfPresent(context.absentKey(), (k, v) -> -k), is(nullValue())); + assertThat(map.compute(context.absentKey(), (k, v) -> -k), is(-context.absentKey())); } @CheckNoWriter diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncAsMapTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncAsMapTest.java index cc299c7914..137e68c339 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncAsMapTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncAsMapTest.java @@ -64,6 +64,7 @@ import com.github.benmanes.caffeine.cache.testing.CacheContext; import com.github.benmanes.caffeine.cache.testing.CacheProvider; import com.github.benmanes.caffeine.cache.testing.CacheSpec; +import com.github.benmanes.caffeine.cache.testing.CacheSpec.Implementation; import com.github.benmanes.caffeine.cache.testing.CacheSpec.Listener; import com.github.benmanes.caffeine.cache.testing.CacheSpec.Population; import com.github.benmanes.caffeine.cache.testing.CacheValidationListener; @@ -750,10 +751,8 @@ public void computeIfAbsent_nullValue(AsyncCache cache, CacheC assertThat(cache.asMap().size(), is(context.original().size())); } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = IllegalStateException.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void computeIfAbsent_recursive(AsyncCache cache, CacheContext context) { Function> mappingFunction = new Function>() { @@ -761,22 +760,25 @@ public void computeIfAbsent_recursive(AsyncCache cache, CacheC return cache.asMap().computeIfAbsent(key, this); } }; - cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction); + try { + cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = IllegalStateException.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void computeIfAbsent_pingpong(AsyncCache cache, CacheContext context) { Function> mappingFunction = new Function>() { @Override public CompletableFuture apply(Integer key) { - Integer value = context.original().get(key); - return cache.asMap().computeIfAbsent(value, this); + return cache.asMap().computeIfAbsent(-key, this); } }; - cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction); + try { + cache.asMap().computeIfAbsent(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } @Test(dataProvider = "caches") @@ -970,10 +972,8 @@ public void compute_remove(AsyncCache cache, CacheContext cont assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPLICIT)); } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = StackOverflowError.class) + @Test(dataProvider = "caches") + @CacheSpec(implementation = Implementation.Caffeine) public void compute_recursive(AsyncCache cache, CacheContext context) { BiFunction, CompletableFuture> mappingFunction = new BiFunction, CompletableFuture>() { @@ -982,26 +982,30 @@ public void compute_recursive(AsyncCache cache, CacheContext c return cache.asMap().compute(key, this); } }; - cache.asMap().compute(context.absentKey(), mappingFunction); + try { + cache.asMap().compute(context.absentKey(), mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } - // FIXME: Requires JDK8 release with JDK-8062841 fix - @CheckNoStats - @CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, - removalListener = { Listener.DEFAULT, Listener.REJECTING }) - @Test(enabled = false, dataProvider = "caches", expectedExceptions = StackOverflowError.class) + @CacheSpec(population = Population.EMPTY, implementation = Implementation.Caffeine) + @Test(dataProvider = "caches") public void compute_pingpong(AsyncCache cache, CacheContext context) { + var key1 = 1; + var key2 = 2; BiFunction, CompletableFuture> mappingFunction = new BiFunction, CompletableFuture>() { @Override public CompletableFuture apply( Integer key, CompletableFuture value) { - return cache.asMap().computeIfPresent(context.lastKey(), this); + return cache.asMap().compute((key == key1) ? key2 : key1, this); } }; - cache.asMap().computeIfPresent(context.firstKey(), mappingFunction); + try { + cache.asMap().compute(key1, mappingFunction); + Assert.fail(); + } catch (StackOverflowError | IllegalStateException e) { /* ignored */ } } - @CheckNoStats @Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void compute_error(AsyncCache cache, CacheContext context) { @@ -1014,8 +1018,8 @@ public void compute_error(AsyncCache cache, CacheContext conte assertThat(context, both(hasMissCount(0)).and(hasHitCount(0))); assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0))); - assertThat(cache.asMap().computeIfPresent(context.absentKey(), - (k, v) -> CompletableFuture.completedFuture(-k)), is(nullValue())); + var future = CompletableFuture.completedFuture(-context.absentKey()); + assertThat(cache.asMap().compute(context.absentKey(), (k, v) -> future), is(future)); } @CheckNoStats