use of com.google.common.util.concurrent.ExecutionError in project caffeine by ben-manes.
the class CacheLoadingTest method testBulkLoadError.
public void testBulkLoadError() throws ExecutionException {
Error e = new Error();
CacheLoader<Object, Object> loader = errorLoader(e);
LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder().recordStats(), 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) {
assertSame(e, expected.getCause());
}
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 load.
private V load(Object key) throws ExecutionException {
long startTime = ticker.read();
V calculatedValue;
try {
/*
* This cast isn't safe, but we can rely on the fact that K is almost always passed to
* Map.get(), and tools like IDEs and Findbugs can catch situations where this isn't the
* case.
*
* The alternative is to add an overloaded method, but the chances of a user calling get()
* instead of the new API and the risks inherent in adding a new API outweigh this little
* hole.
*/
K castKey = (K) key;
calculatedValue = loader.load(castKey);
put(castKey, calculatedValue);
} catch (RuntimeException e) {
statsCounter.recordLoadException(ticker.read() - startTime);
throw new UncheckedExecutionException(e);
} catch (Exception e) {
statsCounter.recordLoadException(ticker.read() - startTime);
throw new ExecutionException(e);
} catch (Error e) {
statsCounter.recordLoadException(ticker.read() - startTime);
throw new ExecutionError(e);
}
if (calculatedValue == null) {
String message = loader + " returned null for key " + key + ".";
throw new CacheLoader.InvalidCacheLoadException(message);
}
statsCounter.recordLoadSuccess(ticker.read() - startTime);
return calculatedValue;
}
use of com.google.common.util.concurrent.ExecutionError in project guava by google.
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().recordStats().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());
}
use of com.google.common.util.concurrent.ExecutionError in project guava by hceylan.
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().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) {
assertSame(e, expected.getCause());
}
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 hceylan.
the class AbstractLoadingCacheTest method testGetUnchecked_error.
public void testGetUnchecked_error() {
final Error cause = new Error();
final AtomicReference<Object> valueRef = new AtomicReference<Object>();
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) {
assertEquals(cause, expected.getCause());
}
Object newValue = new Object();
valueRef.set(newValue);
assertSame(newValue, cache.getUnchecked(new Object()));
}
Aggregations