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.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC vs.
Revision 1.61 by jsr166, Wed Jun 4 04:34:49 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 247 | Line 249 | public class CompletableFutureTest exten
249          f = new CompletableFuture<>();
250          f.obtrudeValue(three);
251          checkCompletedNormally(f, three);
252 +        f.obtrudeValue(null);
253 +        checkCompletedNormally(f, null);
254          f = new CompletableFuture<>();
255          f.completeExceptionally(new CFException());
256          f.obtrudeValue(four);
# Line 279 | Line 283 | public class CompletableFutureTest exten
283       */
284      public void testGetNumberOfDependents() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286 <        assertEquals(f.getNumberOfDependents(), 0);
287 <        CompletableFuture g = f.thenRun(new Noop());
288 <        assertEquals(f.getNumberOfDependents(), 1);
289 <        assertEquals(g.getNumberOfDependents(), 0);
290 <        CompletableFuture h = f.thenRun(new Noop());
291 <        assertEquals(f.getNumberOfDependents(), 2);
286 >        assertEquals(0, f.getNumberOfDependents());
287 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
288 >        assertEquals(1, f.getNumberOfDependents());
289 >        assertEquals(0, g.getNumberOfDependents());
290 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
291 >        assertEquals(2, f.getNumberOfDependents());
292          f.complete(1);
293          checkCompletedNormally(g, null);
294 <        assertEquals(f.getNumberOfDependents(), 0);
295 <        assertEquals(g.getNumberOfDependents(), 0);
294 >        assertEquals(0, f.getNumberOfDependents());
295 >        assertEquals(0, g.getNumberOfDependents());
296      }
297  
298      /**
# Line 316 | Line 320 | public class CompletableFutureTest exten
320          checkCompletedNormally(f, "test");
321      }
322  
323 <    // Choose non-commutative actions for better coverage
324 <
325 <    // A non-commutative function that handles and produces null values as well.
326 <    static Integer subtract(Integer x, Integer y) {
327 <        return (x == null && y == null) ? null :
328 <            ((x == null) ? 42 : x.intValue())
329 <            - ((y == null) ? 99 : y.intValue());
323 >    static final class IntegerSupplier implements Supplier<Integer> {
324 >        final ExecutionMode m;
325 >        int invocationCount = 0;
326 >        final Integer value;
327 >        IntegerSupplier(ExecutionMode m, Integer value) {
328 >            this.m = m;
329 >            this.value = value;
330 >        }
331 >        public Integer get() {
332 >            m.checkExecutionMode();
333 >            invocationCount++;
334 >            return value;
335 >        }
336      }
337  
338      // A function that handles and produces null values as well.
# Line 330 | Line 340 | public class CompletableFutureTest exten
340          return (x == null) ? null : x + 1;
341      }
342  
333    static final Supplier<Integer> supplyOne =
334        () -> Integer.valueOf(1);
335    static final Function<Integer, Integer> inc =
336        (Integer x) -> Integer.valueOf(x.intValue() + 1);
337    static final BiFunction<Integer, Integer, Integer> subtract =
338        (Integer x, Integer y) -> subtract(x, y);
343      static final class IncAction implements Consumer<Integer> {
344          int invocationCount = 0;
345          Integer value;
342        public boolean ran() { return invocationCount == 1; }
346          public void accept(Integer x) {
347              invocationCount++;
348              value = inc(x);
349          }
350      }
351      static final class IncFunction implements Function<Integer,Integer> {
352 +        final ExecutionMode m;
353          int invocationCount = 0;
354          Integer value;
355 <        public boolean ran() { return invocationCount == 1; }
355 >        IncFunction(ExecutionMode m) { this.m = m; }
356          public Integer apply(Integer x) {
357 +            m.checkExecutionMode();
358              invocationCount++;
359              return value = inc(x);
360          }
361      }
362 +
363 +    // Choose non-commutative actions for better coverage
364 +    // A non-commutative function that handles and produces null values as well.
365 +    static Integer subtract(Integer x, Integer y) {
366 +        return (x == null && y == null) ? null :
367 +            ((x == null) ? 42 : x.intValue())
368 +            - ((y == null) ? 99 : y.intValue());
369 +    }
370 +
371      static final class SubtractAction implements BiConsumer<Integer, Integer> {
372 +        final ExecutionMode m;
373          int invocationCount = 0;
374          Integer value;
375          // Check this action was invoked exactly once when result is computed.
376 <        public boolean ran() { return invocationCount == 1; }
376 >        SubtractAction(ExecutionMode m) { this.m = m; }
377          public void accept(Integer x, Integer y) {
378 +            m.checkExecutionMode();
379              invocationCount++;
380              value = subtract(x, y);
381          }
382      }
383      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
384 +        final ExecutionMode m;
385          int invocationCount = 0;
386          Integer value;
387          // Check this action was invoked exactly once when result is computed.
388 <        public boolean ran() { return invocationCount == 1; }
388 >        SubtractFunction(ExecutionMode m) { this.m = m; }
389          public Integer apply(Integer x, Integer y) {
390 +            m.checkExecutionMode();
391              invocationCount++;
392              return value = subtract(x, y);
393          }
394      }
395 +
396      static final class Noop implements Runnable {
397 +        final ExecutionMode m;
398          int invocationCount = 0;
399 <        boolean ran;
399 >        Noop(ExecutionMode m) { this.m = m; }
400          public void run() {
401 +            m.checkExecutionMode();
402              invocationCount++;
382            ran = true;
403          }
404      }
405  
406      static final class FailingSupplier implements Supplier<Integer> {
407 <        boolean ran;
408 <        public Integer get() { ran = true; throw new CFException(); }
407 >        final ExecutionMode m;
408 >        int invocationCount = 0;
409 >        FailingSupplier(ExecutionMode m) { this.m = m; }
410 >        public Integer get() {
411 >            m.checkExecutionMode();
412 >            invocationCount++;
413 >            throw new CFException();
414 >        }
415      }
416      static final class FailingConsumer implements Consumer<Integer> {
417 <        boolean ran;
418 <        public void accept(Integer x) { ran = true; throw new CFException(); }
417 >        final ExecutionMode m;
418 >        int invocationCount = 0;
419 >        FailingConsumer(ExecutionMode m) { this.m = m; }
420 >        public void accept(Integer x) {
421 >            m.checkExecutionMode();
422 >            invocationCount++;
423 >            throw new CFException();
424 >        }
425      }
426      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
427 <        boolean ran;
428 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
427 >        final ExecutionMode m;
428 >        int invocationCount = 0;
429 >        FailingBiConsumer(ExecutionMode m) { this.m = m; }
430 >        public void accept(Integer x, Integer y) {
431 >            m.checkExecutionMode();
432 >            invocationCount++;
433 >            throw new CFException();
434 >        }
435      }
436      static final class FailingFunction implements Function<Integer, Integer> {
437 <        boolean ran;
438 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
437 >        final ExecutionMode m;
438 >        int invocationCount = 0;
439 >        FailingFunction(ExecutionMode m) { this.m = m; }
440 >        public Integer apply(Integer x) {
441 >            m.checkExecutionMode();
442 >            invocationCount++;
443 >            throw new CFException();
444 >        }
445      }
446      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
447 <        boolean ran;
448 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
447 >        final ExecutionMode m;
448 >        int invocationCount = 0;
449 >        FailingBiFunction(ExecutionMode m) { this.m = m; }
450 >        public Integer apply(Integer x, Integer y) {
451 >            m.checkExecutionMode();
452 >            invocationCount++;
453 >            throw new CFException();
454 >        }
455      }
456 <    static final class FailingNoop implements Runnable {
457 <        boolean ran;
458 <        public void run() { ran = true; throw new CFException(); }
456 >    static final class FailingRunnable implements Runnable {
457 >        final ExecutionMode m;
458 >        int invocationCount = 0;
459 >        FailingRunnable(ExecutionMode m) { this.m = m; }
460 >        public void run() {
461 >            m.checkExecutionMode();
462 >            invocationCount++;
463 >            throw new CFException();
464 >        }
465      }
466  
467      static final class CompletableFutureInc
468          implements Function<Integer, CompletableFuture<Integer>> {
469 <        boolean ran;
469 >        final ExecutionMode m;
470 >        int invocationCount = 0;
471 >        CompletableFutureInc(ExecutionMode m) { this.m = m; }
472          public CompletableFuture<Integer> apply(Integer x) {
473 <            ran = true;
473 >            m.checkExecutionMode();
474 >            invocationCount++;
475              CompletableFuture<Integer> f = new CompletableFuture<>();
476 <            f.complete(Integer.valueOf(x.intValue() + 1));
476 >            f.complete(inc(x));
477              return f;
478          }
479      }
480  
481      static final class FailingCompletableFutureFunction
482          implements Function<Integer, CompletableFuture<Integer>> {
483 <        boolean ran;
483 >        final ExecutionMode m;
484 >        int invocationCount = 0;
485 >        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
486          public CompletableFuture<Integer> apply(Integer x) {
487 <            ran = true; throw new CFException();
487 >            m.checkExecutionMode();
488 >            invocationCount++;
489 >            throw new CFException();
490          }
491      }
492  
493      // Used for explicit executor tests
494      static final class ThreadExecutor implements Executor {
495 <        AtomicInteger count = new AtomicInteger(0);
495 >        final AtomicInteger count = new AtomicInteger(0);
496 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
497 >        static boolean startedCurrentThread() {
498 >            return Thread.currentThread().getThreadGroup() == tg;
499 >        }
500  
501          public void execute(Runnable r) {
502              count.getAndIncrement();
503 <            new Thread(r).start();
437 <        }
438 <    }
439 <
440 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
441 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
442 <    }
443 <
444 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
445 <        boolean ran;
446 <        public Integer apply(Integer x, Throwable t) {
447 <            ran = true;
448 <            return (t == null) ? two : three;
503 >            new Thread(tg, r).start();
504          }
505      }
506  
507      /**
508       * Permits the testing of parallel code for the 3 different
509 <     * execution modes without repeating all the testing code.
509 >     * execution modes without copy/pasting all the test methods.
510       */
511      enum ExecutionMode {
512          DEFAULT {
513 +            public void checkExecutionMode() {
514 +                assertFalse(ThreadExecutor.startedCurrentThread());
515 +                assertNull(ForkJoinTask.getPool());
516 +            }
517 +            public CompletableFuture<Void> runAsync(Runnable a) {
518 +                throw new UnsupportedOperationException();
519 +            }
520 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
521 +                throw new UnsupportedOperationException();
522 +            }
523 +            public <T> CompletableFuture<Void> thenRun
524 +                (CompletableFuture<T> f, Runnable a) {
525 +                return f.thenRun(a);
526 +            }
527 +            public <T> CompletableFuture<Void> thenAccept
528 +                (CompletableFuture<T> f, Consumer<? super T> a) {
529 +                return f.thenAccept(a);
530 +            }
531 +            public <T,U> CompletableFuture<U> thenApply
532 +                (CompletableFuture<T> f, Function<? super T,U> a) {
533 +                return f.thenApply(a);
534 +            }
535 +            public <T,U> CompletableFuture<U> thenCompose
536 +                (CompletableFuture<T> f,
537 +                 Function<? super T,? extends CompletionStage<U>> a) {
538 +                return f.thenCompose(a);
539 +            }
540 +            public <T,U> CompletableFuture<U> handle
541 +                (CompletableFuture<T> f,
542 +                 BiFunction<? super T,Throwable,? extends U> a) {
543 +                return f.handle(a);
544 +            }
545 +            public <T> CompletableFuture<T> whenComplete
546 +                (CompletableFuture<T> f,
547 +                 BiConsumer<? super T,? super Throwable> a) {
548 +                return f.whenComplete(a);
549 +            }
550              public <T,U> CompletableFuture<Void> runAfterBoth
551                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
552                  return f.runAfterBoth(g, a);
# Line 471 | Line 563 | public class CompletableFutureTest exten
563                   BiFunction<? super T,? super U,? extends V> a) {
564                  return f.thenCombine(g, a);
565              }
566 <            public <T,U> CompletableFuture<U> applyToEither
566 >            public <T> CompletableFuture<Void> runAfterEither
567                  (CompletableFuture<T> f,
568 <                 CompletionStage<? extends T> g,
569 <                 Function<? super T,U> a) {
570 <                return f.applyToEither(g, a);
568 >                 CompletionStage<?> g,
569 >                 java.lang.Runnable a) {
570 >                return f.runAfterEither(g, a);
571              }
572              public <T> CompletableFuture<Void> acceptEither
573                  (CompletableFuture<T> f,
# Line 483 | Line 575 | public class CompletableFutureTest exten
575                   Consumer<? super T> a) {
576                  return f.acceptEither(g, a);
577              }
578 <            public <T> CompletableFuture<Void> runAfterEither
578 >            public <T,U> CompletableFuture<U> applyToEither
579                  (CompletableFuture<T> f,
580 <                 CompletionStage<?> g,
581 <                 java.lang.Runnable a) {
582 <                return f.runAfterEither(g, a);
580 >                 CompletionStage<? extends T> g,
581 >                 Function<? super T,U> a) {
582 >                return f.applyToEither(g, a);
583 >            }
584 >        },
585 >
586 >        ASYNC {
587 >            public void checkExecutionMode() {
588 >                assertSame(ForkJoinPool.commonPool(),
589 >                           ForkJoinTask.getPool());
590 >            }
591 >            public CompletableFuture<Void> runAsync(Runnable a) {
592 >                return CompletableFuture.runAsync(a);
593 >            }
594 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
595 >                return CompletableFuture.supplyAsync(a);
596 >            }
597 >            public <T> CompletableFuture<Void> thenRun
598 >                (CompletableFuture<T> f, Runnable a) {
599 >                return f.thenRunAsync(a);
600 >            }
601 >            public <T> CompletableFuture<Void> thenAccept
602 >                (CompletableFuture<T> f, Consumer<? super T> a) {
603 >                return f.thenAcceptAsync(a);
604 >            }
605 >            public <T,U> CompletableFuture<U> thenApply
606 >                (CompletableFuture<T> f, Function<? super T,U> a) {
607 >                return f.thenApplyAsync(a);
608              }
609              public <T,U> CompletableFuture<U> thenCompose
610                  (CompletableFuture<T> f,
611                   Function<? super T,? extends CompletionStage<U>> a) {
612 <                return f.thenCompose(a);
612 >                return f.thenComposeAsync(a);
613 >            }
614 >            public <T,U> CompletableFuture<U> handle
615 >                (CompletableFuture<T> f,
616 >                 BiFunction<? super T,Throwable,? extends U> a) {
617 >                return f.handleAsync(a);
618              }
619              public <T> CompletableFuture<T> whenComplete
620                  (CompletableFuture<T> f,
621                   BiConsumer<? super T,? super Throwable> a) {
622 <                return f.whenComplete(a);
622 >                return f.whenCompleteAsync(a);
623              }
502        },
503
504 //             /** Experimental way to do more testing */
505 //         REVERSE_DEFAULT {
506 //             public <T,U> CompletableFuture<Void> runAfterBoth
507 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
508 //                 return g.runAfterBoth(f, a);
509 //             }
510 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
511 //                 (CompletableFuture<T> f,
512 //                  CompletionStage<? extends U> g,
513 //                  BiConsumer<? super T,? super U> a) {
514 //                 return DEFAULT.thenAcceptBoth(f, g, a);
515 //             }
516 //         },
517
518        DEFAULT_ASYNC {
624              public <T,U> CompletableFuture<Void> runAfterBoth
625                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
626                  return f.runAfterBothAsync(g, a);
# Line 532 | Line 637 | public class CompletableFutureTest exten
637                   BiFunction<? super T,? super U,? extends V> a) {
638                  return f.thenCombineAsync(g, a);
639              }
640 <            public <T,U> CompletableFuture<U> applyToEither
640 >            public <T> CompletableFuture<Void> runAfterEither
641                  (CompletableFuture<T> f,
642 <                 CompletionStage<? extends T> g,
643 <                 Function<? super T,U> a) {
644 <                return f.applyToEitherAsync(g, a);
642 >                 CompletionStage<?> g,
643 >                 java.lang.Runnable a) {
644 >                return f.runAfterEitherAsync(g, a);
645              }
646              public <T> CompletableFuture<Void> acceptEither
647                  (CompletableFuture<T> f,
# Line 544 | Line 649 | public class CompletableFutureTest exten
649                   Consumer<? super T> a) {
650                  return f.acceptEitherAsync(g, a);
651              }
652 <            public <T> CompletableFuture<Void> runAfterEither
652 >            public <T,U> CompletableFuture<U> applyToEither
653                  (CompletableFuture<T> f,
654 <                 CompletionStage<?> g,
655 <                 java.lang.Runnable a) {
656 <                return f.runAfterEitherAsync(g, a);
654 >                 CompletionStage<? extends T> g,
655 >                 Function<? super T,U> a) {
656 >                return f.applyToEitherAsync(g, a);
657 >            }
658 >        },
659 >
660 >        EXECUTOR {
661 >            public void checkExecutionMode() {
662 >                assertTrue(ThreadExecutor.startedCurrentThread());
663 >            }
664 >            public CompletableFuture<Void> runAsync(Runnable a) {
665 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
666 >            }
667 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
668 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
669 >            }
670 >            public <T> CompletableFuture<Void> thenRun
671 >                (CompletableFuture<T> f, Runnable a) {
672 >                return f.thenRunAsync(a, new ThreadExecutor());
673 >            }
674 >            public <T> CompletableFuture<Void> thenAccept
675 >                (CompletableFuture<T> f, Consumer<? super T> a) {
676 >                return f.thenAcceptAsync(a, new ThreadExecutor());
677 >            }
678 >            public <T,U> CompletableFuture<U> thenApply
679 >                (CompletableFuture<T> f, Function<? super T,U> a) {
680 >                return f.thenApplyAsync(a, new ThreadExecutor());
681              }
682              public <T,U> CompletableFuture<U> thenCompose
683                  (CompletableFuture<T> f,
684                   Function<? super T,? extends CompletionStage<U>> a) {
685 <                return f.thenComposeAsync(a);
685 >                return f.thenComposeAsync(a, new ThreadExecutor());
686 >            }
687 >            public <T,U> CompletableFuture<U> handle
688 >                (CompletableFuture<T> f,
689 >                 BiFunction<? super T,Throwable,? extends U> a) {
690 >                return f.handleAsync(a, new ThreadExecutor());
691              }
692              public <T> CompletableFuture<T> whenComplete
693                  (CompletableFuture<T> f,
694                   BiConsumer<? super T,? super Throwable> a) {
695 <                return f.whenCompleteAsync(a);
695 >                return f.whenCompleteAsync(a, new ThreadExecutor());
696              }
563        },
564
565 //         REVERSE_DEFAULT_ASYNC {
566 //             public <T,U> CompletableFuture<Void> runAfterBoth
567 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
568 //                 return f.runAfterBothAsync(g, a);
569 //             }
570 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
571 //                 (CompletableFuture<T> f,
572 //                  CompletionStage<? extends U> g,
573 //                  BiConsumer<? super T,? super U> a) {
574 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
575 //             }
576 //         },
577
578        EXECUTOR {
697              public <T,U> CompletableFuture<Void> runAfterBoth
698                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
699                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 592 | Line 710 | public class CompletableFutureTest exten
710                   BiFunction<? super T,? super U,? extends V> a) {
711                  return f.thenCombineAsync(g, a, new ThreadExecutor());
712              }
595            public <T,U> CompletableFuture<U> applyToEither
596                (CompletableFuture<T> f,
597                 CompletionStage<? extends T> g,
598                 Function<? super T,U> a) {
599                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600            }
601            public <T> CompletableFuture<Void> acceptEither
602                (CompletableFuture<T> f,
603                 CompletionStage<? extends T> g,
604                 Consumer<? super T> a) {
605                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606            }
713              public <T> CompletableFuture<Void> runAfterEither
714                  (CompletableFuture<T> f,
715                   CompletionStage<?> g,
716                   java.lang.Runnable a) {
717                  return f.runAfterEitherAsync(g, a, new ThreadExecutor());
718              }
719 <            public <T,U> CompletableFuture<U> thenCompose
719 >            public <T> CompletableFuture<Void> acceptEither
720                  (CompletableFuture<T> f,
721 <                 Function<? super T,? extends CompletionStage<U>> a) {
722 <                return f.thenComposeAsync(a, new ThreadExecutor());
721 >                 CompletionStage<? extends T> g,
722 >                 Consumer<? super T> a) {
723 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
724              }
725 <            public <T> CompletableFuture<T> whenComplete
725 >            public <T,U> CompletableFuture<U> applyToEither
726                  (CompletableFuture<T> f,
727 <                 BiConsumer<? super T,? super Throwable> a) {
728 <                return f.whenCompleteAsync(a, new ThreadExecutor());
727 >                 CompletionStage<? extends T> g,
728 >                 Function<? super T,U> a) {
729 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
730              }
731          };
732  
733 +        public abstract void checkExecutionMode();
734 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
735 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
736 +        public abstract <T> CompletableFuture<Void> thenRun
737 +            (CompletableFuture<T> f, Runnable a);
738 +        public abstract <T> CompletableFuture<Void> thenAccept
739 +            (CompletableFuture<T> f, Consumer<? super T> a);
740 +        public abstract <T,U> CompletableFuture<U> thenApply
741 +            (CompletableFuture<T> f, Function<? super T,U> a);
742 +        public abstract <T,U> CompletableFuture<U> thenCompose
743 +            (CompletableFuture<T> f,
744 +             Function<? super T,? extends CompletionStage<U>> a);
745 +        public abstract <T,U> CompletableFuture<U> handle
746 +            (CompletableFuture<T> f,
747 +             BiFunction<? super T,Throwable,? extends U> a);
748 +        public abstract <T> CompletableFuture<T> whenComplete
749 +            (CompletableFuture<T> f,
750 +             BiConsumer<? super T,? super Throwable> a);
751          public abstract <T,U> CompletableFuture<Void> runAfterBoth
752              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
753          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 632 | Line 758 | public class CompletableFutureTest exten
758              (CompletableFuture<T> f,
759               CompletionStage<? extends U> g,
760               BiFunction<? super T,? super U,? extends V> a);
635        public abstract <T,U> CompletableFuture<U> applyToEither
636            (CompletableFuture<T> f,
637             CompletionStage<? extends T> g,
638             Function<? super T,U> a);
639        public abstract <T> CompletableFuture<Void> acceptEither
640            (CompletableFuture<T> f,
641             CompletionStage<? extends T> g,
642             Consumer<? super T> a);
761          public abstract <T> CompletableFuture<Void> runAfterEither
762              (CompletableFuture<T> f,
763               CompletionStage<?> g,
764               java.lang.Runnable a);
765 <        public abstract <T,U> CompletableFuture<U> thenCompose
765 >        public abstract <T> CompletableFuture<Void> acceptEither
766              (CompletableFuture<T> f,
767 <             Function<? super T,? extends CompletionStage<U>> a);
768 <        public abstract <T> CompletableFuture<T> whenComplete
767 >             CompletionStage<? extends T> g,
768 >             Consumer<? super T> a);
769 >        public abstract <T,U> CompletableFuture<U> applyToEither
770              (CompletableFuture<T> f,
771 <             BiConsumer<? super T,? super Throwable> a);
772 <
654 <
771 >             CompletionStage<? extends T> g,
772 >             Function<? super T,U> a);
773      }
774  
775      /**
776 <     * exceptionally action completes with function value on source
777 <     * exception; otherwise with source value
776 >     * exceptionally action is not invoked when source completes
777 >     * normally, and source result is propagated
778       */
779 <    public void testExceptionally() {
780 <        CompletableFuture<Integer> f = new CompletableFuture<>();
781 <        ExceptionToInteger r = new ExceptionToInteger();
782 <        CompletableFuture<Integer> g = f.exceptionally(r);
783 <        f.completeExceptionally(new CFException());
784 <        checkCompletedNormally(g, three);
779 >    public void testExceptionally_normalCompletion() {
780 >        for (boolean createIncomplete : new boolean[] { true, false })
781 >        for (Integer v1 : new Integer[] { 1, null })
782 >    {
783 >        final AtomicInteger a = new AtomicInteger(0);
784 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
785 >        if (!createIncomplete) f.complete(v1);
786 >        final CompletableFuture<Integer> g = f.exceptionally
787 >            ((Throwable t) -> {
788 >                // Should not be called
789 >                a.getAndIncrement();
790 >                throw new AssertionError();
791 >            });
792 >        if (createIncomplete) f.complete(v1);
793 >
794 >        checkCompletedNormally(g, v1);
795 >        checkCompletedNormally(f, v1);
796 >        assertEquals(0, a.get());
797 >    }}
798  
668        f = new CompletableFuture<>();
669        r = new ExceptionToInteger();
670        g = f.exceptionally(r);
671        f.complete(one);
672        checkCompletedNormally(g, one);
673    }
799  
800      /**
801 <     * handle action completes normally with function value on either
802 <     * normal or exceptional completion of source
801 >     * exceptionally action completes with function value on source
802 >     * exception
803       */
804 <    public void testHandle() {
805 <        CompletableFuture<Integer> f, g;
806 <        IntegerHandler r;
807 <
808 <        f = new CompletableFuture<>();
809 <        f.completeExceptionally(new CFException());
810 <        g = f.handle(r = new IntegerHandler());
811 <        assertTrue(r.ran);
812 <        checkCompletedNormally(g, three);
813 <
814 <        f = new CompletableFuture<>();
815 <        g = f.handle(r = new IntegerHandler());
816 <        assertFalse(r.ran);
817 <        f.completeExceptionally(new CFException());
818 <        checkCompletedNormally(g, three);
819 <        assertTrue(r.ran);
804 >    public void testExceptionally_exceptionalCompletion() {
805 >        for (boolean createIncomplete : new boolean[] { true, false })
806 >        for (Integer v1 : new Integer[] { 1, null })
807 >    {
808 >        final AtomicInteger a = new AtomicInteger(0);
809 >        final CFException ex = new CFException();
810 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
811 >        if (!createIncomplete) f.completeExceptionally(ex);
812 >        final CompletableFuture<Integer> g = f.exceptionally
813 >            ((Throwable t) -> {
814 >                ExecutionMode.DEFAULT.checkExecutionMode();
815 >                threadAssertSame(t, ex);
816 >                a.getAndIncrement();
817 >                return v1;
818 >            });
819 >        if (createIncomplete) f.completeExceptionally(ex);
820  
821 <        f = new CompletableFuture<>();
822 <        f.complete(one);
823 <        g = f.handle(r = new IntegerHandler());
699 <        assertTrue(r.ran);
700 <        checkCompletedNormally(g, two);
821 >        checkCompletedNormally(g, v1);
822 >        assertEquals(1, a.get());
823 >    }}
824  
825 <        f = new CompletableFuture<>();
826 <        g = f.handle(r = new IntegerHandler());
827 <        assertFalse(r.ran);
828 <        f.complete(one);
829 <        assertTrue(r.ran);
830 <        checkCompletedNormally(g, two);
831 <    }
825 >    public void testExceptionally_exceptionalCompletionActionFailed() {
826 >        for (boolean createIncomplete : new boolean[] { true, false })
827 >        for (Integer v1 : new Integer[] { 1, null })
828 >    {
829 >        final AtomicInteger a = new AtomicInteger(0);
830 >        final CFException ex1 = new CFException();
831 >        final CFException ex2 = new CFException();
832 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
833 >        if (!createIncomplete) f.completeExceptionally(ex1);
834 >        final CompletableFuture<Integer> g = f.exceptionally
835 >            ((Throwable t) -> {
836 >                ExecutionMode.DEFAULT.checkExecutionMode();
837 >                threadAssertSame(t, ex1);
838 >                a.getAndIncrement();
839 >                throw ex2;
840 >            });
841 >        if (createIncomplete) f.completeExceptionally(ex1);
842  
843 <    /**
844 <     * runAsync completes after running Runnable
845 <     */
713 <    public void testRunAsync() {
714 <        Noop r = new Noop();
715 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
716 <        assertNull(f.join());
717 <        assertTrue(r.ran);
718 <        checkCompletedNormally(f, null);
719 <    }
843 >        checkCompletedWithWrappedCFException(g, ex2);
844 >        assertEquals(1, a.get());
845 >    }}
846  
847      /**
848 <     * runAsync with executor completes after running Runnable
848 >     * handle action completes normally with function value on normal
849 >     * completion of source
850       */
851 <    public void testRunAsync2() {
852 <        Noop r = new Noop();
853 <        ThreadExecutor exec = new ThreadExecutor();
854 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
855 <        assertNull(f.join());
856 <        assertTrue(r.ran);
857 <        checkCompletedNormally(f, null);
858 <        assertEquals(1, exec.count.get());
859 <    }
851 >    public void testHandle_normalCompletion() {
852 >        for (ExecutionMode m : ExecutionMode.values())
853 >        for (boolean createIncomplete : new boolean[] { true, false })
854 >        for (Integer v1 : new Integer[] { 1, null })
855 >    {
856 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
857 >        final AtomicInteger a = new AtomicInteger(0);
858 >        if (!createIncomplete) f.complete(v1);
859 >        final CompletableFuture<Integer> g = m.handle
860 >            (f,
861 >             (Integer x, Throwable t) -> {
862 >                m.checkExecutionMode();
863 >                threadAssertSame(x, v1);
864 >                threadAssertNull(t);
865 >                a.getAndIncrement();
866 >                return inc(v1);
867 >            });
868 >        if (createIncomplete) f.complete(v1);
869  
870 <    /**
871 <     * failing runAsync completes exceptionally after running Runnable
872 <     */
873 <    public void testRunAsync3() {
738 <        FailingNoop r = new FailingNoop();
739 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
740 <        checkCompletedWithWrappedCFException(f);
741 <        assertTrue(r.ran);
742 <    }
870 >        checkCompletedNormally(g, inc(v1));
871 >        checkCompletedNormally(f, v1);
872 >        assertEquals(1, a.get());
873 >    }}
874  
875      /**
876 <     * supplyAsync completes with result of supplier
876 >     * handle action completes normally with function value on
877 >     * exceptional completion of source
878       */
879 <    public void testSupplyAsync() {
880 <        CompletableFuture<Integer> f;
881 <        f = CompletableFuture.supplyAsync(supplyOne);
882 <        assertEquals(f.join(), one);
883 <        checkCompletedNormally(f, one);
884 <    }
879 >    public void testHandle_exceptionalCompletion() {
880 >        for (ExecutionMode m : ExecutionMode.values())
881 >        for (boolean createIncomplete : new boolean[] { true, false })
882 >        for (Integer v1 : new Integer[] { 1, null })
883 >    {
884 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
885 >        final AtomicInteger a = new AtomicInteger(0);
886 >        final CFException ex = new CFException();
887 >        if (!createIncomplete) f.completeExceptionally(ex);
888 >        final CompletableFuture<Integer> g = m.handle
889 >            (f,
890 >             (Integer x, Throwable t) -> {
891 >                m.checkExecutionMode();
892 >                threadAssertNull(x);
893 >                threadAssertSame(t, ex);
894 >                a.getAndIncrement();
895 >                return v1;
896 >            });
897 >        if (createIncomplete) f.completeExceptionally(ex);
898  
899 <    /**
900 <     * supplyAsync with executor completes with result of supplier
901 <     */
902 <    public void testSupplyAsync2() {
758 <        CompletableFuture<Integer> f;
759 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
760 <        assertEquals(f.join(), one);
761 <        checkCompletedNormally(f, one);
762 <    }
899 >        checkCompletedNormally(g, v1);
900 >        checkCompletedWithWrappedCFException(f, ex);
901 >        assertEquals(1, a.get());
902 >    }}
903  
904      /**
905 <     * Failing supplyAsync completes exceptionally
905 >     * handle action completes normally with function value on
906 >     * cancelled source
907       */
908 <    public void testSupplyAsync3() {
909 <        FailingSupplier r = new FailingSupplier();
910 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
911 <        checkCompletedWithWrappedCFException(f);
912 <        assertTrue(r.ran);
913 <    }
908 >    public void testHandle_sourceCancelled() {
909 >        for (ExecutionMode m : ExecutionMode.values())
910 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
911 >        for (boolean createIncomplete : new boolean[] { true, false })
912 >        for (Integer v1 : new Integer[] { 1, null })
913 >    {
914 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
915 >        final AtomicInteger a = new AtomicInteger(0);
916 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
917 >        final CompletableFuture<Integer> g = m.handle
918 >            (f,
919 >             (Integer x, Throwable t) -> {
920 >                m.checkExecutionMode();
921 >                threadAssertNull(x);
922 >                threadAssertTrue(t instanceof CancellationException);
923 >                a.getAndIncrement();
924 >                return v1;
925 >            });
926 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
927  
928 <    // seq completion methods
928 >        checkCompletedNormally(g, v1);
929 >        checkCancelled(f);
930 >        assertEquals(1, a.get());
931 >    }}
932  
933      /**
934 <     * thenRun result completes normally after normal completion of source
934 >     * handle result completes exceptionally if action does
935       */
936 <    public void testThenRun() {
937 <        CompletableFuture<Integer> f;
938 <        CompletableFuture<Void> g;
939 <        Noop r;
940 <
941 <        f = new CompletableFuture<>();
942 <        g = f.thenRun(r = new Noop());
943 <        f.complete(null);
944 <        checkCompletedNormally(g, null);
945 <        assertTrue(r.ran);
946 <
947 <        f = new CompletableFuture<>();
948 <        f.complete(null);
949 <        g = f.thenRun(r = new Noop());
950 <        checkCompletedNormally(g, null);
951 <        assertTrue(r.ran);
952 <    }
936 >    public void testHandle_sourceFailedActionFailed() {
937 >        for (ExecutionMode m : ExecutionMode.values())
938 >        for (boolean createIncomplete : new boolean[] { true, false })
939 >    {
940 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
941 >        final AtomicInteger a = new AtomicInteger(0);
942 >        final CFException ex1 = new CFException();
943 >        final CFException ex2 = new CFException();
944 >        if (!createIncomplete) f.completeExceptionally(ex1);
945 >        final CompletableFuture<Integer> g = m.handle
946 >            (f,
947 >             (Integer x, Throwable t) -> {
948 >                m.checkExecutionMode();
949 >                threadAssertNull(x);
950 >                threadAssertSame(ex1, t);
951 >                a.getAndIncrement();
952 >                throw ex2;
953 >            });
954 >        if (createIncomplete) f.completeExceptionally(ex1);
955  
956 <    /**
957 <     * thenRun result completes exceptionally after exceptional
958 <     * completion of source
959 <     */
801 <    public void testThenRun2() {
802 <        CompletableFuture<Integer> f;
803 <        CompletableFuture<Void> g;
804 <        Noop r;
956 >        checkCompletedWithWrappedCFException(g, ex2);
957 >        checkCompletedWithWrappedCFException(f, ex1);
958 >        assertEquals(1, a.get());
959 >    }}
960  
961 <        f = new CompletableFuture<>();
962 <        g = f.thenRun(r = new Noop());
963 <        f.completeExceptionally(new CFException());
964 <        checkCompletedWithWrappedCFException(g);
965 <        assertFalse(r.ran);
961 >    public void testHandle_sourceCompletedNormallyActionFailed() {
962 >        for (ExecutionMode m : ExecutionMode.values())
963 >        for (boolean createIncomplete : new boolean[] { true, false })
964 >        for (Integer v1 : new Integer[] { 1, null })
965 >    {
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        final AtomicInteger a = new AtomicInteger(0);
968 >        final CFException ex = new CFException();
969 >        if (!createIncomplete) f.complete(v1);
970 >        final CompletableFuture<Integer> g = m.handle
971 >            (f,
972 >             (Integer x, Throwable t) -> {
973 >                m.checkExecutionMode();
974 >                threadAssertSame(x, v1);
975 >                threadAssertNull(t);
976 >                a.getAndIncrement();
977 >                throw ex;
978 >            });
979 >        if (createIncomplete) f.complete(v1);
980  
981 <        f = new CompletableFuture<>();
982 <        f.completeExceptionally(new CFException());
983 <        g = f.thenRun(r = new Noop());
984 <        checkCompletedWithWrappedCFException(g);
816 <        assertFalse(r.ran);
817 <    }
981 >        checkCompletedWithWrappedCFException(g, ex);
982 >        checkCompletedNormally(f, v1);
983 >        assertEquals(1, a.get());
984 >    }}
985  
986      /**
987 <     * thenRun result completes exceptionally if action does
987 >     * runAsync completes after running Runnable
988       */
989 <    public void testThenRun3() {
990 <        CompletableFuture<Integer> f;
991 <        CompletableFuture<Void> g;
992 <        FailingNoop r;
993 <
994 <        f = new CompletableFuture<>();
995 <        g = f.thenRun(r = new FailingNoop());
996 <        f.complete(null);
997 <        checkCompletedWithWrappedCFException(g);
998 <
999 <        f = new CompletableFuture<>();
1000 <        f.complete(null);
1001 <        g = f.thenRun(r = new FailingNoop());
835 <        checkCompletedWithWrappedCFException(g);
836 <    }
989 >    public void testRunAsync_normalCompletion() {
990 >        ExecutionMode[] executionModes = {
991 >            ExecutionMode.ASYNC,
992 >            ExecutionMode.EXECUTOR,
993 >        };
994 >        for (ExecutionMode m : executionModes)
995 >    {
996 >        final Noop r = new Noop(m);
997 >        final CompletableFuture<Void> f = m.runAsync(r);
998 >        assertNull(f.join());
999 >        checkCompletedNormally(f, null);
1000 >        assertEquals(1, r.invocationCount);
1001 >    }}
1002  
1003      /**
1004 <     * thenRun result completes exceptionally if source cancelled
1004 >     * failing runAsync completes exceptionally after running Runnable
1005       */
1006 <    public void testThenRun4() {
1007 <        CompletableFuture<Integer> f;
1008 <        CompletableFuture<Void> g;
1009 <        Noop r;
1010 <
1011 <        f = new CompletableFuture<>();
1012 <        g = f.thenRun(r = new Noop());
1013 <        assertTrue(f.cancel(true));
1014 <        checkCompletedWithWrappedCancellationException(g);
1015 <
1016 <        f = new CompletableFuture<>();
1017 <        assertTrue(f.cancel(true));
853 <        g = f.thenRun(r = new Noop());
854 <        checkCompletedWithWrappedCancellationException(g);
855 <    }
1006 >    public void testRunAsync_exceptionalCompletion() {
1007 >        ExecutionMode[] executionModes = {
1008 >            ExecutionMode.ASYNC,
1009 >            ExecutionMode.EXECUTOR,
1010 >        };
1011 >        for (ExecutionMode m : executionModes)
1012 >    {
1013 >        final FailingRunnable r = new FailingRunnable(m);
1014 >        final CompletableFuture<Void> f = m.runAsync(r);
1015 >        checkCompletedWithWrappedCFException(f);
1016 >        assertEquals(1, r.invocationCount);
1017 >    }}
1018  
1019      /**
1020 <     * thenApply result completes normally after normal completion of source
1020 >     * supplyAsync completes with result of supplier
1021       */
1022 <    public void testThenApply() {
1023 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1024 <        CompletableFuture<Integer> g = f.thenApply(inc);
1025 <        f.complete(one);
1026 <        checkCompletedNormally(g, two);
1027 <    }
1022 >    public void testSupplyAsync_normalCompletion() {
1023 >        ExecutionMode[] executionModes = {
1024 >            ExecutionMode.ASYNC,
1025 >            ExecutionMode.EXECUTOR,
1026 >        };
1027 >        for (ExecutionMode m : executionModes)
1028 >        for (Integer v1 : new Integer[] { 1, null })
1029 >    {
1030 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1031 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1032 >        assertSame(v1, f.join());
1033 >        checkCompletedNormally(f, v1);
1034 >        assertEquals(1, r.invocationCount);
1035 >    }}
1036  
1037      /**
1038 <     * thenApply result completes exceptionally after exceptional
869 <     * completion of source
1038 >     * Failing supplyAsync completes exceptionally
1039       */
1040 <    public void testThenApply2() {
1041 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1042 <        CompletableFuture<Integer> g = f.thenApply(inc);
1043 <        f.completeExceptionally(new CFException());
1044 <        checkCompletedWithWrappedCFException(g);
1045 <    }
1040 >    public void testSupplyAsync_exceptionalCompletion() {
1041 >        ExecutionMode[] executionModes = {
1042 >            ExecutionMode.ASYNC,
1043 >            ExecutionMode.EXECUTOR,
1044 >        };
1045 >        for (ExecutionMode m : executionModes)
1046 >    {
1047 >        FailingSupplier r = new FailingSupplier(m);
1048 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1049 >        checkCompletedWithWrappedCFException(f);
1050 >        assertEquals(1, r.invocationCount);
1051 >    }}
1052  
1053 <    /**
879 <     * thenApply result completes exceptionally if action does
880 <     */
881 <    public void testThenApply3() {
882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
883 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
884 <        f.complete(one);
885 <        checkCompletedWithWrappedCFException(g);
886 <    }
1053 >    // seq completion methods
1054  
1055      /**
1056 <     * thenApply result completes exceptionally if source cancelled
1056 >     * thenRun result completes normally after normal completion of source
1057       */
1058 <    public void testThenApply4() {
1059 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1060 <        CompletableFuture<Integer> g = f.thenApply(inc);
1061 <        assertTrue(f.cancel(true));
1062 <        checkCompletedWithWrappedCancellationException(g);
1063 <    }
1058 >    public void testThenRun_normalCompletion() {
1059 >        for (ExecutionMode m : ExecutionMode.values())
1060 >        for (boolean createIncomplete : new boolean[] { true, false })
1061 >        for (Integer v1 : new Integer[] { 1, null })
1062 >    {
1063 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1064 >        final Noop r = new Noop(m);
1065 >        if (!createIncomplete) f.complete(v1);
1066 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1067 >        if (createIncomplete) {
1068 >            checkIncomplete(g);
1069 >            f.complete(v1);
1070 >        }
1071  
898    /**
899     * thenAccept result completes normally after normal completion of source
900     */
901    public void testThenAccept() {
902        CompletableFuture<Integer> f = new CompletableFuture<>();
903        IncAction r = new IncAction();
904        CompletableFuture<Void> g = f.thenAccept(r);
905        f.complete(one);
1072          checkCompletedNormally(g, null);
1073 <        assertEquals(r.value, (Integer) 2);
1074 <    }
1073 >        checkCompletedNormally(f, v1);
1074 >        assertEquals(1, r.invocationCount);
1075 >    }}
1076  
1077      /**
1078 <     * thenAccept result completes exceptionally after exceptional
1078 >     * thenRun result completes exceptionally after exceptional
1079       * completion of source
1080       */
1081 <    public void testThenAccept2() {
1082 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1083 <        IncAction r = new IncAction();
1084 <        CompletableFuture<Void> g = f.thenAccept(r);
1085 <        f.completeExceptionally(new CFException());
1086 <        checkCompletedWithWrappedCFException(g);
1087 <    }
1081 >    public void testThenRun_exceptionalCompletion() {
1082 >        for (ExecutionMode m : ExecutionMode.values())
1083 >        for (boolean createIncomplete : new boolean[] { true, false })
1084 >    {
1085 >        final CFException ex = new CFException();
1086 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1087 >        final Noop r = new Noop(m);
1088 >        if (!createIncomplete) f.completeExceptionally(ex);
1089 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1090 >        if (createIncomplete) {
1091 >            checkIncomplete(g);
1092 >            f.completeExceptionally(ex);
1093 >        }
1094  
1095 <    /**
1096 <     * thenAccept result completes exceptionally if action does
1097 <     */
1098 <    public void testThenAccept3() {
926 <        CompletableFuture<Integer> f = new CompletableFuture<>();
927 <        FailingConsumer r = new FailingConsumer();
928 <        CompletableFuture<Void> g = f.thenAccept(r);
929 <        f.complete(one);
930 <        checkCompletedWithWrappedCFException(g);
931 <        assertTrue(r.ran);
932 <    }
1095 >        checkCompletedWithWrappedCFException(g, ex);
1096 >        checkCompletedWithWrappedCFException(f, ex);
1097 >        assertEquals(0, r.invocationCount);
1098 >    }}
1099  
1100      /**
1101 <     * thenAccept result completes exceptionally if source cancelled
1101 >     * thenRun result completes exceptionally if source cancelled
1102       */
1103 <    public void testThenAccept4() {
1104 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1105 <        IncAction r = new IncAction();
1106 <        CompletableFuture<Void> g = f.thenAccept(r);
1107 <        assertTrue(f.cancel(true));
1103 >    public void testThenRun_sourceCancelled() {
1104 >        for (ExecutionMode m : ExecutionMode.values())
1105 >        for (boolean createIncomplete : new boolean[] { true, false })
1106 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1107 >    {
1108 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1109 >        final Noop r = new Noop(m);
1110 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1111 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1112 >        if (createIncomplete) {
1113 >            checkIncomplete(g);
1114 >            assertTrue(f.cancel(mayInterruptIfRunning));
1115 >        }
1116 >
1117          checkCompletedWithWrappedCancellationException(g);
1118 <    }
1118 >        checkCancelled(f);
1119 >        assertEquals(0, r.invocationCount);
1120 >    }}
1121  
1122      /**
1123 <     * thenCombine result completes normally after normal completion
947 <     * of sources
1123 >     * thenRun result completes exceptionally if action does
1124       */
1125 <    public void testThenCombine_normalCompletion1() {
1125 >    public void testThenRun_actionFailed() {
1126          for (ExecutionMode m : ExecutionMode.values())
1127 +        for (boolean createIncomplete : new boolean[] { true, false })
1128          for (Integer v1 : new Integer[] { 1, null })
1129 <        for (Integer v2 : new Integer[] { 2, null }) {
953 <
1129 >    {
1130          final CompletableFuture<Integer> f = new CompletableFuture<>();
1131 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1132 <        final SubtractFunction r = new SubtractFunction();
1133 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1134 <
1135 <        f.complete(v1);
1136 <        checkIncomplete(h);
961 <        assertEquals(r.invocationCount, 0);
962 <        g.complete(v2);
963 <
964 <        checkCompletedNormally(h, subtract(v1, v2));
965 <        checkCompletedNormally(f, v1);
966 <        checkCompletedNormally(g, v2);
967 <        assertEquals(r.invocationCount, 1);
1131 >        final FailingRunnable r = new FailingRunnable(m);
1132 >        if (!createIncomplete) f.complete(v1);
1133 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1134 >        if (createIncomplete) {
1135 >            checkIncomplete(g);
1136 >            f.complete(v1);
1137          }
969    }
970
971    public void testThenCombine_normalCompletion2() {
972        for (ExecutionMode m : ExecutionMode.values())
973        for (Integer v1 : new Integer[] { 1, null })
974        for (Integer v2 : new Integer[] { 2, null }) {
975
976        final CompletableFuture<Integer> f = new CompletableFuture<>();
977        final CompletableFuture<Integer> g = new CompletableFuture<>();
978        final SubtractFunction r = new SubtractFunction();
979        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
980
981        g.complete(v2);
982        checkIncomplete(h);
983        assertEquals(r.invocationCount, 0);
984        f.complete(v1);
1138  
1139 <        checkCompletedNormally(h, subtract(v1, v2));
1139 >        checkCompletedWithWrappedCFException(g);
1140          checkCompletedNormally(f, v1);
1141 <        checkCompletedNormally(g, v2);
989 <        assertEquals(r.invocationCount, 1);
990 <        }
991 <    }
1141 >    }}
1142  
1143 <    public void testThenCombine_normalCompletion3() {
1143 >    /**
1144 >     * thenApply result completes normally after normal completion of source
1145 >     */
1146 >    public void testThenApply_normalCompletion() {
1147          for (ExecutionMode m : ExecutionMode.values())
1148 +        for (boolean createIncomplete : new boolean[] { true, false })
1149          for (Integer v1 : new Integer[] { 1, null })
1150 <        for (Integer v2 : new Integer[] { 2, null }) {
997 <
1150 >    {
1151          final CompletableFuture<Integer> f = new CompletableFuture<>();
1152 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1153 <        final SubtractFunction r = new SubtractFunction();
1154 <
1155 <        g.complete(v2);
1156 <        f.complete(v1);
1157 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1005 <
1006 <        checkCompletedNormally(h, subtract(v1, v2));
1007 <        checkCompletedNormally(f, v1);
1008 <        checkCompletedNormally(g, v2);
1009 <        assertEquals(r.invocationCount, 1);
1152 >        final IncFunction r = new IncFunction(m);
1153 >        if (!createIncomplete) f.complete(v1);
1154 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1155 >        if (createIncomplete) {
1156 >            checkIncomplete(g);
1157 >            f.complete(v1);
1158          }
1011    }
1159  
1160 <    public void testThenCombine_normalCompletion4() {
1014 <        for (ExecutionMode m : ExecutionMode.values())
1015 <        for (Integer v1 : new Integer[] { 1, null })
1016 <        for (Integer v2 : new Integer[] { 2, null }) {
1017 <
1018 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1020 <        final SubtractFunction r = new SubtractFunction();
1021 <
1022 <        f.complete(v1);
1023 <        g.complete(v2);
1024 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1025 <
1026 <        checkCompletedNormally(h, subtract(v1, v2));
1160 >        checkCompletedNormally(g, inc(v1));
1161          checkCompletedNormally(f, v1);
1162 <        checkCompletedNormally(g, v2);
1163 <        assertEquals(r.invocationCount, 1);
1030 <        }
1031 <    }
1162 >        assertEquals(1, r.invocationCount);
1163 >    }}
1164  
1165      /**
1166 <     * thenCombine result completes exceptionally after exceptional
1167 <     * completion of either source
1166 >     * thenApply result completes exceptionally after exceptional
1167 >     * completion of source
1168       */
1169 <    public void testThenCombine_exceptionalCompletion1() {
1169 >    public void testThenApply_exceptionalCompletion() {
1170          for (ExecutionMode m : ExecutionMode.values())
1171 <        for (Integer v1 : new Integer[] { 1, null }) {
1172 <
1041 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1042 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1043 <        final SubtractFunction r = new SubtractFunction();
1044 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1171 >        for (boolean createIncomplete : new boolean[] { true, false })
1172 >    {
1173          final CFException ex = new CFException();
1046
1047        f.completeExceptionally(ex);
1048        checkIncomplete(h);
1049        g.complete(v1);
1050
1051        checkCompletedWithWrappedCFException(h, ex);
1052        checkCompletedWithWrappedCFException(f, ex);
1053        assertEquals(r.invocationCount, 0);
1054        checkCompletedNormally(g, v1);
1055        }
1056    }
1057
1058    public void testThenCombine_exceptionalCompletion2() {
1059        for (ExecutionMode m : ExecutionMode.values())
1060        for (Integer v1 : new Integer[] { 1, null }) {
1061
1174          final CompletableFuture<Integer> f = new CompletableFuture<>();
1175 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1176 <        final SubtractFunction r = new SubtractFunction();
1177 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1178 <        final CFException ex = new CFException();
1179 <
1180 <        g.completeExceptionally(ex);
1069 <        checkIncomplete(h);
1070 <        f.complete(v1);
1071 <
1072 <        checkCompletedWithWrappedCFException(h, ex);
1073 <        checkCompletedWithWrappedCFException(g, ex);
1074 <        assertEquals(r.invocationCount, 0);
1075 <        checkCompletedNormally(f, v1);
1175 >        final IncFunction r = new IncFunction(m);
1176 >        if (!createIncomplete) f.completeExceptionally(ex);
1177 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1178 >        if (createIncomplete) {
1179 >            checkIncomplete(g);
1180 >            f.completeExceptionally(ex);
1181          }
1077    }
1078
1079    public void testThenCombine_exceptionalCompletion3() {
1080        for (ExecutionMode m : ExecutionMode.values())
1081        for (Integer v1 : new Integer[] { 1, null }) {
1182  
1083        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084        final CompletableFuture<Integer> g = new CompletableFuture<>();
1085        final SubtractFunction r = new SubtractFunction();
1086        final CFException ex = new CFException();
1087
1088        g.completeExceptionally(ex);
1089        f.complete(v1);
1090        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1091
1092        checkCompletedWithWrappedCFException(h, ex);
1183          checkCompletedWithWrappedCFException(g, ex);
1184 <        assertEquals(r.invocationCount, 0);
1185 <        checkCompletedNormally(f, v1);
1186 <        }
1097 <    }
1184 >        checkCompletedWithWrappedCFException(f, ex);
1185 >        assertEquals(0, r.invocationCount);
1186 >    }}
1187  
1188 <    public void testThenCombine_exceptionalCompletion4() {
1188 >    /**
1189 >     * thenApply result completes exceptionally if source cancelled
1190 >     */
1191 >    public void testThenApply_sourceCancelled() {
1192          for (ExecutionMode m : ExecutionMode.values())
1193 <        for (Integer v1 : new Integer[] { 1, null }) {
1194 <
1193 >        for (boolean createIncomplete : new boolean[] { true, false })
1194 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1195 >    {
1196          final CompletableFuture<Integer> f = new CompletableFuture<>();
1197 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1198 <        final SubtractFunction r = new SubtractFunction();
1199 <        final CFException ex = new CFException();
1200 <
1201 <        f.completeExceptionally(ex);
1202 <        g.complete(v1);
1110 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1111 <
1112 <        checkCompletedWithWrappedCFException(h, ex);
1113 <        checkCompletedWithWrappedCFException(f, ex);
1114 <        assertEquals(r.invocationCount, 0);
1115 <        checkCompletedNormally(g, v1);
1197 >        final IncFunction r = new IncFunction(m);
1198 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1199 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1200 >        if (createIncomplete) {
1201 >            checkIncomplete(g);
1202 >            assertTrue(f.cancel(mayInterruptIfRunning));
1203          }
1204 <    }
1204 >
1205 >        checkCompletedWithWrappedCancellationException(g);
1206 >        checkCancelled(f);
1207 >        assertEquals(0, r.invocationCount);
1208 >    }}
1209  
1210      /**
1211 <     * thenCombine result completes exceptionally if action does
1211 >     * thenApply result completes exceptionally if action does
1212       */
1213 <    public void testThenCombine_actionFailed1() {
1213 >    public void testThenApply_actionFailed() {
1214          for (ExecutionMode m : ExecutionMode.values())
1215 +        for (boolean createIncomplete : new boolean[] { true, false })
1216          for (Integer v1 : new Integer[] { 1, null })
1217 <        for (Integer v2 : new Integer[] { 2, null }) {
1126 <
1217 >    {
1218          final CompletableFuture<Integer> f = new CompletableFuture<>();
1219 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1220 <        final FailingBiFunction r = new FailingBiFunction();
1221 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1222 <
1223 <        f.complete(v1);
1224 <        checkIncomplete(h);
1225 <        g.complete(v2);
1219 >        final FailingFunction r = new FailingFunction(m);
1220 >        if (!createIncomplete) f.complete(v1);
1221 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1222 >        if (createIncomplete) {
1223 >            checkIncomplete(g);
1224 >            f.complete(v1);
1225 >        }
1226  
1227 <        checkCompletedWithWrappedCFException(h);
1227 >        checkCompletedWithWrappedCFException(g);
1228          checkCompletedNormally(f, v1);
1229 <        checkCompletedNormally(g, v2);
1139 <        }
1140 <    }
1229 >    }}
1230  
1231 <    public void testThenCombine_actionFailed2() {
1231 >    /**
1232 >     * thenAccept result completes normally after normal completion of source
1233 >     */
1234 >    public void testThenAccept_normalCompletion() {
1235          for (ExecutionMode m : ExecutionMode.values())
1236 +        for (boolean createIncomplete : new boolean[] { true, false })
1237          for (Integer v1 : new Integer[] { 1, null })
1238 <        for (Integer v2 : new Integer[] { 2, null }) {
1146 <
1238 >    {
1239          final CompletableFuture<Integer> f = new CompletableFuture<>();
1240 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1241 <        final FailingBiFunction r = new FailingBiFunction();
1242 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1243 <
1244 <        g.complete(v2);
1245 <        checkIncomplete(h);
1246 <        f.complete(v1);
1240 >        final IncAction r = new IncAction();
1241 >        if (!createIncomplete) f.complete(v1);
1242 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1243 >        if (createIncomplete) {
1244 >            checkIncomplete(g);
1245 >            f.complete(v1);
1246 >        }
1247  
1248 <        checkCompletedWithWrappedCFException(h);
1248 >        checkCompletedNormally(g, null);
1249          checkCompletedNormally(f, v1);
1250 <        checkCompletedNormally(g, v2);
1251 <        }
1252 <    }
1250 >        assertEquals(1, r.invocationCount);
1251 >        assertEquals(inc(v1), r.value);
1252 >    }}
1253  
1254      /**
1255 <     * thenCombine result completes exceptionally if either source cancelled
1255 >     * thenAccept result completes exceptionally after exceptional
1256 >     * completion of source
1257       */
1258 <    public void testThenCombine_sourceCancelled1() {
1258 >    public void testThenAccept_exceptionalCompletion() {
1259          for (ExecutionMode m : ExecutionMode.values())
1260 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1261 <        for (Integer v1 : new Integer[] { 1, null }) {
1262 <
1260 >        for (boolean createIncomplete : new boolean[] { true, false })
1261 >    {
1262 >        final CFException ex = new CFException();
1263          final CompletableFuture<Integer> f = new CompletableFuture<>();
1264 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1265 <        final SubtractFunction r = new SubtractFunction();
1266 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1267 <
1268 <        assertTrue(f.cancel(mayInterruptIfRunning));
1269 <        checkIncomplete(h);
1177 <        g.complete(v1);
1178 <
1179 <        checkCompletedWithWrappedCancellationException(h);
1180 <        checkCancelled(f);
1181 <        assertEquals(r.invocationCount, 0);
1182 <        checkCompletedNormally(g, v1);
1264 >        final IncAction r = new IncAction();
1265 >        if (!createIncomplete) f.completeExceptionally(ex);
1266 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1267 >        if (createIncomplete) {
1268 >            checkIncomplete(g);
1269 >            f.completeExceptionally(ex);
1270          }
1184    }
1185
1186    public void testThenCombine_sourceCancelled2() {
1187        for (ExecutionMode m : ExecutionMode.values())
1188        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1189        for (Integer v1 : new Integer[] { 1, null }) {
1271  
1272 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1273 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1274 <        final SubtractFunction r = new SubtractFunction();
1275 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1195 <
1196 <        assertTrue(g.cancel(mayInterruptIfRunning));
1197 <        checkIncomplete(h);
1198 <        f.complete(v1);
1199 <
1200 <        checkCompletedWithWrappedCancellationException(h);
1201 <        checkCancelled(g);
1202 <        assertEquals(r.invocationCount, 0);
1203 <        checkCompletedNormally(f, v1);
1204 <        }
1205 <    }
1272 >        checkCompletedWithWrappedCFException(g, ex);
1273 >        checkCompletedWithWrappedCFException(f, ex);
1274 >        assertEquals(0, r.invocationCount);
1275 >    }}
1276  
1277 <    public void testThenCombine_sourceCancelled3() {
1277 >    /**
1278 >     * thenAccept result completes exceptionally if source cancelled
1279 >     */
1280 >    public void testThenAccept_sourceCancelled() {
1281          for (ExecutionMode m : ExecutionMode.values())
1282 +        for (boolean createIncomplete : new boolean[] { true, false })
1283          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1284 <        for (Integer v1 : new Integer[] { 1, null }) {
1211 <
1284 >    {
1285          final CompletableFuture<Integer> f = new CompletableFuture<>();
1286 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1287 <        final SubtractFunction r = new SubtractFunction();
1288 <
1289 <        assertTrue(g.cancel(mayInterruptIfRunning));
1290 <        f.complete(v1);
1291 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1219 <
1220 <        checkCompletedWithWrappedCancellationException(h);
1221 <        checkCancelled(g);
1222 <        assertEquals(r.invocationCount, 0);
1223 <        checkCompletedNormally(f, v1);
1286 >        final IncAction r = new IncAction();
1287 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1288 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1289 >        if (createIncomplete) {
1290 >            checkIncomplete(g);
1291 >            assertTrue(f.cancel(mayInterruptIfRunning));
1292          }
1225    }
1226
1227    public void testThenCombine_sourceCancelled4() {
1228        for (ExecutionMode m : ExecutionMode.values())
1229        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
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 SubtractFunction r = new SubtractFunction();
1293  
1294 <        assertTrue(f.cancel(mayInterruptIfRunning));
1237 <        g.complete(v1);
1238 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1239 <
1240 <        checkCompletedWithWrappedCancellationException(h);
1294 >        checkCompletedWithWrappedCancellationException(g);
1295          checkCancelled(f);
1296 <        assertEquals(r.invocationCount, 0);
1297 <        checkCompletedNormally(g, v1);
1244 <        }
1245 <    }
1296 >        assertEquals(0, r.invocationCount);
1297 >    }}
1298  
1299      /**
1300 <     * thenAcceptBoth result completes normally after normal
1249 <     * completion of sources
1300 >     * thenAccept result completes exceptionally if action does
1301       */
1302 <    public void testThenAcceptBoth_normalCompletion1() {
1302 >    public void testThenAccept_actionFailed() {
1303          for (ExecutionMode m : ExecutionMode.values())
1304 +        for (boolean createIncomplete : new boolean[] { true, false })
1305          for (Integer v1 : new Integer[] { 1, null })
1306 <        for (Integer v2 : new Integer[] { 2, null }) {
1255 <
1306 >    {
1307          final CompletableFuture<Integer> f = new CompletableFuture<>();
1308 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1309 <        final SubtractAction r = new SubtractAction();
1310 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1311 <
1312 <        f.complete(v1);
1313 <        checkIncomplete(h);
1263 <        assertFalse(r.ran());
1264 <        g.complete(v2);
1265 <
1266 <        checkCompletedNormally(h, null);
1267 <        assertEquals(r.value, subtract(v1, v2));
1268 <        checkCompletedNormally(f, v1);
1269 <        checkCompletedNormally(g, v2);
1308 >        final FailingConsumer r = new FailingConsumer(m);
1309 >        if (!createIncomplete) f.complete(v1);
1310 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1311 >        if (createIncomplete) {
1312 >            checkIncomplete(g);
1313 >            f.complete(v1);
1314          }
1271    }
1272
1273    public void testThenAcceptBoth_normalCompletion2() {
1274        for (ExecutionMode m : ExecutionMode.values())
1275        for (Integer v1 : new Integer[] { 1, null })
1276        for (Integer v2 : new Integer[] { 2, null }) {
1315  
1316 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1279 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1280 <        final SubtractAction r = new SubtractAction();
1281 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1282 <
1283 <        g.complete(v2);
1284 <        checkIncomplete(h);
1285 <        assertFalse(r.ran());
1286 <        f.complete(v1);
1287 <
1288 <        checkCompletedNormally(h, null);
1289 <        assertEquals(r.value, subtract(v1, v2));
1316 >        checkCompletedWithWrappedCFException(g);
1317          checkCompletedNormally(f, v1);
1318 <        checkCompletedNormally(g, v2);
1292 <        }
1293 <    }
1318 >    }}
1319  
1320 <    public void testThenAcceptBoth_normalCompletion3() {
1320 >    /**
1321 >     * thenCombine result completes normally after normal completion
1322 >     * of sources
1323 >     */
1324 >    public void testThenCombine_normalCompletion() {
1325          for (ExecutionMode m : ExecutionMode.values())
1326 +        for (boolean createIncomplete : new boolean[] { true, false })
1327 +        for (boolean fFirst : new boolean[] { true, false })
1328          for (Integer v1 : new Integer[] { 1, null })
1329 <        for (Integer v2 : new Integer[] { 2, null }) {
1330 <
1329 >        for (Integer v2 : new Integer[] { 2, null })
1330 >    {
1331          final CompletableFuture<Integer> f = new CompletableFuture<>();
1332          final CompletableFuture<Integer> g = new CompletableFuture<>();
1333 <        final SubtractAction r = new SubtractAction();
1303 <
1304 <        g.complete(v2);
1305 <        f.complete(v1);
1306 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1333 >        final SubtractFunction r = new SubtractFunction(m);
1334  
1335 <        checkCompletedNormally(h, null);
1336 <        assertEquals(r.value, subtract(v1, v2));
1337 <        checkCompletedNormally(f, v1);
1338 <        checkCompletedNormally(g, v2);
1335 >        if (fFirst) f.complete(v1); else g.complete(v2);
1336 >        if (!createIncomplete)
1337 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1338 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1339 >        if (createIncomplete) {
1340 >            checkIncomplete(h);
1341 >            assertEquals(0, r.invocationCount);
1342 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1343          }
1313    }
1344  
1345 <    public void testThenAcceptBoth_normalCompletion4() {
1316 <        for (ExecutionMode m : ExecutionMode.values())
1317 <        for (Integer v1 : new Integer[] { 1, null })
1318 <        for (Integer v2 : new Integer[] { 2, null }) {
1319 <
1320 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 <        final SubtractAction r = new SubtractAction();
1323 <
1324 <        f.complete(v1);
1325 <        g.complete(v2);
1326 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1327 <
1328 <        checkCompletedNormally(h, null);
1329 <        assertEquals(r.value, subtract(v1, v2));
1345 >        checkCompletedNormally(h, subtract(v1, v2));
1346          checkCompletedNormally(f, v1);
1347          checkCompletedNormally(g, v2);
1348 <        }
1349 <    }
1348 >        assertEquals(1, r.invocationCount);
1349 >    }}
1350  
1351      /**
1352 <     * thenAcceptBoth result completes exceptionally after exceptional
1352 >     * thenCombine result completes exceptionally after exceptional
1353       * completion of either source
1354       */
1355 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1355 >    public void testThenCombine_exceptionalCompletion() {
1356          for (ExecutionMode m : ExecutionMode.values())
1357 <        for (Integer v1 : new Integer[] { 1, null }) {
1358 <
1357 >        for (boolean createIncomplete : new boolean[] { true, false })
1358 >        for (boolean fFirst : new boolean[] { true, false })
1359 >        for (Integer v1 : new Integer[] { 1, null })
1360 >    {
1361          final CompletableFuture<Integer> f = new CompletableFuture<>();
1362          final CompletableFuture<Integer> g = new CompletableFuture<>();
1345        final SubtractAction r = new SubtractAction();
1346        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1363          final CFException ex = new CFException();
1364 +        final SubtractFunction r = new SubtractFunction(m);
1365  
1366 <        f.completeExceptionally(ex);
1367 <        checkIncomplete(h);
1368 <        g.complete(v1);
1369 <
1370 <        checkCompletedWithWrappedCFException(h, ex);
1371 <        checkCompletedWithWrappedCFException(f, ex);
1372 <        assertEquals(r.invocationCount, 0);
1356 <        checkCompletedNormally(g, v1);
1366 >        (fFirst ? f : g).complete(v1);
1367 >        if (!createIncomplete)
1368 >            (!fFirst ? f : g).completeExceptionally(ex);
1369 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1370 >        if (createIncomplete) {
1371 >            checkIncomplete(h);
1372 >            (!fFirst ? f : g).completeExceptionally(ex);
1373          }
1358    }
1359
1360    public void testThenAcceptBoth_exceptionalCompletion2() {
1361        for (ExecutionMode m : ExecutionMode.values())
1362        for (Integer v1 : new Integer[] { 1, null }) {
1363
1364        final CompletableFuture<Integer> f = new CompletableFuture<>();
1365        final CompletableFuture<Integer> g = new CompletableFuture<>();
1366        final SubtractAction r = new SubtractAction();
1367        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1368        final CFException ex = new CFException();
1369
1370        g.completeExceptionally(ex);
1371        checkIncomplete(h);
1372        f.complete(v1);
1374  
1375          checkCompletedWithWrappedCFException(h, ex);
1376 <        checkCompletedWithWrappedCFException(g, ex);
1377 <        assertEquals(r.invocationCount, 0);
1378 <        checkCompletedNormally(f, v1);
1379 <        }
1379 <    }
1376 >        assertEquals(0, r.invocationCount);
1377 >        checkCompletedNormally(fFirst ? f : g, v1);
1378 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1379 >    }}
1380  
1381 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1381 >    /**
1382 >     * thenCombine result completes exceptionally if either source cancelled
1383 >     */
1384 >    public void testThenCombine_sourceCancelled() {
1385          for (ExecutionMode m : ExecutionMode.values())
1386 <        for (Integer v1 : new Integer[] { 1, null }) {
1387 <
1386 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1387 >        for (boolean createIncomplete : new boolean[] { true, false })
1388 >        for (boolean fFirst : new boolean[] { true, false })
1389 >        for (Integer v1 : new Integer[] { 1, null })
1390 >    {
1391          final CompletableFuture<Integer> f = new CompletableFuture<>();
1392          final CompletableFuture<Integer> g = new CompletableFuture<>();
1393 <        final SubtractAction r = new SubtractAction();
1388 <        final CFException ex = new CFException();
1389 <
1390 <        g.completeExceptionally(ex);
1391 <        f.complete(v1);
1392 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1393 >        final SubtractFunction r = new SubtractFunction(m);
1394  
1395 <        checkCompletedWithWrappedCFException(h, ex);
1396 <        checkCompletedWithWrappedCFException(g, ex);
1397 <        assertEquals(r.invocationCount, 0);
1398 <        checkCompletedNormally(f, v1);
1395 >        (fFirst ? f : g).complete(v1);
1396 >        if (!createIncomplete)
1397 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1398 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1399 >        if (createIncomplete) {
1400 >            checkIncomplete(h);
1401 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1402          }
1399    }
1400
1401    public void testThenAcceptBoth_exceptionalCompletion4() {
1402        for (ExecutionMode m : ExecutionMode.values())
1403        for (Integer v1 : new Integer[] { 1, null }) {
1404
1405        final CompletableFuture<Integer> f = new CompletableFuture<>();
1406        final CompletableFuture<Integer> g = new CompletableFuture<>();
1407        final SubtractAction r = new SubtractAction();
1408        final CFException ex = new CFException();
1409
1410        f.completeExceptionally(ex);
1411        g.complete(v1);
1412        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1403  
1404 <        checkCompletedWithWrappedCFException(h, ex);
1405 <        checkCompletedWithWrappedCFException(f, ex);
1406 <        assertFalse(r.ran());
1407 <        checkCompletedNormally(g, v1);
1408 <        }
1419 <    }
1404 >        checkCompletedWithWrappedCancellationException(h);
1405 >        checkCancelled(!fFirst ? f : g);
1406 >        assertEquals(0, r.invocationCount);
1407 >        checkCompletedNormally(fFirst ? f : g, v1);
1408 >    }}
1409  
1410      /**
1411 <     * thenAcceptBoth result completes exceptionally if action does
1411 >     * thenCombine result completes exceptionally if action does
1412       */
1413 <    public void testThenAcceptBoth_actionFailed1() {
1413 >    public void testThenCombine_actionFailed() {
1414          for (ExecutionMode m : ExecutionMode.values())
1415 +        for (boolean fFirst : new boolean[] { true, false })
1416          for (Integer v1 : new Integer[] { 1, null })
1417 <        for (Integer v2 : new Integer[] { 2, null }) {
1418 <
1417 >        for (Integer v2 : new Integer[] { 2, null })
1418 >    {
1419          final CompletableFuture<Integer> f = new CompletableFuture<>();
1420          final CompletableFuture<Integer> g = new CompletableFuture<>();
1421 <        final FailingBiConsumer r = new FailingBiConsumer();
1422 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1421 >        final FailingBiFunction r = new FailingBiFunction(m);
1422 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1423  
1424 <        f.complete(v1);
1425 <        checkIncomplete(h);
1426 <        g.complete(v2);
1424 >        if (fFirst) {
1425 >            f.complete(v1);
1426 >            g.complete(v2);
1427 >        } else {
1428 >            g.complete(v2);
1429 >            f.complete(v1);
1430 >        }
1431  
1432          checkCompletedWithWrappedCFException(h);
1433          checkCompletedNormally(f, v1);
1434          checkCompletedNormally(g, v2);
1435 <        }
1442 <    }
1435 >    }}
1436  
1437 <    public void testThenAcceptBoth_actionFailed2() {
1437 >    /**
1438 >     * thenAcceptBoth result completes normally after normal
1439 >     * completion of sources
1440 >     */
1441 >    public void testThenAcceptBoth_normalCompletion() {
1442          for (ExecutionMode m : ExecutionMode.values())
1443 +        for (boolean createIncomplete : new boolean[] { true, false })
1444 +        for (boolean fFirst : new boolean[] { true, false })
1445          for (Integer v1 : new Integer[] { 1, null })
1446 <        for (Integer v2 : new Integer[] { 2, null }) {
1447 <
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 FailingBiConsumer r = new FailingBiConsumer();
1452 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1450 >        final SubtractAction r = new SubtractAction(m);
1451  
1452 <        g.complete(v2);
1453 <        checkIncomplete(h);
1454 <        f.complete(v1);
1452 >        if (fFirst) f.complete(v1); else g.complete(v2);
1453 >        if (!createIncomplete)
1454 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1455 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1456 >        if (createIncomplete) {
1457 >            checkIncomplete(h);
1458 >            assertEquals(0, r.invocationCount);
1459 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1460 >        }
1461  
1462 <        checkCompletedWithWrappedCFException(h);
1462 >        checkCompletedNormally(h, null);
1463 >        assertEquals(subtract(v1, v2), r.value);
1464          checkCompletedNormally(f, v1);
1465          checkCompletedNormally(g, v2);
1466 <        }
1462 <    }
1466 >    }}
1467  
1468      /**
1469 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1469 >     * thenAcceptBoth result completes exceptionally after exceptional
1470 >     * completion of either source
1471       */
1472 <    public void testThenAcceptBoth_sourceCancelled1() {
1472 >    public void testThenAcceptBoth_exceptionalCompletion() {
1473          for (ExecutionMode m : ExecutionMode.values())
1474 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1475 <        for (Integer v1 : new Integer[] { 1, null }) {
1476 <
1474 >        for (boolean createIncomplete : new boolean[] { true, false })
1475 >        for (boolean fFirst : new boolean[] { true, false })
1476 >        for (Integer v1 : new Integer[] { 1, null })
1477 >    {
1478          final CompletableFuture<Integer> f = new CompletableFuture<>();
1479          final CompletableFuture<Integer> g = new CompletableFuture<>();
1480 <        final SubtractAction r = new SubtractAction();
1481 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1476 <
1477 <        assertTrue(f.cancel(mayInterruptIfRunning));
1478 <        checkIncomplete(h);
1479 <        g.complete(v1);
1480 <
1481 <        checkCompletedWithWrappedCancellationException(h);
1482 <        checkCancelled(f);
1483 <        assertEquals(r.invocationCount, 0);
1484 <        checkCompletedNormally(g, v1);
1485 <        }
1486 <    }
1487 <
1488 <    public void testThenAcceptBoth_sourceCancelled2() {
1489 <        for (ExecutionMode m : ExecutionMode.values())
1490 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1491 <        for (Integer v1 : new Integer[] { 1, null }) {
1480 >        final CFException ex = new CFException();
1481 >        final SubtractAction r = new SubtractAction(m);
1482  
1483 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1484 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1485 <        final SubtractAction r = new SubtractAction();
1483 >        (fFirst ? f : g).complete(v1);
1484 >        if (!createIncomplete)
1485 >            (!fFirst ? f : g).completeExceptionally(ex);
1486          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1487 <
1488 <        assertTrue(g.cancel(mayInterruptIfRunning));
1489 <        checkIncomplete(h);
1500 <        f.complete(v1);
1501 <
1502 <        checkCompletedWithWrappedCancellationException(h);
1503 <        checkCancelled(g);
1504 <        assertEquals(r.invocationCount, 0);
1505 <        checkCompletedNormally(f, v1);
1487 >        if (createIncomplete) {
1488 >            checkIncomplete(h);
1489 >            (!fFirst ? f : g).completeExceptionally(ex);
1490          }
1507    }
1508
1509    public void testThenAcceptBoth_sourceCancelled3() {
1510        for (ExecutionMode m : ExecutionMode.values())
1511        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1512        for (Integer v1 : new Integer[] { 1, null }) {
1513
1514        final CompletableFuture<Integer> f = new CompletableFuture<>();
1515        final CompletableFuture<Integer> g = new CompletableFuture<>();
1516        final SubtractAction r = new SubtractAction();
1491  
1492 <        assertTrue(g.cancel(mayInterruptIfRunning));
1493 <        f.complete(v1);
1494 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1495 <
1496 <        checkCompletedWithWrappedCancellationException(h);
1523 <        checkCancelled(g);
1524 <        assertEquals(r.invocationCount, 0);
1525 <        checkCompletedNormally(f, v1);
1526 <        }
1527 <    }
1492 >        checkCompletedWithWrappedCFException(h, ex);
1493 >        assertEquals(0, r.invocationCount);
1494 >        checkCompletedNormally(fFirst ? f : g, v1);
1495 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1496 >    }}
1497  
1498 <    public void testThenAcceptBoth_sourceCancelled4() {
1498 >    /**
1499 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1500 >     */
1501 >    public void testThenAcceptBoth_sourceCancelled() {
1502          for (ExecutionMode m : ExecutionMode.values())
1503          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1504 <        for (Integer v1 : new Integer[] { 1, null }) {
1505 <
1504 >        for (boolean createIncomplete : new boolean[] { true, false })
1505 >        for (boolean fFirst : new boolean[] { true, false })
1506 >        for (Integer v1 : new Integer[] { 1, null })
1507 >    {
1508          final CompletableFuture<Integer> f = new CompletableFuture<>();
1509          final CompletableFuture<Integer> g = new CompletableFuture<>();
1510 <        final SubtractAction r = new SubtractAction();
1510 >        final SubtractAction r = new SubtractAction(m);
1511  
1512 <        assertTrue(f.cancel(mayInterruptIfRunning));
1513 <        g.complete(v1);
1512 >        (fFirst ? f : g).complete(v1);
1513 >        if (!createIncomplete)
1514 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1515          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1516 +        if (createIncomplete) {
1517 +            checkIncomplete(h);
1518 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1519 +        }
1520  
1521          checkCompletedWithWrappedCancellationException(h);
1522 <        checkCancelled(f);
1523 <        assertEquals(r.invocationCount, 0);
1524 <        checkCompletedNormally(g, v1);
1525 <        }
1547 <    }
1522 >        checkCancelled(!fFirst ? f : g);
1523 >        assertEquals(0, r.invocationCount);
1524 >        checkCompletedNormally(fFirst ? f : g, v1);
1525 >    }}
1526  
1527      /**
1528 <     * runAfterBoth result completes normally after normal
1551 <     * completion of sources
1528 >     * thenAcceptBoth result completes exceptionally if action does
1529       */
1530 <    public void testRunAfterBoth_normalCompletion1() {
1530 >    public void testThenAcceptBoth_actionFailed() {
1531          for (ExecutionMode m : ExecutionMode.values())
1532 +        for (boolean fFirst : new boolean[] { true, false })
1533          for (Integer v1 : new Integer[] { 1, null })
1534 <        for (Integer v2 : new Integer[] { 2, null }) {
1535 <
1534 >        for (Integer v2 : new Integer[] { 2, null })
1535 >    {
1536          final CompletableFuture<Integer> f = new CompletableFuture<>();
1537          final CompletableFuture<Integer> g = new CompletableFuture<>();
1538 <        final Noop r = new Noop();
1539 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1562 <
1563 <        f.complete(v1);
1564 <        checkIncomplete(h);
1565 <        assertFalse(r.ran);
1566 <        g.complete(v2);
1538 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1539 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1540  
1541 <        checkCompletedNormally(h, null);
1542 <        assertEquals(r.invocationCount, 1);
1543 <        checkCompletedNormally(f, v1);
1544 <        checkCompletedNormally(g, v2);
1541 >        if (fFirst) {
1542 >            f.complete(v1);
1543 >            g.complete(v2);
1544 >        } else {
1545 >            g.complete(v2);
1546 >            f.complete(v1);
1547          }
1573    }
1548  
1549 <    public void testRunAfterBoth_normalCompletion2() {
1576 <        for (ExecutionMode m : ExecutionMode.values())
1577 <        for (Integer v1 : new Integer[] { 1, null })
1578 <        for (Integer v2 : new Integer[] { 2, null }) {
1579 <
1580 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1581 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1582 <        final Noop r = new Noop();
1583 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1584 <
1585 <        g.complete(v2);
1586 <        checkIncomplete(h);
1587 <        assertFalse(r.ran);
1588 <        f.complete(v1);
1589 <
1590 <        checkCompletedNormally(h, null);
1591 <        assertEquals(r.invocationCount, 1);
1549 >        checkCompletedWithWrappedCFException(h);
1550          checkCompletedNormally(f, v1);
1551          checkCompletedNormally(g, v2);
1552 <        }
1595 <    }
1552 >    }}
1553  
1554 <    public void testRunAfterBoth_normalCompletion3() {
1554 >    /**
1555 >     * runAfterBoth result completes normally after normal
1556 >     * completion of sources
1557 >     */
1558 >    public void testRunAfterBoth_normalCompletion() {
1559          for (ExecutionMode m : ExecutionMode.values())
1560 +        for (boolean createIncomplete : new boolean[] { true, false })
1561 +        for (boolean fFirst : new boolean[] { true, false })
1562          for (Integer v1 : new Integer[] { 1, null })
1563 <        for (Integer v2 : new Integer[] { 2, null }) {
1564 <
1563 >        for (Integer v2 : new Integer[] { 2, null })
1564 >    {
1565          final CompletableFuture<Integer> f = new CompletableFuture<>();
1566          final CompletableFuture<Integer> g = new CompletableFuture<>();
1567 <        final Noop r = new Noop();
1567 >        final Noop r = new Noop(m);
1568  
1569 <        g.complete(v2);
1570 <        f.complete(v1);
1569 >        if (fFirst) f.complete(v1); else g.complete(v2);
1570 >        if (!createIncomplete)
1571 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1572          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1573 <
1574 <        checkCompletedNormally(h, null);
1575 <        assertTrue(r.ran);
1576 <        checkCompletedNormally(f, v1);
1613 <        checkCompletedNormally(g, v2);
1573 >        if (createIncomplete) {
1574 >            checkIncomplete(h);
1575 >            assertEquals(0, r.invocationCount);
1576 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1577          }
1615    }
1616
1617    public void testRunAfterBoth_normalCompletion4() {
1618        for (ExecutionMode m : ExecutionMode.values())
1619        for (Integer v1 : new Integer[] { 1, null })
1620        for (Integer v2 : new Integer[] { 2, null }) {
1621
1622        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623        final CompletableFuture<Integer> g = new CompletableFuture<>();
1624        final Noop r = new Noop();
1625
1626        f.complete(v1);
1627        g.complete(v2);
1628        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1578  
1579          checkCompletedNormally(h, null);
1580 <        assertEquals(r.invocationCount, 1);
1580 >        assertEquals(1, r.invocationCount);
1581          checkCompletedNormally(f, v1);
1582          checkCompletedNormally(g, v2);
1583 <        }
1635 <    }
1583 >    }}
1584  
1585      /**
1586       * runAfterBoth result completes exceptionally after exceptional
1587       * completion of either source
1588       */
1589 <    public void testRunAfterBoth_exceptionalCompletion1() {
1589 >    public void testRunAfterBoth_exceptionalCompletion() {
1590          for (ExecutionMode m : ExecutionMode.values())
1591 <        for (Integer v1 : new Integer[] { 1, null }) {
1592 <
1593 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1594 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1647 <        final Noop r = new Noop();
1648 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1649 <        final CFException ex = new CFException();
1650 <
1651 <        f.completeExceptionally(ex);
1652 <        checkIncomplete(h);
1653 <        g.complete(v1);
1654 <
1655 <        checkCompletedWithWrappedCFException(h, ex);
1656 <        checkCompletedWithWrappedCFException(f, ex);
1657 <        assertEquals(r.invocationCount, 0);
1658 <        checkCompletedNormally(g, v1);
1659 <        }
1660 <    }
1661 <
1662 <    public void testRunAfterBoth_exceptionalCompletion2() {
1663 <        for (ExecutionMode m : ExecutionMode.values())
1664 <        for (Integer v1 : new Integer[] { 1, null }) {
1665 <
1666 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 <        final Noop r = new Noop();
1669 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 <        final CFException ex = new CFException();
1671 <
1672 <        g.completeExceptionally(ex);
1673 <        checkIncomplete(h);
1674 <        f.complete(v1);
1675 <
1676 <        checkCompletedWithWrappedCFException(h, ex);
1677 <        checkCompletedWithWrappedCFException(g, ex);
1678 <        assertEquals(r.invocationCount, 0);
1679 <        checkCompletedNormally(f, v1);
1680 <        }
1681 <    }
1682 <
1683 <    public void testRunAfterBoth_exceptionalCompletion3() {
1684 <        for (ExecutionMode m : ExecutionMode.values())
1685 <        for (Integer v1 : new Integer[] { 1, null }) {
1686 <
1591 >        for (boolean createIncomplete : new boolean[] { true, false })
1592 >        for (boolean fFirst : new boolean[] { true, false })
1593 >        for (Integer v1 : new Integer[] { 1, null })
1594 >    {
1595          final CompletableFuture<Integer> f = new CompletableFuture<>();
1596          final CompletableFuture<Integer> g = new CompletableFuture<>();
1689        final Noop r = new Noop();
1597          final CFException ex = new CFException();
1598 +        final Noop r = new Noop(m);
1599  
1600 <        g.completeExceptionally(ex);
1601 <        f.complete(v1);
1600 >        (fFirst ? f : g).complete(v1);
1601 >        if (!createIncomplete)
1602 >            (!fFirst ? f : g).completeExceptionally(ex);
1603          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1604 <
1605 <        checkCompletedWithWrappedCFException(h, ex);
1606 <        checkCompletedWithWrappedCFException(g, ex);
1698 <        assertEquals(r.invocationCount, 0);
1699 <        checkCompletedNormally(f, v1);
1604 >        if (createIncomplete) {
1605 >            checkIncomplete(h);
1606 >            (!fFirst ? f : g).completeExceptionally(ex);
1607          }
1701    }
1702
1703    public void testRunAfterBoth_exceptionalCompletion4() {
1704        for (ExecutionMode m : ExecutionMode.values())
1705        for (Integer v1 : new Integer[] { 1, null }) {
1706
1707        final CompletableFuture<Integer> f = new CompletableFuture<>();
1708        final CompletableFuture<Integer> g = new CompletableFuture<>();
1709        final Noop r = new Noop();
1710        final CFException ex = new CFException();
1711
1712        f.completeExceptionally(ex);
1713        g.complete(v1);
1714        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1608  
1609          checkCompletedWithWrappedCFException(h, ex);
1610 <        checkCompletedWithWrappedCFException(f, ex);
1611 <        assertEquals(r.invocationCount, 0);
1612 <        checkCompletedNormally(g, v1);
1613 <        }
1721 <    }
1610 >        assertEquals(0, r.invocationCount);
1611 >        checkCompletedNormally(fFirst ? f : g, v1);
1612 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1613 >    }}
1614  
1615      /**
1616 <     * runAfterBoth result completes exceptionally if action does
1616 >     * runAfterBoth result completes exceptionally if either source cancelled
1617       */
1618 <    public void testRunAfterBoth_actionFailed1() {
1618 >    public void testRunAfterBoth_sourceCancelled() {
1619          for (ExecutionMode m : ExecutionMode.values())
1620 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1621 +        for (boolean createIncomplete : new boolean[] { true, false })
1622 +        for (boolean fFirst : new boolean[] { true, false })
1623          for (Integer v1 : new Integer[] { 1, null })
1624 <        for (Integer v2 : new Integer[] { 2, null }) {
1730 <
1624 >    {
1625          final CompletableFuture<Integer> f = new CompletableFuture<>();
1626          final CompletableFuture<Integer> g = new CompletableFuture<>();
1627 <        final FailingNoop r = new FailingNoop();
1734 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1735 <
1736 <        f.complete(v1);
1737 <        checkIncomplete(h);
1738 <        g.complete(v2);
1739 <
1740 <        checkCompletedWithWrappedCFException(h);
1741 <        checkCompletedNormally(f, v1);
1742 <        checkCompletedNormally(g, v2);
1743 <        }
1744 <    }
1627 >        final Noop r = new Noop(m);
1628  
1746    public void testRunAfterBoth_actionFailed2() {
1747        for (ExecutionMode m : ExecutionMode.values())
1748        for (Integer v1 : new Integer[] { 1, null })
1749        for (Integer v2 : new Integer[] { 2, null }) {
1629  
1630 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1631 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1632 <        final FailingNoop r = new FailingNoop();
1630 >        (fFirst ? f : g).complete(v1);
1631 >        if (!createIncomplete)
1632 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1633          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1634 <
1635 <        g.complete(v2);
1636 <        checkIncomplete(h);
1758 <        f.complete(v1);
1759 <
1760 <        checkCompletedWithWrappedCFException(h);
1761 <        checkCompletedNormally(f, v1);
1762 <        checkCompletedNormally(g, v2);
1634 >        if (createIncomplete) {
1635 >            checkIncomplete(h);
1636 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1637          }
1638 <    }
1638 >
1639 >        checkCompletedWithWrappedCancellationException(h);
1640 >        checkCancelled(!fFirst ? f : g);
1641 >        assertEquals(0, r.invocationCount);
1642 >        checkCompletedNormally(fFirst ? f : g, v1);
1643 >    }}
1644  
1645      /**
1646 <     * runAfterBoth result completes exceptionally if either source cancelled
1646 >     * runAfterBoth result completes exceptionally if action does
1647       */
1648 <    public void testRunAfterBoth_sourceCancelled1() {
1648 >    public void testRunAfterBoth_actionFailed() {
1649          for (ExecutionMode m : ExecutionMode.values())
1650 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1651 <        for (Integer v1 : new Integer[] { 1, null }) {
1652 <
1650 >        for (boolean fFirst : new boolean[] { true, false })
1651 >        for (Integer v1 : new Integer[] { 1, null })
1652 >        for (Integer v2 : new Integer[] { 2, null })
1653 >    {
1654          final CompletableFuture<Integer> f = new CompletableFuture<>();
1655          final CompletableFuture<Integer> g = new CompletableFuture<>();
1656 <        final Noop r = new Noop();
1777 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1656 >        final FailingRunnable r = new FailingRunnable(m);
1657  
1658 <        assertTrue(f.cancel(mayInterruptIfRunning));
1659 <        checkIncomplete(h);
1660 <        g.complete(v1);
1661 <
1662 <        checkCompletedWithWrappedCancellationException(h);
1663 <        checkCancelled(f);
1664 <        assertEquals(r.invocationCount, 0);
1786 <        checkCompletedNormally(g, v1);
1658 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1659 >        if (fFirst) {
1660 >            f.complete(v1);
1661 >            g.complete(v2);
1662 >        } else {
1663 >            g.complete(v2);
1664 >            f.complete(v1);
1665          }
1666 <    }
1789 <
1790 <    public void testRunAfterBoth_sourceCancelled2() {
1791 <        for (ExecutionMode m : ExecutionMode.values())
1792 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1793 <        for (Integer v1 : new Integer[] { 1, null }) {
1666 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1667  
1668 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1669 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1797 <        final Noop r = new Noop();
1798 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1799 <
1800 <        assertTrue(g.cancel(mayInterruptIfRunning));
1801 <        checkIncomplete(h);
1802 <        f.complete(v1);
1803 <
1804 <        checkCompletedWithWrappedCancellationException(h);
1805 <        checkCancelled(g);
1806 <        assertEquals(r.invocationCount, 0);
1668 >        checkCompletedWithWrappedCFException(h1);
1669 >        checkCompletedWithWrappedCFException(h2);
1670          checkCompletedNormally(f, v1);
1671 <        }
1672 <    }
1810 <
1811 <    public void testRunAfterBoth_sourceCancelled3() {
1812 <        for (ExecutionMode m : ExecutionMode.values())
1813 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1814 <        for (Integer v1 : new Integer[] { 1, null }) {
1815 <
1816 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1817 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 <        final Noop r = new Noop();
1819 <
1820 <        assertTrue(g.cancel(mayInterruptIfRunning));
1821 <        f.complete(v1);
1822 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1823 <
1824 <        checkCompletedWithWrappedCancellationException(h);
1825 <        checkCancelled(g);
1826 <        assertEquals(r.invocationCount, 0);
1827 <        checkCompletedNormally(f, v1);
1828 <        }
1829 <    }
1830 <
1831 <    public void testRunAfterBoth_sourceCancelled4() {
1832 <        for (ExecutionMode m : ExecutionMode.values())
1833 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1834 <        for (Integer v1 : new Integer[] { 1, null }) {
1835 <
1836 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1837 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1838 <        final Noop r = new Noop();
1839 <
1840 <        assertTrue(f.cancel(mayInterruptIfRunning));
1841 <        g.complete(v1);
1842 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843 <
1844 <        checkCompletedWithWrappedCancellationException(h);
1845 <        checkCancelled(f);
1846 <        assertEquals(r.invocationCount, 0);
1847 <        checkCompletedNormally(g, v1);
1848 <        }
1849 <    }
1671 >        checkCompletedNormally(g, v2);
1672 >    }}
1673  
1674      /**
1675       * applyToEither result completes normally after normal completion
1676       * of either source
1677       */
1678 <    public void testApplyToEither_normalCompletion1() {
1678 >    public void testApplyToEither_normalCompletion() {
1679          for (ExecutionMode m : ExecutionMode.values())
1680 +        for (boolean createIncomplete : new boolean[] { true, false })
1681 +        for (boolean fFirst : new boolean[] { true, false })
1682          for (Integer v1 : new Integer[] { 1, null })
1683 <        for (Integer v2 : new Integer[] { 2, null }) {
1684 <
1683 >        for (Integer v2 : new Integer[] { 2, null })
1684 >    {
1685          final CompletableFuture<Integer> f = new CompletableFuture<>();
1686          final CompletableFuture<Integer> g = new CompletableFuture<>();
1687 <        final IncFunction r = new IncFunction();
1863 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1687 >        final IncFunction r = new IncFunction(m);
1688  
1689 <        f.complete(v1);
1690 <        checkCompletedNormally(h, inc(v1));
1691 <        g.complete(v2);
1689 >        if (!createIncomplete)
1690 >            if (fFirst) f.complete(v1); else g.complete(v2);
1691 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1692 >        if (createIncomplete) {
1693 >            checkIncomplete(h);
1694 >            assertEquals(0, r.invocationCount);
1695 >            if (fFirst) f.complete(v1); else g.complete(v2);
1696 >        }
1697 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1698 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1699  
1700          checkCompletedNormally(f, v1);
1701          checkCompletedNormally(g, v2);
1702 <        checkCompletedNormally(h, inc(v1));
1703 <        }
1873 <    }
1702 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1703 >    }}
1704  
1705 <    public void testApplyToEither_normalCompletion2() {
1705 >    public void testApplyToEither_normalCompletionBothAvailable() {
1706          for (ExecutionMode m : ExecutionMode.values())
1707 +        for (boolean fFirst : new boolean[] { true, false })
1708          for (Integer v1 : new Integer[] { 1, null })
1709 <        for (Integer v2 : new Integer[] { 2, null }) {
1710 <
1709 >        for (Integer v2 : new Integer[] { 2, null })
1710 >    {
1711          final CompletableFuture<Integer> f = new CompletableFuture<>();
1712          final CompletableFuture<Integer> g = new CompletableFuture<>();
1713 <        final IncFunction r = new IncFunction();
1883 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1884 <
1885 <        g.complete(v2);
1886 <        checkCompletedNormally(h, inc(v2));
1887 <        f.complete(v1);
1713 >        final IncFunction r = new IncFunction(m);
1714  
1715 <        checkCompletedNormally(f, v1);
1716 <        checkCompletedNormally(g, v2);
1717 <        checkCompletedNormally(h, inc(v2));
1715 >        if (fFirst) {
1716 >            f.complete(v1);
1717 >            g.complete(v2);
1718 >        } else {
1719 >            g.complete(v2);
1720 >            f.complete(v1);
1721          }
1893    }
1894    public void testApplyToEither_normalCompletion3() {
1895        for (ExecutionMode m : ExecutionMode.values())
1896        for (Integer v1 : new Integer[] { 1, null })
1897        for (Integer v2 : new Integer[] { 2, null }) {
1898
1899        final CompletableFuture<Integer> f = new CompletableFuture<>();
1900        final CompletableFuture<Integer> g = new CompletableFuture<>();
1901        final IncFunction r = new IncFunction();
1722  
1903        f.complete(v1);
1904        g.complete(v2);
1723          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1724  
1725          checkCompletedNormally(f, v1);
# Line 1910 | Line 1728 | public class CompletableFutureTest exten
1728          // unspecified behavior
1729          assertTrue(Objects.equals(h.join(), inc(v1)) ||
1730                     Objects.equals(h.join(), inc(v2)));
1731 <        assertEquals(r.invocationCount, 1);
1732 <        }
1915 <    }
1731 >        assertEquals(1, r.invocationCount);
1732 >    }}
1733  
1734      /**
1735       * applyToEither result completes exceptionally after exceptional
# Line 1920 | Line 1737 | public class CompletableFutureTest exten
1737       */
1738      public void testApplyToEither_exceptionalCompletion1() {
1739          for (ExecutionMode m : ExecutionMode.values())
1740 <        for (Integer v1 : new Integer[] { 1, null }) {
1741 <
1740 >        for (boolean createIncomplete : new boolean[] { true, false })
1741 >        for (boolean fFirst : new boolean[] { true, false })
1742 >        for (Integer v1 : new Integer[] { 1, null })
1743 >    {
1744          final CompletableFuture<Integer> f = new CompletableFuture<>();
1745          final CompletableFuture<Integer> g = new CompletableFuture<>();
1927        final IncFunction r = new IncFunction();
1928        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1746          final CFException ex = new CFException();
1747 +        final IncFunction r = new IncFunction(m);
1748  
1749 <        f.completeExceptionally(ex);
1932 <        checkCompletedWithWrappedCFException(h, ex);
1933 <        g.complete(v1);
1934 <
1935 <        assertEquals(r.invocationCount, 0);
1936 <        checkCompletedNormally(g, v1);
1937 <        checkCompletedWithWrappedCFException(f, ex);
1938 <        checkCompletedWithWrappedCFException(h, ex);
1939 <        }
1940 <    }
1941 <
1942 <    public void testApplyToEither_exceptionalCompletion2() {
1943 <        for (ExecutionMode m : ExecutionMode.values())
1944 <        for (Integer v1 : new Integer[] { 1, null }) {
1945 <
1946 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1947 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1948 <        final IncFunction r = new IncFunction();
1749 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1750          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1751 <        final CFException ex = new CFException();
1751 >        if (createIncomplete) {
1752 >            checkIncomplete(h);
1753 >            assertEquals(0, r.invocationCount);
1754 >            (fFirst ? f : g).completeExceptionally(ex);
1755 >        }
1756  
1952        g.completeExceptionally(ex);
1757          checkCompletedWithWrappedCFException(h, ex);
1758 <        f.complete(v1);
1758 >        (!fFirst ? f : g).complete(v1);
1759  
1760 <        assertEquals(r.invocationCount, 0);
1761 <        checkCompletedNormally(f, v1);
1762 <        checkCompletedWithWrappedCFException(g, ex);
1760 >        assertEquals(0, r.invocationCount);
1761 >        checkCompletedNormally(!fFirst ? f : g, v1);
1762 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1763          checkCompletedWithWrappedCFException(h, ex);
1764 <        }
1961 <    }
1764 >    }}
1765  
1766 <    public void testApplyToEither_exceptionalCompletion3() {
1766 >    public void testApplyToEither_exceptionalCompletion2() {
1767          for (ExecutionMode m : ExecutionMode.values())
1768 <        for (Integer v1 : new Integer[] { 1, null }) {
1769 <
1768 >        for (boolean reverseArgs : new boolean[] { true, false })
1769 >        for (boolean fFirst : new boolean[] { true, false })
1770 >        for (Integer v1 : new Integer[] { 1, null })
1771 >    {
1772          final CompletableFuture<Integer> f = new CompletableFuture<>();
1773          final CompletableFuture<Integer> g = new CompletableFuture<>();
1774 <        final IncFunction r = new IncFunction();
1774 >        final IncFunction r1 = new IncFunction(m);
1775 >        final IncFunction r2 = new IncFunction(m);
1776          final CFException ex = new CFException();
1777 <
1778 <        g.completeExceptionally(ex);
1779 <        f.complete(v1);
1780 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1777 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1778 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1779 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1780 >        if (fFirst) {
1781 >            f.complete(v1);
1782 >            g.completeExceptionally(ex);
1783 >        } else {
1784 >            g.completeExceptionally(ex);
1785 >            f.complete(v1);
1786 >        }
1787 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1788  
1789          // unspecified behavior
1977        Integer v;
1790          try {
1791 <            assertEquals(h.join(), inc(v1));
1792 <            assertEquals(r.invocationCount, 1);
1791 >            assertEquals(inc(v1), h1.join());
1792 >            assertEquals(1, r1.invocationCount);
1793          } catch (CompletionException ok) {
1794 <            checkCompletedWithWrappedCFException(h, ex);
1795 <            assertEquals(r.invocationCount, 0);
1794 >            checkCompletedWithWrappedCFException(h1, ex);
1795 >            assertEquals(0, r1.invocationCount);
1796          }
1797  
1986        checkCompletedWithWrappedCFException(g, ex);
1987        checkCompletedNormally(f, v1);
1988        }
1989    }
1990
1991    public void testApplyToEither_exceptionalCompletion4() {
1992        for (ExecutionMode m : ExecutionMode.values())
1993        for (Integer v1 : new Integer[] { 1, null }) {
1994
1995        final CompletableFuture<Integer> f = new CompletableFuture<>();
1996        final CompletableFuture<Integer> g = new CompletableFuture<>();
1997        final IncFunction r = new IncFunction();
1998        final CFException ex = new CFException();
1999
2000        f.completeExceptionally(ex);
2001        g.complete(v1);
2002        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2003
2004        // unspecified behavior
2005        Integer v;
1798          try {
1799 <            assertEquals(h.join(), inc(v1));
1800 <            assertEquals(r.invocationCount, 1);
1799 >            assertEquals(inc(v1), h2.join());
1800 >            assertEquals(1, r2.invocationCount);
1801          } catch (CompletionException ok) {
1802 <            checkCompletedWithWrappedCFException(h, ex);
1803 <            assertEquals(r.invocationCount, 0);
1802 >            checkCompletedWithWrappedCFException(h2, ex);
1803 >            assertEquals(0, r2.invocationCount);
1804          }
1805  
1806 <        checkCompletedWithWrappedCFException(f, ex);
1807 <        checkCompletedNormally(g, v1);
1808 <        }
2017 <    }
1806 >        checkCompletedWithWrappedCFException(g, ex);
1807 >        checkCompletedNormally(f, v1);
1808 >    }}
1809  
1810      /**
1811       * applyToEither result completes exceptionally if action does
# Line 2022 | Line 1813 | public class CompletableFutureTest exten
1813      public void testApplyToEither_actionFailed1() {
1814          for (ExecutionMode m : ExecutionMode.values())
1815          for (Integer v1 : new Integer[] { 1, null })
1816 <        for (Integer v2 : new Integer[] { 2, null }) {
1817 <
1816 >        for (Integer v2 : new Integer[] { 2, null })
1817 >    {
1818          final CompletableFuture<Integer> f = new CompletableFuture<>();
1819          final CompletableFuture<Integer> g = new CompletableFuture<>();
1820 <        final FailingFunction r = new FailingFunction();
1820 >        final FailingFunction r = new FailingFunction(m);
1821          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1822  
1823          f.complete(v1);
# Line 2034 | Line 1825 | public class CompletableFutureTest exten
1825          g.complete(v2);
1826          checkCompletedNormally(f, v1);
1827          checkCompletedNormally(g, v2);
1828 <        }
2038 <    }
1828 >    }}
1829  
1830      public void testApplyToEither_actionFailed2() {
1831          for (ExecutionMode m : ExecutionMode.values())
1832          for (Integer v1 : new Integer[] { 1, null })
1833 <        for (Integer v2 : new Integer[] { 2, null }) {
1834 <
1833 >        for (Integer v2 : new Integer[] { 2, null })
1834 >    {
1835          final CompletableFuture<Integer> f = new CompletableFuture<>();
1836          final CompletableFuture<Integer> g = new CompletableFuture<>();
1837 <        final FailingFunction r = new FailingFunction();
1837 >        final FailingFunction r = new FailingFunction(m);
1838          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1839  
1840          g.complete(v2);
# Line 2052 | Line 1842 | public class CompletableFutureTest exten
1842          f.complete(v1);
1843          checkCompletedNormally(f, v1);
1844          checkCompletedNormally(g, v2);
1845 <        }
2056 <    }
1845 >    }}
1846  
1847      /**
1848       * applyToEither result completes exceptionally if either source cancelled
# Line 2061 | Line 1850 | public class CompletableFutureTest exten
1850      public void testApplyToEither_sourceCancelled1() {
1851          for (ExecutionMode m : ExecutionMode.values())
1852          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1853 <        for (Integer v1 : new Integer[] { 1, null }) {
1854 <
1853 >        for (boolean createIncomplete : new boolean[] { true, false })
1854 >        for (boolean fFirst : new boolean[] { true, false })
1855 >        for (Integer v1 : new Integer[] { 1, null })
1856 >    {
1857          final CompletableFuture<Integer> f = new CompletableFuture<>();
1858          final CompletableFuture<Integer> g = new CompletableFuture<>();
1859 <        final IncFunction r = new IncFunction();
1859 >        final IncFunction r = new IncFunction(m);
1860 >
1861 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1862          final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1863 +        if (createIncomplete) {
1864 +            checkIncomplete(h);
1865 +            assertEquals(0, r.invocationCount);
1866 +            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1867 +        }
1868  
2071        assertTrue(f.cancel(mayInterruptIfRunning));
1869          checkCompletedWithWrappedCancellationException(h);
1870 <        g.complete(v1);
1870 >        (!fFirst ? f : g).complete(v1);
1871  
1872 <        checkCancelled(f);
1873 <        assertEquals(r.invocationCount, 0);
1874 <        checkCompletedNormally(g, v1);
1872 >        assertEquals(0, r.invocationCount);
1873 >        checkCompletedNormally(!fFirst ? f : g, v1);
1874 >        checkCancelled(fFirst ? f : g);
1875          checkCompletedWithWrappedCancellationException(h);
1876 <        }
2080 <    }
1876 >    }}
1877  
1878      public void testApplyToEither_sourceCancelled2() {
1879          for (ExecutionMode m : ExecutionMode.values())
1880          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1881 <        for (Integer v1 : new Integer[] { 1, null }) {
1882 <
1881 >        for (boolean reverseArgs : new boolean[] { true, false })
1882 >        for (boolean fFirst : new boolean[] { true, false })
1883 >        for (Integer v1 : new Integer[] { 1, null })
1884 >    {
1885          final CompletableFuture<Integer> f = new CompletableFuture<>();
1886          final CompletableFuture<Integer> g = new CompletableFuture<>();
1887 <        final IncFunction r = new IncFunction();
1888 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1889 <
1890 <        assertTrue(g.cancel(mayInterruptIfRunning));
1891 <        checkCompletedWithWrappedCancellationException(h);
2094 <        f.complete(v1);
1887 >        final IncFunction r1 = new IncFunction(m);
1888 >        final IncFunction r2 = new IncFunction(m);
1889 >        final CFException ex = new CFException();
1890 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1891 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1892  
1893 <        checkCancelled(g);
1894 <        assertEquals(r.invocationCount, 0);
1895 <        checkCompletedNormally(f, v1);
1896 <        checkCompletedWithWrappedCancellationException(h);
1893 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1894 >        if (fFirst) {
1895 >            f.complete(v1);
1896 >            assertTrue(g.cancel(mayInterruptIfRunning));
1897 >        } else {
1898 >            assertTrue(g.cancel(mayInterruptIfRunning));
1899 >            f.complete(v1);
1900          }
1901 <    }
2102 <
2103 <    public void testApplyToEither_sourceCancelled3() {
2104 <        for (ExecutionMode m : ExecutionMode.values())
2105 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2106 <        for (Integer v1 : new Integer[] { 1, null }) {
2107 <
2108 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2109 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2110 <        final IncFunction r = new IncFunction();
2111 <
2112 <        assertTrue(g.cancel(mayInterruptIfRunning));
2113 <        f.complete(v1);
2114 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1901 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1902  
1903          // unspecified behavior
2117        Integer v;
1904          try {
1905 <            assertEquals(h.join(), inc(v1));
1906 <            assertEquals(r.invocationCount, 1);
1905 >            assertEquals(inc(v1), h1.join());
1906 >            assertEquals(1, r1.invocationCount);
1907          } catch (CompletionException ok) {
1908 <            checkCompletedWithWrappedCancellationException(h);
1909 <            assertEquals(r.invocationCount, 0);
1908 >            checkCompletedWithWrappedCancellationException(h1);
1909 >            assertEquals(0, r1.invocationCount);
1910          }
1911  
2126        checkCancelled(g);
2127        checkCompletedNormally(f, v1);
2128        }
2129    }
2130
2131    public void testApplyToEither_sourceCancelled4() {
2132        for (ExecutionMode m : ExecutionMode.values())
2133        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2134        for (Integer v1 : new Integer[] { 1, null }) {
2135
2136        final CompletableFuture<Integer> f = new CompletableFuture<>();
2137        final CompletableFuture<Integer> g = new CompletableFuture<>();
2138        final IncFunction r = new IncFunction();
2139
2140        assertTrue(f.cancel(mayInterruptIfRunning));
2141        g.complete(v1);
2142        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2143
2144        // unspecified behavior
2145        Integer v;
1912          try {
1913 <            assertEquals(h.join(), inc(v1));
1914 <            assertEquals(r.invocationCount, 1);
1913 >            assertEquals(inc(v1), h2.join());
1914 >            assertEquals(1, r2.invocationCount);
1915          } catch (CompletionException ok) {
1916 <            checkCompletedWithWrappedCancellationException(h);
1917 <            assertEquals(r.invocationCount, 0);
1916 >            checkCompletedWithWrappedCancellationException(h2);
1917 >            assertEquals(0, r2.invocationCount);
1918          }
1919  
1920 <        checkCancelled(f);
1921 <        checkCompletedNormally(g, v1);
1922 <        }
2157 <    }
1920 >        checkCancelled(g);
1921 >        checkCompletedNormally(f, v1);
1922 >    }}
1923  
1924      /**
1925       * acceptEither result completes normally after normal completion
# Line 2163 | Line 1928 | public class CompletableFutureTest exten
1928      public void testAcceptEither_normalCompletion1() {
1929          for (ExecutionMode m : ExecutionMode.values())
1930          for (Integer v1 : new Integer[] { 1, null })
1931 <        for (Integer v2 : new Integer[] { 2, null }) {
1932 <
1931 >        for (Integer v2 : new Integer[] { 2, null })
1932 >    {
1933          final CompletableFuture<Integer> f = new CompletableFuture<>();
1934          final CompletableFuture<Integer> g = new CompletableFuture<>();
1935          final IncAction r = new IncAction();
# Line 2172 | Line 1937 | public class CompletableFutureTest exten
1937  
1938          f.complete(v1);
1939          checkCompletedNormally(h, null);
1940 <        assertEquals(r.value, inc(v1));
1940 >        assertEquals(inc(v1), r.value);
1941          g.complete(v2);
1942  
1943          checkCompletedNormally(f, v1);
1944          checkCompletedNormally(g, v2);
1945          checkCompletedNormally(h, null);
1946 <        }
2182 <    }
1946 >    }}
1947  
1948      public void testAcceptEither_normalCompletion2() {
1949          for (ExecutionMode m : ExecutionMode.values())
1950          for (Integer v1 : new Integer[] { 1, null })
1951 <        for (Integer v2 : new Integer[] { 2, null }) {
1952 <
1951 >        for (Integer v2 : new Integer[] { 2, null })
1952 >    {
1953          final CompletableFuture<Integer> f = new CompletableFuture<>();
1954          final CompletableFuture<Integer> g = new CompletableFuture<>();
1955          final IncAction r = new IncAction();
# Line 2193 | Line 1957 | public class CompletableFutureTest exten
1957  
1958          g.complete(v2);
1959          checkCompletedNormally(h, null);
1960 <        assertEquals(r.value, inc(v2));
1960 >        assertEquals(inc(v2), r.value);
1961          f.complete(v1);
1962  
1963          checkCompletedNormally(f, v1);
1964          checkCompletedNormally(g, v2);
1965          checkCompletedNormally(h, null);
1966 <        }
1967 <    }
1966 >    }}
1967 >
1968      public void testAcceptEither_normalCompletion3() {
1969          for (ExecutionMode m : ExecutionMode.values())
1970          for (Integer v1 : new Integer[] { 1, null })
1971 <        for (Integer v2 : new Integer[] { 2, null }) {
1972 <
1971 >        for (Integer v2 : new Integer[] { 2, null })
1972 >    {
1973          final CompletableFuture<Integer> f = new CompletableFuture<>();
1974          final CompletableFuture<Integer> g = new CompletableFuture<>();
1975          final IncAction r = new IncAction();
# Line 2221 | Line 1985 | public class CompletableFutureTest exten
1985          // unspecified behavior
1986          assertTrue(Objects.equals(r.value, inc(v1)) ||
1987                     Objects.equals(r.value, inc(v2)));
1988 <        }
2225 <    }
1988 >    }}
1989  
1990      /**
1991       * acceptEither result completes exceptionally after exceptional
# Line 2230 | Line 1993 | public class CompletableFutureTest exten
1993       */
1994      public void testAcceptEither_exceptionalCompletion1() {
1995          for (ExecutionMode m : ExecutionMode.values())
1996 <        for (Integer v1 : new Integer[] { 1, null }) {
1997 <
1996 >        for (Integer v1 : new Integer[] { 1, null })
1997 >    {
1998          final CompletableFuture<Integer> f = new CompletableFuture<>();
1999          final CompletableFuture<Integer> g = new CompletableFuture<>();
2000          final IncAction r = new IncAction();
# Line 2242 | Line 2005 | public class CompletableFutureTest exten
2005          checkCompletedWithWrappedCFException(h, ex);
2006          g.complete(v1);
2007  
2008 <        assertEquals(r.invocationCount, 0);
2008 >        assertEquals(0, r.invocationCount);
2009          checkCompletedNormally(g, v1);
2010          checkCompletedWithWrappedCFException(f, ex);
2011          checkCompletedWithWrappedCFException(h, ex);
2012 <        }
2250 <    }
2012 >    }}
2013  
2014      public void testAcceptEither_exceptionalCompletion2() {
2015          for (ExecutionMode m : ExecutionMode.values())
2016 <        for (Integer v1 : new Integer[] { 1, null }) {
2017 <
2016 >        for (Integer v1 : new Integer[] { 1, null })
2017 >    {
2018          final CompletableFuture<Integer> f = new CompletableFuture<>();
2019          final CompletableFuture<Integer> g = new CompletableFuture<>();
2020          final IncAction r = new IncAction();
# Line 2263 | Line 2025 | public class CompletableFutureTest exten
2025          checkCompletedWithWrappedCFException(h, ex);
2026          f.complete(v1);
2027  
2028 <        assertEquals(r.invocationCount, 0);
2028 >        assertEquals(0, r.invocationCount);
2029          checkCompletedNormally(f, v1);
2030          checkCompletedWithWrappedCFException(g, ex);
2031          checkCompletedWithWrappedCFException(h, ex);
2032 <        }
2271 <    }
2032 >    }}
2033  
2034      public void testAcceptEither_exceptionalCompletion3() {
2035          for (ExecutionMode m : ExecutionMode.values())
2036 <        for (Integer v1 : new Integer[] { 1, null }) {
2037 <
2036 >        for (Integer v1 : new Integer[] { 1, null })
2037 >    {
2038          final CompletableFuture<Integer> f = new CompletableFuture<>();
2039          final CompletableFuture<Integer> g = new CompletableFuture<>();
2040          final IncAction r = new IncAction();
# Line 2286 | Line 2047 | public class CompletableFutureTest exten
2047          // unspecified behavior
2048          Integer v;
2049          try {
2050 <            assertEquals(h.join(), null);
2051 <            assertEquals(r.invocationCount, 1);
2050 >            assertNull(h.join());
2051 >            assertEquals(1, r.invocationCount);
2052              assertEquals(inc(v1), r.value);
2053          } catch (CompletionException ok) {
2054              checkCompletedWithWrappedCFException(h, ex);
2055 <            assertEquals(r.invocationCount, 0);
2055 >            assertEquals(0, r.invocationCount);
2056          }
2057  
2058          checkCompletedWithWrappedCFException(g, ex);
2059          checkCompletedNormally(f, v1);
2060 <        }
2300 <    }
2060 >    }}
2061  
2062      public void testAcceptEither_exceptionalCompletion4() {
2063          for (ExecutionMode m : ExecutionMode.values())
2064 <        for (Integer v1 : new Integer[] { 1, null }) {
2065 <
2064 >        for (Integer v1 : new Integer[] { 1, null })
2065 >    {
2066          final CompletableFuture<Integer> f = new CompletableFuture<>();
2067          final CompletableFuture<Integer> g = new CompletableFuture<>();
2068          final IncAction r = new IncAction();
# Line 2315 | Line 2075 | public class CompletableFutureTest exten
2075          // unspecified behavior
2076          Integer v;
2077          try {
2078 <            assertEquals(h.join(), null);
2079 <            assertEquals(r.invocationCount, 1);
2078 >            assertNull(h.join());
2079 >            assertEquals(1, r.invocationCount);
2080              assertEquals(inc(v1), r.value);
2081          } catch (CompletionException ok) {
2082              checkCompletedWithWrappedCFException(h, ex);
2083 <            assertEquals(r.invocationCount, 0);
2083 >            assertEquals(0, r.invocationCount);
2084          }
2085  
2086          checkCompletedWithWrappedCFException(f, ex);
2087          checkCompletedNormally(g, v1);
2088 <        }
2329 <    }
2088 >    }}
2089  
2090      /**
2091       * acceptEither result completes exceptionally if action does
# Line 2334 | Line 2093 | public class CompletableFutureTest exten
2093      public void testAcceptEither_actionFailed1() {
2094          for (ExecutionMode m : ExecutionMode.values())
2095          for (Integer v1 : new Integer[] { 1, null })
2096 <        for (Integer v2 : new Integer[] { 2, null }) {
2097 <
2096 >        for (Integer v2 : new Integer[] { 2, null })
2097 >    {
2098          final CompletableFuture<Integer> f = new CompletableFuture<>();
2099          final CompletableFuture<Integer> g = new CompletableFuture<>();
2100 <        final FailingConsumer r = new FailingConsumer();
2100 >        final FailingConsumer r = new FailingConsumer(m);
2101          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2102  
2103          f.complete(v1);
# Line 2346 | Line 2105 | public class CompletableFutureTest exten
2105          g.complete(v2);
2106          checkCompletedNormally(f, v1);
2107          checkCompletedNormally(g, v2);
2108 <        }
2350 <    }
2108 >    }}
2109  
2110      public void testAcceptEither_actionFailed2() {
2111          for (ExecutionMode m : ExecutionMode.values())
2112          for (Integer v1 : new Integer[] { 1, null })
2113 <        for (Integer v2 : new Integer[] { 2, null }) {
2114 <
2113 >        for (Integer v2 : new Integer[] { 2, null })
2114 >    {
2115          final CompletableFuture<Integer> f = new CompletableFuture<>();
2116          final CompletableFuture<Integer> g = new CompletableFuture<>();
2117 <        final FailingConsumer r = new FailingConsumer();
2117 >        final FailingConsumer r = new FailingConsumer(m);
2118          final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2119  
2120          g.complete(v2);
# Line 2364 | Line 2122 | public class CompletableFutureTest exten
2122          f.complete(v1);
2123          checkCompletedNormally(f, v1);
2124          checkCompletedNormally(g, v2);
2125 <        }
2368 <    }
2125 >    }}
2126  
2127      /**
2128       * acceptEither result completes exceptionally if either source cancelled
# Line 2373 | Line 2130 | public class CompletableFutureTest exten
2130      public void testAcceptEither_sourceCancelled1() {
2131          for (ExecutionMode m : ExecutionMode.values())
2132          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2133 <        for (Integer v1 : new Integer[] { 1, null }) {
2134 <
2133 >        for (Integer v1 : new Integer[] { 1, null })
2134 >    {
2135          final CompletableFuture<Integer> f = new CompletableFuture<>();
2136          final CompletableFuture<Integer> g = new CompletableFuture<>();
2137          final IncAction r = new IncAction();
# Line 2385 | Line 2142 | public class CompletableFutureTest exten
2142          g.complete(v1);
2143  
2144          checkCancelled(f);
2145 <        assertEquals(r.invocationCount, 0);
2145 >        assertEquals(0, r.invocationCount);
2146          checkCompletedNormally(g, v1);
2147          checkCompletedWithWrappedCancellationException(h);
2148 <        }
2392 <    }
2148 >    }}
2149  
2150      public void testAcceptEither_sourceCancelled2() {
2151          for (ExecutionMode m : ExecutionMode.values())
2152          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2153 <        for (Integer v1 : new Integer[] { 1, null }) {
2154 <
2153 >        for (Integer v1 : new Integer[] { 1, null })
2154 >    {
2155          final CompletableFuture<Integer> f = new CompletableFuture<>();
2156          final CompletableFuture<Integer> g = new CompletableFuture<>();
2157          final IncAction r = new IncAction();
# Line 2406 | Line 2162 | public class CompletableFutureTest exten
2162          f.complete(v1);
2163  
2164          checkCancelled(g);
2165 <        assertEquals(r.invocationCount, 0);
2165 >        assertEquals(0, r.invocationCount);
2166          checkCompletedNormally(f, v1);
2167          checkCompletedWithWrappedCancellationException(h);
2168 <        }
2413 <    }
2168 >    }}
2169  
2170      public void testAcceptEither_sourceCancelled3() {
2171          for (ExecutionMode m : ExecutionMode.values())
2172          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2173 <        for (Integer v1 : new Integer[] { 1, null }) {
2174 <
2173 >        for (Integer v1 : new Integer[] { 1, null })
2174 >    {
2175          final CompletableFuture<Integer> f = new CompletableFuture<>();
2176          final CompletableFuture<Integer> g = new CompletableFuture<>();
2177          final IncAction r = new IncAction();
# Line 2428 | Line 2183 | public class CompletableFutureTest exten
2183          // unspecified behavior
2184          Integer v;
2185          try {
2186 <            assertEquals(h.join(), null);
2187 <            assertEquals(r.invocationCount, 1);
2186 >            assertNull(h.join());
2187 >            assertEquals(1, r.invocationCount);
2188              assertEquals(inc(v1), r.value);
2189          } catch (CompletionException ok) {
2190              checkCompletedWithWrappedCancellationException(h);
2191 <            assertEquals(r.invocationCount, 0);
2191 >            assertEquals(0, r.invocationCount);
2192          }
2193  
2194          checkCancelled(g);
2195          checkCompletedNormally(f, v1);
2196 <        }
2442 <    }
2196 >    }}
2197  
2198      public void testAcceptEither_sourceCancelled4() {
2199          for (ExecutionMode m : ExecutionMode.values())
2200          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2201 <        for (Integer v1 : new Integer[] { 1, null }) {
2202 <
2201 >        for (Integer v1 : new Integer[] { 1, null })
2202 >    {
2203          final CompletableFuture<Integer> f = new CompletableFuture<>();
2204          final CompletableFuture<Integer> g = new CompletableFuture<>();
2205          final IncAction r = new IncAction();
# Line 2457 | Line 2211 | public class CompletableFutureTest exten
2211          // unspecified behavior
2212          Integer v;
2213          try {
2214 <            assertEquals(h.join(), null);
2215 <            assertEquals(r.invocationCount, 1);
2214 >            assertNull(h.join());
2215 >            assertEquals(1, r.invocationCount);
2216              assertEquals(inc(v1), r.value);
2217          } catch (CompletionException ok) {
2218              checkCompletedWithWrappedCancellationException(h);
2219 <            assertEquals(r.invocationCount, 0);
2219 >            assertEquals(0, r.invocationCount);
2220          }
2221  
2222          checkCancelled(f);
2223          checkCompletedNormally(g, v1);
2224 <        }
2471 <    }
2224 >    }}
2225  
2226      /**
2227       * runAfterEither result completes normally after normal completion
# Line 2477 | Line 2230 | public class CompletableFutureTest exten
2230      public void testRunAfterEither_normalCompletion1() {
2231          for (ExecutionMode m : ExecutionMode.values())
2232          for (Integer v1 : new Integer[] { 1, null })
2233 <        for (Integer v2 : new Integer[] { 2, null }) {
2234 <
2233 >        for (Integer v2 : new Integer[] { 2, null })
2234 >    {
2235          final CompletableFuture<Integer> f = new CompletableFuture<>();
2236          final CompletableFuture<Integer> g = new CompletableFuture<>();
2237 <        final Noop r = new Noop();
2237 >        final Noop r = new Noop(m);
2238          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2239  
2240          f.complete(v1);
2241          checkCompletedNormally(h, null);
2242 <        assertEquals(r.invocationCount, 1);
2242 >        assertEquals(1, r.invocationCount);
2243          g.complete(v2);
2244  
2245          checkCompletedNormally(f, v1);
2246          checkCompletedNormally(g, v2);
2247          checkCompletedNormally(h, null);
2248 <        assertEquals(r.invocationCount, 1);
2249 <        }
2497 <    }
2248 >        assertEquals(1, r.invocationCount);
2249 >    }}
2250  
2251      public void testRunAfterEither_normalCompletion2() {
2252          for (ExecutionMode m : ExecutionMode.values())
2253          for (Integer v1 : new Integer[] { 1, null })
2254 <        for (Integer v2 : new Integer[] { 2, null }) {
2255 <
2254 >        for (Integer v2 : new Integer[] { 2, null })
2255 >    {
2256          final CompletableFuture<Integer> f = new CompletableFuture<>();
2257          final CompletableFuture<Integer> g = new CompletableFuture<>();
2258 <        final Noop r = new Noop();
2258 >        final Noop r = new Noop(m);
2259          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2260  
2261          g.complete(v2);
2262          checkCompletedNormally(h, null);
2263 <        assertEquals(r.invocationCount, 1);
2263 >        assertEquals(1, r.invocationCount);
2264          f.complete(v1);
2265  
2266          checkCompletedNormally(f, v1);
2267          checkCompletedNormally(g, v2);
2268          checkCompletedNormally(h, null);
2269 <        assertEquals(r.invocationCount, 1);
2270 <        }
2271 <    }
2269 >        assertEquals(1, r.invocationCount);
2270 >        }}
2271 >
2272      public void testRunAfterEither_normalCompletion3() {
2273          for (ExecutionMode m : ExecutionMode.values())
2274          for (Integer v1 : new Integer[] { 1, null })
2275 <        for (Integer v2 : new Integer[] { 2, null }) {
2276 <
2275 >        for (Integer v2 : new Integer[] { 2, null })
2276 >    {
2277          final CompletableFuture<Integer> f = new CompletableFuture<>();
2278          final CompletableFuture<Integer> g = new CompletableFuture<>();
2279 <        final Noop r = new Noop();
2279 >        final Noop r = new Noop(m);
2280  
2281          f.complete(v1);
2282          g.complete(v2);
# Line 2533 | Line 2285 | public class CompletableFutureTest exten
2285          checkCompletedNormally(h, null);
2286          checkCompletedNormally(f, v1);
2287          checkCompletedNormally(g, v2);
2288 <        assertEquals(r.invocationCount, 1);
2289 <        }
2538 <    }
2288 >        assertEquals(1, r.invocationCount);
2289 >    }}
2290  
2291      /**
2292       * runAfterEither result completes exceptionally after exceptional
# Line 2543 | Line 2294 | public class CompletableFutureTest exten
2294       */
2295      public void testRunAfterEither_exceptionalCompletion1() {
2296          for (ExecutionMode m : ExecutionMode.values())
2297 <        for (Integer v1 : new Integer[] { 1, null }) {
2298 <
2297 >        for (Integer v1 : new Integer[] { 1, null })
2298 >    {
2299          final CompletableFuture<Integer> f = new CompletableFuture<>();
2300          final CompletableFuture<Integer> g = new CompletableFuture<>();
2301 <        final Noop r = new Noop();
2301 >        final Noop r = new Noop(m);
2302          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2303          final CFException ex = new CFException();
2304  
# Line 2555 | Line 2306 | public class CompletableFutureTest exten
2306          checkCompletedWithWrappedCFException(h, ex);
2307          g.complete(v1);
2308  
2309 <        assertEquals(r.invocationCount, 0);
2309 >        assertEquals(0, r.invocationCount);
2310          checkCompletedNormally(g, v1);
2311          checkCompletedWithWrappedCFException(f, ex);
2312          checkCompletedWithWrappedCFException(h, ex);
2313 <        }
2563 <    }
2313 >    }}
2314  
2315      public void testRunAfterEither_exceptionalCompletion2() {
2316          for (ExecutionMode m : ExecutionMode.values())
2317 <        for (Integer v1 : new Integer[] { 1, null }) {
2318 <
2317 >        for (Integer v1 : new Integer[] { 1, null })
2318 >    {
2319          final CompletableFuture<Integer> f = new CompletableFuture<>();
2320          final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 <        final Noop r = new Noop();
2321 >        final Noop r = new Noop(m);
2322          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2323          final CFException ex = new CFException();
2324  
# Line 2576 | Line 2326 | public class CompletableFutureTest exten
2326          checkCompletedWithWrappedCFException(h, ex);
2327          f.complete(v1);
2328  
2329 <        assertEquals(r.invocationCount, 0);
2329 >        assertEquals(0, r.invocationCount);
2330          checkCompletedNormally(f, v1);
2331          checkCompletedWithWrappedCFException(g, ex);
2332          checkCompletedWithWrappedCFException(h, ex);
2333 <        }
2584 <    }
2333 >    }}
2334  
2335      public void testRunAfterEither_exceptionalCompletion3() {
2336          for (ExecutionMode m : ExecutionMode.values())
2337 <        for (Integer v1 : new Integer[] { 1, null }) {
2338 <
2337 >        for (Integer v1 : new Integer[] { 1, null })
2338 >    {
2339          final CompletableFuture<Integer> f = new CompletableFuture<>();
2340          final CompletableFuture<Integer> g = new CompletableFuture<>();
2341 <        final Noop r = new Noop();
2341 >        final Noop r = new Noop(m);
2342          final CFException ex = new CFException();
2343  
2344          g.completeExceptionally(ex);
# Line 2599 | Line 2348 | public class CompletableFutureTest exten
2348          // unspecified behavior
2349          Integer v;
2350          try {
2351 <            assertEquals(h.join(), null);
2352 <            assertEquals(r.invocationCount, 1);
2351 >            assertNull(h.join());
2352 >            assertEquals(1, r.invocationCount);
2353          } catch (CompletionException ok) {
2354              checkCompletedWithWrappedCFException(h, ex);
2355 <            assertEquals(r.invocationCount, 0);
2355 >            assertEquals(0, r.invocationCount);
2356          }
2357  
2358          checkCompletedWithWrappedCFException(g, ex);
2359          checkCompletedNormally(f, v1);
2360 <        }
2612 <    }
2360 >    }}
2361  
2362      public void testRunAfterEither_exceptionalCompletion4() {
2363          for (ExecutionMode m : ExecutionMode.values())
2364 <        for (Integer v1 : new Integer[] { 1, null }) {
2365 <
2364 >        for (Integer v1 : new Integer[] { 1, null })
2365 >    {
2366          final CompletableFuture<Integer> f = new CompletableFuture<>();
2367          final CompletableFuture<Integer> g = new CompletableFuture<>();
2368 <        final Noop r = new Noop();
2368 >        final Noop r = new Noop(m);
2369          final CFException ex = new CFException();
2370  
2371          f.completeExceptionally(ex);
# Line 2627 | Line 2375 | public class CompletableFutureTest exten
2375          // unspecified behavior
2376          Integer v;
2377          try {
2378 <            assertEquals(h.join(), null);
2379 <            assertEquals(r.invocationCount, 1);
2378 >            assertNull(h.join());
2379 >            assertEquals(1, r.invocationCount);
2380          } catch (CompletionException ok) {
2381              checkCompletedWithWrappedCFException(h, ex);
2382 <            assertEquals(r.invocationCount, 0);
2382 >            assertEquals(0, r.invocationCount);
2383          }
2384  
2385          checkCompletedWithWrappedCFException(f, ex);
2386          checkCompletedNormally(g, v1);
2387 <        }
2640 <    }
2387 >    }}
2388  
2389      /**
2390       * runAfterEither result completes exceptionally if action does
# Line 2645 | Line 2392 | public class CompletableFutureTest exten
2392      public void testRunAfterEither_actionFailed1() {
2393          for (ExecutionMode m : ExecutionMode.values())
2394          for (Integer v1 : new Integer[] { 1, null })
2395 <        for (Integer v2 : new Integer[] { 2, null }) {
2396 <
2395 >        for (Integer v2 : new Integer[] { 2, null })
2396 >    {
2397          final CompletableFuture<Integer> f = new CompletableFuture<>();
2398          final CompletableFuture<Integer> g = new CompletableFuture<>();
2399 <        final FailingNoop r = new FailingNoop();
2399 >        final FailingRunnable r = new FailingRunnable(m);
2400          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2401  
2402          f.complete(v1);
# Line 2657 | Line 2404 | public class CompletableFutureTest exten
2404          g.complete(v2);
2405          checkCompletedNormally(f, v1);
2406          checkCompletedNormally(g, v2);
2407 <        }
2661 <    }
2407 >    }}
2408  
2409      public void testRunAfterEither_actionFailed2() {
2410          for (ExecutionMode m : ExecutionMode.values())
2411          for (Integer v1 : new Integer[] { 1, null })
2412 <        for (Integer v2 : new Integer[] { 2, null }) {
2413 <
2412 >        for (Integer v2 : new Integer[] { 2, null })
2413 >    {
2414          final CompletableFuture<Integer> f = new CompletableFuture<>();
2415          final CompletableFuture<Integer> g = new CompletableFuture<>();
2416 <        final FailingNoop r = new FailingNoop();
2416 >        final FailingRunnable r = new FailingRunnable(m);
2417          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2418  
2419          g.complete(v2);
# Line 2675 | Line 2421 | public class CompletableFutureTest exten
2421          f.complete(v1);
2422          checkCompletedNormally(f, v1);
2423          checkCompletedNormally(g, v2);
2424 <        }
2679 <    }
2424 >    }}
2425  
2426      /**
2427       * runAfterEither result completes exceptionally if either source cancelled
# Line 2684 | Line 2429 | public class CompletableFutureTest exten
2429      public void testRunAfterEither_sourceCancelled1() {
2430          for (ExecutionMode m : ExecutionMode.values())
2431          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2432 <        for (Integer v1 : new Integer[] { 1, null }) {
2433 <
2432 >        for (Integer v1 : new Integer[] { 1, null })
2433 >    {
2434          final CompletableFuture<Integer> f = new CompletableFuture<>();
2435          final CompletableFuture<Integer> g = new CompletableFuture<>();
2436 <        final Noop r = new Noop();
2436 >        final Noop r = new Noop(m);
2437          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2438  
2439          assertTrue(f.cancel(mayInterruptIfRunning));
# Line 2696 | Line 2441 | public class CompletableFutureTest exten
2441          g.complete(v1);
2442  
2443          checkCancelled(f);
2444 <        assertEquals(r.invocationCount, 0);
2444 >        assertEquals(0, r.invocationCount);
2445          checkCompletedNormally(g, v1);
2446          checkCompletedWithWrappedCancellationException(h);
2447 <        }
2703 <    }
2447 >    }}
2448  
2449      public void testRunAfterEither_sourceCancelled2() {
2450          for (ExecutionMode m : ExecutionMode.values())
2451          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2452 <        for (Integer v1 : new Integer[] { 1, null }) {
2453 <
2452 >        for (Integer v1 : new Integer[] { 1, null })
2453 >    {
2454          final CompletableFuture<Integer> f = new CompletableFuture<>();
2455          final CompletableFuture<Integer> g = new CompletableFuture<>();
2456 <        final Noop r = new Noop();
2456 >        final Noop r = new Noop(m);
2457          final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2458  
2459          assertTrue(g.cancel(mayInterruptIfRunning));
# Line 2717 | Line 2461 | public class CompletableFutureTest exten
2461          f.complete(v1);
2462  
2463          checkCancelled(g);
2464 <        assertEquals(r.invocationCount, 0);
2464 >        assertEquals(0, r.invocationCount);
2465          checkCompletedNormally(f, v1);
2466          checkCompletedWithWrappedCancellationException(h);
2467 <        }
2724 <    }
2467 >    }}
2468  
2469      public void testRunAfterEither_sourceCancelled3() {
2470          for (ExecutionMode m : ExecutionMode.values())
2471          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2472 <        for (Integer v1 : new Integer[] { 1, null }) {
2473 <
2472 >        for (Integer v1 : new Integer[] { 1, null })
2473 >    {
2474          final CompletableFuture<Integer> f = new CompletableFuture<>();
2475          final CompletableFuture<Integer> g = new CompletableFuture<>();
2476 <        final Noop r = new Noop();
2476 >        final Noop r = new Noop(m);
2477  
2478          assertTrue(g.cancel(mayInterruptIfRunning));
2479          f.complete(v1);
# Line 2739 | Line 2482 | public class CompletableFutureTest exten
2482          // unspecified behavior
2483          Integer v;
2484          try {
2485 <            assertEquals(h.join(), null);
2486 <            assertEquals(r.invocationCount, 1);
2485 >            assertNull(h.join());
2486 >            assertEquals(1, r.invocationCount);
2487          } catch (CompletionException ok) {
2488              checkCompletedWithWrappedCancellationException(h);
2489 <            assertEquals(r.invocationCount, 0);
2489 >            assertEquals(0, r.invocationCount);
2490          }
2491  
2492          checkCancelled(g);
2493          checkCompletedNormally(f, v1);
2494 <        }
2752 <    }
2494 >    }}
2495  
2496      public void testRunAfterEither_sourceCancelled4() {
2497          for (ExecutionMode m : ExecutionMode.values())
2498          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2499 <        for (Integer v1 : new Integer[] { 1, null }) {
2500 <
2499 >        for (Integer v1 : new Integer[] { 1, null })
2500 >    {
2501          final CompletableFuture<Integer> f = new CompletableFuture<>();
2502          final CompletableFuture<Integer> g = new CompletableFuture<>();
2503 <        final Noop r = new Noop();
2503 >        final Noop r = new Noop(m);
2504  
2505          assertTrue(f.cancel(mayInterruptIfRunning));
2506          g.complete(v1);
# Line 2767 | Line 2509 | public class CompletableFutureTest exten
2509          // unspecified behavior
2510          Integer v;
2511          try {
2512 <            assertEquals(h.join(), null);
2513 <            assertEquals(r.invocationCount, 1);
2512 >            assertNull(h.join());
2513 >            assertEquals(1, r.invocationCount);
2514          } catch (CompletionException ok) {
2515              checkCompletedWithWrappedCancellationException(h);
2516 <            assertEquals(r.invocationCount, 0);
2516 >            assertEquals(0, r.invocationCount);
2517          }
2518  
2519          checkCancelled(f);
2520          checkCompletedNormally(g, v1);
2521 <        }
2780 <    }
2521 >    }}
2522  
2523      /**
2524       * thenCompose result completes normally after normal completion of source
2525       */
2526 <    public void testThenCompose() {
2527 <        CompletableFuture<Integer> f, g;
2528 <        CompletableFutureInc r;
2529 <
2530 <        f = new CompletableFuture<>();
2531 <        g = f.thenCompose(r = new CompletableFutureInc());
2532 <        f.complete(one);
2533 <        checkCompletedNormally(g, two);
2534 <        assertTrue(r.ran);
2526 >    public void testThenCompose_normalCompletion() {
2527 >        for (ExecutionMode m : ExecutionMode.values())
2528 >        for (boolean createIncomplete : new boolean[] { true, false })
2529 >        for (Integer v1 : new Integer[] { 1, null })
2530 >    {
2531 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2532 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2533 >        if (!createIncomplete) f.complete(v1);
2534 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2535 >        if (createIncomplete) f.complete(v1);
2536  
2537 <        f = new CompletableFuture<>();
2538 <        f.complete(one);
2539 <        g = f.thenCompose(r = new CompletableFutureInc());
2540 <        checkCompletedNormally(g, two);
2799 <        assertTrue(r.ran);
2800 <    }
2537 >        checkCompletedNormally(g, inc(v1));
2538 >        checkCompletedNormally(f, v1);
2539 >        assertEquals(1, r.invocationCount);
2540 >    }}
2541  
2542      /**
2543       * thenCompose result completes exceptionally after exceptional
2544       * completion of source
2545       */
2546 <    public void testThenCompose2() {
2547 <        CompletableFuture<Integer> f, g;
2548 <        CompletableFutureInc r;
2549 <
2550 <        f = new CompletableFuture<>();
2551 <        g = f.thenCompose(r = new CompletableFutureInc());
2552 <        f.completeExceptionally(new CFException());
2553 <        checkCompletedWithWrappedCFException(g);
2546 >    public void testThenCompose_exceptionalCompletion() {
2547 >        for (ExecutionMode m : ExecutionMode.values())
2548 >        for (boolean createIncomplete : new boolean[] { true, false })
2549 >    {
2550 >        final CFException ex = new CFException();
2551 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2552 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2553 >        if (!createIncomplete) f.completeExceptionally(ex);
2554 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2555 >        if (createIncomplete) f.completeExceptionally(ex);
2556  
2557 <        f = new CompletableFuture<>();
2558 <        f.completeExceptionally(new CFException());
2559 <        g = f.thenCompose(r = new CompletableFutureInc());
2560 <        checkCompletedWithWrappedCFException(g);
2819 <    }
2557 >        checkCompletedWithWrappedCFException(g, ex);
2558 >        checkCompletedWithWrappedCFException(f, ex);
2559 >        assertEquals(0, r.invocationCount);
2560 >    }}
2561  
2562      /**
2563       * thenCompose result completes exceptionally if action does
2564       */
2565 <    public void testThenCompose3() {
2566 <        CompletableFuture<Integer> f, g;
2567 <        FailingCompletableFutureFunction r;
2568 <
2569 <        f = new CompletableFuture<>();
2570 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2571 <        f.complete(one);
2572 <        checkCompletedWithWrappedCFException(g);
2565 >    public void testThenCompose_actionFailed() {
2566 >        for (ExecutionMode m : ExecutionMode.values())
2567 >        for (boolean createIncomplete : new boolean[] { true, false })
2568 >        for (Integer v1 : new Integer[] { 1, null })
2569 >    {
2570 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2571 >        final FailingCompletableFutureFunction r
2572 >            = new FailingCompletableFutureFunction(m);
2573 >        if (!createIncomplete) f.complete(v1);
2574 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2575 >        if (createIncomplete) f.complete(v1);
2576  
2833        f = new CompletableFuture<>();
2834        f.complete(one);
2835        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2577          checkCompletedWithWrappedCFException(g);
2578 <    }
2578 >        checkCompletedNormally(f, v1);
2579 >    }}
2580  
2581      /**
2582       * thenCompose result completes exceptionally if source cancelled
2583       */
2584 <    public void testThenCompose4() {
2585 <        CompletableFuture<Integer> f, g;
2586 <        CompletableFutureInc r;
2587 <
2588 <        f = new CompletableFuture<>();
2589 <        g = f.thenCompose(r = new CompletableFutureInc());
2590 <        assertTrue(f.cancel(true));
2591 <        checkCompletedWithWrappedCancellationException(g);
2592 <
2593 <        f = new CompletableFuture<>();
2594 <        assertTrue(f.cancel(true));
2595 <        g = f.thenCompose(r = new CompletableFutureInc());
2596 <        checkCompletedWithWrappedCancellationException(g);
2855 <    }
2856 <
2857 <    // asyncs
2858 <
2859 <    /**
2860 <     * thenRunAsync result completes normally after normal completion of source
2861 <     */
2862 <    public void testThenRunAsync() {
2863 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2864 <        Noop r = new Noop();
2865 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2866 <        f.complete(null);
2867 <        checkCompletedNormally(g, null);
2868 <
2869 <        // reordered version
2870 <        f = new CompletableFuture<>();
2871 <        f.complete(null);
2872 <        r = new Noop();
2873 <        g = f.thenRunAsync(r);
2874 <        checkCompletedNormally(g, null);
2875 <    }
2876 <
2877 <    /**
2878 <     * thenRunAsync result completes exceptionally after exceptional
2879 <     * completion of source
2880 <     */
2881 <    public void testThenRunAsync2() {
2882 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2883 <        Noop r = new Noop();
2884 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2885 <        f.completeExceptionally(new CFException());
2886 <        try {
2887 <            g.join();
2888 <            shouldThrow();
2889 <        } catch (CompletionException success) {}
2890 <        checkCompletedWithWrappedCFException(g);
2891 <    }
2892 <
2893 <    /**
2894 <     * thenRunAsync result completes exceptionally if action does
2895 <     */
2896 <    public void testThenRunAsync3() {
2897 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2898 <        FailingNoop r = new FailingNoop();
2899 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2900 <        f.complete(null);
2901 <        checkCompletedWithWrappedCFException(g);
2902 <    }
2903 <
2904 <    /**
2905 <     * thenRunAsync result completes exceptionally if source cancelled
2906 <     */
2907 <    public void testThenRunAsync4() {
2908 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2909 <        Noop r = new Noop();
2910 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2911 <        assertTrue(f.cancel(true));
2912 <        checkCompletedWithWrappedCancellationException(g);
2913 <    }
2914 <
2915 <    /**
2916 <     * thenApplyAsync result completes normally after normal completion of source
2917 <     */
2918 <    public void testThenApplyAsync() {
2919 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2920 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2921 <        f.complete(one);
2922 <        checkCompletedNormally(g, two);
2923 <    }
2924 <
2925 <    /**
2926 <     * thenApplyAsync result completes exceptionally after exceptional
2927 <     * completion of source
2928 <     */
2929 <    public void testThenApplyAsync2() {
2930 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2931 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2932 <        f.completeExceptionally(new CFException());
2933 <        checkCompletedWithWrappedCFException(g);
2934 <    }
2935 <
2936 <    /**
2937 <     * thenApplyAsync result completes exceptionally if action does
2938 <     */
2939 <    public void testThenApplyAsync3() {
2940 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2941 <        FailingFunction r = new FailingFunction();
2942 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
2943 <        f.complete(null);
2944 <        checkCompletedWithWrappedCFException(g);
2945 <    }
2946 <
2947 <    /**
2948 <     * thenApplyAsync result completes exceptionally if source cancelled
2949 <     */
2950 <    public void testThenApplyAsync4() {
2951 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2952 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2953 <        assertTrue(f.cancel(true));
2954 <        checkCompletedWithWrappedCancellationException(g);
2955 <    }
2956 <
2957 <    /**
2958 <     * thenAcceptAsync result completes normally after normal
2959 <     * completion of source
2960 <     */
2961 <    public void testThenAcceptAsync() {
2962 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2963 <        IncAction r = new IncAction();
2964 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2965 <        f.complete(one);
2966 <        checkCompletedNormally(g, null);
2967 <        assertEquals(r.value, (Integer) 2);
2968 <    }
2969 <
2970 <    /**
2971 <     * thenAcceptAsync result completes exceptionally after exceptional
2972 <     * completion of source
2973 <     */
2974 <    public void testThenAcceptAsync2() {
2975 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2976 <        IncAction r = new IncAction();
2977 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2978 <        f.completeExceptionally(new CFException());
2979 <        checkCompletedWithWrappedCFException(g);
2980 <    }
2981 <
2982 <    /**
2983 <     * thenAcceptAsync result completes exceptionally if action does
2984 <     */
2985 <    public void testThenAcceptAsync3() {
2986 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2987 <        FailingConsumer r = new FailingConsumer();
2988 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2989 <        f.complete(null);
2990 <        checkCompletedWithWrappedCFException(g);
2991 <    }
2992 <
2993 <    /**
2994 <     * thenAcceptAsync result completes exceptionally if source cancelled
2995 <     */
2996 <    public void testThenAcceptAsync4() {
2997 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2998 <        IncAction r = new IncAction();
2999 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3000 <        assertTrue(f.cancel(true));
3001 <        checkCompletedWithWrappedCancellationException(g);
3002 <    }
3003 <
3004 <    /**
3005 <     * thenComposeAsync result completes normally after normal
3006 <     * completion of source
3007 <     */
3008 <    public void testThenComposeAsync() {
3009 <        CompletableFuture<Integer> f, g;
3010 <        CompletableFutureInc r;
3011 <
3012 <        f = new CompletableFuture<>();
3013 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3014 <        f.complete(one);
3015 <        checkCompletedNormally(g, two);
3016 <
3017 <        f = new CompletableFuture<>();
3018 <        f.complete(one);
3019 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3020 <        checkCompletedNormally(g, two);
3021 <    }
3022 <
3023 <    /**
3024 <     * thenComposeAsync result completes exceptionally after
3025 <     * exceptional completion of source
3026 <     */
3027 <    public void testThenComposeAsync2() {
3028 <        CompletableFuture<Integer> f, g;
3029 <        CompletableFutureInc r;
3030 <
3031 <        f = new CompletableFuture<>();
3032 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3033 <        f.completeExceptionally(new CFException());
3034 <        checkCompletedWithWrappedCFException(g);
3035 <        assertFalse(r.ran);
3036 <
3037 <        f = new CompletableFuture<>();
3038 <        f.completeExceptionally(new CFException());
3039 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3040 <        checkCompletedWithWrappedCFException(g);
3041 <        assertFalse(r.ran);
3042 <    }
3043 <
3044 <    /**
3045 <     * thenComposeAsync result completes exceptionally if action does
3046 <     */
3047 <    public void testThenComposeAsync3() {
3048 <        CompletableFuture<Integer> f, g;
3049 <        FailingCompletableFutureFunction r;
3050 <
3051 <        f = new CompletableFuture<>();
3052 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3053 <        f.complete(one);
3054 <        checkCompletedWithWrappedCFException(g);
3055 <
3056 <        f = new CompletableFuture<>();
3057 <        f.complete(one);
3058 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3059 <        checkCompletedWithWrappedCFException(g);
3060 <    }
3061 <
3062 <    /**
3063 <     * thenComposeAsync result completes exceptionally if source cancelled
3064 <     */
3065 <    public void testThenComposeAsync4() {
3066 <        CompletableFuture<Integer> f, g;
3067 <        CompletableFutureInc r;
3068 <
3069 <        f = new CompletableFuture<>();
3070 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3071 <        assertTrue(f.cancel(true));
3072 <        checkCompletedWithWrappedCancellationException(g);
3073 <
3074 <        f = new CompletableFuture<>();
3075 <        assertTrue(f.cancel(true));
3076 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3077 <        checkCompletedWithWrappedCancellationException(g);
3078 <    }
3079 <
3080 <    // async with explicit executors
3081 <
3082 <    /**
3083 <     * thenRunAsync result completes normally after normal completion of source
3084 <     */
3085 <    public void testThenRunAsyncE() {
3086 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3087 <        Noop r = new Noop();
3088 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3089 <        f.complete(null);
3090 <        checkCompletedNormally(g, null);
3091 <
3092 <        // reordered version
3093 <        f = new CompletableFuture<>();
3094 <        f.complete(null);
3095 <        r = new Noop();
3096 <        g = f.thenRunAsync(r, new ThreadExecutor());
3097 <        checkCompletedNormally(g, null);
3098 <    }
3099 <
3100 <    /**
3101 <     * thenRunAsync result completes exceptionally after exceptional
3102 <     * completion of source
3103 <     */
3104 <    public void testThenRunAsync2E() {
3105 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3106 <        Noop r = new Noop();
3107 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3108 <        f.completeExceptionally(new CFException());
3109 <        try {
3110 <            g.join();
3111 <            shouldThrow();
3112 <        } catch (CompletionException success) {}
3113 <        checkCompletedWithWrappedCFException(g);
3114 <    }
3115 <
3116 <    /**
3117 <     * thenRunAsync result completes exceptionally if action does
3118 <     */
3119 <    public void testThenRunAsync3E() {
3120 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3121 <        FailingNoop r = new FailingNoop();
3122 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3123 <        f.complete(null);
3124 <        checkCompletedWithWrappedCFException(g);
3125 <    }
3126 <
3127 <    /**
3128 <     * thenRunAsync result completes exceptionally if source cancelled
3129 <     */
3130 <    public void testThenRunAsync4E() {
3131 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3132 <        Noop r = new Noop();
3133 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3134 <        assertTrue(f.cancel(true));
3135 <        checkCompletedWithWrappedCancellationException(g);
3136 <    }
3137 <
3138 <    /**
3139 <     * thenApplyAsync result completes normally after normal completion of source
3140 <     */
3141 <    public void testThenApplyAsyncE() {
3142 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3143 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3144 <        f.complete(one);
3145 <        checkCompletedNormally(g, two);
3146 <    }
3147 <
3148 <    /**
3149 <     * thenApplyAsync result completes exceptionally after exceptional
3150 <     * completion of source
3151 <     */
3152 <    public void testThenApplyAsync2E() {
3153 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3154 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3155 <        f.completeExceptionally(new CFException());
3156 <        checkCompletedWithWrappedCFException(g);
3157 <    }
3158 <
3159 <    /**
3160 <     * thenApplyAsync result completes exceptionally if action does
3161 <     */
3162 <    public void testThenApplyAsync3E() {
3163 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3164 <        FailingFunction r = new FailingFunction();
3165 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3166 <        f.complete(null);
3167 <        checkCompletedWithWrappedCFException(g);
3168 <    }
3169 <
3170 <    /**
3171 <     * thenApplyAsync result completes exceptionally if source cancelled
3172 <     */
3173 <    public void testThenApplyAsync4E() {
3174 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3175 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3176 <        assertTrue(f.cancel(true));
3177 <        checkCompletedWithWrappedCancellationException(g);
3178 <    }
3179 <
3180 <    /**
3181 <     * thenAcceptAsync result completes normally after normal
3182 <     * completion of source
3183 <     */
3184 <    public void testThenAcceptAsyncE() {
3185 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3186 <        IncAction r = new IncAction();
3187 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3188 <        f.complete(one);
3189 <        checkCompletedNormally(g, null);
3190 <        assertEquals(r.value, (Integer) 2);
3191 <    }
3192 <
3193 <    /**
3194 <     * thenAcceptAsync result completes exceptionally after exceptional
3195 <     * completion of source
3196 <     */
3197 <    public void testThenAcceptAsync2E() {
3198 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3199 <        IncAction r = new IncAction();
3200 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3201 <        f.completeExceptionally(new CFException());
3202 <        checkCompletedWithWrappedCFException(g);
3203 <    }
3204 <
3205 <    /**
3206 <     * thenAcceptAsync result completes exceptionally if action does
3207 <     */
3208 <    public void testThenAcceptAsync3E() {
3209 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3210 <        FailingConsumer r = new FailingConsumer();
3211 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3212 <        f.complete(null);
3213 <        checkCompletedWithWrappedCFException(g);
3214 <    }
3215 <
3216 <    /**
3217 <     * thenAcceptAsync result completes exceptionally if source cancelled
3218 <     */
3219 <    public void testThenAcceptAsync4E() {
3220 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3221 <        IncAction r = new IncAction();
3222 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3223 <        assertTrue(f.cancel(true));
3224 <        checkCompletedWithWrappedCancellationException(g);
3225 <    }
3226 <
3227 <    /**
3228 <     * thenComposeAsync result completes normally after normal
3229 <     * completion of source
3230 <     */
3231 <    public void testThenComposeAsyncE() {
3232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3233 <        CompletableFutureInc r = new CompletableFutureInc();
3234 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3235 <        f.complete(one);
3236 <        checkCompletedNormally(g, two);
3237 <    }
3238 <
3239 <    /**
3240 <     * thenComposeAsync result completes exceptionally after
3241 <     * exceptional completion of source
3242 <     */
3243 <    public void testThenComposeAsync2E() {
3244 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3245 <        CompletableFutureInc r = new CompletableFutureInc();
3246 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3247 <        f.completeExceptionally(new CFException());
3248 <        checkCompletedWithWrappedCFException(g);
3249 <    }
3250 <
3251 <    /**
3252 <     * thenComposeAsync result completes exceptionally if action does
3253 <     */
3254 <    public void testThenComposeAsync3E() {
3255 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3256 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3257 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3258 <        f.complete(one);
3259 <        checkCompletedWithWrappedCFException(g);
3260 <    }
2584 >    public void testThenCompose_sourceCancelled() {
2585 >        for (ExecutionMode m : ExecutionMode.values())
2586 >        for (boolean createIncomplete : new boolean[] { true, false })
2587 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2588 >    {
2589 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2590 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2591 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2592 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2593 >        if (createIncomplete) {
2594 >            checkIncomplete(g);
2595 >            assertTrue(f.cancel(mayInterruptIfRunning));
2596 >        }
2597  
3262    /**
3263     * thenComposeAsync result completes exceptionally if source cancelled
3264     */
3265    public void testThenComposeAsync4E() {
3266        CompletableFuture<Integer> f = new CompletableFuture<>();
3267        CompletableFutureInc r = new CompletableFutureInc();
3268        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3269        assertTrue(f.cancel(true));
2598          checkCompletedWithWrappedCancellationException(g);
2599 <    }
2599 >        checkCancelled(f);
2600 >    }}
2601  
2602      // other static methods
2603  
# Line 3287 | Line 2616 | public class CompletableFutureTest exten
2616       */
2617      public void testAllOf_normal() throws Exception {
2618          for (int k = 1; k < 20; ++k) {
2619 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2619 >            CompletableFuture<Integer>[] fs
2620 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2621              for (int i = 0; i < k; ++i)
2622                  fs[i] = new CompletableFuture<>();
2623              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3301 | Line 2631 | public class CompletableFutureTest exten
2631          }
2632      }
2633  
2634 +    public void testAllOf_backwards() throws Exception {
2635 +        for (int k = 1; k < 20; ++k) {
2636 +            CompletableFuture<Integer>[] fs
2637 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2638 +            for (int i = 0; i < k; ++i)
2639 +                fs[i] = new CompletableFuture<>();
2640 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2641 +            for (int i = k - 1; i >= 0; i--) {
2642 +                checkIncomplete(f);
2643 +                checkIncomplete(CompletableFuture.allOf(fs));
2644 +                fs[i].complete(one);
2645 +            }
2646 +            checkCompletedNormally(f, null);
2647 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2648 +        }
2649 +    }
2650 +
2651      /**
2652       * anyOf(no component futures) returns an incomplete future
2653       */
# Line 3359 | Line 2706 | public class CompletableFutureTest exten
2706          Runnable[] throwingActions = {
2707              () -> CompletableFuture.supplyAsync(null),
2708              () -> CompletableFuture.supplyAsync(null, exec),
2709 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2709 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2710  
2711              () -> CompletableFuture.runAsync(null),
2712              () -> CompletableFuture.runAsync(null, exec),
# Line 3432 | Line 2779 | public class CompletableFutureTest exten
2779  
2780              () -> f.thenCompose(null),
2781              () -> f.thenComposeAsync(null),
2782 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2782 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2783              () -> f.thenComposeAsync(null, exec),
2784  
2785              () -> f.exceptionally(null),
# Line 3470 | Line 2817 | public class CompletableFutureTest exten
2817       */
2818      public void testWhenComplete_normalCompletion1() {
2819          for (ExecutionMode m : ExecutionMode.values())
2820 <        for (Integer v1 : new Integer[] { 1, null }) {
2821 <
2822 <        final AtomicInteger a = new AtomicInteger();
2820 >        for (boolean createIncomplete : new boolean[] { true, false })
2821 >        for (Integer v1 : new Integer[] { 1, null })
2822 >    {
2823 >        final AtomicInteger a = new AtomicInteger(0);
2824          final CompletableFuture<Integer> f = new CompletableFuture<>();
2825 <        final CompletableFuture<Integer> g =
2826 <            m.whenComplete(f,
2827 <                           (Integer x, Throwable t) -> {
2828 <                               threadAssertSame(x, v1);
2829 <                               threadAssertNull(t);
2830 <                               a.getAndIncrement();
2831 <                           });
2832 <        f.complete(v1);
2833 <        checkCompletedNormally(f, v1);
2825 >        if (!createIncomplete) f.complete(v1);
2826 >        final CompletableFuture<Integer> g = m.whenComplete
2827 >            (f,
2828 >             (Integer x, Throwable t) -> {
2829 >                threadAssertSame(x, v1);
2830 >                threadAssertNull(t);
2831 >                a.getAndIncrement();
2832 >            });
2833 >        if (createIncomplete) f.complete(v1);
2834 >
2835          checkCompletedNormally(g, v1);
2836 <        assertEquals(a.get(), 1);
2837 <        }
2838 <    }
2836 >        checkCompletedNormally(f, v1);
2837 >        assertEquals(1, a.get());
2838 >    }}
2839  
2840      /**
2841       * whenComplete action executes on exceptional completion, propagating
# Line 3494 | Line 2843 | public class CompletableFutureTest exten
2843       */
2844      public void testWhenComplete_exceptionalCompletion() {
2845          for (ExecutionMode m : ExecutionMode.values())
2846 <        for (Integer v1 : new Integer[] { 1, null }) {
2847 <
2848 <        final AtomicInteger a = new AtomicInteger();
2846 >        for (boolean createIncomplete : new boolean[] { true, false })
2847 >        for (Integer v1 : new Integer[] { 1, null })
2848 >    {
2849 >        final AtomicInteger a = new AtomicInteger(0);
2850          final CFException ex = new CFException();
2851          final CompletableFuture<Integer> f = new CompletableFuture<>();
2852 +        if (!createIncomplete) f.completeExceptionally(ex);
2853          final CompletableFuture<Integer> g = m.whenComplete
2854              (f,
2855               (Integer x, Throwable t) -> {
# Line 3506 | Line 2857 | public class CompletableFutureTest exten
2857                  threadAssertSame(t, ex);
2858                  a.getAndIncrement();
2859              });
2860 <        f.completeExceptionally(ex);
2860 >        if (createIncomplete) f.completeExceptionally(ex);
2861          checkCompletedWithWrappedCFException(f, ex);
2862          checkCompletedWithWrappedCFException(g, ex);
2863 <        assertEquals(a.get(), 1);
2864 <        }
2865 <    }
2863 >        assertEquals(1, a.get());
2864 >    }}
2865 >
2866 >    /**
2867 >     * whenComplete action executes on cancelled source, propagating
2868 >     * CancellationException.
2869 >     */
2870 >    public void testWhenComplete_sourceCancelled() {
2871 >        for (ExecutionMode m : ExecutionMode.values())
2872 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2873 >        for (boolean createIncomplete : new boolean[] { true, false })
2874 >    {
2875 >        final AtomicInteger a = new AtomicInteger(0);
2876 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2877 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2878 >        final CompletableFuture<Integer> g = m.whenComplete
2879 >            (f,
2880 >             (Integer x, Throwable t) -> {
2881 >                threadAssertNull(x);
2882 >                threadAssertTrue(t instanceof CancellationException);
2883 >                a.getAndIncrement();
2884 >            });
2885 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2886 >
2887 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2888 >        checkCompletedWithWrappedCancellationException(g);
2889 >        checkCancelled(f);
2890 >        assertEquals(1, a.get());
2891 >    }}
2892  
2893      /**
2894       * If a whenComplete action throws an exception when triggered by
2895       * a normal completion, it completes exceptionally
2896       */
2897      public void testWhenComplete_actionFailed() {
2898 +        for (boolean createIncomplete : new boolean[] { true, false })
2899          for (ExecutionMode m : ExecutionMode.values())
2900 <        for (Integer v1 : new Integer[] { 1, null }) {
2901 <
2900 >        for (Integer v1 : new Integer[] { 1, null })
2901 >    {
2902 >        final AtomicInteger a = new AtomicInteger(0);
2903          final CFException ex = new CFException();
2904          final CompletableFuture<Integer> f = new CompletableFuture<>();
2905 +        if (!createIncomplete) f.complete(v1);
2906          final CompletableFuture<Integer> g = m.whenComplete
2907              (f,
2908               (Integer x, Throwable t) -> {
2909                  threadAssertSame(x, v1);
2910                  threadAssertNull(t);
2911 +                a.getAndIncrement();
2912                  throw ex;
2913              });
2914 <        f.complete(v1);
2914 >        if (createIncomplete) f.complete(v1);
2915          checkCompletedNormally(f, v1);
2916          checkCompletedWithWrappedCFException(g, ex);
2917 <        }
2918 <    }
2917 >        assertEquals(1, a.get());
2918 >    }}
2919  
2920      /**
2921       * If a whenComplete action throws an exception when triggered by
# Line 3542 | Line 2923 | public class CompletableFutureTest exten
2923       * exception takes precedence.
2924       */
2925      public void testWhenComplete_actionFailedSourceFailed() {
2926 +        for (boolean createIncomplete : new boolean[] { true, false })
2927          for (ExecutionMode m : ExecutionMode.values())
2928 <        for (Integer v1 : new Integer[] { 1, null }) {
2929 <
2928 >        for (Integer v1 : new Integer[] { 1, null })
2929 >    {
2930 >        final AtomicInteger a = new AtomicInteger(0);
2931          final CFException ex1 = new CFException();
2932          final CFException ex2 = new CFException();
2933          final CompletableFuture<Integer> f = new CompletableFuture<>();
2934 +
2935 +        if (!createIncomplete) f.completeExceptionally(ex1);
2936          final CompletableFuture<Integer> g = m.whenComplete
2937              (f,
2938               (Integer x, Throwable t) -> {
2939                  threadAssertSame(t, ex1);
2940                  threadAssertNull(x);
2941 +                a.getAndIncrement();
2942                  throw ex2;
2943              });
2944 <        f.completeExceptionally(ex1);
2944 >        if (createIncomplete) f.completeExceptionally(ex1);
2945 >
2946          checkCompletedWithWrappedCFException(f, ex1);
2947          checkCompletedWithWrappedCFException(g, ex1);
2948 <        }
2949 <    }
3563 <
3564 <    /**
3565 <     * handleAsync action completes normally with function value on
3566 <     * either normal or exceptional completion of source
3567 <     */
3568 <    public void testHandleAsync() {
3569 <        CompletableFuture<Integer> f, g;
3570 <        IntegerHandler r;
3571 <
3572 <        f = new CompletableFuture<>();
3573 <        g = f.handleAsync(r = new IntegerHandler());
3574 <        assertFalse(r.ran);
3575 <        f.completeExceptionally(new CFException());
3576 <        checkCompletedWithWrappedCFException(f);
3577 <        checkCompletedNormally(g, three);
3578 <        assertTrue(r.ran);
3579 <
3580 <        f = new CompletableFuture<>();
3581 <        g = f.handleAsync(r = new IntegerHandler());
3582 <        assertFalse(r.ran);
3583 <        f.completeExceptionally(new CFException());
3584 <        checkCompletedWithWrappedCFException(f);
3585 <        checkCompletedNormally(g, three);
3586 <        assertTrue(r.ran);
3587 <
3588 <        f = new CompletableFuture<>();
3589 <        g = f.handleAsync(r = new IntegerHandler());
3590 <        assertFalse(r.ran);
3591 <        f.complete(one);
3592 <        checkCompletedNormally(f, one);
3593 <        checkCompletedNormally(g, two);
3594 <        assertTrue(r.ran);
3595 <
3596 <        f = new CompletableFuture<>();
3597 <        g = f.handleAsync(r = new IntegerHandler());
3598 <        assertFalse(r.ran);
3599 <        f.complete(one);
3600 <        checkCompletedNormally(f, one);
3601 <        checkCompletedNormally(g, two);
3602 <        assertTrue(r.ran);
3603 <    }
3604 <
3605 <    /**
3606 <     * handleAsync action with Executor completes normally with
3607 <     * function value on either normal or exceptional completion of
3608 <     * source
3609 <     */
3610 <    public void testHandleAsync2() {
3611 <        CompletableFuture<Integer> f, g;
3612 <        ThreadExecutor exec = new ThreadExecutor();
3613 <        IntegerHandler r;
3614 <
3615 <        f = new CompletableFuture<>();
3616 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3617 <        assertFalse(r.ran);
3618 <        f.completeExceptionally(new CFException());
3619 <        checkCompletedWithWrappedCFException(f);
3620 <        checkCompletedNormally(g, three);
3621 <        assertTrue(r.ran);
3622 <
3623 <        f = new CompletableFuture<>();
3624 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3625 <        assertFalse(r.ran);
3626 <        f.completeExceptionally(new CFException());
3627 <        checkCompletedWithWrappedCFException(f);
3628 <        checkCompletedNormally(g, three);
3629 <        assertTrue(r.ran);
3630 <
3631 <        f = new CompletableFuture<>();
3632 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3633 <        assertFalse(r.ran);
3634 <        f.complete(one);
3635 <        checkCompletedNormally(f, one);
3636 <        checkCompletedNormally(g, two);
3637 <        assertTrue(r.ran);
3638 <
3639 <        f = new CompletableFuture<>();
3640 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3641 <        assertFalse(r.ran);
3642 <        f.complete(one);
3643 <        checkCompletedNormally(f, one);
3644 <        checkCompletedNormally(g, two);
3645 <        assertTrue(r.ran);
3646 <    }
2948 >        assertEquals(1, a.get());
2949 >    }}
2950  
2951   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines