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_maxWeight() { CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener(); IdentityLoader<Integer> loader = identityLoader(); LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder() .maximumWeight(2 * MAX_SIZE) .weigher(constantWeigher(2)) .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); }
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()); }
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()); }
public void testReloadAfterFailure() throws ExecutionException { final AtomicInteger count = new AtomicInteger(); final Exception e = new IllegalStateException("exception to trigger failure on first load()"); CacheLoader<Integer, String> failOnceFunction = new CacheLoader<Integer, String>() { @Override public String load(Integer key) throws Exception { if (count.getAndIncrement() == 0) { throw e; } return key.toString(); } }; CountingRemovalListener<Integer, String> removalListener = countingRemovalListener(); LoadingCache<Integer, String> cache = CacheBuilder.newBuilder() .removalListener(removalListener) .build(failOnceFunction); try { cache.getUnchecked(1); fail(); } catch (UncheckedExecutionException ue) { assertSame(e, ue.getCause()); } assertEquals("1", cache.getUnchecked(1)); assertEquals(0, removalListener.getCount()); count.set(0); cache.refresh(2); checkLoggedCause(e); assertEquals("2", cache.getUnchecked(2)); assertEquals(0, removalListener.getCount()); }
public void testEviction_overflow() { CountingRemovalListener<Object, Object> removalListener = countingRemovalListener(); IdentityLoader<Object> loader = identityLoader(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .concurrencyLevel(1) .maximumWeight(1L << 31) .weigher(constantWeigher(Integer.MAX_VALUE)) .removalListener(removalListener) .build(loader); cache.getUnchecked(objectWithHash(0)); cache.getUnchecked(objectWithHash(0)); CacheTesting.processPendingNotifications(cache); assertEquals(1, removalListener.getCount()); }
public void testExpiration_expireAfterWrite() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterWrite(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); checkExpiration(cache, loader, ticker, removalListener); }
public void testExpiration_expireAfterAccess() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); checkExpiration(cache, loader, ticker, removalListener); }
public void testExpiringGet_expireAfterWrite() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterWrite(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); runExpirationTest(cache, loader, ticker, removalListener); }
public void testExpiringGet_expireAfterAccess() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); runExpirationTest(cache, loader, ticker, removalListener); }
public void testRemovalScheduler_expireAfterWrite() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterWrite(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME); }
public void testRemovalScheduler_expireAfterAccess() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME); }
public void testRemovalScheduler_expireAfterBoth() { FakeTicker ticker = new FakeTicker(); CountingRemovalListener<String, Integer> removalListener = countingRemovalListener(); WatchedCreatorLoader loader = new WatchedCreatorLoader(); LoadingCache<String, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(EXPIRING_TIME, MILLISECONDS) .expireAfterWrite(EXPIRING_TIME, MILLISECONDS) .removalListener(removalListener) .ticker(ticker) .build(loader); runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME); }
@GwtIncompatible // CacheTesting public void testNullCache() { CountingRemovalListener<Object, Object> listener = countingRemovalListener(); LoadingCache<Object, Object> nullCache = new CacheBuilder<Object, Object>() .maximumSize(0) .removalListener(listener) .build(identityLoader()); assertEquals(0, nullCache.size()); Object key = new Object(); assertSame(key, nullCache.getUnchecked(key)); assertEquals(1, listener.getCount()); assertEquals(0, nullCache.size()); CacheTesting.checkEmpty(nullCache.asMap()); }