Search in sources :

Example 26 with FakeTicker

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

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)

Example 27 with FakeTicker

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

the class CacheExpirationTest method testExpiration_invalidateAll.

public void testExpiration_invalidateAll() {
    FakeTicker ticker = new FakeTicker();
    QueuingRemovalListener<Integer, Integer> listener = TestingRemovalListeners.queuingRemovalListener();
    Cache<Integer, Integer> cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).removalListener(listener).ticker(ticker).build();
    cache.put(1, 1);
    ticker.advance(10, TimeUnit.MINUTES);
    cache.invalidateAll();
    assertThat(listener.poll().getCause()).isEqualTo(RemovalCause.EXPIRED);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 28 with FakeTicker

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

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) QueuingRemovalListener(com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener) CountingRemovalListener(com.google.common.cache.TestingRemovalListeners.CountingRemovalListener)

Example 29 with FakeTicker

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

the class LocalCacheTest method testExpireAfterWrite.

public void testExpireAfterWrite() {
    FakeTicker ticker = new FakeTicker();
    LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).ticker(ticker).expireAfterWrite(2, TimeUnit.NANOSECONDS));
    Segment<Object, Object> segment = map.segments[0];
    Object key = new Object();
    Object value = new Object();
    map.put(key, value);
    ReferenceEntry<Object, Object> entry = map.getEntry(key);
    assertTrue(map.isLive(entry, ticker.read()));
    segment.writeQueue.add(entry);
    assertSame(value, map.get(key));
    assertSame(entry, segment.writeQueue.peek());
    assertEquals(1, segment.writeQueue.size());
    segment.recordRead(entry, ticker.read());
    segment.expireEntries(ticker.read());
    assertSame(value, map.get(key));
    assertSame(entry, segment.writeQueue.peek());
    assertEquals(1, segment.writeQueue.size());
    ticker.advance(1);
    segment.recordRead(entry, ticker.read());
    segment.expireEntries(ticker.read());
    assertSame(value, map.get(key));
    assertSame(entry, segment.writeQueue.peek());
    assertEquals(1, segment.writeQueue.size());
    ticker.advance(1);
    assertNull(map.get(key));
    segment.expireEntries(ticker.read());
    assertNull(map.get(key));
    assertTrue(segment.writeQueue.isEmpty());
}
Also used : FakeTicker(com.google.common.testing.FakeTicker)

Example 30 with FakeTicker

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

the class LocalCacheTest method testRemovalListener_expired.

public void testRemovalListener_expired() {
    FakeTicker ticker = new FakeTicker();
    QueuingRemovalListener<Object, Object> listener = queuingRemovalListener();
    LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).expireAfterWrite(3, TimeUnit.NANOSECONDS).ticker(ticker).removalListener(listener));
    assertTrue(listener.isEmpty());
    Object one = new Object();
    Object two = new Object();
    Object three = new Object();
    Object four = new Object();
    Object five = new Object();
    map.put(one, two);
    ticker.advance(1);
    map.put(two, three);
    ticker.advance(1);
    map.put(three, four);
    assertTrue(listener.isEmpty());
    ticker.advance(1);
    map.put(four, five);
    assertNotified(listener, one, two, RemovalCause.EXPIRED);
    assertTrue(listener.isEmpty());
}
Also used : 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