From faddb1483ea251e6fb82d93f67533ceb5b8187db Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Sat, 2 Jan 2021 01:31:25 -0800 Subject: [PATCH] Split Policy.Expiration into fixed and refresh interfaces --- .../caffeine/cache/BoundedLocalCache.java | 40 ++---- .../benmanes/caffeine/cache/Policy.java | 101 +++++++++++++- .../caffeine/cache/UnboundedLocalCache.java | 6 +- .../benmanes/caffeine/cache/CaffeineTest.java | 18 +-- .../caffeine/cache/ExpireAfterAccessTest.java | 38 +++--- .../caffeine/cache/ExpireAfterWriteTest.java | 38 +++--- .../caffeine/cache/RefreshAfterWriteTest.java | 124 +----------------- .../caffeine/cache/testing/CacheProvider.java | 17 ++- .../testing/CacheValidationListener.java | 4 +- .../cache/testing/ExpireAfterAccess.java | 4 +- .../cache/testing/ExpireAfterWrite.java | 4 +- .../cache/testing/GuavaCacheFromContext.java | 6 +- .../cache/testing/RefreshAfterWrite.java | 4 +- 13 files changed, 189 insertions(+), 215 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java index 52447a39d0..743fa700c0 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java @@ -3416,9 +3416,9 @@ static final class BoundedPolicy implements Policy { final boolean isWeighted; @Nullable Optional> eviction; - @Nullable Optional> refreshes; - @Nullable Optional> afterWrite; - @Nullable Optional> afterAccess; + @Nullable Optional> refreshes; + @Nullable Optional> afterWrite; + @Nullable Optional> afterAccess; @Nullable Optional> variable; BoundedPolicy(BoundedLocalCache cache, Function transformer, boolean isWeighted) { @@ -3442,7 +3442,7 @@ static final class BoundedPolicy implements Policy { ? (eviction == null) ? (eviction = Optional.of(new BoundedEviction())) : eviction : Optional.empty(); } - @Override public Optional> expireAfterAccess() { + @Override public Optional> expireAfterAccess() { if (!cache.expiresAfterAccess()) { return Optional.empty(); } @@ -3450,7 +3450,7 @@ static final class BoundedPolicy implements Policy { ? (afterAccess = Optional.of(new BoundedExpireAfterAccess())) : afterAccess; } - @Override public Optional> expireAfterWrite() { + @Override public Optional> expireAfterWrite() { if (!cache.expiresAfterWrite()) { return Optional.empty(); } @@ -3466,7 +3466,7 @@ static final class BoundedPolicy implements Policy { ? (variable = Optional.of(new BoundedVarExpiration())) : variable; } - @Override public Optional> refreshAfterWrite() { + @Override public Optional> refreshAfterWrite() { if (!cache.refreshAfterWrite()) { return Optional.empty(); } @@ -3529,7 +3529,7 @@ final class BoundedEviction implements Eviction { } @SuppressWarnings("PreferJavaTimeOverload") - final class BoundedExpireAfterAccess implements Expiration { + final class BoundedExpireAfterAccess implements FixedExpiration { @Override public OptionalLong ageOf(K key, TimeUnit unit) { requireNonNull(key); requireNonNull(unit); @@ -3560,7 +3560,7 @@ final class BoundedExpireAfterAccess implements Expiration { } @SuppressWarnings("PreferJavaTimeOverload") - final class BoundedExpireAfterWrite implements Expiration { + final class BoundedExpireAfterWrite implements FixedExpiration { @Override public OptionalLong ageOf(K key, TimeUnit unit) { requireNonNull(key); requireNonNull(unit); @@ -3666,7 +3666,7 @@ public long expireAfterRead(K key, V value, long currentTime, long currentDurati } @SuppressWarnings("PreferJavaTimeOverload") - final class BoundedRefreshAfterWrite implements Expiration { + final class BoundedRefreshAfterWrite implements FixedRefresh { @Override public OptionalLong ageOf(K key, TimeUnit unit) { requireNonNull(key); requireNonNull(unit); @@ -3680,32 +3680,14 @@ final class BoundedRefreshAfterWrite implements Expiration { ? OptionalLong.empty() : OptionalLong.of(unit.convert(age, TimeUnit.NANOSECONDS)); } - @Override public long getExpiresAfter(TimeUnit unit) { + @Override public long getRefreshesAfter(TimeUnit unit) { return unit.convert(cache.refreshAfterWriteNanos(), TimeUnit.NANOSECONDS); } - @Override public void setExpiresAfter(long duration, TimeUnit unit) { + @Override public void setRefreshesAfter(long duration, TimeUnit unit) { requireArgument(duration >= 0); cache.setRefreshAfterWriteNanos(unit.toNanos(duration)); cache.scheduleAfterWrite(); } - @SuppressWarnings("PMD.SimplifiedTernary") // false positive (#1424) - @Override public Map oldest(int limit) { - return cache.expiresAfterWrite() - ? expireAfterWrite().get().oldest(limit) - : sortedByWriteTime(limit, /* ascending */ true); - } - @SuppressWarnings("PMD.SimplifiedTernary") // false positive (#1424) - @Override public Map youngest(int limit) { - return cache.expiresAfterWrite() - ? expireAfterWrite().get().youngest(limit) - : sortedByWriteTime(limit, /* ascending */ false); - } - Map sortedByWriteTime(int limit, boolean ascending) { - Comparator> comparator = Comparator.comparingLong(Node::getWriteTime); - Iterator> iterator = cache.data.values().stream().parallel().sorted( - ascending ? comparator : comparator.reversed()).limit(limit).iterator(); - return cache.fixedSnapshot(() -> iterator, limit, transformer); - } } } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Policy.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Policy.java index 1d4b15966f..5b02e869b9 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Policy.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Policy.java @@ -86,7 +86,7 @@ default V getIfPresentQuietly(@NonNull @CompatibleWith("K") Object key) { * used */ @NonNull - Optional> expireAfterAccess(); + Optional> expireAfterAccess(); /** * Returns access to perform operations based on the time-to-live expiration policy. This policy @@ -100,7 +100,7 @@ default V getIfPresentQuietly(@NonNull @CompatibleWith("K") Object key) { * used */ @NonNull - Optional> expireAfterWrite(); + Optional> expireAfterWrite(); /** * Returns access to perform operations based on the variable expiration policy. This policy @@ -129,7 +129,7 @@ default Optional> expireVariably() { * @return access to low-level operations for this cache if a time-to-live refresh policy is used */ @NonNull - Optional> refreshAfterWrite(); + Optional> refreshAfterWrite(); /** The low-level operations for a cache with a size-based eviction policy. */ interface Eviction { @@ -223,7 +223,7 @@ default OptionalInt weightOf(@NonNull K key) { } /** The low-level operations for a cache with a fixed expiration policy. */ - interface Expiration { // To be renamed FixedExpiration in version 3.0.0 + interface FixedExpiration { /** * Returns the age of the entry based on the expiration policy. The entry's age is the cache's @@ -515,4 +515,97 @@ default void put(@NonNull K key, @NonNull V value, @NonNull Duration duration) { @NonNull Map<@NonNull K, @NonNull V> youngest(@NonNegative int limit); } + + /** The low-level operations for a cache with a fixed refresh policy. */ + interface FixedRefresh { + + /** + * Returns the age of the entry based on the refresh policy. The entry's age is the cache's + * estimate of the amount of time since the entry's refresh time was last reset. + *

+ * An expiration policy uses the age to determine if an entry is fresh or stale by comparing it + * to the freshness lifetime. This is calculated as {@code fresh = freshnessLifetime > age} + * where {@code freshnessLifetime = expires - currentTime}. + *

+ * This method is scheduled for removal in version 3.0.0. + * + * @param key the key for the entry being queried + * @param unit the unit that {@code age} is expressed in + * @return the age if the entry is present in the cache + */ + @NonNull + OptionalLong ageOf(@NonNull K key, @NonNull TimeUnit unit); + + /** + * Returns the age of the entry based on the expiration policy. The entry's age is the cache's + * estimate of the amount of time since the entry's expiration was last reset. + *

+ * An expiration policy uses the age to determine if an entry is fresh or stale by comparing it + * to the freshness lifetime. This is calculated as {@code fresh = freshnessLifetime > age} + * where {@code freshnessLifetime = expires - currentTime}. + * + * @param key the key for the entry being queried + * @return the age if the entry is present in the cache + */ + @NonNull + default Optional ageOf(@NonNull K key) { + // This method will be abstract in version 3.0.0 + OptionalLong duration = ageOf(key, TimeUnit.NANOSECONDS); + return duration.isPresent() + ? Optional.of(Duration.ofNanos(duration.getAsLong())) + : Optional.empty(); + } + + /** + * Returns the fixed duration used to determine if an entry should be eligible for reloading due + * to elapsing this time bound. An entry is considered fresh if its age is less than this + * duration, and stale otherwise. The refresh policy determines when the entry's age is + * reset. + *

+ * This method is scheduled for removal in version 3.0.0. + * + * @param unit the unit that duration is expressed in + * @return the length of time after which an entry is eligible to be reloaded + */ + @NonNegative + long getRefreshesAfter(@NonNull TimeUnit unit); + + /** + * Returns the fixed duration used to determine if an entry should be eligible for reloading due + * to elapsing this time bound. An entry is considered fresh if its age is less than this + * duration, and stale otherwise. The refresh policy determines when the entry's age is + * reset. + * + * @return the length of time after which an entry is eligible to be reloaded + */ + @NonNull + default Duration getRefreshesAfter() { + // This method will be abstract in version 3.0.0 + return Duration.ofNanos(getRefreshesAfter(TimeUnit.NANOSECONDS)); + } + + /** + * Specifies that each entry should be eligible for reloading once a fixed duration has elapsed. + * The refresh policy determines when the entry's age is reset. + *

+ * This method is scheduled for removal in version 3.0.0. + * + * @param duration the length of time after which an entry is eligible to be reloaded + * @param unit the unit that {@code duration} is expressed in + * @throws IllegalArgumentException if {@code duration} is negative + */ + void setRefreshesAfter(@NonNegative long duration, @NonNull TimeUnit unit); + + /** + * Specifies that each entry should be eligible for reloading once a fixed duration has elapsed. + * The refresh policy determines when the entry's age is reset. + * + * @param duration the length of time after which an entry is eligible to be reloaded + * @throws IllegalArgumentException if {@code duration} is negative + */ + default void setRefreshesAfter(@NonNull Duration duration) { + // This method will be abstract in version 3.0.0 + setRefreshesAfter(duration.toNanos(), TimeUnit.NANOSECONDS); + } + } } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java index d089b99af3..7004b9c37b 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/UnboundedLocalCache.java @@ -906,13 +906,13 @@ static final class UnboundedPolicy implements Policy { @Override public Optional> eviction() { return Optional.empty(); } - @Override public Optional> expireAfterAccess() { + @Override public Optional> expireAfterAccess() { return Optional.empty(); } - @Override public Optional> expireAfterWrite() { + @Override public Optional> expireAfterWrite() { return Optional.empty(); } - @Override public Optional> refreshAfterWrite() { + @Override public Optional> refreshAfterWrite() { return Optional.empty(); } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineTest.java index 4a3ad5d01b..306c9af36f 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/CaffeineTest.java @@ -34,7 +34,7 @@ import org.testng.annotations.Test; import com.github.benmanes.caffeine.cache.Policy.Eviction; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; import com.github.benmanes.caffeine.cache.stats.StatsCounter; import com.google.common.testing.FakeTicker; import com.google.common.util.concurrent.MoreExecutors; @@ -324,7 +324,7 @@ public void expireAfterAccess_twice() { public void expireAfterAccess_small() { Caffeine builder = Caffeine.newBuilder().expireAfterAccess(0, TimeUnit.MILLISECONDS); assertThat(builder.expireAfterAccessNanos, is(0L)); - Expiration expiration = builder.build().policy().expireAfterAccess().get(); + FixedExpiration expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); } @@ -333,7 +333,7 @@ public void expireAfterAccess_large() { Caffeine builder = Caffeine.newBuilder() .expireAfterAccess(Integer.MAX_VALUE, TimeUnit.NANOSECONDS); assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE)); - Expiration expiration = builder.build().policy().expireAfterAccess().get(); + FixedExpiration expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); } @@ -359,7 +359,7 @@ public void expireAfterAccess_duration_twice() { public void expireAfterAccess_duration_small() { Caffeine builder = Caffeine.newBuilder().expireAfterAccess(Duration.ofMillis(0)); assertThat(builder.expireAfterAccessNanos, is(0L)); - Expiration expiration = builder.build().policy().expireAfterAccess().get(); + FixedExpiration expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); } @@ -368,7 +368,7 @@ public void expireAfterAccess_duration_large() { Caffeine builder = Caffeine.newBuilder() .expireAfterAccess(Duration.ofNanos(Integer.MAX_VALUE)); assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE)); - Expiration expiration = builder.build().policy().expireAfterAccess().get(); + FixedExpiration expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); } @@ -394,7 +394,7 @@ public void expireAfterWrite_twice() { public void expireAfterWrite_small() { Caffeine builder = Caffeine.newBuilder().expireAfterWrite(0, TimeUnit.MILLISECONDS); assertThat(builder.expireAfterWriteNanos, is(0L)); - Expiration expiration = builder.build().policy().expireAfterWrite().get(); + FixedExpiration expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); } @@ -403,7 +403,7 @@ public void expireAfterWrite_large() { Caffeine builder = Caffeine.newBuilder() .expireAfterWrite(Integer.MAX_VALUE, TimeUnit.NANOSECONDS); assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE)); - Expiration expiration = builder.build().policy().expireAfterWrite().get(); + FixedExpiration expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); } @@ -429,7 +429,7 @@ public void expireAfterWrite_duration_twice() { public void expireAfterWrite_duration_small() { Caffeine builder = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(0)); assertThat(builder.expireAfterWriteNanos, is(0L)); - Expiration expiration = builder.build().policy().expireAfterWrite().get(); + FixedExpiration expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); } @@ -438,7 +438,7 @@ public void expireAfterWrite_duration_large() { Caffeine builder = Caffeine.newBuilder() .expireAfterWrite(Duration.ofNanos(Integer.MAX_VALUE)); assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE)); - Expiration expiration = builder.build().policy().expireAfterWrite().get(); + FixedExpiration expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterAccessTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterAccessTest.java index bf12831dd3..c4553b8d1a 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterAccessTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterAccessTest.java @@ -40,7 +40,7 @@ import org.testng.annotations.Listeners; import org.testng.annotations.Test; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; import com.github.benmanes.caffeine.cache.testing.CacheContext; import com.github.benmanes.caffeine.cache.testing.CacheProvider; import com.github.benmanes.caffeine.cache.testing.CacheSpec; @@ -233,21 +233,21 @@ public void getIfPresentQuietly(Cache cache, CacheContext cont @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void getExpiresAfter(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.getExpiresAfter(TimeUnit.MINUTES), is(1L)); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void getExpiresAfter_duration(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.getExpiresAfter(), is(Duration.ofMinutes(1L))); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void setExpiresAfter(Cache cache, CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.setExpiresAfter(2, TimeUnit.MINUTES); assertThat(expireAfterAccess.getExpiresAfter(TimeUnit.MINUTES), is(2L)); @@ -259,7 +259,7 @@ public void setExpiresAfter(Cache cache, CacheContext context, @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void setExpiresAfter_duration(Cache cache, CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.setExpiresAfter(Duration.ofMinutes(2L)); assertThat(expireAfterAccess.getExpiresAfter(), is(Duration.ofMinutes(2L))); @@ -272,7 +272,7 @@ public void setExpiresAfter_duration(Cache cache, CacheContext @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void ageOf(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(0L)); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(expireAfterAccess.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(30L)); @@ -284,7 +284,7 @@ public void ageOf(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void ageOf_duration(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.ageOf(context.firstKey()), is(Optional.of(Duration.ZERO))); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(expireAfterAccess.ageOf(context.firstKey()), @@ -298,21 +298,21 @@ public void ageOf_duration(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void oldest_unmodifiable(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.oldest(Integer.MAX_VALUE).clear();; } @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void oldest_negative(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.oldest(-1); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void oldest_zero(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.oldest(0), is(emptyMap())); } @@ -320,7 +320,7 @@ public void oldest_zero(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expireAfterAccess = Expire.ONE_MINUTE) public void oldest_partial(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { int count = (int) context.initialSize() / 2; assertThat(expireAfterAccess.oldest(count).size(), is(count)); } @@ -330,7 +330,7 @@ public void oldest_partial(CacheContext context, population = {Population.PARTIAL, Population.FULL}, expireAfterAccess = Expire.ONE_MINUTE, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void oldest_order(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { Map oldest = expireAfterAccess.oldest(Integer.MAX_VALUE); assertThat(oldest.keySet(), contains(context.original().keySet().toArray(new Integer[0]))); } @@ -338,7 +338,7 @@ public void oldest_order(CacheContext context, @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void oldest_snapshot(Cache cache, CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { Map oldest = expireAfterAccess.oldest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(oldest, is(equalTo(context.original()))); @@ -349,21 +349,21 @@ public void oldest_snapshot(Cache cache, CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void youngest_unmodifiable(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.youngest(Integer.MAX_VALUE).clear();; } @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void youngest_negative(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { expireAfterAccess.youngest(-1); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void youngest_zero(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { assertThat(expireAfterAccess.youngest(0), is(emptyMap())); } @@ -371,7 +371,7 @@ public void youngest_zero(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expireAfterAccess = Expire.ONE_MINUTE) public void youngest_partial(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { int count = (int) context.initialSize() / 2; assertThat(expireAfterAccess.youngest(count).size(), is(count)); } @@ -381,7 +381,7 @@ public void youngest_partial(CacheContext context, population = {Population.PARTIAL, Population.FULL}, expireAfterAccess = Expire.ONE_MINUTE, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void youngest_order(CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { Map youngest = expireAfterAccess.youngest(Integer.MAX_VALUE); Set keys = new LinkedHashSet<>(ImmutableList.copyOf(youngest.keySet()).reverse()); assertThat(keys, contains(context.original().keySet().toArray(new Integer[0]))); @@ -390,7 +390,7 @@ public void youngest_order(CacheContext context, @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterAccess = Expire.ONE_MINUTE) public void youngest_snapshot(Cache cache, CacheContext context, - @ExpireAfterAccess Expiration expireAfterAccess) { + @ExpireAfterAccess FixedExpiration expireAfterAccess) { Map youngest = expireAfterAccess.youngest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(youngest, is(equalTo(context.original()))); diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java index d835677541..85b708ab69 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java @@ -38,7 +38,7 @@ import org.testng.annotations.Listeners; import org.testng.annotations.Test; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; import com.github.benmanes.caffeine.cache.testing.CacheContext; import com.github.benmanes.caffeine.cache.testing.CacheProvider; import com.github.benmanes.caffeine.cache.testing.CacheSpec; @@ -223,7 +223,7 @@ public void getIfPresentQuietly(Cache cache, CacheContext cont @CacheSpec(implementation = Implementation.Caffeine, mustExpireWithAnyOf = AFTER_WRITE, expireAfterWrite = Expire.ONE_MINUTE) public void getExpiresAfter(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.getExpiresAfter(TimeUnit.MINUTES), is(1L)); } @@ -231,7 +231,7 @@ public void getExpiresAfter(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, mustExpireWithAnyOf = AFTER_WRITE, expireAfterWrite = Expire.ONE_MINUTE) public void getExpiresAfter_duration(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.getExpiresAfter(), is(Duration.ofMinutes(1L))); } @@ -239,7 +239,7 @@ public void getExpiresAfter_duration(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, mustExpireWithAnyOf = AFTER_WRITE, expireAfterWrite = Expire.ONE_MINUTE) public void setExpiresAfter(Cache cache, CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.setExpiresAfter(2, TimeUnit.MINUTES); assertThat(expireAfterWrite.getExpiresAfter(TimeUnit.MINUTES), is(2L)); @@ -252,7 +252,7 @@ public void setExpiresAfter(Cache cache, CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, mustExpireWithAnyOf = AFTER_WRITE, expireAfterWrite = Expire.ONE_MINUTE) public void setExpiresAfter_duration(Cache cache, CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.setExpiresAfter(Duration.ofMinutes(2)); assertThat(expireAfterWrite.getExpiresAfter(), is(Duration.ofMinutes(2L))); @@ -265,7 +265,7 @@ public void setExpiresAfter_duration(Cache cache, CacheContext @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void ageOf(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(0L)); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(expireAfterWrite.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(30L)); @@ -277,7 +277,7 @@ public void ageOf(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void ageOf_duration(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.ageOf(context.firstKey()), is(Optional.of(Duration.ZERO))); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(expireAfterWrite.ageOf(context.firstKey()), @@ -291,21 +291,21 @@ public void ageOf_duration(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void oldest_unmodifiable(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.oldest(Integer.MAX_VALUE).clear(); } @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void oldest_negative(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.oldest(-1); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) public void oldest_zero(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.oldest(0), is(emptyMap())); } @@ -313,7 +313,7 @@ public void oldest_zero(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expireAfterWrite = Expire.ONE_MINUTE) public void oldest_partial(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { int count = (int) context.initialSize() / 2; assertThat(expireAfterWrite.oldest(count).size(), is(count)); } @@ -323,7 +323,7 @@ public void oldest_partial(CacheContext context, population = {Population.PARTIAL, Population.FULL}, expireAfterWrite = Expire.ONE_MINUTE, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void oldest_order(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { Map oldest = expireAfterWrite.oldest(Integer.MAX_VALUE); assertThat(oldest.keySet(), contains(context.original().keySet().toArray(new Integer[0]))); } @@ -331,7 +331,7 @@ public void oldest_order(CacheContext context, @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) public void oldest_snapshot(Cache cache, CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { Map oldest = expireAfterWrite.oldest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(oldest, is(equalTo(context.original()))); @@ -342,21 +342,21 @@ public void oldest_snapshot(Cache cache, CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void youngest_unmodifiable(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.youngest(Integer.MAX_VALUE).clear();; } @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void youngest_negative(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { expireAfterWrite.youngest(-1); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) public void youngest_zero(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { assertThat(expireAfterWrite.youngest(0), is(emptyMap())); } @@ -364,7 +364,7 @@ public void youngest_zero(CacheContext context, @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, expireAfterWrite = Expire.ONE_MINUTE) public void youngest_partial(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { int count = (int) context.initialSize() / 2; assertThat(expireAfterWrite.youngest(count).size(), is(count)); } @@ -374,7 +374,7 @@ public void youngest_partial(CacheContext context, population = {Population.PARTIAL, Population.FULL}, expireAfterWrite = Expire.ONE_MINUTE, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void youngest_order(CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { Map youngest = expireAfterWrite.youngest(Integer.MAX_VALUE); Set keys = new LinkedHashSet<>(ImmutableList.copyOf(youngest.keySet()).reverse()); assertThat(keys, contains(Iterables.toArray(keys, Integer.class))); @@ -383,7 +383,7 @@ public void youngest_order(CacheContext context, @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, expireAfterWrite = Expire.ONE_MINUTE) public void youngest_snapshot(Cache cache, CacheContext context, - @ExpireAfterWrite Expiration expireAfterWrite) { + @ExpireAfterWrite FixedExpiration expireAfterWrite) { Map youngest = expireAfterWrite.youngest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(youngest, is(equalTo(context.original()))); diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/RefreshAfterWriteTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/RefreshAfterWriteTest.java index 32b48ccf42..48d8f953b7 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/RefreshAfterWriteTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/RefreshAfterWriteTest.java @@ -18,23 +18,17 @@ import static com.github.benmanes.caffeine.cache.testing.RemovalListenerVerifier.verifyRemovalListener; import static com.github.benmanes.caffeine.cache.testing.StatsVerifier.verifyStats; import static com.github.benmanes.caffeine.testing.Awaits.await; -import static com.github.benmanes.caffeine.testing.IsEmptyMap.emptyMap; import static com.github.benmanes.caffeine.testing.IsFutureValue.futureOf; import static java.util.stream.Collectors.toList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -45,11 +39,10 @@ import org.testng.annotations.Listeners; import org.testng.annotations.Test; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedRefresh; 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.Advance; import com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor; import com.github.benmanes.caffeine.cache.testing.CacheSpec.Compute; import com.github.benmanes.caffeine.cache.testing.CacheSpec.Expire; @@ -366,23 +359,23 @@ public void invalidate(CacheContext context) { @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) public void getExpiresAfter(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - assertThat(refreshAfterWrite.getExpiresAfter(TimeUnit.MINUTES), is(1L)); + @RefreshAfterWrite FixedRefresh refreshAfterWrite) { + assertThat(refreshAfterWrite.getRefreshesAfter(TimeUnit.MINUTES), is(1L)); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) public void setExpiresAfter(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - refreshAfterWrite.setExpiresAfter(2, TimeUnit.MINUTES); - assertThat(refreshAfterWrite.getExpiresAfter(TimeUnit.MINUTES), is(2L)); + @RefreshAfterWrite FixedRefresh refreshAfterWrite) { + refreshAfterWrite.setRefreshesAfter(2, TimeUnit.MINUTES); + assertThat(refreshAfterWrite.getRefreshesAfter(TimeUnit.MINUTES), is(2L)); } @Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }) public void ageOf(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { + @RefreshAfterWrite FixedRefresh refreshAfterWrite) { assertThat(refreshAfterWrite.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(0L)); context.ticker().advance(30, TimeUnit.SECONDS); assertThat(refreshAfterWrite.ageOf(context.firstKey(), TimeUnit.SECONDS).getAsLong(), is(30L)); @@ -390,107 +383,4 @@ public void ageOf(CacheContext context, assertThat(refreshAfterWrite.ageOf( context.firstKey(), TimeUnit.SECONDS).isPresent(), is(false)); } - - /* --------------- Policy: oldest --------------- */ - - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) - public void oldest_unmodifiable(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - refreshAfterWrite.oldest(Integer.MAX_VALUE).clear();; - } - - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) - public void oldest_negative(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - refreshAfterWrite.oldest(-1); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - public void oldest_zero(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - assertThat(refreshAfterWrite.oldest(0), is(emptyMap())); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, - population = Population.FULL, refreshAfterWrite = Expire.ONE_MINUTE) - public void oldest_partial(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - int count = (int) context.initialSize() / 2; - assertThat(refreshAfterWrite.oldest(count).size(), is(count)); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, - refreshAfterWrite = Expire.ONE_MINUTE, advanceOnPopulation = Advance.ONE_MINUTE, - removalListener = { Listener.DEFAULT, Listener.REJECTING }) - public void oldest_order(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - Map oldest = refreshAfterWrite.oldest(Integer.MAX_VALUE); - assertThat(oldest.keySet(), contains(context.original().keySet().toArray(new Integer[0]))); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - public void oldest_snapshot(Cache cache, CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - Map oldest = refreshAfterWrite.oldest(Integer.MAX_VALUE); - cache.invalidateAll(); - assertThat(oldest, is(equalTo(context.original()))); - } - - /* --------------- Policy: youngest --------------- */ - - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) - public void youngest_unmodifiable(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - refreshAfterWrite.youngest(Integer.MAX_VALUE).clear();; - } - - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) - public void youngest_negative(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - refreshAfterWrite.youngest(-1); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - public void youngest_zero(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - assertThat(refreshAfterWrite.youngest(0), is(emptyMap())); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, - population = Population.FULL, refreshAfterWrite = Expire.ONE_MINUTE) - public void youngest_partial(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - int count = (int) context.initialSize() / 2; - assertThat(refreshAfterWrite.youngest(count).size(), is(count)); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, - refreshAfterWrite = Expire.ONE_MINUTE, advanceOnPopulation = Advance.ONE_MINUTE, - removalListener = { Listener.DEFAULT, Listener.REJECTING }) - public void youngest_order(CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - Map youngest = refreshAfterWrite.youngest(Integer.MAX_VALUE); - Set keys = new LinkedHashSet<>(ImmutableList.copyOf(youngest.keySet()).reverse()); - assertThat(keys, contains(context.original().keySet().toArray(new Integer[0]))); - } - - @Test(dataProvider = "caches") - @CacheSpec(implementation = Implementation.Caffeine, refreshAfterWrite = Expire.ONE_MINUTE) - public void youngest_snapshot(Cache cache, CacheContext context, - @RefreshAfterWrite Expiration refreshAfterWrite) { - Map youngest = refreshAfterWrite.youngest(Integer.MAX_VALUE); - cache.invalidateAll(); - assertThat(youngest, is(equalTo(context.original()))); - } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheProvider.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheProvider.java index 2732119542..814f8e2eaa 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheProvider.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheProvider.java @@ -78,7 +78,7 @@ private static CacheGenerator newCacheGenerator(Method testMethod) { /** * Converts each scenario into test case parameters. Supports injecting {@link LoadingCache}, * {@link Cache}, {@link CacheContext}, the {@link ConcurrentMap} {@link Cache#asMap()} view, - * {@link Policy.Eviction}, and {@link Policy.Expiration}. + * {@link Policy.Eviction}, and {@link Policy.FixedExpiration}. */ private static Iterator asTestCases(Method testMethod, Stream>> scenarios) { @@ -108,10 +108,12 @@ private static Iterator asTestCases(Method testMethod, params[i] = cache.asMap(); } else if (clazz.isAssignableFrom(Policy.Eviction.class)) { params[i] = cache.policy().eviction().get(); - } else if (clazz.isAssignableFrom(Policy.Expiration.class)) { + } else if (clazz.isAssignableFrom(Policy.FixedExpiration.class)) { params[i] = expirationPolicy(parameters[i], cache); } else if (clazz.isAssignableFrom(Policy.VarExpiration.class)) { params[i] = cache.policy().expireVariably().get(); + } else if (clazz.isAssignableFrom(Policy.FixedRefresh.class)) { + params[i] = refreshPolicy(parameters[i], cache); } if (params[i] == null) { checkNotNull(params[i], "Unknown parameter type: %s", clazz); @@ -122,13 +124,20 @@ private static Iterator asTestCases(Method testMethod, } /** Returns the fixed expiration policy for the given parameter. */ - private static Policy.Expiration expirationPolicy( + private static Policy.FixedExpiration expirationPolicy( Parameter parameter, Cache cache) { if (parameter.isAnnotationPresent(ExpireAfterAccess.class)) { return cache.policy().expireAfterAccess().get(); } else if (parameter.isAnnotationPresent(ExpireAfterWrite.class)) { return cache.policy().expireAfterWrite().get(); - } else if (parameter.isAnnotationPresent(RefreshAfterWrite.class)) { + } + throw new AssertionError("Expiration parameter must have a qualifier annotation"); + } + + /** Returns the fixed expiration policy for the given parameter. */ + private static Policy.FixedRefresh refreshPolicy( + Parameter parameter, Cache cache) { + if (parameter.isAnnotationPresent(RefreshAfterWrite.class)) { return cache.policy().refreshAfterWrite().get(); } throw new AssertionError("Expiration parameter must have a qualifier annotation"); diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheValidationListener.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheValidationListener.java index c05ea9cc13..c51287fa5b 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheValidationListener.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/CacheValidationListener.java @@ -56,7 +56,7 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Policy.Eviction; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; import com.github.benmanes.caffeine.cache.Policy.VarExpiration; import com.github.benmanes.caffeine.cache.Reset; import com.github.benmanes.caffeine.cache.testing.CacheSpec.CacheExecutor; @@ -302,7 +302,7 @@ private void stringifyParams(ITestResult testResult, boolean briefParams) { Object param = params[i]; if ((param instanceof AsyncCache) || (param instanceof Cache) || (param instanceof Map) || (param instanceof Eviction) - || (param instanceof Expiration) || (param instanceof VarExpiration) + || (param instanceof FixedExpiration) || (param instanceof VarExpiration) || ((param instanceof CacheContext) && briefParams)) { params[i] = simpleNames.get(param.getClass(), key -> ((Class) key).getSimpleName()); } else if (param instanceof CacheContext) { diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterAccess.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterAccess.java index 4ad06b057c..ca5bc6c831 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterAccess.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterAccess.java @@ -21,10 +21,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; /** - * An annotation to qualify the parameterized {@link Expiration}. + * An annotation to qualify the parameterized {@link FixedExpiration}. * * @author ben.manes@gmail.com (Ben Manes) */ diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterWrite.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterWrite.java index 8bbcd33fbc..c7b24c43dc 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterWrite.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/ExpireAfterWrite.java @@ -21,10 +21,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedExpiration; /** - * An annotation to qualify the parameterized {@link Expiration}. + * An annotation to qualify the parameterized {@link FixedExpiration}. * * @author ben.manes@gmail.com (Ben Manes) */ diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/GuavaCacheFromContext.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/GuavaCacheFromContext.java index 176b9267a6..1e32afd4bc 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/GuavaCacheFromContext.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/GuavaCacheFromContext.java @@ -431,13 +431,13 @@ public Policy policy() { @Override public Optional> eviction() { return Optional.empty(); } - @Override public Optional> expireAfterAccess() { + @Override public Optional> expireAfterAccess() { return Optional.empty(); } - @Override public Optional> expireAfterWrite() { + @Override public Optional> expireAfterWrite() { return Optional.empty(); } - @Override public Optional> refreshAfterWrite() { + @Override public Optional> refreshAfterWrite() { return Optional.empty(); } }; diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RefreshAfterWrite.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RefreshAfterWrite.java index b0ddd3d9d2..97827953d4 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RefreshAfterWrite.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/testing/RefreshAfterWrite.java @@ -21,10 +21,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; -import com.github.benmanes.caffeine.cache.Policy.Expiration; +import com.github.benmanes.caffeine.cache.Policy.FixedRefresh; /** - * An annotation to qualify the parameterized {@link Expiration}. + * An annotation to qualify the parameterized {@link FixedRefresh}. * * @author ben.manes@gmail.com (Ben Manes) */