/** * Extract the exception (throwable) inside the ScheduledFutureTask * @param runnable - The runable that was executed * @return exception enclosed in the Runnable if any; null otherwise */ private Throwable extractThrowable(Runnable runnable) { // Check for exceptions wrapped by FutureTask. // We do this by calling get(), which will cause it to throw any saved exception. // Check for isDone to prevent blocking if ((runnable instanceof Future<?>) && ((Future<?>) runnable).isDone()) { try { ((Future<?>) runnable).get(); } catch (CancellationException e) { LOG.info("Task {} cancelled", runnable, e.getCause()); } catch (InterruptedException e) { LOG.info("Task {} was interrupted", runnable, e); } catch (ExecutionException e) { return e.getCause(); } } return null; }
public void testCancel_notDoneNoInterrupt() throws Exception { Future<?> future = newFutureInstance(); assertTrue(future.cancel(false)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertNull(tryInternalFastPathGetFailure(future)); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { assertNotNull(e.getCause()); } }
public void testCancel_notDoneInterrupt() throws Exception { Future<?> future = newFutureInstance(); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertNull(tryInternalFastPathGetFailure(future)); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { assertNotNull(e.getCause()); } }
@GwtIncompatible public void testImmediateCancelledFutureStack() throws Exception { ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture(); assertTrue(future.isCancelled()); try { CallerClass2.get(future); fail(); } catch (CancellationException expected) { // There should be two CancellationException chained together. The outer one should have the // stack trace of where the get() call was made, and the inner should have the stack trace of // where the immediateCancelledFuture() call was made. List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace()); assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); // See AbstractFutureCancellationCauseTest for how to set causes. assertThat(expected.getCause()).isNull(); } }
public void testCancel_notDoneNoInterrupt() throws Exception { Future<?> future = newFutureInstance(); assertTrue(future.cancel(false)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { assertNotNull(e.getCause()); } }
public void testCancel_notDoneInterrupt() throws Exception { Future<?> future = newFutureInstance(); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { assertNotNull(e.getCause()); } }
@Test public void withRunningStackTest() throws InterruptedException, ExecutionException { SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); slf.setRunningThread(Thread.currentThread()); CancelDebuggingListenableFuture<Object> debugFuture = new CancelDebuggingListenableFuture<>(slf); assertTrue(debugFuture.cancel(false)); try { debugFuture.get(); fail("Exception should have thrown"); } catch (CancellationException e) { // expected assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof FutureProcessingStack); assertEquals(this.getClass().getName(), e.getCause().getStackTrace()[3].getClassName()); } } }
@GwtIncompatible public void testImmediateCancelledFutureStack() throws Exception { ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture(); assertTrue(future.isCancelled()); try { CallerClass2.get(future); fail(); } catch (CancellationException expected) { // There should be two CancellationException chained together. The outer one should have the // stack trace of where the get() call was made, and the inner should have the stack trace of // where the immediateCancelledFuture() call was made. List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace()); assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); // See AbstractFutureCancellationCauseTest for how to set causes. assertThat(expected.getCause()).isNull(); } }
@Test public void notStartedTest() throws InterruptedException, ExecutionException { SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); CancelDebuggingListenableFuture<Object> debugFuture = new CancelDebuggingListenableFuture<>(slf); assertTrue(debugFuture.cancel(false)); try { debugFuture.get(); fail("Exception should have thrown"); } catch (CancellationException e) { // expected assertNull(e.getCause()); } }
public void testCancel_notDoneInterrupt() throws Exception { InterruptibleFuture future = new InterruptibleFuture(); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertTrue(future.wasInterrupted()); assertTrue(future.interruptTaskWasCalled); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { // See AbstractFutureCancellationCauseTest for how to set causes assertNull(e.getCause()); } }
public void testCancel_notDoneNoInterrupt() throws Exception { InterruptibleFuture future = new InterruptibleFuture(); assertTrue(future.cancel(false)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertFalse(future.wasInterrupted()); assertFalse(future.interruptTaskWasCalled); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { // See AbstractFutureCancellationCauseTest for how to set causes assertNull(e.getCause()); } }