use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
the class AbstractJCacheTest method before.
@BeforeMethod(alwaysRun = true)
public void before() {
ticker = new FakeTicker();
jcache = (CacheProxy<Integer, Integer>) cacheManager.createCache("jcache", getConfiguration());
jcacheLoading = (LoadingCacheProxy<Integer, Integer>) cacheManager.createCache("jcacheLoading", getLoadingConfiguration());
}
use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
the class CacheExpirationTest method testExpiration_expireAfterWrite.
public void testExpiration_expireAfterWrite() {
FakeTicker ticker = new FakeTicker();
CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
WatchedCreatorLoader loader = new WatchedCreatorLoader();
LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder().expireAfterWrite(EXPIRING_TIME, MILLISECONDS).executor(MoreExecutors.directExecutor()).removalListener(removalListener).ticker(ticker::read), loader);
checkExpiration(cache, loader, ticker, removalListener);
}
use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
the class CacheExpirationTest method testExpirationOrder_access.
public void testExpirationOrder_access() {
// test lru within a single segment
FakeTicker ticker = new FakeTicker();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder().expireAfterAccess(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);
// reorder
getAll(cache, asList(0, 1, 2));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(2, MILLISECONDS);
assertThat(keySet).containsExactly(3, 4, 5, 6, 7, 8, 9, 0, 1, 2);
// 3 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(4, 5, 6, 7, 8, 9, 0, 1, 2);
// reorder
getAll(cache, asList(5, 7, 9));
CacheTesting.drainRecencyQueues(cache);
assertThat(keySet).containsExactly(4, 6, 8, 0, 1, 2, 5, 7, 9);
// 4 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(6, 8, 0, 1, 2, 5, 7, 9);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(6, 8, 0, 1, 2, 5, 7, 9);
// 6 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(8, 0, 1, 2, 5, 7, 9);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(8, 0, 1, 2, 5, 7, 9);
// 8 expires
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(0, 1, 2, 5, 7, 9);
}
use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.
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 = 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 testRefreshNull.
public void testRefreshNull() {
final Object one = 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(null);
}
};
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());
}
Aggregations