public void testMaximumSize_setTwice() { Caffeine<Object, Object> builder = Caffeine.newBuilder().maximumSize(16); try { // even to the same value is not allowed builder.maximumSize(16); fail(); } catch (IllegalStateException expected) {} }
@Test(expectedExceptions = IllegalArgumentException.class) public void maximumSize_negative() { Caffeine.newBuilder().maximumSize(-1); }
/** Configures the maximum size and returns if set. */ private boolean configureMaximumSize() { if (config.getMaximumSize().isPresent()) { caffeine.maximumSize(config.getMaximumSize().getAsLong()); } return config.getMaximumSize().isPresent(); }
@Test(expectedExceptions = IllegalStateException.class) public void maximumWeight_maximumSize() { Caffeine.newBuilder().maximumSize(1).maximumWeight(1); }
public void testMaximumSize_negative() { Caffeine<Object, Object> builder = Caffeine.newBuilder(); try { builder.maximumSize(-1); fail(); } catch (IllegalArgumentException expected) {} }
@Test public void maximumSize_small() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(0); assertThat(builder.maximumSize, is(0L)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is(0L)); }
@Test public void maximumSize_large() { Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(Integer.MAX_VALUE); assertThat(builder.maximumSize, is((long) Integer.MAX_VALUE)); Cache<?, ?> cache = builder.build(); assertThat(cache.policy().eviction().get().getMaximum(), is((long) Integer.MAX_VALUE)); }
public void testComputeIfAbsentEviction() { Cache<String, String> c = CaffeinatedGuava.build( Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1)); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo(""); assertThat(c.size()).isEqualTo(1); assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo(""); }
@Override public void setUp() throws Exception { super.setUp(); this.cache = CaffeinatedGuava.build(Caffeine.newBuilder() .expireAfterAccess(500000, TimeUnit.MILLISECONDS) .executor(MoreExecutors.directExecutor()) .maximumSize(count)); }
public void testComputeEviction() { Cache<String, String> c = CaffeinatedGuava.build( Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1)); assertThat(c.asMap().compute("hash-1", (k, v) -> "a")).isEqualTo("a"); assertThat(c.asMap().compute("hash-1", (k, v) -> "b")).isEqualTo("b"); assertThat(c.asMap().compute("hash-1", (k, v) -> "c")).isEqualTo("c"); assertThat(c.size()).isEqualTo(1); assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo(""); }
private static <K, V> ConcurrentMap<K, V> map() { return Caffeine.newBuilder() .maximumSize(Integer.MAX_VALUE) .<K, V>build().asMap(); }
private static <K, V> ConcurrentMap<K, V> map() { return Caffeine.newBuilder() .maximumSize(Integer.MAX_VALUE) .<K, V>build().asMap(); }
public CaffeinePolicy(Config config) { policyStats = new PolicyStats("product.Caffeine"); BasicSettings settings = new BasicSettings(config); maximumSize = settings.maximumSize(); cache = Caffeine.newBuilder() .initialCapacity(maximumSize) .maximumSize(maximumSize) .executor(Runnable::run) .build(); }
@GwtIncompatible("maximumWeight") public void testMaximumSize_andWeight() { Caffeine<Object, Object> builder = Caffeine.newBuilder().maximumSize(16); try { builder.maximumWeight(16); fail(); } catch (IllegalStateException expected) {} }
public void testEviction_maxSizeOneSegment() { IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .executor(MoreExecutors.directExecutor()) .maximumSize(MAX_SIZE), loader); for (int i = 0; i < 2 * MAX_SIZE; i++) { cache.getUnchecked(i); assertEquals(Math.min(i + 1, MAX_SIZE), cache.size()); } assertEquals(MAX_SIZE, cache.size()); CacheTesting.checkValidState(cache); }
public void testGet_computeNull() { LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder() .maximumSize(0) .removalListener(listener), constantLoader(null)); try { cache.getUnchecked(new Object()); fail(); } catch (InvalidCacheLoadException e) { /* expected */} assertTrue(listener.isEmpty()); checkEmpty(cache); }
private static void addBoundedTests(TestSuite suite) throws Exception { suite.addTest(MapTestFactory.suite("BoundedCache", () -> { Cache<String, String> cache = Caffeine.newBuilder().maximumSize(Long.MAX_VALUE).build(); return cache.asMap(); })); suite.addTest(MapTestFactory.suite("BoundedAsyncCache", () -> { AsyncLoadingCache<String, String> cache = Caffeine.newBuilder() .maximumSize(Long.MAX_VALUE) .buildAsync(key -> null); return cache.synchronous().asMap(); })); }
@Test public void changeCaffeineRecreateCache() { CaffeineCacheManager cm = new CaffeineCacheManager("c1"); Cache cache1 = cm.getCache("c1"); Caffeine<Object, Object> caffeine = Caffeine.newBuilder().maximumSize(10); cm.setCaffeine(caffeine); Cache cache1x = cm.getCache("c1"); assertTrue(cache1x != cache1); cm.setCaffeine(caffeine); // Set same instance Cache cache1xx = cm.getCache("c1"); assertSame(cache1x, cache1xx); }
public void testCaffeineFrom_string() { Caffeine<?, ?> fromString = Caffeine.from( "initialCapacity=10,maximumSize=20,weakKeys,weakValues,expireAfterAccess=10m"); Caffeine<?, ?> expected = Caffeine.newBuilder() .initialCapacity(10) .maximumSize(20) .weakKeys() .weakValues() .expireAfterAccess(10L, TimeUnit.MINUTES); assertCaffeineEquivalence(expected, fromString); }
public void testParse_maximumSize() { CaffeineSpec spec = parse("maximumSize=9000"); assertEquals(spec.initialCapacity, UNSET_INT); assertEquals(9000, spec.maximumSize); assertNull(spec.keyStrength); assertNull(spec.valueStrength); assertNull(spec.expireAfterWriteTimeUnit); assertNull(spec.expireAfterAccessTimeUnit); assertNull(spec.refreshAfterWriteTimeUnit); assertCaffeineEquivalence( Caffeine.newBuilder().maximumSize(9000), Caffeine.from(spec)); }