use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
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 = CaffeinatedGuava.build(Caffeine.newBuilder().recordStats().ticker(ticker::read).refreshAfterWrite(1, MILLISECONDS).executor(MoreExecutors.directExecutor()), 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);
// Allow refresh to return old value while refreshing
cache.getUnchecked(key);
assertSame(two, cache.getUnchecked(key));
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(2, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(3, 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(4, stats.hitCount());
}
use of com.google.common.testing.FakeTicker in project guava by hceylan.
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().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 hceylan.
the class CacheLoadingTest method testRefresh_getIfPresent.
public void testRefresh_getIfPresent() {
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().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.getIfPresent(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.getIfPresent(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.getIfPresent(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 hceylan.
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().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 hceylan.
the class CacheLoadingTest method testRefreshError.
public void testRefreshError() {
final Object one = new Object();
final Error e = new Error();
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().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());
}
Aggregations