runningThreadFuture.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt();
runningThreadFuture.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt();
@Test (expected = ExecutionException.class) public void getNullExceptionTest() throws InterruptedException, ExecutionException { slf.setFailure(null); slf.get(); }
@Test public void getExecutionExceptionTest() throws InterruptedException { Exception failure = new Exception(); slf.setFailure(failure); try { slf.get(); fail("Exception should have thrown"); } catch (ExecutionException e) { assertTrue(failure == e.getCause()); } }
@Test public void getWithTimeoutExecutionExceptionTest() throws InterruptedException, TimeoutException { Exception failure = new Exception(); slf.setFailure(failure); try { slf.get(100, TimeUnit.MILLISECONDS); fail("Exception should have thrown"); } catch (ExecutionException e) { assertTrue(failure == e.getCause()); } }
@Test public void getCancellationTest() throws InterruptedException, ExecutionException { slf.cancel(false); try { slf.get(); fail("Exception should have thrown"); } catch (CancellationException e) { // expected } }
@Test public void getWithTimeoutCancellationTest() throws InterruptedException, ExecutionException, TimeoutException { slf.cancel(false); try { slf.get(100, TimeUnit.MILLISECONDS); fail("Exception should have thrown"); } catch (CancellationException e) { // expected } }
@Test (expected = IllegalStateException.class) public void getAfterClearResultFail() throws InterruptedException, ExecutionException { slf.setResult(null); slf.clearResult(); slf.get(); fail("Should have thrown exception"); }
@Test (expected = IllegalStateException.class) public void getAfterClearFailureResultFail() throws InterruptedException, ExecutionException { slf.setFailure(null); slf.clearResult(); slf.get(); fail("Should have thrown exception"); }
@Test public void addAsCallbackFailureTest() throws InterruptedException { Exception e = new Exception(); ListenableFuture<String> failureFuture = new ImmediateFailureListenableFuture<>(e); failureFuture.addCallback(slf); assertTrue(slf.isDone()); try { slf.get(); fail("Exception should have thrown"); } catch (ExecutionException ee) { assertTrue(ee.getCause() == e); } }
@Test public void getTimeoutTest() throws InterruptedException, ExecutionException { long startTime = Clock.accurateForwardProgressingMillis(); try { slf.get(DELAY_TIME, TimeUnit.MILLISECONDS); fail("Exception should have thrown"); } catch (TimeoutException e) { // expected } long endTime = Clock.accurateForwardProgressingMillis(); assertTrue(endTime - startTime >= DELAY_TIME); }
@Test public void cancelIncompleteFuturesTest() throws InterruptedException, ExecutionException { List<SettableListenableFuture<?>> futures = new ArrayList<>(TEST_QTY); for (int i = 0; i < TEST_QTY; i++) { SettableListenableFuture<?> slf = new SettableListenableFuture<>(); futures.add(slf); if (i % 2 == 0) { slf.setResult(null); } } FutureUtils.cancelIncompleteFutures(futures, false); for (int i = 0; i < TEST_QTY; i++) { SettableListenableFuture<?> slf = futures.get(i); assertTrue(slf.isDone()); if (i % 2 == 0) { slf.get(); // should not throw as was completed normally } else { assertTrue(slf.isCancelled()); } } }
@Test public void cancelIncompleteFuturesIfAnyFailTest() throws InterruptedException, ExecutionException { List<SettableListenableFuture<?>> futures = new ArrayList<>(TEST_QTY); for (int i = 0; i < TEST_QTY; i++) { SettableListenableFuture<?> slf = new SettableListenableFuture<>(); futures.add(slf); if (i % 2 == 0) { slf.setResult(null); } } FutureUtils.cancelIncompleteFuturesIfAnyFail(false, futures, false); // fail one future futures.get(1).setFailure(null); for (int i = 0; i < TEST_QTY; i++) { SettableListenableFuture<?> slf = futures.get(i); assertTrue(slf.isDone()); if (i % 2 == 0) { slf.get(); // should not throw as was completed normally } else if (i != 1) { // skip manually failed future assertTrue(slf.isCancelled()); } } }
@Test public void getResultTest() throws InterruptedException, ExecutionException { final String testResult = StringUtils.makeRandomString(5); PriorityScheduler scheduler = new StrictPriorityScheduler(1); try { scheduler.schedule(new Runnable() { @Override public void run() { slf.setResult(testResult); } }, DELAY_TIME); assertTrue(slf.get() == testResult); } finally { scheduler.shutdownNow(); } }
@Test public void addAsCallbackResultTest() throws InterruptedException, ExecutionException { String testResult = StringUtils.makeRandomString(5); ListenableFuture<String> resultFuture = new ImmediateResultListenableFuture<>(testResult); resultFuture.addCallback(slf); assertTrue(slf.isDone()); assertEquals(testResult, slf.get()); }
@Test public void getWithTimeoutResultTest() throws InterruptedException, ExecutionException, TimeoutException { final String testResult = StringUtils.makeRandomString(5); PriorityScheduler scheduler = new StrictPriorityScheduler(1); try { scheduler.prestartAllThreads(); scheduler.schedule(new Runnable() { @Override public void run() { slf.setResult(testResult); } }, DELAY_TIME); assertTrue(slf.get(DELAY_TIME + (SLOW_MACHINE ? 2000 : 1000), TimeUnit.MILLISECONDS) == testResult); } finally { scheduler.shutdownNow(); } }