use of com.google.common.testing.FakeTicker in project guava by hceylan.
the class CacheExpirationTest method testRemovalScheduler_expireAfterAccess.
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);
}
use of com.google.common.testing.FakeTicker in project guava by hceylan.
the class CacheExpirationTest method testRemovalScheduler_expireAfterBoth.
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);
}
use of com.google.common.testing.FakeTicker in project guava by hceylan.
the class CacheExpirationTest method testExpiringGet_expireAfterAccess.
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);
}
use of com.google.common.testing.FakeTicker in project guava by hceylan.
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 hceylan.
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);
}
Aggregations