@Override public void handleFailure(Throwable t) { if (throwableType == null || throwableType.isAssignableFrom(t.getClass())) { try { slf.setRunningThread(Thread.currentThread()); slf.setResult(mapper.apply((TT)t)); } catch (Throwable newT) { slf.setFailure(newT); } } else { slf.setFailure(t); } } }, executor, optimizeExecution);
@Override public void handleFailure(Throwable t) { if (throwableType == null || throwableType.isAssignableFrom(t.getClass())) { try { slf.setRunningThread(Thread.currentThread()); slf.setResult(mapper.apply((TT)t)); } catch (Throwable newT) { slf.setFailure(newT); } } else { slf.setFailure(t); } } }, executor, optimizeExecution);
@Override public void handleResult(ST result) { try { slf.setRunningThread(Thread.currentThread()); slf.setResult(mapper.apply(result)); } catch (Throwable t) { if (reportedTransformedExceptions) { // failure calculating transformation, let handler get a chance to see the uncaught exception // This makes the behavior closer to if the exception was thrown from a task submitted to the pool ExceptionUtils.handleException(t); } slf.setFailure(t); } } }, executor, optimizeExecution);
@Override public void handleResult(ST result) { try { slf.setRunningThread(Thread.currentThread()); slf.setResult(mapper.apply(result)); } catch (Throwable t) { if (reportedTransformedExceptions) { // failure calculating transformation, let handler get a chance to see the uncaught exception // This makes the behavior closer to if the exception was thrown from a task submitted to the pool ExceptionUtils.handleException(t); } slf.setFailure(t); } } }, executor, optimizeExecution);
@Test public void getRunningStackTraceTest() { assertNull(slf.getRunningStackTrace()); slf.setRunningThread(Thread.currentThread()); StackTraceElement[] stack = slf.getRunningStackTrace(); assertEquals(this.getClass().getName(), stack[2].getClassName()); slf.setResult(null); assertNull(slf.getRunningStackTrace()); }
@Test public void getMappedRunningStackTraceTest() { ListenableFuture<Object> mappedFuture = slf.map((o) -> o).map((o) -> null); assertNull(mappedFuture.getRunningStackTrace()); slf.setRunningThread(Thread.currentThread()); StackTraceElement[] stack = mappedFuture.getRunningStackTrace(); assertEquals(this.getClass().getName(), stack[4].getClassName()); slf.setResult(null); assertNull(mappedFuture.getRunningStackTrace()); }
@Test public void cancelWithInterruptTest() { BlockingTestRunnable btr = new BlockingTestRunnable(); Thread runningThread = new Thread(btr); try { runningThread.start(); slf.setRunningThread(runningThread); btr.blockTillStarted(); assertTrue(slf.cancel(true)); // should unblock when interrupted btr.blockTillFinished(); } finally { btr.unblock(); } }
@Test public void executeWhileGetRunningStackTraceTest() { SettableListenableFuture<Boolean> startingFuture = new SettableListenableFuture<>(); SettableListenableFuture<Boolean> runningFuture = new SettableListenableFuture<>(); ListenableFuture<Boolean> future = FutureUtils.executeWhile(startingFuture, () -> runningFuture, (loop) -> loop); try { startingFuture.setResult(true); // loop to running future runningFuture.setRunningThread(Thread.currentThread()); StackTraceElement[] stack = future.getRunningStackTrace(); assertNotNull(stack); assertEquals(this.getClass().getName(), stack[3].getClassName()); } finally { runningFuture.setResult(false); } } }
@Test public void getFlatMappedRunningStackTraceTest() { ListenableFuture<Object> mappedFuture = slf.flatMap((o) -> FutureUtils.immediateResultFuture(o)) .flatMap((o) -> FutureUtils.immediateResultFuture(null)); assertNull(mappedFuture.getRunningStackTrace()); slf.setRunningThread(Thread.currentThread()); StackTraceElement[] stack = mappedFuture.getRunningStackTrace(); assertEquals(this.getClass().getName(), stack[4].getClassName()); slf.setResult(null); assertNull(mappedFuture.getRunningStackTrace()); }
@Test public void getRunningStackTraceTest() { SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); ScheduledFutureDelegate<?> testItem = new ScheduledFutureDelegate<>(slf, null); assertNull(testItem.getRunningStackTrace()); slf.setRunningThread(Thread.currentThread()); assertNotNull(testItem.getRunningStackTrace()); } }
@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()); } } }