Search in sources :

Example 66 with FakeTicker

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

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(4, MILLISECONDS).expireAfterAccess(2, 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();
    ASSERT.that(keySet).hasContentsAnyOrder(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);
    ASSERT.that(keySet).hasContentsAnyOrder(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);
    ASSERT.that(keySet).hasContentsAnyOrder(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);
    ASSERT.that(keySet).hasContentsAnyOrder(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);
    ASSERT.that(keySet).hasContentsAnyOrder(3, 6);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 67 with FakeTicker

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

the class CacheExpirationTest method testExpirationOrder_write.

public void testExpirationOrder_write() 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(10, MILLISECONDS).ticker(ticker).build(loader);
    for (int i = 0; i < 10; i++) {
        cache.getUnchecked(i);
        ticker.advance(1, MILLISECONDS);
    }
    Set<Integer> keySet = cache.asMap().keySet();
    ASSERT.that(keySet).hasContentsAnyOrder(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    // 0 expires
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(1, 2, 3, 4, 5, 6, 7, 8, 9);
    // get doesn't stop 1 from expiring
    getAll(cache, asList(0, 1, 2));
    CacheTesting.drainRecencyQueues(cache);
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(2, 3, 4, 5, 6, 7, 8, 9, 0);
    // get(K, Callable) doesn't stop 2 from expiring
    cache.get(2, Callables.returning(-2));
    CacheTesting.drainRecencyQueues(cache);
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(3, 4, 5, 6, 7, 8, 9, 0);
    // asMap.put saves 3
    cache.asMap().put(3, -3);
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(4, 5, 6, 7, 8, 9, 0, 3);
    // asMap.replace saves 4
    cache.asMap().replace(4, -4);
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(5, 6, 7, 8, 9, 0, 3, 4);
    // 5 expires
    ticker.advance(1, MILLISECONDS);
    ASSERT.that(keySet).hasContentsAnyOrder(6, 7, 8, 9, 0, 3, 4);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeTicker(com.google.common.testing.FakeTicker)

Example 68 with FakeTicker

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

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 = CacheBuilder.newBuilder().expireAfterWrite(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 69 with FakeTicker

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

the class LocalCacheTest method testSegmentGetAndContains.

public void testSegmentGetAndContains() {
    FakeTicker ticker = new FakeTicker();
    LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder().concurrencyLevel(1).ticker(ticker).expireAfterAccess(1, TimeUnit.NANOSECONDS));
    Segment<Object, Object> segment = map.segments[0];
    // TODO(fry): check recency ordering
    Object key = new Object();
    int hash = map.hash(key);
    Object value = new Object();
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    int index = hash & (table.length() - 1);
    ReferenceEntry<Object, Object> entry = map.newEntry(key, hash, null);
    ValueReference<Object, Object> valueRef = map.newValueReference(entry, value, 1);
    entry.setValueReference(valueRef);
    assertNull(segment.get(key, hash));
    // count == 0
    table.set(index, entry);
    assertNull(segment.get(key, hash));
    assertFalse(segment.containsKey(key, hash));
    assertFalse(segment.containsValue(value));
    // count == 1
    segment.count++;
    assertSame(value, segment.get(key, hash));
    assertTrue(segment.containsKey(key, hash));
    assertTrue(segment.containsValue(value));
    // don't see absent values now that count > 0
    assertNull(segment.get(new Object(), hash));
    // null key
    DummyEntry<Object, Object> nullEntry = DummyEntry.create(null, hash, entry);
    Object nullValue = new Object();
    ValueReference<Object, Object> nullValueRef = map.newValueReference(nullEntry, nullValue, 1);
    nullEntry.setValueReference(nullValueRef);
    table.set(index, nullEntry);
    // skip the null key
    assertSame(value, segment.get(key, hash));
    assertTrue(segment.containsKey(key, hash));
    assertTrue(segment.containsValue(value));
    assertFalse(segment.containsValue(nullValue));
    // hash collision
    DummyEntry<Object, Object> dummy = DummyEntry.create(new Object(), hash, entry);
    Object dummyValue = new Object();
    ValueReference<Object, Object> dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
    dummy.setValueReference(dummyValueRef);
    table.set(index, dummy);
    assertSame(value, segment.get(key, hash));
    assertTrue(segment.containsKey(key, hash));
    assertTrue(segment.containsValue(value));
    assertTrue(segment.containsValue(dummyValue));
    // key collision
    dummy = DummyEntry.create(key, hash, entry);
    dummyValue = new Object();
    dummyValueRef = map.newValueReference(dummy, dummyValue, 1);
    dummy.setValueReference(dummyValueRef);
    table.set(index, dummy);
    // returns the most recent entry
    assertSame(dummyValue, segment.get(key, hash));
    assertTrue(segment.containsKey(key, hash));
    assertTrue(segment.containsValue(value));
    assertTrue(segment.containsValue(dummyValue));
    // expired
    dummy.setAccessTime(ticker.read() - 2);
    assertNull(segment.get(key, hash));
    assertFalse(segment.containsKey(key, hash));
    assertTrue(segment.containsValue(value));
    assertFalse(segment.containsValue(dummyValue));
}
Also used : ReferenceEntry(com.google.common.cache.LocalCache.ReferenceEntry) FakeTicker(com.google.common.testing.FakeTicker)

Example 70 with FakeTicker

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

the class ShiroCaffeineCacheTest method setUp.

@Before
public void setUp() {
    fakeTicker = new FakeTicker();
    cache = new ShiroCaffeineCache<>(fakeTicker::read, Runnable::run, TTL, 5);
}
Also used : FakeTicker(com.google.common.testing.FakeTicker) Before(org.junit.Before)

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