--- jsr166/src/test/tck/CompletableFutureTest.java 2013/04/08 20:46:59 1.22 +++ jsr166/src/test/tck/CompletableFutureTest.java 2013/07/22 18:25:42 1.30 @@ -68,6 +68,7 @@ public class CompletableFutureTest exten } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); + assertFalse(f.isCompletedExceptionally()); assertTrue(f.toString().contains("[Completed normally]")); } @@ -121,6 +122,7 @@ public class CompletableFutureTest exten } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); + assertTrue(f.isCompletedExceptionally()); assertTrue(f.isCancelled()); assertTrue(f.toString().contains("[Completed exceptionally]")); } @@ -152,6 +154,7 @@ public class CompletableFutureTest exten } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); + assertTrue(f.isCompletedExceptionally()); assertTrue(f.toString().contains("[Completed exceptionally]")); } @@ -256,7 +259,6 @@ public class CompletableFutureTest exten assertEquals(g.getNumberOfDependents(), 0); } - /** * toString indicates current completion state */ @@ -371,10 +373,9 @@ public class CompletableFutureTest exten } } - /** * exceptionally action completes with function value on source - * exception; otherwise with source value + * exception; otherwise with source value */ public void testExceptionally() { CompletableFuture f = new CompletableFuture<>(); @@ -495,17 +496,21 @@ public class CompletableFutureTest exten * thenRun result completes normally after normal completion of source */ public void testThenRun() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRun(r); + CompletableFuture f; + CompletableFuture g; + Noop r; + + f = new CompletableFuture<>(); + g = f.thenRun(r = new Noop()); f.complete(null); checkCompletedNormally(g, null); - // reordered version + assertTrue(r.ran); + f = new CompletableFuture<>(); f.complete(null); - r = new Noop(); - g = f.thenRun(r); + g = f.thenRun(r = new Noop()); checkCompletedNormally(g, null); + assertTrue(r.ran); } /** @@ -513,32 +518,58 @@ public class CompletableFutureTest exten * completion of source */ public void testThenRun2() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRun(r); + CompletableFuture f; + CompletableFuture g; + Noop r; + + f = new CompletableFuture<>(); + g = f.thenRun(r = new Noop()); f.completeExceptionally(new CFException()); checkCompletedWithWrappedCFException(g); + assertFalse(r.ran); + + f = new CompletableFuture<>(); + f.completeExceptionally(new CFException()); + g = f.thenRun(r = new Noop()); + checkCompletedWithWrappedCFException(g); + assertFalse(r.ran); } /** * thenRun result completes exceptionally if action does */ public void testThenRun3() { - CompletableFuture f = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.thenRun(r); + CompletableFuture f; + CompletableFuture g; + FailingNoop r; + + f = new CompletableFuture<>(); + g = f.thenRun(r = new FailingNoop()); f.complete(null); checkCompletedWithWrappedCFException(g); + + f = new CompletableFuture<>(); + f.complete(null); + g = f.thenRun(r = new FailingNoop()); + checkCompletedWithWrappedCFException(g); } /** * thenRun result completes exceptionally if source cancelled */ public void testThenRun4() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRun(r); + CompletableFuture f; + CompletableFuture g; + Noop r; + + f = new CompletableFuture<>(); + g = f.thenRun(r = new Noop()); + assertTrue(f.cancel(true)); + checkCompletedWithWrappedCancellationException(g); + + f = new CompletableFuture<>(); assertTrue(f.cancel(true)); + g = f.thenRun(r = new Noop()); checkCompletedWithWrappedCancellationException(g); } @@ -630,7 +661,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - /** * thenCombine result completes normally after normal completion * of sources @@ -1138,7 +1168,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - /** * runAfterEither result completes normally after normal completion * of either source @@ -1287,7 +1316,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - // asyncs /** @@ -1320,8 +1348,7 @@ public class CompletableFutureTest exten try { g.join(); shouldThrow(); - } catch (Exception ok) { - } + } catch (CompletionException success) {} checkCompletedWithWrappedCFException(g); } @@ -2055,7 +2082,6 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); } - // async with explicit executors /** @@ -2088,8 +2114,7 @@ public class CompletableFutureTest exten try { g.join(); shouldThrow(); - } catch (Exception ok) { - } + } catch (CompletionException success) {} checkCompletedWithWrappedCFException(g); } @@ -2826,14 +2851,15 @@ public class CompletableFutureTest exten * with the value null */ public void testAllOf_empty() throws Exception { - CompletableFuture f = CompletableFuture.allOf(); + CompletableFuture f = CompletableFuture.allOf(); checkCompletedNormally(f, null); } /** - * allOf returns a future completed when all components complete + * allOf returns a future completed normally with the value null + * when all components complete normally */ - public void testAllOf() throws Exception { + public void testAllOf_normal() throws Exception { for (int k = 1; k < 20; ++k) { CompletableFuture[] fs = (CompletableFuture[]) new CompletableFuture[k]; for (int i = 0; i < k; ++i) @@ -2841,9 +2867,11 @@ public class CompletableFutureTest exten CompletableFuture f = CompletableFuture.allOf(fs); for (int i = 0; i < k; ++i) { checkIncomplete(f); + checkIncomplete(CompletableFuture.allOf(fs)); fs[i].complete(one); } checkCompletedNormally(f, null); + checkCompletedNormally(CompletableFuture.allOf(fs), null); } } @@ -2851,15 +2879,16 @@ public class CompletableFutureTest exten * anyOf(no component futures) returns an incomplete future */ public void testAnyOf_empty() throws Exception { - CompletableFuture f = CompletableFuture.anyOf(); + CompletableFuture f = CompletableFuture.anyOf(); checkIncomplete(f); } /** - * anyOf returns a future completed when any components complete + * anyOf returns a future completed normally with a value when + * a component future does */ - public void testAnyOf() throws Exception { - for (int k = 1; k < 20; ++k) { + public void testAnyOf_normal() throws Exception { + for (int k = 0; k < 10; ++k) { CompletableFuture[] fs = new CompletableFuture[k]; for (int i = 0; i < k; ++i) fs[i] = new CompletableFuture<>(); @@ -2868,6 +2897,25 @@ public class CompletableFutureTest exten for (int i = 0; i < k; ++i) { fs[i].complete(one); checkCompletedNormally(f, one); + checkCompletedNormally(CompletableFuture.anyOf(fs), one); + } + } + } + + /** + * anyOf result completes exceptionally when any component does. + */ + public void testAnyOf_exceptional() throws Exception { + for (int k = 0; k < 10; ++k) { + CompletableFuture[] fs = new CompletableFuture[k]; + for (int i = 0; i < k; ++i) + fs[i] = new CompletableFuture<>(); + CompletableFuture f = CompletableFuture.anyOf(fs); + checkIncomplete(f); + for (int i = 0; i < k; ++i) { + fs[i].completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); } } } @@ -2980,4 +3028,233 @@ public class CompletableFutureTest exten assertEquals(0, exec.count.get()); } + /** + * toCompletableFuture returns this CompletableFuture. + */ + public void testToCompletableFuture() { + CompletableFuture f = new CompletableFuture<>(); + assertSame(f, f.toCompletableFuture()); + } + + /** + * whenComplete action executes on normal completion, propagating + * source result. + */ + public void testWhenComplete1() { + final AtomicInteger a = new AtomicInteger(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); + f.complete(three); + checkCompletedNormally(f, three); + checkCompletedNormally(g, three); + assertEquals(a.get(), 1); + } + + /** + * whenComplete action executes on exceptional completion, propagating + * source result. + */ + public void testWhenComplete2() { + final AtomicInteger a = new AtomicInteger(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); + f.completeExceptionally(new CFException()); + assertTrue(f.isCompletedExceptionally()); + assertTrue(g.isCompletedExceptionally()); + assertEquals(a.get(), 1); + } + + /** + * If a whenComplete action throws an exception when triggered by + * a normal completion, it completes exceptionally + */ + public void testWhenComplete3() { + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenComplete((Integer x, Throwable t) -> + { throw new CFException(); } ); + f.complete(three); + checkCompletedNormally(f, three); + assertTrue(g.isCompletedExceptionally()); + checkCompletedWithWrappedCFException(g); + } + + /** + * whenCompleteAsync action executes on normal completion, propagating + * source result. + */ + public void testWhenCompleteAsync1() { + final AtomicInteger a = new AtomicInteger(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); + f.complete(three); + checkCompletedNormally(f, three); + checkCompletedNormally(g, three); + assertEquals(a.get(), 1); + } + + /** + * whenCompleteAsync action executes on exceptional completion, propagating + * source result. + */ + public void testWhenCompleteAsync2() { + final AtomicInteger a = new AtomicInteger(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedWithWrappedCFException(g); + } + + /** + * If a whenCompleteAsync action throws an exception when + * triggered by a normal completion, it completes exceptionally + */ + public void testWhenCompleteAsync3() { + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> + { throw new CFException(); } ); + f.complete(three); + checkCompletedNormally(f, three); + checkCompletedWithWrappedCFException(g); + } + + /** + * whenCompleteAsync action executes on normal completion, propagating + * source result. + */ + public void testWhenCompleteAsync1e() { + final AtomicInteger a = new AtomicInteger(); + ThreadExecutor exec = new ThreadExecutor(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), + exec); + f.complete(three); + checkCompletedNormally(f, three); + checkCompletedNormally(g, three); + assertEquals(a.get(), 1); + } + + /** + * whenCompleteAsync action executes on exceptional completion, propagating + * source result. + */ + public void testWhenCompleteAsync2e() { + final AtomicInteger a = new AtomicInteger(); + ThreadExecutor exec = new ThreadExecutor(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), + exec); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedWithWrappedCFException(g); + } + + /** + * If a whenCompleteAsync action throws an exception when triggered + * by a normal completion, it completes exceptionally + */ + public void testWhenCompleteAsync3e() { + ThreadExecutor exec = new ThreadExecutor(); + CompletableFuture f = new CompletableFuture<>(); + CompletableFuture g = + f.whenCompleteAsync((Integer x, Throwable t) -> + { throw new CFException(); }, + exec); + f.complete(three); + checkCompletedNormally(f, three); + checkCompletedWithWrappedCFException(g); + } + + /** + * handleAsync action completes normally with function value on + * either normal or exceptional completion of source + */ + public void testHandleAsync() { + CompletableFuture f, g; + IntegerHandler r; + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler()); + assertFalse(r.ran); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedNormally(g, three); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler()); + assertFalse(r.ran); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedNormally(g, three); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler()); + assertFalse(r.ran); + f.complete(one); + checkCompletedNormally(f, one); + checkCompletedNormally(g, two); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler()); + assertFalse(r.ran); + f.complete(one); + checkCompletedNormally(f, one); + checkCompletedNormally(g, two); + assertTrue(r.ran); + } + + /** + * handleAsync action with Executor completes normally with + * function value on either normal or exceptional completion of + * source + */ + public void testHandleAsync2() { + CompletableFuture f, g; + ThreadExecutor exec = new ThreadExecutor(); + IntegerHandler r; + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler(), exec); + assertFalse(r.ran); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedNormally(g, three); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler(), exec); + assertFalse(r.ran); + f.completeExceptionally(new CFException()); + checkCompletedWithWrappedCFException(f); + checkCompletedNormally(g, three); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler(), exec); + assertFalse(r.ran); + f.complete(one); + checkCompletedNormally(f, one); + checkCompletedNormally(g, two); + assertTrue(r.ran); + + f = new CompletableFuture<>(); + g = f.handleAsync(r = new IntegerHandler(), exec); + assertFalse(r.ran); + f.complete(one); + checkCompletedNormally(f, one); + checkCompletedNormally(g, two); + assertTrue(r.ran); + } + }