--- jsr166/src/test/tck/CompletableFutureTest.java 2018/07/22 22:08:49 1.197 +++ jsr166/src/test/tck/CompletableFutureTest.java 2018/09/17 11:50:55 1.198 @@ -557,6 +557,29 @@ public class CompletableFutureTest exten } } + static class FailingExceptionalCompletableFutureFunction extends CheckedAction + implements Function> + { + final CFException ex; + FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } + public CompletableFuture apply(Throwable x) { + invoked(); + throw ex; + } + } + + static class ExceptionalCompletableFutureFunction extends CheckedAction + implements Function> { + final Integer value = 3; + ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); } + public CompletionStage apply(Throwable x) { + invoked(); + CompletableFuture d = new CompletableFuture(); + d.complete(value); + return d; + } + } + static class FailingCompletableFutureFunction extends CheckedIntegerAction implements Function> { @@ -672,8 +695,16 @@ public class CompletableFutureTest exten Function a) { return f.applyToEither(g, a); } + public CompletableFuture exceptionally + (CompletableFuture f, + Function fn) { + return f.exceptionally(fn); + } + public CompletableFuture exceptionallyCompose + (CompletableFuture f, Function> fn) { + return f.exceptionallyCompose(fn); + } }, - ASYNC { public void checkExecutionMode() { assertEquals(defaultExecutorIsCommonPool, @@ -746,6 +777,17 @@ public class CompletableFutureTest exten Function a) { return f.applyToEitherAsync(g, a); } + public CompletableFuture exceptionally + (CompletableFuture f, + Function fn) { + return f.exceptionallyAsync(fn); + } + + public CompletableFuture exceptionallyCompose + (CompletableFuture f, Function> fn) { + return f.exceptionallyComposeAsync(fn); + } + }, EXECUTOR { @@ -819,6 +861,16 @@ public class CompletableFutureTest exten Function a) { return f.applyToEitherAsync(g, a, new ThreadExecutor()); } + public CompletableFuture exceptionally + (CompletableFuture f, + Function fn) { + return f.exceptionallyAsync(fn, new ThreadExecutor()); + } + public CompletableFuture exceptionallyCompose + (CompletableFuture f, Function> fn) { + return f.exceptionallyComposeAsync(fn, new ThreadExecutor()); + } + }; public abstract void checkExecutionMode(); @@ -861,6 +913,12 @@ public class CompletableFutureTest exten (CompletableFuture f, CompletionStage g, Function a); + public abstract CompletableFuture exceptionally + (CompletableFuture f, + Function fn); + public abstract CompletableFuture exceptionallyCompose + (CompletableFuture f, + Function> fn); } /** @@ -868,14 +926,15 @@ public class CompletableFutureTest exten * normally, and source result is propagated */ public void testExceptionally_normalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) 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 = f.exceptionally - ((Throwable t) -> { + final CompletableFuture g = m.exceptionally + (f, (Throwable t) -> { a.getAndIncrement(); threadFail("should not be called"); return null; // unreached @@ -892,6 +951,7 @@ public class CompletableFutureTest exten * exception */ public void testExceptionally_exceptionalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { @@ -899,9 +959,9 @@ public class CompletableFutureTest exten final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex); - final CompletableFuture g = f.exceptionally - ((Throwable t) -> { - ExecutionMode.SYNC.checkExecutionMode(); + final CompletableFuture g = m.exceptionally + (f, (Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(t, ex); a.getAndIncrement(); return v1; @@ -917,6 +977,7 @@ public class CompletableFutureTest exten * exceptionally with that exception */ public void testExceptionally_exceptionalCompletionActionFailed() { + for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) { final AtomicInteger a = new AtomicInteger(0); @@ -924,9 +985,9 @@ public class CompletableFutureTest exten final CFException ex2 = new CFException(); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex1); - final CompletableFuture g = f.exceptionally - ((Throwable t) -> { - ExecutionMode.SYNC.checkExecutionMode(); + final CompletableFuture g = m.exceptionally + (f, (Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(t, ex1); a.getAndIncrement(); throw ex2; @@ -3097,6 +3158,70 @@ public class CompletableFutureTest exten checkCompletedNormally(f, v1); }} + /** + * exceptionallyCompose result completes normally after normal + * completion of source + */ + public void testExceptionallyCompose_normalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final ExceptionalCompletableFutureFunction r = + new ExceptionalCompletableFutureFunction(m); + if (!createIncomplete) assertTrue(f.complete(v1)); + final CompletableFuture g = m.exceptionallyCompose(f, r); + if (createIncomplete) assertTrue(f.complete(v1)); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v1); + r.assertNotInvoked(); + }} + + /** + * exceptionallyCompose result completes normally after exceptional + * completion of source + */ + public void testExceptionallyCompose_exceptionalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + { + final CFException ex = new CFException(); + final ExceptionalCompletableFutureFunction r = + new ExceptionalCompletableFutureFunction(m); + final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.exceptionallyCompose(f, r); + if (createIncomplete) f.completeExceptionally(ex); + + checkCompletedExceptionally(f, ex); + checkCompletedNormally(g, r.value); + r.assertInvoked(); + }} + + /** + * exceptionallyCompose completes exceptionally on exception if action does + */ + public void testExceptionallyCompose_actionFailed() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final FailingExceptionalCompletableFutureFunction r + = new FailingExceptionalCompletableFutureFunction(m); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.exceptionallyCompose(f, r); + if (createIncomplete) f.completeExceptionally(ex); + + checkCompletedExceptionally(f, ex); + checkCompletedWithWrappedException(g, r.ex); + r.assertInvoked(); + }} + + // other static methods /**