use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheRefreshTest method testAutoRefresh.
public void testAutoRefresh() {
FakeTicker ticker = new FakeTicker();
IncrementingLoader loader = incrementingLoader();
LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().refreshAfterWrite(3, MILLISECONDS).expireAfterWrite(6, MILLISECONDS).lenientParsing().ticker(ticker).build(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);
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);
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);
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());
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheBuilderGwtTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
fakeTicker = new FakeTicker();
}
use of com.google.common.testing.FakeTicker in project guava by google.
the class CacheExpirationTest method testExpirationOrder_writeAccess.
public void testExpirationOrder_writeAccess() throws ExecutionException {
// test lru within a single segment
FakeTicker ticker = new FakeTicker();
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(5, MILLISECONDS).expireAfterAccess(3, MILLISECONDS).ticker(ticker).build(loader);
for (int i = 0; i < 5; i++) {
cache.getUnchecked(i);
}
ticker.advance(1, MILLISECONDS);
for (int i = 5; 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);
// get saves 1, 3; 0, 2, 4 expire
getAll(cache, asList(1, 3));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(5, 6, 7, 8, 9, 1, 3);
// get saves 6, 8; 5, 7, 9 expire
getAll(cache, asList(6, 8));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(1, 3, 6, 8);
// get fails to save 1, put saves 3
cache.asMap().put(3, -3);
getAll(cache, asList(1));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(6, 8, 3);
// get(K, Callable) fails to save 8, replace saves 6
cache.asMap().replace(6, -6);
cache.get(8, Callables.returning(-8));
CacheTesting.drainRecencyQueues(cache);
ticker.advance(1, MILLISECONDS);
assertThat(keySet).containsExactly(3, 6);
}
use of com.google.common.testing.FakeTicker in project guava by google.
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 google.
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);
}
Aggregations