public void testExpiration_invalidateAll() { FakeTicker ticker = new FakeTicker(); QueuingRemovalListener<Integer, Integer> listener = TestingRemovalListeners.queuingRemovalListener(); Cache<Integer, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(1, TimeUnit.MINUTES) .removalListener(listener) .ticker(ticker) .build(); cache.put(1, 1); ticker.advance(10, TimeUnit.MINUTES); cache.invalidateAll(); assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED); }
assertTrue(listener.isEmpty()); assertEquals(0, segment.count); assertNull(segment.get(key, hash)); assertSame(value, segment.get(key, hash)); assertEquals(1, segment.count); assertTrue(listener.isEmpty()); assertEquals(1, segment.count); assertSame(value, segment.get(key, hash)); RemovalNotification<Object, Object> notification = listener.remove(); assertEquals(immutableEntry(key, value2), notification); assertEquals(RemovalCause.REPLACED, notification.getCause()); assertTrue(listener.isEmpty()); listener.clear(); assertEquals(0, segment.count); table.set(index, entry); assertSame(value3, segment.get(key, hash)); assertEquals(1, segment.count); assertTrue(listener.isEmpty()); assertSame(value4, segment.get(key, hash)); assertEquals(1, segment.count); notification = listener.remove(); assertEquals(immutableEntry(key, value3), notification); assertEquals(RemovalCause.REPLACED, notification.getCause()); 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 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_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()); }
public void testGet_expireAfterWrite() { Object computed = new Object(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .expireAfterWrite(0, SECONDS) .removalListener(listener) .build(constantLoader(computed)); Object key = new Object(); assertSame(computed, cache.getUnchecked(key)); RemovalNotification<Object, Object> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(computed, notification.getValue()); assertSame(RemovalCause.SIZE, notification.getCause()); assertTrue(listener.isEmpty()); checkEmpty(cache); }
public void testGet_runtimeException() { final RuntimeException e = new RuntimeException(); LoadingCache<Object, Object> map = CacheBuilder.newBuilder() .maximumSize(0) .removalListener(listener) .build(exceptionLoader(e)); try { map.getUnchecked(new Object()); fail(); } catch (UncheckedExecutionException uee) { assertSame(e, uee.getCause()); } assertTrue(listener.isEmpty()); checkEmpty(map); } }
public void testExpiration_invalidateAll() { FakeTicker ticker = new FakeTicker(); QueuingRemovalListener<Integer, Integer> listener = TestingRemovalListeners.queuingRemovalListener(); Cache<Integer, Integer> cache = CacheBuilder.newBuilder() .expireAfterAccess(1, TimeUnit.MINUTES) .removalListener(listener) .ticker(ticker) .build(); cache.put(1, 1); ticker.advance(10, TimeUnit.MINUTES); cache.invalidateAll(); assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED); }
public void testGet() { Object computed = new Object(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .maximumSize(0) .removalListener(listener) .build(constantLoader(computed)); Object key = new Object(); assertSame(computed, cache.getUnchecked(key)); RemovalNotification<Object, Object> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(computed, notification.getValue()); assertSame(RemovalCause.SIZE, notification.getCause()); assertTrue(listener.isEmpty()); checkEmpty(cache); }
public void testGet_expireAfterAccess() { Object computed = new Object(); LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .expireAfterAccess(0, SECONDS) .removalListener(listener) .build(constantLoader(computed)); Object key = new Object(); assertSame(computed, cache.getUnchecked(key)); RemovalNotification<Object, Object> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(computed, notification.getValue()); assertSame(RemovalCause.SIZE, notification.getCause()); assertTrue(listener.isEmpty()); checkEmpty(cache); }
static <K, V> void assertNotified( QueuingRemovalListener<K, V> listener, K key, V value, RemovalCause cause) { RemovalNotification<K, V> notification = listener.remove(); assertSame(key, notification.getKey()); assertSame(value, notification.getValue()); assertSame(cause, notification.getCause()); }
public void testGet_computeNull() { LoadingCache<Object, Object> cache = CacheBuilder.newBuilder() .maximumSize(0) .removalListener(listener) .build(constantLoader(null)); try { cache.getUnchecked(new Object()); fail(); } catch (InvalidCacheLoadException e) { /* expected */} assertTrue(listener.isEmpty()); checkEmpty(cache); }