@Override public void put(K key, V value) { cache.put(key, value); }
@Test(dataProvider = "caches") @CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) public void put_sameWeight(Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) { cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1))); cache.put("a", asList(-1, -2, -3)); assertThat(cache.estimatedSize(), is(2L)); assertThat(eviction.weightedSize().getAsLong(), is(4L)); }
@Override public void put(Object key, @Nullable Object value) { this.cache.put(key, toStoreValue(value)); }
@Override public void put(NamedKey key, byte[] value) { cache.put(key, serialize(value)); }
/** * Gets the human friendly location of where the violation was raised. */ public static String getMessage(ConstraintViolation<?> v, Invocable invocable) { final Pair<Path, ? extends ConstraintDescriptor<?>> of = Pair.of(v.getPropertyPath(), v.getConstraintDescriptor()); final String cachePrefix = PREFIX_CACHE.getIfPresent(of); if (cachePrefix == null) { final String prefix = calculatePrefix(v, invocable); PREFIX_CACHE.put(of, prefix); return prefix + v.getMessage(); } return cachePrefix + v.getMessage(); }
private void checkEvict(Cache<Integer, Integer> cache, List<Integer> keys, Integer... expect) { keys.forEach(i -> cache.put(i, i)); checkContainsInOrder(cache, expect); }
@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, weigher = CacheWeigher.COLLECTION, population = Population.EMPTY, keys = ReferenceType.STRONG, values = ReferenceType.STRONG) public void put(Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) { cache.put("a", asList(1, 2, 3)); assertThat(cache.estimatedSize(), is(1L)); assertThat(eviction.weightedSize().getAsLong(), is(3L)); }
@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 computeIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.asMap().computeIfAbsent(1, k -> 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.FULL, maximumSize = Maximum.FULL, executorFailure = ExecutorFailure.EXPECTED, executor = CacheExecutor.REJECTING, removalListener = Listener.CONSUMING) public void scheduleDrainBuffers_rejected(Cache<Integer, Integer> cache, CacheContext context) { cache.put(context.absentKey(), context.absentValue()); }
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void put_nullKeyAndValue(Cache<Integer, Integer> cache, CacheContext context) { cache.put(null, null); }
@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 compute_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) { cache.put(1, ImmutableList.of(1)); context.ticker().advance(1, TimeUnit.MINUTES); cache.asMap().compute(1, (k, v) -> 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_onWrite(Cache<Integer, Integer> cache, CacheContext context) { BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache); cache.put(1, 1); int size = localCache.accessOrderEdenDeque().size() + localCache.accessOrderProbationDeque().size(); assertThat(localCache.writeBuffer().size(), is(0)); assertThat(size, is(1)); }
@Test(dataProvider = "caches") @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) public void put_insert(Cache<Integer, Integer> cache, CacheContext context) { cache.put(context.absentKey(), context.absentValue()); assertThat(cache.estimatedSize(), is(context.initialSize() + 1)); assertThat(cache.getIfPresent(context.absentKey()), is(context.absentValue())); }
@CheckNoStats @Test(dataProvider = "caches", expectedExceptions = WriteException.class) @CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG, compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING) public void put_insert_writerFails(Cache<Integer, Integer> cache, CacheContext context) { try { cache.put(context.absentKey(), context.absentValue()); } finally { assertThat(cache.asMap(), equalTo(context.original())); } }
@CheckNoStats @Test(dataProvider = "caches", expectedExceptions = WriteException.class) @CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG, population = { Population.SINGLETON, Population.PARTIAL, Population.FULL }, compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING) public void put_replace_writerFails(Cache<Integer, Integer> cache, CacheContext context) { try { cache.put(context.middleKey(), context.absentValue()); } finally { assertThat(cache.asMap(), equalTo(context.original())); } }
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void put_nullKey(Cache<Integer, Integer> cache, CacheContext context) { cache.put(null, context.absentValue()); }
@CheckNoWriter @CheckNoStats @CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING }) @Test(dataProvider = "caches", expectedExceptions = NullPointerException.class) public void put_nullValue(Cache<Integer, Integer> cache, CacheContext context) { cache.put(context.absentKey(), null); }
@CheckNoWriter @CacheSpec(maximumSize = Maximum.FULL, weigher = CacheWeigher.NEGATIVE, population = Population.EMPTY) @Test(dataProvider = "caches", expectedExceptions = { IllegalArgumentException.class, IllegalStateException.class }) public void put_negativeWeight(Cache<Integer, Integer> cache, CacheContext context) { cache.put(context.absentKey(), context.absentValue()); }
@CacheSpec(maximumSize = Maximum.FULL, weigher = CacheWeigher.ZERO, population = Population.EMPTY) @Test(dataProvider = "caches") public void put_zeroWeight(Cache<Integer, Integer> cache, CacheContext context) { cache.put(context.absentKey(), context.absentValue()); verifyWriter(context, (verifier, writer) -> { verifier.wrote(context.absentKey(), context.absentValue()); }); }