@Test public void canConfigureStatusMappers() { CancellableTask.ResultToStatus<TimeoutableResult, Object> resultToStatus = new CancellableTask.ResultToStatus<TimeoutableResult, Object>() { @Override public TimeoutableResult resultToStatus(Object o) { return TimeoutableResult.ERROR; } }; CancellableTask.ThrowableToStatus<TimeoutableResult> throwableToStatus = new CancellableTask.ThrowableToStatus<TimeoutableResult>() { @Override public TimeoutableResult throwableToStatus(Throwable t) { return TimeoutableResult.SUCCESS; } }; PrecipicePromise<TimeoutableResult, Object> successPromise = mock(PrecipicePromise.class); when(successPromise.future()).thenReturn(mock(PrecipiceFuture.class)); Object result = new Object(); CancellableTask<TimeoutableResult, Object> cancellableTask = new CancellableTask<>(resultToStatus, throwableToStatus, TestCallable.success(result), successPromise); cancellableTask.run(); verify(successPromise).complete(TimeoutableResult.ERROR, result); PrecipicePromise<TimeoutableResult, Object> errorPromise = mock(PrecipicePromise.class); when(errorPromise.future()).thenReturn(mock(PrecipiceFuture.class)); final IOException exception = new IOException(); CancellableTask<TimeoutableResult, Object> cancellableTask2 = new CancellableTask<>(resultToStatus, throwableToStatus, new Callable<Object>() { @Override public Object call() throws Exception { throw exception; } }, errorPromise); cancellableTask2.run(); verify(errorPromise).completeExceptionally(TimeoutableResult.SUCCESS, exception); }
@Override public void run() { try { int state = this.state.get(); if (state == PENDING && !promise.future().isDone()) { runner = Thread.currentThread(); T result = callable.call(); safeSetSuccess(result); } else if (state == INTERRUPTING) { waitForInterruption(); } } catch (InterruptedException e) { } catch (Throwable e) { safeSetErred(e); } finally { Thread.interrupted(); } }
CancellableTask implements Runnable, Cancellable { @Override public void run() { try { int state = this.state.get(); if (state == PENDING && !promise.future().isDone()) { runner = Thread.currentThread(); T result = callable.call(); safeSetSuccess(result); } else if (state == INTERRUPTING) { waitForInterruption(); } } catch (InterruptedException e) { } catch (Throwable e) { safeSetErred(e); } finally { Thread.interrupted(); } } }
CancellableTask implements Runnable, Cancellable { @Override public void run() { try { int state = this.state.get(); if (state == PENDING && !promise.future().isDone()) { runner = Thread.currentThread(); T result = callable.call(); safeSetSuccess(result); } else if (state == INTERRUPTING) { waitForInterruption(); } } catch (InterruptedException e) { } catch (Throwable e) { safeSetErred(e); } finally { Thread.interrupted(); } } CancellableTask(ResultToStatus<Status, T> resultToStatus, ThrowableToStatus<Status> throwableToStatus,
                           Callable<T> callable, PrecipicePromise<Status, T> promise); }
CancellableTask implements Runnable, Cancellable { @Override public void run() { try { int state = this.state.get(); if (state == PENDING && !promise.future().isDone()) { runner = Thread.currentThread(); T result = callable.call(); safeSetSuccess(result); } else if (state == INTERRUPTING) { waitForInterruption(); } } catch (InterruptedException e) { } catch (Throwable e) { safeSetErred(e); } finally { Thread.interrupted(); } } CancellableTask(ResultToStatus<Status, T> resultToStatus, ThrowableToStatus<Status> throwableToStatus,
                           Callable<T> callable, PrecipicePromise<Status, T> promise); @Override void run(); @Override void cancel(); void cancel(Status cancelledStatus, Exception exception); }
CancellableTask implements Runnable, Cancellable { @Override public void run() { try { int state = this.state.get(); if (state == PENDING && !promise.future().isDone()) { runner = Thread.currentThread(); T result = callable.call(); safeSetSuccess(result); } else if (state == INTERRUPTING) { waitForInterruption(); } } catch (InterruptedException e) { } catch (Throwable e) { safeSetErred(e); } finally { Thread.interrupted(); } } CancellableTask(ResultToStatus<Status, T> resultToStatus, ThrowableToStatus<Status> throwableToStatus,
                           Callable<T> callable, PrecipicePromise<Status, T> promise); @Override void run(); @Override void cancel(); void cancel(Status cancelledStatus, Exception exception);  }