private SessionFactory getSessionFactory() { try { return this.sessionFactoryFuture.get(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new IllegalStateException("Interrupted during initialization of Hibernate SessionFactory", ex); } catch (ExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof HibernateException) { // Rethrow a provider configuration exception (possibly with a nested cause) directly throw (HibernateException) cause; } throw new IllegalStateException("Failed to asynchronously initialize Hibernate SessionFactory: " + ex.getMessage(), cause); } } }
CommandStatus tryWaitForFinalStatus(final Duration timeout) throws InterruptedException { try { return finalStatusFuture.get(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (final ExecutionException e) { if (e.getCause() instanceof RuntimeException) { throw (RuntimeException)e.getCause(); } throw new RuntimeException("Error executing command " + commandId, e.getCause()); } catch (final TimeoutException e) { return currentStatus; } }
@Override public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException | TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
@Test(/* timeout = 5000, */expected = IndexOutOfBoundsException.class) public void testExceptionWithMoreThanOneElement() throws Throwable { Observable<String> obs = Observable.just("one", "two"); Future<String> f = obs.toFuture(); try { // we expect an exception since there are more than 1 element f.get(); fail("Should have thrown!"); } catch (ExecutionException e) { throw e.getCause(); } }
/** * Contacts each regionserver and fetches metadata about regions. * @param regionServerList - the list of region servers to connect to * @throws IOException if a remote or network exception occurs */ void processRegionServers(Collection<ServerName> regionServerList) throws IOException, InterruptedException { List<WorkItemRegion> workItems = new ArrayList<>(regionServerList.size()); List<Future<Void>> workFutures; // loop to contact each region server in parallel for (ServerName rsinfo: regionServerList) { workItems.add(new WorkItemRegion(this, rsinfo, errors, connection)); } workFutures = executor.invokeAll(workItems); for(int i=0; i<workFutures.size(); i++) { WorkItemRegion item = workItems.get(i); Future<Void> f = workFutures.get(i); try { f.get(); } catch(ExecutionException e) { LOG.warn("Could not process regionserver " + item.rsinfo.getHostAndPort(), e.getCause()); } } }
@Test public void invokeAllPropagatesException() throws Exception { int numTasks = 5; final AtomicInteger id = new AtomicInteger(); List<Callable<Void>> tasks = new ArrayList<>(); final Exception testException = new Exception("test message"); for (int i = 0; i < numTasks; i++) { tasks.add(new Callable<Void>() { @Override public Void call() throws Exception { int myId = id.incrementAndGet(); // The 3rd task throws an exception if (myId == 3) { throw testException; } return null; } }); } try { CommonUtils.invokeAll(tasks, 2 * Constants.SECOND_MS); fail("Expected an exception to be thrown"); } catch (ExecutionException e) { assertSame(testException, e.getCause()); } }
@Test public void throwsSetErrorWrappedInExecutionException() throws Exception { Throwable exception = new OutOfMemoryError(); assertTrue(settableListenableFuture.setException(exception)); try { settableListenableFuture.get(); fail("Expected ExecutionException"); } catch (ExecutionException ex) { assertThat(ex.getCause(), equalTo(exception)); } assertFalse(settableListenableFuture.isCancelled()); assertTrue(settableListenableFuture.isDone()); }
@Test public void onErrorOnComplete() throws Exception { fs.onError(new TestException("One")); fs.onComplete(); try { fs.get(5, TimeUnit.MILLISECONDS); } catch (ExecutionException ex) { assertTrue(ex.toString(), ex.getCause() instanceof TestException); assertEquals("One", ex.getCause().getMessage()); } }
@Test public void testNoTable() throws InterruptedException { for (RegionLocateType locateType : RegionLocateType.values()) { try { getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, locateType, false).get(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(TableNotFoundException.class)); } } }
@Override public void runUninterruptiblyWithTimeout( Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { Uninterruptibles.getUninterruptibly(future, timeoutDuration, timeoutUnit); } catch (TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
public static void assertFutureError(Future<?> future, Class<? extends Throwable> exceptionClass) throws InterruptedException { try { future.get(); fail("Expected a " + exceptionClass.getSimpleName() + " exception, but got success."); } catch (ExecutionException ee) { Throwable cause = ee.getCause(); assertEquals("Expected a " + exceptionClass.getSimpleName() + " exception, but got " + cause.getClass().getSimpleName(), exceptionClass, cause.getClass()); } }
@Test public void isnt_inheritable() throws Exception { ExecutorService service = Executors.newCachedThreadPool(); try (Scope scope = currentTraceContext.newScope(context)) { assertThat(service.submit(() -> { verifyImplicitContext(null); return currentTraceContext.get(); }).get()).isNull(); } catch (ExecutionException e) { if (e.getCause() instanceof Error) throw (Error) e.getCause(); throw (Exception) e.getCause(); } assertThat(service.submit(currentTraceContext::get).get()) .isNull(); verifyImplicitContext(null); service.shutdownNow(); }
/** * Tests that when one task throws an exception and other tasks time out, the exception is * propagated. */ @Test public void invokeAllPropagatesExceptionWithTimeout() throws Exception { int numTasks = 5; final AtomicInteger id = new AtomicInteger(); List<Callable<Void>> tasks = new ArrayList<>(); final Exception testException = new Exception("test message"); for (int i = 0; i < numTasks; i++) { tasks.add(() -> { int myId = id.incrementAndGet(); // The 3rd task throws an exception, other tasks sleep. if (myId == 3) { throw testException; } else { Thread.sleep(10 * Constants.SECOND_MS); } return null; }); } try { CommonUtils.invokeAll(tasks, 500); fail("Expected an exception to be thrown"); } catch (ExecutionException e) { assertSame(testException, e.getCause()); } }
@Test public void throwsSetExceptionWrappedInExecutionException() throws Exception { Throwable exception = new RuntimeException(); assertTrue(settableListenableFuture.setException(exception)); try { settableListenableFuture.get(); fail("Expected ExecutionException"); } catch (ExecutionException ex) { assertThat(ex.getCause(), equalTo(exception)); } assertFalse(settableListenableFuture.isCancelled()); assertTrue(settableListenableFuture.isDone()); }
@CanIgnoreReturnValue @Override public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException { checkNotNull(callable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<T> future = executor.submit(callable); try { return future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException | TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
@Test public void onErrorOnComplete() throws Exception { List<Throwable> errors = TestHelper.trackPluginErrors(); try { fo.onError(new TestException("One")); fo.onComplete(); try { fo.get(5, TimeUnit.MILLISECONDS); } catch (ExecutionException ex) { assertTrue(ex.toString(), ex.getCause() instanceof TestException); assertEquals("One", ex.getCause().getMessage()); } TestHelper.assertUndeliverable(errors, 0, NoSuchElementException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void testDeleteUnknownJar() throws Exception { final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request = createRequest("doesnotexist.jar"); try { jarDeleteHandler.handleRequest(request, restfulGateway).get(); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getMessage(), containsString("File doesnotexist.jar does not exist in")); assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST)); } }