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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC vs.
Revision 1.46 by jsr166, Mon Jun 2 17:41:22 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 }) {
# Line 954 | Line 1141 | public class CompletableFutureTest exten
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);
968 <        }
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);
1162 >        assertEquals(1, r.invocationCount);
1163          }
1164      }
1165  
# Line 1050 | Line 1183 | public class CompletableFutureTest exten
1183  
1184          checkCompletedWithWrappedCFException(h, ex);
1185          checkCompletedWithWrappedCFException(f, ex);
1186 <        assertEquals(r.invocationCount, 0);
1186 >        assertEquals(0, r.invocationCount);
1187          checkCompletedNormally(g, v1);
1188          }
1189      }
# Line 1071 | Line 1204 | public class CompletableFutureTest exten
1204  
1205          checkCompletedWithWrappedCFException(h, ex);
1206          checkCompletedWithWrappedCFException(g, ex);
1207 <        assertEquals(r.invocationCount, 0);
1207 >        assertEquals(0, r.invocationCount);
1208          checkCompletedNormally(f, v1);
1209          }
1210      }
# Line 1091 | Line 1224 | public class CompletableFutureTest exten
1224  
1225          checkCompletedWithWrappedCFException(h, ex);
1226          checkCompletedWithWrappedCFException(g, ex);
1227 <        assertEquals(r.invocationCount, 0);
1227 >        assertEquals(0, r.invocationCount);
1228          checkCompletedNormally(f, v1);
1229          }
1230      }
# Line 1111 | Line 1244 | public class CompletableFutureTest exten
1244  
1245          checkCompletedWithWrappedCFException(h, ex);
1246          checkCompletedWithWrappedCFException(f, ex);
1247 <        assertEquals(r.invocationCount, 0);
1247 >        assertEquals(0, r.invocationCount);
1248          checkCompletedNormally(g, v1);
1249          }
1250      }
# Line 1178 | Line 1311 | public class CompletableFutureTest exten
1311  
1312          checkCompletedWithWrappedCancellationException(h);
1313          checkCancelled(f);
1314 <        assertEquals(r.invocationCount, 0);
1314 >        assertEquals(0, r.invocationCount);
1315          checkCompletedNormally(g, v1);
1316          }
1317      }
# Line 1199 | Line 1332 | public class CompletableFutureTest exten
1332  
1333          checkCompletedWithWrappedCancellationException(h);
1334          checkCancelled(g);
1335 <        assertEquals(r.invocationCount, 0);
1335 >        assertEquals(0, r.invocationCount);
1336          checkCompletedNormally(f, v1);
1337          }
1338      }
# Line 1219 | Line 1352 | public class CompletableFutureTest exten
1352  
1353          checkCompletedWithWrappedCancellationException(h);
1354          checkCancelled(g);
1355 <        assertEquals(r.invocationCount, 0);
1355 >        assertEquals(0, r.invocationCount);
1356          checkCompletedNormally(f, v1);
1357          }
1358      }
# Line 1239 | Line 1372 | public class CompletableFutureTest exten
1372  
1373          checkCompletedWithWrappedCancellationException(h);
1374          checkCancelled(f);
1375 <        assertEquals(r.invocationCount, 0);
1375 >        assertEquals(0, r.invocationCount);
1376          checkCompletedNormally(g, v1);
1377          }
1378      }
# Line 1260 | Line 1393 | public class CompletableFutureTest exten
1393  
1394          f.complete(v1);
1395          checkIncomplete(h);
1396 <        assertFalse(r.ran());
1396 >        assertEquals(0, r.invocationCount);
1397          g.complete(v2);
1398  
1399          checkCompletedNormally(h, null);
1400 <        assertEquals(r.value, subtract(v1, v2));
1400 >        assertEquals(subtract(v1, v2), r.value);
1401          checkCompletedNormally(f, v1);
1402          checkCompletedNormally(g, v2);
1403          }
# Line 1282 | Line 1415 | public class CompletableFutureTest exten
1415  
1416          g.complete(v2);
1417          checkIncomplete(h);
1418 <        assertFalse(r.ran());
1418 >        assertEquals(0, r.invocationCount);
1419          f.complete(v1);
1420  
1421          checkCompletedNormally(h, null);
1422 <        assertEquals(r.value, subtract(v1, v2));
1422 >        assertEquals(subtract(v1, v2), r.value);
1423          checkCompletedNormally(f, v1);
1424          checkCompletedNormally(g, v2);
1425          }
# Line 1306 | Line 1439 | public class CompletableFutureTest exten
1439          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1440  
1441          checkCompletedNormally(h, null);
1442 <        assertEquals(r.value, subtract(v1, v2));
1442 >        assertEquals(subtract(v1, v2), r.value);
1443          checkCompletedNormally(f, v1);
1444          checkCompletedNormally(g, v2);
1445          }
# Line 1326 | Line 1459 | public class CompletableFutureTest exten
1459          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1460  
1461          checkCompletedNormally(h, null);
1462 <        assertEquals(r.value, subtract(v1, v2));
1462 >        assertEquals(subtract(v1, v2), r.value);
1463          checkCompletedNormally(f, v1);
1464          checkCompletedNormally(g, v2);
1465          }
# Line 1352 | Line 1485 | public class CompletableFutureTest exten
1485  
1486          checkCompletedWithWrappedCFException(h, ex);
1487          checkCompletedWithWrappedCFException(f, ex);
1488 <        assertEquals(r.invocationCount, 0);
1488 >        assertEquals(0, r.invocationCount);
1489          checkCompletedNormally(g, v1);
1490          }
1491      }
# Line 1373 | Line 1506 | public class CompletableFutureTest exten
1506  
1507          checkCompletedWithWrappedCFException(h, ex);
1508          checkCompletedWithWrappedCFException(g, ex);
1509 <        assertEquals(r.invocationCount, 0);
1509 >        assertEquals(0, r.invocationCount);
1510          checkCompletedNormally(f, v1);
1511          }
1512      }
# Line 1393 | Line 1526 | public class CompletableFutureTest exten
1526  
1527          checkCompletedWithWrappedCFException(h, ex);
1528          checkCompletedWithWrappedCFException(g, ex);
1529 <        assertEquals(r.invocationCount, 0);
1529 >        assertEquals(0, r.invocationCount);
1530          checkCompletedNormally(f, v1);
1531          }
1532      }
# Line 1413 | Line 1546 | public class CompletableFutureTest exten
1546  
1547          checkCompletedWithWrappedCFException(h, ex);
1548          checkCompletedWithWrappedCFException(f, ex);
1549 <        assertFalse(r.ran());
1549 >        assertEquals(0, r.invocationCount);
1550          checkCompletedNormally(g, v1);
1551          }
1552      }
# Line 1480 | Line 1613 | public class CompletableFutureTest exten
1613  
1614          checkCompletedWithWrappedCancellationException(h);
1615          checkCancelled(f);
1616 <        assertEquals(r.invocationCount, 0);
1616 >        assertEquals(0, r.invocationCount);
1617          checkCompletedNormally(g, v1);
1618          }
1619      }
# Line 1501 | Line 1634 | public class CompletableFutureTest exten
1634  
1635          checkCompletedWithWrappedCancellationException(h);
1636          checkCancelled(g);
1637 <        assertEquals(r.invocationCount, 0);
1637 >        assertEquals(0, r.invocationCount);
1638          checkCompletedNormally(f, v1);
1639          }
1640      }
# Line 1521 | Line 1654 | public class CompletableFutureTest exten
1654  
1655          checkCompletedWithWrappedCancellationException(h);
1656          checkCancelled(g);
1657 <        assertEquals(r.invocationCount, 0);
1657 >        assertEquals(0, r.invocationCount);
1658          checkCompletedNormally(f, v1);
1659          }
1660      }
# Line 1541 | Line 1674 | public class CompletableFutureTest exten
1674  
1675          checkCompletedWithWrappedCancellationException(h);
1676          checkCancelled(f);
1677 <        assertEquals(r.invocationCount, 0);
1677 >        assertEquals(0, r.invocationCount);
1678          checkCompletedNormally(g, v1);
1679          }
1680      }
# Line 1562 | Line 1695 | public class CompletableFutureTest exten
1695  
1696          f.complete(v1);
1697          checkIncomplete(h);
1698 <        assertFalse(r.ran);
1698 >        assertEquals(0, r.invocationCount);
1699          g.complete(v2);
1700  
1701          checkCompletedNormally(h, null);
1702 <        assertEquals(r.invocationCount, 1);
1702 >        assertEquals(1, r.invocationCount);
1703          checkCompletedNormally(f, v1);
1704          checkCompletedNormally(g, v2);
1705          }
# Line 1584 | Line 1717 | public class CompletableFutureTest exten
1717  
1718          g.complete(v2);
1719          checkIncomplete(h);
1720 <        assertFalse(r.ran);
1720 >        assertEquals(0, r.invocationCount);
1721          f.complete(v1);
1722  
1723          checkCompletedNormally(h, null);
1724 <        assertEquals(r.invocationCount, 1);
1724 >        assertEquals(1, r.invocationCount);
1725          checkCompletedNormally(f, v1);
1726          checkCompletedNormally(g, v2);
1727          }
# Line 1608 | Line 1741 | public class CompletableFutureTest exten
1741          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1742  
1743          checkCompletedNormally(h, null);
1744 <        assertTrue(r.ran);
1744 >        assertEquals(1, r.invocationCount);
1745          checkCompletedNormally(f, v1);
1746          checkCompletedNormally(g, v2);
1747          }
# Line 1628 | Line 1761 | public class CompletableFutureTest exten
1761          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1762  
1763          checkCompletedNormally(h, null);
1764 <        assertEquals(r.invocationCount, 1);
1764 >        assertEquals(1, r.invocationCount);
1765          checkCompletedNormally(f, v1);
1766          checkCompletedNormally(g, v2);
1767          }
# Line 1654 | Line 1787 | public class CompletableFutureTest exten
1787  
1788          checkCompletedWithWrappedCFException(h, ex);
1789          checkCompletedWithWrappedCFException(f, ex);
1790 <        assertEquals(r.invocationCount, 0);
1790 >        assertEquals(0, r.invocationCount);
1791          checkCompletedNormally(g, v1);
1792          }
1793      }
# Line 1675 | Line 1808 | public class CompletableFutureTest exten
1808  
1809          checkCompletedWithWrappedCFException(h, ex);
1810          checkCompletedWithWrappedCFException(g, ex);
1811 <        assertEquals(r.invocationCount, 0);
1811 >        assertEquals(0, r.invocationCount);
1812          checkCompletedNormally(f, v1);
1813          }
1814      }
# Line 1695 | Line 1828 | public class CompletableFutureTest exten
1828  
1829          checkCompletedWithWrappedCFException(h, ex);
1830          checkCompletedWithWrappedCFException(g, ex);
1831 <        assertEquals(r.invocationCount, 0);
1831 >        assertEquals(0, r.invocationCount);
1832          checkCompletedNormally(f, v1);
1833          }
1834      }
# Line 1715 | Line 1848 | public class CompletableFutureTest exten
1848  
1849          checkCompletedWithWrappedCFException(h, ex);
1850          checkCompletedWithWrappedCFException(f, ex);
1851 <        assertEquals(r.invocationCount, 0);
1851 >        assertEquals(0, r.invocationCount);
1852          checkCompletedNormally(g, v1);
1853          }
1854      }
# Line 1782 | Line 1915 | public class CompletableFutureTest exten
1915  
1916          checkCompletedWithWrappedCancellationException(h);
1917          checkCancelled(f);
1918 <        assertEquals(r.invocationCount, 0);
1918 >        assertEquals(0, r.invocationCount);
1919          checkCompletedNormally(g, v1);
1920          }
1921      }
# Line 1803 | Line 1936 | public class CompletableFutureTest exten
1936  
1937          checkCompletedWithWrappedCancellationException(h);
1938          checkCancelled(g);
1939 <        assertEquals(r.invocationCount, 0);
1939 >        assertEquals(0, r.invocationCount);
1940          checkCompletedNormally(f, v1);
1941          }
1942      }
# Line 1823 | Line 1956 | public class CompletableFutureTest exten
1956  
1957          checkCompletedWithWrappedCancellationException(h);
1958          checkCancelled(g);
1959 <        assertEquals(r.invocationCount, 0);
1959 >        assertEquals(0, r.invocationCount);
1960          checkCompletedNormally(f, v1);
1961          }
1962      }
# Line 1843 | Line 1976 | public class CompletableFutureTest exten
1976  
1977          checkCompletedWithWrappedCancellationException(h);
1978          checkCancelled(f);
1979 <        assertEquals(r.invocationCount, 0);
1979 >        assertEquals(0, r.invocationCount);
1980          checkCompletedNormally(g, v1);
1981          }
1982      }
# Line 1910 | Line 2043 | public class CompletableFutureTest exten
2043          // unspecified behavior
2044          assertTrue(Objects.equals(h.join(), inc(v1)) ||
2045                     Objects.equals(h.join(), inc(v2)));
2046 <        assertEquals(r.invocationCount, 1);
2046 >        assertEquals(1, r.invocationCount);
2047          }
2048      }
2049  
# Line 1932 | Line 2065 | public class CompletableFutureTest exten
2065          checkCompletedWithWrappedCFException(h, ex);
2066          g.complete(v1);
2067  
2068 <        assertEquals(r.invocationCount, 0);
2068 >        assertEquals(0, r.invocationCount);
2069          checkCompletedNormally(g, v1);
2070          checkCompletedWithWrappedCFException(f, ex);
2071          checkCompletedWithWrappedCFException(h, ex);
# Line 1953 | Line 2086 | public class CompletableFutureTest exten
2086          checkCompletedWithWrappedCFException(h, ex);
2087          f.complete(v1);
2088  
2089 <        assertEquals(r.invocationCount, 0);
2089 >        assertEquals(0, r.invocationCount);
2090          checkCompletedNormally(f, v1);
2091          checkCompletedWithWrappedCFException(g, ex);
2092          checkCompletedWithWrappedCFException(h, ex);
# Line 1976 | Line 2109 | public class CompletableFutureTest exten
2109          // unspecified behavior
2110          Integer v;
2111          try {
2112 <            assertEquals(h.join(), inc(v1));
2113 <            assertEquals(r.invocationCount, 1);
2112 >            assertEquals(inc(v1), h.join());
2113 >            assertEquals(1, r.invocationCount);
2114          } catch (CompletionException ok) {
2115              checkCompletedWithWrappedCFException(h, ex);
2116 <            assertEquals(r.invocationCount, 0);
2116 >            assertEquals(0, r.invocationCount);
2117          }
2118  
2119          checkCompletedWithWrappedCFException(g, ex);
# Line 2004 | Line 2137 | public class CompletableFutureTest exten
2137          // unspecified behavior
2138          Integer v;
2139          try {
2140 <            assertEquals(h.join(), inc(v1));
2141 <            assertEquals(r.invocationCount, 1);
2140 >            assertEquals(inc(v1), h.join());
2141 >            assertEquals(1, r.invocationCount);
2142          } catch (CompletionException ok) {
2143              checkCompletedWithWrappedCFException(h, ex);
2144 <            assertEquals(r.invocationCount, 0);
2144 >            assertEquals(0, r.invocationCount);
2145          }
2146  
2147          checkCompletedWithWrappedCFException(f, ex);
# Line 2073 | Line 2206 | public class CompletableFutureTest exten
2206          g.complete(v1);
2207  
2208          checkCancelled(f);
2209 <        assertEquals(r.invocationCount, 0);
2209 >        assertEquals(0, r.invocationCount);
2210          checkCompletedNormally(g, v1);
2211          checkCompletedWithWrappedCancellationException(h);
2212          }
# Line 2094 | Line 2227 | public class CompletableFutureTest exten
2227          f.complete(v1);
2228  
2229          checkCancelled(g);
2230 <        assertEquals(r.invocationCount, 0);
2230 >        assertEquals(0, r.invocationCount);
2231          checkCompletedNormally(f, v1);
2232          checkCompletedWithWrappedCancellationException(h);
2233          }
# Line 2116 | Line 2249 | public class CompletableFutureTest exten
2249          // unspecified behavior
2250          Integer v;
2251          try {
2252 <            assertEquals(h.join(), inc(v1));
2253 <            assertEquals(r.invocationCount, 1);
2252 >            assertEquals(inc(v1), h.join());
2253 >            assertEquals(1, r.invocationCount);
2254          } catch (CompletionException ok) {
2255              checkCompletedWithWrappedCancellationException(h);
2256 <            assertEquals(r.invocationCount, 0);
2256 >            assertEquals(0, r.invocationCount);
2257          }
2258  
2259          checkCancelled(g);
# Line 2144 | Line 2277 | public class CompletableFutureTest exten
2277          // unspecified behavior
2278          Integer v;
2279          try {
2280 <            assertEquals(h.join(), inc(v1));
2281 <            assertEquals(r.invocationCount, 1);
2280 >            assertEquals(inc(v1), h.join());
2281 >            assertEquals(1, r.invocationCount);
2282          } catch (CompletionException ok) {
2283              checkCompletedWithWrappedCancellationException(h);
2284 <            assertEquals(r.invocationCount, 0);
2284 >            assertEquals(0, r.invocationCount);
2285          }
2286  
2287          checkCancelled(f);
# Line 2172 | Line 2305 | public class CompletableFutureTest exten
2305  
2306          f.complete(v1);
2307          checkCompletedNormally(h, null);
2308 <        assertEquals(r.value, inc(v1));
2308 >        assertEquals(inc(v1), r.value);
2309          g.complete(v2);
2310  
2311          checkCompletedNormally(f, v1);
# Line 2193 | Line 2326 | public class CompletableFutureTest exten
2326  
2327          g.complete(v2);
2328          checkCompletedNormally(h, null);
2329 <        assertEquals(r.value, inc(v2));
2329 >        assertEquals(inc(v2), r.value);
2330          f.complete(v1);
2331  
2332          checkCompletedNormally(f, v1);
# Line 2242 | Line 2375 | public class CompletableFutureTest exten
2375          checkCompletedWithWrappedCFException(h, ex);
2376          g.complete(v1);
2377  
2378 <        assertEquals(r.invocationCount, 0);
2378 >        assertEquals(0, r.invocationCount);
2379          checkCompletedNormally(g, v1);
2380          checkCompletedWithWrappedCFException(f, ex);
2381          checkCompletedWithWrappedCFException(h, ex);
# Line 2263 | Line 2396 | public class CompletableFutureTest exten
2396          checkCompletedWithWrappedCFException(h, ex);
2397          f.complete(v1);
2398  
2399 <        assertEquals(r.invocationCount, 0);
2399 >        assertEquals(0, r.invocationCount);
2400          checkCompletedNormally(f, v1);
2401          checkCompletedWithWrappedCFException(g, ex);
2402          checkCompletedWithWrappedCFException(h, ex);
# Line 2286 | Line 2419 | public class CompletableFutureTest exten
2419          // unspecified behavior
2420          Integer v;
2421          try {
2422 <            assertEquals(h.join(), null);
2423 <            assertEquals(r.invocationCount, 1);
2422 >            assertNull(h.join());
2423 >            assertEquals(1, r.invocationCount);
2424              assertEquals(inc(v1), r.value);
2425          } catch (CompletionException ok) {
2426              checkCompletedWithWrappedCFException(h, ex);
2427 <            assertEquals(r.invocationCount, 0);
2427 >            assertEquals(0, r.invocationCount);
2428          }
2429  
2430          checkCompletedWithWrappedCFException(g, ex);
# Line 2315 | Line 2448 | public class CompletableFutureTest exten
2448          // unspecified behavior
2449          Integer v;
2450          try {
2451 <            assertEquals(h.join(), null);
2452 <            assertEquals(r.invocationCount, 1);
2451 >            assertNull(h.join());
2452 >            assertEquals(1, r.invocationCount);
2453              assertEquals(inc(v1), r.value);
2454          } catch (CompletionException ok) {
2455              checkCompletedWithWrappedCFException(h, ex);
2456 <            assertEquals(r.invocationCount, 0);
2456 >            assertEquals(0, r.invocationCount);
2457          }
2458  
2459          checkCompletedWithWrappedCFException(f, ex);
# Line 2385 | Line 2518 | public class CompletableFutureTest exten
2518          g.complete(v1);
2519  
2520          checkCancelled(f);
2521 <        assertEquals(r.invocationCount, 0);
2521 >        assertEquals(0, r.invocationCount);
2522          checkCompletedNormally(g, v1);
2523          checkCompletedWithWrappedCancellationException(h);
2524          }
# Line 2406 | Line 2539 | public class CompletableFutureTest exten
2539          f.complete(v1);
2540  
2541          checkCancelled(g);
2542 <        assertEquals(r.invocationCount, 0);
2542 >        assertEquals(0, r.invocationCount);
2543          checkCompletedNormally(f, v1);
2544          checkCompletedWithWrappedCancellationException(h);
2545          }
# Line 2428 | Line 2561 | public class CompletableFutureTest exten
2561          // unspecified behavior
2562          Integer v;
2563          try {
2564 <            assertEquals(h.join(), null);
2565 <            assertEquals(r.invocationCount, 1);
2564 >            assertNull(h.join());
2565 >            assertEquals(1, r.invocationCount);
2566              assertEquals(inc(v1), r.value);
2567          } catch (CompletionException ok) {
2568              checkCompletedWithWrappedCancellationException(h);
2569 <            assertEquals(r.invocationCount, 0);
2569 >            assertEquals(0, r.invocationCount);
2570          }
2571  
2572          checkCancelled(g);
# Line 2457 | Line 2590 | public class CompletableFutureTest exten
2590          // unspecified behavior
2591          Integer v;
2592          try {
2593 <            assertEquals(h.join(), null);
2594 <            assertEquals(r.invocationCount, 1);
2593 >            assertNull(h.join());
2594 >            assertEquals(1, r.invocationCount);
2595              assertEquals(inc(v1), r.value);
2596          } catch (CompletionException ok) {
2597              checkCompletedWithWrappedCancellationException(h);
2598 <            assertEquals(r.invocationCount, 0);
2598 >            assertEquals(0, r.invocationCount);
2599          }
2600  
2601          checkCancelled(f);
# Line 2486 | Line 2619 | public class CompletableFutureTest exten
2619  
2620          f.complete(v1);
2621          checkCompletedNormally(h, null);
2622 <        assertEquals(r.invocationCount, 1);
2622 >        assertEquals(1, r.invocationCount);
2623          g.complete(v2);
2624  
2625          checkCompletedNormally(f, v1);
2626          checkCompletedNormally(g, v2);
2627          checkCompletedNormally(h, null);
2628 <        assertEquals(r.invocationCount, 1);
2628 >        assertEquals(1, r.invocationCount);
2629          }
2630      }
2631  
# Line 2508 | Line 2641 | public class CompletableFutureTest exten
2641  
2642          g.complete(v2);
2643          checkCompletedNormally(h, null);
2644 <        assertEquals(r.invocationCount, 1);
2644 >        assertEquals(1, r.invocationCount);
2645          f.complete(v1);
2646  
2647          checkCompletedNormally(f, v1);
2648          checkCompletedNormally(g, v2);
2649          checkCompletedNormally(h, null);
2650 <        assertEquals(r.invocationCount, 1);
2650 >        assertEquals(1, r.invocationCount);
2651          }
2652      }
2653      public void testRunAfterEither_normalCompletion3() {
# Line 2533 | Line 2666 | public class CompletableFutureTest exten
2666          checkCompletedNormally(h, null);
2667          checkCompletedNormally(f, v1);
2668          checkCompletedNormally(g, v2);
2669 <        assertEquals(r.invocationCount, 1);
2669 >        assertEquals(1, r.invocationCount);
2670          }
2671      }
2672  
# Line 2555 | Line 2688 | public class CompletableFutureTest exten
2688          checkCompletedWithWrappedCFException(h, ex);
2689          g.complete(v1);
2690  
2691 <        assertEquals(r.invocationCount, 0);
2691 >        assertEquals(0, r.invocationCount);
2692          checkCompletedNormally(g, v1);
2693          checkCompletedWithWrappedCFException(f, ex);
2694          checkCompletedWithWrappedCFException(h, ex);
# Line 2576 | Line 2709 | public class CompletableFutureTest exten
2709          checkCompletedWithWrappedCFException(h, ex);
2710          f.complete(v1);
2711  
2712 <        assertEquals(r.invocationCount, 0);
2712 >        assertEquals(0, r.invocationCount);
2713          checkCompletedNormally(f, v1);
2714          checkCompletedWithWrappedCFException(g, ex);
2715          checkCompletedWithWrappedCFException(h, ex);
# Line 2599 | Line 2732 | public class CompletableFutureTest exten
2732          // unspecified behavior
2733          Integer v;
2734          try {
2735 <            assertEquals(h.join(), null);
2736 <            assertEquals(r.invocationCount, 1);
2735 >            assertNull(h.join());
2736 >            assertEquals(1, r.invocationCount);
2737          } catch (CompletionException ok) {
2738              checkCompletedWithWrappedCFException(h, ex);
2739 <            assertEquals(r.invocationCount, 0);
2739 >            assertEquals(0, r.invocationCount);
2740          }
2741  
2742          checkCompletedWithWrappedCFException(g, ex);
# Line 2627 | Line 2760 | public class CompletableFutureTest exten
2760          // unspecified behavior
2761          Integer v;
2762          try {
2763 <            assertEquals(h.join(), null);
2764 <            assertEquals(r.invocationCount, 1);
2763 >            assertNull(h.join());
2764 >            assertEquals(1, r.invocationCount);
2765          } catch (CompletionException ok) {
2766              checkCompletedWithWrappedCFException(h, ex);
2767 <            assertEquals(r.invocationCount, 0);
2767 >            assertEquals(0, r.invocationCount);
2768          }
2769  
2770          checkCompletedWithWrappedCFException(f, ex);
# Line 2696 | Line 2829 | public class CompletableFutureTest exten
2829          g.complete(v1);
2830  
2831          checkCancelled(f);
2832 <        assertEquals(r.invocationCount, 0);
2832 >        assertEquals(0, r.invocationCount);
2833          checkCompletedNormally(g, v1);
2834          checkCompletedWithWrappedCancellationException(h);
2835          }
# Line 2717 | Line 2850 | public class CompletableFutureTest exten
2850          f.complete(v1);
2851  
2852          checkCancelled(g);
2853 <        assertEquals(r.invocationCount, 0);
2853 >        assertEquals(0, r.invocationCount);
2854          checkCompletedNormally(f, v1);
2855          checkCompletedWithWrappedCancellationException(h);
2856          }
# Line 2739 | Line 2872 | public class CompletableFutureTest exten
2872          // unspecified behavior
2873          Integer v;
2874          try {
2875 <            assertEquals(h.join(), null);
2876 <            assertEquals(r.invocationCount, 1);
2875 >            assertNull(h.join());
2876 >            assertEquals(1, r.invocationCount);
2877          } catch (CompletionException ok) {
2878              checkCompletedWithWrappedCancellationException(h);
2879 <            assertEquals(r.invocationCount, 0);
2879 >            assertEquals(0, r.invocationCount);
2880          }
2881  
2882          checkCancelled(g);
# Line 2767 | Line 2900 | public class CompletableFutureTest exten
2900          // unspecified behavior
2901          Integer v;
2902          try {
2903 <            assertEquals(h.join(), null);
2904 <            assertEquals(r.invocationCount, 1);
2903 >            assertNull(h.join());
2904 >            assertEquals(1, r.invocationCount);
2905          } catch (CompletionException ok) {
2906              checkCompletedWithWrappedCancellationException(h);
2907 <            assertEquals(r.invocationCount, 0);
2907 >            assertEquals(0, r.invocationCount);
2908          }
2909  
2910          checkCancelled(f);
# Line 2782 | Line 2915 | public class CompletableFutureTest exten
2915      /**
2916       * thenCompose result completes normally after normal completion of source
2917       */
2918 <    public void testThenCompose() {
2919 <        CompletableFuture<Integer> f, g;
2920 <        CompletableFutureInc r;
2918 >    public void testThenCompose_normalCompletion1() {
2919 >        for (ExecutionMode m : ExecutionMode.values())
2920 >        for (Integer v1 : new Integer[] { 1, null }) {
2921  
2922 <        f = new CompletableFuture<>();
2923 <        g = f.thenCompose(r = new CompletableFutureInc());
2924 <        f.complete(one);
2925 <        checkCompletedNormally(g, two);
2926 <        assertTrue(r.ran);
2922 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2923 >        final CompletableFutureInc r = new CompletableFutureInc();
2924 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2925 >        f.complete(v1);
2926 >        checkCompletedNormally(g, inc(v1));
2927 >        checkCompletedNormally(f, v1);
2928 >        assertEquals(1, r.invocationCount);
2929 >        }
2930 >    }
2931  
2932 <        f = new CompletableFuture<>();
2933 <        f.complete(one);
2934 <        g = f.thenCompose(r = new CompletableFutureInc());
2935 <        checkCompletedNormally(g, two);
2936 <        assertTrue(r.ran);
2932 >    public void testThenCompose_normalCompletion2() {
2933 >        for (ExecutionMode m : ExecutionMode.values())
2934 >        for (Integer v1 : new Integer[] { 1, null }) {
2935 >
2936 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2937 >        final CompletableFutureInc r = new CompletableFutureInc();
2938 >        f.complete(v1);
2939 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2940 >        checkCompletedNormally(g, inc(v1));
2941 >        checkCompletedNormally(f, v1);
2942 >        assertEquals(1, r.invocationCount);
2943 >        }
2944      }
2945  
2946      /**
2947       * thenCompose result completes exceptionally after exceptional
2948       * completion of source
2949       */
2950 <    public void testThenCompose2() {
2951 <        CompletableFuture<Integer> f, g;
2808 <        CompletableFutureInc r;
2950 >    public void testThenCompose_exceptionalCompletion1() {
2951 >        for (ExecutionMode m : ExecutionMode.values()) {
2952  
2953 <        f = new CompletableFuture<>();
2954 <        g = f.thenCompose(r = new CompletableFutureInc());
2955 <        f.completeExceptionally(new CFException());
2956 <        checkCompletedWithWrappedCFException(g);
2953 >        final CFException ex = new CFException();
2954 >        final CompletableFutureInc r = new CompletableFutureInc();
2955 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2956 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2957 >        f.completeExceptionally(ex);
2958 >        checkCompletedWithWrappedCFException(g, ex);
2959 >        checkCompletedWithWrappedCFException(f, ex);
2960 >        }
2961 >    }
2962  
2963 <        f = new CompletableFuture<>();
2964 <        f.completeExceptionally(new CFException());
2965 <        g = f.thenCompose(r = new CompletableFutureInc());
2966 <        checkCompletedWithWrappedCFException(g);
2963 >    public void testThenCompose_exceptionalCompletion2() {
2964 >        for (ExecutionMode m : ExecutionMode.values()) {
2965 >
2966 >        final CFException ex = new CFException();
2967 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2968 >        f.completeExceptionally(ex);
2969 >        final CompletableFutureInc r = new CompletableFutureInc();
2970 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2971 >        checkCompletedWithWrappedCFException(g, ex);
2972 >        checkCompletedWithWrappedCFException(f, ex);
2973 >        }
2974      }
2975  
2976      /**
2977       * thenCompose result completes exceptionally if action does
2978       */
2979 <    public void testThenCompose3() {
2980 <        CompletableFuture<Integer> f, g;
2981 <        FailingCompletableFutureFunction r;
2979 >    public void testThenCompose_actionFailed1() {
2980 >        for (ExecutionMode m : ExecutionMode.values())
2981 >        for (Integer v1 : new Integer[] { 1, null }) {
2982  
2983 <        f = new CompletableFuture<>();
2984 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2985 <        f.complete(one);
2983 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2984 >        final FailingCompletableFutureFunction r
2985 >            = new FailingCompletableFutureFunction();
2986 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2987 >        f.complete(v1);
2988          checkCompletedWithWrappedCFException(g);
2989 +        checkCompletedNormally(f, v1);
2990 +        }
2991 +    }
2992  
2993 <        f = new CompletableFuture<>();
2994 <        f.complete(one);
2995 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2993 >    public void testThenCompose_actionFailed2() {
2994 >        for (ExecutionMode m : ExecutionMode.values())
2995 >        for (Integer v1 : new Integer[] { 1, null }) {
2996 >
2997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2998 >        f.complete(v1);
2999 >        final FailingCompletableFutureFunction r
3000 >            = new FailingCompletableFutureFunction();
3001 >        final CompletableFuture<Integer> g = f.thenCompose(r);
3002          checkCompletedWithWrappedCFException(g);
3003 +        checkCompletedNormally(f, v1);
3004 +        }
3005      }
3006  
3007      /**
3008       * thenCompose result completes exceptionally if source cancelled
3009       */
3010 <    public void testThenCompose4() {
3011 <        CompletableFuture<Integer> f, g;
3012 <        CompletableFutureInc r;
3010 >    public void testThenCompose_sourceCancelled1() {
3011 >        for (ExecutionMode m : ExecutionMode.values())
3012 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
3013  
3014 <        f = new CompletableFuture<>();
3015 <        g = f.thenCompose(r = new CompletableFutureInc());
3016 <        assertTrue(f.cancel(true));
3014 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3015 >        final CompletableFutureInc r = new CompletableFutureInc();
3016 >        final CompletableFuture<Integer> g = f.thenCompose(r);
3017 >        assertTrue(f.cancel(mayInterruptIfRunning));
3018          checkCompletedWithWrappedCancellationException(g);
3019 +        checkCancelled(f);
3020 +        }
3021 +    }
3022  
3023 <        f = new CompletableFuture<>();
3024 <        assertTrue(f.cancel(true));
3025 <        g = f.thenCompose(r = new CompletableFutureInc());
3023 >    public void testThenCompose_sourceCancelled2() {
3024 >        for (ExecutionMode m : ExecutionMode.values())
3025 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
3026 >
3027 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3028 >        assertTrue(f.cancel(mayInterruptIfRunning));
3029 >        final CompletableFutureInc r = new CompletableFutureInc();
3030 >        final CompletableFuture<Integer> g = f.thenCompose(r);
3031          checkCompletedWithWrappedCancellationException(g);
3032 +        checkCancelled(f);
3033 +        }
3034      }
3035  
3036      // asyncs
# Line 3001 | Line 3180 | public class CompletableFutureTest exten
3180          checkCompletedWithWrappedCancellationException(g);
3181      }
3182  
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
3183      // async with explicit executors
3184  
3185      /**
# Line 3224 | Line 3327 | public class CompletableFutureTest exten
3327          checkCompletedWithWrappedCancellationException(g);
3328      }
3329  
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
3330      // other static methods
3331  
3332      /**
# Line 3470 | Line 3527 | public class CompletableFutureTest exten
3527       */
3528      public void testWhenComplete_normalCompletion1() {
3529          for (ExecutionMode m : ExecutionMode.values())
3530 <        for (Integer v1 : new Integer[] { 1, null }) {
3531 <
3532 <        final AtomicInteger a = new AtomicInteger();
3530 >        for (boolean createIncomplete : new boolean[] { true, false })
3531 >        for (Integer v1 : new Integer[] { 1, null })
3532 >    {
3533 >        final AtomicInteger a = new AtomicInteger(0);
3534          final CompletableFuture<Integer> f = new CompletableFuture<>();
3535 <        final CompletableFuture<Integer> g =
3536 <            m.whenComplete(f,
3537 <                           (Integer x, Throwable t) -> {
3538 <                               threadAssertSame(x, v1);
3539 <                               threadAssertNull(t);
3540 <                               a.getAndIncrement();
3541 <                           });
3542 <        f.complete(v1);
3543 <        checkCompletedNormally(f, v1);
3535 >        if (!createIncomplete) f.complete(v1);
3536 >        final CompletableFuture<Integer> g = m.whenComplete
3537 >            (f,
3538 >             (Integer x, Throwable t) -> {
3539 >                threadAssertSame(x, v1);
3540 >                threadAssertNull(t);
3541 >                a.getAndIncrement();
3542 >            });
3543 >        if (createIncomplete) f.complete(v1);
3544 >
3545          checkCompletedNormally(g, v1);
3546 <        assertEquals(a.get(), 1);
3547 <        }
3548 <    }
3546 >        checkCompletedNormally(f, v1);
3547 >        assertEquals(1, a.get());
3548 >    }}
3549  
3550      /**
3551       * whenComplete action executes on exceptional completion, propagating
# Line 3494 | Line 3553 | public class CompletableFutureTest exten
3553       */
3554      public void testWhenComplete_exceptionalCompletion() {
3555          for (ExecutionMode m : ExecutionMode.values())
3556 <        for (Integer v1 : new Integer[] { 1, null }) {
3557 <
3558 <        final AtomicInteger a = new AtomicInteger();
3556 >        for (boolean createIncomplete : new boolean[] { true, false })
3557 >        for (Integer v1 : new Integer[] { 1, null })
3558 >    {
3559 >        final AtomicInteger a = new AtomicInteger(0);
3560          final CFException ex = new CFException();
3561          final CompletableFuture<Integer> f = new CompletableFuture<>();
3562 +        if (!createIncomplete) f.completeExceptionally(ex);
3563          final CompletableFuture<Integer> g = m.whenComplete
3564              (f,
3565               (Integer x, Throwable t) -> {
# Line 3506 | Line 3567 | public class CompletableFutureTest exten
3567                  threadAssertSame(t, ex);
3568                  a.getAndIncrement();
3569              });
3570 <        f.completeExceptionally(ex);
3570 >        if (createIncomplete) f.completeExceptionally(ex);
3571          checkCompletedWithWrappedCFException(f, ex);
3572          checkCompletedWithWrappedCFException(g, ex);
3573 <        assertEquals(a.get(), 1);
3574 <        }
3575 <    }
3573 >        assertEquals(1, a.get());
3574 >    }}
3575 >
3576 >    /**
3577 >     * whenComplete action executes on cancelled source, propagating
3578 >     * CancellationException.
3579 >     */
3580 >    public void testWhenComplete_sourceCancelled() {
3581 >        for (ExecutionMode m : ExecutionMode.values())
3582 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3583 >        for (boolean createIncomplete : new boolean[] { true, false })
3584 >    {
3585 >        final AtomicInteger a = new AtomicInteger(0);
3586 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3587 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3588 >        final CompletableFuture<Integer> g = m.whenComplete
3589 >            (f,
3590 >             (Integer x, Throwable t) -> {
3591 >                threadAssertNull(x);
3592 >                threadAssertTrue(t instanceof CancellationException);
3593 >                a.getAndIncrement();
3594 >            });
3595 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3596 >
3597 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3598 >        checkCompletedWithWrappedCancellationException(g);
3599 >        checkCancelled(f);
3600 >        assertEquals(1, a.get());
3601 >    }}
3602  
3603      /**
3604       * If a whenComplete action throws an exception when triggered by
3605       * a normal completion, it completes exceptionally
3606       */
3607      public void testWhenComplete_actionFailed() {
3608 +        for (boolean createIncomplete : new boolean[] { true, false })
3609          for (ExecutionMode m : ExecutionMode.values())
3610          for (Integer v1 : new Integer[] { 1, null }) {
3611  
3612 +        final AtomicInteger a = new AtomicInteger(0);
3613          final CFException ex = new CFException();
3614          final CompletableFuture<Integer> f = new CompletableFuture<>();
3615 +        if (!createIncomplete) f.complete(v1);
3616          final CompletableFuture<Integer> g = m.whenComplete
3617              (f,
3618               (Integer x, Throwable t) -> {
3619                  threadAssertSame(x, v1);
3620                  threadAssertNull(t);
3621 +                a.getAndIncrement();
3622                  throw ex;
3623              });
3624 <        f.complete(v1);
3624 >        if (createIncomplete) f.complete(v1);
3625          checkCompletedNormally(f, v1);
3626          checkCompletedWithWrappedCFException(g, ex);
3627 +        assertEquals(1, a.get());
3628          }
3629      }
3630  
# Line 3542 | Line 3634 | public class CompletableFutureTest exten
3634       * exception takes precedence.
3635       */
3636      public void testWhenComplete_actionFailedSourceFailed() {
3637 +        for (boolean createIncomplete : new boolean[] { true, false })
3638          for (ExecutionMode m : ExecutionMode.values())
3639          for (Integer v1 : new Integer[] { 1, null }) {
3640  
3641 +        final AtomicInteger a = new AtomicInteger(0);
3642          final CFException ex1 = new CFException();
3643          final CFException ex2 = new CFException();
3644          final CompletableFuture<Integer> f = new CompletableFuture<>();
3645 +
3646 +        if (!createIncomplete) f.completeExceptionally(ex1);
3647          final CompletableFuture<Integer> g = m.whenComplete
3648              (f,
3649               (Integer x, Throwable t) -> {
3650                  threadAssertSame(t, ex1);
3651                  threadAssertNull(x);
3652 +                a.getAndIncrement();
3653                  throw ex2;
3654              });
3655 <        f.completeExceptionally(ex1);
3655 >        if (createIncomplete) f.completeExceptionally(ex1);
3656 >
3657          checkCompletedWithWrappedCFException(f, ex1);
3658          checkCompletedWithWrappedCFException(g, ex1);
3659 +        assertEquals(1, a.get());
3660          }
3661      }
3662  
3564    /**
3565     * handleAsync action completes normally with function value on
3566     * either normal or exceptional completion of source
3567     */
3568    public void testHandleAsync() {
3569        CompletableFuture<Integer> f, g;
3570        IntegerHandler r;
3571
3572        f = new CompletableFuture<>();
3573        g = f.handleAsync(r = new IntegerHandler());
3574        assertFalse(r.ran);
3575        f.completeExceptionally(new CFException());
3576        checkCompletedWithWrappedCFException(f);
3577        checkCompletedNormally(g, three);
3578        assertTrue(r.ran);
3579
3580        f = new CompletableFuture<>();
3581        g = f.handleAsync(r = new IntegerHandler());
3582        assertFalse(r.ran);
3583        f.completeExceptionally(new CFException());
3584        checkCompletedWithWrappedCFException(f);
3585        checkCompletedNormally(g, three);
3586        assertTrue(r.ran);
3587
3588        f = new CompletableFuture<>();
3589        g = f.handleAsync(r = new IntegerHandler());
3590        assertFalse(r.ran);
3591        f.complete(one);
3592        checkCompletedNormally(f, one);
3593        checkCompletedNormally(g, two);
3594        assertTrue(r.ran);
3595
3596        f = new CompletableFuture<>();
3597        g = f.handleAsync(r = new IntegerHandler());
3598        assertFalse(r.ran);
3599        f.complete(one);
3600        checkCompletedNormally(f, one);
3601        checkCompletedNormally(g, two);
3602        assertTrue(r.ran);
3603    }
3604
3605    /**
3606     * handleAsync action with Executor completes normally with
3607     * function value on either normal or exceptional completion of
3608     * source
3609     */
3610    public void testHandleAsync2() {
3611        CompletableFuture<Integer> f, g;
3612        ThreadExecutor exec = new ThreadExecutor();
3613        IntegerHandler r;
3614
3615        f = new CompletableFuture<>();
3616        g = f.handleAsync(r = new IntegerHandler(), exec);
3617        assertFalse(r.ran);
3618        f.completeExceptionally(new CFException());
3619        checkCompletedWithWrappedCFException(f);
3620        checkCompletedNormally(g, three);
3621        assertTrue(r.ran);
3622
3623        f = new CompletableFuture<>();
3624        g = f.handleAsync(r = new IntegerHandler(), exec);
3625        assertFalse(r.ran);
3626        f.completeExceptionally(new CFException());
3627        checkCompletedWithWrappedCFException(f);
3628        checkCompletedNormally(g, three);
3629        assertTrue(r.ran);
3630
3631        f = new CompletableFuture<>();
3632        g = f.handleAsync(r = new IntegerHandler(), exec);
3633        assertFalse(r.ran);
3634        f.complete(one);
3635        checkCompletedNormally(f, one);
3636        checkCompletedNormally(g, two);
3637        assertTrue(r.ran);
3638
3639        f = new CompletableFuture<>();
3640        g = f.handleAsync(r = new IntegerHandler(), exec);
3641        assertFalse(r.ran);
3642        f.complete(one);
3643        checkCompletedNormally(f, one);
3644        checkCompletedNormally(g, two);
3645        assertTrue(r.ran);
3646    }
3647
3663   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines