ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.36 by jsr166, Sun Jun 1 23:12:45 2014 UTC vs.
Revision 1.77 by jsr166, Sat Jun 7 21:46:50 2014 UTC

# Line 17 | Line 17 | import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19   import java.util.concurrent.CompletionStage;
20 + import java.util.concurrent.ForkJoinPool;
21 + import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.TimeoutException;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 103 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 129 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 216 | Line 239 | public class CompletableFutureTest exten
239       */
240      public void testCompleteExceptionally() {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242 +        CFException ex = new CFException();
243          checkIncomplete(f);
244 <        f.completeExceptionally(new CFException());
245 <        checkCompletedWithWrappedCFException(f);
244 >        f.completeExceptionally(ex);
245 >        checkCompletedExceptionally(f, ex);
246      }
247  
248      /**
# Line 247 | Line 271 | public class CompletableFutureTest exten
271          f = new CompletableFuture<>();
272          f.obtrudeValue(three);
273          checkCompletedNormally(f, three);
274 +        f.obtrudeValue(null);
275 +        checkCompletedNormally(f, null);
276          f = new CompletableFuture<>();
277          f.completeExceptionally(new CFException());
278          f.obtrudeValue(four);
# Line 257 | Line 283 | public class CompletableFutureTest exten
283       * obtrudeException forces completion with given exception
284       */
285      public void testObtrudeException() {
286 <        CompletableFuture<Integer> f = new CompletableFuture<>();
287 <        checkIncomplete(f);
288 <        f.complete(one);
289 <        checkCompletedNormally(f, one);
290 <        f.obtrudeException(new CFException());
265 <        checkCompletedWithWrappedCFException(f);
286 >        for (Integer v1 : new Integer[] { 1, null })
287 >    {
288 >        CFException ex;
289 >        CompletableFuture<Integer> f;
290 >
291          f = new CompletableFuture<>();
292 <        f.obtrudeException(new CFException());
293 <        checkCompletedWithWrappedCFException(f);
292 >        f.complete(v1);
293 >        for (int i = 0; i < 2; i++) {
294 >            f.obtrudeException(ex = new CFException());
295 >            checkCompletedExceptionally(f, ex);
296 >        }
297 >
298          f = new CompletableFuture<>();
299 +        for (int i = 0; i < 2; i++) {
300 +            f.obtrudeException(ex = new CFException());
301 +            checkCompletedExceptionally(f, ex);
302 +        }
303 +
304 +        f = new CompletableFuture<>();
305 +        f.completeExceptionally(ex = new CFException());
306 +        f.obtrudeValue(v1);
307 +        checkCompletedNormally(f, v1);
308 +        f.obtrudeException(ex = new CFException());
309 +        checkCompletedExceptionally(f, ex);
310          f.completeExceptionally(new CFException());
311 <        f.obtrudeValue(four);
312 <        checkCompletedNormally(f, four);
313 <        f.obtrudeException(new CFException());
314 <        checkCompletedWithWrappedCFException(f);
275 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
318       */
319      public void testGetNumberOfDependents() {
320 +        for (ExecutionMode m : ExecutionMode.values())
321 +    {
322          CompletableFuture<Integer> f = new CompletableFuture<>();
323 <        assertEquals(f.getNumberOfDependents(), 0);
324 <        CompletableFuture g = f.thenRun(new Noop());
325 <        assertEquals(f.getNumberOfDependents(), 1);
326 <        assertEquals(g.getNumberOfDependents(), 0);
327 <        CompletableFuture h = f.thenRun(new Noop());
328 <        assertEquals(f.getNumberOfDependents(), 2);
323 >        assertEquals(0, f.getNumberOfDependents());
324 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
325 >        assertEquals(1, f.getNumberOfDependents());
326 >        assertEquals(0, g.getNumberOfDependents());
327 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
328 >        assertEquals(2, f.getNumberOfDependents());
329 >        assertEquals(0, h.getNumberOfDependents());
330          f.complete(1);
331          checkCompletedNormally(g, null);
332 <        assertEquals(f.getNumberOfDependents(), 0);
333 <        assertEquals(g.getNumberOfDependents(), 0);
334 <    }
332 >        checkCompletedNormally(h, null);
333 >        assertEquals(0, f.getNumberOfDependents());
334 >        assertEquals(0, g.getNumberOfDependents());
335 >        assertEquals(0, h.getNumberOfDependents());
336 >    }}
337  
338      /**
339       * toString indicates current completion state
# Line 306 | Line 350 | public class CompletableFutureTest exten
350          f = new CompletableFuture<String>();
351          f.completeExceptionally(new IndexOutOfBoundsException());
352          assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +
354 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
355 +            f = new CompletableFuture<String>();
356 +            f.cancel(mayInterruptIfRunning);
357 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
358 +        }
359      }
360  
361      /**
# Line 316 | Line 366 | public class CompletableFutureTest exten
366          checkCompletedNormally(f, "test");
367      }
368  
369 <    // Choose non-commutative actions for better coverage
369 >    abstract class CheckedAction {
370 >        int invocationCount = 0;
371 >        final ExecutionMode m;
372 >        CheckedAction(ExecutionMode m) { this.m = m; }
373 >        void invoked() {
374 >            m.checkExecutionMode();
375 >            assertEquals(0, invocationCount++);
376 >        }
377 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
378 >        void assertInvoked() { assertEquals(1, invocationCount); }
379 >    }
380 >
381 >    abstract class CheckedIntegerAction extends CheckedAction {
382 >        Integer value;
383 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
384 >        void assertValue(Integer expected) {
385 >            assertInvoked();
386 >            assertEquals(expected, value);
387 >        }
388 >    }
389 >
390 >    class IntegerSupplier extends CheckedAction
391 >        implements Supplier<Integer>
392 >    {
393 >        final Integer value;
394 >        IntegerSupplier(ExecutionMode m, Integer value) {
395 >            super(m);
396 >            this.value = value;
397 >        }
398 >        public Integer get() {
399 >            invoked();
400 >            return value;
401 >        }
402 >    }
403 >
404 >    // A function that handles and produces null values as well.
405 >    static Integer inc(Integer x) {
406 >        return (x == null) ? null : x + 1;
407 >    }
408 >
409 >    class NoopConsumer extends CheckedIntegerAction
410 >        implements Consumer<Integer>
411 >    {
412 >        NoopConsumer(ExecutionMode m) { super(m); }
413 >        public void accept(Integer x) {
414 >            invoked();
415 >            value = x;
416 >        }
417 >    }
418 >
419 >    class IncFunction extends CheckedIntegerAction
420 >        implements Function<Integer,Integer>
421 >    {
422 >        IncFunction(ExecutionMode m) { super(m); }
423 >        public Integer apply(Integer x) {
424 >            invoked();
425 >            return value = inc(x);
426 >        }
427 >    }
428  
429 <    // A non-commutative function that handles null values as well
430 <    public static int subtract(Integer x, Integer y) {
431 <        return ((x == null) ? 42 : x.intValue())
429 >    // Choose non-commutative actions for better coverage
430 >    // A non-commutative function that handles and produces null values as well.
431 >    static Integer subtract(Integer x, Integer y) {
432 >        return (x == null && y == null) ? null :
433 >            ((x == null) ? 42 : x.intValue())
434              - ((y == null) ? 99 : y.intValue());
435      }
436  
437 <    static final Supplier<Integer> supplyOne =
438 <        () -> Integer.valueOf(1);
439 <    static final Function<Integer, Integer> inc =
440 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
331 <    static final BiFunction<Integer, Integer, Integer> subtract =
332 <        (Integer x, Integer y) -> subtract(x, y);
333 <    static final class IncAction implements Consumer<Integer> {
334 <        int value;
335 <        public void accept(Integer x) { value = x.intValue() + 1; }
336 <    }
337 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
338 <        volatile int invocationCount = 0;
339 <        int value;
340 <        // Check this action was invoked exactly once when result is computed.
341 <        public boolean ran() { return invocationCount == 1; }
437 >    class SubtractAction extends CheckedIntegerAction
438 >        implements BiConsumer<Integer, Integer>
439 >    {
440 >        SubtractAction(ExecutionMode m) { super(m); }
441          public void accept(Integer x, Integer y) {
442 <            invocationCount++;
442 >            invoked();
443              value = subtract(x, y);
444          }
445      }
446 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
447 <        volatile int invocationCount = 0;
448 <        int value;
449 <        // Check this action was invoked exactly once when result is computed.
450 <        public boolean ran() { return invocationCount == 1; }
446 >
447 >    class SubtractFunction extends CheckedIntegerAction
448 >        implements BiFunction<Integer, Integer, Integer>
449 >    {
450 >        SubtractFunction(ExecutionMode m) { super(m); }
451          public Integer apply(Integer x, Integer y) {
452 <            invocationCount++;
453 <            return subtract(x, y);
452 >            invoked();
453 >            return value = subtract(x, y);
454          }
455      }
456 <    static final class Noop implements Runnable {
457 <        boolean ran;
458 <        public void run() { ran = true; }
456 >
457 >    class Noop extends CheckedAction implements Runnable {
458 >        Noop(ExecutionMode m) { super(m); }
459 >        public void run() {
460 >            invoked();
461 >        }
462      }
463  
464 <    static final class FailingSupplier implements Supplier<Integer> {
465 <        boolean ran;
466 <        public Integer get() { ran = true; throw new CFException(); }
464 >    class FailingSupplier extends CheckedAction
465 >        implements Supplier<Integer>
466 >    {
467 >        FailingSupplier(ExecutionMode m) { super(m); }
468 >        public Integer get() {
469 >            invoked();
470 >            throw new CFException();
471 >        }
472      }
473 <    static final class FailingConsumer implements Consumer<Integer> {
474 <        boolean ran;
475 <        public void accept(Integer x) { ran = true; throw new CFException(); }
473 >
474 >    class FailingConsumer extends CheckedIntegerAction
475 >        implements Consumer<Integer>
476 >    {
477 >        FailingConsumer(ExecutionMode m) { super(m); }
478 >        public void accept(Integer x) {
479 >            invoked();
480 >            value = x;
481 >            throw new CFException();
482 >        }
483      }
484 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
485 <        boolean ran;
486 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
484 >
485 >    class FailingBiConsumer extends CheckedIntegerAction
486 >        implements BiConsumer<Integer, Integer>
487 >    {
488 >        FailingBiConsumer(ExecutionMode m) { super(m); }
489 >        public void accept(Integer x, Integer y) {
490 >            invoked();
491 >            value = subtract(x, y);
492 >            throw new CFException();
493 >        }
494      }
495 <    static final class FailingFunction implements Function<Integer, Integer> {
496 <        boolean ran;
497 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
495 >
496 >    class FailingFunction extends CheckedIntegerAction
497 >        implements Function<Integer, Integer>
498 >    {
499 >        FailingFunction(ExecutionMode m) { super(m); }
500 >        public Integer apply(Integer x) {
501 >            invoked();
502 >            value = x;
503 >            throw new CFException();
504 >        }
505      }
506 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
507 <        boolean ran;
508 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
506 >
507 >    class FailingBiFunction extends CheckedIntegerAction
508 >        implements BiFunction<Integer, Integer, Integer>
509 >    {
510 >        FailingBiFunction(ExecutionMode m) { super(m); }
511 >        public Integer apply(Integer x, Integer y) {
512 >            invoked();
513 >            value = subtract(x, y);
514 >            throw new CFException();
515 >        }
516      }
517 <    static final class FailingNoop implements Runnable {
518 <        boolean ran;
519 <        public void run() { ran = true; throw new CFException(); }
517 >
518 >    class FailingRunnable extends CheckedAction implements Runnable {
519 >        FailingRunnable(ExecutionMode m) { super(m); }
520 >        public void run() {
521 >            invoked();
522 >            throw new CFException();
523 >        }
524      }
525  
526 <    static final class CompletableFutureInc
527 <        implements Function<Integer, CompletableFuture<Integer>> {
528 <        boolean ran;
526 >
527 >    class CompletableFutureInc extends CheckedIntegerAction
528 >        implements Function<Integer, CompletableFuture<Integer>>
529 >    {
530 >        CompletableFutureInc(ExecutionMode m) { super(m); }
531          public CompletableFuture<Integer> apply(Integer x) {
532 <            ran = true;
532 >            invoked();
533 >            value = x;
534              CompletableFuture<Integer> f = new CompletableFuture<>();
535 <            f.complete(Integer.valueOf(x.intValue() + 1));
535 >            f.complete(inc(x));
536              return f;
537          }
538      }
539  
540 <    static final class FailingCompletableFutureFunction
541 <        implements Function<Integer, CompletableFuture<Integer>> {
542 <        boolean ran;
540 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
541 >        implements Function<Integer, CompletableFuture<Integer>>
542 >    {
543 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
544          public CompletableFuture<Integer> apply(Integer x) {
545 <            ran = true; throw new CFException();
545 >            invoked();
546 >            value = x;
547 >            throw new CFException();
548          }
549      }
550  
551      // Used for explicit executor tests
552      static final class ThreadExecutor implements Executor {
553 <        AtomicInteger count = new AtomicInteger(0);
553 >        final AtomicInteger count = new AtomicInteger(0);
554 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
555 >        static boolean startedCurrentThread() {
556 >            return Thread.currentThread().getThreadGroup() == tg;
557 >        }
558  
559          public void execute(Runnable r) {
560              count.getAndIncrement();
561 <            new Thread(r).start();
413 <        }
414 <    }
415 <
416 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
417 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
418 <    }
419 <
420 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
421 <        boolean ran;
422 <        public Integer apply(Integer x, Throwable t) {
423 <            ran = true;
424 <            return (t == null) ? two : three;
561 >            new Thread(tg, r).start();
562          }
563      }
564  
565      /**
566       * Permits the testing of parallel code for the 3 different
567 <     * execution modes without repeating all the testing code.
567 >     * execution modes without copy/pasting all the test methods.
568       */
569      enum ExecutionMode {
570          DEFAULT {
571 +            public void checkExecutionMode() {
572 +                assertFalse(ThreadExecutor.startedCurrentThread());
573 +                assertNull(ForkJoinTask.getPool());
574 +            }
575 +            public CompletableFuture<Void> runAsync(Runnable a) {
576 +                throw new UnsupportedOperationException();
577 +            }
578 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
579 +                throw new UnsupportedOperationException();
580 +            }
581 +            public <T> CompletableFuture<Void> thenRun
582 +                (CompletableFuture<T> f, Runnable a) {
583 +                return f.thenRun(a);
584 +            }
585 +            public <T> CompletableFuture<Void> thenAccept
586 +                (CompletableFuture<T> f, Consumer<? super T> a) {
587 +                return f.thenAccept(a);
588 +            }
589 +            public <T,U> CompletableFuture<U> thenApply
590 +                (CompletableFuture<T> f, Function<? super T,U> a) {
591 +                return f.thenApply(a);
592 +            }
593 +            public <T,U> CompletableFuture<U> thenCompose
594 +                (CompletableFuture<T> f,
595 +                 Function<? super T,? extends CompletionStage<U>> a) {
596 +                return f.thenCompose(a);
597 +            }
598 +            public <T,U> CompletableFuture<U> handle
599 +                (CompletableFuture<T> f,
600 +                 BiFunction<? super T,Throwable,? extends U> a) {
601 +                return f.handle(a);
602 +            }
603 +            public <T> CompletableFuture<T> whenComplete
604 +                (CompletableFuture<T> f,
605 +                 BiConsumer<? super T,? super Throwable> a) {
606 +                return f.whenComplete(a);
607 +            }
608              public <T,U> CompletableFuture<Void> runAfterBoth
609                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
610                  return f.runAfterBoth(g, a);
# Line 447 | Line 621 | public class CompletableFutureTest exten
621                   BiFunction<? super T,? super U,? extends V> a) {
622                  return f.thenCombine(g, a);
623              }
624 +            public <T> CompletableFuture<Void> runAfterEither
625 +                (CompletableFuture<T> f,
626 +                 CompletionStage<?> g,
627 +                 java.lang.Runnable a) {
628 +                return f.runAfterEither(g, a);
629 +            }
630 +            public <T> CompletableFuture<Void> acceptEither
631 +                (CompletableFuture<T> f,
632 +                 CompletionStage<? extends T> g,
633 +                 Consumer<? super T> a) {
634 +                return f.acceptEither(g, a);
635 +            }
636 +            public <T,U> CompletableFuture<U> applyToEither
637 +                (CompletableFuture<T> f,
638 +                 CompletionStage<? extends T> g,
639 +                 Function<? super T,U> a) {
640 +                return f.applyToEither(g, a);
641 +            }
642          },
643  
644 < //             /** Experimental way to do more testing */
645 < //         REVERSE_DEFAULT {
646 < //             public <T,U> CompletableFuture<Void> runAfterBoth
647 < //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
648 < //                 return g.runAfterBoth(f, a);
649 < //             }
650 < //             public <T,U> CompletableFuture<Void> thenAcceptBoth
651 < //                 (CompletableFuture<T> f,
652 < //                  CompletionStage<? extends U> g,
653 < //                  BiConsumer<? super T,? super U> a) {
654 < //                 return DEFAULT.thenAcceptBoth(f, g, a);
655 < //             }
656 < //         },
657 <
658 <        DEFAULT_ASYNC {
644 >        ASYNC {
645 >            public void checkExecutionMode() {
646 >                assertSame(ForkJoinPool.commonPool(),
647 >                           ForkJoinTask.getPool());
648 >            }
649 >            public CompletableFuture<Void> runAsync(Runnable a) {
650 >                return CompletableFuture.runAsync(a);
651 >            }
652 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
653 >                return CompletableFuture.supplyAsync(a);
654 >            }
655 >            public <T> CompletableFuture<Void> thenRun
656 >                (CompletableFuture<T> f, Runnable a) {
657 >                return f.thenRunAsync(a);
658 >            }
659 >            public <T> CompletableFuture<Void> thenAccept
660 >                (CompletableFuture<T> f, Consumer<? super T> a) {
661 >                return f.thenAcceptAsync(a);
662 >            }
663 >            public <T,U> CompletableFuture<U> thenApply
664 >                (CompletableFuture<T> f, Function<? super T,U> a) {
665 >                return f.thenApplyAsync(a);
666 >            }
667 >            public <T,U> CompletableFuture<U> thenCompose
668 >                (CompletableFuture<T> f,
669 >                 Function<? super T,? extends CompletionStage<U>> a) {
670 >                return f.thenComposeAsync(a);
671 >            }
672 >            public <T,U> CompletableFuture<U> handle
673 >                (CompletableFuture<T> f,
674 >                 BiFunction<? super T,Throwable,? extends U> a) {
675 >                return f.handleAsync(a);
676 >            }
677 >            public <T> CompletableFuture<T> whenComplete
678 >                (CompletableFuture<T> f,
679 >                 BiConsumer<? super T,? super Throwable> a) {
680 >                return f.whenCompleteAsync(a);
681 >            }
682              public <T,U> CompletableFuture<Void> runAfterBoth
683                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
684                  return f.runAfterBothAsync(g, a);
# Line 480 | Line 695 | public class CompletableFutureTest exten
695                   BiFunction<? super T,? super U,? extends V> a) {
696                  return f.thenCombineAsync(g, a);
697              }
698 +            public <T> CompletableFuture<Void> runAfterEither
699 +                (CompletableFuture<T> f,
700 +                 CompletionStage<?> g,
701 +                 java.lang.Runnable a) {
702 +                return f.runAfterEitherAsync(g, a);
703 +            }
704 +            public <T> CompletableFuture<Void> acceptEither
705 +                (CompletableFuture<T> f,
706 +                 CompletionStage<? extends T> g,
707 +                 Consumer<? super T> a) {
708 +                return f.acceptEitherAsync(g, a);
709 +            }
710 +            public <T,U> CompletableFuture<U> applyToEither
711 +                (CompletableFuture<T> f,
712 +                 CompletionStage<? extends T> g,
713 +                 Function<? super T,U> a) {
714 +                return f.applyToEitherAsync(g, a);
715 +            }
716          },
717  
485 //         REVERSE_DEFAULT_ASYNC {
486 //             public <T,U> CompletableFuture<Void> runAfterBoth
487 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
488 //                 return f.runAfterBothAsync(g, a);
489 //             }
490 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
491 //                 (CompletableFuture<T> f,
492 //                  CompletionStage<? extends U> g,
493 //                  BiConsumer<? super T,? super U> a) {
494 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
495 //             }
496 //         },
497
718          EXECUTOR {
719 +            public void checkExecutionMode() {
720 +                assertTrue(ThreadExecutor.startedCurrentThread());
721 +            }
722 +            public CompletableFuture<Void> runAsync(Runnable a) {
723 +                return CompletableFuture.runAsync(a, new ThreadExecutor());
724 +            }
725 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
726 +                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
727 +            }
728 +            public <T> CompletableFuture<Void> thenRun
729 +                (CompletableFuture<T> f, Runnable a) {
730 +                return f.thenRunAsync(a, new ThreadExecutor());
731 +            }
732 +            public <T> CompletableFuture<Void> thenAccept
733 +                (CompletableFuture<T> f, Consumer<? super T> a) {
734 +                return f.thenAcceptAsync(a, new ThreadExecutor());
735 +            }
736 +            public <T,U> CompletableFuture<U> thenApply
737 +                (CompletableFuture<T> f, Function<? super T,U> a) {
738 +                return f.thenApplyAsync(a, new ThreadExecutor());
739 +            }
740 +            public <T,U> CompletableFuture<U> thenCompose
741 +                (CompletableFuture<T> f,
742 +                 Function<? super T,? extends CompletionStage<U>> a) {
743 +                return f.thenComposeAsync(a, new ThreadExecutor());
744 +            }
745 +            public <T,U> CompletableFuture<U> handle
746 +                (CompletableFuture<T> f,
747 +                 BiFunction<? super T,Throwable,? extends U> a) {
748 +                return f.handleAsync(a, new ThreadExecutor());
749 +            }
750 +            public <T> CompletableFuture<T> whenComplete
751 +                (CompletableFuture<T> f,
752 +                 BiConsumer<? super T,? super Throwable> a) {
753 +                return f.whenCompleteAsync(a, new ThreadExecutor());
754 +            }
755              public <T,U> CompletableFuture<Void> runAfterBoth
756                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
757                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 512 | Line 768 | public class CompletableFutureTest exten
768                   BiFunction<? super T,? super U,? extends V> a) {
769                  return f.thenCombineAsync(g, a, new ThreadExecutor());
770              }
771 +            public <T> CompletableFuture<Void> runAfterEither
772 +                (CompletableFuture<T> f,
773 +                 CompletionStage<?> g,
774 +                 java.lang.Runnable a) {
775 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
776 +            }
777 +            public <T> CompletableFuture<Void> acceptEither
778 +                (CompletableFuture<T> f,
779 +                 CompletionStage<? extends T> g,
780 +                 Consumer<? super T> a) {
781 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
782 +            }
783 +            public <T,U> CompletableFuture<U> applyToEither
784 +                (CompletableFuture<T> f,
785 +                 CompletionStage<? extends T> g,
786 +                 Function<? super T,U> a) {
787 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
788 +            }
789          };
790  
791 +        public abstract void checkExecutionMode();
792 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
793 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
794 +        public abstract <T> CompletableFuture<Void> thenRun
795 +            (CompletableFuture<T> f, Runnable a);
796 +        public abstract <T> CompletableFuture<Void> thenAccept
797 +            (CompletableFuture<T> f, Consumer<? super T> a);
798 +        public abstract <T,U> CompletableFuture<U> thenApply
799 +            (CompletableFuture<T> f, Function<? super T,U> a);
800 +        public abstract <T,U> CompletableFuture<U> thenCompose
801 +            (CompletableFuture<T> f,
802 +             Function<? super T,? extends CompletionStage<U>> a);
803 +        public abstract <T,U> CompletableFuture<U> handle
804 +            (CompletableFuture<T> f,
805 +             BiFunction<? super T,Throwable,? extends U> a);
806 +        public abstract <T> CompletableFuture<T> whenComplete
807 +            (CompletableFuture<T> f,
808 +             BiConsumer<? super T,? super Throwable> a);
809          public abstract <T,U> CompletableFuture<Void> runAfterBoth
810              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
811          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 524 | Line 816 | public class CompletableFutureTest exten
816              (CompletableFuture<T> f,
817               CompletionStage<? extends U> g,
818               BiFunction<? super T,? super U,? extends V> a);
819 +        public abstract <T> CompletableFuture<Void> runAfterEither
820 +            (CompletableFuture<T> f,
821 +             CompletionStage<?> g,
822 +             java.lang.Runnable a);
823 +        public abstract <T> CompletableFuture<Void> acceptEither
824 +            (CompletableFuture<T> f,
825 +             CompletionStage<? extends T> g,
826 +             Consumer<? super T> a);
827 +        public abstract <T,U> CompletableFuture<U> applyToEither
828 +            (CompletableFuture<T> f,
829 +             CompletionStage<? extends T> g,
830 +             Function<? super T,U> a);
831      }
832  
833      /**
834 <     * exceptionally action completes with function value on source
835 <     * exception; otherwise with source value
834 >     * exceptionally action is not invoked when source completes
835 >     * normally, and source result is propagated
836       */
837 <    public void testExceptionally() {
838 <        CompletableFuture<Integer> f = new CompletableFuture<>();
839 <        ExceptionToInteger r = new ExceptionToInteger();
840 <        CompletableFuture<Integer> g = f.exceptionally(r);
841 <        f.completeExceptionally(new CFException());
842 <        checkCompletedNormally(g, three);
837 >    public void testExceptionally_normalCompletion() {
838 >        for (boolean createIncomplete : new boolean[] { true, false })
839 >        for (Integer v1 : new Integer[] { 1, null })
840 >    {
841 >        final AtomicInteger a = new AtomicInteger(0);
842 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
843 >        if (!createIncomplete) f.complete(v1);
844 >        final CompletableFuture<Integer> g = f.exceptionally
845 >            ((Throwable t) -> {
846 >                // Should not be called
847 >                a.getAndIncrement();
848 >                throw new AssertionError();
849 >            });
850 >        if (createIncomplete) f.complete(v1);
851  
852 <        f = new CompletableFuture<>();
853 <        r = new ExceptionToInteger();
854 <        g = f.exceptionally(r);
855 <        f.complete(one);
544 <        checkCompletedNormally(g, one);
545 <    }
852 >        checkCompletedNormally(g, v1);
853 >        checkCompletedNormally(f, v1);
854 >        assertEquals(0, a.get());
855 >    }}
856  
857      /**
858 <     * handle action completes normally with function value on either
859 <     * normal or exceptional completion of source
858 >     * exceptionally action completes with function value on source
859 >     * exception
860       */
861 <    public void testHandle() {
862 <        CompletableFuture<Integer> f, g;
863 <        IntegerHandler r;
864 <
865 <        f = new CompletableFuture<>();
866 <        f.completeExceptionally(new CFException());
867 <        g = f.handle(r = new IntegerHandler());
868 <        assertTrue(r.ran);
869 <        checkCompletedNormally(g, three);
870 <
871 <        f = new CompletableFuture<>();
872 <        g = f.handle(r = new IntegerHandler());
873 <        assertFalse(r.ran);
874 <        f.completeExceptionally(new CFException());
875 <        checkCompletedNormally(g, three);
876 <        assertTrue(r.ran);
861 >    public void testExceptionally_exceptionalCompletion() {
862 >        for (boolean createIncomplete : new boolean[] { true, false })
863 >        for (Integer v1 : new Integer[] { 1, null })
864 >    {
865 >        final AtomicInteger a = new AtomicInteger(0);
866 >        final CFException ex = new CFException();
867 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
868 >        if (!createIncomplete) f.completeExceptionally(ex);
869 >        final CompletableFuture<Integer> g = f.exceptionally
870 >            ((Throwable t) -> {
871 >                ExecutionMode.DEFAULT.checkExecutionMode();
872 >                threadAssertSame(t, ex);
873 >                a.getAndIncrement();
874 >                return v1;
875 >            });
876 >        if (createIncomplete) f.completeExceptionally(ex);
877  
878 <        f = new CompletableFuture<>();
879 <        f.complete(one);
880 <        g = f.handle(r = new IntegerHandler());
571 <        assertTrue(r.ran);
572 <        checkCompletedNormally(g, two);
878 >        checkCompletedNormally(g, v1);
879 >        assertEquals(1, a.get());
880 >    }}
881  
882 <        f = new CompletableFuture<>();
883 <        g = f.handle(r = new IntegerHandler());
884 <        assertFalse(r.ran);
885 <        f.complete(one);
886 <        assertTrue(r.ran);
887 <        checkCompletedNormally(g, two);
888 <    }
882 >    public void testExceptionally_exceptionalCompletionActionFailed() {
883 >        for (boolean createIncomplete : new boolean[] { true, false })
884 >        for (Integer v1 : new Integer[] { 1, null })
885 >    {
886 >        final AtomicInteger a = new AtomicInteger(0);
887 >        final CFException ex1 = new CFException();
888 >        final CFException ex2 = new CFException();
889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
890 >        if (!createIncomplete) f.completeExceptionally(ex1);
891 >        final CompletableFuture<Integer> g = f.exceptionally
892 >            ((Throwable t) -> {
893 >                ExecutionMode.DEFAULT.checkExecutionMode();
894 >                threadAssertSame(t, ex1);
895 >                a.getAndIncrement();
896 >                throw ex2;
897 >            });
898 >        if (createIncomplete) f.completeExceptionally(ex1);
899 >
900 >        checkCompletedWithWrappedException(g, ex2);
901 >        assertEquals(1, a.get());
902 >    }}
903  
904      /**
905 <     * runAsync completes after running Runnable
905 >     * whenComplete action executes on normal completion, propagating
906 >     * source result.
907       */
908 <    public void testRunAsync() {
909 <        Noop r = new Noop();
910 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
911 <        assertNull(f.join());
912 <        assertTrue(r.ran);
913 <        checkCompletedNormally(f, null);
914 <    }
908 >    public void testWhenComplete_normalCompletion1() {
909 >        for (ExecutionMode m : ExecutionMode.values())
910 >        for (boolean createIncomplete : new boolean[] { true, false })
911 >        for (Integer v1 : new Integer[] { 1, null })
912 >    {
913 >        final AtomicInteger a = new AtomicInteger(0);
914 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
915 >        if (!createIncomplete) f.complete(v1);
916 >        final CompletableFuture<Integer> g = m.whenComplete
917 >            (f,
918 >             (Integer x, Throwable t) -> {
919 >                m.checkExecutionMode();
920 >                threadAssertSame(x, v1);
921 >                threadAssertNull(t);
922 >                a.getAndIncrement();
923 >            });
924 >        if (createIncomplete) f.complete(v1);
925  
926 <    /**
927 <     * runAsync with executor completes after running Runnable
928 <     */
929 <    public void testRunAsync2() {
597 <        Noop r = new Noop();
598 <        ThreadExecutor exec = new ThreadExecutor();
599 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
600 <        assertNull(f.join());
601 <        assertTrue(r.ran);
602 <        checkCompletedNormally(f, null);
603 <        assertEquals(1, exec.count.get());
604 <    }
926 >        checkCompletedNormally(g, v1);
927 >        checkCompletedNormally(f, v1);
928 >        assertEquals(1, a.get());
929 >    }}
930  
931      /**
932 <     * failing runAsync completes exceptionally after running Runnable
932 >     * whenComplete action executes on exceptional completion, propagating
933 >     * source result.
934       */
935 <    public void testRunAsync3() {
936 <        FailingNoop r = new FailingNoop();
937 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
938 <        checkCompletedWithWrappedCFException(f);
939 <        assertTrue(r.ran);
940 <    }
935 >    public void testWhenComplete_exceptionalCompletion() {
936 >        for (ExecutionMode m : ExecutionMode.values())
937 >        for (boolean createIncomplete : new boolean[] { true, false })
938 >        for (Integer v1 : new Integer[] { 1, null })
939 >    {
940 >        final AtomicInteger a = new AtomicInteger(0);
941 >        final CFException ex = new CFException();
942 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
943 >        if (!createIncomplete) f.completeExceptionally(ex);
944 >        final CompletableFuture<Integer> g = m.whenComplete
945 >            (f,
946 >             (Integer x, Throwable t) -> {
947 >                m.checkExecutionMode();
948 >                threadAssertNull(x);
949 >                threadAssertSame(t, ex);
950 >                a.getAndIncrement();
951 >            });
952 >        if (createIncomplete) f.completeExceptionally(ex);
953 >
954 >        checkCompletedWithWrappedException(g, ex);
955 >        checkCompletedExceptionally(f, ex);
956 >        assertEquals(1, a.get());
957 >    }}
958  
959      /**
960 <     * supplyAsync completes with result of supplier
960 >     * whenComplete action executes on cancelled source, propagating
961 >     * CancellationException.
962       */
963 <    public void testSupplyAsync() {
964 <        CompletableFuture<Integer> f;
965 <        f = CompletableFuture.supplyAsync(supplyOne);
966 <        assertEquals(f.join(), one);
967 <        checkCompletedNormally(f, one);
968 <    }
963 >    public void testWhenComplete_sourceCancelled() {
964 >        for (ExecutionMode m : ExecutionMode.values())
965 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
966 >        for (boolean createIncomplete : new boolean[] { true, false })
967 >    {
968 >        final AtomicInteger a = new AtomicInteger(0);
969 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
970 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971 >        final CompletableFuture<Integer> g = m.whenComplete
972 >            (f,
973 >             (Integer x, Throwable t) -> {
974 >                m.checkExecutionMode();
975 >                threadAssertNull(x);
976 >                threadAssertTrue(t instanceof CancellationException);
977 >                a.getAndIncrement();
978 >            });
979 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
980  
981 <    /**
982 <     * supplyAsync with executor completes with result of supplier
983 <     */
984 <    public void testSupplyAsync2() {
630 <        CompletableFuture<Integer> f;
631 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
632 <        assertEquals(f.join(), one);
633 <        checkCompletedNormally(f, one);
634 <    }
981 >        checkCompletedWithWrappedCancellationException(g);
982 >        checkCancelled(f);
983 >        assertEquals(1, a.get());
984 >    }}
985  
986      /**
987 <     * Failing supplyAsync completes exceptionally
987 >     * If a whenComplete action throws an exception when triggered by
988 >     * a normal completion, it completes exceptionally
989       */
990 <    public void testSupplyAsync3() {
991 <        FailingSupplier r = new FailingSupplier();
992 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
993 <        checkCompletedWithWrappedCFException(f);
994 <        assertTrue(r.ran);
995 <    }
990 >    public void testWhenComplete_actionFailed() {
991 >        for (boolean createIncomplete : new boolean[] { true, false })
992 >        for (ExecutionMode m : ExecutionMode.values())
993 >        for (Integer v1 : new Integer[] { 1, null })
994 >    {
995 >        final AtomicInteger a = new AtomicInteger(0);
996 >        final CFException ex = new CFException();
997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
998 >        if (!createIncomplete) f.complete(v1);
999 >        final CompletableFuture<Integer> g = m.whenComplete
1000 >            (f,
1001 >             (Integer x, Throwable t) -> {
1002 >                m.checkExecutionMode();
1003 >                threadAssertSame(x, v1);
1004 >                threadAssertNull(t);
1005 >                a.getAndIncrement();
1006 >                throw ex;
1007 >            });
1008 >        if (createIncomplete) f.complete(v1);
1009  
1010 <    // seq completion methods
1010 >        checkCompletedWithWrappedException(g, ex);
1011 >        checkCompletedNormally(f, v1);
1012 >        assertEquals(1, a.get());
1013 >    }}
1014  
1015      /**
1016 <     * thenRun result completes normally after normal completion of source
1016 >     * If a whenComplete action throws an exception when triggered by
1017 >     * a source completion that also throws an exception, the source
1018 >     * exception takes precedence.
1019       */
1020 <    public void testThenRun() {
1021 <        CompletableFuture<Integer> f;
1022 <        CompletableFuture<Void> g;
1023 <        Noop r;
1024 <
1025 <        f = new CompletableFuture<>();
1026 <        g = f.thenRun(r = new Noop());
1027 <        f.complete(null);
1028 <        checkCompletedNormally(g, null);
1029 <        assertTrue(r.ran);
1030 <
1031 <        f = new CompletableFuture<>();
1032 <        f.complete(null);
1033 <        g = f.thenRun(r = new Noop());
1034 <        checkCompletedNormally(g, null);
1035 <        assertTrue(r.ran);
1036 <    }
1020 >    public void testWhenComplete_actionFailedSourceFailed() {
1021 >        for (boolean createIncomplete : new boolean[] { true, false })
1022 >        for (ExecutionMode m : ExecutionMode.values())
1023 >        for (Integer v1 : new Integer[] { 1, null })
1024 >    {
1025 >        final AtomicInteger a = new AtomicInteger(0);
1026 >        final CFException ex1 = new CFException();
1027 >        final CFException ex2 = new CFException();
1028 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1029 >
1030 >        if (!createIncomplete) f.completeExceptionally(ex1);
1031 >        final CompletableFuture<Integer> g = m.whenComplete
1032 >            (f,
1033 >             (Integer x, Throwable t) -> {
1034 >                m.checkExecutionMode();
1035 >                threadAssertSame(t, ex1);
1036 >                threadAssertNull(x);
1037 >                a.getAndIncrement();
1038 >                throw ex2;
1039 >            });
1040 >        if (createIncomplete) f.completeExceptionally(ex1);
1041 >
1042 >        checkCompletedWithWrappedException(g, ex1);
1043 >        checkCompletedExceptionally(f, ex1);
1044 >        assertEquals(1, a.get());
1045 >    }}
1046  
1047      /**
1048 <     * thenRun result completes exceptionally after exceptional
1048 >     * handle action completes normally with function value on normal
1049       * completion of source
1050       */
1051 <    public void testThenRun2() {
1052 <        CompletableFuture<Integer> f;
1053 <        CompletableFuture<Void> g;
1054 <        Noop r;
1055 <
1056 <        f = new CompletableFuture<>();
1057 <        g = f.thenRun(r = new Noop());
1058 <        f.completeExceptionally(new CFException());
1059 <        checkCompletedWithWrappedCFException(g);
1060 <        assertFalse(r.ran);
1051 >    public void testHandle_normalCompletion() {
1052 >        for (ExecutionMode m : ExecutionMode.values())
1053 >        for (boolean createIncomplete : new boolean[] { true, false })
1054 >        for (Integer v1 : new Integer[] { 1, null })
1055 >    {
1056 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1057 >        final AtomicInteger a = new AtomicInteger(0);
1058 >        if (!createIncomplete) f.complete(v1);
1059 >        final CompletableFuture<Integer> g = m.handle
1060 >            (f,
1061 >             (Integer x, Throwable t) -> {
1062 >                m.checkExecutionMode();
1063 >                threadAssertSame(x, v1);
1064 >                threadAssertNull(t);
1065 >                a.getAndIncrement();
1066 >                return inc(v1);
1067 >            });
1068 >        if (createIncomplete) f.complete(v1);
1069  
1070 <        f = new CompletableFuture<>();
1071 <        f.completeExceptionally(new CFException());
1072 <        g = f.thenRun(r = new Noop());
1073 <        checkCompletedWithWrappedCFException(g);
688 <        assertFalse(r.ran);
689 <    }
1070 >        checkCompletedNormally(g, inc(v1));
1071 >        checkCompletedNormally(f, v1);
1072 >        assertEquals(1, a.get());
1073 >    }}
1074  
1075      /**
1076 <     * thenRun result completes exceptionally if action does
1076 >     * handle action completes normally with function value on
1077 >     * exceptional completion of source
1078       */
1079 <    public void testThenRun3() {
1080 <        CompletableFuture<Integer> f;
1081 <        CompletableFuture<Void> g;
1082 <        FailingNoop r;
1079 >    public void testHandle_exceptionalCompletion() {
1080 >        for (ExecutionMode m : ExecutionMode.values())
1081 >        for (boolean createIncomplete : new boolean[] { true, false })
1082 >        for (Integer v1 : new Integer[] { 1, null })
1083 >    {
1084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1085 >        final AtomicInteger a = new AtomicInteger(0);
1086 >        final CFException ex = new CFException();
1087 >        if (!createIncomplete) f.completeExceptionally(ex);
1088 >        final CompletableFuture<Integer> g = m.handle
1089 >            (f,
1090 >             (Integer x, Throwable t) -> {
1091 >                m.checkExecutionMode();
1092 >                threadAssertNull(x);
1093 >                threadAssertSame(t, ex);
1094 >                a.getAndIncrement();
1095 >                return v1;
1096 >            });
1097 >        if (createIncomplete) f.completeExceptionally(ex);
1098  
1099 <        f = new CompletableFuture<>();
1100 <        g = f.thenRun(r = new FailingNoop());
1101 <        f.complete(null);
1102 <        checkCompletedWithWrappedCFException(g);
1099 >        checkCompletedNormally(g, v1);
1100 >        checkCompletedExceptionally(f, ex);
1101 >        assertEquals(1, a.get());
1102 >    }}
1103  
1104 <        f = new CompletableFuture<>();
1105 <        f.complete(null);
1106 <        g = f.thenRun(r = new FailingNoop());
1107 <        checkCompletedWithWrappedCFException(g);
1108 <    }
1104 >    /**
1105 >     * handle action completes normally with function value on
1106 >     * cancelled source
1107 >     */
1108 >    public void testHandle_sourceCancelled() {
1109 >        for (ExecutionMode m : ExecutionMode.values())
1110 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1111 >        for (boolean createIncomplete : new boolean[] { true, false })
1112 >        for (Integer v1 : new Integer[] { 1, null })
1113 >    {
1114 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1115 >        final AtomicInteger a = new AtomicInteger(0);
1116 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1117 >        final CompletableFuture<Integer> g = m.handle
1118 >            (f,
1119 >             (Integer x, Throwable t) -> {
1120 >                m.checkExecutionMode();
1121 >                threadAssertNull(x);
1122 >                threadAssertTrue(t instanceof CancellationException);
1123 >                a.getAndIncrement();
1124 >                return v1;
1125 >            });
1126 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1127 >
1128 >        checkCompletedNormally(g, v1);
1129 >        checkCancelled(f);
1130 >        assertEquals(1, a.get());
1131 >    }}
1132  
1133      /**
1134 <     * thenRun result completes exceptionally if source cancelled
1134 >     * handle result completes exceptionally if action does
1135       */
1136 <    public void testThenRun4() {
1137 <        CompletableFuture<Integer> f;
1138 <        CompletableFuture<Void> g;
1139 <        Noop r;
1136 >    public void testHandle_sourceFailedActionFailed() {
1137 >        for (ExecutionMode m : ExecutionMode.values())
1138 >        for (boolean createIncomplete : new boolean[] { true, false })
1139 >    {
1140 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1141 >        final AtomicInteger a = new AtomicInteger(0);
1142 >        final CFException ex1 = new CFException();
1143 >        final CFException ex2 = new CFException();
1144 >        if (!createIncomplete) f.completeExceptionally(ex1);
1145 >        final CompletableFuture<Integer> g = m.handle
1146 >            (f,
1147 >             (Integer x, Throwable t) -> {
1148 >                m.checkExecutionMode();
1149 >                threadAssertNull(x);
1150 >                threadAssertSame(ex1, t);
1151 >                a.getAndIncrement();
1152 >                throw ex2;
1153 >            });
1154 >        if (createIncomplete) f.completeExceptionally(ex1);
1155 >
1156 >        checkCompletedWithWrappedException(g, ex2);
1157 >        checkCompletedExceptionally(f, ex1);
1158 >        assertEquals(1, a.get());
1159 >    }}
1160  
1161 <        f = new CompletableFuture<>();
1162 <        g = f.thenRun(r = new Noop());
1163 <        assertTrue(f.cancel(true));
1164 <        checkCompletedWithWrappedCancellationException(g);
1161 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1162 >        for (ExecutionMode m : ExecutionMode.values())
1163 >        for (boolean createIncomplete : new boolean[] { true, false })
1164 >        for (Integer v1 : new Integer[] { 1, null })
1165 >    {
1166 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1167 >        final AtomicInteger a = new AtomicInteger(0);
1168 >        final CFException ex = new CFException();
1169 >        if (!createIncomplete) f.complete(v1);
1170 >        final CompletableFuture<Integer> g = m.handle
1171 >            (f,
1172 >             (Integer x, Throwable t) -> {
1173 >                m.checkExecutionMode();
1174 >                threadAssertSame(x, v1);
1175 >                threadAssertNull(t);
1176 >                a.getAndIncrement();
1177 >                throw ex;
1178 >            });
1179 >        if (createIncomplete) f.complete(v1);
1180  
1181 <        f = new CompletableFuture<>();
1182 <        assertTrue(f.cancel(true));
1183 <        g = f.thenRun(r = new Noop());
1184 <        checkCompletedWithWrappedCancellationException(g);
727 <    }
1181 >        checkCompletedWithWrappedException(g, ex);
1182 >        checkCompletedNormally(f, v1);
1183 >        assertEquals(1, a.get());
1184 >    }}
1185  
1186      /**
1187 <     * thenApply result completes normally after normal completion of source
1187 >     * runAsync completes after running Runnable
1188       */
1189 <    public void testThenApply() {
1190 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1191 <        CompletableFuture<Integer> g = f.thenApply(inc);
1192 <        f.complete(one);
1193 <        checkCompletedNormally(g, two);
1194 <    }
1189 >    public void testRunAsync_normalCompletion() {
1190 >        ExecutionMode[] executionModes = {
1191 >            ExecutionMode.ASYNC,
1192 >            ExecutionMode.EXECUTOR,
1193 >        };
1194 >        for (ExecutionMode m : executionModes)
1195 >    {
1196 >        final Noop r = new Noop(m);
1197 >        final CompletableFuture<Void> f = m.runAsync(r);
1198 >        assertNull(f.join());
1199 >        checkCompletedNormally(f, null);
1200 >        r.assertInvoked();
1201 >    }}
1202  
1203      /**
1204 <     * thenApply result completes exceptionally after exceptional
741 <     * completion of source
1204 >     * failing runAsync completes exceptionally after running Runnable
1205       */
1206 <    public void testThenApply2() {
1207 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1208 <        CompletableFuture<Integer> g = f.thenApply(inc);
1209 <        f.completeExceptionally(new CFException());
1210 <        checkCompletedWithWrappedCFException(g);
1211 <    }
1206 >    public void testRunAsync_exceptionalCompletion() {
1207 >        ExecutionMode[] executionModes = {
1208 >            ExecutionMode.ASYNC,
1209 >            ExecutionMode.EXECUTOR,
1210 >        };
1211 >        for (ExecutionMode m : executionModes)
1212 >    {
1213 >        final FailingRunnable r = new FailingRunnable(m);
1214 >        final CompletableFuture<Void> f = m.runAsync(r);
1215 >        checkCompletedWithWrappedCFException(f);
1216 >        r.assertInvoked();
1217 >    }}
1218  
1219      /**
1220 <     * thenApply result completes exceptionally if action does
1220 >     * supplyAsync completes with result of supplier
1221       */
1222 <    public void testThenApply3() {
1223 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1224 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1225 <        f.complete(one);
1226 <        checkCompletedWithWrappedCFException(g);
1227 <    }
1222 >    public void testSupplyAsync_normalCompletion() {
1223 >        ExecutionMode[] executionModes = {
1224 >            ExecutionMode.ASYNC,
1225 >            ExecutionMode.EXECUTOR,
1226 >        };
1227 >        for (ExecutionMode m : executionModes)
1228 >        for (Integer v1 : new Integer[] { 1, null })
1229 >    {
1230 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1231 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1232 >        assertSame(v1, f.join());
1233 >        checkCompletedNormally(f, v1);
1234 >        r.assertInvoked();
1235 >    }}
1236  
1237      /**
1238 <     * thenApply result completes exceptionally if source cancelled
1238 >     * Failing supplyAsync completes exceptionally
1239       */
1240 <    public void testThenApply4() {
1241 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1242 <        CompletableFuture<Integer> g = f.thenApply(inc);
1243 <        assertTrue(f.cancel(true));
1244 <        checkCompletedWithWrappedCancellationException(g);
1245 <    }
1240 >    public void testSupplyAsync_exceptionalCompletion() {
1241 >        ExecutionMode[] executionModes = {
1242 >            ExecutionMode.ASYNC,
1243 >            ExecutionMode.EXECUTOR,
1244 >        };
1245 >        for (ExecutionMode m : executionModes)
1246 >    {
1247 >        FailingSupplier r = new FailingSupplier(m);
1248 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1249 >        checkCompletedWithWrappedCFException(f);
1250 >        r.assertInvoked();
1251 >    }}
1252 >
1253 >    // seq completion methods
1254  
1255      /**
1256 <     * thenAccept result completes normally after normal completion of source
1256 >     * thenRun result completes normally after normal completion of source
1257       */
1258 <    public void testThenAccept() {
1259 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1260 <        IncAction r = new IncAction();
1261 <        CompletableFuture<Void> g = f.thenAccept(r);
1262 <        f.complete(one);
1258 >    public void testThenRun_normalCompletion() {
1259 >        for (ExecutionMode m : ExecutionMode.values())
1260 >        for (boolean createIncomplete : new boolean[] { true, false })
1261 >        for (Integer v1 : new Integer[] { 1, null })
1262 >    {
1263 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1264 >        final Noop r = new Noop(m);
1265 >        if (!createIncomplete) f.complete(v1);
1266 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1267 >        if (createIncomplete) {
1268 >            checkIncomplete(g);
1269 >            f.complete(v1);
1270 >        }
1271 >
1272          checkCompletedNormally(g, null);
1273 <        assertEquals(r.value, 2);
1274 <    }
1273 >        checkCompletedNormally(f, v1);
1274 >        r.assertInvoked();
1275 >    }}
1276  
1277      /**
1278 <     * thenAccept result completes exceptionally after exceptional
1278 >     * thenRun result completes exceptionally after exceptional
1279       * completion of source
1280       */
1281 <    public void testThenAccept2() {
1282 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1283 <        IncAction r = new IncAction();
1284 <        CompletableFuture<Void> g = f.thenAccept(r);
1285 <        f.completeExceptionally(new CFException());
1286 <        checkCompletedWithWrappedCFException(g);
1287 <    }
1281 >    public void testThenRun_exceptionalCompletion() {
1282 >        for (ExecutionMode m : ExecutionMode.values())
1283 >        for (boolean createIncomplete : new boolean[] { true, false })
1284 >    {
1285 >        final CFException ex = new CFException();
1286 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1287 >        final Noop r = new Noop(m);
1288 >        if (!createIncomplete) f.completeExceptionally(ex);
1289 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1290 >        if (createIncomplete) {
1291 >            checkIncomplete(g);
1292 >            f.completeExceptionally(ex);
1293 >        }
1294  
1295 <    /**
1296 <     * thenAccept result completes exceptionally if action does
1297 <     */
1298 <    public void testThenAccept3() {
798 <        CompletableFuture<Integer> f = new CompletableFuture<>();
799 <        FailingConsumer r = new FailingConsumer();
800 <        CompletableFuture<Void> g = f.thenAccept(r);
801 <        f.complete(one);
802 <        checkCompletedWithWrappedCFException(g);
803 <        assertTrue(r.ran);
804 <    }
1295 >        checkCompletedWithWrappedException(g, ex);
1296 >        checkCompletedExceptionally(f, ex);
1297 >        r.assertNotInvoked();
1298 >    }}
1299  
1300      /**
1301 <     * thenAccept result completes exceptionally if source cancelled
1301 >     * thenRun result completes exceptionally if source cancelled
1302       */
1303 <    public void testThenAccept4() {
1304 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1305 <        IncAction r = new IncAction();
1306 <        CompletableFuture<Void> g = f.thenAccept(r);
1307 <        assertTrue(f.cancel(true));
1303 >    public void testThenRun_sourceCancelled() {
1304 >        for (ExecutionMode m : ExecutionMode.values())
1305 >        for (boolean createIncomplete : new boolean[] { true, false })
1306 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1307 >    {
1308 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1309 >        final Noop r = new Noop(m);
1310 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1311 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1312 >        if (createIncomplete) {
1313 >            checkIncomplete(g);
1314 >            assertTrue(f.cancel(mayInterruptIfRunning));
1315 >        }
1316 >
1317          checkCompletedWithWrappedCancellationException(g);
1318 <    }
1318 >        checkCancelled(f);
1319 >        r.assertNotInvoked();
1320 >    }}
1321  
1322      /**
1323 <     * thenCombine result completes normally after normal completion
819 <     * of sources
1323 >     * thenRun result completes exceptionally if action does
1324       */
1325 <    public void testThenCombine_normalCompletion1() {
1325 >    public void testThenRun_actionFailed() {
1326          for (ExecutionMode m : ExecutionMode.values())
1327 +        for (boolean createIncomplete : new boolean[] { true, false })
1328          for (Integer v1 : new Integer[] { 1, null })
1329 <        for (Integer v2 : new Integer[] { 2, null }) {
825 <
1329 >    {
1330          final CompletableFuture<Integer> f = new CompletableFuture<>();
1331 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1332 <        final SubtractFunction r = new SubtractFunction();
1333 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1334 <
1335 <        f.complete(v1);
1336 <        checkIncomplete(h);
1337 <        assertFalse(r.ran());
834 <        g.complete(v2);
1331 >        final FailingRunnable r = new FailingRunnable(m);
1332 >        if (!createIncomplete) f.complete(v1);
1333 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1334 >        if (createIncomplete) {
1335 >            checkIncomplete(g);
1336 >            f.complete(v1);
1337 >        }
1338  
1339 <        checkCompletedNormally(h, subtract(v1, v2));
1339 >        checkCompletedWithWrappedCFException(g);
1340          checkCompletedNormally(f, v1);
1341 <        checkCompletedNormally(g, v2);
839 <        }
840 <    }
1341 >    }}
1342  
1343 <    public void testThenCombine_normalCompletion2() {
1343 >    /**
1344 >     * thenApply result completes normally after normal completion of source
1345 >     */
1346 >    public void testThenApply_normalCompletion() {
1347          for (ExecutionMode m : ExecutionMode.values())
1348 +        for (boolean createIncomplete : new boolean[] { true, false })
1349          for (Integer v1 : new Integer[] { 1, null })
1350 <        for (Integer v2 : new Integer[] { 2, null }) {
846 <
1350 >    {
1351          final CompletableFuture<Integer> f = new CompletableFuture<>();
1352 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1353 <        final SubtractFunction r = new SubtractFunction();
1354 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1355 <
1356 <        g.complete(v2);
1357 <        checkIncomplete(h);
1358 <        assertFalse(r.ran());
855 <        f.complete(v1);
1352 >        final IncFunction r = new IncFunction(m);
1353 >        if (!createIncomplete) f.complete(v1);
1354 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1355 >        if (createIncomplete) {
1356 >            checkIncomplete(g);
1357 >            f.complete(v1);
1358 >        }
1359  
1360 <        checkCompletedNormally(h, subtract(v1, v2));
1360 >        checkCompletedNormally(g, inc(v1));
1361          checkCompletedNormally(f, v1);
1362 <        checkCompletedNormally(g, v2);
1363 <        }
861 <    }
1362 >        r.assertValue(inc(v1));
1363 >    }}
1364  
1365 <    public void testThenCombine_normalCompletion3() {
1365 >    /**
1366 >     * thenApply result completes exceptionally after exceptional
1367 >     * completion of source
1368 >     */
1369 >    public void testThenApply_exceptionalCompletion() {
1370          for (ExecutionMode m : ExecutionMode.values())
1371 <        for (Integer v1 : new Integer[] { 1, null })
1372 <        for (Integer v2 : new Integer[] { 2, null }) {
1373 <
1371 >        for (boolean createIncomplete : new boolean[] { true, false })
1372 >    {
1373 >        final CFException ex = new CFException();
1374          final CompletableFuture<Integer> f = new CompletableFuture<>();
1375 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1376 <        final SubtractFunction r = new SubtractFunction();
1375 >        final IncFunction r = new IncFunction(m);
1376 >        if (!createIncomplete) f.completeExceptionally(ex);
1377 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1378 >        if (createIncomplete) {
1379 >            checkIncomplete(g);
1380 >            f.completeExceptionally(ex);
1381 >        }
1382  
1383 <        g.complete(v2);
1384 <        f.complete(v1);
1385 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1383 >        checkCompletedWithWrappedException(g, ex);
1384 >        checkCompletedExceptionally(f, ex);
1385 >        r.assertNotInvoked();
1386 >    }}
1387  
1388 <        checkCompletedNormally(h, subtract(v1, v2));
1389 <        checkCompletedNormally(f, v1);
1390 <        checkCompletedNormally(g, v2);
1388 >    /**
1389 >     * thenApply result completes exceptionally if source cancelled
1390 >     */
1391 >    public void testThenApply_sourceCancelled() {
1392 >        for (ExecutionMode m : ExecutionMode.values())
1393 >        for (boolean createIncomplete : new boolean[] { true, false })
1394 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1395 >    {
1396 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1397 >        final IncFunction r = new IncFunction(m);
1398 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1399 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1400 >        if (createIncomplete) {
1401 >            checkIncomplete(g);
1402 >            assertTrue(f.cancel(mayInterruptIfRunning));
1403          }
880    }
1404  
1405 <    public void testThenCombine_normalCompletion4() {
1405 >        checkCompletedWithWrappedCancellationException(g);
1406 >        checkCancelled(f);
1407 >        r.assertNotInvoked();
1408 >    }}
1409 >
1410 >    /**
1411 >     * thenApply result completes exceptionally if action does
1412 >     */
1413 >    public void testThenApply_actionFailed() {
1414          for (ExecutionMode m : ExecutionMode.values())
1415 +        for (boolean createIncomplete : new boolean[] { true, false })
1416          for (Integer v1 : new Integer[] { 1, null })
1417 <        for (Integer v2 : new Integer[] { 2, null }) {
886 <
1417 >    {
1418          final CompletableFuture<Integer> f = new CompletableFuture<>();
1419 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1420 <        final SubtractFunction r = new SubtractFunction();
1421 <
1422 <        f.complete(v1);
1423 <        g.complete(v2);
1424 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1419 >        final FailingFunction r = new FailingFunction(m);
1420 >        if (!createIncomplete) f.complete(v1);
1421 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1422 >        if (createIncomplete) {
1423 >            checkIncomplete(g);
1424 >            f.complete(v1);
1425 >        }
1426  
1427 <        checkCompletedNormally(h, subtract(v1, v2));
1427 >        checkCompletedWithWrappedCFException(g);
1428          checkCompletedNormally(f, v1);
1429 <        checkCompletedNormally(g, v2);
898 <        }
899 <    }
1429 >    }}
1430  
1431      /**
1432 <     * thenCombine result completes exceptionally after exceptional
903 <     * completion of either source
1432 >     * thenAccept result completes normally after normal completion of source
1433       */
1434 <    public void testThenCombine_exceptionalCompletion1() {
1434 >    public void testThenAccept_normalCompletion() {
1435          for (ExecutionMode m : ExecutionMode.values())
1436 <        for (Integer v1 : new Integer[] { 1, null }) {
1437 <
1436 >        for (boolean createIncomplete : new boolean[] { true, false })
1437 >        for (Integer v1 : new Integer[] { 1, null })
1438 >    {
1439          final CompletableFuture<Integer> f = new CompletableFuture<>();
1440 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1441 <        final SubtractFunction r = new SubtractFunction();
1442 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1443 <        final CFException ex = new CFException();
1444 <
1445 <        f.completeExceptionally(ex);
916 <        checkIncomplete(h);
917 <        g.complete(v1);
918 <
919 <        checkCompletedWithWrappedCFException(h, ex);
920 <        checkCompletedWithWrappedCFException(f, ex);
921 <        assertFalse(r.ran());
922 <        checkCompletedNormally(g, v1);
1440 >        final NoopConsumer r = new NoopConsumer(m);
1441 >        if (!createIncomplete) f.complete(v1);
1442 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1443 >        if (createIncomplete) {
1444 >            checkIncomplete(g);
1445 >            f.complete(v1);
1446          }
924    }
925
926    public void testThenCombine_exceptionalCompletion2() {
927        for (ExecutionMode m : ExecutionMode.values())
928        for (Integer v1 : new Integer[] { 1, null }) {
929
930        final CompletableFuture<Integer> f = new CompletableFuture<>();
931        final CompletableFuture<Integer> g = new CompletableFuture<>();
932        final SubtractFunction r = new SubtractFunction();
933        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
934        final CFException ex = new CFException();
1447  
1448 <        g.completeExceptionally(ex);
1449 <        checkIncomplete(h);
938 <        f.complete(v1);
939 <
940 <        checkCompletedWithWrappedCFException(h, ex);
941 <        checkCompletedWithWrappedCFException(g, ex);
942 <        assertFalse(r.ran());
1448 >        checkCompletedNormally(g, null);
1449 >        r.assertValue(v1);
1450          checkCompletedNormally(f, v1);
1451 <        }
945 <    }
1451 >    }}
1452  
1453 <    public void testThenCombine_exceptionalCompletion3() {
1453 >    /**
1454 >     * thenAccept result completes exceptionally after exceptional
1455 >     * completion of source
1456 >     */
1457 >    public void testThenAccept_exceptionalCompletion() {
1458          for (ExecutionMode m : ExecutionMode.values())
1459 <        for (Integer v1 : new Integer[] { 1, null }) {
1460 <
951 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
952 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
953 <        final SubtractFunction r = new SubtractFunction();
1459 >        for (boolean createIncomplete : new boolean[] { true, false })
1460 >    {
1461          final CFException ex = new CFException();
1462 <
1463 <        g.completeExceptionally(ex);
1464 <        f.complete(v1);
1465 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1466 <
1467 <        checkCompletedWithWrappedCFException(h, ex);
1468 <        checkCompletedWithWrappedCFException(g, ex);
962 <        assertFalse(r.ran());
963 <        checkCompletedNormally(f, v1);
1462 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1463 >        final NoopConsumer r = new NoopConsumer(m);
1464 >        if (!createIncomplete) f.completeExceptionally(ex);
1465 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1466 >        if (createIncomplete) {
1467 >            checkIncomplete(g);
1468 >            f.completeExceptionally(ex);
1469          }
965    }
1470  
1471 <    public void testThenCombine_exceptionalCompletion4() {
1472 <        for (ExecutionMode m : ExecutionMode.values())
1473 <        for (Integer v1 : new Integer[] { 1, null }) {
1471 >        checkCompletedWithWrappedException(g, ex);
1472 >        checkCompletedExceptionally(f, ex);
1473 >        r.assertNotInvoked();
1474 >    }}
1475  
1476 +    /**
1477 +     * thenAccept result completes exceptionally if source cancelled
1478 +     */
1479 +    public void testThenAccept_sourceCancelled() {
1480 +        for (ExecutionMode m : ExecutionMode.values())
1481 +        for (boolean createIncomplete : new boolean[] { true, false })
1482 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1483 +    {
1484          final CompletableFuture<Integer> f = new CompletableFuture<>();
1485 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1486 <        final SubtractFunction r = new SubtractFunction();
1487 <        final CFException ex = new CFException();
1488 <
1489 <        f.completeExceptionally(ex);
1490 <        g.complete(v1);
978 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
979 <
980 <        checkCompletedWithWrappedCFException(h, ex);
981 <        checkCompletedWithWrappedCFException(f, ex);
982 <        assertFalse(r.ran());
983 <        checkCompletedNormally(g, v1);
1485 >        final NoopConsumer r = new NoopConsumer(m);
1486 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1487 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1488 >        if (createIncomplete) {
1489 >            checkIncomplete(g);
1490 >            assertTrue(f.cancel(mayInterruptIfRunning));
1491          }
1492 <    }
1492 >
1493 >        checkCompletedWithWrappedCancellationException(g);
1494 >        checkCancelled(f);
1495 >        r.assertNotInvoked();
1496 >    }}
1497  
1498      /**
1499 <     * thenCombine result completes exceptionally if action does
1499 >     * thenAccept result completes exceptionally if action does
1500       */
1501 <    public void testThenCombine_actionFailed1() {
1501 >    public void testThenAccept_actionFailed() {
1502          for (ExecutionMode m : ExecutionMode.values())
1503 +        for (boolean createIncomplete : new boolean[] { true, false })
1504          for (Integer v1 : new Integer[] { 1, null })
1505 <        for (Integer v2 : new Integer[] { 2, null }) {
994 <
1505 >    {
1506          final CompletableFuture<Integer> f = new CompletableFuture<>();
1507 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1508 <        final FailingBiFunction r = new FailingBiFunction();
1509 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1510 <
1511 <        f.complete(v1);
1512 <        checkIncomplete(h);
1513 <        g.complete(v2);
1507 >        final FailingConsumer r = new FailingConsumer(m);
1508 >        if (!createIncomplete) f.complete(v1);
1509 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1510 >        if (createIncomplete) {
1511 >            checkIncomplete(g);
1512 >            f.complete(v1);
1513 >        }
1514  
1515 <        checkCompletedWithWrappedCFException(h);
1515 >        checkCompletedWithWrappedCFException(g);
1516          checkCompletedNormally(f, v1);
1517 <        checkCompletedNormally(g, v2);
1007 <        }
1008 <    }
1517 >    }}
1518  
1519 <    public void testThenCombine_actionFailed2() {
1519 >    /**
1520 >     * thenCombine result completes normally after normal completion
1521 >     * of sources
1522 >     */
1523 >    public void testThenCombine_normalCompletion() {
1524          for (ExecutionMode m : ExecutionMode.values())
1525 +        for (boolean createIncomplete : new boolean[] { true, false })
1526 +        for (boolean fFirst : new boolean[] { true, false })
1527          for (Integer v1 : new Integer[] { 1, null })
1528 <        for (Integer v2 : new Integer[] { 2, null }) {
1529 <
1528 >        for (Integer v2 : new Integer[] { 2, null })
1529 >    {
1530          final CompletableFuture<Integer> f = new CompletableFuture<>();
1531          final CompletableFuture<Integer> g = new CompletableFuture<>();
1532 <        final FailingBiFunction r = new FailingBiFunction();
1018 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1532 >        final SubtractFunction r = new SubtractFunction(m);
1533  
1534 <        g.complete(v2);
1535 <        checkIncomplete(h);
1536 <        f.complete(v1);
1534 >        if (fFirst) f.complete(v1); else g.complete(v2);
1535 >        if (!createIncomplete)
1536 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1537 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1538 >        if (createIncomplete) {
1539 >            checkIncomplete(h);
1540 >            r.assertNotInvoked();
1541 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1542 >        }
1543  
1544 <        checkCompletedWithWrappedCFException(h);
1544 >        checkCompletedNormally(h, subtract(v1, v2));
1545          checkCompletedNormally(f, v1);
1546          checkCompletedNormally(g, v2);
1547 <        }
1548 <    }
1547 >        r.assertValue(subtract(v1, v2));
1548 >    }}
1549  
1550      /**
1551 <     * thenCombine result completes exceptionally if either source cancelled
1551 >     * thenCombine result completes exceptionally after exceptional
1552 >     * completion of either source
1553       */
1554 <    public void testThenCombine_sourceCancelled1() {
1554 >    public void testThenCombine_exceptionalCompletion() {
1555          for (ExecutionMode m : ExecutionMode.values())
1556 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1557 <        for (Integer v1 : new Integer[] { 1, null }) {
1558 <
1556 >        for (boolean createIncomplete : new boolean[] { true, false })
1557 >        for (boolean fFirst : new boolean[] { true, false })
1558 >        for (Integer v1 : new Integer[] { 1, null })
1559 >    {
1560          final CompletableFuture<Integer> f = new CompletableFuture<>();
1561          final CompletableFuture<Integer> g = new CompletableFuture<>();
1562 <        final SubtractFunction r = new SubtractFunction();
1563 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1042 <
1043 <        assertTrue(f.cancel(mayInterruptIfRunning));
1044 <        checkIncomplete(h);
1045 <        g.complete(v1);
1046 <
1047 <        checkCompletedWithWrappedCancellationException(h);
1048 <        checkCancelled(f);
1049 <        assertFalse(r.ran());
1050 <        checkCompletedNormally(g, v1);
1051 <        }
1052 <    }
1053 <
1054 <    public void testThenCombine_sourceCancelled2() {
1055 <        for (ExecutionMode m : ExecutionMode.values())
1056 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1057 <        for (Integer v1 : new Integer[] { 1, null }) {
1562 >        final CFException ex = new CFException();
1563 >        final SubtractFunction r = new SubtractFunction(m);
1564  
1565 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1566 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1567 <        final SubtractFunction r = new SubtractFunction();
1565 >        (fFirst ? f : g).complete(v1);
1566 >        if (!createIncomplete)
1567 >            (!fFirst ? f : g).completeExceptionally(ex);
1568          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1569 <
1570 <        assertTrue(g.cancel(mayInterruptIfRunning));
1571 <        checkIncomplete(h);
1066 <        f.complete(v1);
1067 <
1068 <        checkCompletedWithWrappedCancellationException(h);
1069 <        checkCancelled(g);
1070 <        assertFalse(r.ran());
1071 <        checkCompletedNormally(f, v1);
1569 >        if (createIncomplete) {
1570 >            checkIncomplete(h);
1571 >            (!fFirst ? f : g).completeExceptionally(ex);
1572          }
1073    }
1573  
1574 <    public void testThenCombine_sourceCancelled3() {
1574 >        checkCompletedWithWrappedException(h, ex);
1575 >        r.assertNotInvoked();
1576 >        checkCompletedNormally(fFirst ? f : g, v1);
1577 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1578 >    }}
1579 >
1580 >    /**
1581 >     * thenCombine result completes exceptionally if either source cancelled
1582 >     */
1583 >    public void testThenCombine_sourceCancelled() {
1584          for (ExecutionMode m : ExecutionMode.values())
1585          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1586 <        for (Integer v1 : new Integer[] { 1, null }) {
1587 <
1586 >        for (boolean createIncomplete : new boolean[] { true, false })
1587 >        for (boolean fFirst : new boolean[] { true, false })
1588 >        for (Integer v1 : new Integer[] { 1, null })
1589 >    {
1590          final CompletableFuture<Integer> f = new CompletableFuture<>();
1591          final CompletableFuture<Integer> g = new CompletableFuture<>();
1592 <        final SubtractFunction r = new SubtractFunction();
1592 >        final SubtractFunction r = new SubtractFunction(m);
1593  
1594 <        assertTrue(g.cancel(mayInterruptIfRunning));
1595 <        f.complete(v1);
1594 >        (fFirst ? f : g).complete(v1);
1595 >        if (!createIncomplete)
1596 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1597          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1598 +        if (createIncomplete) {
1599 +            checkIncomplete(h);
1600 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1601 +        }
1602  
1603          checkCompletedWithWrappedCancellationException(h);
1604 <        checkCancelled(g);
1605 <        assertFalse(r.ran());
1606 <        checkCompletedNormally(f, v1);
1607 <        }
1093 <    }
1604 >        checkCancelled(!fFirst ? f : g);
1605 >        r.assertNotInvoked();
1606 >        checkCompletedNormally(fFirst ? f : g, v1);
1607 >    }}
1608  
1609 <    public void testThenCombine_sourceCancelled4() {
1609 >    /**
1610 >     * thenCombine result completes exceptionally if action does
1611 >     */
1612 >    public void testThenCombine_actionFailed() {
1613          for (ExecutionMode m : ExecutionMode.values())
1614 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1615 <        for (Integer v1 : new Integer[] { 1, null }) {
1616 <
1614 >        for (boolean fFirst : new boolean[] { true, false })
1615 >        for (Integer v1 : new Integer[] { 1, null })
1616 >        for (Integer v2 : new Integer[] { 2, null })
1617 >    {
1618          final CompletableFuture<Integer> f = new CompletableFuture<>();
1619          final CompletableFuture<Integer> g = new CompletableFuture<>();
1620 <        final SubtractFunction r = new SubtractFunction();
1103 <
1104 <        assertTrue(f.cancel(mayInterruptIfRunning));
1105 <        g.complete(v1);
1620 >        final FailingBiFunction r = new FailingBiFunction(m);
1621          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1622  
1623 <        checkCompletedWithWrappedCancellationException(h);
1624 <        checkCancelled(f);
1625 <        assertFalse(r.ran());
1626 <        checkCompletedNormally(g, v1);
1623 >        if (fFirst) {
1624 >            f.complete(v1);
1625 >            g.complete(v2);
1626 >        } else {
1627 >            g.complete(v2);
1628 >            f.complete(v1);
1629          }
1630 <    }
1630 >
1631 >        checkCompletedWithWrappedCFException(h);
1632 >        checkCompletedNormally(f, v1);
1633 >        checkCompletedNormally(g, v2);
1634 >    }}
1635  
1636      /**
1637       * thenAcceptBoth result completes normally after normal
1638       * completion of sources
1639       */
1640 <    public void testThenAcceptBoth_normalCompletion1() {
1640 >    public void testThenAcceptBoth_normalCompletion() {
1641          for (ExecutionMode m : ExecutionMode.values())
1642 +        for (boolean createIncomplete : new boolean[] { true, false })
1643 +        for (boolean fFirst : new boolean[] { true, false })
1644          for (Integer v1 : new Integer[] { 1, null })
1645 <        for (Integer v2 : new Integer[] { 2, null }) {
1646 <
1645 >        for (Integer v2 : new Integer[] { 2, null })
1646 >    {
1647          final CompletableFuture<Integer> f = new CompletableFuture<>();
1648          final CompletableFuture<Integer> g = new CompletableFuture<>();
1649 <        final SubtractAction r = new SubtractAction();
1127 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1649 >        final SubtractAction r = new SubtractAction(m);
1650  
1651 <        f.complete(v1);
1652 <        checkIncomplete(h);
1653 <        assertEquals(r.value, 0);
1654 <        g.complete(v2);
1651 >        if (fFirst) f.complete(v1); else g.complete(v2);
1652 >        if (!createIncomplete)
1653 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1654 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1655 >        if (createIncomplete) {
1656 >            checkIncomplete(h);
1657 >            r.assertNotInvoked();
1658 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1659 >        }
1660  
1661          checkCompletedNormally(h, null);
1662 <        assertEquals(r.value, subtract(v1, v2));
1662 >        r.assertValue(subtract(v1, v2));
1663          checkCompletedNormally(f, v1);
1664          checkCompletedNormally(g, v2);
1665 <        }
1139 <    }
1665 >    }}
1666  
1667 <    public void testThenAcceptBoth_normalCompletion2() {
1667 >    /**
1668 >     * thenAcceptBoth result completes exceptionally after exceptional
1669 >     * completion of either source
1670 >     */
1671 >    public void testThenAcceptBoth_exceptionalCompletion() {
1672          for (ExecutionMode m : ExecutionMode.values())
1673 +        for (boolean createIncomplete : new boolean[] { true, false })
1674 +        for (boolean fFirst : new boolean[] { true, false })
1675          for (Integer v1 : new Integer[] { 1, null })
1676 <        for (Integer v2 : new Integer[] { 2, null }) {
1145 <
1676 >    {
1677          final CompletableFuture<Integer> f = new CompletableFuture<>();
1678          final CompletableFuture<Integer> g = new CompletableFuture<>();
1679 <        final SubtractAction r = new SubtractAction();
1680 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1150 <
1151 <        g.complete(v2);
1152 <        checkIncomplete(h);
1153 <        assertEquals(r.value, 0);
1154 <        f.complete(v1);
1679 >        final CFException ex = new CFException();
1680 >        final SubtractAction r = new SubtractAction(m);
1681  
1682 <        checkCompletedNormally(h, null);
1683 <        assertEquals(r.value, subtract(v1, v2));
1684 <        checkCompletedNormally(f, v1);
1685 <        checkCompletedNormally(g, v2);
1682 >        (fFirst ? f : g).complete(v1);
1683 >        if (!createIncomplete)
1684 >            (!fFirst ? f : g).completeExceptionally(ex);
1685 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1686 >        if (createIncomplete) {
1687 >            checkIncomplete(h);
1688 >            (!fFirst ? f : g).completeExceptionally(ex);
1689          }
1161    }
1690  
1691 <    public void testThenAcceptBoth_normalCompletion3() {
1691 >        checkCompletedWithWrappedException(h, ex);
1692 >        r.assertNotInvoked();
1693 >        checkCompletedNormally(fFirst ? f : g, v1);
1694 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1695 >    }}
1696 >
1697 >    /**
1698 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1699 >     */
1700 >    public void testThenAcceptBoth_sourceCancelled() {
1701          for (ExecutionMode m : ExecutionMode.values())
1702 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1703 +        for (boolean createIncomplete : new boolean[] { true, false })
1704 +        for (boolean fFirst : new boolean[] { true, false })
1705          for (Integer v1 : new Integer[] { 1, null })
1706 <        for (Integer v2 : new Integer[] { 2, null }) {
1167 <
1706 >    {
1707          final CompletableFuture<Integer> f = new CompletableFuture<>();
1708          final CompletableFuture<Integer> g = new CompletableFuture<>();
1709 <        final SubtractAction r = new SubtractAction();
1709 >        final SubtractAction r = new SubtractAction(m);
1710  
1711 <        g.complete(v2);
1712 <        f.complete(v1);
1711 >        (fFirst ? f : g).complete(v1);
1712 >        if (!createIncomplete)
1713 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1714          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1715 <
1716 <        checkCompletedNormally(h, null);
1717 <        assertEquals(r.value, subtract(v1, v2));
1178 <        checkCompletedNormally(f, v1);
1179 <        checkCompletedNormally(g, v2);
1715 >        if (createIncomplete) {
1716 >            checkIncomplete(h);
1717 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1718          }
1181    }
1719  
1720 <    public void testThenAcceptBoth_normalCompletion4() {
1720 >        checkCompletedWithWrappedCancellationException(h);
1721 >        checkCancelled(!fFirst ? f : g);
1722 >        r.assertNotInvoked();
1723 >        checkCompletedNormally(fFirst ? f : g, v1);
1724 >    }}
1725 >
1726 >    /**
1727 >     * thenAcceptBoth result completes exceptionally if action does
1728 >     */
1729 >    public void testThenAcceptBoth_actionFailed() {
1730          for (ExecutionMode m : ExecutionMode.values())
1731 +        for (boolean fFirst : new boolean[] { true, false })
1732          for (Integer v1 : new Integer[] { 1, null })
1733 <        for (Integer v2 : new Integer[] { 2, null }) {
1734 <
1733 >        for (Integer v2 : new Integer[] { 2, null })
1734 >    {
1735          final CompletableFuture<Integer> f = new CompletableFuture<>();
1736          final CompletableFuture<Integer> g = new CompletableFuture<>();
1737 <        final SubtractAction r = new SubtractAction();
1191 <
1192 <        f.complete(v1);
1193 <        g.complete(v2);
1737 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1738          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1739  
1740 <        checkCompletedNormally(h, null);
1741 <        assertEquals(r.value, subtract(v1, v2));
1740 >        if (fFirst) {
1741 >            f.complete(v1);
1742 >            g.complete(v2);
1743 >        } else {
1744 >            g.complete(v2);
1745 >            f.complete(v1);
1746 >        }
1747 >
1748 >        checkCompletedWithWrappedCFException(h);
1749          checkCompletedNormally(f, v1);
1750          checkCompletedNormally(g, v2);
1751 <        }
1201 <    }
1751 >    }}
1752  
1753      /**
1754 <     * thenAcceptBoth result completes exceptionally after exceptional
1755 <     * completion of either source
1754 >     * runAfterBoth result completes normally after normal
1755 >     * completion of sources
1756       */
1757 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1757 >    public void testRunAfterBoth_normalCompletion() {
1758          for (ExecutionMode m : ExecutionMode.values())
1759 <        for (Integer v1 : new Integer[] { 1, null }) {
1760 <
1759 >        for (boolean createIncomplete : new boolean[] { true, false })
1760 >        for (boolean fFirst : new boolean[] { true, false })
1761 >        for (Integer v1 : new Integer[] { 1, null })
1762 >        for (Integer v2 : new Integer[] { 2, null })
1763 >    {
1764          final CompletableFuture<Integer> f = new CompletableFuture<>();
1765          final CompletableFuture<Integer> g = new CompletableFuture<>();
1766 <        final SubtractAction r = new SubtractAction();
1214 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1215 <        final CFException ex = new CFException();
1216 <
1217 <        f.completeExceptionally(ex);
1218 <        checkIncomplete(h);
1219 <        g.complete(v1);
1766 >        final Noop r = new Noop(m);
1767  
1768 <        checkCompletedWithWrappedCFException(h, ex);
1769 <        checkCompletedWithWrappedCFException(f, ex);
1770 <        assertFalse(r.ran());
1771 <        checkCompletedNormally(g, v1);
1768 >        if (fFirst) f.complete(v1); else g.complete(v2);
1769 >        if (!createIncomplete)
1770 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1771 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1772 >        if (createIncomplete) {
1773 >            checkIncomplete(h);
1774 >            r.assertNotInvoked();
1775 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1776          }
1226    }
1227
1228    public void testThenAcceptBoth_exceptionalCompletion2() {
1229        for (ExecutionMode m : ExecutionMode.values())
1230        for (Integer v1 : new Integer[] { 1, null }) {
1231
1232        final CompletableFuture<Integer> f = new CompletableFuture<>();
1233        final CompletableFuture<Integer> g = new CompletableFuture<>();
1234        final SubtractAction r = new SubtractAction();
1235        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1236        final CFException ex = new CFException();
1777  
1778 <        g.completeExceptionally(ex);
1779 <        checkIncomplete(h);
1240 <        f.complete(v1);
1241 <
1242 <        checkCompletedWithWrappedCFException(h, ex);
1243 <        checkCompletedWithWrappedCFException(g, ex);
1244 <        assertFalse(r.ran());
1778 >        checkCompletedNormally(h, null);
1779 >        r.assertInvoked();
1780          checkCompletedNormally(f, v1);
1781 <        }
1782 <    }
1781 >        checkCompletedNormally(g, v2);
1782 >    }}
1783  
1784 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1784 >    /**
1785 >     * runAfterBoth result completes exceptionally after exceptional
1786 >     * completion of either source
1787 >     */
1788 >    public void testRunAfterBoth_exceptionalCompletion() {
1789          for (ExecutionMode m : ExecutionMode.values())
1790 <        for (Integer v1 : new Integer[] { 1, null }) {
1791 <
1790 >        for (boolean createIncomplete : new boolean[] { true, false })
1791 >        for (boolean fFirst : new boolean[] { true, false })
1792 >        for (Integer v1 : new Integer[] { 1, null })
1793 >    {
1794          final CompletableFuture<Integer> f = new CompletableFuture<>();
1795          final CompletableFuture<Integer> g = new CompletableFuture<>();
1255        final SubtractAction r = new SubtractAction();
1796          final CFException ex = new CFException();
1797 +        final Noop r = new Noop(m);
1798  
1799 <        g.completeExceptionally(ex);
1800 <        f.complete(v1);
1801 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1802 <
1803 <        checkCompletedWithWrappedCFException(h, ex);
1804 <        checkCompletedWithWrappedCFException(g, ex);
1805 <        assertFalse(r.ran());
1265 <        checkCompletedNormally(f, v1);
1799 >        (fFirst ? f : g).complete(v1);
1800 >        if (!createIncomplete)
1801 >            (!fFirst ? f : g).completeExceptionally(ex);
1802 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1803 >        if (createIncomplete) {
1804 >            checkIncomplete(h);
1805 >            (!fFirst ? f : g).completeExceptionally(ex);
1806          }
1267    }
1807  
1808 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1809 <        for (ExecutionMode m : ExecutionMode.values())
1810 <        for (Integer v1 : new Integer[] { 1, null }) {
1808 >        checkCompletedWithWrappedException(h, ex);
1809 >        r.assertNotInvoked();
1810 >        checkCompletedNormally(fFirst ? f : g, v1);
1811 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1812 >    }}
1813  
1814 +    /**
1815 +     * runAfterBoth result completes exceptionally if either source cancelled
1816 +     */
1817 +    public void testRunAfterBoth_sourceCancelled() {
1818 +        for (ExecutionMode m : ExecutionMode.values())
1819 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1820 +        for (boolean createIncomplete : new boolean[] { true, false })
1821 +        for (boolean fFirst : new boolean[] { true, false })
1822 +        for (Integer v1 : new Integer[] { 1, null })
1823 +    {
1824          final CompletableFuture<Integer> f = new CompletableFuture<>();
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826 <        final SubtractAction r = new SubtractAction();
1276 <        final CFException ex = new CFException();
1826 >        final Noop r = new Noop(m);
1827  
1828 <        f.completeExceptionally(ex);
1829 <        g.complete(v1);
1830 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1831 <
1832 <        checkCompletedWithWrappedCFException(h, ex);
1833 <        checkCompletedWithWrappedCFException(f, ex);
1834 <        assertFalse(r.ran());
1285 <        checkCompletedNormally(g, v1);
1828 >        (fFirst ? f : g).complete(v1);
1829 >        if (!createIncomplete)
1830 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1831 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1832 >        if (createIncomplete) {
1833 >            checkIncomplete(h);
1834 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1835          }
1836 <    }
1836 >
1837 >        checkCompletedWithWrappedCancellationException(h);
1838 >        checkCancelled(!fFirst ? f : g);
1839 >        r.assertNotInvoked();
1840 >        checkCompletedNormally(fFirst ? f : g, v1);
1841 >    }}
1842  
1843      /**
1844 <     * thenAcceptBoth result completes exceptionally if action does
1844 >     * runAfterBoth result completes exceptionally if action does
1845       */
1846 <    public void testThenAcceptBoth_actionFailed1() {
1846 >    public void testRunAfterBoth_actionFailed() {
1847          for (ExecutionMode m : ExecutionMode.values())
1848 +        for (boolean fFirst : new boolean[] { true, false })
1849          for (Integer v1 : new Integer[] { 1, null })
1850 <        for (Integer v2 : new Integer[] { 2, null }) {
1851 <
1850 >        for (Integer v2 : new Integer[] { 2, null })
1851 >    {
1852          final CompletableFuture<Integer> f = new CompletableFuture<>();
1853          final CompletableFuture<Integer> g = new CompletableFuture<>();
1854 <        final FailingBiConsumer r = new FailingBiConsumer();
1855 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1854 >        final FailingRunnable r1 = new FailingRunnable(m);
1855 >        final FailingRunnable r2 = new FailingRunnable(m);
1856  
1857 <        f.complete(v1);
1858 <        checkIncomplete(h);
1859 <        g.complete(v2);
1857 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1858 >        if (fFirst) {
1859 >            f.complete(v1);
1860 >            g.complete(v2);
1861 >        } else {
1862 >            g.complete(v2);
1863 >            f.complete(v1);
1864 >        }
1865 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1866  
1867 <        checkCompletedWithWrappedCFException(h);
1867 >        checkCompletedWithWrappedCFException(h1);
1868 >        checkCompletedWithWrappedCFException(h2);
1869          checkCompletedNormally(f, v1);
1870          checkCompletedNormally(g, v2);
1871 <        }
1310 <    }
1871 >    }}
1872  
1873 <    public void testThenAcceptBoth_actionFailed2() {
1873 >    /**
1874 >     * applyToEither result completes normally after normal completion
1875 >     * of either source
1876 >     */
1877 >    public void testApplyToEither_normalCompletion() {
1878          for (ExecutionMode m : ExecutionMode.values())
1879          for (Integer v1 : new Integer[] { 1, null })
1880 <        for (Integer v2 : new Integer[] { 2, null }) {
1881 <
1880 >        for (Integer v2 : new Integer[] { 2, null })
1881 >    {
1882          final CompletableFuture<Integer> f = new CompletableFuture<>();
1883          final CompletableFuture<Integer> g = new CompletableFuture<>();
1884 <        final FailingBiConsumer r = new FailingBiConsumer();
1885 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1884 >        final IncFunction[] rs = new IncFunction[6];
1885 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1886  
1887 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1888 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1889 +        checkIncomplete(h0);
1890 +        checkIncomplete(h1);
1891 +        rs[0].assertNotInvoked();
1892 +        rs[1].assertNotInvoked();
1893 +        f.complete(v1);
1894 +        checkCompletedNormally(h0, inc(v1));
1895 +        checkCompletedNormally(h1, inc(v1));
1896 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1897 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1898 +        checkCompletedNormally(h2, inc(v1));
1899 +        checkCompletedNormally(h3, inc(v1));
1900          g.complete(v2);
1323        checkIncomplete(h);
1324        f.complete(v1);
1901  
1902 <        checkCompletedWithWrappedCFException(h);
1902 >        // unspecified behavior - both source completions available
1903 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1904 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1905 >        rs[4].assertValue(h4.join());
1906 >        rs[5].assertValue(h5.join());
1907 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1908 >                   Objects.equals(inc(v2), h4.join()));
1909 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1910 >                   Objects.equals(inc(v2), h5.join()));
1911 >
1912          checkCompletedNormally(f, v1);
1913          checkCompletedNormally(g, v2);
1914 <        }
1915 <    }
1914 >        checkCompletedNormally(h0, inc(v1));
1915 >        checkCompletedNormally(h1, inc(v1));
1916 >        checkCompletedNormally(h2, inc(v1));
1917 >        checkCompletedNormally(h3, inc(v1));
1918 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1919 >    }}
1920  
1921      /**
1922 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1922 >     * applyToEither result completes exceptionally after exceptional
1923 >     * completion of either source
1924       */
1925 <    public void testThenAcceptBoth_sourceCancelled1() {
1925 >    public void testApplyToEither_exceptionalCompletion() {
1926          for (ExecutionMode m : ExecutionMode.values())
1927 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1928 <        for (Integer v1 : new Integer[] { 1, null }) {
1339 <
1927 >        for (Integer v1 : new Integer[] { 1, null })
1928 >    {
1929          final CompletableFuture<Integer> f = new CompletableFuture<>();
1930          final CompletableFuture<Integer> g = new CompletableFuture<>();
1931 <        final SubtractAction r = new SubtractAction();
1932 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1931 >        final CFException ex = new CFException();
1932 >        final IncFunction[] rs = new IncFunction[6];
1933 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1934  
1935 <        assertTrue(f.cancel(mayInterruptIfRunning));
1936 <        checkIncomplete(h);
1935 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1936 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1937 >        checkIncomplete(h0);
1938 >        checkIncomplete(h1);
1939 >        rs[0].assertNotInvoked();
1940 >        rs[1].assertNotInvoked();
1941 >        f.completeExceptionally(ex);
1942 >        checkCompletedWithWrappedException(h0, ex);
1943 >        checkCompletedWithWrappedException(h1, ex);
1944 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1945 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1946 >        checkCompletedWithWrappedException(h2, ex);
1947 >        checkCompletedWithWrappedException(h3, ex);
1948          g.complete(v1);
1949  
1950 <        checkCompletedWithWrappedCancellationException(h);
1951 <        checkCancelled(f);
1952 <        assertFalse(r.ran());
1953 <        checkCompletedNormally(g, v1);
1950 >        // unspecified behavior - both source completions available
1951 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1952 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1953 >        try {
1954 >            assertEquals(inc(v1), h4.join());
1955 >            rs[4].assertValue(inc(v1));
1956 >        } catch (CompletionException ok) {
1957 >            checkCompletedWithWrappedException(h4, ex);
1958 >            rs[4].assertNotInvoked();
1959 >        }
1960 >        try {
1961 >            assertEquals(inc(v1), h5.join());
1962 >            rs[5].assertValue(inc(v1));
1963 >        } catch (CompletionException ok) {
1964 >            checkCompletedWithWrappedException(h5, ex);
1965 >            rs[5].assertNotInvoked();
1966          }
1354    }
1967  
1968 <    public void testThenAcceptBoth_sourceCancelled2() {
1969 <        for (ExecutionMode m : ExecutionMode.values())
1970 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1971 <        for (Integer v1 : new Integer[] { 1, null }) {
1968 >        checkCompletedExceptionally(f, ex);
1969 >        checkCompletedNormally(g, v1);
1970 >        checkCompletedWithWrappedException(h0, ex);
1971 >        checkCompletedWithWrappedException(h1, ex);
1972 >        checkCompletedWithWrappedException(h2, ex);
1973 >        checkCompletedWithWrappedException(h3, ex);
1974 >        checkCompletedWithWrappedException(h4, ex);
1975 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1976 >    }}
1977  
1978 +    public void testApplyToEither_exceptionalCompletion2() {
1979 +        for (ExecutionMode m : ExecutionMode.values())
1980 +        for (boolean fFirst : new boolean[] { true, false })
1981 +        for (Integer v1 : new Integer[] { 1, null })
1982 +    {
1983          final CompletableFuture<Integer> f = new CompletableFuture<>();
1984          final CompletableFuture<Integer> g = new CompletableFuture<>();
1985 <        final SubtractAction r = new SubtractAction();
1986 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1987 <
1366 <        assertTrue(g.cancel(mayInterruptIfRunning));
1367 <        checkIncomplete(h);
1368 <        f.complete(v1);
1985 >        final CFException ex = new CFException();
1986 >        final IncFunction[] rs = new IncFunction[6];
1987 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1988  
1989 <        checkCompletedWithWrappedCancellationException(h);
1990 <        checkCancelled(g);
1991 <        assertFalse(r.ran());
1992 <        checkCompletedNormally(f, v1);
1989 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1990 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1991 >        if (fFirst) {
1992 >            f.complete(v1);
1993 >            g.completeExceptionally(ex);
1994 >        } else {
1995 >            g.completeExceptionally(ex);
1996 >            f.complete(v1);
1997          }
1998 <    }
1999 <
1377 <    public void testThenAcceptBoth_sourceCancelled3() {
1378 <        for (ExecutionMode m : ExecutionMode.values())
1379 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1380 <        for (Integer v1 : new Integer[] { 1, null }) {
1998 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1999 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2000  
2001 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2002 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2003 <        final SubtractAction r = new SubtractAction();
2004 <
2005 <        assertTrue(g.cancel(mayInterruptIfRunning));
2006 <        f.complete(v1);
2007 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
2001 >        // unspecified behavior - both source completions available
2002 >        try {
2003 >            assertEquals(inc(v1), h0.join());
2004 >            rs[0].assertValue(inc(v1));
2005 >        } catch (CompletionException ok) {
2006 >            checkCompletedWithWrappedException(h0, ex);
2007 >            rs[0].assertNotInvoked();
2008 >        }
2009 >        try {
2010 >            assertEquals(inc(v1), h1.join());
2011 >            rs[1].assertValue(inc(v1));
2012 >        } catch (CompletionException ok) {
2013 >            checkCompletedWithWrappedException(h1, ex);
2014 >            rs[1].assertNotInvoked();
2015 >        }
2016 >        try {
2017 >            assertEquals(inc(v1), h2.join());
2018 >            rs[2].assertValue(inc(v1));
2019 >        } catch (CompletionException ok) {
2020 >            checkCompletedWithWrappedException(h2, ex);
2021 >            rs[2].assertNotInvoked();
2022 >        }
2023 >        try {
2024 >            assertEquals(inc(v1), h3.join());
2025 >            rs[3].assertValue(inc(v1));
2026 >        } catch (CompletionException ok) {
2027 >            checkCompletedWithWrappedException(h3, ex);
2028 >            rs[3].assertNotInvoked();
2029 >        }
2030  
1390        checkCompletedWithWrappedCancellationException(h);
1391        checkCancelled(g);
1392        assertFalse(r.ran());
2031          checkCompletedNormally(f, v1);
2032 <        }
2033 <    }
2032 >        checkCompletedExceptionally(g, ex);
2033 >    }}
2034  
2035 <    public void testThenAcceptBoth_sourceCancelled4() {
2035 >    /**
2036 >     * applyToEither result completes exceptionally if either source cancelled
2037 >     */
2038 >    public void testApplyToEither_sourceCancelled() {
2039          for (ExecutionMode m : ExecutionMode.values())
2040          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2041 <        for (Integer v1 : new Integer[] { 1, null }) {
2042 <
2041 >        for (Integer v1 : new Integer[] { 1, null })
2042 >    {
2043          final CompletableFuture<Integer> f = new CompletableFuture<>();
2044          final CompletableFuture<Integer> g = new CompletableFuture<>();
2045 <        final SubtractAction r = new SubtractAction();
2045 >        final IncFunction[] rs = new IncFunction[6];
2046 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2047  
2048 <        assertTrue(f.cancel(mayInterruptIfRunning));
2048 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2049 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2050 >        checkIncomplete(h0);
2051 >        checkIncomplete(h1);
2052 >        rs[0].assertNotInvoked();
2053 >        rs[1].assertNotInvoked();
2054 >        f.cancel(mayInterruptIfRunning);
2055 >        checkCompletedWithWrappedCancellationException(h0);
2056 >        checkCompletedWithWrappedCancellationException(h1);
2057 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2058 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2059 >        checkCompletedWithWrappedCancellationException(h2);
2060 >        checkCompletedWithWrappedCancellationException(h3);
2061          g.complete(v1);
1408        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
2062  
2063 <        checkCompletedWithWrappedCancellationException(h);
2063 >        // unspecified behavior - both source completions available
2064 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2065 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2066 >        try {
2067 >            assertEquals(inc(v1), h4.join());
2068 >            rs[4].assertValue(inc(v1));
2069 >        } catch (CompletionException ok) {
2070 >            checkCompletedWithWrappedCancellationException(h4);
2071 >            rs[4].assertNotInvoked();
2072 >        }
2073 >        try {
2074 >            assertEquals(inc(v1), h5.join());
2075 >            rs[5].assertValue(inc(v1));
2076 >        } catch (CompletionException ok) {
2077 >            checkCompletedWithWrappedCancellationException(h5);
2078 >            rs[5].assertNotInvoked();
2079 >        }
2080 >
2081          checkCancelled(f);
1412        assertFalse(r.ran());
2082          checkCompletedNormally(g, v1);
2083 <        }
2084 <    }
2083 >        checkCompletedWithWrappedCancellationException(h0);
2084 >        checkCompletedWithWrappedCancellationException(h1);
2085 >        checkCompletedWithWrappedCancellationException(h2);
2086 >        checkCompletedWithWrappedCancellationException(h3);
2087 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2088 >    }}
2089  
2090 <    /**
1418 <     * runAfterBoth result completes normally after normal
1419 <     * completion of sources
1420 <     */
1421 <    public void testRunAfterBoth_normalCompletion1() {
2090 >    public void testApplyToEither_sourceCancelled2() {
2091          for (ExecutionMode m : ExecutionMode.values())
2092 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2093 +        for (boolean fFirst : new boolean[] { true, false })
2094          for (Integer v1 : new Integer[] { 1, null })
2095 <        for (Integer v2 : new Integer[] { 2, null }) {
1425 <
2095 >    {
2096          final CompletableFuture<Integer> f = new CompletableFuture<>();
2097          final CompletableFuture<Integer> g = new CompletableFuture<>();
2098 <        final Noop r = new Noop();
2099 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1430 <
1431 <        f.complete(v1);
1432 <        checkIncomplete(h);
1433 <        assertFalse(r.ran);
1434 <        g.complete(v2);
2098 >        final IncFunction[] rs = new IncFunction[6];
2099 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2100  
2101 <        checkCompletedNormally(h, null);
2102 <        assertTrue(r.ran);
2103 <        checkCompletedNormally(f, v1);
2104 <        checkCompletedNormally(g, v2);
2101 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2102 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2103 >        if (fFirst) {
2104 >            f.complete(v1);
2105 >            g.cancel(mayInterruptIfRunning);
2106 >        } else {
2107 >            g.cancel(mayInterruptIfRunning);
2108 >            f.complete(v1);
2109          }
2110 <    }
2111 <
1443 <    public void testRunAfterBoth_normalCompletion2() {
1444 <        for (ExecutionMode m : ExecutionMode.values())
1445 <        for (Integer v1 : new Integer[] { 1, null })
1446 <        for (Integer v2 : new Integer[] { 2, null }) {
1447 <
1448 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1449 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1450 <        final Noop r = new Noop();
1451 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2110 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2111 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2112  
2113 <        g.complete(v2);
2114 <        checkIncomplete(h);
2115 <        assertFalse(r.ran);
2116 <        f.complete(v1);
2113 >        // unspecified behavior - both source completions available
2114 >        try {
2115 >            assertEquals(inc(v1), h0.join());
2116 >            rs[0].assertValue(inc(v1));
2117 >        } catch (CompletionException ok) {
2118 >            checkCompletedWithWrappedCancellationException(h0);
2119 >            rs[0].assertNotInvoked();
2120 >        }
2121 >        try {
2122 >            assertEquals(inc(v1), h1.join());
2123 >            rs[1].assertValue(inc(v1));
2124 >        } catch (CompletionException ok) {
2125 >            checkCompletedWithWrappedCancellationException(h1);
2126 >            rs[1].assertNotInvoked();
2127 >        }
2128 >        try {
2129 >            assertEquals(inc(v1), h2.join());
2130 >            rs[2].assertValue(inc(v1));
2131 >        } catch (CompletionException ok) {
2132 >            checkCompletedWithWrappedCancellationException(h2);
2133 >            rs[2].assertNotInvoked();
2134 >        }
2135 >        try {
2136 >            assertEquals(inc(v1), h3.join());
2137 >            rs[3].assertValue(inc(v1));
2138 >        } catch (CompletionException ok) {
2139 >            checkCompletedWithWrappedCancellationException(h3);
2140 >            rs[3].assertNotInvoked();
2141 >        }
2142  
1458        checkCompletedNormally(h, null);
1459        assertTrue(r.ran);
2143          checkCompletedNormally(f, v1);
2144 <        checkCompletedNormally(g, v2);
2145 <        }
1463 <    }
2144 >        checkCancelled(g);
2145 >    }}
2146  
2147 <    public void testRunAfterBoth_normalCompletion3() {
2147 >    /**
2148 >     * applyToEither result completes exceptionally if action does
2149 >     */
2150 >    public void testApplyToEither_actionFailed() {
2151          for (ExecutionMode m : ExecutionMode.values())
2152          for (Integer v1 : new Integer[] { 1, null })
2153 <        for (Integer v2 : new Integer[] { 2, null }) {
2154 <
2153 >        for (Integer v2 : new Integer[] { 2, null })
2154 >    {
2155          final CompletableFuture<Integer> f = new CompletableFuture<>();
2156          final CompletableFuture<Integer> g = new CompletableFuture<>();
2157 <        final Noop r = new Noop();
2157 >        final FailingFunction[] rs = new FailingFunction[6];
2158 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2159 >
2160 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2161 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2162 >        f.complete(v1);
2163 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2164 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2165 >        checkCompletedWithWrappedCFException(h0);
2166 >        checkCompletedWithWrappedCFException(h1);
2167 >        checkCompletedWithWrappedCFException(h2);
2168 >        checkCompletedWithWrappedCFException(h3);
2169 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2170  
2171          g.complete(v2);
1475        f.complete(v1);
1476        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2172  
2173 <        checkCompletedNormally(h, null);
2174 <        assertTrue(r.ran);
2173 >        // unspecified behavior - both source completions available
2174 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2175 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2176 >
2177 >        checkCompletedWithWrappedCFException(h4);
2178 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2179 >                   Objects.equals(v2, rs[4].value));
2180 >        checkCompletedWithWrappedCFException(h5);
2181 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2182 >                   Objects.equals(v2, rs[5].value));
2183 >
2184          checkCompletedNormally(f, v1);
2185          checkCompletedNormally(g, v2);
2186 <        }
1483 <    }
2186 >    }}
2187  
2188 <    public void testRunAfterBoth_normalCompletion4() {
2188 >    /**
2189 >     * acceptEither result completes normally after normal completion
2190 >     * of either source
2191 >     */
2192 >    public void testAcceptEither_normalCompletion() {
2193          for (ExecutionMode m : ExecutionMode.values())
2194          for (Integer v1 : new Integer[] { 1, null })
2195 <        for (Integer v2 : new Integer[] { 2, null }) {
2196 <
2195 >        for (Integer v2 : new Integer[] { 2, null })
2196 >    {
2197          final CompletableFuture<Integer> f = new CompletableFuture<>();
2198          final CompletableFuture<Integer> g = new CompletableFuture<>();
2199 <        final Noop r = new Noop();
2199 >        final NoopConsumer[] rs = new NoopConsumer[6];
2200 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2201  
2202 <        f.complete(v1);
2202 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2203 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2204 >        checkIncomplete(h0);
2205 >        checkIncomplete(h1);
2206 >        rs[0].assertNotInvoked();
2207 >        rs[1].assertNotInvoked();
2208 >        f.complete(v1);
2209 >        checkCompletedNormally(h0, null);
2210 >        checkCompletedNormally(h1, null);
2211 >        rs[0].assertValue(v1);
2212 >        rs[1].assertValue(v1);
2213 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2214 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2215 >        checkCompletedNormally(h2, null);
2216 >        checkCompletedNormally(h3, null);
2217 >        rs[2].assertValue(v1);
2218 >        rs[3].assertValue(v1);
2219          g.complete(v2);
1496        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2220  
2221 <        checkCompletedNormally(h, null);
2222 <        assertTrue(r.ran);
2221 >        // unspecified behavior - both source completions available
2222 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2223 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2224 >        checkCompletedNormally(h4, null);
2225 >        checkCompletedNormally(h5, null);
2226 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2227 >                   Objects.equals(v2, rs[4].value));
2228 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2229 >                   Objects.equals(v2, rs[5].value));
2230 >
2231          checkCompletedNormally(f, v1);
2232          checkCompletedNormally(g, v2);
2233 <        }
2234 <    }
2233 >        checkCompletedNormally(h0, null);
2234 >        checkCompletedNormally(h1, null);
2235 >        checkCompletedNormally(h2, null);
2236 >        checkCompletedNormally(h3, null);
2237 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2238 >    }}
2239  
2240      /**
2241 <     * runAfterBoth result completes exceptionally after exceptional
2241 >     * acceptEither result completes exceptionally after exceptional
2242       * completion of either source
2243       */
2244 <    public void testRunAfterBoth_exceptionalCompletion1() {
2244 >    public void testAcceptEither_exceptionalCompletion() {
2245          for (ExecutionMode m : ExecutionMode.values())
2246 <        for (Integer v1 : new Integer[] { 1, null }) {
2247 <
2246 >        for (Integer v1 : new Integer[] { 1, null })
2247 >    {
2248          final CompletableFuture<Integer> f = new CompletableFuture<>();
2249          final CompletableFuture<Integer> g = new CompletableFuture<>();
1515        final Noop r = new Noop();
1516        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2250          final CFException ex = new CFException();
2251 +        final NoopConsumer[] rs = new NoopConsumer[6];
2252 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2253  
2254 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2255 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2256 +        checkIncomplete(h0);
2257 +        checkIncomplete(h1);
2258 +        rs[0].assertNotInvoked();
2259 +        rs[1].assertNotInvoked();
2260          f.completeExceptionally(ex);
2261 <        checkIncomplete(h);
2261 >        checkCompletedWithWrappedException(h0, ex);
2262 >        checkCompletedWithWrappedException(h1, ex);
2263 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2264 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2265 >        checkCompletedWithWrappedException(h2, ex);
2266 >        checkCompletedWithWrappedException(h3, ex);
2267 >
2268          g.complete(v1);
2269  
2270 <        checkCompletedWithWrappedCFException(h, ex);
2271 <        checkCompletedWithWrappedCFException(f, ex);
2272 <        assertFalse(r.ran);
2273 <        checkCompletedNormally(g, v1);
2270 >        // unspecified behavior - both source completions available
2271 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2272 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2273 >        try {
2274 >            assertNull(h4.join());
2275 >            rs[4].assertValue(v1);
2276 >        } catch (CompletionException ok) {
2277 >            checkCompletedWithWrappedException(h4, ex);
2278 >            rs[4].assertNotInvoked();
2279 >        }
2280 >        try {
2281 >            assertNull(h5.join());
2282 >            rs[5].assertValue(v1);
2283 >        } catch (CompletionException ok) {
2284 >            checkCompletedWithWrappedException(h5, ex);
2285 >            rs[5].assertNotInvoked();
2286          }
1528    }
2287  
2288 <    public void testRunAfterBoth_exceptionalCompletion2() {
2289 <        for (ExecutionMode m : ExecutionMode.values())
2290 <        for (Integer v1 : new Integer[] { 1, null }) {
2288 >        checkCompletedExceptionally(f, ex);
2289 >        checkCompletedNormally(g, v1);
2290 >        checkCompletedWithWrappedException(h0, ex);
2291 >        checkCompletedWithWrappedException(h1, ex);
2292 >        checkCompletedWithWrappedException(h2, ex);
2293 >        checkCompletedWithWrappedException(h3, ex);
2294 >        checkCompletedWithWrappedException(h4, ex);
2295 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2296 >    }}
2297  
2298 +    public void testAcceptEither_exceptionalCompletion2() {
2299 +        for (ExecutionMode m : ExecutionMode.values())
2300 +        for (boolean fFirst : new boolean[] { true, false })
2301 +        for (Integer v1 : new Integer[] { 1, null })
2302 +    {
2303          final CompletableFuture<Integer> f = new CompletableFuture<>();
2304          final CompletableFuture<Integer> g = new CompletableFuture<>();
1536        final Noop r = new Noop();
1537        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2305          final CFException ex = new CFException();
2306 +        final NoopConsumer[] rs = new NoopConsumer[6];
2307 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2308  
2309 <        g.completeExceptionally(ex);
2310 <        checkIncomplete(h);
2311 <        f.complete(v1);
2312 <
2313 <        checkCompletedWithWrappedCFException(h, ex);
2314 <        checkCompletedWithWrappedCFException(g, ex);
2315 <        assertFalse(r.ran);
2316 <        checkCompletedNormally(f, v1);
2309 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2310 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2311 >        if (fFirst) {
2312 >            f.complete(v1);
2313 >            g.completeExceptionally(ex);
2314 >        } else {
2315 >            g.completeExceptionally(ex);
2316 >            f.complete(v1);
2317          }
2318 <    }
2319 <
1551 <    public void testRunAfterBoth_exceptionalCompletion3() {
1552 <        for (ExecutionMode m : ExecutionMode.values())
1553 <        for (Integer v1 : new Integer[] { 1, null }) {
2318 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2319 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2320  
2321 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2322 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2323 <        final Noop r = new Noop();
2324 <        final CFException ex = new CFException();
2325 <
2326 <        g.completeExceptionally(ex);
2327 <        f.complete(v1);
2328 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2321 >        // unspecified behavior - both source completions available
2322 >        try {
2323 >            assertEquals(null, h0.join());
2324 >            rs[0].assertValue(v1);
2325 >        } catch (CompletionException ok) {
2326 >            checkCompletedWithWrappedException(h0, ex);
2327 >            rs[0].assertNotInvoked();
2328 >        }
2329 >        try {
2330 >            assertEquals(null, h1.join());
2331 >            rs[1].assertValue(v1);
2332 >        } catch (CompletionException ok) {
2333 >            checkCompletedWithWrappedException(h1, ex);
2334 >            rs[1].assertNotInvoked();
2335 >        }
2336 >        try {
2337 >            assertEquals(null, h2.join());
2338 >            rs[2].assertValue(v1);
2339 >        } catch (CompletionException ok) {
2340 >            checkCompletedWithWrappedException(h2, ex);
2341 >            rs[2].assertNotInvoked();
2342 >        }
2343 >        try {
2344 >            assertEquals(null, h3.join());
2345 >            rs[3].assertValue(v1);
2346 >        } catch (CompletionException ok) {
2347 >            checkCompletedWithWrappedException(h3, ex);
2348 >            rs[3].assertNotInvoked();
2349 >        }
2350  
1564        checkCompletedWithWrappedCFException(h, ex);
1565        checkCompletedWithWrappedCFException(g, ex);
1566        assertFalse(r.ran);
2351          checkCompletedNormally(f, v1);
2352 <        }
2353 <    }
2352 >        checkCompletedExceptionally(g, ex);
2353 >    }}
2354  
2355 <    public void testRunAfterBoth_exceptionalCompletion4() {
2355 >    /**
2356 >     * acceptEither result completes exceptionally if either source cancelled
2357 >     */
2358 >    public void testAcceptEither_sourceCancelled() {
2359          for (ExecutionMode m : ExecutionMode.values())
2360 <        for (Integer v1 : new Integer[] { 1, null }) {
2361 <
2360 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2361 >        for (Integer v1 : new Integer[] { 1, null })
2362 >    {
2363          final CompletableFuture<Integer> f = new CompletableFuture<>();
2364          final CompletableFuture<Integer> g = new CompletableFuture<>();
2365 <        final Noop r = new Noop();
2366 <        final CFException ex = new CFException();
2365 >        final NoopConsumer[] rs = new NoopConsumer[6];
2366 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2367 >
2368 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2369 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2370 >        checkIncomplete(h0);
2371 >        checkIncomplete(h1);
2372 >        rs[0].assertNotInvoked();
2373 >        rs[1].assertNotInvoked();
2374 >        f.cancel(mayInterruptIfRunning);
2375 >        checkCompletedWithWrappedCancellationException(h0);
2376 >        checkCompletedWithWrappedCancellationException(h1);
2377 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2378 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2379 >        checkCompletedWithWrappedCancellationException(h2);
2380 >        checkCompletedWithWrappedCancellationException(h3);
2381  
1580        f.completeExceptionally(ex);
2382          g.complete(v1);
1582        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2383  
2384 <        checkCompletedWithWrappedCFException(h, ex);
2385 <        checkCompletedWithWrappedCFException(f, ex);
2386 <        assertFalse(r.ran);
2387 <        checkCompletedNormally(g, v1);
2384 >        // unspecified behavior - both source completions available
2385 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2386 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2387 >        try {
2388 >            assertNull(h4.join());
2389 >            rs[4].assertValue(v1);
2390 >        } catch (CompletionException ok) {
2391 >            checkCompletedWithWrappedCancellationException(h4);
2392 >            rs[4].assertNotInvoked();
2393 >        }
2394 >        try {
2395 >            assertNull(h5.join());
2396 >            rs[5].assertValue(v1);
2397 >        } catch (CompletionException ok) {
2398 >            checkCompletedWithWrappedCancellationException(h5);
2399 >            rs[5].assertNotInvoked();
2400          }
2401 <    }
2401 >
2402 >        checkCancelled(f);
2403 >        checkCompletedNormally(g, v1);
2404 >        checkCompletedWithWrappedCancellationException(h0);
2405 >        checkCompletedWithWrappedCancellationException(h1);
2406 >        checkCompletedWithWrappedCancellationException(h2);
2407 >        checkCompletedWithWrappedCancellationException(h3);
2408 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2409 >    }}
2410  
2411      /**
2412 <     * runAfterBoth result completes exceptionally if action does
2412 >     * acceptEither result completes exceptionally if action does
2413       */
2414 <    public void testRunAfterBoth_actionFailed1() {
2414 >    public void testAcceptEither_actionFailed() {
2415          for (ExecutionMode m : ExecutionMode.values())
2416          for (Integer v1 : new Integer[] { 1, null })
2417 <        for (Integer v2 : new Integer[] { 2, null }) {
2418 <
2417 >        for (Integer v2 : new Integer[] { 2, null })
2418 >    {
2419          final CompletableFuture<Integer> f = new CompletableFuture<>();
2420          final CompletableFuture<Integer> g = new CompletableFuture<>();
2421 <        final FailingNoop r = new FailingNoop();
2422 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2421 >        final FailingConsumer[] rs = new FailingConsumer[6];
2422 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2423 >
2424 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2425 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2426 >        f.complete(v1);
2427 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2428 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2429 >        checkCompletedWithWrappedCFException(h0);
2430 >        checkCompletedWithWrappedCFException(h1);
2431 >        checkCompletedWithWrappedCFException(h2);
2432 >        checkCompletedWithWrappedCFException(h3);
2433 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2434  
1604        f.complete(v1);
1605        checkIncomplete(h);
2435          g.complete(v2);
2436  
2437 <        checkCompletedWithWrappedCFException(h);
2437 >        // unspecified behavior - both source completions available
2438 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2439 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2440 >
2441 >        checkCompletedWithWrappedCFException(h4);
2442 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2443 >                   Objects.equals(v2, rs[4].value));
2444 >        checkCompletedWithWrappedCFException(h5);
2445 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2446 >                   Objects.equals(v2, rs[5].value));
2447 >
2448          checkCompletedNormally(f, v1);
2449          checkCompletedNormally(g, v2);
2450 <        }
1612 <    }
2450 >    }}
2451  
2452 <    public void testRunAfterBoth_actionFailed2() {
2452 >    /**
2453 >     * runAfterEither result completes normally after normal completion
2454 >     * of either source
2455 >     */
2456 >    public void testRunAfterEither_normalCompletion() {
2457          for (ExecutionMode m : ExecutionMode.values())
2458          for (Integer v1 : new Integer[] { 1, null })
2459 <        for (Integer v2 : new Integer[] { 2, null }) {
2460 <
2459 >        for (Integer v2 : new Integer[] { 2, null })
2460 >    {
2461          final CompletableFuture<Integer> f = new CompletableFuture<>();
2462          final CompletableFuture<Integer> g = new CompletableFuture<>();
2463 <        final FailingNoop r = new FailingNoop();
2464 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2463 >        final Noop[] rs = new Noop[6];
2464 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2465 >
2466 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2467 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2468 >        checkIncomplete(h0);
2469 >        checkIncomplete(h1);
2470 >        rs[0].assertNotInvoked();
2471 >        rs[1].assertNotInvoked();
2472 >        f.complete(v1);
2473 >        checkCompletedNormally(h0, null);
2474 >        checkCompletedNormally(h1, null);
2475 >        rs[0].assertInvoked();
2476 >        rs[1].assertInvoked();
2477 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2478 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2479 >        checkCompletedNormally(h2, null);
2480 >        checkCompletedNormally(h3, null);
2481 >        rs[2].assertInvoked();
2482 >        rs[3].assertInvoked();
2483  
2484          g.complete(v2);
1625        checkIncomplete(h);
1626        f.complete(v1);
2485  
2486 <        checkCompletedWithWrappedCFException(h);
2486 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2487 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2488 >
2489          checkCompletedNormally(f, v1);
2490          checkCompletedNormally(g, v2);
2491 <        }
2492 <    }
2491 >        checkCompletedNormally(h0, null);
2492 >        checkCompletedNormally(h1, null);
2493 >        checkCompletedNormally(h2, null);
2494 >        checkCompletedNormally(h3, null);
2495 >        checkCompletedNormally(h4, null);
2496 >        checkCompletedNormally(h5, null);
2497 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2498 >    }}
2499  
2500      /**
2501 <     * runAfterBoth result completes exceptionally if either source cancelled
2501 >     * runAfterEither result completes exceptionally after exceptional
2502 >     * completion of either source
2503       */
2504 <    public void testRunAfterBoth_sourceCancelled1() {
2504 >    public void testRunAfterEither_exceptionalCompletion() {
2505          for (ExecutionMode m : ExecutionMode.values())
2506 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2507 <        for (Integer v1 : new Integer[] { 1, null }) {
1641 <
2506 >        for (Integer v1 : new Integer[] { 1, null })
2507 >    {
2508          final CompletableFuture<Integer> f = new CompletableFuture<>();
2509          final CompletableFuture<Integer> g = new CompletableFuture<>();
2510 <        final Noop r = new Noop();
2511 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2510 >        final CFException ex = new CFException();
2511 >        final Noop[] rs = new Noop[6];
2512 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2513 >
2514 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2515 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2516 >        checkIncomplete(h0);
2517 >        checkIncomplete(h1);
2518 >        rs[0].assertNotInvoked();
2519 >        rs[1].assertNotInvoked();
2520 >        f.completeExceptionally(ex);
2521 >        checkCompletedWithWrappedException(h0, ex);
2522 >        checkCompletedWithWrappedException(h1, ex);
2523 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2524 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2525 >        checkCompletedWithWrappedException(h2, ex);
2526 >        checkCompletedWithWrappedException(h3, ex);
2527  
1647        assertTrue(f.cancel(mayInterruptIfRunning));
1648        checkIncomplete(h);
2528          g.complete(v1);
2529  
2530 <        checkCompletedWithWrappedCancellationException(h);
2531 <        checkCancelled(f);
2532 <        assertFalse(r.ran);
2533 <        checkCompletedNormally(g, v1);
2530 >        // unspecified behavior - both source completions available
2531 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2532 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2533 >        try {
2534 >            assertNull(h4.join());
2535 >            rs[4].assertInvoked();
2536 >        } catch (CompletionException ok) {
2537 >            checkCompletedWithWrappedException(h4, ex);
2538 >            rs[4].assertNotInvoked();
2539 >        }
2540 >        try {
2541 >            assertNull(h5.join());
2542 >            rs[5].assertInvoked();
2543 >        } catch (CompletionException ok) {
2544 >            checkCompletedWithWrappedException(h5, ex);
2545 >            rs[5].assertNotInvoked();
2546          }
1656    }
2547  
2548 <    public void testRunAfterBoth_sourceCancelled2() {
2549 <        for (ExecutionMode m : ExecutionMode.values())
2550 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2551 <        for (Integer v1 : new Integer[] { 1, null }) {
2548 >        checkCompletedExceptionally(f, ex);
2549 >        checkCompletedNormally(g, v1);
2550 >        checkCompletedWithWrappedException(h0, ex);
2551 >        checkCompletedWithWrappedException(h1, ex);
2552 >        checkCompletedWithWrappedException(h2, ex);
2553 >        checkCompletedWithWrappedException(h3, ex);
2554 >        checkCompletedWithWrappedException(h4, ex);
2555 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2556 >    }}
2557  
2558 +    public void testRunAfterEither_exceptionalCompletion2() {
2559 +        for (ExecutionMode m : ExecutionMode.values())
2560 +        for (boolean fFirst : new boolean[] { true, false })
2561 +        for (Integer v1 : new Integer[] { 1, null })
2562 +    {
2563          final CompletableFuture<Integer> f = new CompletableFuture<>();
2564          final CompletableFuture<Integer> g = new CompletableFuture<>();
2565 <        final Noop r = new Noop();
2566 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2567 <
1668 <        assertTrue(g.cancel(mayInterruptIfRunning));
1669 <        checkIncomplete(h);
1670 <        f.complete(v1);
2565 >        final CFException ex = new CFException();
2566 >        final Noop[] rs = new Noop[6];
2567 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2568  
2569 <        checkCompletedWithWrappedCancellationException(h);
2570 <        checkCancelled(g);
2571 <        assertFalse(r.ran);
2572 <        checkCompletedNormally(f, v1);
2569 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2570 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2571 >        if (fFirst) {
2572 >            f.complete(v1);
2573 >            g.completeExceptionally(ex);
2574 >        } else {
2575 >            g.completeExceptionally(ex);
2576 >            f.complete(v1);
2577          }
2578 <    }
2579 <
1679 <    public void testRunAfterBoth_sourceCancelled3() {
1680 <        for (ExecutionMode m : ExecutionMode.values())
1681 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1682 <        for (Integer v1 : new Integer[] { 1, null }) {
1683 <
1684 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1685 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1686 <        final Noop r = new Noop();
2578 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2579 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2580  
2581 <        assertTrue(g.cancel(mayInterruptIfRunning));
2582 <        f.complete(v1);
2583 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2581 >        // unspecified behavior - both source completions available
2582 >        try {
2583 >            assertEquals(null, h0.join());
2584 >            rs[0].assertInvoked();
2585 >        } catch (CompletionException ok) {
2586 >            checkCompletedWithWrappedException(h0, ex);
2587 >            rs[0].assertNotInvoked();
2588 >        }
2589 >        try {
2590 >            assertEquals(null, h1.join());
2591 >            rs[1].assertInvoked();
2592 >        } catch (CompletionException ok) {
2593 >            checkCompletedWithWrappedException(h1, ex);
2594 >            rs[1].assertNotInvoked();
2595 >        }
2596 >        try {
2597 >            assertEquals(null, h2.join());
2598 >            rs[2].assertInvoked();
2599 >        } catch (CompletionException ok) {
2600 >            checkCompletedWithWrappedException(h2, ex);
2601 >            rs[2].assertNotInvoked();
2602 >        }
2603 >        try {
2604 >            assertEquals(null, h3.join());
2605 >            rs[3].assertInvoked();
2606 >        } catch (CompletionException ok) {
2607 >            checkCompletedWithWrappedException(h3, ex);
2608 >            rs[3].assertNotInvoked();
2609 >        }
2610  
1692        checkCompletedWithWrappedCancellationException(h);
1693        checkCancelled(g);
1694        assertFalse(r.ran);
2611          checkCompletedNormally(f, v1);
2612 <        }
2613 <    }
2612 >        checkCompletedExceptionally(g, ex);
2613 >    }}
2614  
2615 <    public void testRunAfterBoth_sourceCancelled4() {
2615 >    /**
2616 >     * runAfterEither result completes exceptionally if either source cancelled
2617 >     */
2618 >    public void testRunAfterEither_sourceCancelled() {
2619          for (ExecutionMode m : ExecutionMode.values())
2620          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2621 <        for (Integer v1 : new Integer[] { 1, null }) {
2622 <
2621 >        for (Integer v1 : new Integer[] { 1, null })
2622 >    {
2623          final CompletableFuture<Integer> f = new CompletableFuture<>();
2624          final CompletableFuture<Integer> g = new CompletableFuture<>();
2625 <        final Noop r = new Noop();
2625 >        final Noop[] rs = new Noop[6];
2626 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2627 >
2628 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2629 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2630 >        checkIncomplete(h0);
2631 >        checkIncomplete(h1);
2632 >        rs[0].assertNotInvoked();
2633 >        rs[1].assertNotInvoked();
2634 >        f.cancel(mayInterruptIfRunning);
2635 >        checkCompletedWithWrappedCancellationException(h0);
2636 >        checkCompletedWithWrappedCancellationException(h1);
2637 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2638 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2639 >        checkCompletedWithWrappedCancellationException(h2);
2640 >        checkCompletedWithWrappedCancellationException(h3);
2641  
1708        assertTrue(f.cancel(mayInterruptIfRunning));
2642          g.complete(v1);
1710        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2643  
2644 <        checkCompletedWithWrappedCancellationException(h);
2645 <        checkCancelled(f);
2646 <        assertFalse(r.ran);
2647 <        checkCompletedNormally(g, v1);
2644 >        // unspecified behavior - both source completions available
2645 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2646 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2647 >        try {
2648 >            assertNull(h4.join());
2649 >            rs[4].assertInvoked();
2650 >        } catch (CompletionException ok) {
2651 >            checkCompletedWithWrappedCancellationException(h4);
2652 >            rs[4].assertNotInvoked();
2653 >        }
2654 >        try {
2655 >            assertNull(h5.join());
2656 >            rs[5].assertInvoked();
2657 >        } catch (CompletionException ok) {
2658 >            checkCompletedWithWrappedCancellationException(h5);
2659 >            rs[5].assertNotInvoked();
2660          }
1717    }
1718
1719    /**
1720     * applyToEither result completes normally after normal completion
1721     * of either source
1722     */
1723    public void testApplyToEither() {
1724        CompletableFuture<Integer> f = new CompletableFuture<>();
1725        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1726        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1727        f.complete(one);
1728        checkCompletedNormally(g, two);
1729        f2.complete(one);
1730        checkCompletedNormally(g, two);
2661  
2662 <        f = new CompletableFuture<>();
2663 <        f.complete(one);
2664 <        f2 = new CompletableFuture<>();
2665 <        g = f.applyToEither(f2, inc);
2666 <        checkCompletedNormally(g, two);
2667 <    }
2668 <
2669 <    /**
1740 <     * applyToEither result completes exceptionally after exceptional
1741 <     * completion of either source
1742 <     */
1743 <    public void testApplyToEither2() {
1744 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1745 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1747 <        f.completeExceptionally(new CFException());
1748 <        f2.complete(one);
1749 <        checkCompletedWithWrappedCFException(g);
1750 <
1751 <        f = new CompletableFuture<>();
1752 <        f2 = new CompletableFuture<>();
1753 <        f2.completeExceptionally(new CFException());
1754 <        g = f.applyToEither(f2, inc);
1755 <        checkCompletedWithWrappedCFException(g);
1756 <    }
1757 <
1758 <    /**
1759 <     * applyToEither result completes exceptionally if action does
1760 <     */
1761 <    public void testApplyToEither3() {
1762 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1763 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1764 <        FailingFunction r = new FailingFunction();
1765 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1766 <        f2.complete(two);
1767 <        checkCompletedWithWrappedCFException(g);
1768 <    }
1769 <
1770 <    /**
1771 <     * applyToEither result completes exceptionally if either source cancelled
1772 <     */
1773 <    public void testApplyToEither4() {
1774 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1775 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1776 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1777 <        assertTrue(f.cancel(true));
1778 <        checkCompletedWithWrappedCancellationException(g);
1779 <        f = new CompletableFuture<>();
1780 <        f2 = new CompletableFuture<>();
1781 <        assertTrue(f2.cancel(true));
1782 <        checkCompletedWithWrappedCancellationException(g);
1783 <    }
1784 <
1785 <    /**
1786 <     * acceptEither result completes normally after normal completion
1787 <     * of either source
1788 <     */
1789 <    public void testAcceptEither() {
1790 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1791 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1792 <        IncAction r = new IncAction();
1793 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1794 <        f.complete(one);
1795 <        checkCompletedNormally(g, null);
1796 <        f2.complete(one);
1797 <        checkCompletedNormally(g, null);
1798 <        assertEquals(r.value, 2);
1799 <
1800 <        r = new IncAction();
1801 <        f = new CompletableFuture<>();
1802 <        f.complete(one);
1803 <        f2 = new CompletableFuture<>();
1804 <        g = f.acceptEither(f2, r);
1805 <        checkCompletedNormally(g, null);
1806 <        assertEquals(r.value, 2);
1807 <    }
1808 <
1809 <    /**
1810 <     * acceptEither result completes exceptionally after exceptional
1811 <     * completion of either source
1812 <     */
1813 <    public void testAcceptEither2() {
1814 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1815 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1816 <        IncAction r = new IncAction();
1817 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1818 <        f.completeExceptionally(new CFException());
1819 <        f2.complete(one);
1820 <        checkCompletedWithWrappedCFException(g);
1821 <
1822 <        r = new IncAction();
1823 <        f = new CompletableFuture<>();
1824 <        f2 = new CompletableFuture<>();
1825 <        f2.completeExceptionally(new CFException());
1826 <        g = f.acceptEither(f2, r);
1827 <        checkCompletedWithWrappedCFException(g);
1828 <    }
1829 <
1830 <    /**
1831 <     * acceptEither result completes exceptionally if action does
1832 <     */
1833 <    public void testAcceptEither3() {
1834 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1835 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1836 <        FailingConsumer r = new FailingConsumer();
1837 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1838 <        f2.complete(two);
1839 <        checkCompletedWithWrappedCFException(g);
1840 <    }
1841 <
1842 <    /**
1843 <     * acceptEither result completes exceptionally if either source cancelled
1844 <     */
1845 <    public void testAcceptEither4() {
1846 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1847 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1848 <        IncAction r = new IncAction();
1849 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1850 <        assertTrue(f.cancel(true));
1851 <        checkCompletedWithWrappedCancellationException(g);
1852 <        f = new CompletableFuture<>();
1853 <        f2 = new CompletableFuture<>();
1854 <        assertTrue(f2.cancel(true));
1855 <        checkCompletedWithWrappedCancellationException(g);
1856 <    }
1857 <
1858 <    /**
1859 <     * runAfterEither result completes normally after normal completion
1860 <     * of either source
1861 <     */
1862 <    public void testRunAfterEither() {
1863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1864 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1865 <        Noop r = new Noop();
1866 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1867 <        f.complete(one);
1868 <        checkCompletedNormally(g, null);
1869 <        f2.complete(one);
1870 <        checkCompletedNormally(g, null);
1871 <        assertTrue(r.ran);
1872 <
1873 <        r = new Noop();
1874 <        f = new CompletableFuture<>();
1875 <        f.complete(one);
1876 <        f2 = new CompletableFuture<>();
1877 <        g = f.runAfterEither(f2, r);
1878 <        checkCompletedNormally(g, null);
1879 <        assertTrue(r.ran);
1880 <    }
1881 <
1882 <    /**
1883 <     * runAfterEither result completes exceptionally after exceptional
1884 <     * completion of either source
1885 <     */
1886 <    public void testRunAfterEither2() {
1887 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1888 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1889 <        Noop r = new Noop();
1890 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1891 <        f.completeExceptionally(new CFException());
1892 <        f2.complete(one);
1893 <        checkCompletedWithWrappedCFException(g);
1894 <
1895 <        r = new Noop();
1896 <        f = new CompletableFuture<>();
1897 <        f2 = new CompletableFuture<>();
1898 <        f2.completeExceptionally(new CFException());
1899 <        g = f.runAfterEither(f2, r);
1900 <        checkCompletedWithWrappedCFException(g);
1901 <    }
2662 >        checkCancelled(f);
2663 >        checkCompletedNormally(g, v1);
2664 >        checkCompletedWithWrappedCancellationException(h0);
2665 >        checkCompletedWithWrappedCancellationException(h1);
2666 >        checkCompletedWithWrappedCancellationException(h2);
2667 >        checkCompletedWithWrappedCancellationException(h3);
2668 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2669 >    }}
2670  
2671      /**
2672       * runAfterEither result completes exceptionally if action does
2673       */
2674 <    public void testRunAfterEither3() {
2675 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2676 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2677 <        FailingNoop r = new FailingNoop();
2678 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2679 <        f2.complete(two);
2680 <        checkCompletedWithWrappedCFException(g);
2681 <    }
2674 >    public void testRunAfterEither_actionFailed() {
2675 >        for (ExecutionMode m : ExecutionMode.values())
2676 >        for (Integer v1 : new Integer[] { 1, null })
2677 >        for (Integer v2 : new Integer[] { 2, null })
2678 >    {
2679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 >        final FailingRunnable[] rs = new FailingRunnable[6];
2682 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2683  
2684 <    /**
2685 <     * runAfterEither result completes exceptionally if either source cancelled
2686 <     */
2687 <    public void testRunAfterEither4() {
2688 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2689 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2690 <        Noop r = new Noop();
2691 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2692 <        assertTrue(f.cancel(true));
2693 <        checkCompletedWithWrappedCancellationException(g);
2694 <        f = new CompletableFuture<>();
2695 <        f2 = new CompletableFuture<>();
2696 <        assertTrue(f2.cancel(true));
2697 <        checkCompletedWithWrappedCancellationException(g);
2698 <    }
2684 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2685 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2686 >        f.complete(v1);
2687 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2688 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2689 >        checkCompletedWithWrappedCFException(h0);
2690 >        checkCompletedWithWrappedCFException(h1);
2691 >        checkCompletedWithWrappedCFException(h2);
2692 >        checkCompletedWithWrappedCFException(h3);
2693 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2694 >        g.complete(v2);
2695 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2696 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2697 >        checkCompletedWithWrappedCFException(h4);
2698 >        checkCompletedWithWrappedCFException(h5);
2699 >
2700 >        checkCompletedNormally(f, v1);
2701 >        checkCompletedNormally(g, v2);
2702 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2703 >    }}
2704  
2705      /**
2706       * thenCompose result completes normally after normal completion of source
2707       */
2708 <    public void testThenCompose() {
2709 <        CompletableFuture<Integer> f, g;
2710 <        CompletableFutureInc r;
2711 <
2712 <        f = new CompletableFuture<>();
2713 <        g = f.thenCompose(r = new CompletableFutureInc());
2714 <        f.complete(one);
2715 <        checkCompletedNormally(g, two);
2716 <        assertTrue(r.ran);
2708 >    public void testThenCompose_normalCompletion() {
2709 >        for (ExecutionMode m : ExecutionMode.values())
2710 >        for (boolean createIncomplete : new boolean[] { true, false })
2711 >        for (Integer v1 : new Integer[] { 1, null })
2712 >    {
2713 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2714 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2715 >        if (!createIncomplete) f.complete(v1);
2716 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2717 >        if (createIncomplete) f.complete(v1);
2718  
2719 <        f = new CompletableFuture<>();
2720 <        f.complete(one);
2721 <        g = f.thenCompose(r = new CompletableFutureInc());
2722 <        checkCompletedNormally(g, two);
1948 <        assertTrue(r.ran);
1949 <    }
2719 >        checkCompletedNormally(g, inc(v1));
2720 >        checkCompletedNormally(f, v1);
2721 >        r.assertValue(v1);
2722 >    }}
2723  
2724      /**
2725       * thenCompose result completes exceptionally after exceptional
2726       * completion of source
2727       */
2728 <    public void testThenCompose2() {
2729 <        CompletableFuture<Integer> f, g;
2730 <        CompletableFutureInc r;
2731 <
2732 <        f = new CompletableFuture<>();
2733 <        g = f.thenCompose(r = new CompletableFutureInc());
2734 <        f.completeExceptionally(new CFException());
2735 <        checkCompletedWithWrappedCFException(g);
2736 <
2737 <        f = new CompletableFuture<>();
2738 <        f.completeExceptionally(new CFException());
2739 <        g = f.thenCompose(r = new CompletableFutureInc());
2740 <        checkCompletedWithWrappedCFException(g);
2741 <    }
2728 >    public void testThenCompose_exceptionalCompletion() {
2729 >        for (ExecutionMode m : ExecutionMode.values())
2730 >        for (boolean createIncomplete : new boolean[] { true, false })
2731 >    {
2732 >        final CFException ex = new CFException();
2733 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2734 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2735 >        if (!createIncomplete) f.completeExceptionally(ex);
2736 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2737 >        if (createIncomplete) f.completeExceptionally(ex);
2738 >
2739 >        checkCompletedWithWrappedException(g, ex);
2740 >        checkCompletedExceptionally(f, ex);
2741 >        r.assertNotInvoked();
2742 >    }}
2743  
2744      /**
2745       * thenCompose result completes exceptionally if action does
2746       */
2747 <    public void testThenCompose3() {
2748 <        CompletableFuture<Integer> f, g;
2749 <        FailingCompletableFutureFunction r;
2750 <
2751 <        f = new CompletableFuture<>();
2752 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2753 <        f.complete(one);
2754 <        checkCompletedWithWrappedCFException(g);
2747 >    public void testThenCompose_actionFailed() {
2748 >        for (ExecutionMode m : ExecutionMode.values())
2749 >        for (boolean createIncomplete : new boolean[] { true, false })
2750 >        for (Integer v1 : new Integer[] { 1, null })
2751 >    {
2752 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2753 >        final FailingCompletableFutureFunction r
2754 >            = new FailingCompletableFutureFunction(m);
2755 >        if (!createIncomplete) f.complete(v1);
2756 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2757 >        if (createIncomplete) f.complete(v1);
2758  
1982        f = new CompletableFuture<>();
1983        f.complete(one);
1984        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2759          checkCompletedWithWrappedCFException(g);
2760 <    }
2760 >        checkCompletedNormally(f, v1);
2761 >    }}
2762  
2763      /**
2764       * thenCompose result completes exceptionally if source cancelled
2765       */
2766 <    public void testThenCompose4() {
2767 <        CompletableFuture<Integer> f, g;
2768 <        CompletableFutureInc r;
2769 <
2770 <        f = new CompletableFuture<>();
2771 <        g = f.thenCompose(r = new CompletableFutureInc());
2772 <        assertTrue(f.cancel(true));
2773 <        checkCompletedWithWrappedCancellationException(g);
2774 <
2775 <        f = new CompletableFuture<>();
2776 <        assertTrue(f.cancel(true));
2777 <        g = f.thenCompose(r = new CompletableFutureInc());
2778 <        checkCompletedWithWrappedCancellationException(g);
2004 <    }
2005 <
2006 <    // asyncs
2007 <
2008 <    /**
2009 <     * thenRunAsync result completes normally after normal completion of source
2010 <     */
2011 <    public void testThenRunAsync() {
2012 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2013 <        Noop r = new Noop();
2014 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2015 <        f.complete(null);
2016 <        checkCompletedNormally(g, null);
2017 <
2018 <        // reordered version
2019 <        f = new CompletableFuture<>();
2020 <        f.complete(null);
2021 <        r = new Noop();
2022 <        g = f.thenRunAsync(r);
2023 <        checkCompletedNormally(g, null);
2024 <    }
2025 <
2026 <    /**
2027 <     * thenRunAsync result completes exceptionally after exceptional
2028 <     * completion of source
2029 <     */
2030 <    public void testThenRunAsync2() {
2031 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2032 <        Noop r = new Noop();
2033 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2034 <        f.completeExceptionally(new CFException());
2035 <        try {
2036 <            g.join();
2037 <            shouldThrow();
2038 <        } catch (CompletionException success) {}
2039 <        checkCompletedWithWrappedCFException(g);
2040 <    }
2041 <
2042 <    /**
2043 <     * thenRunAsync result completes exceptionally if action does
2044 <     */
2045 <    public void testThenRunAsync3() {
2046 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2047 <        FailingNoop r = new FailingNoop();
2048 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2049 <        f.complete(null);
2050 <        checkCompletedWithWrappedCFException(g);
2051 <    }
2052 <
2053 <    /**
2054 <     * thenRunAsync result completes exceptionally if source cancelled
2055 <     */
2056 <    public void testThenRunAsync4() {
2057 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2058 <        Noop r = new Noop();
2059 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2060 <        assertTrue(f.cancel(true));
2061 <        checkCompletedWithWrappedCancellationException(g);
2062 <    }
2063 <
2064 <    /**
2065 <     * thenApplyAsync result completes normally after normal completion of source
2066 <     */
2067 <    public void testThenApplyAsync() {
2068 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2069 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2070 <        f.complete(one);
2071 <        checkCompletedNormally(g, two);
2072 <    }
2073 <
2074 <    /**
2075 <     * thenApplyAsync result completes exceptionally after exceptional
2076 <     * completion of source
2077 <     */
2078 <    public void testThenApplyAsync2() {
2079 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2080 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2081 <        f.completeExceptionally(new CFException());
2082 <        checkCompletedWithWrappedCFException(g);
2083 <    }
2084 <
2085 <    /**
2086 <     * thenApplyAsync result completes exceptionally if action does
2087 <     */
2088 <    public void testThenApplyAsync3() {
2089 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2090 <        FailingFunction r = new FailingFunction();
2091 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2092 <        f.complete(null);
2093 <        checkCompletedWithWrappedCFException(g);
2094 <    }
2095 <
2096 <    /**
2097 <     * thenApplyAsync result completes exceptionally if source cancelled
2098 <     */
2099 <    public void testThenApplyAsync4() {
2100 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2101 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2102 <        assertTrue(f.cancel(true));
2103 <        checkCompletedWithWrappedCancellationException(g);
2104 <    }
2105 <
2106 <    /**
2107 <     * thenAcceptAsync result completes normally after normal
2108 <     * completion of source
2109 <     */
2110 <    public void testThenAcceptAsync() {
2111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2112 <        IncAction r = new IncAction();
2113 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2114 <        f.complete(one);
2115 <        checkCompletedNormally(g, null);
2116 <        assertEquals(r.value, 2);
2117 <    }
2118 <
2119 <    /**
2120 <     * thenAcceptAsync result completes exceptionally after exceptional
2121 <     * completion of source
2122 <     */
2123 <    public void testThenAcceptAsync2() {
2124 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        IncAction r = new IncAction();
2126 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2127 <        f.completeExceptionally(new CFException());
2128 <        checkCompletedWithWrappedCFException(g);
2129 <    }
2130 <
2131 <    /**
2132 <     * thenAcceptAsync result completes exceptionally if action does
2133 <     */
2134 <    public void testThenAcceptAsync3() {
2135 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2136 <        FailingConsumer r = new FailingConsumer();
2137 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2138 <        f.complete(null);
2139 <        checkCompletedWithWrappedCFException(g);
2140 <    }
2141 <
2142 <    /**
2143 <     * thenAcceptAsync result completes exceptionally if source cancelled
2144 <     */
2145 <    public void testThenAcceptAsync4() {
2146 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2147 <        IncAction r = new IncAction();
2148 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2149 <        assertTrue(f.cancel(true));
2150 <        checkCompletedWithWrappedCancellationException(g);
2151 <    }
2152 <
2153 <    /**
2154 <     * applyToEitherAsync result completes normally after normal
2155 <     * completion of sources
2156 <     */
2157 <    public void testApplyToEitherAsync() {
2158 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2159 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2160 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2161 <        f.complete(one);
2162 <        checkCompletedNormally(g, two);
2163 <
2164 <        f = new CompletableFuture<>();
2165 <        f.complete(one);
2166 <        f2 = new CompletableFuture<>();
2167 <        g = f.applyToEitherAsync(f2, inc);
2168 <        checkCompletedNormally(g, two);
2169 <    }
2170 <
2171 <    /**
2172 <     * applyToEitherAsync result completes exceptionally after exceptional
2173 <     * completion of source
2174 <     */
2175 <    public void testApplyToEitherAsync2() {
2176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2177 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2178 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2179 <        f.completeExceptionally(new CFException());
2180 <        checkCompletedWithWrappedCFException(g);
2181 <
2182 <        f = new CompletableFuture<>();
2183 <        f2 = new CompletableFuture<>();
2184 <        f2.completeExceptionally(new CFException());
2185 <        g = f.applyToEitherAsync(f2, inc);
2186 <        f.complete(one);
2187 <        checkCompletedWithWrappedCFException(g);
2188 <    }
2189 <
2190 <    /**
2191 <     * applyToEitherAsync result completes exceptionally if action does
2192 <     */
2193 <    public void testApplyToEitherAsync3() {
2194 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2195 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2196 <        FailingFunction r = new FailingFunction();
2197 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2198 <        f.complete(one);
2199 <        checkCompletedWithWrappedCFException(g);
2200 <    }
2201 <
2202 <    /**
2203 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2204 <     */
2205 <    public void testApplyToEitherAsync4() {
2206 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2207 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2208 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2209 <        assertTrue(f.cancel(true));
2210 <        checkCompletedWithWrappedCancellationException(g);
2211 <
2212 <        f = new CompletableFuture<>();
2213 <        f2 = new CompletableFuture<>();
2214 <        assertTrue(f2.cancel(true));
2215 <        g = f.applyToEitherAsync(f2, inc);
2216 <        checkCompletedWithWrappedCancellationException(g);
2217 <    }
2218 <
2219 <    /**
2220 <     * acceptEitherAsync result completes normally after normal
2221 <     * completion of sources
2222 <     */
2223 <    public void testAcceptEitherAsync() {
2224 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2225 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226 <        IncAction r = new IncAction();
2227 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2228 <        f.complete(one);
2229 <        checkCompletedNormally(g, null);
2230 <        assertEquals(r.value, 2);
2231 <
2232 <        r = new IncAction();
2233 <        f = new CompletableFuture<>();
2234 <        f.complete(one);
2235 <        f2 = new CompletableFuture<>();
2236 <        g = f.acceptEitherAsync(f2, r);
2237 <        checkCompletedNormally(g, null);
2238 <        assertEquals(r.value, 2);
2239 <    }
2240 <
2241 <    /**
2242 <     * acceptEitherAsync result completes exceptionally after exceptional
2243 <     * completion of source
2244 <     */
2245 <    public void testAcceptEitherAsync2() {
2246 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2247 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248 <        IncAction r = new IncAction();
2249 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2250 <        f.completeExceptionally(new CFException());
2251 <        checkCompletedWithWrappedCFException(g);
2252 <
2253 <        r = new IncAction();
2254 <        f = new CompletableFuture<>();
2255 <        f2 = new CompletableFuture<>();
2256 <        f2.completeExceptionally(new CFException());
2257 <        g = f.acceptEitherAsync(f2, r);
2258 <        f.complete(one);
2259 <        checkCompletedWithWrappedCFException(g);
2260 <    }
2261 <
2262 <    /**
2263 <     * acceptEitherAsync result completes exceptionally if action does
2264 <     */
2265 <    public void testAcceptEitherAsync3() {
2266 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2267 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2268 <        FailingConsumer r = new FailingConsumer();
2269 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2270 <        f.complete(one);
2271 <        checkCompletedWithWrappedCFException(g);
2272 <    }
2273 <
2274 <    /**
2275 <     * acceptEitherAsync result completes exceptionally if either
2276 <     * source cancelled
2277 <     */
2278 <    public void testAcceptEitherAsync4() {
2279 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2280 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2281 <        IncAction r = new IncAction();
2282 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2283 <        assertTrue(f.cancel(true));
2284 <        checkCompletedWithWrappedCancellationException(g);
2285 <
2286 <        r = new IncAction();
2287 <        f = new CompletableFuture<>();
2288 <        f2 = new CompletableFuture<>();
2289 <        assertTrue(f2.cancel(true));
2290 <        g = f.acceptEitherAsync(f2, r);
2291 <        checkCompletedWithWrappedCancellationException(g);
2292 <    }
2293 <
2294 <    /**
2295 <     * runAfterEitherAsync result completes normally after normal
2296 <     * completion of sources
2297 <     */
2298 <    public void testRunAfterEitherAsync() {
2299 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2300 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2301 <        Noop r = new Noop();
2302 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2303 <        f.complete(one);
2304 <        checkCompletedNormally(g, null);
2305 <        assertTrue(r.ran);
2306 <
2307 <        r = new Noop();
2308 <        f = new CompletableFuture<>();
2309 <        f.complete(one);
2310 <        f2 = new CompletableFuture<>();
2311 <        g = f.runAfterEitherAsync(f2, r);
2312 <        checkCompletedNormally(g, null);
2313 <        assertTrue(r.ran);
2314 <    }
2315 <
2316 <    /**
2317 <     * runAfterEitherAsync result completes exceptionally after exceptional
2318 <     * completion of source
2319 <     */
2320 <    public void testRunAfterEitherAsync2() {
2321 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2322 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2323 <        Noop r = new Noop();
2324 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2325 <        f.completeExceptionally(new CFException());
2326 <        checkCompletedWithWrappedCFException(g);
2327 <
2328 <        r = new Noop();
2329 <        f = new CompletableFuture<>();
2330 <        f2 = new CompletableFuture<>();
2331 <        f2.completeExceptionally(new CFException());
2332 <        g = f.runAfterEitherAsync(f2, r);
2333 <        f.complete(one);
2334 <        checkCompletedWithWrappedCFException(g);
2335 <    }
2336 <
2337 <    /**
2338 <     * runAfterEitherAsync result completes exceptionally if action does
2339 <     */
2340 <    public void testRunAfterEitherAsync3() {
2341 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2342 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2343 <        FailingNoop r = new FailingNoop();
2344 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2345 <        f.complete(one);
2346 <        checkCompletedWithWrappedCFException(g);
2347 <    }
2348 <
2349 <    /**
2350 <     * runAfterEitherAsync result completes exceptionally if either
2351 <     * source cancelled
2352 <     */
2353 <    public void testRunAfterEitherAsync4() {
2354 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2355 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356 <        Noop r = new Noop();
2357 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2358 <        assertTrue(f.cancel(true));
2359 <        checkCompletedWithWrappedCancellationException(g);
2360 <
2361 <        r = new Noop();
2362 <        f = new CompletableFuture<>();
2363 <        f2 = new CompletableFuture<>();
2364 <        assertTrue(f2.cancel(true));
2365 <        g = f.runAfterEitherAsync(f2, r);
2366 <        checkCompletedWithWrappedCancellationException(g);
2367 <    }
2368 <
2369 <    /**
2370 <     * thenComposeAsync result completes normally after normal
2371 <     * completion of source
2372 <     */
2373 <    public void testThenComposeAsync() {
2374 <        CompletableFuture<Integer> f, g;
2375 <        CompletableFutureInc r;
2376 <
2377 <        f = new CompletableFuture<>();
2378 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2379 <        f.complete(one);
2380 <        checkCompletedNormally(g, two);
2381 <
2382 <        f = new CompletableFuture<>();
2383 <        f.complete(one);
2384 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2385 <        checkCompletedNormally(g, two);
2386 <    }
2387 <
2388 <    /**
2389 <     * thenComposeAsync result completes exceptionally after
2390 <     * exceptional completion of source
2391 <     */
2392 <    public void testThenComposeAsync2() {
2393 <        CompletableFuture<Integer> f, g;
2394 <        CompletableFutureInc r;
2395 <
2396 <        f = new CompletableFuture<>();
2397 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2398 <        f.completeExceptionally(new CFException());
2399 <        checkCompletedWithWrappedCFException(g);
2400 <        assertFalse(r.ran);
2401 <
2402 <        f = new CompletableFuture<>();
2403 <        f.completeExceptionally(new CFException());
2404 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2405 <        checkCompletedWithWrappedCFException(g);
2406 <        assertFalse(r.ran);
2407 <    }
2408 <
2409 <    /**
2410 <     * thenComposeAsync result completes exceptionally if action does
2411 <     */
2412 <    public void testThenComposeAsync3() {
2413 <        CompletableFuture<Integer> f, g;
2414 <        FailingCompletableFutureFunction r;
2415 <
2416 <        f = new CompletableFuture<>();
2417 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2418 <        f.complete(one);
2419 <        checkCompletedWithWrappedCFException(g);
2420 <
2421 <        f = new CompletableFuture<>();
2422 <        f.complete(one);
2423 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2424 <        checkCompletedWithWrappedCFException(g);
2425 <    }
2426 <
2427 <    /**
2428 <     * thenComposeAsync result completes exceptionally if source cancelled
2429 <     */
2430 <    public void testThenComposeAsync4() {
2431 <        CompletableFuture<Integer> f, g;
2432 <        CompletableFutureInc r;
2433 <
2434 <        f = new CompletableFuture<>();
2435 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2436 <        assertTrue(f.cancel(true));
2437 <        checkCompletedWithWrappedCancellationException(g);
2438 <
2439 <        f = new CompletableFuture<>();
2440 <        assertTrue(f.cancel(true));
2441 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2442 <        checkCompletedWithWrappedCancellationException(g);
2443 <    }
2444 <
2445 <    // async with explicit executors
2446 <
2447 <    /**
2448 <     * thenRunAsync result completes normally after normal completion of source
2449 <     */
2450 <    public void testThenRunAsyncE() {
2451 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2452 <        Noop r = new Noop();
2453 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2454 <        f.complete(null);
2455 <        checkCompletedNormally(g, null);
2456 <
2457 <        // reordered version
2458 <        f = new CompletableFuture<>();
2459 <        f.complete(null);
2460 <        r = new Noop();
2461 <        g = f.thenRunAsync(r, new ThreadExecutor());
2462 <        checkCompletedNormally(g, null);
2463 <    }
2464 <
2465 <    /**
2466 <     * thenRunAsync result completes exceptionally after exceptional
2467 <     * completion of source
2468 <     */
2469 <    public void testThenRunAsync2E() {
2470 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2471 <        Noop r = new Noop();
2472 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2473 <        f.completeExceptionally(new CFException());
2474 <        try {
2475 <            g.join();
2476 <            shouldThrow();
2477 <        } catch (CompletionException success) {}
2478 <        checkCompletedWithWrappedCFException(g);
2479 <    }
2480 <
2481 <    /**
2482 <     * thenRunAsync result completes exceptionally if action does
2483 <     */
2484 <    public void testThenRunAsync3E() {
2485 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2486 <        FailingNoop r = new FailingNoop();
2487 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2488 <        f.complete(null);
2489 <        checkCompletedWithWrappedCFException(g);
2490 <    }
2491 <
2492 <    /**
2493 <     * thenRunAsync result completes exceptionally if source cancelled
2494 <     */
2495 <    public void testThenRunAsync4E() {
2496 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2497 <        Noop r = new Noop();
2498 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2499 <        assertTrue(f.cancel(true));
2500 <        checkCompletedWithWrappedCancellationException(g);
2501 <    }
2502 <
2503 <    /**
2504 <     * thenApplyAsync result completes normally after normal completion of source
2505 <     */
2506 <    public void testThenApplyAsyncE() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2508 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2509 <        f.complete(one);
2510 <        checkCompletedNormally(g, two);
2511 <    }
2512 <
2513 <    /**
2514 <     * thenApplyAsync result completes exceptionally after exceptional
2515 <     * completion of source
2516 <     */
2517 <    public void testThenApplyAsync2E() {
2518 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2519 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2520 <        f.completeExceptionally(new CFException());
2521 <        checkCompletedWithWrappedCFException(g);
2522 <    }
2523 <
2524 <    /**
2525 <     * thenApplyAsync result completes exceptionally if action does
2526 <     */
2527 <    public void testThenApplyAsync3E() {
2528 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2529 <        FailingFunction r = new FailingFunction();
2530 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2531 <        f.complete(null);
2532 <        checkCompletedWithWrappedCFException(g);
2533 <    }
2534 <
2535 <    /**
2536 <     * thenApplyAsync result completes exceptionally if source cancelled
2537 <     */
2538 <    public void testThenApplyAsync4E() {
2539 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2540 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2541 <        assertTrue(f.cancel(true));
2542 <        checkCompletedWithWrappedCancellationException(g);
2543 <    }
2544 <
2545 <    /**
2546 <     * thenAcceptAsync result completes normally after normal
2547 <     * completion of source
2548 <     */
2549 <    public void testThenAcceptAsyncE() {
2550 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2551 <        IncAction r = new IncAction();
2552 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2553 <        f.complete(one);
2554 <        checkCompletedNormally(g, null);
2555 <        assertEquals(r.value, 2);
2556 <    }
2557 <
2558 <    /**
2559 <     * thenAcceptAsync result completes exceptionally after exceptional
2560 <     * completion of source
2561 <     */
2562 <    public void testThenAcceptAsync2E() {
2563 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2564 <        IncAction r = new IncAction();
2565 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2566 <        f.completeExceptionally(new CFException());
2567 <        checkCompletedWithWrappedCFException(g);
2568 <    }
2569 <
2570 <    /**
2571 <     * thenAcceptAsync result completes exceptionally if action does
2572 <     */
2573 <    public void testThenAcceptAsync3E() {
2574 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2575 <        FailingConsumer r = new FailingConsumer();
2576 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2577 <        f.complete(null);
2578 <        checkCompletedWithWrappedCFException(g);
2579 <    }
2580 <
2581 <    /**
2582 <     * thenAcceptAsync result completes exceptionally if source cancelled
2583 <     */
2584 <    public void testThenAcceptAsync4E() {
2585 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2586 <        IncAction r = new IncAction();
2587 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2588 <        assertTrue(f.cancel(true));
2589 <        checkCompletedWithWrappedCancellationException(g);
2590 <    }
2591 <
2592 <    /**
2593 <     * applyToEitherAsync result completes normally after normal
2594 <     * completion of sources
2595 <     */
2596 <    public void testApplyToEitherAsyncE() {
2597 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2598 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2599 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2600 <        f.complete(one);
2601 <        checkCompletedNormally(g, two);
2602 <
2603 <        f = new CompletableFuture<>();
2604 <        f.complete(one);
2605 <        f2 = new CompletableFuture<>();
2606 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2607 <        checkCompletedNormally(g, two);
2608 <    }
2609 <
2610 <    /**
2611 <     * applyToEitherAsync result completes exceptionally after exceptional
2612 <     * completion of source
2613 <     */
2614 <    public void testApplyToEitherAsync2E() {
2615 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2616 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2617 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618 <        f.completeExceptionally(new CFException());
2619 <        checkCompletedWithWrappedCFException(g);
2620 <
2621 <        f = new CompletableFuture<>();
2622 <        f2 = new CompletableFuture<>();
2623 <        f2.completeExceptionally(new CFException());
2624 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625 <        f.complete(one);
2626 <        checkCompletedWithWrappedCFException(g);
2627 <    }
2628 <
2629 <    /**
2630 <     * applyToEitherAsync result completes exceptionally if action does
2631 <     */
2632 <    public void testApplyToEitherAsync3E() {
2633 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2634 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635 <        FailingFunction r = new FailingFunction();
2636 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2637 <        f.complete(one);
2638 <        checkCompletedWithWrappedCFException(g);
2639 <    }
2640 <
2641 <    /**
2642 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2643 <     */
2644 <    public void testApplyToEitherAsync4E() {
2645 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2646 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2647 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2648 <        assertTrue(f.cancel(true));
2649 <        checkCompletedWithWrappedCancellationException(g);
2650 <
2651 <        f = new CompletableFuture<>();
2652 <        f2 = new CompletableFuture<>();
2653 <        assertTrue(f2.cancel(true));
2654 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2655 <        checkCompletedWithWrappedCancellationException(g);
2656 <    }
2657 <
2658 <    /**
2659 <     * acceptEitherAsync result completes normally after normal
2660 <     * completion of sources
2661 <     */
2662 <    public void testAcceptEitherAsyncE() {
2663 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2664 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2665 <        IncAction r = new IncAction();
2666 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2667 <        f.complete(one);
2668 <        checkCompletedNormally(g, null);
2669 <        assertEquals(r.value, 2);
2670 <
2671 <        r = new IncAction();
2672 <        f = new CompletableFuture<>();
2673 <        f.complete(one);
2674 <        f2 = new CompletableFuture<>();
2675 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2676 <        checkCompletedNormally(g, null);
2677 <        assertEquals(r.value, 2);
2678 <    }
2679 <
2680 <    /**
2681 <     * acceptEitherAsync result completes exceptionally after exceptional
2682 <     * completion of source
2683 <     */
2684 <    public void testAcceptEitherAsync2E() {
2685 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2686 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2687 <        IncAction r = new IncAction();
2688 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2689 <        f.completeExceptionally(new CFException());
2690 <        checkCompletedWithWrappedCFException(g);
2691 <
2692 <        r = new IncAction();
2693 <        f = new CompletableFuture<>();
2694 <        f2 = new CompletableFuture<>();
2695 <        f2.completeExceptionally(new CFException());
2696 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2697 <        f.complete(one);
2698 <        checkCompletedWithWrappedCFException(g);
2699 <    }
2700 <
2701 <    /**
2702 <     * acceptEitherAsync result completes exceptionally if action does
2703 <     */
2704 <    public void testAcceptEitherAsync3E() {
2705 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2706 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2707 <        FailingConsumer r = new FailingConsumer();
2708 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2709 <        f.complete(one);
2710 <        checkCompletedWithWrappedCFException(g);
2711 <    }
2712 <
2713 <    /**
2714 <     * acceptEitherAsync result completes exceptionally if either
2715 <     * source cancelled
2716 <     */
2717 <    public void testAcceptEitherAsync4E() {
2718 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2719 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2720 <        IncAction r = new IncAction();
2721 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722 <        assertTrue(f.cancel(true));
2723 <        checkCompletedWithWrappedCancellationException(g);
2724 <
2725 <        r = new IncAction();
2726 <        f = new CompletableFuture<>();
2727 <        f2 = new CompletableFuture<>();
2728 <        assertTrue(f2.cancel(true));
2729 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2730 <        checkCompletedWithWrappedCancellationException(g);
2731 <    }
2732 <
2733 <    /**
2734 <     * runAfterEitherAsync result completes normally after normal
2735 <     * completion of sources
2736 <     */
2737 <    public void testRunAfterEitherAsyncE() {
2738 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2739 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740 <        Noop r = new Noop();
2741 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2742 <        f.complete(one);
2743 <        checkCompletedNormally(g, null);
2744 <        assertTrue(r.ran);
2745 <
2746 <        r = new Noop();
2747 <        f = new CompletableFuture<>();
2748 <        f.complete(one);
2749 <        f2 = new CompletableFuture<>();
2750 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2751 <        checkCompletedNormally(g, null);
2752 <        assertTrue(r.ran);
2753 <    }
2754 <
2755 <    /**
2756 <     * runAfterEitherAsync result completes exceptionally after exceptional
2757 <     * completion of source
2758 <     */
2759 <    public void testRunAfterEitherAsync2E() {
2760 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2761 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2762 <        Noop r = new Noop();
2763 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2764 <        f.completeExceptionally(new CFException());
2765 <        checkCompletedWithWrappedCFException(g);
2766 <
2767 <        r = new Noop();
2768 <        f = new CompletableFuture<>();
2769 <        f2 = new CompletableFuture<>();
2770 <        f2.completeExceptionally(new CFException());
2771 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2772 <        f.complete(one);
2773 <        checkCompletedWithWrappedCFException(g);
2774 <    }
2775 <
2776 <    /**
2777 <     * runAfterEitherAsync result completes exceptionally if action does
2778 <     */
2779 <    public void testRunAfterEitherAsync3E() {
2780 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2781 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2782 <        FailingNoop r = new FailingNoop();
2783 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2784 <        f.complete(one);
2785 <        checkCompletedWithWrappedCFException(g);
2786 <    }
2787 <
2788 <    /**
2789 <     * runAfterEitherAsync result completes exceptionally if either
2790 <     * source cancelled
2791 <     */
2792 <    public void testRunAfterEitherAsync4E() {
2793 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2794 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2795 <        Noop r = new Noop();
2796 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797 <        assertTrue(f.cancel(true));
2798 <        checkCompletedWithWrappedCancellationException(g);
2799 <
2800 <        r = new Noop();
2801 <        f = new CompletableFuture<>();
2802 <        f2 = new CompletableFuture<>();
2803 <        assertTrue(f2.cancel(true));
2804 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2805 <        checkCompletedWithWrappedCancellationException(g);
2806 <    }
2807 <
2808 <    /**
2809 <     * thenComposeAsync result completes normally after normal
2810 <     * completion of source
2811 <     */
2812 <    public void testThenComposeAsyncE() {
2813 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2814 <        CompletableFutureInc r = new CompletableFutureInc();
2815 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2816 <        f.complete(one);
2817 <        checkCompletedNormally(g, two);
2818 <    }
2819 <
2820 <    /**
2821 <     * thenComposeAsync result completes exceptionally after
2822 <     * exceptional completion of source
2823 <     */
2824 <    public void testThenComposeAsync2E() {
2825 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2826 <        CompletableFutureInc r = new CompletableFutureInc();
2827 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2828 <        f.completeExceptionally(new CFException());
2829 <        checkCompletedWithWrappedCFException(g);
2830 <    }
2831 <
2832 <    /**
2833 <     * thenComposeAsync result completes exceptionally if action does
2834 <     */
2835 <    public void testThenComposeAsync3E() {
2836 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2837 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2838 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2839 <        f.complete(one);
2840 <        checkCompletedWithWrappedCFException(g);
2841 <    }
2766 >    public void testThenCompose_sourceCancelled() {
2767 >        for (ExecutionMode m : ExecutionMode.values())
2768 >        for (boolean createIncomplete : new boolean[] { true, false })
2769 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2770 >    {
2771 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2772 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2773 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2774 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2775 >        if (createIncomplete) {
2776 >            checkIncomplete(g);
2777 >            assertTrue(f.cancel(mayInterruptIfRunning));
2778 >        }
2779  
2843    /**
2844     * thenComposeAsync result completes exceptionally if source cancelled
2845     */
2846    public void testThenComposeAsync4E() {
2847        CompletableFuture<Integer> f = new CompletableFuture<>();
2848        CompletableFutureInc r = new CompletableFutureInc();
2849        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2850        assertTrue(f.cancel(true));
2780          checkCompletedWithWrappedCancellationException(g);
2781 <    }
2781 >        checkCancelled(f);
2782 >    }}
2783  
2784      // other static methods
2785  
# Line 2868 | Line 2798 | public class CompletableFutureTest exten
2798       */
2799      public void testAllOf_normal() throws Exception {
2800          for (int k = 1; k < 20; ++k) {
2801 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2801 >            CompletableFuture<Integer>[] fs
2802 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2803              for (int i = 0; i < k; ++i)
2804                  fs[i] = new CompletableFuture<>();
2805              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 2882 | Line 2813 | public class CompletableFutureTest exten
2813          }
2814      }
2815  
2816 +    public void testAllOf_backwards() throws Exception {
2817 +        for (int k = 1; k < 20; ++k) {
2818 +            CompletableFuture<Integer>[] fs
2819 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2820 +            for (int i = 0; i < k; ++i)
2821 +                fs[i] = new CompletableFuture<>();
2822 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2823 +            for (int i = k - 1; i >= 0; i--) {
2824 +                checkIncomplete(f);
2825 +                checkIncomplete(CompletableFuture.allOf(fs));
2826 +                fs[i].complete(one);
2827 +            }
2828 +            checkCompletedNormally(f, null);
2829 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2830 +        }
2831 +    }
2832 +
2833      /**
2834       * anyOf(no component futures) returns an incomplete future
2835       */
# Line 2940 | Line 2888 | public class CompletableFutureTest exten
2888          Runnable[] throwingActions = {
2889              () -> CompletableFuture.supplyAsync(null),
2890              () -> CompletableFuture.supplyAsync(null, exec),
2891 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2891 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2892  
2893              () -> CompletableFuture.runAsync(null),
2894              () -> CompletableFuture.runAsync(null, exec),
# Line 3013 | Line 2961 | public class CompletableFutureTest exten
2961  
2962              () -> f.thenCompose(null),
2963              () -> f.thenComposeAsync(null),
2964 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2964 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2965              () -> f.thenComposeAsync(null, exec),
2966  
2967              () -> f.exceptionally(null),
# Line 3045 | Line 2993 | public class CompletableFutureTest exten
2993          assertSame(f, f.toCompletableFuture());
2994      }
2995  
3048    /**
3049     * whenComplete action executes on normal completion, propagating
3050     * source result.
3051     */
3052    public void testWhenComplete1() {
3053        final AtomicInteger a = new AtomicInteger();
3054        CompletableFuture<Integer> f = new CompletableFuture<>();
3055        CompletableFuture<Integer> g =
3056            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3057        f.complete(three);
3058        checkCompletedNormally(f, three);
3059        checkCompletedNormally(g, three);
3060        assertEquals(a.get(), 1);
3061    }
3062
3063    /**
3064     * whenComplete action executes on exceptional completion, propagating
3065     * source result.
3066     */
3067    public void testWhenComplete2() {
3068        final AtomicInteger a = new AtomicInteger();
3069        CompletableFuture<Integer> f = new CompletableFuture<>();
3070        CompletableFuture<Integer> g =
3071            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3072        f.completeExceptionally(new CFException());
3073        assertTrue(f.isCompletedExceptionally());
3074        assertTrue(g.isCompletedExceptionally());
3075        assertEquals(a.get(), 1);
3076    }
3077
3078    /**
3079     * If a whenComplete action throws an exception when triggered by
3080     * a normal completion, it completes exceptionally
3081     */
3082    public void testWhenComplete3() {
3083        CompletableFuture<Integer> f = new CompletableFuture<>();
3084        CompletableFuture<Integer> g =
3085            f.whenComplete((Integer x, Throwable t) ->
3086                           { throw new CFException(); } );
3087        f.complete(three);
3088        checkCompletedNormally(f, three);
3089        assertTrue(g.isCompletedExceptionally());
3090        checkCompletedWithWrappedCFException(g);
3091    }
3092
3093    /**
3094     * whenCompleteAsync action executes on normal completion, propagating
3095     * source result.
3096     */
3097    public void testWhenCompleteAsync1() {
3098        final AtomicInteger a = new AtomicInteger();
3099        CompletableFuture<Integer> f = new CompletableFuture<>();
3100        CompletableFuture<Integer> g =
3101            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3102        f.complete(three);
3103        checkCompletedNormally(f, three);
3104        checkCompletedNormally(g, three);
3105        assertEquals(a.get(), 1);
3106    }
3107
3108    /**
3109     * whenCompleteAsync action executes on exceptional completion, propagating
3110     * source result.
3111     */
3112    public void testWhenCompleteAsync2() {
3113        final AtomicInteger a = new AtomicInteger();
3114        CompletableFuture<Integer> f = new CompletableFuture<>();
3115        CompletableFuture<Integer> g =
3116            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3117        f.completeExceptionally(new CFException());
3118        checkCompletedWithWrappedCFException(f);
3119        checkCompletedWithWrappedCFException(g);
3120    }
3121
3122    /**
3123     * If a whenCompleteAsync action throws an exception when
3124     * triggered by a normal completion, it completes exceptionally
3125     */
3126    public void testWhenCompleteAsync3() {
3127        CompletableFuture<Integer> f = new CompletableFuture<>();
3128        CompletableFuture<Integer> g =
3129            f.whenCompleteAsync((Integer x, Throwable t) ->
3130                           { throw new CFException(); } );
3131        f.complete(three);
3132        checkCompletedNormally(f, three);
3133        checkCompletedWithWrappedCFException(g);
3134    }
3135
3136    /**
3137     * whenCompleteAsync action executes on normal completion, propagating
3138     * source result.
3139     */
3140    public void testWhenCompleteAsync1e() {
3141        final AtomicInteger a = new AtomicInteger();
3142        ThreadExecutor exec = new ThreadExecutor();
3143        CompletableFuture<Integer> f = new CompletableFuture<>();
3144        CompletableFuture<Integer> g =
3145            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3146                                exec);
3147        f.complete(three);
3148        checkCompletedNormally(f, three);
3149        checkCompletedNormally(g, three);
3150        assertEquals(a.get(), 1);
3151    }
3152
3153    /**
3154     * whenCompleteAsync action executes on exceptional completion, propagating
3155     * source result.
3156     */
3157    public void testWhenCompleteAsync2e() {
3158        final AtomicInteger a = new AtomicInteger();
3159        ThreadExecutor exec = new ThreadExecutor();
3160        CompletableFuture<Integer> f = new CompletableFuture<>();
3161        CompletableFuture<Integer> g =
3162            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3163                                exec);
3164        f.completeExceptionally(new CFException());
3165        checkCompletedWithWrappedCFException(f);
3166        checkCompletedWithWrappedCFException(g);
3167    }
3168
3169    /**
3170     * If a whenCompleteAsync action throws an exception when triggered
3171     * by a normal completion, it completes exceptionally
3172     */
3173    public void testWhenCompleteAsync3e() {
3174        ThreadExecutor exec = new ThreadExecutor();
3175        CompletableFuture<Integer> f = new CompletableFuture<>();
3176        CompletableFuture<Integer> g =
3177            f.whenCompleteAsync((Integer x, Throwable t) ->
3178                                { throw new CFException(); },
3179                                exec);
3180        f.complete(three);
3181        checkCompletedNormally(f, three);
3182        checkCompletedWithWrappedCFException(g);
3183    }
3184
3185    /**
3186     * handleAsync action completes normally with function value on
3187     * either normal or exceptional completion of source
3188     */
3189    public void testHandleAsync() {
3190        CompletableFuture<Integer> f, g;
3191        IntegerHandler r;
3192
3193        f = new CompletableFuture<>();
3194        g = f.handleAsync(r = new IntegerHandler());
3195        assertFalse(r.ran);
3196        f.completeExceptionally(new CFException());
3197        checkCompletedWithWrappedCFException(f);
3198        checkCompletedNormally(g, three);
3199        assertTrue(r.ran);
3200
3201        f = new CompletableFuture<>();
3202        g = f.handleAsync(r = new IntegerHandler());
3203        assertFalse(r.ran);
3204        f.completeExceptionally(new CFException());
3205        checkCompletedWithWrappedCFException(f);
3206        checkCompletedNormally(g, three);
3207        assertTrue(r.ran);
3208
3209        f = new CompletableFuture<>();
3210        g = f.handleAsync(r = new IntegerHandler());
3211        assertFalse(r.ran);
3212        f.complete(one);
3213        checkCompletedNormally(f, one);
3214        checkCompletedNormally(g, two);
3215        assertTrue(r.ran);
3216
3217        f = new CompletableFuture<>();
3218        g = f.handleAsync(r = new IntegerHandler());
3219        assertFalse(r.ran);
3220        f.complete(one);
3221        checkCompletedNormally(f, one);
3222        checkCompletedNormally(g, two);
3223        assertTrue(r.ran);
3224    }
3225
3226    /**
3227     * handleAsync action with Executor completes normally with
3228     * function value on either normal or exceptional completion of
3229     * source
3230     */
3231    public void testHandleAsync2() {
3232        CompletableFuture<Integer> f, g;
3233        ThreadExecutor exec = new ThreadExecutor();
3234        IntegerHandler r;
3235
3236        f = new CompletableFuture<>();
3237        g = f.handleAsync(r = new IntegerHandler(), exec);
3238        assertFalse(r.ran);
3239        f.completeExceptionally(new CFException());
3240        checkCompletedWithWrappedCFException(f);
3241        checkCompletedNormally(g, three);
3242        assertTrue(r.ran);
3243
3244        f = new CompletableFuture<>();
3245        g = f.handleAsync(r = new IntegerHandler(), exec);
3246        assertFalse(r.ran);
3247        f.completeExceptionally(new CFException());
3248        checkCompletedWithWrappedCFException(f);
3249        checkCompletedNormally(g, three);
3250        assertTrue(r.ran);
3251
3252        f = new CompletableFuture<>();
3253        g = f.handleAsync(r = new IntegerHandler(), exec);
3254        assertFalse(r.ran);
3255        f.complete(one);
3256        checkCompletedNormally(f, one);
3257        checkCompletedNormally(g, two);
3258        assertTrue(r.ran);
3259
3260        f = new CompletableFuture<>();
3261        g = f.handleAsync(r = new IntegerHandler(), exec);
3262        assertFalse(r.ran);
3263        f.complete(one);
3264        checkCompletedNormally(f, one);
3265        checkCompletedNormally(g, two);
3266        assertTrue(r.ran);
3267    }
3268
2996   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines