Search in sources :

Example 16 with CountingLoader

use of com.google.common.cache.TestingCacheLoaders.CountingLoader in project guava by google.

the class CacheLoadingTest method testReloadAfterSimulatedValueReclamation.

public void testReloadAfterSimulatedValueReclamation() throws ExecutionException {
    CountingLoader countingLoader = new CountingLoader();
    LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().concurrencyLevel(1).weakValues().build(countingLoader);
    Object key = new Object();
    assertNotNull(cache.getUnchecked(key));
    CacheTesting.simulateValueReclamation(cache, key);
    // this blocks if computation can't deal with partially-collected values
    assertNotNull(cache.getUnchecked(key));
    assertEquals(1, cache.size());
    assertEquals(2, countingLoader.getCount());
    CacheTesting.simulateValueReclamation(cache, key);
    cache.refresh(key);
    checkNothingLogged();
    assertEquals(1, cache.size());
    assertEquals(3, countingLoader.getCount());
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader)

Example 17 with CountingLoader

use of com.google.common.cache.TestingCacheLoaders.CountingLoader in project guava by google.

the class LocalCacheTest method testComputeExpiredEntry.

// Perhaps emulator clock does not update between the two get() calls?
@AndroidIncompatible
public void testComputeExpiredEntry() throws ExecutionException {
    CacheBuilder<Object, Object> builder = createCacheBuilder().expireAfterWrite(1, TimeUnit.NANOSECONDS);
    CountingLoader loader = new CountingLoader();
    LocalCache<Object, Object> map = makeLocalCache(builder);
    assertEquals(0, loader.getCount());
    Object key = new Object();
    Object one = map.get(key, loader);
    assertEquals(1, loader.getCount());
    Object two = map.get(key, loader);
    assertNotSame(one, two);
    assertEquals(2, loader.getCount());
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader)

Example 18 with CountingLoader

use of com.google.common.cache.TestingCacheLoaders.CountingLoader in project guava by google.

the class LocalCacheTest method testComputeExistingEntry.

public void testComputeExistingEntry() throws ExecutionException {
    CountingLoader loader = new CountingLoader();
    LocalCache<Object, Object> map = makeLocalCache(createCacheBuilder());
    assertEquals(0, loader.getCount());
    Object key = new Object();
    Object value = new Object();
    map.put(key, value);
    assertEquals(value, map.get(key, loader));
    assertEquals(0, loader.getCount());
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader)

Aggregations

CountingLoader (com.google.common.cache.TestingCacheLoaders.CountingLoader)18 ReferenceEntry (com.google.common.cache.LocalCache.ReferenceEntry)6 WeakReference (java.lang.ref.WeakReference)2 Random (java.util.Random)2