use of com.google.common.util.concurrent.UncheckedExecutionException in project azure-sdk-for-java by Azure.
the class CachingKeyResolverTest method KeyVault_CachingKeyResolverThrows.
/*
* Tests the behavior of CachingKeyResolver when resolving key throws
* and validate that the failed entity is not added to the cache.
*/
@Test
public void KeyVault_CachingKeyResolverThrows() {
IKeyResolver mockedKeyResolver = mock(IKeyResolver.class);
CachingKeyResolver resolver = new CachingKeyResolver(10, mockedKeyResolver);
// First throw exception and for the second call return a value
when(mockedKeyResolver.resolveKeyAsync(keyId)).thenThrow(new RuntimeException("test")).thenReturn(ikeyAsync);
try {
resolver.resolveKeyAsync(keyId);
assertFalse("Should have thrown an exception.", true);
} catch (UncheckedExecutionException e) {
assertTrue("RuntimeException is expected.", e.getCause() instanceof RuntimeException);
}
resolver.resolveKeyAsync(keyId);
resolver.resolveKeyAsync(keyId);
verify(mockedKeyResolver, times(2)).resolveKeyAsync(keyId);
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project jackrabbit-oak by apache.
the class MongoDocumentStore method find.
@SuppressWarnings("unchecked")
private <T extends Document> T find(final Collection<T> collection, final String key, boolean preferCached, final int maxCacheAge) {
if (collection != Collection.NODES) {
return findUncachedWithRetry(collection, key, DocumentReadPreference.PRIMARY, 2);
}
NodeDocument doc;
if (maxCacheAge > 0 || preferCached) {
// first try without lock
doc = nodesCache.getIfPresent(key);
if (doc != null) {
if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
stats.doneFindCached(collection, key);
if (doc == NodeDocument.NULL) {
return null;
}
return (T) doc;
}
}
}
Throwable t;
try {
Lock lock = nodeLocks.acquire(key);
try {
if (maxCacheAge > 0 || preferCached) {
// try again some other thread may have populated
// the cache by now
doc = nodesCache.getIfPresent(key);
if (doc != null) {
if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
stats.doneFindCached(collection, key);
if (doc == NodeDocument.NULL) {
return null;
}
return (T) doc;
}
}
}
final NodeDocument d = (NodeDocument) findUncachedWithRetry(collection, key, getReadPreference(maxCacheAge), 2);
invalidateCache(collection, key);
doc = nodesCache.get(key, new Callable<NodeDocument>() {
@Override
public NodeDocument call() throws Exception {
return d == null ? NodeDocument.NULL : d;
}
});
} finally {
lock.unlock();
}
if (doc == NodeDocument.NULL) {
return null;
} else {
return (T) doc;
}
} catch (UncheckedExecutionException e) {
t = e.getCause();
} catch (ExecutionException e) {
t = e.getCause();
} catch (RuntimeException e) {
t = e;
}
throw new DocumentStoreException("Failed to load document with " + key, t);
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project gerrit by GerritCodeReview.
the class MergeabilityCacheImpl method get.
@Override
public boolean get(ObjectId commit, Ref intoRef, SubmitType submitType, String mergeStrategy, Branch.NameKey dest, Repository repo) {
ObjectId into = intoRef != null ? intoRef.getObjectId() : ObjectId.zeroId();
EntryKey key = new EntryKey(commit, into, submitType, mergeStrategy);
try {
return cache.get(key, () -> {
if (key.into.equals(ObjectId.zeroId())) {
// Assume yes on new branch.
return true;
}
try (CodeReviewRevWalk rw = CodeReviewCommit.newRevWalk(repo)) {
Set<RevCommit> accepted = SubmitDryRun.getAlreadyAccepted(repo, rw);
accepted.add(rw.parseCommit(key.into));
accepted.addAll(Arrays.asList(rw.parseCommit(key.commit).getParents()));
return submitDryRun.run(key.submitType, repo, rw, dest, key.into, key.commit, accepted);
}
});
} catch (ExecutionException | UncheckedExecutionException e) {
log.error(String.format("Error checking mergeability of %s into %s (%s)", key.commit.name(), key.into.name(), key.submitType.name()), e.getCause());
return false;
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project google-cloud-java by GoogleCloudPlatform.
the class BaseEmulatorHelper method waitForProcess.
private static int waitForProcess(final Process process, Duration timeout) throws InterruptedException, TimeoutException {
if (process == null) {
return 0;
}
final SettableFuture<Integer> exitValue = SettableFuture.create();
Thread waiter = new Thread(new Runnable() {
@Override
public void run() {
try {
exitValue.set(process.waitFor());
} catch (InterruptedException e) {
exitValue.setException(e);
}
}
});
waiter.start();
try {
return exitValue.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
if (e.getCause() instanceof InterruptedException) {
throw (InterruptedException) e.getCause();
}
throw new UncheckedExecutionException(e);
} finally {
waiter.interrupt();
}
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project caffeine by ben-manes.
the class CacheLoadingTest method testLoadUncheckedException.
public void testLoadUncheckedException() throws ExecutionException {
Exception e = new RuntimeException();
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());
try {
cache.get(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());
try {
cache.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException 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());
Exception callableException = new RuntimeException();
try {
cache.get(new Object(), throwing(callableException));
fail();
} catch (UncheckedExecutionException expected) {
assertSame(callableException, 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 (UncheckedExecutionException 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