use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
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 = CaffeinatedGuava.build(Caffeine.newBuilder().expireAfterAccess(EXPIRING_TIME, MILLISECONDS).expireAfterWrite(EXPIRING_TIME, MILLISECONDS).executor(MoreExecutors.directExecutor()).removalListener(removalListener).ticker(ticker::read), loader);
runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}
use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
the class CacheExpirationTest method testExpirationOrder_write.
public void testExpirationOrder_write() throws ExecutionException {
// test lru within a single segment
FakeTicker ticker = new FakeTicker();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder().expireAfterWrite(11, MILLISECONDS).ticker(ticker::read), loader);
for (int i = 0; i < 10; i++) {
cache.getUnchecked(i);
ticker.advance(1, MILLISECONDS);
}
Set<Integer> keySet = cache.asMap().keySet();
assertThat(keySet).containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
// 0 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9);
// get doesn't stop 1 from expiring
getAll(cache, asList(0, 1, 2));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(2, 3, 4, 5, 6, 7, 8, 9, 0);
// get(K, Callable) doesn't stop 2 from expiring
cache.get(2, Callables.returning(-2));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0);
// asMap.put saves 3
cache.asMap().put(3, -3);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(4, 5, 6, 7, 8, 9, 0, 3);
// asMap.replace saves 4
cache.asMap().replace(4, -4);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 0, 3, 4);
// 5 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(6, 7, 8, 9, 0, 3, 4);
}
use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
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 = 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);
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(2, 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 caffeine by ben-manes.
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 = 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.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 caffeine by ben-manes.
the class CacheRefreshTest method testAutoRefresh.
public void testAutoRefresh() {
FakeTicker ticker = new FakeTicker();
IncrementingLoader loader = incrementingLoader();
LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder().refreshAfterWrite(3, MILLISECONDS).expireAfterWrite(6, MILLISECONDS).executor(MoreExecutors.directExecutor()).ticker(ticker::read), loader);
int expectedLoads = 0;
int expectedReloads = 0;
for (int i = 0; i < 3; i++) {
assertEquals(Integer.valueOf(i), cache.getUnchecked(i));
expectedLoads++;
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
ticker.advance(1, MILLISECONDS);
}
assertEquals(Integer.valueOf(0), cache.getUnchecked(0));
assertEquals(Integer.valueOf(1), cache.getUnchecked(1));
assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
// refresh 0
ticker.advance(1, MILLISECONDS);
// Allow refresh to return old value while refreshing
cache.getUnchecked(0);
assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
expectedReloads++;
assertEquals(Integer.valueOf(1), cache.getUnchecked(1));
assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
// write to 1 to delay its refresh
cache.asMap().put(1, -1);
ticker.advance(1, MILLISECONDS);
assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
assertEquals(Integer.valueOf(2), cache.getUnchecked(2));
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
// refresh 2
ticker.advance(1, MILLISECONDS);
// Allow refresh to return old value while refreshing
cache.getUnchecked(2);
assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
expectedReloads++;
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
ticker.advance(1, MILLISECONDS);
assertEquals(Integer.valueOf(1), cache.getUnchecked(0));
assertEquals(Integer.valueOf(-1), cache.getUnchecked(1));
assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
// refresh 0 and 1
ticker.advance(1, MILLISECONDS);
// Allow refresh to return old value while refreshing
cache.getUnchecked(0);
// Allow refresh to return old value while refreshing
cache.getUnchecked(1);
assertEquals(Integer.valueOf(2), cache.getUnchecked(0));
expectedReloads++;
assertEquals(Integer.valueOf(0), cache.getUnchecked(1));
expectedReloads++;
assertEquals(Integer.valueOf(3), cache.getUnchecked(2));
assertEquals(expectedLoads, loader.getLoadCount());
assertEquals(expectedReloads, loader.getReloadCount());
}
Aggregations