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.41 by jsr166, Mon Jun 2 01:46:11 2014 UTC vs.
Revision 1.47 by jsr166, Mon Jun 2 18:21:34 2014 UTC

# Line 247 | Line 247 | public class CompletableFutureTest exten
247          f = new CompletableFuture<>();
248          f.obtrudeValue(three);
249          checkCompletedNormally(f, three);
250 +        f.obtrudeValue(null);
251 +        checkCompletedNormally(f, null);
252          f = new CompletableFuture<>();
253          f.completeExceptionally(new CFException());
254          f.obtrudeValue(four);
# Line 279 | Line 281 | public class CompletableFutureTest exten
281       */
282      public void testGetNumberOfDependents() {
283          CompletableFuture<Integer> f = new CompletableFuture<>();
284 <        assertEquals(f.getNumberOfDependents(), 0);
284 >        assertEquals(0, f.getNumberOfDependents());
285          CompletableFuture g = f.thenRun(new Noop());
286 <        assertEquals(f.getNumberOfDependents(), 1);
287 <        assertEquals(g.getNumberOfDependents(), 0);
286 >        assertEquals(1, f.getNumberOfDependents());
287 >        assertEquals(0, g.getNumberOfDependents());
288          CompletableFuture h = f.thenRun(new Noop());
289 <        assertEquals(f.getNumberOfDependents(), 2);
289 >        assertEquals(2, f.getNumberOfDependents());
290          f.complete(1);
291          checkCompletedNormally(g, null);
292 <        assertEquals(f.getNumberOfDependents(), 0);
293 <        assertEquals(g.getNumberOfDependents(), 0);
292 >        assertEquals(0, f.getNumberOfDependents());
293 >        assertEquals(0, g.getNumberOfDependents());
294      }
295  
296      /**
# Line 339 | Line 341 | public class CompletableFutureTest exten
341      static final class IncAction implements Consumer<Integer> {
342          int invocationCount = 0;
343          Integer value;
342        public boolean ran() { return invocationCount == 1; }
344          public void accept(Integer x) {
345              invocationCount++;
346              value = inc(x);
# Line 348 | Line 349 | public class CompletableFutureTest exten
349      static final class IncFunction implements Function<Integer,Integer> {
350          int invocationCount = 0;
351          Integer value;
351        public boolean ran() { return invocationCount == 1; }
352          public Integer apply(Integer x) {
353              invocationCount++;
354              return value = inc(x);
# Line 358 | Line 358 | public class CompletableFutureTest exten
358          int invocationCount = 0;
359          Integer value;
360          // Check this action was invoked exactly once when result is computed.
361        public boolean ran() { return invocationCount == 1; }
361          public void accept(Integer x, Integer y) {
362              invocationCount++;
363              value = subtract(x, y);
# Line 368 | Line 367 | public class CompletableFutureTest exten
367          int invocationCount = 0;
368          Integer value;
369          // Check this action was invoked exactly once when result is computed.
371        public boolean ran() { return invocationCount == 1; }
370          public Integer apply(Integer x, Integer y) {
371              invocationCount++;
372              return value = subtract(x, y);
# Line 376 | Line 374 | public class CompletableFutureTest exten
374      }
375      static final class Noop implements Runnable {
376          int invocationCount = 0;
379        boolean ran;
377          public void run() {
378              invocationCount++;
382            ran = true;
379          }
380      }
381  
382      static final class FailingSupplier implements Supplier<Integer> {
383 <        boolean ran;
384 <        public Integer get() { ran = true; throw new CFException(); }
383 >        int invocationCount = 0;
384 >        public Integer get() {
385 >            invocationCount++;
386 >            throw new CFException();
387 >        }
388      }
389      static final class FailingConsumer implements Consumer<Integer> {
390 <        boolean ran;
391 <        public void accept(Integer x) { ran = true; throw new CFException(); }
390 >        int invocationCount = 0;
391 >        public void accept(Integer x) {
392 >            invocationCount++;
393 >            throw new CFException();
394 >        }
395      }
396      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
397 <        boolean ran;
398 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
397 >        int invocationCount = 0;
398 >        public void accept(Integer x, Integer y) {
399 >            invocationCount++;
400 >            throw new CFException();
401 >        }
402      }
403      static final class FailingFunction implements Function<Integer, Integer> {
404 <        boolean ran;
405 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
404 >        int invocationCount = 0;
405 >        public Integer apply(Integer x) {
406 >            invocationCount++;
407 >            throw new CFException();
408 >        }
409      }
410      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
411 <        boolean ran;
412 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
411 >        int invocationCount = 0;
412 >        public Integer apply(Integer x, Integer y) {
413 >            invocationCount++;
414 >            throw new CFException();
415 >        }
416      }
417      static final class FailingNoop implements Runnable {
418 <        boolean ran;
419 <        public void run() { ran = true; throw new CFException(); }
418 >        int invocationCount = 0;
419 >        public void run() {
420 >            invocationCount++;
421 >            throw new CFException();
422 >        }
423      }
424  
425      static final class CompletableFutureInc
426          implements Function<Integer, CompletableFuture<Integer>> {
427 <        boolean ran;
427 >        int invocationCount = 0;
428          public CompletableFuture<Integer> apply(Integer x) {
429 <            ran = true;
429 >            invocationCount++;
430              CompletableFuture<Integer> f = new CompletableFuture<>();
431 <            f.complete(Integer.valueOf(x.intValue() + 1));
431 >            f.complete(inc(x));
432              return f;
433          }
434      }
435  
436      static final class FailingCompletableFutureFunction
437          implements Function<Integer, CompletableFuture<Integer>> {
438 <        boolean ran;
438 >        int invocationCount = 0;
439          public CompletableFuture<Integer> apply(Integer x) {
440 <            ran = true; throw new CFException();
440 >            invocationCount++;
441 >            throw new CFException();
442          }
443      }
444  
# Line 437 | Line 452 | public class CompletableFutureTest exten
452          }
453      }
454  
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;
449        }
450    }
451
455      /**
456       * Permits the testing of parallel code for the 3 different
457       * execution modes without repeating all the testing code.
458       */
459      enum ExecutionMode {
460          DEFAULT {
461 +            public <T> CompletableFuture<Void> thenRun
462 +                (CompletableFuture<T> f, Runnable a) {
463 +                return f.thenRun(a);
464 +            }
465 +            public <T> CompletableFuture<Void> thenAccept
466 +                (CompletableFuture<T> f, Consumer<? super T> a) {
467 +                return f.thenAccept(a);
468 +            }
469 +            public <T,U> CompletableFuture<U> thenApply
470 +                (CompletableFuture<T> f, Function<? super T,U> a) {
471 +                return f.thenApply(a);
472 +            }
473 +            public <T,U> CompletableFuture<U> thenCompose
474 +                (CompletableFuture<T> f,
475 +                 Function<? super T,? extends CompletionStage<U>> a) {
476 +                return f.thenCompose(a);
477 +            }
478 +            public <T,U> CompletableFuture<U> handle
479 +                (CompletableFuture<T> f,
480 +                 BiFunction<? super T,Throwable,? extends U> a) {
481 +                return f.handle(a);
482 +            }
483 +            public <T> CompletableFuture<T> whenComplete
484 +                (CompletableFuture<T> f,
485 +                 BiConsumer<? super T,? super Throwable> a) {
486 +                return f.whenComplete(a);
487 +            }
488              public <T,U> CompletableFuture<Void> runAfterBoth
489                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
490                  return f.runAfterBoth(g, a);
# Line 471 | Line 501 | public class CompletableFutureTest exten
501                   BiFunction<? super T,? super U,? extends V> a) {
502                  return f.thenCombine(g, a);
503              }
504 <            public <T,U> CompletableFuture<U> applyToEither
504 >            public <T> CompletableFuture<Void> runAfterEither
505                  (CompletableFuture<T> f,
506 <                 CompletionStage<? extends T> g,
507 <                 Function<? super T,U> a) {
508 <                return f.applyToEither(g, a);
506 >                 CompletionStage<?> g,
507 >                 java.lang.Runnable a) {
508 >                return f.runAfterEither(g, a);
509              }
510              public <T> CompletableFuture<Void> acceptEither
511                  (CompletableFuture<T> f,
# Line 483 | Line 513 | public class CompletableFutureTest exten
513                   Consumer<? super T> a) {
514                  return f.acceptEither(g, a);
515              }
516 <            public <T> CompletableFuture<Void> runAfterEither
516 >            public <T,U> CompletableFuture<U> applyToEither
517                  (CompletableFuture<T> f,
518 <                 CompletionStage<?> g,
519 <                 java.lang.Runnable a) {
520 <                return f.runAfterEither(g, a);
518 >                 CompletionStage<? extends T> g,
519 >                 Function<? super T,U> a) {
520 >                return f.applyToEither(g, a);
521 >            }
522 >        },
523 >
524 >        DEFAULT_ASYNC {
525 >            public <T> CompletableFuture<Void> thenRun
526 >                (CompletableFuture<T> f, Runnable a) {
527 >                return f.thenRunAsync(a);
528 >            }
529 >            public <T> CompletableFuture<Void> thenAccept
530 >                (CompletableFuture<T> f, Consumer<? super T> a) {
531 >                return f.thenAcceptAsync(a);
532 >            }
533 >            public <T,U> CompletableFuture<U> thenApply
534 >                (CompletableFuture<T> f, Function<? super T,U> a) {
535 >                return f.thenApplyAsync(a);
536              }
537              public <T,U> CompletableFuture<U> thenCompose
538                  (CompletableFuture<T> f,
539                   Function<? super T,? extends CompletionStage<U>> a) {
540 <                return f.thenCompose(a);
540 >                return f.thenComposeAsync(a);
541 >            }
542 >            public <T,U> CompletableFuture<U> handle
543 >                (CompletableFuture<T> f,
544 >                 BiFunction<? super T,Throwable,? extends U> a) {
545 >                return f.handleAsync(a);
546              }
547              public <T> CompletableFuture<T> whenComplete
548                  (CompletableFuture<T> f,
549                   BiConsumer<? super T,? super Throwable> a) {
550 <                return f.whenComplete(a);
550 >                return f.whenCompleteAsync(a);
551              }
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 {
552              public <T,U> CompletableFuture<Void> runAfterBoth
553                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
554                  return f.runAfterBothAsync(g, a);
# Line 532 | Line 565 | public class CompletableFutureTest exten
565                   BiFunction<? super T,? super U,? extends V> a) {
566                  return f.thenCombineAsync(g, a);
567              }
568 <            public <T,U> CompletableFuture<U> applyToEither
568 >            public <T> CompletableFuture<Void> runAfterEither
569                  (CompletableFuture<T> f,
570 <                 CompletionStage<? extends T> g,
571 <                 Function<? super T,U> a) {
572 <                return f.applyToEitherAsync(g, a);
570 >                 CompletionStage<?> g,
571 >                 java.lang.Runnable a) {
572 >                return f.runAfterEitherAsync(g, a);
573              }
574              public <T> CompletableFuture<Void> acceptEither
575                  (CompletableFuture<T> f,
# Line 544 | Line 577 | public class CompletableFutureTest exten
577                   Consumer<? super T> a) {
578                  return f.acceptEitherAsync(g, a);
579              }
580 <            public <T> CompletableFuture<Void> runAfterEither
580 >            public <T,U> CompletableFuture<U> applyToEither
581                  (CompletableFuture<T> f,
582 <                 CompletionStage<?> g,
583 <                 java.lang.Runnable a) {
584 <                return f.runAfterEitherAsync(g, a);
582 >                 CompletionStage<? extends T> g,
583 >                 Function<? super T,U> a) {
584 >                return f.applyToEitherAsync(g, a);
585 >            }
586 >        },
587 >
588 >        EXECUTOR {
589 >            public <T> CompletableFuture<Void> thenRun
590 >                (CompletableFuture<T> f, Runnable a) {
591 >                return f.thenRunAsync(a, new ThreadExecutor());
592 >            }
593 >            public <T> CompletableFuture<Void> thenAccept
594 >                (CompletableFuture<T> f, Consumer<? super T> a) {
595 >                return f.thenAcceptAsync(a, new ThreadExecutor());
596 >            }
597 >            public <T,U> CompletableFuture<U> thenApply
598 >                (CompletableFuture<T> f, Function<? super T,U> a) {
599 >                return f.thenApplyAsync(a, new ThreadExecutor());
600              }
601              public <T,U> CompletableFuture<U> thenCompose
602                  (CompletableFuture<T> f,
603                   Function<? super T,? extends CompletionStage<U>> a) {
604 <                return f.thenComposeAsync(a);
604 >                return f.thenComposeAsync(a, new ThreadExecutor());
605 >            }
606 >            public <T,U> CompletableFuture<U> handle
607 >                (CompletableFuture<T> f,
608 >                 BiFunction<? super T,Throwable,? extends U> a) {
609 >                return f.handleAsync(a, new ThreadExecutor());
610              }
611              public <T> CompletableFuture<T> whenComplete
612                  (CompletableFuture<T> f,
613                   BiConsumer<? super T,? super Throwable> a) {
614 <                return f.whenCompleteAsync(a);
614 >                return f.whenCompleteAsync(a, new ThreadExecutor());
615              }
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 {
616              public <T,U> CompletableFuture<Void> runAfterBoth
617                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
618                  return f.runAfterBothAsync(g, a, new ThreadExecutor());
# Line 592 | Line 629 | public class CompletableFutureTest exten
629                   BiFunction<? super T,? super U,? extends V> a) {
630                  return f.thenCombineAsync(g, a, new ThreadExecutor());
631              }
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            }
632              public <T> CompletableFuture<Void> runAfterEither
633                  (CompletableFuture<T> f,
634                   CompletionStage<?> g,
635                   java.lang.Runnable a) {
636                  return f.runAfterEitherAsync(g, a, new ThreadExecutor());
637              }
638 <            public <T,U> CompletableFuture<U> thenCompose
638 >            public <T> CompletableFuture<Void> acceptEither
639                  (CompletableFuture<T> f,
640 <                 Function<? super T,? extends CompletionStage<U>> a) {
641 <                return f.thenComposeAsync(a, new ThreadExecutor());
640 >                 CompletionStage<? extends T> g,
641 >                 Consumer<? super T> a) {
642 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
643              }
644 <            public <T> CompletableFuture<T> whenComplete
644 >            public <T,U> CompletableFuture<U> applyToEither
645                  (CompletableFuture<T> f,
646 <                 BiConsumer<? super T,? super Throwable> a) {
647 <                return f.whenCompleteAsync(a, new ThreadExecutor());
646 >                 CompletionStage<? extends T> g,
647 >                 Function<? super T,U> a) {
648 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
649              }
650          };
651  
652 +        public abstract <T> CompletableFuture<Void> thenRun
653 +            (CompletableFuture<T> f, Runnable a);
654 +        public abstract <T> CompletableFuture<Void> thenAccept
655 +            (CompletableFuture<T> f, Consumer<? super T> a);
656 +        public abstract <T,U> CompletableFuture<U> thenApply
657 +            (CompletableFuture<T> f, Function<? super T,U> a);
658 +        public abstract <T,U> CompletableFuture<U> thenCompose
659 +            (CompletableFuture<T> f,
660 +             Function<? super T,? extends CompletionStage<U>> a);
661 +        public abstract <T,U> CompletableFuture<U> handle
662 +            (CompletableFuture<T> f,
663 +             BiFunction<? super T,Throwable,? extends U> a);
664 +        public abstract <T> CompletableFuture<T> whenComplete
665 +            (CompletableFuture<T> f,
666 +             BiConsumer<? super T,? super Throwable> a);
667          public abstract <T,U> CompletableFuture<Void> runAfterBoth
668              (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
669          public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
# Line 632 | Line 674 | public class CompletableFutureTest exten
674              (CompletableFuture<T> f,
675               CompletionStage<? extends U> g,
676               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);
677          public abstract <T> CompletableFuture<Void> runAfterEither
678              (CompletableFuture<T> f,
679               CompletionStage<?> g,
680               java.lang.Runnable a);
681 <        public abstract <T,U> CompletableFuture<U> thenCompose
681 >        public abstract <T> CompletableFuture<Void> acceptEither
682              (CompletableFuture<T> f,
683 <             Function<? super T,? extends CompletionStage<U>> a);
684 <        public abstract <T> CompletableFuture<T> whenComplete
683 >             CompletionStage<? extends T> g,
684 >             Consumer<? super T> a);
685 >        public abstract <T,U> CompletableFuture<U> applyToEither
686              (CompletableFuture<T> f,
687 <             BiConsumer<? super T,? super Throwable> a);
687 >             CompletionStage<? extends T> g,
688 >             Function<? super T,U> a);
689 >    }
690  
691 +    /**
692 +     * exceptionally action is not invoked when source completes
693 +     * normally, and source result is propagated
694 +     */
695 +    public void testExceptionally_normalCompletion() {
696 +        for (boolean createIncomplete : new boolean[] { true, false })
697 +        for (Integer v1 : new Integer[] { 1, null })
698 +    {
699 +        final AtomicInteger a = new AtomicInteger(0);
700 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
701 +        if (!createIncomplete) f.complete(v1);
702 +        final CompletableFuture<Integer> g = f.exceptionally
703 +            ((Throwable t) -> {
704 +                // Should not be called
705 +                a.getAndIncrement();
706 +                throw new AssertionError();
707 +            });
708 +        if (createIncomplete) f.complete(v1);
709 +
710 +        checkCompletedNormally(g, v1);
711 +        checkCompletedNormally(f, v1);
712 +        assertEquals(0, a.get());
713 +    }}
714  
655    }
715  
716      /**
717       * exceptionally action completes with function value on source
718 <     * exception; otherwise with source value
718 >     * exception
719       */
720 <    public void testExceptionally() {
721 <        CompletableFuture<Integer> f = new CompletableFuture<>();
722 <        ExceptionToInteger r = new ExceptionToInteger();
723 <        CompletableFuture<Integer> g = f.exceptionally(r);
724 <        f.completeExceptionally(new CFException());
725 <        checkCompletedNormally(g, three);
720 >    public void testExceptionally_exceptionalCompletion() {
721 >        for (boolean createIncomplete : new boolean[] { true, false })
722 >        for (Integer v1 : new Integer[] { 1, null })
723 >    {
724 >        final AtomicInteger a = new AtomicInteger(0);
725 >        final CFException ex = new CFException();
726 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
727 >        if (!createIncomplete) f.completeExceptionally(ex);
728 >        final CompletableFuture<Integer> g = f.exceptionally
729 >            ((Throwable t) -> {
730 >                threadAssertSame(t, ex);
731 >                a.getAndIncrement();
732 >                return v1;
733 >            });
734 >        if (createIncomplete) f.completeExceptionally(ex);
735  
736 <        f = new CompletableFuture<>();
737 <        r = new ExceptionToInteger();
738 <        g = f.exceptionally(r);
739 <        f.complete(one);
740 <        checkCompletedNormally(g, one);
741 <    }
736 >        checkCompletedNormally(g, v1);
737 >        assertEquals(1, a.get());
738 >    }}
739 >
740 >    public void testExceptionally_exceptionalCompletionActionFailed() {
741 >        for (boolean createIncomplete : new boolean[] { true, false })
742 >        for (Integer v1 : new Integer[] { 1, null })
743 >    {
744 >        final AtomicInteger a = new AtomicInteger(0);
745 >        final CFException ex1 = new CFException();
746 >        final CFException ex2 = new CFException();
747 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
748 >        if (!createIncomplete) f.completeExceptionally(ex1);
749 >        final CompletableFuture<Integer> g = f.exceptionally
750 >            ((Throwable t) -> {
751 >                threadAssertSame(t, ex1);
752 >                a.getAndIncrement();
753 >                throw ex2;
754 >            });
755 >        if (createIncomplete) f.completeExceptionally(ex1);
756 >
757 >        checkCompletedWithWrappedCFException(g, ex2);
758 >        assertEquals(1, a.get());
759 >    }}
760  
761      /**
762 <     * handle action completes normally with function value on either
763 <     * normal or exceptional completion of source
762 >     * handle action completes normally with function value on normal
763 >     * completion of source
764       */
765 <    public void testHandle() {
766 <        CompletableFuture<Integer> f, g;
767 <        IntegerHandler r;
765 >    public void testHandle_normalCompletion() {
766 >        for (ExecutionMode m : ExecutionMode.values())
767 >        for (boolean createIncomplete : new boolean[] { true, false })
768 >        for (Integer v1 : new Integer[] { 1, null })
769 >    {
770 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
771 >        final AtomicInteger a = new AtomicInteger(0);
772 >        if (!createIncomplete) f.complete(v1);
773 >        final CompletableFuture<Integer> g = m.handle
774 >            (f,
775 >             (Integer x, Throwable t) -> {
776 >                threadAssertSame(x, v1);
777 >                threadAssertNull(t);
778 >                a.getAndIncrement();
779 >                return inc(v1);
780 >            });
781 >        if (createIncomplete) f.complete(v1);
782  
783 <        f = new CompletableFuture<>();
784 <        f.completeExceptionally(new CFException());
785 <        g = f.handle(r = new IntegerHandler());
786 <        assertTrue(r.ran);
687 <        checkCompletedNormally(g, three);
783 >        checkCompletedNormally(g, inc(v1));
784 >        checkCompletedNormally(f, v1);
785 >        assertEquals(1, a.get());
786 >    }}
787  
788 <        f = new CompletableFuture<>();
789 <        g = f.handle(r = new IntegerHandler());
790 <        assertFalse(r.ran);
791 <        f.completeExceptionally(new CFException());
792 <        checkCompletedNormally(g, three);
793 <        assertTrue(r.ran);
788 >    /**
789 >     * handle action completes normally with function value on
790 >     * exceptional completion of source
791 >     */
792 >    public void testHandle_exceptionalCompletion() {
793 >        for (ExecutionMode m : ExecutionMode.values())
794 >        for (boolean createIncomplete : new boolean[] { true, false })
795 >        for (Integer v1 : new Integer[] { 1, null })
796 >    {
797 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
798 >        final AtomicInteger a = new AtomicInteger(0);
799 >        final CFException ex = new CFException();
800 >        if (!createIncomplete) f.completeExceptionally(ex);
801 >        final CompletableFuture<Integer> g = m.handle
802 >            (f,
803 >             (Integer x, Throwable t) -> {
804 >                threadAssertNull(x);
805 >                threadAssertSame(t, ex);
806 >                a.getAndIncrement();
807 >                return v1;
808 >            });
809 >        if (createIncomplete) f.completeExceptionally(ex);
810  
811 <        f = new CompletableFuture<>();
812 <        f.complete(one);
813 <        g = f.handle(r = new IntegerHandler());
814 <        assertTrue(r.ran);
700 <        checkCompletedNormally(g, two);
811 >        checkCompletedNormally(g, v1);
812 >        checkCompletedWithWrappedCFException(f, ex);
813 >        assertEquals(1, a.get());
814 >    }}
815  
816 <        f = new CompletableFuture<>();
817 <        g = f.handle(r = new IntegerHandler());
818 <        assertFalse(r.ran);
819 <        f.complete(one);
820 <        assertTrue(r.ran);
821 <        checkCompletedNormally(g, two);
822 <    }
816 >    /**
817 >     * handle action completes normally with function value on
818 >     * cancelled source
819 >     */
820 >    public void testHandle_sourceCancelled() {
821 >        for (ExecutionMode m : ExecutionMode.values())
822 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
823 >        for (boolean createIncomplete : new boolean[] { true, false })
824 >        for (Integer v1 : new Integer[] { 1, null })
825 >    {
826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
827 >        final AtomicInteger a = new AtomicInteger(0);
828 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
829 >        final CompletableFuture<Integer> g = m.handle
830 >            (f,
831 >             (Integer x, Throwable t) -> {
832 >                threadAssertNull(x);
833 >                threadAssertTrue(t instanceof CancellationException);
834 >                a.getAndIncrement();
835 >                return v1;
836 >            });
837 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
838 >
839 >        checkCompletedNormally(g, v1);
840 >        checkCancelled(f);
841 >        assertEquals(1, a.get());
842 >    }}
843 >
844 >    /**
845 >     * handle result completes exceptionally if action does
846 >     */
847 >    public void testHandle_sourceFailedActionFailed() {
848 >        for (ExecutionMode m : ExecutionMode.values())
849 >        for (boolean createIncomplete : new boolean[] { true, false })
850 >    {
851 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
852 >        final AtomicInteger a = new AtomicInteger(0);
853 >        final CFException ex1 = new CFException();
854 >        final CFException ex2 = new CFException();
855 >        if (!createIncomplete) f.completeExceptionally(ex1);
856 >        final CompletableFuture<Integer> g = m.handle
857 >            (f,
858 >             (Integer x, Throwable t) -> {
859 >                threadAssertNull(x);
860 >                threadAssertSame(ex1, t);
861 >                a.getAndIncrement();
862 >                throw ex2;
863 >            });
864 >        if (createIncomplete) f.completeExceptionally(ex1);
865 >
866 >        checkCompletedWithWrappedCFException(g, ex2);
867 >        checkCompletedWithWrappedCFException(f, ex1);
868 >        assertEquals(1, a.get());
869 >    }}
870 >
871 >    public void testHandle_sourceCompletedNormallyActionFailed() {
872 >        for (ExecutionMode m : ExecutionMode.values())
873 >        for (boolean createIncomplete : new boolean[] { true, false })
874 >        for (Integer v1 : new Integer[] { 1, null })
875 >    {
876 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
877 >        final AtomicInteger a = new AtomicInteger(0);
878 >        final CFException ex = new CFException();
879 >        if (!createIncomplete) f.complete(v1);
880 >        final CompletableFuture<Integer> g = m.handle
881 >            (f,
882 >             (Integer x, Throwable t) -> {
883 >                threadAssertSame(x, v1);
884 >                threadAssertNull(t);
885 >                a.getAndIncrement();
886 >                throw ex;
887 >            });
888 >        if (createIncomplete) f.complete(v1);
889 >
890 >        checkCompletedWithWrappedCFException(g, ex);
891 >        checkCompletedNormally(f, v1);
892 >        assertEquals(1, a.get());
893 >    }}
894  
895      /**
896       * runAsync completes after running Runnable
# Line 714 | Line 899 | public class CompletableFutureTest exten
899          Noop r = new Noop();
900          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
901          assertNull(f.join());
902 <        assertTrue(r.ran);
902 >        assertEquals(1, r.invocationCount);
903          checkCompletedNormally(f, null);
904      }
905  
# Line 726 | Line 911 | public class CompletableFutureTest exten
911          ThreadExecutor exec = new ThreadExecutor();
912          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
913          assertNull(f.join());
914 <        assertTrue(r.ran);
914 >        assertEquals(1, r.invocationCount);
915          checkCompletedNormally(f, null);
916          assertEquals(1, exec.count.get());
917      }
# Line 738 | Line 923 | public class CompletableFutureTest exten
923          FailingNoop r = new FailingNoop();
924          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
925          checkCompletedWithWrappedCFException(f);
926 <        assertTrue(r.ran);
926 >        assertEquals(1, r.invocationCount);
927      }
928  
929      /**
# Line 768 | Line 953 | public class CompletableFutureTest exten
953          FailingSupplier r = new FailingSupplier();
954          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
955          checkCompletedWithWrappedCFException(f);
956 <        assertTrue(r.ran);
956 >        assertEquals(1, r.invocationCount);
957      }
958  
959      // seq completion methods
# Line 785 | Line 970 | public class CompletableFutureTest exten
970          g = f.thenRun(r = new Noop());
971          f.complete(null);
972          checkCompletedNormally(g, null);
973 <        assertTrue(r.ran);
973 >        assertEquals(1, r.invocationCount);
974  
975          f = new CompletableFuture<>();
976          f.complete(null);
977          g = f.thenRun(r = new Noop());
978          checkCompletedNormally(g, null);
979 <        assertTrue(r.ran);
979 >        assertEquals(1, r.invocationCount);
980      }
981  
982      /**
# Line 807 | Line 992 | public class CompletableFutureTest exten
992          g = f.thenRun(r = new Noop());
993          f.completeExceptionally(new CFException());
994          checkCompletedWithWrappedCFException(g);
995 <        assertFalse(r.ran);
995 >        assertEquals(0, r.invocationCount);
996  
997          f = new CompletableFuture<>();
998          f.completeExceptionally(new CFException());
999          g = f.thenRun(r = new Noop());
1000          checkCompletedWithWrappedCFException(g);
1001 <        assertFalse(r.ran);
1001 >        assertEquals(0, r.invocationCount);
1002      }
1003  
1004      /**
# Line 928 | Line 1113 | public class CompletableFutureTest exten
1113          CompletableFuture<Void> g = f.thenAccept(r);
1114          f.complete(one);
1115          checkCompletedWithWrappedCFException(g);
1116 <        assertTrue(r.ran);
1116 >        assertEquals(1, r.invocationCount);
1117      }
1118  
1119      /**
# Line 947 | Line 1132 | public class CompletableFutureTest exten
1132       * of sources
1133       */
1134      public void testThenCombine_normalCompletion1() {
1135 +        for (boolean createIncomplete : new boolean[] { true, false })
1136 +        for (boolean fFirst : new boolean[] { true, false })
1137          for (ExecutionMode m : ExecutionMode.values())
1138          for (Integer v1 : new Integer[] { 1, null })
1139 <        for (Integer v2 : new Integer[] { 2, null }) {
1140 <
1139 >        for (Integer v2 : new Integer[] { 2, null })
1140 >    {
1141          final CompletableFuture<Integer> f = new CompletableFuture<>();
1142          final CompletableFuture<Integer> g = new CompletableFuture<>();
1143          final SubtractFunction r = new SubtractFunction();
1144 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1144 >        CompletableFuture<Integer> h = null;
1145 >        if (createIncomplete) h = m.thenCombine(f, g, r);
1146  
1147 <        f.complete(v1);
1148 <        checkIncomplete(h);
1149 <        assertEquals(r.invocationCount, 0);
1150 <        g.complete(v2);
1147 >        if (fFirst)
1148 >            f.complete(v1);
1149 >        else
1150 >            g.complete(v2);
1151 >        if (createIncomplete) checkIncomplete(h);
1152 >        assertEquals(0, r.invocationCount);
1153 >        if (!fFirst)
1154 >            f.complete(v1);
1155 >        else
1156 >            g.complete(v2);
1157 >        if (!createIncomplete) h = m.thenCombine(f, g, r);
1158  
1159          checkCompletedNormally(h, subtract(v1, v2));
1160          checkCompletedNormally(f, v1);
1161          checkCompletedNormally(g, v2);
1162 <        assertEquals(r.invocationCount, 1);
1163 <        }
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);
985 <
986 <        checkCompletedNormally(h, subtract(v1, v2));
987 <        checkCompletedNormally(f, v1);
988 <        checkCompletedNormally(g, v2);
989 <        assertEquals(r.invocationCount, 1);
990 <        }
991 <    }
992 <
993 <    public void testThenCombine_normalCompletion3() {
994 <        for (ExecutionMode m : ExecutionMode.values())
995 <        for (Integer v1 : new Integer[] { 1, null })
996 <        for (Integer v2 : new Integer[] { 2, null }) {
997 <
998 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
999 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1000 <        final SubtractFunction r = new SubtractFunction();
1001 <
1002 <        g.complete(v2);
1003 <        f.complete(v1);
1004 <        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);
1010 <        }
1011 <    }
1012 <
1013 <    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));
1027 <        checkCompletedNormally(f, v1);
1028 <        checkCompletedNormally(g, v2);
1029 <        assertEquals(r.invocationCount, 1);
1030 <        }
1031 <    }
1162 >        assertEquals(1, r.invocationCount);
1163 >    }}
1164  
1165      /**
1166       * thenCombine result completes exceptionally after exceptional
# Line 1036 | Line 1168 | public class CompletableFutureTest exten
1168       */
1169      public void testThenCombine_exceptionalCompletion1() {
1170          for (ExecutionMode m : ExecutionMode.values())
1171 <        for (Integer v1 : new Integer[] { 1, null }) {
1172 <
1171 >        for (Integer v1 : new Integer[] { 1, null })
1172 >    {
1173          final CompletableFuture<Integer> f = new CompletableFuture<>();
1174          final CompletableFuture<Integer> g = new CompletableFuture<>();
1175          final SubtractFunction r = new SubtractFunction();
# Line 1050 | Line 1182 | public class CompletableFutureTest exten
1182  
1183          checkCompletedWithWrappedCFException(h, ex);
1184          checkCompletedWithWrappedCFException(f, ex);
1185 <        assertEquals(r.invocationCount, 0);
1185 >        assertEquals(0, r.invocationCount);
1186          checkCompletedNormally(g, v1);
1187 <        }
1056 <    }
1187 >    }}
1188  
1189      public void testThenCombine_exceptionalCompletion2() {
1190          for (ExecutionMode m : ExecutionMode.values())
1191 <        for (Integer v1 : new Integer[] { 1, null }) {
1192 <
1191 >        for (Integer v1 : new Integer[] { 1, null })
1192 >    {
1193          final CompletableFuture<Integer> f = new CompletableFuture<>();
1194          final CompletableFuture<Integer> g = new CompletableFuture<>();
1195          final SubtractFunction r = new SubtractFunction();
# Line 1071 | Line 1202 | public class CompletableFutureTest exten
1202  
1203          checkCompletedWithWrappedCFException(h, ex);
1204          checkCompletedWithWrappedCFException(g, ex);
1205 <        assertEquals(r.invocationCount, 0);
1205 >        assertEquals(0, r.invocationCount);
1206          checkCompletedNormally(f, v1);
1207 <        }
1077 <    }
1207 >    }}
1208  
1209      public void testThenCombine_exceptionalCompletion3() {
1210          for (ExecutionMode m : ExecutionMode.values())
1211 <        for (Integer v1 : new Integer[] { 1, null }) {
1212 <
1211 >        for (Integer v1 : new Integer[] { 1, null })
1212 >    {
1213          final CompletableFuture<Integer> f = new CompletableFuture<>();
1214          final CompletableFuture<Integer> g = new CompletableFuture<>();
1215          final SubtractFunction r = new SubtractFunction();
# Line 1091 | Line 1221 | public class CompletableFutureTest exten
1221  
1222          checkCompletedWithWrappedCFException(h, ex);
1223          checkCompletedWithWrappedCFException(g, ex);
1224 <        assertEquals(r.invocationCount, 0);
1224 >        assertEquals(0, r.invocationCount);
1225          checkCompletedNormally(f, v1);
1226 <        }
1097 <    }
1226 >    }}
1227  
1228      public void testThenCombine_exceptionalCompletion4() {
1229          for (ExecutionMode m : ExecutionMode.values())
1230 <        for (Integer v1 : new Integer[] { 1, null }) {
1231 <
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();
# Line 1111 | Line 1240 | public class CompletableFutureTest exten
1240  
1241          checkCompletedWithWrappedCFException(h, ex);
1242          checkCompletedWithWrappedCFException(f, ex);
1243 <        assertEquals(r.invocationCount, 0);
1243 >        assertEquals(0, r.invocationCount);
1244          checkCompletedNormally(g, v1);
1245 <        }
1117 <    }
1245 >    }}
1246  
1247      /**
1248       * thenCombine result completes exceptionally if action does
# Line 1122 | Line 1250 | public class CompletableFutureTest exten
1250      public void testThenCombine_actionFailed1() {
1251          for (ExecutionMode m : ExecutionMode.values())
1252          for (Integer v1 : new Integer[] { 1, null })
1253 <        for (Integer v2 : new Integer[] { 2, null }) {
1254 <
1253 >        for (Integer v2 : new Integer[] { 2, null })
1254 >    {
1255          final CompletableFuture<Integer> f = new CompletableFuture<>();
1256          final CompletableFuture<Integer> g = new CompletableFuture<>();
1257          final FailingBiFunction r = new FailingBiFunction();
# Line 1136 | Line 1264 | public class CompletableFutureTest exten
1264          checkCompletedWithWrappedCFException(h);
1265          checkCompletedNormally(f, v1);
1266          checkCompletedNormally(g, v2);
1267 <        }
1140 <    }
1267 >    }}
1268  
1269      public void testThenCombine_actionFailed2() {
1270          for (ExecutionMode m : ExecutionMode.values())
1271          for (Integer v1 : new Integer[] { 1, null })
1272 <        for (Integer v2 : new Integer[] { 2, null }) {
1273 <
1272 >        for (Integer v2 : new Integer[] { 2, null })
1273 >    {
1274          final CompletableFuture<Integer> f = new CompletableFuture<>();
1275          final CompletableFuture<Integer> g = new CompletableFuture<>();
1276          final FailingBiFunction r = new FailingBiFunction();
# Line 1156 | Line 1283 | public class CompletableFutureTest exten
1283          checkCompletedWithWrappedCFException(h);
1284          checkCompletedNormally(f, v1);
1285          checkCompletedNormally(g, v2);
1286 <        }
1160 <    }
1286 >    }}
1287  
1288      /**
1289       * thenCombine result completes exceptionally if either source cancelled
# Line 1165 | Line 1291 | public class CompletableFutureTest exten
1291      public void testThenCombine_sourceCancelled1() {
1292          for (ExecutionMode m : ExecutionMode.values())
1293          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294 <        for (Integer v1 : new Integer[] { 1, null }) {
1295 <
1294 >        for (Integer v1 : new Integer[] { 1, null })
1295 >    {
1296          final CompletableFuture<Integer> f = new CompletableFuture<>();
1297          final CompletableFuture<Integer> g = new CompletableFuture<>();
1298          final SubtractFunction r = new SubtractFunction();
# Line 1178 | Line 1304 | public class CompletableFutureTest exten
1304  
1305          checkCompletedWithWrappedCancellationException(h);
1306          checkCancelled(f);
1307 <        assertEquals(r.invocationCount, 0);
1307 >        assertEquals(0, r.invocationCount);
1308          checkCompletedNormally(g, v1);
1309 <        }
1184 <    }
1309 >    }}
1310  
1311      public void testThenCombine_sourceCancelled2() {
1312          for (ExecutionMode m : ExecutionMode.values())
1313          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1314 <        for (Integer v1 : new Integer[] { 1, null }) {
1315 <
1314 >        for (Integer v1 : new Integer[] { 1, null })
1315 >    {
1316          final CompletableFuture<Integer> f = new CompletableFuture<>();
1317          final CompletableFuture<Integer> g = new CompletableFuture<>();
1318          final SubtractFunction r = new SubtractFunction();
# Line 1199 | Line 1324 | public class CompletableFutureTest exten
1324  
1325          checkCompletedWithWrappedCancellationException(h);
1326          checkCancelled(g);
1327 <        assertEquals(r.invocationCount, 0);
1327 >        assertEquals(0, r.invocationCount);
1328          checkCompletedNormally(f, v1);
1329 <        }
1205 <    }
1329 >    }}
1330  
1331      public void testThenCombine_sourceCancelled3() {
1332          for (ExecutionMode m : ExecutionMode.values())
1333          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1334 <        for (Integer v1 : new Integer[] { 1, null }) {
1335 <
1334 >        for (Integer v1 : new Integer[] { 1, null })
1335 >    {
1336          final CompletableFuture<Integer> f = new CompletableFuture<>();
1337          final CompletableFuture<Integer> g = new CompletableFuture<>();
1338          final SubtractFunction r = new SubtractFunction();
# Line 1219 | Line 1343 | public class CompletableFutureTest exten
1343  
1344          checkCompletedWithWrappedCancellationException(h);
1345          checkCancelled(g);
1346 <        assertEquals(r.invocationCount, 0);
1346 >        assertEquals(0, r.invocationCount);
1347          checkCompletedNormally(f, v1);
1348 <        }
1225 <    }
1348 >    }}
1349  
1350      public void testThenCombine_sourceCancelled4() {
1351          for (ExecutionMode m : ExecutionMode.values())
1352          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1353 <        for (Integer v1 : new Integer[] { 1, null }) {
1354 <
1353 >        for (Integer v1 : new Integer[] { 1, null })
1354 >    {
1355          final CompletableFuture<Integer> f = new CompletableFuture<>();
1356          final CompletableFuture<Integer> g = new CompletableFuture<>();
1357          final SubtractFunction r = new SubtractFunction();
# Line 1239 | Line 1362 | public class CompletableFutureTest exten
1362  
1363          checkCompletedWithWrappedCancellationException(h);
1364          checkCancelled(f);
1365 <        assertEquals(r.invocationCount, 0);
1365 >        assertEquals(0, r.invocationCount);
1366          checkCompletedNormally(g, v1);
1367 <        }
1245 <    }
1367 >    }}
1368  
1369      /**
1370       * thenAcceptBoth result completes normally after normal
# Line 1251 | Line 1373 | public class CompletableFutureTest exten
1373      public void testThenAcceptBoth_normalCompletion1() {
1374          for (ExecutionMode m : ExecutionMode.values())
1375          for (Integer v1 : new Integer[] { 1, null })
1376 <        for (Integer v2 : new Integer[] { 2, null }) {
1377 <
1376 >        for (Integer v2 : new Integer[] { 2, null })
1377 >    {
1378          final CompletableFuture<Integer> f = new CompletableFuture<>();
1379          final CompletableFuture<Integer> g = new CompletableFuture<>();
1380          final SubtractAction r = new SubtractAction();
# Line 1260 | Line 1382 | public class CompletableFutureTest exten
1382  
1383          f.complete(v1);
1384          checkIncomplete(h);
1385 <        assertFalse(r.ran());
1385 >        assertEquals(0, r.invocationCount);
1386          g.complete(v2);
1387  
1388          checkCompletedNormally(h, null);
1389 <        assertEquals(r.value, subtract(v1, v2));
1389 >        assertEquals(subtract(v1, v2), r.value);
1390          checkCompletedNormally(f, v1);
1391          checkCompletedNormally(g, v2);
1392 <        }
1271 <    }
1392 >    }}
1393  
1394      public void testThenAcceptBoth_normalCompletion2() {
1395          for (ExecutionMode m : ExecutionMode.values())
1396          for (Integer v1 : new Integer[] { 1, null })
1397 <        for (Integer v2 : new Integer[] { 2, null }) {
1398 <
1397 >        for (Integer v2 : new Integer[] { 2, null })
1398 >    {
1399          final CompletableFuture<Integer> f = new CompletableFuture<>();
1400          final CompletableFuture<Integer> g = new CompletableFuture<>();
1401          final SubtractAction r = new SubtractAction();
# Line 1282 | Line 1403 | public class CompletableFutureTest exten
1403  
1404          g.complete(v2);
1405          checkIncomplete(h);
1406 <        assertFalse(r.ran());
1406 >        assertEquals(0, r.invocationCount);
1407          f.complete(v1);
1408  
1409          checkCompletedNormally(h, null);
1410 <        assertEquals(r.value, subtract(v1, v2));
1410 >        assertEquals(subtract(v1, v2), r.value);
1411          checkCompletedNormally(f, v1);
1412          checkCompletedNormally(g, v2);
1413 <        }
1293 <    }
1413 >    }}
1414  
1415      public void testThenAcceptBoth_normalCompletion3() {
1416          for (ExecutionMode m : ExecutionMode.values())
1417          for (Integer v1 : new Integer[] { 1, null })
1418 <        for (Integer v2 : new Integer[] { 2, null }) {
1419 <
1418 >        for (Integer v2 : new Integer[] { 2, null })
1419 >    {
1420          final CompletableFuture<Integer> f = new CompletableFuture<>();
1421          final CompletableFuture<Integer> g = new CompletableFuture<>();
1422          final SubtractAction r = new SubtractAction();
# Line 1306 | Line 1426 | public class CompletableFutureTest exten
1426          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1427  
1428          checkCompletedNormally(h, null);
1429 <        assertEquals(r.value, subtract(v1, v2));
1429 >        assertEquals(subtract(v1, v2), r.value);
1430          checkCompletedNormally(f, v1);
1431          checkCompletedNormally(g, v2);
1432 <        }
1313 <    }
1432 >    }}
1433  
1434      public void testThenAcceptBoth_normalCompletion4() {
1435          for (ExecutionMode m : ExecutionMode.values())
1436          for (Integer v1 : new Integer[] { 1, null })
1437 <        for (Integer v2 : new Integer[] { 2, null }) {
1438 <
1437 >        for (Integer v2 : new Integer[] { 2, null })
1438 >    {
1439          final CompletableFuture<Integer> f = new CompletableFuture<>();
1440          final CompletableFuture<Integer> g = new CompletableFuture<>();
1441          final SubtractAction r = new SubtractAction();
# Line 1326 | Line 1445 | public class CompletableFutureTest exten
1445          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1446  
1447          checkCompletedNormally(h, null);
1448 <        assertEquals(r.value, subtract(v1, v2));
1448 >        assertEquals(subtract(v1, v2), r.value);
1449          checkCompletedNormally(f, v1);
1450          checkCompletedNormally(g, v2);
1451 <        }
1333 <    }
1451 >    }}
1452  
1453      /**
1454       * thenAcceptBoth result completes exceptionally after exceptional
# Line 1338 | Line 1456 | public class CompletableFutureTest exten
1456       */
1457      public void testThenAcceptBoth_exceptionalCompletion1() {
1458          for (ExecutionMode m : ExecutionMode.values())
1459 <        for (Integer v1 : new Integer[] { 1, null }) {
1460 <
1459 >        for (Integer v1 : new Integer[] { 1, null })
1460 >    {
1461          final CompletableFuture<Integer> f = new CompletableFuture<>();
1462          final CompletableFuture<Integer> g = new CompletableFuture<>();
1463          final SubtractAction r = new SubtractAction();
# Line 1352 | Line 1470 | public class CompletableFutureTest exten
1470  
1471          checkCompletedWithWrappedCFException(h, ex);
1472          checkCompletedWithWrappedCFException(f, ex);
1473 <        assertEquals(r.invocationCount, 0);
1473 >        assertEquals(0, r.invocationCount);
1474          checkCompletedNormally(g, v1);
1475 <        }
1358 <    }
1475 >    }}
1476  
1477      public void testThenAcceptBoth_exceptionalCompletion2() {
1478          for (ExecutionMode m : ExecutionMode.values())
1479 <        for (Integer v1 : new Integer[] { 1, null }) {
1480 <
1479 >        for (Integer v1 : new Integer[] { 1, null })
1480 >    {
1481          final CompletableFuture<Integer> f = new CompletableFuture<>();
1482          final CompletableFuture<Integer> g = new CompletableFuture<>();
1483          final SubtractAction r = new SubtractAction();
# Line 1373 | Line 1490 | public class CompletableFutureTest exten
1490  
1491          checkCompletedWithWrappedCFException(h, ex);
1492          checkCompletedWithWrappedCFException(g, ex);
1493 <        assertEquals(r.invocationCount, 0);
1493 >        assertEquals(0, r.invocationCount);
1494          checkCompletedNormally(f, v1);
1495 <        }
1379 <    }
1495 >    }}
1496  
1497      public void testThenAcceptBoth_exceptionalCompletion3() {
1498          for (ExecutionMode m : ExecutionMode.values())
1499 <        for (Integer v1 : new Integer[] { 1, null }) {
1500 <
1499 >        for (Integer v1 : new Integer[] { 1, null })
1500 >    {
1501          final CompletableFuture<Integer> f = new CompletableFuture<>();
1502          final CompletableFuture<Integer> g = new CompletableFuture<>();
1503          final SubtractAction r = new SubtractAction();
# Line 1393 | Line 1509 | public class CompletableFutureTest exten
1509  
1510          checkCompletedWithWrappedCFException(h, ex);
1511          checkCompletedWithWrappedCFException(g, ex);
1512 <        assertEquals(r.invocationCount, 0);
1512 >        assertEquals(0, r.invocationCount);
1513          checkCompletedNormally(f, v1);
1514 <        }
1399 <    }
1514 >    }}
1515  
1516      public void testThenAcceptBoth_exceptionalCompletion4() {
1517          for (ExecutionMode m : ExecutionMode.values())
1518 <        for (Integer v1 : new Integer[] { 1, null }) {
1519 <
1518 >        for (Integer v1 : new Integer[] { 1, null })
1519 >    {
1520          final CompletableFuture<Integer> f = new CompletableFuture<>();
1521          final CompletableFuture<Integer> g = new CompletableFuture<>();
1522          final SubtractAction r = new SubtractAction();
# Line 1413 | Line 1528 | public class CompletableFutureTest exten
1528  
1529          checkCompletedWithWrappedCFException(h, ex);
1530          checkCompletedWithWrappedCFException(f, ex);
1531 <        assertFalse(r.ran());
1531 >        assertEquals(0, r.invocationCount);
1532          checkCompletedNormally(g, v1);
1533 <        }
1419 <    }
1533 >    }}
1534  
1535      /**
1536       * thenAcceptBoth result completes exceptionally if action does
# Line 1424 | Line 1538 | public class CompletableFutureTest exten
1538      public void testThenAcceptBoth_actionFailed1() {
1539          for (ExecutionMode m : ExecutionMode.values())
1540          for (Integer v1 : new Integer[] { 1, null })
1541 <        for (Integer v2 : new Integer[] { 2, null }) {
1542 <
1541 >        for (Integer v2 : new Integer[] { 2, null })
1542 >    {
1543          final CompletableFuture<Integer> f = new CompletableFuture<>();
1544          final CompletableFuture<Integer> g = new CompletableFuture<>();
1545          final FailingBiConsumer r = new FailingBiConsumer();
# Line 1438 | Line 1552 | public class CompletableFutureTest exten
1552          checkCompletedWithWrappedCFException(h);
1553          checkCompletedNormally(f, v1);
1554          checkCompletedNormally(g, v2);
1555 <        }
1442 <    }
1555 >    }}
1556  
1557      public void testThenAcceptBoth_actionFailed2() {
1558          for (ExecutionMode m : ExecutionMode.values())
1559          for (Integer v1 : new Integer[] { 1, null })
1560 <        for (Integer v2 : new Integer[] { 2, null }) {
1561 <
1560 >        for (Integer v2 : new Integer[] { 2, null })
1561 >    {
1562          final CompletableFuture<Integer> f = new CompletableFuture<>();
1563          final CompletableFuture<Integer> g = new CompletableFuture<>();
1564          final FailingBiConsumer r = new FailingBiConsumer();
# Line 1458 | Line 1571 | public class CompletableFutureTest exten
1571          checkCompletedWithWrappedCFException(h);
1572          checkCompletedNormally(f, v1);
1573          checkCompletedNormally(g, v2);
1574 <        }
1462 <    }
1574 >    }}
1575  
1576      /**
1577       * thenAcceptBoth result completes exceptionally if either source cancelled
# Line 1467 | Line 1579 | public class CompletableFutureTest exten
1579      public void testThenAcceptBoth_sourceCancelled1() {
1580          for (ExecutionMode m : ExecutionMode.values())
1581          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1582 <        for (Integer v1 : new Integer[] { 1, null }) {
1583 <
1582 >        for (Integer v1 : new Integer[] { 1, null })
1583 >    {
1584          final CompletableFuture<Integer> f = new CompletableFuture<>();
1585          final CompletableFuture<Integer> g = new CompletableFuture<>();
1586          final SubtractAction r = new SubtractAction();
# Line 1480 | Line 1592 | public class CompletableFutureTest exten
1592  
1593          checkCompletedWithWrappedCancellationException(h);
1594          checkCancelled(f);
1595 <        assertEquals(r.invocationCount, 0);
1595 >        assertEquals(0, r.invocationCount);
1596          checkCompletedNormally(g, v1);
1597 <        }
1486 <    }
1597 >    }}
1598  
1599      public void testThenAcceptBoth_sourceCancelled2() {
1600          for (ExecutionMode m : ExecutionMode.values())
1601          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1602 <        for (Integer v1 : new Integer[] { 1, null }) {
1603 <
1602 >        for (Integer v1 : new Integer[] { 1, null })
1603 >    {
1604          final CompletableFuture<Integer> f = new CompletableFuture<>();
1605          final CompletableFuture<Integer> g = new CompletableFuture<>();
1606          final SubtractAction r = new SubtractAction();
# Line 1501 | Line 1612 | public class CompletableFutureTest exten
1612  
1613          checkCompletedWithWrappedCancellationException(h);
1614          checkCancelled(g);
1615 <        assertEquals(r.invocationCount, 0);
1615 >        assertEquals(0, r.invocationCount);
1616          checkCompletedNormally(f, v1);
1617 <        }
1507 <    }
1617 >    }}
1618  
1619      public void testThenAcceptBoth_sourceCancelled3() {
1620          for (ExecutionMode m : ExecutionMode.values())
1621          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1622 <        for (Integer v1 : new Integer[] { 1, null }) {
1623 <
1622 >        for (Integer v1 : new Integer[] { 1, null })
1623 >    {
1624          final CompletableFuture<Integer> f = new CompletableFuture<>();
1625          final CompletableFuture<Integer> g = new CompletableFuture<>();
1626          final SubtractAction r = new SubtractAction();
# Line 1521 | Line 1631 | public class CompletableFutureTest exten
1631  
1632          checkCompletedWithWrappedCancellationException(h);
1633          checkCancelled(g);
1634 <        assertEquals(r.invocationCount, 0);
1634 >        assertEquals(0, r.invocationCount);
1635          checkCompletedNormally(f, v1);
1636 <        }
1527 <    }
1636 >    }}
1637  
1638      public void testThenAcceptBoth_sourceCancelled4() {
1639          for (ExecutionMode m : ExecutionMode.values())
1640          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1641 <        for (Integer v1 : new Integer[] { 1, null }) {
1642 <
1641 >        for (Integer v1 : new Integer[] { 1, null })
1642 >    {
1643          final CompletableFuture<Integer> f = new CompletableFuture<>();
1644          final CompletableFuture<Integer> g = new CompletableFuture<>();
1645          final SubtractAction r = new SubtractAction();
# Line 1541 | Line 1650 | public class CompletableFutureTest exten
1650  
1651          checkCompletedWithWrappedCancellationException(h);
1652          checkCancelled(f);
1653 <        assertEquals(r.invocationCount, 0);
1653 >        assertEquals(0, r.invocationCount);
1654          checkCompletedNormally(g, v1);
1655 <        }
1547 <    }
1655 >    }}
1656  
1657      /**
1658       * runAfterBoth result completes normally after normal
# Line 1553 | Line 1661 | public class CompletableFutureTest exten
1661      public void testRunAfterBoth_normalCompletion1() {
1662          for (ExecutionMode m : ExecutionMode.values())
1663          for (Integer v1 : new Integer[] { 1, null })
1664 <        for (Integer v2 : new Integer[] { 2, null }) {
1665 <
1664 >        for (Integer v2 : new Integer[] { 2, null })
1665 >    {
1666          final CompletableFuture<Integer> f = new CompletableFuture<>();
1667          final CompletableFuture<Integer> g = new CompletableFuture<>();
1668          final Noop r = new Noop();
# Line 1562 | Line 1670 | public class CompletableFutureTest exten
1670  
1671          f.complete(v1);
1672          checkIncomplete(h);
1673 <        assertFalse(r.ran);
1673 >        assertEquals(0, r.invocationCount);
1674          g.complete(v2);
1675  
1676          checkCompletedNormally(h, null);
1677 <        assertEquals(r.invocationCount, 1);
1677 >        assertEquals(1, r.invocationCount);
1678          checkCompletedNormally(f, v1);
1679          checkCompletedNormally(g, v2);
1680 <        }
1573 <    }
1680 >    }}
1681  
1682      public void testRunAfterBoth_normalCompletion2() {
1683          for (ExecutionMode m : ExecutionMode.values())
1684          for (Integer v1 : new Integer[] { 1, null })
1685 <        for (Integer v2 : new Integer[] { 2, null }) {
1686 <
1685 >        for (Integer v2 : new Integer[] { 2, null })
1686 >    {
1687          final CompletableFuture<Integer> f = new CompletableFuture<>();
1688          final CompletableFuture<Integer> g = new CompletableFuture<>();
1689          final Noop r = new Noop();
# Line 1584 | Line 1691 | public class CompletableFutureTest exten
1691  
1692          g.complete(v2);
1693          checkIncomplete(h);
1694 <        assertFalse(r.ran);
1694 >        assertEquals(0, r.invocationCount);
1695          f.complete(v1);
1696  
1697          checkCompletedNormally(h, null);
1698 <        assertEquals(r.invocationCount, 1);
1698 >        assertEquals(1, r.invocationCount);
1699          checkCompletedNormally(f, v1);
1700          checkCompletedNormally(g, v2);
1701 <        }
1595 <    }
1701 >    }}
1702  
1703      public void testRunAfterBoth_normalCompletion3() {
1704          for (ExecutionMode m : ExecutionMode.values())
1705          for (Integer v1 : new Integer[] { 1, null })
1706 <        for (Integer v2 : new Integer[] { 2, null }) {
1707 <
1706 >        for (Integer v2 : new Integer[] { 2, null })
1707 >    {
1708          final CompletableFuture<Integer> f = new CompletableFuture<>();
1709          final CompletableFuture<Integer> g = new CompletableFuture<>();
1710          final Noop r = new Noop();
# Line 1608 | Line 1714 | public class CompletableFutureTest exten
1714          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715  
1716          checkCompletedNormally(h, null);
1717 <        assertTrue(r.ran);
1717 >        assertEquals(1, r.invocationCount);
1718          checkCompletedNormally(f, v1);
1719          checkCompletedNormally(g, v2);
1720 <        }
1615 <    }
1720 >    }}
1721  
1722      public void testRunAfterBoth_normalCompletion4() {
1723          for (ExecutionMode m : ExecutionMode.values())
1724          for (Integer v1 : new Integer[] { 1, null })
1725 <        for (Integer v2 : new Integer[] { 2, null }) {
1726 <
1725 >        for (Integer v2 : new Integer[] { 2, null })
1726 >    {
1727          final CompletableFuture<Integer> f = new CompletableFuture<>();
1728          final CompletableFuture<Integer> g = new CompletableFuture<>();
1729          final Noop r = new Noop();
# Line 1628 | Line 1733 | public class CompletableFutureTest exten
1733          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1734  
1735          checkCompletedNormally(h, null);
1736 <        assertEquals(r.invocationCount, 1);
1736 >        assertEquals(1, r.invocationCount);
1737          checkCompletedNormally(f, v1);
1738          checkCompletedNormally(g, v2);
1739 <        }
1635 <    }
1739 >    }}
1740  
1741      /**
1742       * runAfterBoth result completes exceptionally after exceptional
# Line 1640 | Line 1744 | public class CompletableFutureTest exten
1744       */
1745      public void testRunAfterBoth_exceptionalCompletion1() {
1746          for (ExecutionMode m : ExecutionMode.values())
1747 <        for (Integer v1 : new Integer[] { 1, null }) {
1748 <
1747 >        for (Integer v1 : new Integer[] { 1, null })
1748 >    {
1749          final CompletableFuture<Integer> f = new CompletableFuture<>();
1750          final CompletableFuture<Integer> g = new CompletableFuture<>();
1751          final Noop r = new Noop();
# Line 1654 | Line 1758 | public class CompletableFutureTest exten
1758  
1759          checkCompletedWithWrappedCFException(h, ex);
1760          checkCompletedWithWrappedCFException(f, ex);
1761 <        assertEquals(r.invocationCount, 0);
1761 >        assertEquals(0, r.invocationCount);
1762          checkCompletedNormally(g, v1);
1763 <        }
1660 <    }
1763 >    }}
1764  
1765      public void testRunAfterBoth_exceptionalCompletion2() {
1766          for (ExecutionMode m : ExecutionMode.values())
1767 <        for (Integer v1 : new Integer[] { 1, null }) {
1768 <
1767 >        for (Integer v1 : new Integer[] { 1, null })
1768 >    {
1769          final CompletableFuture<Integer> f = new CompletableFuture<>();
1770          final CompletableFuture<Integer> g = new CompletableFuture<>();
1771          final Noop r = new Noop();
# Line 1675 | Line 1778 | public class CompletableFutureTest exten
1778  
1779          checkCompletedWithWrappedCFException(h, ex);
1780          checkCompletedWithWrappedCFException(g, ex);
1781 <        assertEquals(r.invocationCount, 0);
1781 >        assertEquals(0, r.invocationCount);
1782          checkCompletedNormally(f, v1);
1783 <        }
1681 <    }
1783 >    }}
1784  
1785      public void testRunAfterBoth_exceptionalCompletion3() {
1786          for (ExecutionMode m : ExecutionMode.values())
1787 <        for (Integer v1 : new Integer[] { 1, null }) {
1788 <
1787 >        for (Integer v1 : new Integer[] { 1, null })
1788 >    {
1789          final CompletableFuture<Integer> f = new CompletableFuture<>();
1790          final CompletableFuture<Integer> g = new CompletableFuture<>();
1791          final Noop r = new Noop();
# Line 1695 | Line 1797 | public class CompletableFutureTest exten
1797  
1798          checkCompletedWithWrappedCFException(h, ex);
1799          checkCompletedWithWrappedCFException(g, ex);
1800 <        assertEquals(r.invocationCount, 0);
1800 >        assertEquals(0, r.invocationCount);
1801          checkCompletedNormally(f, v1);
1802 <        }
1701 <    }
1802 >    }}
1803  
1804      public void testRunAfterBoth_exceptionalCompletion4() {
1805          for (ExecutionMode m : ExecutionMode.values())
1806 <        for (Integer v1 : new Integer[] { 1, null }) {
1807 <
1806 >        for (Integer v1 : new Integer[] { 1, null })
1807 >    {
1808          final CompletableFuture<Integer> f = new CompletableFuture<>();
1809          final CompletableFuture<Integer> g = new CompletableFuture<>();
1810          final Noop r = new Noop();
# Line 1715 | Line 1816 | public class CompletableFutureTest exten
1816  
1817          checkCompletedWithWrappedCFException(h, ex);
1818          checkCompletedWithWrappedCFException(f, ex);
1819 <        assertEquals(r.invocationCount, 0);
1819 >        assertEquals(0, r.invocationCount);
1820          checkCompletedNormally(g, v1);
1821 <        }
1721 <    }
1821 >    }}
1822  
1823      /**
1824       * runAfterBoth result completes exceptionally if action does
# Line 1726 | Line 1826 | public class CompletableFutureTest exten
1826      public void testRunAfterBoth_actionFailed1() {
1827          for (ExecutionMode m : ExecutionMode.values())
1828          for (Integer v1 : new Integer[] { 1, null })
1829 <        for (Integer v2 : new Integer[] { 2, null }) {
1830 <
1829 >        for (Integer v2 : new Integer[] { 2, null })
1830 >    {
1831          final CompletableFuture<Integer> f = new CompletableFuture<>();
1832          final CompletableFuture<Integer> g = new CompletableFuture<>();
1833          final FailingNoop r = new FailingNoop();
# Line 1740 | Line 1840 | public class CompletableFutureTest exten
1840          checkCompletedWithWrappedCFException(h);
1841          checkCompletedNormally(f, v1);
1842          checkCompletedNormally(g, v2);
1843 <        }
1744 <    }
1843 >    }}
1844  
1845      public void testRunAfterBoth_actionFailed2() {
1846          for (ExecutionMode m : ExecutionMode.values())
1847          for (Integer v1 : new Integer[] { 1, null })
1848 <        for (Integer v2 : new Integer[] { 2, null }) {
1849 <
1848 >        for (Integer v2 : new Integer[] { 2, null })
1849 >    {
1850          final CompletableFuture<Integer> f = new CompletableFuture<>();
1851          final CompletableFuture<Integer> g = new CompletableFuture<>();
1852          final FailingNoop r = new FailingNoop();
# Line 1760 | Line 1859 | public class CompletableFutureTest exten
1859          checkCompletedWithWrappedCFException(h);
1860          checkCompletedNormally(f, v1);
1861          checkCompletedNormally(g, v2);
1862 <        }
1764 <    }
1862 >    }}
1863  
1864      /**
1865       * runAfterBoth result completes exceptionally if either source cancelled
# Line 1769 | Line 1867 | public class CompletableFutureTest exten
1867      public void testRunAfterBoth_sourceCancelled1() {
1868          for (ExecutionMode m : ExecutionMode.values())
1869          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1870 <        for (Integer v1 : new Integer[] { 1, null }) {
1871 <
1870 >        for (Integer v1 : new Integer[] { 1, null })
1871 >    {
1872          final CompletableFuture<Integer> f = new CompletableFuture<>();
1873          final CompletableFuture<Integer> g = new CompletableFuture<>();
1874          final Noop r = new Noop();
# Line 1782 | Line 1880 | public class CompletableFutureTest exten
1880  
1881          checkCompletedWithWrappedCancellationException(h);
1882          checkCancelled(f);
1883 <        assertEquals(r.invocationCount, 0);
1883 >        assertEquals(0, r.invocationCount);
1884          checkCompletedNormally(g, v1);
1885 <        }
1788 <    }
1885 >    }}
1886  
1887      public void testRunAfterBoth_sourceCancelled2() {
1888          for (ExecutionMode m : ExecutionMode.values())
1889          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1890 <        for (Integer v1 : new Integer[] { 1, null }) {
1891 <
1890 >        for (Integer v1 : new Integer[] { 1, null })
1891 >    {
1892          final CompletableFuture<Integer> f = new CompletableFuture<>();
1893          final CompletableFuture<Integer> g = new CompletableFuture<>();
1894          final Noop r = new Noop();
# Line 1803 | Line 1900 | public class CompletableFutureTest exten
1900  
1901          checkCompletedWithWrappedCancellationException(h);
1902          checkCancelled(g);
1903 <        assertEquals(r.invocationCount, 0);
1903 >        assertEquals(0, r.invocationCount);
1904          checkCompletedNormally(f, v1);
1905 <        }
1809 <    }
1905 >    }}
1906  
1907      public void testRunAfterBoth_sourceCancelled3() {
1908          for (ExecutionMode m : ExecutionMode.values())
1909          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1910 <        for (Integer v1 : new Integer[] { 1, null }) {
1911 <
1910 >        for (Integer v1 : new Integer[] { 1, null })
1911 >    {
1912          final CompletableFuture<Integer> f = new CompletableFuture<>();
1913          final CompletableFuture<Integer> g = new CompletableFuture<>();
1914          final Noop r = new Noop();
# Line 1823 | Line 1919 | public class CompletableFutureTest exten
1919  
1920          checkCompletedWithWrappedCancellationException(h);
1921          checkCancelled(g);
1922 <        assertEquals(r.invocationCount, 0);
1922 >        assertEquals(0, r.invocationCount);
1923          checkCompletedNormally(f, v1);
1924 <        }
1829 <    }
1924 >    }}
1925  
1926      public void testRunAfterBoth_sourceCancelled4() {
1927          for (ExecutionMode m : ExecutionMode.values())
1928          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1929 <        for (Integer v1 : new Integer[] { 1, null }) {
1930 <
1929 >        for (Integer v1 : new Integer[] { 1, null })
1930 >    {
1931          final CompletableFuture<Integer> f = new CompletableFuture<>();
1932          final CompletableFuture<Integer> g = new CompletableFuture<>();
1933          final Noop r = new Noop();
# Line 1843 | Line 1938 | public class CompletableFutureTest exten
1938  
1939          checkCompletedWithWrappedCancellationException(h);
1940          checkCancelled(f);
1941 <        assertEquals(r.invocationCount, 0);
1941 >        assertEquals(0, r.invocationCount);
1942          checkCompletedNormally(g, v1);
1943 <        }
1849 <    }
1943 >    }}
1944  
1945      /**
1946       * applyToEither result completes normally after normal completion
# Line 1855 | Line 1949 | public class CompletableFutureTest exten
1949      public void testApplyToEither_normalCompletion1() {
1950          for (ExecutionMode m : ExecutionMode.values())
1951          for (Integer v1 : new Integer[] { 1, null })
1952 <        for (Integer v2 : new Integer[] { 2, null }) {
1953 <
1952 >        for (Integer v2 : new Integer[] { 2, null })
1953 >    {
1954          final CompletableFuture<Integer> f = new CompletableFuture<>();
1955          final CompletableFuture<Integer> g = new CompletableFuture<>();
1956          final IncFunction r = new IncFunction();
# Line 1869 | Line 1963 | public class CompletableFutureTest exten
1963          checkCompletedNormally(f, v1);
1964          checkCompletedNormally(g, v2);
1965          checkCompletedNormally(h, inc(v1));
1966 <        }
1873 <    }
1966 >    }}
1967  
1968      public void testApplyToEither_normalCompletion2() {
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 IncFunction r = new IncFunction();
# Line 1889 | Line 1982 | public class CompletableFutureTest exten
1982          checkCompletedNormally(f, v1);
1983          checkCompletedNormally(g, v2);
1984          checkCompletedNormally(h, inc(v2));
1985 <        }
1986 <    }
1985 >        }}
1986 >
1987      public void testApplyToEither_normalCompletion3() {
1988          for (ExecutionMode m : ExecutionMode.values())
1989          for (Integer v1 : new Integer[] { 1, null })
1990 <        for (Integer v2 : new Integer[] { 2, null }) {
1991 <
1990 >        for (Integer v2 : new Integer[] { 2, null })
1991 >    {
1992          final CompletableFuture<Integer> f = new CompletableFuture<>();
1993          final CompletableFuture<Integer> g = new CompletableFuture<>();
1994          final IncFunction r = new IncFunction();
# Line 1910 | Line 2003 | public class CompletableFutureTest exten
2003          // unspecified behavior
2004          assertTrue(Objects.equals(h.join(), inc(v1)) ||
2005                     Objects.equals(h.join(), inc(v2)));
2006 <        assertEquals(r.invocationCount, 1);
2007 <        }
1915 <    }
2006 >        assertEquals(1, r.invocationCount);
2007 >    }}
2008  
2009      /**
2010       * applyToEither result completes exceptionally after exceptional
# Line 1920 | Line 2012 | public class CompletableFutureTest exten
2012       */
2013      public void testApplyToEither_exceptionalCompletion1() {
2014          for (ExecutionMode m : ExecutionMode.values())
2015 <        for (Integer v1 : new Integer[] { 1, null }) {
2016 <
2015 >        for (Integer v1 : new Integer[] { 1, null })
2016 >    {
2017          final CompletableFuture<Integer> f = new CompletableFuture<>();
2018          final CompletableFuture<Integer> g = new CompletableFuture<>();
2019          final IncFunction r = new IncFunction();
# Line 1932 | Line 2024 | public class CompletableFutureTest exten
2024          checkCompletedWithWrappedCFException(h, ex);
2025          g.complete(v1);
2026  
2027 <        assertEquals(r.invocationCount, 0);
2027 >        assertEquals(0, r.invocationCount);
2028          checkCompletedNormally(g, v1);
2029          checkCompletedWithWrappedCFException(f, ex);
2030          checkCompletedWithWrappedCFException(h, ex);
2031 <        }
1940 <    }
2031 >    }}
2032  
2033      public void testApplyToEither_exceptionalCompletion2() {
2034          for (ExecutionMode m : ExecutionMode.values())
2035 <        for (Integer v1 : new Integer[] { 1, null }) {
2036 <
2035 >        for (Integer v1 : new Integer[] { 1, null })
2036 >    {
2037          final CompletableFuture<Integer> f = new CompletableFuture<>();
2038          final CompletableFuture<Integer> g = new CompletableFuture<>();
2039          final IncFunction r = new IncFunction();
# Line 1953 | Line 2044 | public class CompletableFutureTest exten
2044          checkCompletedWithWrappedCFException(h, ex);
2045          f.complete(v1);
2046  
2047 <        assertEquals(r.invocationCount, 0);
2047 >        assertEquals(0, r.invocationCount);
2048          checkCompletedNormally(f, v1);
2049          checkCompletedWithWrappedCFException(g, ex);
2050          checkCompletedWithWrappedCFException(h, ex);
2051 <        }
1961 <    }
2051 >    }}
2052  
2053      public void testApplyToEither_exceptionalCompletion3() {
2054          for (ExecutionMode m : ExecutionMode.values())
2055 <        for (Integer v1 : new Integer[] { 1, null }) {
2056 <
2055 >        for (Integer v1 : new Integer[] { 1, null })
2056 >    {
2057          final CompletableFuture<Integer> f = new CompletableFuture<>();
2058          final CompletableFuture<Integer> g = new CompletableFuture<>();
2059          final IncFunction r = new IncFunction();
# Line 1976 | Line 2066 | public class CompletableFutureTest exten
2066          // unspecified behavior
2067          Integer v;
2068          try {
2069 <            assertEquals(h.join(), inc(v1));
2070 <            assertEquals(r.invocationCount, 1);
2069 >            assertEquals(inc(v1), h.join());
2070 >            assertEquals(1, r.invocationCount);
2071          } catch (CompletionException ok) {
2072              checkCompletedWithWrappedCFException(h, ex);
2073 <            assertEquals(r.invocationCount, 0);
2073 >            assertEquals(0, r.invocationCount);
2074          }
2075  
2076          checkCompletedWithWrappedCFException(g, ex);
2077          checkCompletedNormally(f, v1);
2078 <        }
1989 <    }
2078 >    }}
2079  
2080      public void testApplyToEither_exceptionalCompletion4() {
2081          for (ExecutionMode m : ExecutionMode.values())
2082 <        for (Integer v1 : new Integer[] { 1, null }) {
2083 <
2082 >        for (Integer v1 : new Integer[] { 1, null })
2083 >    {
2084          final CompletableFuture<Integer> f = new CompletableFuture<>();
2085          final CompletableFuture<Integer> g = new CompletableFuture<>();
2086          final IncFunction r = new IncFunction();
# Line 2004 | Line 2093 | public class CompletableFutureTest exten
2093          // unspecified behavior
2094          Integer v;
2095          try {
2096 <            assertEquals(h.join(), inc(v1));
2097 <            assertEquals(r.invocationCount, 1);
2096 >            assertEquals(inc(v1), h.join());
2097 >            assertEquals(1, r.invocationCount);
2098          } catch (CompletionException ok) {
2099              checkCompletedWithWrappedCFException(h, ex);
2100 <            assertEquals(r.invocationCount, 0);
2100 >            assertEquals(0, r.invocationCount);
2101          }
2102  
2103          checkCompletedWithWrappedCFException(f, ex);
2104          checkCompletedNormally(g, v1);
2105 <        }
2017 <    }
2105 >    }}
2106  
2107      /**
2108       * applyToEither result completes exceptionally if action does
# Line 2022 | Line 2110 | public class CompletableFutureTest exten
2110      public void testApplyToEither_actionFailed1() {
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 FailingFunction r = new FailingFunction();
# Line 2034 | Line 2122 | public class CompletableFutureTest exten
2122          g.complete(v2);
2123          checkCompletedNormally(f, v1);
2124          checkCompletedNormally(g, v2);
2125 <        }
2038 <    }
2125 >    }}
2126  
2127      public void testApplyToEither_actionFailed2() {
2128          for (ExecutionMode m : ExecutionMode.values())
2129          for (Integer v1 : new Integer[] { 1, null })
2130 <        for (Integer v2 : new Integer[] { 2, null }) {
2131 <
2130 >        for (Integer v2 : new Integer[] { 2, null })
2131 >    {
2132          final CompletableFuture<Integer> f = new CompletableFuture<>();
2133          final CompletableFuture<Integer> g = new CompletableFuture<>();
2134          final FailingFunction r = new FailingFunction();
# Line 2052 | Line 2139 | public class CompletableFutureTest exten
2139          f.complete(v1);
2140          checkCompletedNormally(f, v1);
2141          checkCompletedNormally(g, v2);
2142 <        }
2056 <    }
2142 >    }}
2143  
2144      /**
2145       * applyToEither result completes exceptionally if either source cancelled
# Line 2061 | Line 2147 | public class CompletableFutureTest exten
2147      public void testApplyToEither_sourceCancelled1() {
2148          for (ExecutionMode m : ExecutionMode.values())
2149          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2150 <        for (Integer v1 : new Integer[] { 1, null }) {
2151 <
2150 >        for (Integer v1 : new Integer[] { 1, null })
2151 >    {
2152          final CompletableFuture<Integer> f = new CompletableFuture<>();
2153          final CompletableFuture<Integer> g = new CompletableFuture<>();
2154          final IncFunction r = new IncFunction();
# Line 2073 | Line 2159 | public class CompletableFutureTest exten
2159          g.complete(v1);
2160  
2161          checkCancelled(f);
2162 <        assertEquals(r.invocationCount, 0);
2162 >        assertEquals(0, r.invocationCount);
2163          checkCompletedNormally(g, v1);
2164          checkCompletedWithWrappedCancellationException(h);
2165 <        }
2080 <    }
2165 >    }}
2166  
2167      public void testApplyToEither_sourceCancelled2() {
2168          for (ExecutionMode m : ExecutionMode.values())
2169          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2170 <        for (Integer v1 : new Integer[] { 1, null }) {
2171 <
2170 >        for (Integer v1 : new Integer[] { 1, null })
2171 >    {
2172          final CompletableFuture<Integer> f = new CompletableFuture<>();
2173          final CompletableFuture<Integer> g = new CompletableFuture<>();
2174          final IncFunction r = new IncFunction();
# Line 2094 | Line 2179 | public class CompletableFutureTest exten
2179          f.complete(v1);
2180  
2181          checkCancelled(g);
2182 <        assertEquals(r.invocationCount, 0);
2182 >        assertEquals(0, r.invocationCount);
2183          checkCompletedNormally(f, v1);
2184          checkCompletedWithWrappedCancellationException(h);
2185 <        }
2101 <    }
2185 >    }}
2186  
2187      public void testApplyToEither_sourceCancelled3() {
2188          for (ExecutionMode m : ExecutionMode.values())
2189          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2190 <        for (Integer v1 : new Integer[] { 1, null }) {
2191 <
2190 >        for (Integer v1 : new Integer[] { 1, null })
2191 >    {
2192          final CompletableFuture<Integer> f = new CompletableFuture<>();
2193          final CompletableFuture<Integer> g = new CompletableFuture<>();
2194          final IncFunction r = new IncFunction();
# Line 2116 | Line 2200 | public class CompletableFutureTest exten
2200          // unspecified behavior
2201          Integer v;
2202          try {
2203 <            assertEquals(h.join(), inc(v1));
2204 <            assertEquals(r.invocationCount, 1);
2203 >            assertEquals(inc(v1), h.join());
2204 >            assertEquals(1, r.invocationCount);
2205          } catch (CompletionException ok) {
2206              checkCompletedWithWrappedCancellationException(h);
2207 <            assertEquals(r.invocationCount, 0);
2207 >            assertEquals(0, r.invocationCount);
2208          }
2209  
2210          checkCancelled(g);
2211          checkCompletedNormally(f, v1);
2212 <        }
2129 <    }
2212 >    }}
2213  
2214      public void testApplyToEither_sourceCancelled4() {
2215          for (ExecutionMode m : ExecutionMode.values())
2216          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2217 <        for (Integer v1 : new Integer[] { 1, null }) {
2218 <
2217 >        for (Integer v1 : new Integer[] { 1, null })
2218 >    {
2219          final CompletableFuture<Integer> f = new CompletableFuture<>();
2220          final CompletableFuture<Integer> g = new CompletableFuture<>();
2221          final IncFunction r = new IncFunction();
# Line 2144 | Line 2227 | public class CompletableFutureTest exten
2227          // unspecified behavior
2228          Integer v;
2229          try {
2230 <            assertEquals(h.join(), inc(v1));
2231 <            assertEquals(r.invocationCount, 1);
2230 >            assertEquals(inc(v1), h.join());
2231 >            assertEquals(1, r.invocationCount);
2232          } catch (CompletionException ok) {
2233              checkCompletedWithWrappedCancellationException(h);
2234 <            assertEquals(r.invocationCount, 0);
2234 >            assertEquals(0, r.invocationCount);
2235          }
2236  
2237          checkCancelled(f);
2238          checkCompletedNormally(g, v1);
2239 <        }
2157 <    }
2239 >    }}
2240  
2241      /**
2242       * acceptEither result completes normally after normal completion
# Line 2163 | Line 2245 | public class CompletableFutureTest exten
2245      public void testAcceptEither_normalCompletion1() {
2246          for (ExecutionMode m : ExecutionMode.values())
2247          for (Integer v1 : new Integer[] { 1, null })
2248 <        for (Integer v2 : new Integer[] { 2, null }) {
2249 <
2248 >        for (Integer v2 : new Integer[] { 2, null })
2249 >    {
2250          final CompletableFuture<Integer> f = new CompletableFuture<>();
2251          final CompletableFuture<Integer> g = new CompletableFuture<>();
2252          final IncAction r = new IncAction();
# Line 2172 | Line 2254 | public class CompletableFutureTest exten
2254  
2255          f.complete(v1);
2256          checkCompletedNormally(h, null);
2257 <        assertEquals(r.value, inc(v1));
2257 >        assertEquals(inc(v1), r.value);
2258          g.complete(v2);
2259  
2260          checkCompletedNormally(f, v1);
2261          checkCompletedNormally(g, v2);
2262          checkCompletedNormally(h, null);
2263 <        }
2182 <    }
2263 >    }}
2264  
2265      public void testAcceptEither_normalCompletion2() {
2266          for (ExecutionMode m : ExecutionMode.values())
2267          for (Integer v1 : new Integer[] { 1, null })
2268 <        for (Integer v2 : new Integer[] { 2, null }) {
2269 <
2268 >        for (Integer v2 : new Integer[] { 2, null })
2269 >    {
2270          final CompletableFuture<Integer> f = new CompletableFuture<>();
2271          final CompletableFuture<Integer> g = new CompletableFuture<>();
2272          final IncAction r = new IncAction();
# Line 2193 | Line 2274 | public class CompletableFutureTest exten
2274  
2275          g.complete(v2);
2276          checkCompletedNormally(h, null);
2277 <        assertEquals(r.value, inc(v2));
2277 >        assertEquals(inc(v2), r.value);
2278          f.complete(v1);
2279  
2280          checkCompletedNormally(f, v1);
2281          checkCompletedNormally(g, v2);
2282          checkCompletedNormally(h, null);
2283 <        }
2284 <    }
2283 >    }}
2284 >
2285      public void testAcceptEither_normalCompletion3() {
2286          for (ExecutionMode m : ExecutionMode.values())
2287          for (Integer v1 : new Integer[] { 1, null })
2288 <        for (Integer v2 : new Integer[] { 2, null }) {
2289 <
2288 >        for (Integer v2 : new Integer[] { 2, null })
2289 >    {
2290          final CompletableFuture<Integer> f = new CompletableFuture<>();
2291          final CompletableFuture<Integer> g = new CompletableFuture<>();
2292          final IncAction r = new IncAction();
# Line 2221 | Line 2302 | public class CompletableFutureTest exten
2302          // unspecified behavior
2303          assertTrue(Objects.equals(r.value, inc(v1)) ||
2304                     Objects.equals(r.value, inc(v2)));
2305 <        }
2225 <    }
2305 >    }}
2306  
2307      /**
2308       * acceptEither result completes exceptionally after exceptional
# Line 2230 | Line 2310 | public class CompletableFutureTest exten
2310       */
2311      public void testAcceptEither_exceptionalCompletion1() {
2312          for (ExecutionMode m : ExecutionMode.values())
2313 <        for (Integer v1 : new Integer[] { 1, null }) {
2314 <
2313 >        for (Integer v1 : new Integer[] { 1, null })
2314 >    {
2315          final CompletableFuture<Integer> f = new CompletableFuture<>();
2316          final CompletableFuture<Integer> g = new CompletableFuture<>();
2317          final IncAction r = new IncAction();
# Line 2242 | Line 2322 | public class CompletableFutureTest exten
2322          checkCompletedWithWrappedCFException(h, ex);
2323          g.complete(v1);
2324  
2325 <        assertEquals(r.invocationCount, 0);
2325 >        assertEquals(0, r.invocationCount);
2326          checkCompletedNormally(g, v1);
2327          checkCompletedWithWrappedCFException(f, ex);
2328          checkCompletedWithWrappedCFException(h, ex);
2329 <        }
2250 <    }
2329 >    }}
2330  
2331      public void testAcceptEither_exceptionalCompletion2() {
2332          for (ExecutionMode m : ExecutionMode.values())
2333 <        for (Integer v1 : new Integer[] { 1, null }) {
2334 <
2333 >        for (Integer v1 : new Integer[] { 1, null })
2334 >    {
2335          final CompletableFuture<Integer> f = new CompletableFuture<>();
2336          final CompletableFuture<Integer> g = new CompletableFuture<>();
2337          final IncAction r = new IncAction();
# Line 2263 | Line 2342 | public class CompletableFutureTest exten
2342          checkCompletedWithWrappedCFException(h, ex);
2343          f.complete(v1);
2344  
2345 <        assertEquals(r.invocationCount, 0);
2345 >        assertEquals(0, r.invocationCount);
2346          checkCompletedNormally(f, v1);
2347          checkCompletedWithWrappedCFException(g, ex);
2348          checkCompletedWithWrappedCFException(h, ex);
2349 <        }
2271 <    }
2349 >    }}
2350  
2351      public void testAcceptEither_exceptionalCompletion3() {
2352          for (ExecutionMode m : ExecutionMode.values())
2353 <        for (Integer v1 : new Integer[] { 1, null }) {
2354 <
2353 >        for (Integer v1 : new Integer[] { 1, null })
2354 >    {
2355          final CompletableFuture<Integer> f = new CompletableFuture<>();
2356          final CompletableFuture<Integer> g = new CompletableFuture<>();
2357          final IncAction r = new IncAction();
# Line 2286 | Line 2364 | public class CompletableFutureTest exten
2364          // unspecified behavior
2365          Integer v;
2366          try {
2367 <            assertEquals(h.join(), null);
2368 <            assertEquals(r.invocationCount, 1);
2367 >            assertNull(h.join());
2368 >            assertEquals(1, r.invocationCount);
2369              assertEquals(inc(v1), r.value);
2370          } catch (CompletionException ok) {
2371              checkCompletedWithWrappedCFException(h, ex);
2372 <            assertEquals(r.invocationCount, 0);
2372 >            assertEquals(0, r.invocationCount);
2373          }
2374  
2375          checkCompletedWithWrappedCFException(g, ex);
2376          checkCompletedNormally(f, v1);
2377 <        }
2300 <    }
2377 >    }}
2378  
2379      public void testAcceptEither_exceptionalCompletion4() {
2380          for (ExecutionMode m : ExecutionMode.values())
2381 <        for (Integer v1 : new Integer[] { 1, null }) {
2382 <
2381 >        for (Integer v1 : new Integer[] { 1, null })
2382 >    {
2383          final CompletableFuture<Integer> f = new CompletableFuture<>();
2384          final CompletableFuture<Integer> g = new CompletableFuture<>();
2385          final IncAction r = new IncAction();
# Line 2315 | Line 2392 | public class CompletableFutureTest exten
2392          // unspecified behavior
2393          Integer v;
2394          try {
2395 <            assertEquals(h.join(), null);
2396 <            assertEquals(r.invocationCount, 1);
2395 >            assertNull(h.join());
2396 >            assertEquals(1, r.invocationCount);
2397              assertEquals(inc(v1), r.value);
2398          } catch (CompletionException ok) {
2399              checkCompletedWithWrappedCFException(h, ex);
2400 <            assertEquals(r.invocationCount, 0);
2400 >            assertEquals(0, r.invocationCount);
2401          }
2402  
2403          checkCompletedWithWrappedCFException(f, ex);
2404          checkCompletedNormally(g, v1);
2405 <        }
2329 <    }
2405 >    }}
2406  
2407      /**
2408       * acceptEither result completes exceptionally if action does
# Line 2334 | Line 2410 | public class CompletableFutureTest exten
2410      public void testAcceptEither_actionFailed1() {
2411          for (ExecutionMode m : ExecutionMode.values())
2412          for (Integer v1 : new Integer[] { 1, null })
2413 <        for (Integer v2 : new Integer[] { 2, null }) {
2414 <
2413 >        for (Integer v2 : new Integer[] { 2, null })
2414 >    {
2415          final CompletableFuture<Integer> f = new CompletableFuture<>();
2416          final CompletableFuture<Integer> g = new CompletableFuture<>();
2417          final FailingConsumer r = new FailingConsumer();
# Line 2346 | Line 2422 | public class CompletableFutureTest exten
2422          g.complete(v2);
2423          checkCompletedNormally(f, v1);
2424          checkCompletedNormally(g, v2);
2425 <        }
2350 <    }
2425 >    }}
2426  
2427      public void testAcceptEither_actionFailed2() {
2428          for (ExecutionMode m : ExecutionMode.values())
2429          for (Integer v1 : new Integer[] { 1, null })
2430 <        for (Integer v2 : new Integer[] { 2, null }) {
2431 <
2430 >        for (Integer v2 : new Integer[] { 2, null })
2431 >    {
2432          final CompletableFuture<Integer> f = new CompletableFuture<>();
2433          final CompletableFuture<Integer> g = new CompletableFuture<>();
2434          final FailingConsumer r = new FailingConsumer();
# Line 2364 | Line 2439 | public class CompletableFutureTest exten
2439          f.complete(v1);
2440          checkCompletedNormally(f, v1);
2441          checkCompletedNormally(g, v2);
2442 <        }
2368 <    }
2442 >    }}
2443  
2444      /**
2445       * acceptEither result completes exceptionally if either source cancelled
# Line 2373 | Line 2447 | public class CompletableFutureTest exten
2447      public void testAcceptEither_sourceCancelled1() {
2448          for (ExecutionMode m : ExecutionMode.values())
2449          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2450 <        for (Integer v1 : new Integer[] { 1, null }) {
2451 <
2450 >        for (Integer v1 : new Integer[] { 1, null })
2451 >    {
2452          final CompletableFuture<Integer> f = new CompletableFuture<>();
2453          final CompletableFuture<Integer> g = new CompletableFuture<>();
2454          final IncAction r = new IncAction();
# Line 2385 | Line 2459 | public class CompletableFutureTest exten
2459          g.complete(v1);
2460  
2461          checkCancelled(f);
2462 <        assertEquals(r.invocationCount, 0);
2462 >        assertEquals(0, r.invocationCount);
2463          checkCompletedNormally(g, v1);
2464          checkCompletedWithWrappedCancellationException(h);
2465 <        }
2392 <    }
2465 >    }}
2466  
2467      public void testAcceptEither_sourceCancelled2() {
2468          for (ExecutionMode m : ExecutionMode.values())
2469          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2470 <        for (Integer v1 : new Integer[] { 1, null }) {
2471 <
2470 >        for (Integer v1 : new Integer[] { 1, null })
2471 >    {
2472          final CompletableFuture<Integer> f = new CompletableFuture<>();
2473          final CompletableFuture<Integer> g = new CompletableFuture<>();
2474          final IncAction r = new IncAction();
# Line 2406 | Line 2479 | public class CompletableFutureTest exten
2479          f.complete(v1);
2480  
2481          checkCancelled(g);
2482 <        assertEquals(r.invocationCount, 0);
2482 >        assertEquals(0, r.invocationCount);
2483          checkCompletedNormally(f, v1);
2484          checkCompletedWithWrappedCancellationException(h);
2485 <        }
2413 <    }
2485 >    }}
2486  
2487      public void testAcceptEither_sourceCancelled3() {
2488          for (ExecutionMode m : ExecutionMode.values())
2489          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2490 <        for (Integer v1 : new Integer[] { 1, null }) {
2491 <
2490 >        for (Integer v1 : new Integer[] { 1, null })
2491 >    {
2492          final CompletableFuture<Integer> f = new CompletableFuture<>();
2493          final CompletableFuture<Integer> g = new CompletableFuture<>();
2494          final IncAction r = new IncAction();
# Line 2428 | Line 2500 | public class CompletableFutureTest exten
2500          // unspecified behavior
2501          Integer v;
2502          try {
2503 <            assertEquals(h.join(), null);
2504 <            assertEquals(r.invocationCount, 1);
2503 >            assertNull(h.join());
2504 >            assertEquals(1, r.invocationCount);
2505              assertEquals(inc(v1), r.value);
2506          } catch (CompletionException ok) {
2507              checkCompletedWithWrappedCancellationException(h);
2508 <            assertEquals(r.invocationCount, 0);
2508 >            assertEquals(0, r.invocationCount);
2509          }
2510  
2511          checkCancelled(g);
2512          checkCompletedNormally(f, v1);
2513 <        }
2442 <    }
2513 >    }}
2514  
2515      public void testAcceptEither_sourceCancelled4() {
2516          for (ExecutionMode m : ExecutionMode.values())
2517          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2518 <        for (Integer v1 : new Integer[] { 1, null }) {
2519 <
2518 >        for (Integer v1 : new Integer[] { 1, null })
2519 >    {
2520          final CompletableFuture<Integer> f = new CompletableFuture<>();
2521          final CompletableFuture<Integer> g = new CompletableFuture<>();
2522          final IncAction r = new IncAction();
# Line 2457 | Line 2528 | public class CompletableFutureTest exten
2528          // unspecified behavior
2529          Integer v;
2530          try {
2531 <            assertEquals(h.join(), null);
2532 <            assertEquals(r.invocationCount, 1);
2531 >            assertNull(h.join());
2532 >            assertEquals(1, r.invocationCount);
2533              assertEquals(inc(v1), r.value);
2534          } catch (CompletionException ok) {
2535              checkCompletedWithWrappedCancellationException(h);
2536 <            assertEquals(r.invocationCount, 0);
2536 >            assertEquals(0, r.invocationCount);
2537          }
2538  
2539          checkCancelled(f);
2540          checkCompletedNormally(g, v1);
2541 <        }
2471 <    }
2541 >    }}
2542  
2543      /**
2544       * runAfterEither result completes normally after normal completion
# Line 2477 | Line 2547 | public class CompletableFutureTest exten
2547      public void testRunAfterEither_normalCompletion1() {
2548          for (ExecutionMode m : ExecutionMode.values())
2549          for (Integer v1 : new Integer[] { 1, null })
2550 <        for (Integer v2 : new Integer[] { 2, null }) {
2551 <
2550 >        for (Integer v2 : new Integer[] { 2, null })
2551 >    {
2552          final CompletableFuture<Integer> f = new CompletableFuture<>();
2553          final CompletableFuture<Integer> g = new CompletableFuture<>();
2554          final Noop r = new Noop();
# Line 2486 | Line 2556 | public class CompletableFutureTest exten
2556  
2557          f.complete(v1);
2558          checkCompletedNormally(h, null);
2559 <        assertEquals(r.invocationCount, 1);
2559 >        assertEquals(1, r.invocationCount);
2560          g.complete(v2);
2561  
2562          checkCompletedNormally(f, v1);
2563          checkCompletedNormally(g, v2);
2564          checkCompletedNormally(h, null);
2565 <        assertEquals(r.invocationCount, 1);
2566 <        }
2497 <    }
2565 >        assertEquals(1, r.invocationCount);
2566 >    }}
2567  
2568      public void testRunAfterEither_normalCompletion2() {
2569          for (ExecutionMode m : ExecutionMode.values())
2570          for (Integer v1 : new Integer[] { 1, null })
2571 <        for (Integer v2 : new Integer[] { 2, null }) {
2572 <
2571 >        for (Integer v2 : new Integer[] { 2, null })
2572 >    {
2573          final CompletableFuture<Integer> f = new CompletableFuture<>();
2574          final CompletableFuture<Integer> g = new CompletableFuture<>();
2575          final Noop r = new Noop();
# Line 2508 | Line 2577 | public class CompletableFutureTest exten
2577  
2578          g.complete(v2);
2579          checkCompletedNormally(h, null);
2580 <        assertEquals(r.invocationCount, 1);
2580 >        assertEquals(1, r.invocationCount);
2581          f.complete(v1);
2582  
2583          checkCompletedNormally(f, v1);
2584          checkCompletedNormally(g, v2);
2585          checkCompletedNormally(h, null);
2586 <        assertEquals(r.invocationCount, 1);
2587 <        }
2588 <    }
2586 >        assertEquals(1, r.invocationCount);
2587 >        }}
2588 >
2589      public void testRunAfterEither_normalCompletion3() {
2590          for (ExecutionMode m : ExecutionMode.values())
2591          for (Integer v1 : new Integer[] { 1, null })
2592 <        for (Integer v2 : new Integer[] { 2, null }) {
2593 <
2592 >        for (Integer v2 : new Integer[] { 2, null })
2593 >    {
2594          final CompletableFuture<Integer> f = new CompletableFuture<>();
2595          final CompletableFuture<Integer> g = new CompletableFuture<>();
2596          final Noop r = new Noop();
# Line 2533 | Line 2602 | public class CompletableFutureTest exten
2602          checkCompletedNormally(h, null);
2603          checkCompletedNormally(f, v1);
2604          checkCompletedNormally(g, v2);
2605 <        assertEquals(r.invocationCount, 1);
2606 <        }
2538 <    }
2605 >        assertEquals(1, r.invocationCount);
2606 >    }}
2607  
2608      /**
2609       * runAfterEither result completes exceptionally after exceptional
# Line 2543 | Line 2611 | public class CompletableFutureTest exten
2611       */
2612      public void testRunAfterEither_exceptionalCompletion1() {
2613          for (ExecutionMode m : ExecutionMode.values())
2614 <        for (Integer v1 : new Integer[] { 1, null }) {
2615 <
2614 >        for (Integer v1 : new Integer[] { 1, null })
2615 >    {
2616          final CompletableFuture<Integer> f = new CompletableFuture<>();
2617          final CompletableFuture<Integer> g = new CompletableFuture<>();
2618          final Noop r = new Noop();
# Line 2555 | Line 2623 | public class CompletableFutureTest exten
2623          checkCompletedWithWrappedCFException(h, ex);
2624          g.complete(v1);
2625  
2626 <        assertEquals(r.invocationCount, 0);
2626 >        assertEquals(0, r.invocationCount);
2627          checkCompletedNormally(g, v1);
2628          checkCompletedWithWrappedCFException(f, ex);
2629          checkCompletedWithWrappedCFException(h, ex);
2630 <        }
2563 <    }
2630 >    }}
2631  
2632      public void testRunAfterEither_exceptionalCompletion2() {
2633          for (ExecutionMode m : ExecutionMode.values())
2634 <        for (Integer v1 : new Integer[] { 1, null }) {
2635 <
2634 >        for (Integer v1 : new Integer[] { 1, null })
2635 >    {
2636          final CompletableFuture<Integer> f = new CompletableFuture<>();
2637          final CompletableFuture<Integer> g = new CompletableFuture<>();
2638          final Noop r = new Noop();
# Line 2576 | Line 2643 | public class CompletableFutureTest exten
2643          checkCompletedWithWrappedCFException(h, ex);
2644          f.complete(v1);
2645  
2646 <        assertEquals(r.invocationCount, 0);
2646 >        assertEquals(0, r.invocationCount);
2647          checkCompletedNormally(f, v1);
2648          checkCompletedWithWrappedCFException(g, ex);
2649          checkCompletedWithWrappedCFException(h, ex);
2650 <        }
2584 <    }
2650 >    }}
2651  
2652      public void testRunAfterEither_exceptionalCompletion3() {
2653          for (ExecutionMode m : ExecutionMode.values())
2654 <        for (Integer v1 : new Integer[] { 1, null }) {
2655 <
2654 >        for (Integer v1 : new Integer[] { 1, null })
2655 >    {
2656          final CompletableFuture<Integer> f = new CompletableFuture<>();
2657          final CompletableFuture<Integer> g = new CompletableFuture<>();
2658          final Noop r = new Noop();
# Line 2599 | Line 2665 | public class CompletableFutureTest exten
2665          // unspecified behavior
2666          Integer v;
2667          try {
2668 <            assertEquals(h.join(), null);
2669 <            assertEquals(r.invocationCount, 1);
2668 >            assertNull(h.join());
2669 >            assertEquals(1, r.invocationCount);
2670          } catch (CompletionException ok) {
2671              checkCompletedWithWrappedCFException(h, ex);
2672 <            assertEquals(r.invocationCount, 0);
2672 >            assertEquals(0, r.invocationCount);
2673          }
2674  
2675          checkCompletedWithWrappedCFException(g, ex);
2676          checkCompletedNormally(f, v1);
2677 <        }
2612 <    }
2677 >    }}
2678  
2679      public void testRunAfterEither_exceptionalCompletion4() {
2680          for (ExecutionMode m : ExecutionMode.values())
2681 <        for (Integer v1 : new Integer[] { 1, null }) {
2682 <
2681 >        for (Integer v1 : new Integer[] { 1, null })
2682 >    {
2683          final CompletableFuture<Integer> f = new CompletableFuture<>();
2684          final CompletableFuture<Integer> g = new CompletableFuture<>();
2685          final Noop r = new Noop();
# Line 2627 | Line 2692 | public class CompletableFutureTest exten
2692          // unspecified behavior
2693          Integer v;
2694          try {
2695 <            assertEquals(h.join(), null);
2696 <            assertEquals(r.invocationCount, 1);
2695 >            assertNull(h.join());
2696 >            assertEquals(1, r.invocationCount);
2697          } catch (CompletionException ok) {
2698              checkCompletedWithWrappedCFException(h, ex);
2699 <            assertEquals(r.invocationCount, 0);
2699 >            assertEquals(0, r.invocationCount);
2700          }
2701  
2702          checkCompletedWithWrappedCFException(f, ex);
2703          checkCompletedNormally(g, v1);
2704 <        }
2640 <    }
2704 >    }}
2705  
2706      /**
2707       * runAfterEither result completes exceptionally if action does
# Line 2645 | Line 2709 | public class CompletableFutureTest exten
2709      public void testRunAfterEither_actionFailed1() {
2710          for (ExecutionMode m : ExecutionMode.values())
2711          for (Integer v1 : new Integer[] { 1, null })
2712 <        for (Integer v2 : new Integer[] { 2, null }) {
2713 <
2712 >        for (Integer v2 : new Integer[] { 2, null })
2713 >    {
2714          final CompletableFuture<Integer> f = new CompletableFuture<>();
2715          final CompletableFuture<Integer> g = new CompletableFuture<>();
2716          final FailingNoop r = new FailingNoop();
# Line 2657 | Line 2721 | public class CompletableFutureTest exten
2721          g.complete(v2);
2722          checkCompletedNormally(f, v1);
2723          checkCompletedNormally(g, v2);
2724 <        }
2661 <    }
2724 >    }}
2725  
2726      public void testRunAfterEither_actionFailed2() {
2727          for (ExecutionMode m : ExecutionMode.values())
2728          for (Integer v1 : new Integer[] { 1, null })
2729 <        for (Integer v2 : new Integer[] { 2, null }) {
2730 <
2729 >        for (Integer v2 : new Integer[] { 2, null })
2730 >    {
2731          final CompletableFuture<Integer> f = new CompletableFuture<>();
2732          final CompletableFuture<Integer> g = new CompletableFuture<>();
2733          final FailingNoop r = new FailingNoop();
# Line 2675 | Line 2738 | public class CompletableFutureTest exten
2738          f.complete(v1);
2739          checkCompletedNormally(f, v1);
2740          checkCompletedNormally(g, v2);
2741 <        }
2679 <    }
2741 >    }}
2742  
2743      /**
2744       * runAfterEither result completes exceptionally if either source cancelled
# Line 2684 | Line 2746 | public class CompletableFutureTest exten
2746      public void testRunAfterEither_sourceCancelled1() {
2747          for (ExecutionMode m : ExecutionMode.values())
2748          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2749 <        for (Integer v1 : new Integer[] { 1, null }) {
2750 <
2749 >        for (Integer v1 : new Integer[] { 1, null })
2750 >    {
2751          final CompletableFuture<Integer> f = new CompletableFuture<>();
2752          final CompletableFuture<Integer> g = new CompletableFuture<>();
2753          final Noop r = new Noop();
# Line 2696 | Line 2758 | public class CompletableFutureTest exten
2758          g.complete(v1);
2759  
2760          checkCancelled(f);
2761 <        assertEquals(r.invocationCount, 0);
2761 >        assertEquals(0, r.invocationCount);
2762          checkCompletedNormally(g, v1);
2763          checkCompletedWithWrappedCancellationException(h);
2764 <        }
2703 <    }
2764 >    }}
2765  
2766      public void testRunAfterEither_sourceCancelled2() {
2767          for (ExecutionMode m : ExecutionMode.values())
2768          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2769 <        for (Integer v1 : new Integer[] { 1, null }) {
2770 <
2769 >        for (Integer v1 : new Integer[] { 1, null })
2770 >    {
2771          final CompletableFuture<Integer> f = new CompletableFuture<>();
2772          final CompletableFuture<Integer> g = new CompletableFuture<>();
2773          final Noop r = new Noop();
# Line 2717 | Line 2778 | public class CompletableFutureTest exten
2778          f.complete(v1);
2779  
2780          checkCancelled(g);
2781 <        assertEquals(r.invocationCount, 0);
2781 >        assertEquals(0, r.invocationCount);
2782          checkCompletedNormally(f, v1);
2783          checkCompletedWithWrappedCancellationException(h);
2784 <        }
2724 <    }
2784 >    }}
2785  
2786      public void testRunAfterEither_sourceCancelled3() {
2787          for (ExecutionMode m : ExecutionMode.values())
2788          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2789 <        for (Integer v1 : new Integer[] { 1, null }) {
2790 <
2789 >        for (Integer v1 : new Integer[] { 1, null })
2790 >    {
2791          final CompletableFuture<Integer> f = new CompletableFuture<>();
2792          final CompletableFuture<Integer> g = new CompletableFuture<>();
2793          final Noop r = new Noop();
# Line 2739 | Line 2799 | public class CompletableFutureTest exten
2799          // unspecified behavior
2800          Integer v;
2801          try {
2802 <            assertEquals(h.join(), null);
2803 <            assertEquals(r.invocationCount, 1);
2802 >            assertNull(h.join());
2803 >            assertEquals(1, r.invocationCount);
2804          } catch (CompletionException ok) {
2805              checkCompletedWithWrappedCancellationException(h);
2806 <            assertEquals(r.invocationCount, 0);
2806 >            assertEquals(0, r.invocationCount);
2807          }
2808  
2809          checkCancelled(g);
2810          checkCompletedNormally(f, v1);
2811 <        }
2752 <    }
2811 >    }}
2812  
2813      public void testRunAfterEither_sourceCancelled4() {
2814          for (ExecutionMode m : ExecutionMode.values())
2815          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2816 <        for (Integer v1 : new Integer[] { 1, null }) {
2817 <
2816 >        for (Integer v1 : new Integer[] { 1, null })
2817 >    {
2818          final CompletableFuture<Integer> f = new CompletableFuture<>();
2819          final CompletableFuture<Integer> g = new CompletableFuture<>();
2820          final Noop r = new Noop();
# Line 2767 | Line 2826 | public class CompletableFutureTest exten
2826          // unspecified behavior
2827          Integer v;
2828          try {
2829 <            assertEquals(h.join(), null);
2830 <            assertEquals(r.invocationCount, 1);
2829 >            assertNull(h.join());
2830 >            assertEquals(1, r.invocationCount);
2831          } catch (CompletionException ok) {
2832              checkCompletedWithWrappedCancellationException(h);
2833 <            assertEquals(r.invocationCount, 0);
2833 >            assertEquals(0, r.invocationCount);
2834          }
2835  
2836          checkCancelled(f);
2837          checkCompletedNormally(g, v1);
2838 <        }
2780 <    }
2838 >    }}
2839  
2840      /**
2841       * thenCompose result completes normally after normal completion of source
2842       */
2843 <    public void testThenCompose() {
2844 <        CompletableFuture<Integer> f, g;
2845 <        CompletableFutureInc r;
2846 <
2847 <        f = new CompletableFuture<>();
2848 <        g = f.thenCompose(r = new CompletableFutureInc());
2849 <        f.complete(one);
2850 <        checkCompletedNormally(g, two);
2851 <        assertTrue(r.ran);
2843 >    public void testThenCompose_normalCompletion1() {
2844 >        for (ExecutionMode m : ExecutionMode.values())
2845 >        for (Integer v1 : new Integer[] { 1, null })
2846 >    {
2847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2848 >        final CompletableFutureInc r = new CompletableFutureInc();
2849 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2850 >        f.complete(v1);
2851 >        checkCompletedNormally(g, inc(v1));
2852 >        checkCompletedNormally(f, v1);
2853 >        assertEquals(1, r.invocationCount);
2854 >    }}
2855  
2856 <        f = new CompletableFuture<>();
2857 <        f.complete(one);
2858 <        g = f.thenCompose(r = new CompletableFutureInc());
2859 <        checkCompletedNormally(g, two);
2860 <        assertTrue(r.ran);
2861 <    }
2856 >    public void testThenCompose_normalCompletion2() {
2857 >        for (ExecutionMode m : ExecutionMode.values())
2858 >        for (Integer v1 : new Integer[] { 1, null })
2859 >    {
2860 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2861 >        final CompletableFutureInc r = new CompletableFutureInc();
2862 >        f.complete(v1);
2863 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2864 >        checkCompletedNormally(g, inc(v1));
2865 >        checkCompletedNormally(f, v1);
2866 >        assertEquals(1, r.invocationCount);
2867 >    }}
2868  
2869      /**
2870       * thenCompose result completes exceptionally after exceptional
2871       * completion of source
2872       */
2873 <    public void testThenCompose2() {
2874 <        CompletableFuture<Integer> f, g;
2875 <        CompletableFutureInc r;
2876 <
2877 <        f = new CompletableFuture<>();
2878 <        g = f.thenCompose(r = new CompletableFutureInc());
2879 <        f.completeExceptionally(new CFException());
2880 <        checkCompletedWithWrappedCFException(g);
2873 >    public void testThenCompose_exceptionalCompletion1() {
2874 >        for (ExecutionMode m : ExecutionMode.values())
2875 >    {
2876 >        final CFException ex = new CFException();
2877 >        final CompletableFutureInc r = new CompletableFutureInc();
2878 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2879 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2880 >        f.completeExceptionally(ex);
2881 >        checkCompletedWithWrappedCFException(g, ex);
2882 >        checkCompletedWithWrappedCFException(f, ex);
2883 >    }}
2884  
2885 <        f = new CompletableFuture<>();
2886 <        f.completeExceptionally(new CFException());
2887 <        g = f.thenCompose(r = new CompletableFutureInc());
2888 <        checkCompletedWithWrappedCFException(g);
2889 <    }
2885 >    public void testThenCompose_exceptionalCompletion2() {
2886 >        for (ExecutionMode m : ExecutionMode.values())
2887 >    {
2888 >        final CFException ex = new CFException();
2889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2890 >        f.completeExceptionally(ex);
2891 >        final CompletableFutureInc r = new CompletableFutureInc();
2892 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2893 >        checkCompletedWithWrappedCFException(g, ex);
2894 >        checkCompletedWithWrappedCFException(f, ex);
2895 >    }}
2896  
2897      /**
2898       * thenCompose result completes exceptionally if action does
2899       */
2900 <    public void testThenCompose3() {
2901 <        CompletableFuture<Integer> f, g;
2902 <        FailingCompletableFutureFunction r;
2903 <
2904 <        f = new CompletableFuture<>();
2905 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2906 <        f.complete(one);
2900 >    public void testThenCompose_actionFailed1() {
2901 >        for (ExecutionMode m : ExecutionMode.values())
2902 >        for (Integer v1 : new Integer[] { 1, null })
2903 >    {
2904 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2905 >        final FailingCompletableFutureFunction r
2906 >            = new FailingCompletableFutureFunction();
2907 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2908 >        f.complete(v1);
2909          checkCompletedWithWrappedCFException(g);
2910 +        checkCompletedNormally(f, v1);
2911 +    }}
2912  
2913 <        f = new CompletableFuture<>();
2914 <        f.complete(one);
2915 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2913 >    public void testThenCompose_actionFailed2() {
2914 >        for (ExecutionMode m : ExecutionMode.values())
2915 >        for (Integer v1 : new Integer[] { 1, null })
2916 >    {
2917 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2918 >        f.complete(v1);
2919 >        final FailingCompletableFutureFunction r
2920 >            = new FailingCompletableFutureFunction();
2921 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2922          checkCompletedWithWrappedCFException(g);
2923 <    }
2923 >        checkCompletedNormally(f, v1);
2924 >    }}
2925  
2926      /**
2927       * thenCompose result completes exceptionally if source cancelled
2928       */
2929 <    public void testThenCompose4() {
2930 <        CompletableFuture<Integer> f, g;
2931 <        CompletableFutureInc r;
2932 <
2933 <        f = new CompletableFuture<>();
2934 <        g = f.thenCompose(r = new CompletableFutureInc());
2935 <        assertTrue(f.cancel(true));
2929 >    public void testThenCompose_sourceCancelled1() {
2930 >        for (ExecutionMode m : ExecutionMode.values())
2931 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2932 >    {
2933 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2934 >        final CompletableFutureInc r = new CompletableFutureInc();
2935 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2936 >        assertTrue(f.cancel(mayInterruptIfRunning));
2937          checkCompletedWithWrappedCancellationException(g);
2938 +        checkCancelled(f);
2939 +    }}
2940  
2941 <        f = new CompletableFuture<>();
2942 <        assertTrue(f.cancel(true));
2943 <        g = f.thenCompose(r = new CompletableFutureInc());
2941 >    public void testThenCompose_sourceCancelled2() {
2942 >        for (ExecutionMode m : ExecutionMode.values())
2943 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2944 >    {
2945 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2946 >        assertTrue(f.cancel(mayInterruptIfRunning));
2947 >        final CompletableFutureInc r = new CompletableFutureInc();
2948 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2949          checkCompletedWithWrappedCancellationException(g);
2950 <    }
2950 >        checkCancelled(f);
2951 >    }}
2952  
2953      // asyncs
2954  
# Line 3001 | Line 3097 | public class CompletableFutureTest exten
3097          checkCompletedWithWrappedCancellationException(g);
3098      }
3099  
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
3100      // async with explicit executors
3101  
3102      /**
# Line 3224 | Line 3244 | public class CompletableFutureTest exten
3244          checkCompletedWithWrappedCancellationException(g);
3245      }
3246  
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    }
3261
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));
3270        checkCompletedWithWrappedCancellationException(g);
3271    }
3272
3247      // other static methods
3248  
3249      /**
# Line 3468 | Line 3442 | public class CompletableFutureTest exten
3442       * whenComplete action executes on normal completion, propagating
3443       * source result.
3444       */
3445 <    public void testWhenComplete1() {
3446 <        final AtomicInteger a = new AtomicInteger();
3447 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3448 <        CompletableFuture<Integer> g =
3449 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3450 <        f.complete(three);
3451 <        checkCompletedNormally(f, three);
3452 <        checkCompletedNormally(g, three);
3453 <        assertEquals(a.get(), 1);
3454 <    }
3455 <
3456 <    /**
3457 <     * whenComplete action executes on exceptional completion, propagating
3458 <     * source result.
3459 <     */
3460 <    public void testWhenComplete2() {
3487 <        final AtomicInteger a = new AtomicInteger();
3488 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3489 <        CompletableFuture<Integer> g =
3490 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3491 <        f.completeExceptionally(new CFException());
3492 <        assertTrue(f.isCompletedExceptionally());
3493 <        assertTrue(g.isCompletedExceptionally());
3494 <        assertEquals(a.get(), 1);
3495 <    }
3496 <
3497 <    /**
3498 <     * If a whenComplete action throws an exception when triggered by
3499 <     * a normal completion, it completes exceptionally
3500 <     */
3501 <    public void testWhenComplete3() {
3502 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 <        CompletableFuture<Integer> g =
3504 <            f.whenComplete((Integer x, Throwable t) ->
3505 <                           { throw new CFException(); } );
3506 <        f.complete(three);
3507 <        checkCompletedNormally(f, three);
3508 <        assertTrue(g.isCompletedExceptionally());
3509 <        checkCompletedWithWrappedCFException(g);
3510 <    }
3511 <
3512 <    /**
3513 <     * whenCompleteAsync action executes on normal completion, propagating
3514 <     * source result.
3515 <     */
3516 <    public void testWhenCompleteAsync1() {
3517 <        final AtomicInteger a = new AtomicInteger();
3518 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3519 <        CompletableFuture<Integer> g =
3520 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3521 <        f.complete(three);
3522 <        checkCompletedNormally(f, three);
3523 <        checkCompletedNormally(g, three);
3524 <        assertEquals(a.get(), 1);
3525 <    }
3526 <
3527 <    /**
3528 <     * whenCompleteAsync action executes on exceptional completion, propagating
3529 <     * source result.
3530 <     */
3531 <    public void testWhenCompleteAsync2() {
3532 <        final AtomicInteger a = new AtomicInteger();
3533 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3534 <        CompletableFuture<Integer> g =
3535 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3536 <        f.completeExceptionally(new CFException());
3537 <        checkCompletedWithWrappedCFException(f);
3538 <        checkCompletedWithWrappedCFException(g);
3539 <    }
3445 >    public void testWhenComplete_normalCompletion1() {
3446 >        for (ExecutionMode m : ExecutionMode.values())
3447 >        for (boolean createIncomplete : new boolean[] { true, false })
3448 >        for (Integer v1 : new Integer[] { 1, null })
3449 >    {
3450 >        final AtomicInteger a = new AtomicInteger(0);
3451 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3452 >        if (!createIncomplete) f.complete(v1);
3453 >        final CompletableFuture<Integer> g = m.whenComplete
3454 >            (f,
3455 >             (Integer x, Throwable t) -> {
3456 >                threadAssertSame(x, v1);
3457 >                threadAssertNull(t);
3458 >                a.getAndIncrement();
3459 >            });
3460 >        if (createIncomplete) f.complete(v1);
3461  
3462 <    /**
3463 <     * If a whenCompleteAsync action throws an exception when
3464 <     * triggered by a normal completion, it completes exceptionally
3465 <     */
3545 <    public void testWhenCompleteAsync3() {
3546 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3547 <        CompletableFuture<Integer> g =
3548 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3549 <                           { throw new CFException(); } );
3550 <        f.complete(three);
3551 <        checkCompletedNormally(f, three);
3552 <        checkCompletedWithWrappedCFException(g);
3553 <    }
3462 >        checkCompletedNormally(g, v1);
3463 >        checkCompletedNormally(f, v1);
3464 >        assertEquals(1, a.get());
3465 >    }}
3466  
3467      /**
3468 <     * whenCompleteAsync action executes on normal completion, propagating
3468 >     * whenComplete action executes on exceptional completion, propagating
3469       * source result.
3470       */
3471 <    public void testWhenCompleteAsync1e() {
3472 <        final AtomicInteger a = new AtomicInteger();
3473 <        ThreadExecutor exec = new ThreadExecutor();
3474 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3475 <        CompletableFuture<Integer> g =
3476 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3477 <                                exec);
3478 <        f.complete(three);
3479 <        checkCompletedNormally(f, three);
3480 <        checkCompletedNormally(g, three);
3481 <        assertEquals(a.get(), 1);
3482 <    }
3471 >    public void testWhenComplete_exceptionalCompletion() {
3472 >        for (ExecutionMode m : ExecutionMode.values())
3473 >        for (boolean createIncomplete : new boolean[] { true, false })
3474 >        for (Integer v1 : new Integer[] { 1, null })
3475 >    {
3476 >        final AtomicInteger a = new AtomicInteger(0);
3477 >        final CFException ex = new CFException();
3478 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3479 >        if (!createIncomplete) f.completeExceptionally(ex);
3480 >        final CompletableFuture<Integer> g = m.whenComplete
3481 >            (f,
3482 >             (Integer x, Throwable t) -> {
3483 >                threadAssertNull(x);
3484 >                threadAssertSame(t, ex);
3485 >                a.getAndIncrement();
3486 >            });
3487 >        if (createIncomplete) f.completeExceptionally(ex);
3488 >        checkCompletedWithWrappedCFException(f, ex);
3489 >        checkCompletedWithWrappedCFException(g, ex);
3490 >        assertEquals(1, a.get());
3491 >    }}
3492  
3493      /**
3494 <     * whenCompleteAsync action executes on exceptional completion, propagating
3495 <     * source result.
3494 >     * whenComplete action executes on cancelled source, propagating
3495 >     * CancellationException.
3496       */
3497 <    public void testWhenCompleteAsync2e() {
3498 <        final AtomicInteger a = new AtomicInteger();
3499 <        ThreadExecutor exec = new ThreadExecutor();
3500 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3501 <        CompletableFuture<Integer> g =
3502 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3503 <                                exec);
3504 <        f.completeExceptionally(new CFException());
3505 <        checkCompletedWithWrappedCFException(f);
3506 <        checkCompletedWithWrappedCFException(g);
3507 <    }
3497 >    public void testWhenComplete_sourceCancelled() {
3498 >        for (ExecutionMode m : ExecutionMode.values())
3499 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3500 >        for (boolean createIncomplete : new boolean[] { true, false })
3501 >    {
3502 >        final AtomicInteger a = new AtomicInteger(0);
3503 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3504 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3505 >        final CompletableFuture<Integer> g = m.whenComplete
3506 >            (f,
3507 >             (Integer x, Throwable t) -> {
3508 >                threadAssertNull(x);
3509 >                threadAssertTrue(t instanceof CancellationException);
3510 >                a.getAndIncrement();
3511 >            });
3512 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3513  
3514 <    /**
3515 <     * If a whenCompleteAsync action throws an exception when triggered
3516 <     * by a normal completion, it completes exceptionally
3517 <     */
3518 <    public void testWhenCompleteAsync3e() {
3593 <        ThreadExecutor exec = new ThreadExecutor();
3594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3595 <        CompletableFuture<Integer> g =
3596 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3597 <                                { throw new CFException(); },
3598 <                                exec);
3599 <        f.complete(three);
3600 <        checkCompletedNormally(f, three);
3601 <        checkCompletedWithWrappedCFException(g);
3602 <    }
3514 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3515 >        checkCompletedWithWrappedCancellationException(g);
3516 >        checkCancelled(f);
3517 >        assertEquals(1, a.get());
3518 >    }}
3519  
3520      /**
3521 <     * handleAsync action completes normally with function value on
3522 <     * either normal or exceptional completion of source
3521 >     * If a whenComplete action throws an exception when triggered by
3522 >     * a normal completion, it completes exceptionally
3523       */
3524 <    public void testHandleAsync() {
3525 <        CompletableFuture<Integer> f, g;
3526 <        IntegerHandler r;
3527 <
3528 <        f = new CompletableFuture<>();
3529 <        g = f.handleAsync(r = new IntegerHandler());
3530 <        assertFalse(r.ran);
3531 <        f.completeExceptionally(new CFException());
3532 <        checkCompletedWithWrappedCFException(f);
3533 <        checkCompletedNormally(g, three);
3534 <        assertTrue(r.ran);
3535 <
3536 <        f = new CompletableFuture<>();
3537 <        g = f.handleAsync(r = new IntegerHandler());
3538 <        assertFalse(r.ran);
3539 <        f.completeExceptionally(new CFException());
3540 <        checkCompletedWithWrappedCFException(f);
3541 <        checkCompletedNormally(g, three);
3542 <        assertTrue(r.ran);
3543 <
3544 <        f = new CompletableFuture<>();
3545 <        g = f.handleAsync(r = new IntegerHandler());
3630 <        assertFalse(r.ran);
3631 <        f.complete(one);
3632 <        checkCompletedNormally(f, one);
3633 <        checkCompletedNormally(g, two);
3634 <        assertTrue(r.ran);
3635 <
3636 <        f = new CompletableFuture<>();
3637 <        g = f.handleAsync(r = new IntegerHandler());
3638 <        assertFalse(r.ran);
3639 <        f.complete(one);
3640 <        checkCompletedNormally(f, one);
3641 <        checkCompletedNormally(g, two);
3642 <        assertTrue(r.ran);
3643 <    }
3524 >    public void testWhenComplete_actionFailed() {
3525 >        for (boolean createIncomplete : new boolean[] { true, false })
3526 >        for (ExecutionMode m : ExecutionMode.values())
3527 >        for (Integer v1 : new Integer[] { 1, null })
3528 >    {
3529 >        final AtomicInteger a = new AtomicInteger(0);
3530 >        final CFException ex = new CFException();
3531 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3532 >        if (!createIncomplete) f.complete(v1);
3533 >        final CompletableFuture<Integer> g = m.whenComplete
3534 >            (f,
3535 >             (Integer x, Throwable t) -> {
3536 >                threadAssertSame(x, v1);
3537 >                threadAssertNull(t);
3538 >                a.getAndIncrement();
3539 >                throw ex;
3540 >            });
3541 >        if (createIncomplete) f.complete(v1);
3542 >        checkCompletedNormally(f, v1);
3543 >        checkCompletedWithWrappedCFException(g, ex);
3544 >        assertEquals(1, a.get());
3545 >    }}
3546  
3547      /**
3548 <     * handleAsync action with Executor completes normally with
3549 <     * function value on either normal or exceptional completion of
3550 <     * source
3548 >     * If a whenComplete action throws an exception when triggered by
3549 >     * a source completion that also throws an exception, the source
3550 >     * exception takes precedence.
3551       */
3552 <    public void testHandleAsync2() {
3553 <        CompletableFuture<Integer> f, g;
3554 <        ThreadExecutor exec = new ThreadExecutor();
3555 <        IntegerHandler r;
3556 <
3557 <        f = new CompletableFuture<>();
3558 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3559 <        assertFalse(r.ran);
3560 <        f.completeExceptionally(new CFException());
3561 <        checkCompletedWithWrappedCFException(f);
3562 <        checkCompletedNormally(g, three);
3563 <        assertTrue(r.ran);
3564 <
3565 <        f = new CompletableFuture<>();
3566 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3567 <        assertFalse(r.ran);
3568 <        f.completeExceptionally(new CFException());
3569 <        checkCompletedWithWrappedCFException(f);
3570 <        checkCompletedNormally(g, three);
3571 <        assertTrue(r.ran);
3572 <
3573 <        f = new CompletableFuture<>();
3574 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3575 <        assertFalse(r.ran);
3576 <        f.complete(one);
3675 <        checkCompletedNormally(f, one);
3676 <        checkCompletedNormally(g, two);
3677 <        assertTrue(r.ran);
3678 <
3679 <        f = new CompletableFuture<>();
3680 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3681 <        assertFalse(r.ran);
3682 <        f.complete(one);
3683 <        checkCompletedNormally(f, one);
3684 <        checkCompletedNormally(g, two);
3685 <        assertTrue(r.ran);
3686 <    }
3552 >    public void testWhenComplete_actionFailedSourceFailed() {
3553 >        for (boolean createIncomplete : new boolean[] { true, false })
3554 >        for (ExecutionMode m : ExecutionMode.values())
3555 >        for (Integer v1 : new Integer[] { 1, null })
3556 >    {
3557 >        final AtomicInteger a = new AtomicInteger(0);
3558 >        final CFException ex1 = new CFException();
3559 >        final CFException ex2 = new CFException();
3560 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3561 >
3562 >        if (!createIncomplete) f.completeExceptionally(ex1);
3563 >        final CompletableFuture<Integer> g = m.whenComplete
3564 >            (f,
3565 >             (Integer x, Throwable t) -> {
3566 >                threadAssertSame(t, ex1);
3567 >                threadAssertNull(x);
3568 >                a.getAndIncrement();
3569 >                throw ex2;
3570 >            });
3571 >        if (createIncomplete) f.completeExceptionally(ex1);
3572 >
3573 >        checkCompletedWithWrappedCFException(f, ex1);
3574 >        checkCompletedWithWrappedCFException(g, ex1);
3575 >        assertEquals(1, a.get());
3576 >    }}
3577  
3578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines