use of com.google.common.testing.FakeTicker in project guava by google.
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().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.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 google.
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 = 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 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 neo4j by neo4j.
the class LdapCachingTest method setup.
@Before
public void setup() throws Throwable {
SecurityLog securityLog = mock(SecurityLog.class);
InternalFlatFileRealm internalFlatFileRealm = new InternalFlatFileRealm(new InMemoryUserRepository(), new InMemoryRoleRepository(), new BasicPasswordPolicy(), new RateLimitedAuthenticationStrategy(Clock.systemUTC(), 3), mock(JobScheduler.class), new InMemoryUserRepository(), new InMemoryUserRepository());
testRealm = new TestRealm(getLdapConfig(), securityLog, new SecureHasher());
List<Realm> realms = listOf(internalFlatFileRealm, testRealm);
fakeTicker = new FakeTicker();
authManager = new MultiRealmAuthManager(internalFlatFileRealm, realms, new ShiroCaffeineCache.Manager(fakeTicker::read, 100, 10), securityLog, false);
authManager.init();
authManager.start();
authManager.getUserManager().newUser("mike", "123", false);
authManager.getUserManager().newUser("mats", "456", false);
}
use of com.google.common.testing.FakeTicker in project google-cloud-java by GoogleCloudPlatform.
the class WatchdogInterceptorTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ticker = new FakeTicker();
watchdog = new WatchdogInterceptor(TIMEOUT_NANOS, TimeUnit.NANOSECONDS, ticker);
// Nothing special about the deadline: we just want a particular CallOptions instance to expect.
options = CallOptions.DEFAULT.withDeadlineAfter(10, TimeUnit.SECONDS);
metadata = new Metadata();
}
Aggregations