public void testInitialCapacity_large() { CacheBuilder.newBuilder().initialCapacity(Integer.MAX_VALUE); // that the builder didn't blow up is enough; // don't actually create this monster! }
public void testInitialCapacity_setTwice() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().initialCapacity(16); try { // even to the same value is not allowed builder.initialCapacity(16); fail(); } catch (IllegalStateException expected) { } }
public RelationQueryCache(EdgeSerializer edgeSerializer, int capacity) { this.edgeSerializer = edgeSerializer; this.cache = CacheBuilder.newBuilder().maximumSize(capacity*3/2).initialCapacity(capacity) .concurrencyLevel(2).build(); relationTypes = new EnumMap<RelationCategory, SliceQuery>(RelationCategory.class); for (RelationCategory rt : RelationCategory.values()) { relationTypes.put(rt,edgeSerializer.getQuery(rt,false)); } }
public void testInitialCapacity_negative() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); try { builder.initialCapacity(-1); fail(); } catch (IllegalArgumentException expected) { } }
@GwtIncompatible // digs into internals of the non-GWT implementation public void testMaximumSize_largerThanInt() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().initialCapacity(512).maximumSize(Long.MAX_VALUE); LocalCache<?, ?> cache = ((LocalCache.LocalManualCache<?, ?>) builder.build()).localCache; assertThat(cache.segments.length * cache.segments[0].table.length()).isEqualTo(512); }
public GuavaPolicy(Config config) { policyStats = new PolicyStats("product.Guava"); BasicSettings settings = new BasicSettings(config); cache = CacheBuilder.newBuilder() .maximumSize(settings.maximumSize()) .initialCapacity(settings.maximumSize()) .removalListener(notification -> policyStats.recordEviction()) .build(); }
@GwtIncompatible // CacheTesting public void testInitialCapacity_small() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(5).build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); assertEquals(2, map.segments[0].table.length()); assertEquals(2, map.segments[1].table.length()); assertEquals(2, map.segments[2].table.length()); assertEquals(2, map.segments[3].table.length()); }
@GwtIncompatible // CacheTesting public void testInitialCapacity_smallest() { LoadingCache<?, ?> cache = CacheBuilder.newBuilder().initialCapacity(0).build(identityLoader()); LocalCache<?, ?> map = CacheTesting.toLocalCache(cache); assertThat(map.segments).hasLength(4); // 1 is as low as it goes, not 0. it feels dirty to know this/test this. assertEquals(1, map.segments[0].table.length()); assertEquals(1, map.segments[1].table.length()); assertEquals(1, map.segments[2].table.length()); assertEquals(1, map.segments[3].table.length()); }
private static void checkInitialCapacity( int concurrencyLevel, int initialCapacity, int segmentSize) { LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder() .concurrencyLevel(concurrencyLevel) .initialCapacity(initialCapacity)); for (int i = 0; i < map.segments.length; i++) { assertEquals(segmentSize, map.segments[i].table.length()); } }
public void testCacheBuilderFrom_string() { CacheBuilder<?, ?> fromString = CacheBuilder.from( "initialCapacity=10,maximumSize=20,concurrencyLevel=30," + "weakKeys,weakValues,expireAfterAccess=10m"); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .concurrencyLevel(30) .weakKeys() .weakValues() .expireAfterAccess(10L, TimeUnit.MINUTES); assertCacheBuilderEquivalence(expected, fromString); }
public void testPutCausesExpansion() { for (int count = 1; count <= 100; count++) { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object> segment = map.segments[0]; assertEquals(1, segment.table.length()); for (int i = 0; i < count; i++) { Object key = new Object(); Object value = new Object(); segment.put(key, key.hashCode(), value, true); } assertEquals(count, segment.count); assertTrue(count <= segment.threshold); assertTrue(count <= (segment.table.length() * 3 / 4)); assertTrue(count > (segment.table.length() * 3 / 8)); } }
public void testSegmentPut_expand() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object> segment = map.segments[0]; assertEquals(1, segment.table.length()); int count = 1024; for (int i = 0; i < count; i++) { Object key = new Object(); Object value = new Object(); int hash = map.hash(key); assertNull(segment.put(key, hash, value, false)); assertTrue(segment.table.length() > i); } }
public void testGetCausesExpansion() throws ExecutionException { for (int count = 1; count <= 100; count++) { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); Segment<Object, Object> segment = map.segments[0]; assertEquals(1, segment.table.length()); for (int i = 0; i < count; i++) { Object key = new Object(); final Object value = new Object(); segment.get( key, key.hashCode(), new CacheLoader<Object, Object>() { @Override public Object load(Object key) { return value; } }); } assertEquals(count, segment.count); assertTrue(count <= segment.threshold); assertTrue(count <= (segment.table.length() * 3 / 4)); assertTrue(count > (segment.table.length() * 3 / 8)); } }
public void testRemoveIfWithConcurrentRemoval() { LocalCache<Integer, Integer> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(0, 1); map.put(1, 1); map.put(2, 1); map.entrySet() .removeIf( entry -> { assertThat(entry.getValue()).isNotNull(); map.remove((entry.getKey() + 1) % 3); return false; }); assertEquals(1, map.size()); }
public void testRemoveIfWithConcurrentModification() { LocalCache<Integer, Integer> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(1, 1); map.put(2, 1); map.put(3, 1); map.entrySet() .removeIf( entry -> { if (entry.getValue().equals(1)) { map.put(entry.getKey(), 2); return true; } else { return false; } }); assertEquals(3, map.size()); assertFalse(map.containsValue(1)); }
public void testParse_initialCapacity() { CacheBuilderSpec spec = parse("initialCapacity=10"); assertEquals(10, spec.initialCapacity.intValue()); assertNull(spec.maximumSize); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertNull(spec.keyStrength); assertNull(spec.valueStrength); assertNull(spec.writeExpirationTimeUnit); assertNull(spec.accessExpirationTimeUnit); assertCacheBuilderEquivalence( CacheBuilder.newBuilder().initialCapacity(10), CacheBuilder.from(spec)); }
public void testRemoveEntry() { LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder() .concurrencyLevel(1) .initialCapacity(1) .maximumSize(SMALL_MAX_SIZE) .expireAfterWrite(99999, SECONDS) .removalListener(countingRemovalListener())); Segment<Object, Object> segment = map.segments[0]; AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table; assertEquals(1, table.length()); Object key = new Object(); Object value = new Object(); int hash = map.hash(key); DummyEntry<Object, Object> entry = createDummyEntry(key, hash, value, null); // remove absent assertFalse(segment.removeEntry(entry, hash, RemovalCause.COLLECTED)); // remove live segment.recordWrite(entry, 1, map.ticker.read()); table.set(0, entry); segment.count = 1; assertTrue(segment.removeEntry(entry, hash, RemovalCause.COLLECTED)); assertNotificationEnqueued(map, key, value, hash); assertTrue(map.removalNotificationQueue.isEmpty()); assertFalse(segment.accessQueue.contains(entry)); assertFalse(segment.writeQueue.contains(entry)); assertEquals(0, segment.count); assertNull(table.get(0)); }
public void testParse_multipleKeys() { CacheBuilderSpec spec = parse( "initialCapacity=10,maximumSize=20,concurrencyLevel=30," + "weakKeys,weakValues,expireAfterAccess=10m,expireAfterWrite=1h"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertEquals(30, spec.concurrencyLevel.intValue()); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.WEAK, spec.valueStrength); assertEquals(TimeUnit.HOURS, spec.writeExpirationTimeUnit); assertEquals(TimeUnit.MINUTES, spec.accessExpirationTimeUnit); assertEquals(1L, spec.writeExpirationDuration); assertEquals(10L, spec.accessExpirationDuration); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .concurrencyLevel(30) .weakKeys() .weakValues() .expireAfterAccess(10L, TimeUnit.MINUTES) .expireAfterWrite(1L, TimeUnit.HOURS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testParse_whitespaceAllowed() { CacheBuilderSpec spec = parse( " initialCapacity=10,\nmaximumSize=20,\t\r" + "weakKeys \t ,softValues \n , \r expireAfterWrite \t = 15s\n\n"); assertEquals(10, spec.initialCapacity.intValue()); assertEquals(20, spec.maximumSize.intValue()); assertNull(spec.maximumWeight); assertNull(spec.concurrencyLevel); assertEquals(Strength.WEAK, spec.keyStrength); assertEquals(Strength.SOFT, spec.valueStrength); assertEquals(TimeUnit.SECONDS, spec.writeExpirationTimeUnit); assertEquals(15L, spec.writeExpirationDuration); assertNull(spec.accessExpirationTimeUnit); CacheBuilder<?, ?> expected = CacheBuilder.newBuilder() .initialCapacity(10) .maximumSize(20) .weakKeys() .softValues() .expireAfterWrite(15L, TimeUnit.SECONDS); assertCacheBuilderEquivalence(expected, CacheBuilder.from(spec)); }
public void testGetOrDefault() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).initialCapacity(1)); map.put(1, 1); assertEquals(1, map.getOrDefault(1, 2)); assertEquals(2, map.getOrDefault(2, 2)); }