--- jsr166/src/test/tck/CompletableFutureTest.java 2018/09/22 21:04:48 1.201 +++ jsr166/src/test/tck/CompletableFutureTest.java 2018/09/24 00:20:46 1.210 @@ -551,9 +551,7 @@ public class CompletableFutureTest exten public CompletableFuture apply(Integer x) { invoked(); value = x; - CompletableFuture f = new CompletableFuture<>(); - assertTrue(f.complete(inc(x))); - return f; + return CompletableFuture.completedFuture(inc(x)); } } @@ -574,9 +572,7 @@ public class CompletableFutureTest exten ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletionStage apply(Throwable x) { invoked(); - CompletableFuture d = new CompletableFuture(); - d.complete(value); - return d; + return CompletableFuture.completedFuture(value); } } @@ -930,12 +926,10 @@ public class CompletableFutureTest exten for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { - final AtomicInteger a = new AtomicInteger(0); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture g = m.exceptionally (f, (Throwable t) -> { - a.getAndIncrement(); threadFail("should not be called"); return null; // unreached }); @@ -943,7 +937,6 @@ public class CompletableFutureTest exten checkCompletedNormally(g, v1); checkCompletedNormally(f, v1); - assertEquals(0, a.get()); }} /** @@ -3221,6 +3214,59 @@ public class CompletableFutureTest exten r.assertInvoked(); }} + /** + * thenComposeExceptionally result completes exceptionally if the + * result of the action does + */ + public void testExceptionallyCompose_actionReturnsFailingFuture() { + for (ExecutionMode m : ExecutionMode.values()) + for (int order = 0; order < 6; order++) + for (Integer v1 : new Integer[] { 1, null }) + { + final CFException ex0 = new CFException(); + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final CompletableFuture g = new CompletableFuture<>(); + final CompletableFuture h; + // Test all permutations of orders + switch (order) { + case 0: + assertTrue(f.completeExceptionally(ex0)); + assertTrue(g.completeExceptionally(ex)); + h = m.exceptionallyCompose(f, (x -> g)); + break; + case 1: + assertTrue(f.completeExceptionally(ex0)); + h = m.exceptionallyCompose(f, (x -> g)); + assertTrue(g.completeExceptionally(ex)); + break; + case 2: + assertTrue(g.completeExceptionally(ex)); + assertTrue(f.completeExceptionally(ex0)); + h = m.exceptionallyCompose(f, (x -> g)); + break; + case 3: + assertTrue(g.completeExceptionally(ex)); + h = m.exceptionallyCompose(f, (x -> g)); + assertTrue(f.completeExceptionally(ex0)); + break; + case 4: + h = m.exceptionallyCompose(f, (x -> g)); + assertTrue(f.completeExceptionally(ex0)); + assertTrue(g.completeExceptionally(ex)); + break; + case 5: + h = m.exceptionallyCompose(f, (x -> g)); + assertTrue(f.completeExceptionally(ex0)); + assertTrue(g.completeExceptionally(ex)); + break; + default: throw new AssertionError(); + } + + checkCompletedExceptionally(g, ex); + checkCompletedWithWrappedException(h, ex); + checkCompletedExceptionally(f, ex0); + }} // other static methods @@ -4784,10 +4830,32 @@ public class CompletableFutureTest exten } /** + * default-implemented exceptionallyAsync action is not invoked when + * source completes normally, and source result is propagated + */ + public void testDefaultExceptionallyAsync_normalCompletion() { + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final DelegatedCompletionStage d = + new DelegatedCompletionStage(f); + if (!createIncomplete) assertTrue(f.complete(v1)); + final CompletionStage g = d.exceptionallyAsync + ((Throwable t) -> { + threadFail("should not be called"); + return null; // unreached + }); + if (createIncomplete) assertTrue(f.complete(v1)); + + checkCompletedNormally(g.toCompletableFuture(), v1); + }} + + /** * default-implemented exceptionallyAsync action completes with * function value on source exception */ - public void testDefaulExceptionallyAsync_exceptionalCompletion() { + public void testDefaultExceptionallyAsync_exceptionalCompletion() { for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { @@ -4814,7 +4882,7 @@ public class CompletableFutureTest exten * throws an exception, it completes exceptionally with that * exception */ - public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() { + public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) { final AtomicInteger a = new AtomicInteger(0); @@ -4987,7 +5055,7 @@ public class CompletableFutureTest exten final DelegatedCompletionStage d = new DelegatedCompletionStage(f); if (!createIncomplete) assertTrue(f.complete(v1)); - final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); + final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedNormally(f, v1); @@ -5009,7 +5077,7 @@ public class CompletableFutureTest exten final DelegatedCompletionStage d = new DelegatedCompletionStage(f); if (!createIncomplete) f.completeExceptionally(ex); - final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); + final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); if (createIncomplete) f.completeExceptionally(ex); checkCompletedExceptionally(f, ex); @@ -5032,7 +5100,7 @@ public class CompletableFutureTest exten final DelegatedCompletionStage d = new DelegatedCompletionStage(f); if (!createIncomplete) f.completeExceptionally(ex); - final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); + final CompletionStage g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); if (createIncomplete) f.completeExceptionally(ex); checkCompletedExceptionally(f, ex);