use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheExpirationTest method testExpiration_expireAfterAccess.
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);
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheExpirationTest method testExpiration_invalidateAll.
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);
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheExpirationTest method testRemovalListener_expireAfterWrite.
public void testRemovalListener_expireAfterWrite() {
FakeTicker ticker = new FakeTicker();
final AtomicInteger evictionCount = new AtomicInteger();
final AtomicInteger applyCount = new AtomicInteger();
final AtomicInteger totalSum = new AtomicInteger();
RemovalListener<Integer, AtomicInteger> removalListener = new RemovalListener<Integer, AtomicInteger>() {
@Override
public void onRemoval(RemovalNotification<Integer, AtomicInteger> notification) {
if (notification.wasEvicted()) {
evictionCount.incrementAndGet();
totalSum.addAndGet(notification.getValue().get());
}
}
};
CacheLoader<Integer, AtomicInteger> loader = new CacheLoader<Integer, AtomicInteger>() {
@Override
public AtomicInteger load(Integer key) {
applyCount.incrementAndGet();
return new AtomicInteger();
}
};
LoadingCache<Integer, AtomicInteger> cache = CacheBuilder.newBuilder().removalListener(removalListener).expireAfterWrite(10, MILLISECONDS).ticker(ticker).build(loader);
// Increment 100 times
for (int i = 0; i < 100; ++i) {
cache.getUnchecked(10).incrementAndGet();
ticker.advance(1, MILLISECONDS);
}
assertEquals(evictionCount.get() + 1, applyCount.get());
int remaining = cache.getUnchecked(10).get();
assertEquals(100, totalSum.get() + remaining);
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class LocalCacheTest method testExpireAfterWrite.
public void testExpireAfterWrite() {
FakeTicker ticker = new FakeTicker();
LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).ticker(ticker).expireAfterWrite(2, TimeUnit.NANOSECONDS));
Segment<Object, Object> segment = map.segments[0];
Object key = new Object();
Object value = new Object();
map.put(key, value);
ReferenceEntry<Object, Object> entry = map.getEntry(key);
assertTrue(map.isLive(entry, ticker.read()));
segment.writeQueue.add(entry);
assertSame(value, map.get(key));
assertSame(entry, segment.writeQueue.peek());
assertEquals(1, segment.writeQueue.size());
segment.recordRead(entry, ticker.read());
segment.expireEntries(ticker.read());
assertSame(value, map.get(key));
assertSame(entry, segment.writeQueue.peek());
assertEquals(1, segment.writeQueue.size());
ticker.advance(1);
segment.recordRead(entry, ticker.read());
segment.expireEntries(ticker.read());
assertSame(value, map.get(key));
assertSame(entry, segment.writeQueue.peek());
assertEquals(1, segment.writeQueue.size());
ticker.advance(1);
assertNull(map.get(key));
segment.expireEntries(ticker.read());
assertNull(map.get(key));
assertTrue(segment.writeQueue.isEmpty());
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class LocalCacheTest method testRemovalListener_expired.
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());
}
Aggregations