Search in sources :

Example 1 with CountingLoader

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

the class LocalCacheTest method testComputePartiallyCollectedValue.

public void testComputePartiallyCollectedValue() throws ExecutionException {
    CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
    CountingLoader loader = new CountingLoader();
    LocalCache<Object, Object> map = makeLocalCache(builder);
    Segment<Object, Object> segment = map.segments[0];
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    assertEquals(0, loader.getCount());
    Object key = new Object();
    int hash = map.hash(key);
    Object value = new Object();
    int index = hash & (table.length() - 1);
    DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
    DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
    entry.setValueReference(valueRef);
    table.set(index, entry);
    segment.count++;
    assertSame(value, map.get(key, loader));
    assertEquals(0, loader.getCount());
    assertEquals(1, segment.count);
    valueRef.clear();
    assertNotSame(value, map.get(key, loader));
    assertEquals(1, loader.getCount());
    assertEquals(1, segment.count);
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader) ReferenceEntry(com.google.common.cache.LocalCache.ReferenceEntry)

Example 2 with CountingLoader

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

the class LocalCacheTest method testComputePartiallyCollectedKey.

public void testComputePartiallyCollectedKey() throws ExecutionException {
    CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1);
    CountingLoader loader = new CountingLoader();
    LocalCache<Object, Object> map = makeLocalCache(builder);
    Segment<Object, Object> segment = map.segments[0];
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    assertEquals(0, loader.getCount());
    Object key = new Object();
    int hash = map.hash(key);
    Object value = new Object();
    int index = hash & (table.length() - 1);
    DummyEntry<Object, Object> entry = DummyEntry.create(key, hash, null);
    DummyValueReference<Object, Object> valueRef = DummyValueReference.create(value);
    entry.setValueReference(valueRef);
    table.set(index, entry);
    segment.count++;
    assertSame(value, map.get(key, loader));
    assertEquals(0, loader.getCount());
    assertEquals(1, segment.count);
    entry.clearKey();
    assertNotSame(value, map.get(key, loader));
    assertEquals(1, loader.getCount());
    assertEquals(2, segment.count);
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader) ReferenceEntry(com.google.common.cache.LocalCache.ReferenceEntry)

Example 3 with CountingLoader

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

the class LocalCacheTest method testRecordReadOnCompute.

public void testRecordReadOnCompute() throws ExecutionException {
    CountingLoader loader = new CountingLoader();
    for (CacheBuilder<Object, Object> builder : allEvictingMakers()) {
        LocalCache<Object, Object> map = makeLocalCache(builder.concurrencyLevel(1));
        Segment<Object, Object> segment = map.segments[0];
        List<ReferenceEntry<Object, Object>> writeOrder = Lists.newLinkedList();
        List<ReferenceEntry<Object, Object>> readOrder = Lists.newLinkedList();
        for (int i = 0; i < SMALL_MAX_SIZE; i++) {
            Object key = new Object();
            int hash = map.hash(key);
            map.get(key, loader);
            ReferenceEntry<Object, Object> entry = segment.getEntry(key, hash);
            writeOrder.add(entry);
            readOrder.add(entry);
        }
        checkEvictionQueues(map, segment, readOrder, writeOrder);
        checkExpirationTimes(map);
        assertTrue(segment.recencyQueue.isEmpty());
        // access some of the elements
        Random random = new Random();
        List<ReferenceEntry<Object, Object>> reads = Lists.newArrayList();
        Iterator<ReferenceEntry<Object, Object>> i = readOrder.iterator();
        while (i.hasNext()) {
            ReferenceEntry<Object, Object> entry = i.next();
            if (random.nextBoolean()) {
                map.get(entry.getKey(), loader);
                reads.add(entry);
                i.remove();
                assertTrue(segment.recencyQueue.size() <= DRAIN_THRESHOLD);
            }
        }
        int undrainedIndex = reads.size() - segment.recencyQueue.size();
        checkAndDrainRecencyQueue(map, segment, reads.subList(undrainedIndex, reads.size()));
        readOrder.addAll(reads);
        checkEvictionQueues(map, segment, readOrder, writeOrder);
        checkExpirationTimes(map);
    }
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader) ReferenceEntry(com.google.common.cache.LocalCache.ReferenceEntry) Random(java.util.Random)

Example 4 with CountingLoader

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

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 5 with CountingLoader

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

the class CacheLoadingTest method testReloadAfterValueReclamation.

public void testReloadAfterValueReclamation() throws InterruptedException, ExecutionException {
    CountingLoader countingLoader = new CountingLoader();
    LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().weakValues().build(countingLoader);
    ConcurrentMap<Object, Object> map = cache.asMap();
    int iterations = 10;
    WeakReference<Object> ref = new WeakReference<Object>(null);
    int expectedComputations = 0;
    for (int i = 0; i < iterations; i++) {
        // The entry should get garbage collected and recomputed.
        Object oldValue = ref.get();
        if (oldValue == null) {
            expectedComputations++;
        }
        ref = new WeakReference<Object>(cache.getUnchecked(1));
        oldValue = null;
        Thread.sleep(i);
        System.gc();
    }
    assertEquals(expectedComputations, countingLoader.getCount());
    for (int i = 0; i < iterations; i++) {
        // The entry should get garbage collected and recomputed.
        Object oldValue = ref.get();
        if (oldValue == null) {
            expectedComputations++;
        }
        cache.refresh(1);
        checkNothingLogged();
        ref = new WeakReference<Object>(map.get(1));
        oldValue = null;
        Thread.sleep(i);
        System.gc();
    }
    assertEquals(expectedComputations, countingLoader.getCount());
}
Also used : CountingLoader(com.google.common.cache.TestingCacheLoaders.CountingLoader) WeakReference(java.lang.ref.WeakReference)

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