@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = Listener.REJECTING) public void maximumSize_increase_max(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(Long.MAX_VALUE); assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(eviction.getMaximum(), is(Long.MAX_VALUE - Integer.MAX_VALUE)); // impl detail }
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) @Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class) public void maximumSize_decrease_negative(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { try { eviction.setMaximum(-1); } finally { assertThat(eviction.getMaximum(), is(context.maximumWeightOrSize())); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE)); } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void maximumSize_increase(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(2 * context.maximumWeightOrSize()); assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(eviction.getMaximum(), is(2 * context.maximumWeightOrSize())); }
private void resizeIfNecessary() { if (evictionPolicy.getMaximum() != size.get()) { evictionPolicy.setMaximum(size.get()); } }
private void resizeIfNecessary() { if (evictionPolicy.getMaximum() != size.get()) { evictionPolicy.setMaximum(size.get()); } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) public void maximumSize_decrease(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { long newSize = context.maximumWeightOrSize() / 2; eviction.setMaximum(newSize); assertThat(eviction.getMaximum(), is(newSize)); if (context.initialSize() > newSize) { if (context.isZeroWeighted()) { assertThat(cache.estimatedSize(), is(context.initialSize())); assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE)); } else { assertThat(cache.estimatedSize(), is(newSize)); assertThat(cache, hasRemovalNotifications(context, newSize, RemovalCause.SIZE)); } } }
@CheckNoStats @Test(dataProvider = "caches", expectedExceptions = DeleteException.class) @CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG, population = Population.FULL, maximumSize = Maximum.FULL, weigher = {CacheWeigher.DEFAULT, CacheWeigher.TEN}, compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING) public void evict_writerFails(Cache<Integer, Integer> cache, CacheContext context) { try { cache.policy().eviction().ifPresent(policy -> policy.setMaximum(0)); } finally { context.disableRejectingCacheWriter(); assertThat(cache.asMap(), equalTo(context.original())); } }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = { CacheWeigher.DEFAULT, CacheWeigher.TEN }, removalListener = { Listener.DEFAULT, Listener.CONSUMING }) public void maximumSize_decrease_min(Cache<Integer, Integer> cache, CacheContext context, Eviction<Integer, Integer> eviction) { eviction.setMaximum(0); assertThat(eviction.getMaximum(), is(0L)); if (context.initialSize() > 0) { long expect = context.isZeroWeighted() ? context.initialSize() : 0; assertThat(cache.estimatedSize(), is(expect)); } assertThat(cache, hasRemovalNotifications(context, context.initialSize(), RemovalCause.SIZE)); }
@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))); }