Search in sources :

Example 11 with FakeTicker

use of com.google.common.testing.FakeTicker in project guava by hceylan.

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 12 with FakeTicker

use of com.google.common.testing.FakeTicker in project guava by hceylan.

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 13 with FakeTicker

use of com.google.common.testing.FakeTicker in project guava by hceylan.

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 = CacheBuilder.newBuilder().expireAfterAccess(EXPIRING_TIME, MILLISECONDS).removalListener(removalListener).ticker(ticker).build(loader);
    runExpirationTest(cache, loader, ticker, removalListener);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 14 with FakeTicker

use of com.google.common.testing.FakeTicker in project guava by hceylan.

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(RemovalNotification<Integer, AtomicInteger> notification) {
            if (notification.wasEvicted()) {
                evictionCount.incrementAndGet();
                totalSum.addAndGet(notification.getValue().get());
            }
        }
    };
    CacheLoader<Integer, AtomicInteger> loader = new CacheLoader<Integer, AtomicInteger>() {

        @Override
        public AtomicInteger load(Integer key) {
            applyCount.incrementAndGet();
            return new AtomicInteger();
        }
    };
    LoadingCache<Integer, AtomicInteger> cache = CacheBuilder.newBuilder().removalListener(removalListener).expireAfterWrite(10, MILLISECONDS).ticker(ticker).build(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)

Example 15 with FakeTicker

use of com.google.common.testing.FakeTicker in project guava by hceylan.

the class CacheExpirationTest method testExpiration_expireAfterAccess.

public void testExpiration_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);
    checkExpiration(cache, loader, ticker, removalListener);
}
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