@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.TEN) public void weightedSize(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { long weightedSize = 0; for (Integer key : cache.asMap().keySet()) { weightedSize += eviction.weightOf(key).getAsInt(); } assertThat(weightedSize, is(eviction.weightedSize().getAsLong())); assertThat(eviction.weightedSize().getAsLong(), is(10 * cache.estimatedSize())); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY, maximumSize = Maximum.UNREACHABLE, weigher = CacheWeigher.VALUE) public void weightOf(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { Integer key = 1; cache.put(key, 1); assertThat(eviction.weightOf(key).getAsInt(), is(1)); cache.put(key, 2); assertThat(eviction.weightOf(key).getAsInt(), is(2)); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL) public void weightOf_absent(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { assertThat(eviction.weightOf(context.absentKey()), is(OptionalInt.empty())); }