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)))); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL, initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL, weigher = { CacheWeigher.DEFAULT, CacheWeigher.TEN }, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void coldest_order(CacheContext context, Eviction<Integer, Integer> eviction) { Set<Integer> keys = new LinkedHashSet<>(context.original().keySet()); Set<Integer> coldest = new LinkedHashSet<>(eviction.coldest(Integer.MAX_VALUE).keySet()); // 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 coldest_partial(CacheContext context, Eviction<Integer, Integer> eviction) { int count = (int) context.initialSize() / 2; assertThat(eviction.coldest(count).size(), is(count)); }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void coldest_negative(CacheContext context, Eviction<Integer, Integer> eviction) { eviction.coldest(-1); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) public void coldest_zero(CacheContext context, Eviction<Integer, Integer> eviction) { assertThat(eviction.coldest(0), is(emptyMap())); }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) @Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class) public void coldest_unmodifiable(CacheContext context, Eviction<Integer, Integer> eviction) { eviction.coldest(Integer.MAX_VALUE).clear();; }
@Test(dataProvider = "caches") @CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.FULL) public void drain_blocksOrderedMap(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache); checkDrainBlocks(localCache, () -> eviction.coldest(((int) context.maximumSize()))); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL) public void coldest_snapshot(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { Map<Integer, Integer> coldest = eviction.coldest(Integer.MAX_VALUE); cache.invalidateAll(); assertThat(coldest, is(equalTo(context.original()))); }