Search in sources :

Example 6 with CountingLoader

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

the class CacheLoadingTest method testReloadAfterSimulatedKeyReclamation.

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

Example 7 with CountingLoader

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

the class LocalCacheTest method testCompute.

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

Example 8 with CountingLoader

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

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);
    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 9 with CountingLoader

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

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)

Example 10 with CountingLoader

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

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)

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