use of com.google.common.util.concurrent.ExecutionError in project caffeine by ben-manes.
the class CacheLoadingTest method testLoadError.
public void testLoadError() throws ExecutionException {
Error e = new Error();
CacheLoader<Object, Object> loader = errorLoader(e);
LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder().recordStats().executor(MoreExecutors.directExecutor()), loader);
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.get(new Object());
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getUnchecked(new Object());
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
cache.refresh(new Object());
checkLoggedCause(e);
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(3, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
final Error callableError = new Error();
try {
cache.get(new Object(), new Callable<Object>() {
@Override
public Object call() {
throw callableError;
}
});
fail();
} catch (ExecutionError expected) {
assertSame(callableError, expected.getCause());
}
stats = cache.stats();
assertEquals(3, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(4, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getAll(asList(new Object()));
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(4, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(5, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
}
use of com.google.common.util.concurrent.ExecutionError in project guava by google.
the class AbstractLoadingCacheTest method testGetUnchecked_error.
public void testGetUnchecked_error() {
final Error cause = new Error();
final AtomicReference<Object> valueRef = new AtomicReference<>();
LoadingCache<Object, Object> cache = new AbstractLoadingCache<Object, Object>() {
@Override
public Object get(Object key) throws ExecutionException {
Object v = valueRef.get();
if (v == null) {
throw new ExecutionError(cause);
}
return v;
}
@Override
public Object getIfPresent(Object key) {
return valueRef.get();
}
};
try {
cache.getUnchecked(new Object());
fail();
} catch (ExecutionError expected) {
assertThat(expected).hasCauseThat().isEqualTo(cause);
}
Object newValue = new Object();
valueRef.set(newValue);
assertSame(newValue, cache.getUnchecked(new Object()));
}
use of com.google.common.util.concurrent.ExecutionError in project guava by google.
the class CacheLoadingTest method testBulkLoadError.
public void testBulkLoadError() throws ExecutionException {
Error e = new Error();
CacheLoader<Object, Object> loader = errorLoader(e);
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(bulkLoader(loader));
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getAll(asList(new Object()));
fail();
} catch (ExecutionError expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
}
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
}
use of com.google.common.util.concurrent.ExecutionError in project guava by google.
the class LocalCache method loadAll.
/**
* Returns the result of calling {@link CacheLoader#loadAll}, or null if {@code loader} doesn't
* implement {@code loadAll}.
*/
@Nullable
Map<K, V> loadAll(Set<? extends K> keys, CacheLoader<? super K, V> loader) throws ExecutionException {
checkNotNull(loader);
checkNotNull(keys);
Stopwatch stopwatch = Stopwatch.createStarted();
Map<K, V> result;
boolean success = false;
try {
// safe since all keys extend K
@SuppressWarnings("unchecked") Map<K, V> map = (Map<K, V>) loader.loadAll(keys);
result = map;
success = true;
} catch (UnsupportedLoadingOperationException e) {
success = true;
throw e;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ExecutionException(e);
} catch (RuntimeException e) {
throw new UncheckedExecutionException(e);
} catch (Exception e) {
throw new ExecutionException(e);
} catch (Error e) {
throw new ExecutionError(e);
} finally {
if (!success) {
globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
}
}
if (result == null) {
globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
throw new InvalidCacheLoadException(loader + " returned null map from loadAll");
}
stopwatch.stop();
// TODO(fry): batch by segment
boolean nullsPresent = false;
for (Entry<K, V> entry : result.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
if (key == null || value == null) {
// delay failure until non-null entries are stored
nullsPresent = true;
} else {
put(key, value);
}
}
if (nullsPresent) {
globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
throw new InvalidCacheLoadException(loader + " returned null keys or values from loadAll");
}
// TODO(fry): record count of loaded entries
globalStatsCounter.recordLoadSuccess(stopwatch.elapsed(NANOSECONDS));
return result;
}
use of com.google.common.util.concurrent.ExecutionError in project guava by hceylan.
the class CacheLoadingTest method testLoadError.
public void testLoadError() throws ExecutionException {
Error e = new Error();
CacheLoader<Object, Object> loader = errorLoader(e);
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().build(loader);
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.get(new Object());
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getUnchecked(new Object());
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
cache.refresh(new Object());
checkLoggedCause(e);
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(3, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
final Error callableError = new Error();
try {
cache.get(new Object(), new Callable<Object>() {
@Override
public Object call() {
throw callableError;
}
});
fail();
} catch (ExecutionError expected) {
assertSame(callableError, expected.getCause());
}
stats = cache.stats();
assertEquals(3, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(4, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getAll(asList(new Object()));
fail();
} catch (ExecutionError expected) {
assertSame(e, expected.getCause());
}
stats = cache.stats();
assertEquals(4, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(5, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
}
Aggregations