public static boolean isEarlyFinish(final Throwable e) { return isCancellation(e) && e.getCause() instanceof EarlyFinishException; }
public static String failureToString(final Throwable e) { if (isCancellation(e)) { if (isEarlyFinish(e)) { return ""; } else { return "cancelled because: " + toString(e.getCause()); } } else { return toString(e); } }
if (p.isFailed() && Exceptions.isCancellation(p.getError())) {
public void testOnFailureWhenCancelled(int expectedNumberOfTasks) { final AtomicReference<Throwable> variable = new AtomicReference<Throwable>(); Task<String> cancelled = getCancelledTask().onFailure(variable::set); try { runAndWait("AbstractTaskTest.testOnFailureWhenCancelled", cancelled); fail("should have failed"); } catch (Exception ex) { assertTrue(cancelled.isFailed()); assertTrue(Exceptions.isCancellation(cancelled.getError())); } assertNull(variable.get()); assertEquals(countTasks(cancelled.getTrace()), expectedNumberOfTasks); }
@Test public void testTaskCancellationAfterCompleted() throws InterruptedException { final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>(); Task<?> completed = Task.value(10); completed.addListener(p -> { if (p.isFailed() && Exceptions.isCancellation(p.getError())) { cancelActionValue.set(p.getError().getCause()); } } ); runAndWait("TestTaskCancellation.testTaskCancellationAfterCompleted", completed); Exception cancelReason = new Exception(); assertFalse(completed.cancel(cancelReason)); assertNull(cancelActionValue.get()); }
@Test public void testTaskCancellationTimeout() throws InterruptedException { final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>(); final CountDownLatch runLatch = new CountDownLatch(1); final CountDownLatch listenerLatch = new CountDownLatch(1); Task<Integer> uncompleted = Task.async(() -> { runLatch.countDown(); return Promises.settable(); }); uncompleted.addListener(p -> { if (p.isFailed() && Exceptions.isCancellation(p.getError())) { cancelActionValue.set(p.getError().getCause()); } listenerLatch.countDown(); } ); Task<?> task = uncompleted.withTimeout(10, TimeUnit.MILLISECONDS).recover(e -> 0); runAndWait("TestTaskCancellation.testTaskCancellationTimeout", task); assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(cancelActionValue.get() instanceof EarlyFinishException); }
@Test public void testToTryCancelled() throws InterruptedException { Task<String> cancelMain = delayedValue("value", 6000, TimeUnit.MILLISECONDS); Task<Try<String>> task = cancelMain.toTry(); run(task); assertTrue(cancelMain.cancel(new Exception("canceled"))); task.await(); assertTrue(task.isDone()); assertTrue(task.isFailed()); assertTrue(Exceptions.isCancellation(task.getError())); logTracingResults("AbstractTaskTest.testToTryCancelled", task); }
public void testRecoverWithCancelled(int expectedNumberOfTasks) { Task<String> cancelled = getCancelledTask().recoverWith(e -> Task.callable("recover success", () -> "recovered")); try { runAndWait("AbstractTaskTest.testRecoverWithCancelled", cancelled); fail("should have failed"); } catch (Exception ex) { assertTrue(cancelled.isFailed()); assertTrue(Exceptions.isCancellation(cancelled.getError())); } assertEquals(countTasks(cancelled.getTrace()), expectedNumberOfTasks); }
public void testCancelledRecover(int expectedNumberOfTasks) { Task<Integer> cancelled = getCancelledTask().map("strlen", String::length).recover(e -> -1); try { runAndWait("AbstractTaskTest.testCancelledRecover", cancelled); fail("should have failed"); } catch (Exception ex) { assertTrue(cancelled.isFailed()); assertTrue(Exceptions.isCancellation(cancelled.getError())); } assertEquals(countTasks(cancelled.getTrace()), expectedNumberOfTasks); }
@Test public void testTaskCancellationBeforeRun() throws InterruptedException { final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>(); Task<?> uncompleted = Task.async(() -> Promises.settable()); uncompleted.addListener(p -> { if (p.isFailed() && Exceptions.isCancellation(p.getError())) { cancelActionValue.set(p.getError().getCause()); } } ); Exception cancelReason = new Exception(); assertTrue(uncompleted.cancel(cancelReason)); getEngine().run(uncompleted); uncompleted.await(5, TimeUnit.SECONDS); logTracingResults("TestTaskCancellation.testTaskCancellationBeforeRun", uncompleted); assertEquals(cancelActionValue.get(), cancelReason); }
}); uncompleted.addListener(p -> { if (p.isFailed() && Exceptions.isCancellation(p.getError())) { cancelActionValue.set(p.getError().getCause());
@Test public void testTaskCancellationPar() throws InterruptedException { final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>(); Task<Integer> completed = Task.value(10); final CountDownLatch runLatch = new CountDownLatch(1); final CountDownLatch listenerLatch = new CountDownLatch(1); Task<Integer> uncompleted = Task.async(() -> { runLatch.countDown(); return Promises.settable(); }); uncompleted.addListener(p -> { if (p.isFailed() && Exceptions.isCancellation(p.getError())) { cancelActionValue.set(p.getError().getCause()); } listenerLatch.countDown(); } ); Task<?> task = Task.par(completed, uncompleted).map((x, y) -> x + y).withTimeout(100, TimeUnit.MILLISECONDS).recover(e -> 0); runAndWait("TestTaskCancellation.testTaskCancellationPar", task); assertTrue(listenerLatch.await(5, TimeUnit.SECONDS)); assertTrue(cancelActionValue.get() instanceof EarlyFinishException); }
return apply(desc, (src, dst) -> { if (src.isFailed()) { if (!(Exceptions.isCancellation(src.getError()))) { try { consumer.accept(src.getError());
return apply(desc, (src, dst) -> { final Try<T> tryT = Promises.toTry(src); if (tryT.isFailed() && Exceptions.isCancellation(tryT.getError())) { dst.fail(src.getError()); } else {
context.after(that).run(() -> { if (that.isFailed()) { if (!(Exceptions.isCancellation(that.getError()))) { try { Task<T> r = func.apply(that.getError());
return apply(desc, (src, dst) -> { if (src.isFailed()) { if (!(Exceptions.isCancellation(src.getError()))) { try { dst.done(func.apply(src.getError()));
private void assertCancelled(final Task<?> task, Exception exception) { assertTrue(task.isDone()); assertTrue(task.isFailed()); assertTrue(Exceptions.isCancellation(task.getError())); assertEquals(exception, task.getError().getCause()); assertTrue(task.getShallowTrace().getStartNanos() > 0); assertTrue(task.getShallowTrace().getStartNanos() <= task.getShallowTrace().getEndNanos()); try { task.get(); fail("Should have thwon PromiseException"); } catch (PromiseException e) { assertEquals(exception, e.getCause().getCause()); } }
return apply(desc, (src, dst) -> { final Try<T> tryT = Promises.toTry(src); if (tryT.isFailed() && Exceptions.isCancellation(tryT.getError())) { dst.fail(src.getError()); } else {
final SettablePromise<T> recoveryResult = Promises.settable(); if (that.isFailed()) { if (!(Exceptions.isCancellation(that.getError()))) { try { Task<T> r = func.apply(that.getError());
public static String failureToString(final Throwable e) { if (isCancellation(e)) { if (isEarlyFinish(e)) { return ""; } else { return "cancelled because: " + toString(e.getCause()); } } else { return toString(e); } }