@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void hottest_order(CacheContext context, Eviction<Integer, Integer> eviction) { Set<Integer> keys = new LinkedHashSet<>(context.original().keySet()); Set<Integer> hottest = eviction.hottest(Integer.MAX_VALUE).keySet(); Set<Integer> coldest = new LinkedHashSet<>(ImmutableList.copyOf(hottest).reverse()); // Ignore the last key; hard to predict with W-TinyLFU keys.remove(context.lastKey()); coldest.remove(context.lastKey()); assertThat(coldest, contains(keys.toArray())); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL) public void hottest_partial(CacheContext context, Eviction<Integer, Integer> eviction) { int count = (int) context.initialSize() / 2; assertThat(eviction.hottest(count).size(), is(count)); }
static void printKeys(Cache<Long, Long> cache) { if (debug) { Set<Long> keys = cache.policy().eviction() .map(policy -> policy.hottest(Integer.MAX_VALUE).keySet()) .orElseGet(cache.asMap()::keySet); System.out.println(keys); } } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL) public void hottest_snapshot(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { Map<Integer, Integer> hottest = eviction.hottest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(hottest, is(equalTo(context.original()))); } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) public void hottest_zero(CacheContext context, Eviction<Integer, Integer> eviction) { assertThat(eviction.hottest(0), is(emptyMap())); }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void hottest_unmodifiable(CacheContext context, Eviction<Integer, Integer> eviction) { eviction.hottest(Integer.MAX_VALUE).clear(); }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void hottest_negative(CacheContext context, Eviction<Integer, Integer> eviction) { eviction.hottest(-1); }