@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT, expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED, refreshAfterWrite = Expire.DISABLED, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) @Test(dataProvider = "caches") public void noPolicy(Cache<Integer, Integer> cache, CacheContext context) { assertThat(cache.policy().eviction(), is(Optional.empty())); assertThat(cache.policy().expireAfterWrite(), is(Optional.empty())); assertThat(cache.policy().expireAfterAccess(), is(Optional.empty())); assertThat(cache.policy().refreshAfterWrite(), is(Optional.empty())); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, expiry = CacheExpiry.DISABLED) public void expireVariably_notEnabled(Cache<Integer, Integer> cache) { assertThat(cache.policy().expireVariably(), is(Optional.empty())); }
private void checkContainsInOrder(Cache<Integer, Integer> cache, Integer... expect) { cache.cleanUp(); List<Integer> evictionList = ImmutableList.copyOf( cache.policy().eviction().get().coldest(Integer.MAX_VALUE).keySet()); assertThat(cache.asMap().size(), is(equalTo(expect.length))); assertThat(cache.asMap().keySet(), containsInAnyOrder(expect)); assertThat(evictionList, is(equalTo(asList(expect)))); }
/** Returns the fixed expiration policy for the given parameter. */ private static Policy.Expiration<Integer, Integer> expirationPolicy( Parameter parameter, Cache<Integer, Integer> 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)) { return cache.policy().refreshAfterWrite().get(); } throw new AssertionError("Expiration parameter must have a qualifier annotation"); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@Test public void expireAfterAccess_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(0, TimeUnit.MILLISECONDS); assertThat(builder.expireAfterAccessNanos, is(0L)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@Test public void maximumSize_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(0); assertThat(builder.maximumSize, is(0L)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is(0L)); }
@Test public void maximumSize_large() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(Integer.MAX_VALUE); assertThat(builder.maximumSize, is((long) Integer.MAX_VALUE)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is((long) Integer.MAX_VALUE)); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void expireAfterWrite_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterWrite(0, TimeUnit.MILLISECONDS); assertThat(builder.expireAfterWriteNanos, is(0L)); Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L)); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void maximumWeight_small() { Caffeine<?, ?> builder = Caffeine.newBuilder() .maximumWeight(0).weigher(Weigher.singletonWeigher()); assertThat(builder.weigher, is(Weigher.singletonWeigher())); assertThat(builder.maximumWeight, is(0L)); Eviction<?, ?> eviction = builder.build().policy().eviction().get(); assertThat(eviction.getMaximum(), is(0L)); assertThat(eviction.isWeighted(), is(true)); }
@Test 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(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) public void putIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.asMap().putIfAbsent(1, ImmutableList.of(1, 2, 3)); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }
@Test public void maximumWeight_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .maximumWeight(Integer.MAX_VALUE).weigher(Weigher.singletonWeigher()); assertThat(builder.maximumWeight, is((long) Integer.MAX_VALUE)); assertThat(builder.weigher, is(Weigher.singletonWeigher())); Eviction<?, ?> eviction = builder.build().policy().eviction().get(); assertThat(eviction.getMaximum(), is((long) Integer.MAX_VALUE)); assertThat(eviction.isWeighted(), is(true)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE, mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE }, expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS }, expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE}, expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE}) public void put_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.put(1, ImmutableList.of(1, 2, 3)); assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L)); }
@Test(dataProvider = "caches") @CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL) public void drain_blocksCapacity(Cache<Integer, Integer> cache, CacheContext context) { BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache); checkDrainBlocks(localCache, () -> cache.policy().eviction().ifPresent( policy -> policy.setMaximum(0L))); }
@CacheSpec @CheckNoWriter @CheckNoStats @Test(dataProvider = "caches") public void stats(Cache<Integer, Integer> cache, CacheContext context) { CacheStats stats = cache.stats() .plus(new CacheStats(1, 2, 3, 4, 5, 6, 7) .minus(new CacheStats(6, 5, 4, 3, 2, 1, 0))); assertThat(stats, is(new CacheStats(0, 0, 0, 1, 3, 5, 7))); assertThat(cache.policy().isRecordingStats(), is(context.isRecordingStats())); }