public void testComputeIfAbsent_RemovalListener() { List<RemovalNotification<Object, Object>> notifications = new ArrayList<>(); RemovalListener<Object, Object> removalListener = new RemovalListener<Object, Object>() { @Override public void onRemoval(RemovalNotification<Object, Object> notification) { notifications.add(notification); } }; Cache<Object, Object> cache = CacheBuilder.newBuilder().removalListener(removalListener).build(); cache.put("a", "b"); cache.asMap().computeIfAbsent("a", k -> "c"); assertTrue(notifications.toString(), notifications.isEmpty()); }
private void checkNothingLogged() { assertTrue(logHandler.getStoredLogRecords().isEmpty()); }
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)); } }
static <K, V> void checkExpirationTimes(LocalCache<K, V> map) { if (!map.expires()) { return; } for (Segment<K, V> segment : map.segments) { long lastAccessTime = 0; long lastWriteTime = 0; for (ReferenceEntry<K, V> e : segment.recencyQueue) { long accessTime = e.getAccessTime(); assertTrue(accessTime >= lastAccessTime); lastAccessTime = accessTime; long writeTime = e.getWriteTime(); assertTrue(writeTime >= lastWriteTime); lastWriteTime = writeTime; } lastAccessTime = 0; lastWriteTime = 0; for (ReferenceEntry<K, V> e : segment.accessQueue) { long accessTime = e.getAccessTime(); assertTrue(accessTime >= lastAccessTime); lastAccessTime = accessTime; } for (ReferenceEntry<K, V> e : segment.writeQueue) { long writeTime = e.getWriteTime(); assertTrue(writeTime >= lastWriteTime); lastWriteTime = writeTime; } } }
public void testDrainRecencyQueueOnWrite() { for (CacheBuilder<Object, Object> builder : allEvictingMakers()) { LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1)); Segment<Object, Object> segment = map.segments[0]; if (segment.recencyQueue != DISCARDING_QUEUE) { Object keyOne = new Object(); Object valueOne = new Object(); Object keyTwo = new Object(); Object valueTwo = new Object(); map.put(keyOne, valueOne); assertTrue(segment.recencyQueue.isEmpty()); for (int i = 0; i < DRAIN_THRESHOLD / 2; i++) { map.get(keyOne); } assertFalse(segment.recencyQueue.isEmpty()); map.put(keyTwo, valueTwo); assertTrue(segment.recencyQueue.isEmpty()); } } }
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 testRemovalListener_size() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder().concurrencyLevel(1).maximumSize(2).removalListener(listener)); assertTrue(listener.isEmpty()); Object one = new Object(); Object two = new Object(); Object three = new Object(); Object four = new Object(); map.put(one, two); map.put(two, three); assertTrue(listener.isEmpty()); map.put(three, four); assertNotified(listener, one, two, RemovalCause.SIZE); assertTrue(listener.isEmpty()); }
assertTrue(segment.recencyQueue.isEmpty()); assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD); map.put(new Object(), new Object()); assertTrue(segment.recencyQueue.isEmpty()); assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
assertTrue(segment.recencyQueue.isEmpty()); reads.add(entry); i.remove(); assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
assertTrue(segment.recencyQueue.isEmpty()); reads.add(entry); i.remove(); assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
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); } }
.maximumSize(maxSize)); long totalCapacity = 0; assertTrue( "segments=" + map.segments.length + ", maxSize=" + maxSize, map.segments.length <= Math.max(1, maxSize / 10)); totalCapacity += map.segments[i].maxSegmentWeight; assertTrue("totalCapacity=" + totalCapacity + ", maxSize=" + maxSize, totalCapacity == maxSize); .maximumWeight(maxSize) .weigher(constantWeigher(1))); assertTrue( "segments=" + map.segments.length + ", maxSize=" + maxSize, map.segments.length <= Math.max(1, maxSize / 10)); totalCapacity += map.segments[i].maxSegmentWeight; assertTrue("totalCapacity=" + totalCapacity + ", maxSize=" + maxSize, totalCapacity == maxSize);
assertTrue(segment.accessQueue.isEmpty()); assertTrue(segment.writeQueue.isEmpty()); assertEquals(0, segment.readCount.get()); assertEquals(0, segment.count);
public void testRemovalListener_replaced() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().removalListener(listener)); assertTrue(listener.isEmpty()); Object one = new Object(); Object two = new Object(); Object three = new Object(); Object four = new Object(); Object five = new Object(); Object six = new Object(); map.put(one, two); map.put(one, three); assertNotified(listener, one, two, RemovalCause.REPLACED); Map<Object, Object> newMap = ImmutableMap.of(one, four); map.putAll(newMap); assertNotified(listener, one, three, RemovalCause.REPLACED); map.replace(one, five); assertNotified(listener, one, four, RemovalCause.REPLACED); map.replace(one, five, six); assertNotified(listener, one, five, RemovalCause.REPLACED); }
public void testRemovalListener_collected() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder().concurrencyLevel(1).softValues().removalListener(listener)); Segment<Object, Object> segment = map.segments[0]; assertTrue(listener.isEmpty()); Object one = new Object(); Object two = new Object(); Object three = new Object(); map.put(one, two); map.put(two, three); assertTrue(listener.isEmpty()); int hash = map.hash(one); ReferenceEntry<Object, Object> entry = segment.getEntry(one, hash); map.reclaimValue(entry.getValueReference()); assertNotified(listener, one, two, RemovalCause.COLLECTED); assertTrue(listener.isEmpty()); }
public void testRemovalListener_explicit() { QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().removalListener(listener)); assertTrue(listener.isEmpty()); assertNotified(listener, five, six, RemovalCause.EXPLICIT); assertTrue(listener.isEmpty());
public void testRemovalListener_expired() { FakeTicker ticker = new FakeTicker(); QueuingRemovalListener<Object, Object> listener = queuingRemovalListener(); LocalCache<Object, Object> map = makeLocalCache( createCacheBuilder() .concurrencyLevel(1) .expireAfterWrite(3, TimeUnit.NANOSECONDS) .ticker(ticker) .removalListener(listener)); assertTrue(listener.isEmpty()); Object one = new Object(); Object two = new Object(); Object three = new Object(); Object four = new Object(); Object five = new Object(); map.put(one, two); ticker.advance(1); map.put(two, three); ticker.advance(1); map.put(three, four); assertTrue(listener.isEmpty()); ticker.advance(1); map.put(four, five); assertNotified(listener, one, two, RemovalCause.EXPIRED); assertTrue(listener.isEmpty()); }
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 testValues() { LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder()); map.put("foo", "bar"); map.put("baz", "bar"); map.put("quux", "quux"); assertFalse(map.values() instanceof Set); assertTrue(map.values().removeAll(ImmutableSet.of("bar"))); assertEquals(1, map.size()); }
map.put(key, value); ReferenceEntry<Object, Object> entry = map.getEntry(key); assertTrue(map.isLive(entry, ticker.read())); segment.expireEntries(ticker.read()); assertNull(map.get(key)); assertTrue(segment.writeQueue.isEmpty());