@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return futureImp.get(timeout, unit); }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return futureImp.get(timeout, unit); }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return futureImp.get(timeout, unit); }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return futureImp.get(timeout, unit); }
@Override public T get() throws InterruptedException, ExecutionException { try { return delegateFuture.get(); } catch (CancellationException e) { prepareCancellationException(e); throw e; } }
@Override public T get() throws InterruptedException, ExecutionException { try { return delegateFuture.get(); } catch (CancellationException e) { prepareCancellationException(e); throw e; } }
@Override public T get(long arg0, TimeUnit arg1) throws InterruptedException, ExecutionException, TimeoutException { try { return delegateFuture.get(arg0, arg1); } catch (CancellationException e) { prepareCancellationException(e); throw e; } }
private static void verifyCancelationExceptionMessageOnGet(String msg, ListenableFuture<?> lf) throws InterruptedException { try { lf.get(); fail("Exception should have thrown"); } catch (ExecutionException e) { assertEquals(msg, e.getCause().getMessage()); } catch (CancellationException e) { assertEquals(msg, e.getMessage()); } }
@Test public void executeWhileTest() throws InterruptedException, ExecutionException { AtomicInteger ai = new AtomicInteger(); ListenableFuture<Integer> f = FutureUtils.executeWhile(() -> FutureUtils.immediateResultFuture(ai.getAndIncrement()), (i) -> i < 10); assertTrue(f.isDone()); assertEquals(10, f.get().intValue()); }
@Test public void makeCompleteFutureEmptyListTest() throws InterruptedException, ExecutionException { List<ListenableFuture<?>> futures = Collections.emptyList(); ListenableFuture<?> f = FutureUtils.makeCompleteFuture(futures); assertTrue(f.isDone()); assertNull(f.get()); }
@Test public void mapFailureIntoResultTest() throws InterruptedException, ExecutionException { ListenableFuture<?> lf = makeListenableFutureFactory().makeWithFailure(new Exception()); AtomicBoolean mapped = new AtomicBoolean(false); ListenableFuture<?> finalLF = lf.mapFailure(Exception.class, (t) -> { mapped.set(true); return null; }); assertTrue(finalLF.isDone()); assertTrue(mapped.get()); assertTrue(finalLF != lf); assertNull(finalLF.get()); }
@Test public void flatMapFailureIntoResultTest() throws InterruptedException, ExecutionException { ListenableFuture<?> lf = makeListenableFutureFactory().makeWithFailure(new Exception()); AtomicBoolean mapped = new AtomicBoolean(false); ListenableFuture<?> finalLF = lf.flatMapFailure(Exception.class, (t) -> { mapped.set(true); return FutureUtils.immediateResultFuture(null); }); assertTrue(finalLF.isDone()); assertTrue(mapped.get()); assertTrue(finalLF != lf); assertNull(finalLF.get()); }
@Test public void makeSuccessListFutureWithCancelErrorTest() throws ExecutionException, InterruptedException, TimeoutException { List<ListenableFuture<?>> futures = makeFutures(TEST_QTY, -1); SettableListenableFuture<?> cancelFuture = new SettableListenableFuture<>(); cancelFuture.cancel(false); futures.add(cancelFuture); ListenableFuture<List<ListenableFuture<?>>> f = FutureUtils.makeSuccessListFuture(futures); verifyCompleteFuture(f, futures); verifyAllIncluded(futures, f.get(), cancelFuture); }
@Test @Override public void submitRunnableWithResultTest() throws InterruptedException, ExecutionException { Object result = new Object(); TestRunnable tr = new TestRunnable(); ListenableFuture<?> future = executor.submit(tr, result); assertTrue(tr.ranOnce()); assertTrue(tr.executedThread == Thread.currentThread()); assertTrue(future.isDone()); assertTrue(future.get() == result); super.submitRunnableWithResultTest(); }
@Test public void interceptSubmitRunnableWithResultWithPriorityTest() throws InterruptedException, ExecutionException { Object result = new Object(); ListenableFuture<?> f = priorityInterceptor.submit(tr, result, TaskPriority.Low); assertEquals(1, testInterceptor.getInterceptedTasks().size()); assertTrue(tr == testInterceptor.getInterceptedTasks().get(0)); assertEquals(1, scheduler.tick()); // replaced task should run assertEquals(0, tr.getRunCount()); // should have been replaced and not run assertTrue(f.isDone()); assertTrue(f.get() == result); }
@Test public void interceptSubmitRunnableWithResultTest() throws InterruptedException, ExecutionException { Object result = new Object(); ListenableFuture<?> f = executorInterceptor.submit(tr, result); assertEquals(1, testInterceptor.getInterceptedTasks().size()); assertTrue(tr == testInterceptor.getInterceptedTasks().get(0)); assertEquals(1, scheduler.tick()); // replaced task should run assertEquals(0, tr.getRunCount()); // should have been replaced and not run assertTrue(f.isDone()); assertTrue(f.get() == result); }
@Test public void makeCompleteFutureWithResultNullTest() throws InterruptedException, ExecutionException { String result = StringUtils.makeRandomString(5); ListenableFuture<String> f = FutureUtils.makeCompleteFuture(null, result); assertTrue(f.isDone()); assertEquals(result, f.get()); }
@Test public void makeFailurePropagatingCompleteFutureWithResultNullTest() throws InterruptedException, ExecutionException { String result = StringUtils.makeRandomString(5); ListenableFuture<String> f = FutureUtils.makeFailurePropagatingCompleteFuture(null, result); assertTrue(f.isDone()); assertEquals(result, f.get()); }
@Test public void startWithTimeoutTest() throws InterruptedException, ExecutionException, TimeoutException { long start = Clock.accurateForwardProgressingMillis(); ListenableFuture<String> lf = profiler.start(DELAY_TIME); String result = lf.get(DELAY_TIME + (10 * 1000), TimeUnit.MILLISECONDS); long end = Clock.accurateForwardProgressingMillis(); // profiler should be stopped now assertFalse(profiler.isRunning()); assertTrue(end - start >= DELAY_TIME); assertNotNull(result); }
@Test public void runnableScheduleWhileFirstRunInThreadTest() throws Exception { int scheduleDelayMillis = 10; TestableScheduler scheduler = new TestableScheduler(); TestRunnable tr = new TestRunnable(); ListenableFuture<?> f = FutureUtils.scheduleWhile(scheduler, scheduleDelayMillis, false, tr, () -> tr.getRunCount() < 2); assertFalse(f.isDone()); assertEquals(1, scheduler.advance(scheduleDelayMillis)); assertTrue(f.isDone()); assertNull(f.get()); // verify no error state }