Search in sources :

Example 16 with ReferenceEntry

use of com.google.common.cache.LocalCache.ReferenceEntry in project guava by google.

the class LocalCacheTest method testCopyEntry_computing.

public void testCopyEntry_computing() {
    final CountDownLatch startSignal = new CountDownLatch(1);
    final CountDownLatch computingSignal = new CountDownLatch(1);
    final CountDownLatch doneSignal = new CountDownLatch(2);
    final Object computedObject = new Object();
    final CacheLoader<Object, Object> loader = new CacheLoader<Object, Object>() {

        @Override
        public Object load(Object key) throws Exception {
            computingSignal.countDown();
            startSignal.await();
            return computedObject;
        }
    };
    QueuingRemovalListener<Object, Object> listener = queuingRemovalListener();
    CacheBuilder<Object, Object> builder = createCacheBuilder().concurrencyLevel(1).removalListener(listener);
    final LocalCache<Object, Object> map = makeLocalCache(builder);
    Segment<Object, Object> segment = map.segments[0];
    AtomicReferenceArray<ReferenceEntry<Object, Object>> table = segment.table;
    assertTrue(listener.isEmpty());
    final Object one = new Object();
    int hash = map.hash(one);
    int index = hash & (table.length() - 1);
    new Thread() {

        @Override
        public void run() {
            try {
                map.get(one, loader);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
            doneSignal.countDown();
        }
    }.start();
    try {
        computingSignal.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    new Thread() {

        @Override
        public void run() {
            try {
                map.get(one, loader);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
            doneSignal.countDown();
        }
    }.start();
    ReferenceEntry<Object, Object> entry = segment.getEntry(one, hash);
    ReferenceEntry<Object, Object> newEntry = segment.copyEntry(entry, null);
    table.set(index, newEntry);
    @SuppressWarnings("unchecked") LoadingValueReference<Object, Object> valueReference = (LoadingValueReference) newEntry.getValueReference();
    assertFalse(valueReference.futureValue.isDone());
    startSignal.countDown();
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    // force notifications
    map.cleanUp();
    assertTrue(listener.isEmpty());
    assertTrue(map.containsKey(one));
    assertEquals(1, map.size());
    assertSame(computedObject, map.get(one));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ReferenceEntry(com.google.common.cache.LocalCache.ReferenceEntry) LoadingValueReference(com.google.common.cache.LocalCache.LoadingValueReference) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ReferenceEntry (com.google.common.cache.LocalCache.ReferenceEntry)16 Random (java.util.Random)8 CountingLoader (com.google.common.cache.TestingCacheLoaders.CountingLoader)6 LoadingValueReference (com.google.common.cache.LocalCache.LoadingValueReference)2 FakeTicker (com.google.common.testing.FakeTicker)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2