--- jsr166/src/test/tck/CompletableFutureTest.java 2015/11/15 20:03:08 1.135 +++ jsr166/src/test/tck/CompletableFutureTest.java 2015/11/15 23:31:51 1.138 @@ -876,6 +876,10 @@ public class CompletableFutureTest exten assertEquals(1, a.get()); }} + /** + * If an "exceptionally action" throws an exception, it completes + * exceptionally with that exception + */ public void testExceptionally_exceptionalCompletionActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) { @@ -894,6 +898,7 @@ public class CompletableFutureTest exten if (createIncomplete) f.completeExceptionally(ex1); checkCompletedWithWrappedException(g, ex2); + checkCompletedExceptionally(f, ex1); assertEquals(1, a.get()); }} @@ -1035,6 +1040,8 @@ public class CompletableFutureTest exten checkCompletedWithWrappedException(g, ex1); checkCompletedExceptionally(f, ex1); + assertEquals(1, ex1.getSuppressed().length); + assertSame(ex2, ex1.getSuppressed()[0]); assertEquals(1, a.get()); }} @@ -3821,29 +3828,33 @@ public class CompletableFutureTest exten AtomicReference firstFailure = new AtomicReference<>(null); } - // Monadic "plus" + /** Implements "monadic plus". */ static CompletableFuture plus(CompletableFuture f, CompletableFuture g) { PlusFuture plus = new PlusFuture(); BiConsumer action = (T result, Throwable ex) -> { - if (ex == null) { - if (plus.complete(result)) - if (plus.firstFailure.get() != null) + try { + if (ex == null) { + if (plus.complete(result)) + if (plus.firstFailure.get() != null) + plus.firstFailure.set(null); + } + else if (plus.firstFailure.compareAndSet(null, ex)) { + if (plus.isDone()) plus.firstFailure.set(null); - } - else if (plus.firstFailure.compareAndSet(null, ex)) { - if (plus.isDone()) - plus.firstFailure.set(null); - } - else { - // first failure has precedence - Throwable first = plus.firstFailure.getAndSet(null); - - // may fail with "Self-suppression not permitted" - try { first.addSuppressed(ex); } - catch (Exception ignored) {} - - plus.completeExceptionally(first); + } + else { + // first failure has precedence + Throwable first = plus.firstFailure.getAndSet(null); + + // may fail with "Self-suppression not permitted" + try { first.addSuppressed(ex); } + catch (Exception ignored) {} + + plus.completeExceptionally(first); + } + } catch (Throwable unexpected) { + plus.completeExceptionally(unexpected); } }; f.whenComplete(action);