use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheLoadingTest method testRefreshUncheckedException.
public void testRefreshUncheckedException() {
final Object one = new Object();
final Exception e = new RuntimeException();
FakeTicker ticker = new FakeTicker();
CacheLoader<Object, Object> loader = new CacheLoader<Object, Object>() {
@Override
public Object load(Object key) {
return one;
}
@Override
public ListenableFuture<Object> reload(Object key, Object oldValue) {
return Futures.immediateFailedFuture(e);
}
};
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().ticker(ticker).refreshAfterWrite(1, MILLISECONDS).build(loader);
Object key = new Object();
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(1, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
// refreshed
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(2, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(3, stats.hitCount());
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheLoadingTest method testRefresh.
public void testRefresh() {
final Object one = new Object();
final Object two = new Object();
FakeTicker ticker = new FakeTicker();
CacheLoader<Object, Object> loader = new CacheLoader<Object, Object>() {
@Override
public Object load(Object key) {
return one;
}
@Override
public ListenableFuture<Object> reload(Object key, Object oldValue) {
return Futures.immediateFuture(two);
}
};
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().ticker(ticker).refreshAfterWrite(1, MILLISECONDS).build(loader);
Object key = new Object();
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(1, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(two, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(2, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(2, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(two, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(2, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(3, stats.hitCount());
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheLoadingTest method testRefreshCheckedException.
public void testRefreshCheckedException() {
final Object one = new Object();
final Exception e = new Exception();
FakeTicker ticker = new FakeTicker();
CacheLoader<Object, Object> loader = new CacheLoader<Object, Object>() {
@Override
public Object load(Object key) {
return one;
}
@Override
public ListenableFuture<Object> reload(Object key, Object oldValue) {
return Futures.immediateFailedFuture(e);
}
};
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().ticker(ticker).refreshAfterWrite(1, MILLISECONDS).build(loader);
Object key = new Object();
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(1, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
// refreshed
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(2, stats.hitCount());
ticker.advance(1, MILLISECONDS);
assertSame(one, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(1, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(3, stats.hitCount());
}
use of com.google.common.testing.FakeTicker in project guava by google.
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 google.
the class CacheExpirationTest method testExpiringGet_expireAfterWrite.
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);
}
Aggregations