use of com.google.common.util.concurrent.UncheckedExecutionException in project caffeine by ben-manes.
the class CacheLoadingTest method testBulkLoadUncheckedException.
public void testBulkLoadUncheckedException() throws ExecutionException {
Exception e = new RuntimeException();
CacheLoader<Object, Object> loader = exceptionLoader(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 (UncheckedExecutionException 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.UncheckedExecutionException in project caffeine by ben-manes.
the class CacheLoadingTest method testConcurrentLoadingUncheckedException.
/**
* On a concurrent computation that throws an unchecked exception, all threads should get the
* (wrapped) exception, with the loader called only once. The result should not be cached (a later
* request should call the loader again).
*/
private static void testConcurrentLoadingUncheckedException(Caffeine<Object, Object> builder) throws InterruptedException {
int count = 10;
final AtomicInteger callCount = new AtomicInteger();
final CountDownLatch startSignal = new CountDownLatch(count + 1);
final RuntimeException e = new RuntimeException();
LoadingCache<String, String> cache = CaffeinatedGuava.build(builder, new CacheLoader<String, String>() {
@Override
public String load(String key) {
callCount.incrementAndGet();
Uninterruptibles.awaitUninterruptibly(startSignal);
throw e;
}
});
List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
assertEquals(count, callCount.get());
for (int i = 0; i < count; i++) {
// doConcurrentGet alternates between calling getUnchecked and calling get, but an unchecked
// exception thrown by the loader is always wrapped as an UncheckedExecutionException.
assertTrue(result.get(i) instanceof UncheckedExecutionException);
assertSame(e, ((UncheckedExecutionException) result.get(i)).getCause());
}
// subsequent calls should call the loader again, not get the old exception
try {
cache.getUnchecked("bar");
fail();
} catch (UncheckedExecutionException expected) {
}
assertEquals(count + 1, callCount.get());
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project caffeine by ben-manes.
the class CacheLoadingTest method testLoadInterruptedException.
public void testLoadInterruptedException() {
Exception e = new InterruptedException();
CacheLoader<Object, Object> loader = exceptionLoader(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());
// Sanity check:
assertFalse(Thread.interrupted());
try {
cache.get(new Object());
fail();
} catch (ExecutionException expected) {
assertSame(e, expected.getCause());
}
assertTrue(Thread.interrupted());
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 (UncheckedExecutionException expected) {
assertSame(e, expected.getCause());
}
assertTrue(Thread.interrupted());
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
cache.refresh(new Object());
assertTrue(Thread.interrupted());
checkLoggedCause(e);
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(3, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
Exception callableException = new InterruptedException();
try {
cache.get(new Object(), throwing(callableException));
fail();
} catch (ExecutionException expected) {
assertSame(callableException, expected.getCause());
}
assertTrue(Thread.interrupted());
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 (ExecutionException expected) {
assertSame(e, expected.getCause());
}
assertTrue(Thread.interrupted());
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.UncheckedExecutionException in project caffeine by ben-manes.
the class CacheLoadingTest method testBulkLoadingExceptionWithCause.
public void testBulkLoadingExceptionWithCause() {
final Exception cause = new Exception();
final UncheckedExecutionException uee = new UncheckedExecutionException(cause);
final ExecutionException ee = new ExecutionException(cause);
LoadingCache<Object, Object> cacheUnchecked = CaffeinatedGuava.build(Caffeine.newBuilder(), bulkLoader(exceptionLoader(uee)));
LoadingCache<Object, Object> cacheChecked = CaffeinatedGuava.build(Caffeine.newBuilder(), bulkLoader(exceptionLoader(ee)));
try {
cacheUnchecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException e) {
fail();
} catch (UncheckedExecutionException caughtEe) {
assertSame(uee, caughtEe.getCause());
}
try {
cacheChecked.getAll(asList(new Object()));
fail();
} catch (ExecutionException caughtEe) {
assertSame(ee, caughtEe.getCause());
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project crate by crate.
the class SQLExceptionsTest method testUnwrap.
@Test
public void testUnwrap() {
String msg = "cannot cast";
Throwable t = new UncheckedExecutionException(new ExecutionException(new ClassCastException(msg)));
Throwable unwrapped = SQLExceptions.unwrap(t);
assertThat(unwrapped, instanceOf(ClassCastException.class));
assertThat(unwrapped.getMessage(), is(msg));
}
Aggregations