FutureUtils { public static <T> Promise<T> within(final Promise<T> promise, final long timeout, final TimeUnit unit, final Throwable cause, final OrderedScheduler scheduler, final Object key) { if (timeout < DistributedLogConstants.FUTURE_TIMEOUT_IMMEDIATE || promise.isDefined()) { return promise; } scheduler.schedule(key, new Runnable() { @Override public void run() { logger.info("Raise exception", cause); FutureUtils.setException(promise, cause); } }, timeout, unit); return promise; }  static Future<List<R>> processList(List<T> collection,
                                                     Function<T, Future<R>> processFunc,
                                                     @Nullable ExecutorService callbackExecutor); static T bkResult(Future<T> result); static int bkResultCode(Throwable throwable); static T result(Future<T> result); static T result(Future<T> result, Duration duration); static T lockResult(Future<T> result, String lockPath); static Throwable zkException(Throwable throwable, String path); static void cancel(Future<T> future); static Promise<T> within(final Promise<T> promise,
                                        final long timeout,
                                        final TimeUnit unit,
                                        final Throwable cause,
                                        final OrderedScheduler scheduler,
                                        final Object key); static void setValue(final Promise<T> promise,
                                    final T value,
                                    OrderedScheduler scheduler,
                                    Object key); static boolean setValue(Promise<T> promise, T value); static void setException(final Promise<T> promise,
                                        final Throwable throwable,
                                        OrderedScheduler scheduler,
                                        Object key); static boolean setException(Promise<T> promise, Throwable cause); static Promise<Void> ignore(Future<T> future); static Promise<Void> ignore(Future<T> future, final String errorMsg); static BKTransmitException transmitException(int transmitResult);  }
@Test(timeout = 60000) public void testWithin() throws Exception { OrderedScheduler scheduler = OrderedScheduler.newBuilder() .corePoolSize(1) .name("test-within") .build(); final Promise<Void> promiseToTimeout = new Promise<Void>(); final Promise<Void> finalPromise = new Promise<Void>(); FutureUtils.within( promiseToTimeout, 10, TimeUnit.MILLISECONDS, new TestException(), scheduler, "test-within" ).addEventListener(new FutureEventListener<Void>() { @Override public void onFailure(Throwable cause) { FutureUtils.setException(finalPromise, cause); } @Override public void onSuccess(Void value) { FutureUtils.setValue(finalPromise, value); } }); try { FutureUtils.result(finalPromise); fail("Should fail with TestException"); } catch (TestException te) { } }