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); } }
private <T> Task<Response<T>> withD2Timeout(final Task<Response<T>> task, ConfigValue<Long> timeout) { String srcDesc = timeout.getSource().map(src -> " src: " + src).orElse(""); String timeoutTaskName = "withTimeout " + timeout.getValue().intValue() + TimeUnitHelper.toString(TimeUnit.MILLISECONDS) + srcDesc; // make sure that we throw the same exception to maintain backward compatibility with current withTimeout implementation. return task.transform(timeoutTaskName, (Try<Response<T>> tryGet) -> { if (tryGet.isFailed() && tryGet.getError() instanceof TimeoutException) { String timeoutExceptionMessage = "task: '" + task.getName() + "' " + timeoutTaskName; return Failure.of(Exceptions.timeoutException(timeoutExceptionMessage)); } else { return tryGet; } }); }
protected void traceFailure(final Throwable reason) { if (Exceptions.isEarlyFinish(reason)) { _shallowTraceBuilder.setResultType(ResultType.EARLY_FINISH); } else { _shallowTraceBuilder.setResultType(ResultType.ERROR); _shallowTraceBuilder.setValue(Exceptions.failureToString(reason)); } }
/** * Given a task this method will return a {@link ResultType} classification. * * @param task the task to classify * @return the result type classification for the task */ public static ResultType fromTask(final Task<?> task) { if (!task.isDone()) { return UNFINISHED; } else if (task.isFailed()) { if (Exceptions.isEarlyFinish(task.getError())) { return EARLY_FINISH; } return ERROR; } return SUCCESS; } }
private <T> String stringValue(Task<T> task, Function<T, String> traceValueProvider) { if (task.isFailed()) { return Exceptions.failureToString(task.getError()); } else { if (traceValueProvider != null) { try { return traceValueProvider.apply(task.get()); } catch (Exception e) { return e.toString(); } } } return "null"; }
public static Exception noSuchElement(final Throwable cause) { return addCause(new NoSuchElementException(), cause); }
protected void traceFailure(final Throwable reason) { if (Exceptions.isEarlyFinish(reason)) { _shallowTraceBuilder.setResultType(ResultType.EARLY_FINISH); } else { _shallowTraceBuilder.setResultType(ResultType.ERROR); _shallowTraceBuilder.setValue(Exceptions.failureToString(reason)); } }
assertTrue("Should be early finish", Exceptions.isEarlyFinish(par.getError())); assertEquals(1, successful.size()); assertEquals("done1", successful.get(0));
private void traceDone(final T value) { _shallowTraceBuilder.setResultType(ResultType.SUCCESS); final Function<T, String> traceValueProvider = _traceValueProvider; if (traceValueProvider != null) { try { _shallowTraceBuilder.setValue(traceValueProvider.apply(value)); } catch (Exception e) { _shallowTraceBuilder.setValue(Exceptions.failureToString(e)); } } }
public static Exception noSuchElement(final Throwable cause) { return addCause(new NoSuchElementException(), cause); }
if (p.isFailed() && Exceptions.isCancellation(p.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); } }
@Test public void testTimeoutTaskWithTimeout() throws InterruptedException { // This task will not complete on its own, which allows us to test the timeout final Task<String> task = new BaseTask<String>("task") { @Override protected Promise<? extends String> run(final Context context) throws Exception { return Promises.settable(); } }; final Task<String> timeoutTask = task.withTimeout(200, TimeUnit.MILLISECONDS); try { runAndWait("TestTasks.testTimeoutTaskWithTimeout", timeoutTask); fail("task should finish with Error"); } catch (Throwable t) { assertTrue(timeoutTask.getError() instanceof TimeoutException); } assertTrue(timeoutTask.isFailed()); assertTrue(task.await(5, TimeUnit.SECONDS)); // The original task should also be failed - this time with an early finish // exception. assertTrue(task.isFailed()); assertTrue(Exceptions.isEarlyFinish(task.getError())); }
final Task<?> timeoutTask = Task.action("timeout", () -> { if (committed.compareAndSet(false, true)) { result.fail(Exceptions.timeoutException(timeoutExceptionMessage));
private <T> String stringValue(Task<T> task, Function<T, String> traceValueProvider) { if (task.isFailed()) { return Exceptions.failureToString(task.getError()); } else { if (traceValueProvider != null) { try { return traceValueProvider.apply(task.get()); } catch (Exception e) { return e.toString(); } } } return "null"; }
@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()); }
if (task.isFailed() && !(Exceptions.isEarlyFinish(task.getError()))) { assertNotNull(trace.getValue()); } else {
final Task<?> timeoutTask = Task.action("timeout", () -> { if (committed.compareAndSet(false, true)) { result.fail(Exceptions.timeoutException(timeoutExceptionMessage));
private void traceDone(final T value) { _shallowTraceBuilder.setResultType(ResultType.SUCCESS); final Function<T, String> traceValueProvider = _traceValueProvider; if (traceValueProvider != null) { try { _shallowTraceBuilder.setValue(traceValueProvider.apply(value)); } catch (Exception e) { _shallowTraceBuilder.setValue(Exceptions.failureToString(e)); } } }