public void testValues_clear() { for (LoadingCache<Object, Object> cache : caches()) { warmUp(cache, 0, 100); Collection<Object> values = cache.asMap().values(); values.clear(); checkEmpty(values); checkEmpty(cache); } }
public void testPutAll_populated() { for (LoadingCache<Object, Object> cache : caches()) { // don't let the entries get GCed List<Entry<Object, Object>> warmed = warmUp(cache); Object newKey = new Object(); Object newValue = new Object(); cache.asMap().putAll(ImmutableMap.of(newKey, newValue)); // this getUnchecked() call shouldn't be a cache miss; verified below assertEquals(newValue, cache.getUnchecked(newKey)); assertEquals(WARMUP_SIZE, cache.stats().missCount()); checkValidState(cache); } }
public void testEviction_lru() { // test lru within a single segment IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(10).build(loader); CacheTesting.warmUp(cache, 0, 10); Set<Integer> keySet = cache.asMap().keySet(); assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // re-order getAll(cache, asList(0, 1, 2)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0, 1, 2); // evict 3, 4, 5 getAll(cache, asList(10, 11, 12)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(6, 7, 8, 9, 0, 1, 2, 10, 11, 12); // re-order getAll(cache, asList(6, 7, 8)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(9, 0, 1, 2, 10, 11, 12, 6, 7, 8); // evict 9, 0, 1 getAll(cache, asList(13, 14, 15)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(2, 10, 11, 12, 6, 7, 8, 13, 14, 15); }
static void drainRecencyQueues(Cache<?, ?> cache) { if (hasLocalCache(cache)) { LocalCache<?, ?> map = toLocalCache(cache); for (Segment<?, ?> segment : map.segments) { drainRecencyQueue(segment); } } }
static int accessQueueSize(Cache<?, ?> cache) { LocalCache<?, ?> cchm = toLocalCache(cache); int size = 0; for (Segment<?, ?> segment : cchm.segments) { size += accessQueueSize(segment); } return size; }
/** * Peeks into the cache's internals to verify that its expiration queue is consistent. Verifies * that the next/prev links in the expiration queue are correct, and that the queue is ordered by * expiration time. */ static void checkExpiration(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkExpiration(toLocalCache(cache)); } }
static void expireEntries(LocalCache<?, ?> cchm, long expiringTime, FakeTicker ticker) { for (Segment<?, ?> segment : cchm.segments) { drainRecencyQueue(segment); } ticker.advance(2 * expiringTime, TimeUnit.MILLISECONDS); long now = ticker.read(); for (Segment<?, ?> segment : cchm.segments) { expireEntries(segment, now); assertEquals("Expiration queue must be empty by now", 0, writeQueueSize(segment)); assertEquals("Expiration queue must be empty by now", 0, accessQueueSize(segment)); assertEquals("Segments must be empty by now", 0, segmentSize(segment)); } cchm.processPendingNotifications(); }
/** * Assuming the given cache has maximum size {@code maxSize}, this method populates the cache (by * getting a bunch of different keys), then makes sure all the items in the cache are also in the * eviction queue. It will invoke the given {@code operation} on the first element in the eviction * queue, and then reverify that all items in the cache are in the eviction queue, and verify that * the head of the eviction queue has changed as a result of the operation. */ static void checkRecency( LoadingCache<Integer, Integer> cache, int maxSize, Receiver<ReferenceEntry<Integer, Integer>> operation) { checkNotNull(operation); if (hasLocalCache(cache)) { warmUp(cache, 0, 2 * maxSize); LocalCache<Integer, Integer> cchm = toLocalCache(cache); Segment<?, ?> segment = cchm.segments[0]; drainRecencyQueue(segment); assertEquals(maxSize, accessQueueSize(cache)); assertEquals(maxSize, cache.size()); ReferenceEntry<?, ?> originalHead = segment.accessQueue.peek(); @SuppressWarnings("unchecked") ReferenceEntry<Integer, Integer> entry = (ReferenceEntry) originalHead; operation.accept(entry); drainRecencyQueue(segment); assertNotSame(originalHead, segment.accessQueue.peek()); assertEquals(cache.size(), accessQueueSize(cache)); } }
public void testEviction_maxSize() { CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener(); IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder() .maximumSize(MAX_SIZE) .removalListener(removalListener) .build(loader); for (int i = 0; i < 2 * MAX_SIZE; i++) { cache.getUnchecked(i); assertTrue(cache.size() <= MAX_SIZE); } assertEquals(MAX_SIZE, CacheTesting.accessQueueSize(cache)); assertEquals(MAX_SIZE, cache.size()); CacheTesting.processPendingNotifications(cache); assertEquals(MAX_SIZE, removalListener.getCount()); CacheTesting.checkValidState(cache); }
public void testEviction_invalidateAll() { // test that .invalidateAll() resets total weight state correctly IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(10).build(loader); Set<Integer> keySet = cache.asMap().keySet(); assertThat(keySet).isEmpty(); // add 0, 1, 2, 3, 4 getAll(cache, asList(0, 1, 2, 3, 4)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(0, 1, 2, 3, 4); // invalidate all cache.invalidateAll(); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).isEmpty(); // add 5, 6, 7, 8, 9, 10, 11, 12 getAll(cache, asList(5, 6, 7, 8, 9, 10, 11, 12)); CacheTesting.drainRecencyQueues(cache); assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 10, 11, 12); }
static void checkEmpty(ConcurrentMap<?, ?> map) { checkEmpty(map.keySet()); checkEmpty(map.values()); checkEmpty(map.entrySet()); assertEquals(ImmutableMap.of(), map); assertEquals(ImmutableMap.of().hashCode(), map.hashCode()); assertEquals(ImmutableMap.of().toString(), map.toString()); if (map instanceof LocalCache) { LocalCache<?, ?> cchm = (LocalCache<?, ?>) map; checkValidState(cchm); assertTrue(cchm.isEmpty()); assertEquals(0, cchm.size()); for (LocalCache.Segment<?, ?> segment : cchm.segments) { assertEquals(0, segment.count); assertEquals(0, segmentSize(segment)); assertTrue(segment.writeQueue.isEmpty()); assertTrue(segment.accessQueue.isEmpty()); } } }
/** * Peeks into the cache's internals to check its internal consistency. Verifies that each * segment's count matches its #elements (after cleanup), each segment is unlocked, each entry * contains a non-null key and value, and the eviction and expiration queues are consistent (see * {@link #checkEviction}, {@link #checkExpiration}). */ static void checkValidState(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkValidState(toLocalCache(cache)); } }
private void checkExpiration( LoadingCache<String, Integer> cache, WatchedCreatorLoader loader, FakeTicker ticker, CountingRemovalListener<String, Integer> removalListener) { for (int i = 0; i < 10; i++) { assertEquals(Integer.valueOf(VALUE_PREFIX + i), cache.getUnchecked(KEY_PREFIX + i)); } for (int i = 0; i < 10; i++) { loader.reset(); assertEquals(Integer.valueOf(VALUE_PREFIX + i), cache.getUnchecked(KEY_PREFIX + i)); assertFalse("Creator should not have been called @#" + i, loader.wasCalled()); } CacheTesting.expireEntries((LoadingCache<?, ?>) cache, EXPIRING_TIME, ticker); assertEquals("Map must be empty by now", 0, cache.size()); assertEquals("Eviction notifications must be received", 10, removalListener.getCount()); CacheTesting.expireEntries((LoadingCache<?, ?>) cache, EXPIRING_TIME, ticker); // ensure that no new notifications are sent assertEquals("Eviction notifications must be received", 10, removalListener.getCount()); }
/** * Peeks into the cache's internals to verify that its eviction queue is consistent. Verifies that * the prev/next links are correct, and that all items in each segment are also in that segment's * eviction (recency) queue. */ static void checkEviction(Cache<?, ?> cache) { if (hasLocalCache(cache)) { checkEviction(toLocalCache(cache)); } }
static void expireEntries(Cache<?, ?> cache, long expiringTime, FakeTicker ticker) { checkNotNull(ticker); expireEntries(toLocalCache(cache), expiringTime, ticker); }
static void checkValidState(LocalCache<?, ?> cchm) { for (Segment<?, ?> segment : cchm.segments) { segment.cleanUp(); assertFalse(segment.isLocked()); Map<?, ?> table = segmentTable(segment); // cleanup and then check count after we have a strong reference to all entries segment.cleanUp(); // under high memory pressure keys/values may be nulled out but not yet enqueued assertThat(table.size()).isAtMost(segment.count); for (Entry<?, ?> entry : table.entrySet()) { assertNotNull(entry.getKey()); assertNotNull(entry.getValue()); assertSame(entry.getValue(), cchm.get(entry.getKey())); } } checkEviction(cchm); checkExpiration(cchm); }
assertThat(cache.asMap().keySet()).isEmpty(); CacheTesting.processPendingNotifications(cache); assertThat(removalListener.getCount()).isEqualTo(1); assertThat(cache.asMap().keySet()).containsExactly(2); CacheTesting.processPendingNotifications(cache); CacheTesting.checkValidState(cache); assertThat(removalListener.getCount()).isEqualTo(1); assertThat(cache.asMap().keySet()).containsExactly(2, 4); CacheTesting.processPendingNotifications(cache); assertThat(removalListener.getCount()).isEqualTo(1); assertThat(cache.asMap().keySet()).containsExactly(2, 4); CacheTesting.processPendingNotifications(cache); assertThat(removalListener.getCount()).isEqualTo(2); CacheTesting.checkValidState(cache);
static int expirationQueueSize(Cache<?, ?> cache) { return Math.max(accessQueueSize(cache), writeQueueSize(cache)); }
public void testUpdateRecency_onGet() { IdentityLoader<Integer> loader = identityLoader(); final LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(MAX_SIZE).build(loader); CacheTesting.checkRecency( cache, MAX_SIZE, new Receiver<ReferenceEntry<Integer, Integer>>() { @Override public void accept(ReferenceEntry<Integer, Integer> entry) { cache.getUnchecked(entry.getKey()); } }); }
static void checkEviction(LocalCache<?, ?> map) { if (map.evictsBySize()) { for (Segment<?, ?> segment : map.segments) { drainRecencyQueue(segment); assertEquals(0, segment.recencyQueue.size()); assertEquals(0, segment.readCount.get()); ReferenceEntry<?, ?> prev = null; for (ReferenceEntry<?, ?> current : segment.accessQueue) { if (prev != null) { assertSame(prev, current.getPreviousInAccessQueue()); assertSame(prev.getNextInAccessQueue(), current); } Object key = current.getKey(); if (key != null) { assertSame(current, segment.getEntry(key, current.getHash())); } prev = current; } } } else { for (Segment<?, ?> segment : map.segments) { assertEquals(0, segment.recencyQueue.size()); } } }