Search in sources :

Example 41 with FakeTicker

use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.

the class CacheLoadingTest method testRefreshUncheckedException.

public void testRefreshUncheckedException() {
    final Object one = new Object();
    final Exception e = new RuntimeException();
    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());
}
Also used : FakeTicker(com.google.common.testing.FakeTicker) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) ExecutionException(java.util.concurrent.ExecutionException)

Example 42 with FakeTicker

use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.

the class CacheExpirationTest method testExpiringGet_expireAfterAccess.

public void testExpiringGet_expireAfterAccess() {
    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).executor(MoreExecutors.directExecutor()).removalListener(removalListener).ticker(ticker::read), loader);
    runExpirationTest(cache, loader, ticker, removalListener);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 43 with FakeTicker

use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.

the class CacheExpirationTest method testExpiringGet_expireAfterWrite.

public void testExpiringGet_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);
    runExpirationTest(cache, loader, ticker, removalListener);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 44 with FakeTicker

use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.

the class CacheExpirationTest method testRemovalListener_expireAfterWrite.

public void testRemovalListener_expireAfterWrite() {
    FakeTicker ticker = new FakeTicker();
    final AtomicInteger evictionCount = new AtomicInteger();
    final AtomicInteger applyCount = new AtomicInteger();
    final AtomicInteger totalSum = new AtomicInteger();
    RemovalListener<Integer, AtomicInteger> removalListener = new RemovalListener<Integer, AtomicInteger>() {

        @Override
        public void onRemoval(Integer key, AtomicInteger value, RemovalCause cause) {
            if (cause.wasEvicted()) {
                evictionCount.incrementAndGet();
                totalSum.addAndGet(value.get());
            }
        }
    };
    CacheLoader<Integer, AtomicInteger> loader = new CacheLoader<Integer, AtomicInteger>() {

        @Override
        public AtomicInteger load(Integer key) {
            applyCount.incrementAndGet();
            return new AtomicInteger();
        }
    };
    LoadingCache<Integer, AtomicInteger> cache = CaffeinatedGuava.build(Caffeine.newBuilder().removalListener(removalListener).expireAfterWrite(10, MILLISECONDS).executor(MoreExecutors.directExecutor()).ticker(ticker::read), loader);
    // Increment 100 times
    for (int i = 0; i < 100; ++i) {
        cache.getUnchecked(10).incrementAndGet();
        ticker.advance(1, MILLISECONDS);
    }
    assertEquals(evictionCount.get() + 1, applyCount.get());
    int remaining = cache.getUnchecked(10).get();
    assertEquals(100, totalSum.get() + remaining);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker) TestingRemovalListeners.countingRemovalListener(com.google.common.cache.TestingRemovalListeners.countingRemovalListener) CountingRemovalListener(com.google.common.cache.TestingRemovalListeners.CountingRemovalListener) RemovalListener(com.github.benmanes.caffeine.cache.RemovalListener) RemovalCause(com.github.benmanes.caffeine.cache.RemovalCause)

Example 45 with FakeTicker

use of com.google.common.testing.FakeTicker in project caffeine by ben-manes.

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 = CaffeinatedGuava.build(Caffeine.newBuilder().expireAfterWrite(5, MILLISECONDS).expireAfterAccess(3, MILLISECONDS).ticker(ticker::read), 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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Aggregations

FakeTicker (com.google.common.testing.FakeTicker)70 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)34 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)6 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)6 ExecutionException (java.util.concurrent.ExecutionException)6 IOException (java.io.IOException)4 IncrementingLoader (com.google.common.cache.TestingCacheLoaders.IncrementingLoader)3 CountingRemovalListener (com.google.common.cache.TestingRemovalListeners.CountingRemovalListener)3 TestingRemovalListeners.countingRemovalListener (com.google.common.cache.TestingRemovalListeners.countingRemovalListener)3 ExecutionError (com.google.common.util.concurrent.ExecutionError)3 Before (org.junit.Before)3 ReferenceEntry (com.google.common.cache.LocalCache.ReferenceEntry)2 RemovalCause (com.github.benmanes.caffeine.cache.RemovalCause)1 RemovalListener (com.github.benmanes.caffeine.cache.RemovalListener)1 QueuingRemovalListener (com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener)1 Metadata (io.grpc.Metadata)1 Realm (org.apache.shiro.realm.Realm)1 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)1 BasicPasswordPolicy (org.neo4j.server.security.auth.BasicPasswordPolicy)1 InMemoryUserRepository (org.neo4j.server.security.auth.InMemoryUserRepository)1