use of com.google.common.cache.LocalCache.LoadingValueReference in project guava by hceylan.
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));
}
use of com.google.common.cache.LocalCache.LoadingValueReference 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));
}
Aggregations