@Test public void makeResultListOrderTest() throws InterruptedException, ExecutionException { List<String> expectedResults = new ArrayList<>(TEST_QTY); List<SettableListenableFuture<String>> futures = new ArrayList<>(TEST_QTY); for (int i = 0; i < TEST_QTY; i++) { String result = StringUtils.makeRandomString(5); expectedResults.add(result); SettableListenableFuture<String> slf = new SettableListenableFuture<>(); if (i % 2 == 1) { slf.setResult(result); } futures.add(slf); } ListenableFuture<List<String>> collectionFuture = FutureUtils.makeResultListFuture(futures, false); for (int i = 0; i < TEST_QTY; i++) { SettableListenableFuture<String> slf = futures.get(i); if (! slf.isDone()) { slf.setResult(expectedResults.get(i)); } } List<String> actualResults = collectionFuture.get(); for (int i = 0; i < TEST_QTY; i++) { assertEquals(expectedResults.get(i), actualResults.get(i)); } }
if (! slf.isDone()) { slf.setResult(expectedResults.get(i - 1));
@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 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 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 cancelTest() { assertTrue(slf.cancel(false)); assertFalse(slf.cancel(false)); assertTrue(slf.isCancelled()); assertTrue(slf.isDone()); }