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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.36 by jsr166, Sun Jun 1 23:12:45 2014 UTC vs.
Revision 1.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 318 | Line 320 | public class CompletableFutureTest exten
320  
321      // Choose non-commutative actions for better coverage
322  
323 <    // A non-commutative function that handles null values as well
324 <    public static int subtract(Integer x, Integer y) {
325 <        return ((x == null) ? 42 : x.intValue())
323 >    // A non-commutative function that handles and produces null values as well.
324 >    static Integer subtract(Integer x, Integer y) {
325 >        return (x == null && y == null) ? null :
326 >            ((x == null) ? 42 : x.intValue())
327              - ((y == null) ? 99 : y.intValue());
328      }
329  
330 +    // A function that handles and produces null values as well.
331 +    static Integer inc(Integer x) {
332 +        return (x == null) ? null : x + 1;
333 +    }
334 +
335      static final Supplier<Integer> supplyOne =
336          () -> Integer.valueOf(1);
337      static final Function<Integer, Integer> inc =
# Line 331 | Line 339 | public class CompletableFutureTest exten
339      static final BiFunction<Integer, Integer, Integer> subtract =
340          (Integer x, Integer y) -> subtract(x, y);
341      static final class IncAction implements Consumer<Integer> {
342 <        int value;
343 <        public void accept(Integer x) { value = x.intValue() + 1; }
342 >        int invocationCount = 0;
343 >        Integer value;
344 >        public void accept(Integer x) {
345 >            invocationCount++;
346 >            value = inc(x);
347 >        }
348 >    }
349 >    static final class IncFunction implements Function<Integer,Integer> {
350 >        int invocationCount = 0;
351 >        Integer value;
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356      }
357      static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 <        volatile int invocationCount = 0;
359 <        int value;
358 >        int invocationCount = 0;
359 >        Integer value;
360          // Check this action was invoked exactly once when result is computed.
341        public boolean ran() { return invocationCount == 1; }
361          public void accept(Integer x, Integer y) {
362              invocationCount++;
363              value = subtract(x, y);
364          }
365      }
366      static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
367 <        volatile int invocationCount = 0;
368 <        int value;
367 >        int invocationCount = 0;
368 >        Integer value;
369          // Check this action was invoked exactly once when result is computed.
351        public boolean ran() { return invocationCount == 1; }
370          public Integer apply(Integer x, Integer y) {
371              invocationCount++;
372 <            return subtract(x, y);
372 >            return value = subtract(x, y);
373          }
374      }
375      static final class Noop implements Runnable {
376 <        boolean ran;
377 <        public void run() { ran = true; }
376 >        int invocationCount = 0;
377 >        public void run() {
378 >            invocationCount++;
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 413 | Line 452 | public class CompletableFutureTest exten
452          }
453      }
454  
416    static final class ExceptionToInteger implements Function<Throwable, Integer> {
417        public Integer apply(Throwable x) { return Integer.valueOf(3); }
418    }
419
420    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
421        boolean ran;
422        public Integer apply(Integer x, Throwable t) {
423            ran = true;
424            return (t == null) ? two : three;
425        }
426    }
427
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 447 | 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> CompletableFuture<Void> runAfterEither
505 +                (CompletableFuture<T> f,
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,
512 +                 CompletionStage<? extends T> g,
513 +                 Consumer<? super T> a) {
514 +                return f.acceptEither(g, a);
515 +            }
516 +            public <T,U> CompletableFuture<U> applyToEither
517 +                (CompletableFuture<T> f,
518 +                 CompletionStage<? extends T> g,
519 +                 Function<? super T,U> a) {
520 +                return f.applyToEither(g, a);
521 +            }
522          },
523  
452 //             /** Experimental way to do more testing */
453 //         REVERSE_DEFAULT {
454 //             public <T,U> CompletableFuture<Void> runAfterBoth
455 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
456 //                 return g.runAfterBoth(f, a);
457 //             }
458 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
459 //                 (CompletableFuture<T> f,
460 //                  CompletionStage<? extends U> g,
461 //                  BiConsumer<? super T,? super U> a) {
462 //                 return DEFAULT.thenAcceptBoth(f, g, a);
463 //             }
464 //         },
465
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.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.whenCompleteAsync(a);
551 +            }
552              public <T,U> CompletableFuture<Void> runAfterBoth
553                  (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
554                  return f.runAfterBothAsync(g, a);
# Line 480 | 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> CompletableFuture<Void> runAfterEither
569 +                (CompletableFuture<T> f,
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,
576 +                 CompletionStage<? extends T> g,
577 +                 Consumer<? super T> a) {
578 +                return f.acceptEitherAsync(g, a);
579 +            }
580 +            public <T,U> CompletableFuture<U> applyToEither
581 +                (CompletableFuture<T> f,
582 +                 CompletionStage<? extends T> g,
583 +                 Function<? super T,U> a) {
584 +                return f.applyToEitherAsync(g, a);
585 +            }
586          },
587  
485 //         REVERSE_DEFAULT_ASYNC {
486 //             public <T,U> CompletableFuture<Void> runAfterBoth
487 //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
488 //                 return f.runAfterBothAsync(g, a);
489 //             }
490 //             public <T,U> CompletableFuture<Void> thenAcceptBoth
491 //                 (CompletableFuture<T> f,
492 //                  CompletionStage<? extends U> g,
493 //                  BiConsumer<? super T,? super U> a) {
494 //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
495 //             }
496 //         },
497
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, 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, new ThreadExecutor());
615 +            }
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 512 | Line 629 | public class CompletableFutureTest exten
629                   BiFunction<? super T,? super U,? extends V> a) {
630                  return f.thenCombineAsync(g, a, new ThreadExecutor());
631              }
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> CompletableFuture<Void> acceptEither
639 +                (CompletableFuture<T> f,
640 +                 CompletionStage<? extends T> g,
641 +                 Consumer<? super T> a) {
642 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
643 +            }
644 +            public <T,U> CompletableFuture<U> applyToEither
645 +                (CompletableFuture<T> f,
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 524 | Line 674 | public class CompletableFutureTest exten
674              (CompletableFuture<T> f,
675               CompletionStage<? extends U> g,
676               BiFunction<? super T,? super U,? extends V> a);
677 +        public abstract <T> CompletableFuture<Void> runAfterEither
678 +            (CompletableFuture<T> f,
679 +             CompletionStage<?> g,
680 +             java.lang.Runnable a);
681 +        public abstract <T> CompletableFuture<Void> acceptEither
682 +            (CompletableFuture<T> f,
683 +             CompletionStage<? extends T> g,
684 +             Consumer<? super T> a);
685 +        public abstract <T,U> CompletableFuture<U> applyToEither
686 +            (CompletableFuture<T> f,
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 +
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);
559 <        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);
572 <        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 586 | 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 598 | 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 610 | 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 640 | 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 657 | 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 679 | 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 776 | Line 1089 | public class CompletableFutureTest exten
1089          CompletableFuture<Void> g = f.thenAccept(r);
1090          f.complete(one);
1091          checkCompletedNormally(g, null);
1092 <        assertEquals(r.value, 2);
1092 >        assertEquals(r.value, (Integer) 2);
1093      }
1094  
1095      /**
# Line 800 | 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 819 | 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 826 | 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);
1145 <
831 <        f.complete(v1);
832 <        checkIncomplete(h);
833 <        assertFalse(r.ran());
834 <        g.complete(v2);
835 <
836 <        checkCompletedNormally(h, subtract(v1, v2));
837 <        checkCompletedNormally(f, v1);
838 <        checkCompletedNormally(g, v2);
839 <        }
840 <    }
841 <
842 <    public void testThenCombine_normalCompletion2() {
843 <        for (ExecutionMode m : ExecutionMode.values())
844 <        for (Integer v1 : new Integer[] { 1, null })
845 <        for (Integer v2 : new Integer[] { 2, null }) {
846 <
847 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
848 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
849 <        final SubtractFunction r = new SubtractFunction();
850 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
851 <
852 <        g.complete(v2);
853 <        checkIncomplete(h);
854 <        assertFalse(r.ran());
855 <        f.complete(v1);
856 <
857 <        checkCompletedNormally(h, subtract(v1, v2));
858 <        checkCompletedNormally(f, v1);
859 <        checkCompletedNormally(g, v2);
860 <        }
861 <    }
862 <
863 <    public void testThenCombine_normalCompletion3() {
864 <        for (ExecutionMode m : ExecutionMode.values())
865 <        for (Integer v1 : new Integer[] { 1, null })
866 <        for (Integer v2 : new Integer[] { 2, null }) {
867 <
868 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
869 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
870 <        final SubtractFunction r = new SubtractFunction();
871 <
872 <        g.complete(v2);
873 <        f.complete(v1);
874 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
875 <
876 <        checkCompletedNormally(h, subtract(v1, v2));
877 <        checkCompletedNormally(f, v1);
878 <        checkCompletedNormally(g, v2);
879 <        }
880 <    }
881 <
882 <    public void testThenCombine_normalCompletion4() {
883 <        for (ExecutionMode m : ExecutionMode.values())
884 <        for (Integer v1 : new Integer[] { 1, null })
885 <        for (Integer v2 : new Integer[] { 2, null }) {
886 <
887 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
888 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
889 <        final SubtractFunction r = new SubtractFunction();
1144 >        CompletableFuture<Integer> h = null;
1145 >        if (createIncomplete) h = m.thenCombine(f, g, r);
1146  
1147 <        f.complete(v1);
1148 <        g.complete(v2);
1149 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
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(1, r.invocationCount);
1163          }
1164      }
1165  
# Line 918 | Line 1183 | public class CompletableFutureTest exten
1183  
1184          checkCompletedWithWrappedCFException(h, ex);
1185          checkCompletedWithWrappedCFException(f, ex);
1186 <        assertFalse(r.ran());
1186 >        assertEquals(0, r.invocationCount);
1187          checkCompletedNormally(g, v1);
1188          }
1189      }
# Line 930 | Line 1195 | public class CompletableFutureTest exten
1195          final CompletableFuture<Integer> f = new CompletableFuture<>();
1196          final CompletableFuture<Integer> g = new CompletableFuture<>();
1197          final SubtractFunction r = new SubtractFunction();
1198 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1198 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1199          final CFException ex = new CFException();
1200  
1201          g.completeExceptionally(ex);
# Line 939 | Line 1204 | public class CompletableFutureTest exten
1204  
1205          checkCompletedWithWrappedCFException(h, ex);
1206          checkCompletedWithWrappedCFException(g, ex);
1207 <        assertFalse(r.ran());
1207 >        assertEquals(0, r.invocationCount);
1208          checkCompletedNormally(f, v1);
1209          }
1210      }
# Line 959 | Line 1224 | public class CompletableFutureTest exten
1224  
1225          checkCompletedWithWrappedCFException(h, ex);
1226          checkCompletedWithWrappedCFException(g, ex);
1227 <        assertFalse(r.ran());
1227 >        assertEquals(0, r.invocationCount);
1228          checkCompletedNormally(f, v1);
1229          }
1230      }
# Line 975 | Line 1240 | public class CompletableFutureTest exten
1240  
1241          f.completeExceptionally(ex);
1242          g.complete(v1);
1243 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
1243 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1244  
1245          checkCompletedWithWrappedCFException(h, ex);
1246          checkCompletedWithWrappedCFException(f, ex);
1247 <        assertFalse(r.ran());
1247 >        assertEquals(0, r.invocationCount);
1248          checkCompletedNormally(g, v1);
1249          }
1250      }
# Line 1046 | Line 1311 | public class CompletableFutureTest exten
1311  
1312          checkCompletedWithWrappedCancellationException(h);
1313          checkCancelled(f);
1314 <        assertFalse(r.ran());
1314 >        assertEquals(0, r.invocationCount);
1315          checkCompletedNormally(g, v1);
1316          }
1317      }
# Line 1067 | Line 1332 | public class CompletableFutureTest exten
1332  
1333          checkCompletedWithWrappedCancellationException(h);
1334          checkCancelled(g);
1335 <        assertFalse(r.ran());
1335 >        assertEquals(0, r.invocationCount);
1336          checkCompletedNormally(f, v1);
1337          }
1338      }
# Line 1087 | Line 1352 | public class CompletableFutureTest exten
1352  
1353          checkCompletedWithWrappedCancellationException(h);
1354          checkCancelled(g);
1355 <        assertFalse(r.ran());
1355 >        assertEquals(0, r.invocationCount);
1356          checkCompletedNormally(f, v1);
1357          }
1358      }
# Line 1107 | Line 1372 | public class CompletableFutureTest exten
1372  
1373          checkCompletedWithWrappedCancellationException(h);
1374          checkCancelled(f);
1375 <        assertFalse(r.ran());
1375 >        assertEquals(0, r.invocationCount);
1376          checkCompletedNormally(g, v1);
1377          }
1378      }
# Line 1128 | Line 1393 | public class CompletableFutureTest exten
1393  
1394          f.complete(v1);
1395          checkIncomplete(h);
1396 <        assertEquals(r.value, 0);
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 1150 | Line 1415 | public class CompletableFutureTest exten
1415  
1416          g.complete(v2);
1417          checkIncomplete(h);
1418 <        assertEquals(r.value, 0);
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 1174 | 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 1194 | 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 1220 | Line 1485 | public class CompletableFutureTest exten
1485  
1486          checkCompletedWithWrappedCFException(h, ex);
1487          checkCompletedWithWrappedCFException(f, ex);
1488 <        assertFalse(r.ran());
1488 >        assertEquals(0, r.invocationCount);
1489          checkCompletedNormally(g, v1);
1490          }
1491      }
# Line 1241 | Line 1506 | public class CompletableFutureTest exten
1506  
1507          checkCompletedWithWrappedCFException(h, ex);
1508          checkCompletedWithWrappedCFException(g, ex);
1509 <        assertFalse(r.ran());
1509 >        assertEquals(0, r.invocationCount);
1510          checkCompletedNormally(f, v1);
1511          }
1512      }
# Line 1261 | Line 1526 | public class CompletableFutureTest exten
1526  
1527          checkCompletedWithWrappedCFException(h, ex);
1528          checkCompletedWithWrappedCFException(g, ex);
1529 <        assertFalse(r.ran());
1529 >        assertEquals(0, r.invocationCount);
1530          checkCompletedNormally(f, v1);
1531          }
1532      }
# Line 1281 | 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 1348 | Line 1613 | public class CompletableFutureTest exten
1613  
1614          checkCompletedWithWrappedCancellationException(h);
1615          checkCancelled(f);
1616 <        assertFalse(r.ran());
1616 >        assertEquals(0, r.invocationCount);
1617          checkCompletedNormally(g, v1);
1618          }
1619      }
# Line 1369 | Line 1634 | public class CompletableFutureTest exten
1634  
1635          checkCompletedWithWrappedCancellationException(h);
1636          checkCancelled(g);
1637 <        assertFalse(r.ran());
1637 >        assertEquals(0, r.invocationCount);
1638          checkCompletedNormally(f, v1);
1639          }
1640      }
# Line 1389 | Line 1654 | public class CompletableFutureTest exten
1654  
1655          checkCompletedWithWrappedCancellationException(h);
1656          checkCancelled(g);
1657 <        assertFalse(r.ran());
1657 >        assertEquals(0, r.invocationCount);
1658          checkCompletedNormally(f, v1);
1659          }
1660      }
# Line 1409 | Line 1674 | public class CompletableFutureTest exten
1674  
1675          checkCompletedWithWrappedCancellationException(h);
1676          checkCancelled(f);
1677 <        assertFalse(r.ran());
1677 >        assertEquals(0, r.invocationCount);
1678          checkCompletedNormally(g, v1);
1679          }
1680      }
# Line 1430 | 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 <        assertTrue(r.ran);
1702 >        assertEquals(1, r.invocationCount);
1703          checkCompletedNormally(f, v1);
1704          checkCompletedNormally(g, v2);
1705          }
# Line 1452 | 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 <        assertTrue(r.ran);
1724 >        assertEquals(1, r.invocationCount);
1725          checkCompletedNormally(f, v1);
1726          checkCompletedNormally(g, v2);
1727          }
# Line 1476 | 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 1496 | Line 1761 | public class CompletableFutureTest exten
1761          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1762  
1763          checkCompletedNormally(h, null);
1764 <        assertTrue(r.ran);
1764 >        assertEquals(1, r.invocationCount);
1765          checkCompletedNormally(f, v1);
1766          checkCompletedNormally(g, v2);
1767          }
# Line 1522 | Line 1787 | public class CompletableFutureTest exten
1787  
1788          checkCompletedWithWrappedCFException(h, ex);
1789          checkCompletedWithWrappedCFException(f, ex);
1790 <        assertFalse(r.ran);
1790 >        assertEquals(0, r.invocationCount);
1791          checkCompletedNormally(g, v1);
1792          }
1793      }
# Line 1543 | Line 1808 | public class CompletableFutureTest exten
1808  
1809          checkCompletedWithWrappedCFException(h, ex);
1810          checkCompletedWithWrappedCFException(g, ex);
1811 <        assertFalse(r.ran);
1811 >        assertEquals(0, r.invocationCount);
1812          checkCompletedNormally(f, v1);
1813          }
1814      }
# Line 1563 | Line 1828 | public class CompletableFutureTest exten
1828  
1829          checkCompletedWithWrappedCFException(h, ex);
1830          checkCompletedWithWrappedCFException(g, ex);
1831 <        assertFalse(r.ran);
1831 >        assertEquals(0, r.invocationCount);
1832          checkCompletedNormally(f, v1);
1833          }
1834      }
# Line 1583 | Line 1848 | public class CompletableFutureTest exten
1848  
1849          checkCompletedWithWrappedCFException(h, ex);
1850          checkCompletedWithWrappedCFException(f, ex);
1851 <        assertFalse(r.ran);
1851 >        assertEquals(0, r.invocationCount);
1852          checkCompletedNormally(g, v1);
1853          }
1854      }
# Line 1650 | Line 1915 | public class CompletableFutureTest exten
1915  
1916          checkCompletedWithWrappedCancellationException(h);
1917          checkCancelled(f);
1918 <        assertFalse(r.ran);
1918 >        assertEquals(0, r.invocationCount);
1919          checkCompletedNormally(g, v1);
1920          }
1921      }
# Line 1671 | Line 1936 | public class CompletableFutureTest exten
1936  
1937          checkCompletedWithWrappedCancellationException(h);
1938          checkCancelled(g);
1939 <        assertFalse(r.ran);
1939 >        assertEquals(0, r.invocationCount);
1940          checkCompletedNormally(f, v1);
1941          }
1942      }
# Line 1691 | Line 1956 | public class CompletableFutureTest exten
1956  
1957          checkCompletedWithWrappedCancellationException(h);
1958          checkCancelled(g);
1959 <        assertFalse(r.ran);
1959 >        assertEquals(0, r.invocationCount);
1960          checkCompletedNormally(f, v1);
1961          }
1962      }
# Line 1711 | Line 1976 | public class CompletableFutureTest exten
1976  
1977          checkCompletedWithWrappedCancellationException(h);
1978          checkCancelled(f);
1979 <        assertFalse(r.ran);
1979 >        assertEquals(0, r.invocationCount);
1980          checkCompletedNormally(g, v1);
1981          }
1982      }
# Line 1720 | Line 1985 | public class CompletableFutureTest exten
1985       * applyToEither result completes normally after normal completion
1986       * of either source
1987       */
1988 <    public void testApplyToEither() {
1989 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1990 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1991 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1727 <        f.complete(one);
1728 <        checkCompletedNormally(g, two);
1729 <        f2.complete(one);
1730 <        checkCompletedNormally(g, two);
1988 >    public void testApplyToEither_normalCompletion1() {
1989 >        for (ExecutionMode m : ExecutionMode.values())
1990 >        for (Integer v1 : new Integer[] { 1, null })
1991 >        for (Integer v2 : new Integer[] { 2, null }) {
1992  
1993 <        f = new CompletableFuture<>();
1994 <        f.complete(one);
1995 <        f2 = new CompletableFuture<>();
1996 <        g = f.applyToEither(f2, inc);
1997 <        checkCompletedNormally(g, two);
1993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1994 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1995 >        final IncFunction r = new IncFunction();
1996 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1997 >
1998 >        f.complete(v1);
1999 >        checkCompletedNormally(h, inc(v1));
2000 >        g.complete(v2);
2001 >
2002 >        checkCompletedNormally(f, v1);
2003 >        checkCompletedNormally(g, v2);
2004 >        checkCompletedNormally(h, inc(v1));
2005 >        }
2006 >    }
2007 >
2008 >    public void testApplyToEither_normalCompletion2() {
2009 >        for (ExecutionMode m : ExecutionMode.values())
2010 >        for (Integer v1 : new Integer[] { 1, null })
2011 >        for (Integer v2 : new Integer[] { 2, null }) {
2012 >
2013 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2014 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2015 >        final IncFunction r = new IncFunction();
2016 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2017 >
2018 >        g.complete(v2);
2019 >        checkCompletedNormally(h, inc(v2));
2020 >        f.complete(v1);
2021 >
2022 >        checkCompletedNormally(f, v1);
2023 >        checkCompletedNormally(g, v2);
2024 >        checkCompletedNormally(h, inc(v2));
2025 >        }
2026 >    }
2027 >    public void testApplyToEither_normalCompletion3() {
2028 >        for (ExecutionMode m : ExecutionMode.values())
2029 >        for (Integer v1 : new Integer[] { 1, null })
2030 >        for (Integer v2 : new Integer[] { 2, null }) {
2031 >
2032 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2033 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2034 >        final IncFunction r = new IncFunction();
2035 >
2036 >        f.complete(v1);
2037 >        g.complete(v2);
2038 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2039 >
2040 >        checkCompletedNormally(f, v1);
2041 >        checkCompletedNormally(g, v2);
2042 >
2043 >        // unspecified behavior
2044 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2045 >                   Objects.equals(h.join(), inc(v2)));
2046 >        assertEquals(1, r.invocationCount);
2047 >        }
2048      }
2049  
2050      /**
2051       * applyToEither result completes exceptionally after exceptional
2052       * completion of either source
2053       */
2054 <    public void testApplyToEither2() {
2055 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2056 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1747 <        f.completeExceptionally(new CFException());
1748 <        f2.complete(one);
1749 <        checkCompletedWithWrappedCFException(g);
2054 >    public void testApplyToEither_exceptionalCompletion1() {
2055 >        for (ExecutionMode m : ExecutionMode.values())
2056 >        for (Integer v1 : new Integer[] { 1, null }) {
2057  
2058 <        f = new CompletableFuture<>();
2059 <        f2 = new CompletableFuture<>();
2060 <        f2.completeExceptionally(new CFException());
2061 <        g = f.applyToEither(f2, inc);
2062 <        checkCompletedWithWrappedCFException(g);
2058 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2059 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2060 >        final IncFunction r = new IncFunction();
2061 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2062 >        final CFException ex = new CFException();
2063 >
2064 >        f.completeExceptionally(ex);
2065 >        checkCompletedWithWrappedCFException(h, ex);
2066 >        g.complete(v1);
2067 >
2068 >        assertEquals(0, r.invocationCount);
2069 >        checkCompletedNormally(g, v1);
2070 >        checkCompletedWithWrappedCFException(f, ex);
2071 >        checkCompletedWithWrappedCFException(h, ex);
2072 >        }
2073 >    }
2074 >
2075 >    public void testApplyToEither_exceptionalCompletion2() {
2076 >        for (ExecutionMode m : ExecutionMode.values())
2077 >        for (Integer v1 : new Integer[] { 1, null }) {
2078 >
2079 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2080 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2081 >        final IncFunction r = new IncFunction();
2082 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2083 >        final CFException ex = new CFException();
2084 >
2085 >        g.completeExceptionally(ex);
2086 >        checkCompletedWithWrappedCFException(h, ex);
2087 >        f.complete(v1);
2088 >
2089 >        assertEquals(0, r.invocationCount);
2090 >        checkCompletedNormally(f, v1);
2091 >        checkCompletedWithWrappedCFException(g, ex);
2092 >        checkCompletedWithWrappedCFException(h, ex);
2093 >        }
2094 >    }
2095 >
2096 >    public void testApplyToEither_exceptionalCompletion3() {
2097 >        for (ExecutionMode m : ExecutionMode.values())
2098 >        for (Integer v1 : new Integer[] { 1, null }) {
2099 >
2100 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2101 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2102 >        final IncFunction r = new IncFunction();
2103 >        final CFException ex = new CFException();
2104 >
2105 >        g.completeExceptionally(ex);
2106 >        f.complete(v1);
2107 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2108 >
2109 >        // unspecified behavior
2110 >        Integer v;
2111 >        try {
2112 >            assertEquals(inc(v1), h.join());
2113 >            assertEquals(1, r.invocationCount);
2114 >        } catch (CompletionException ok) {
2115 >            checkCompletedWithWrappedCFException(h, ex);
2116 >            assertEquals(0, r.invocationCount);
2117 >        }
2118 >
2119 >        checkCompletedWithWrappedCFException(g, ex);
2120 >        checkCompletedNormally(f, v1);
2121 >        }
2122 >    }
2123 >
2124 >    public void testApplyToEither_exceptionalCompletion4() {
2125 >        for (ExecutionMode m : ExecutionMode.values())
2126 >        for (Integer v1 : new Integer[] { 1, null }) {
2127 >
2128 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2129 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2130 >        final IncFunction r = new IncFunction();
2131 >        final CFException ex = new CFException();
2132 >
2133 >        f.completeExceptionally(ex);
2134 >        g.complete(v1);
2135 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2136 >
2137 >        // unspecified behavior
2138 >        Integer v;
2139 >        try {
2140 >            assertEquals(inc(v1), h.join());
2141 >            assertEquals(1, r.invocationCount);
2142 >        } catch (CompletionException ok) {
2143 >            checkCompletedWithWrappedCFException(h, ex);
2144 >            assertEquals(0, r.invocationCount);
2145 >        }
2146 >
2147 >        checkCompletedWithWrappedCFException(f, ex);
2148 >        checkCompletedNormally(g, v1);
2149 >        }
2150      }
2151  
2152      /**
2153       * applyToEither result completes exceptionally if action does
2154       */
2155 <    public void testApplyToEither3() {
2156 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2157 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2158 <        FailingFunction r = new FailingFunction();
2159 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2160 <        f2.complete(two);
2161 <        checkCompletedWithWrappedCFException(g);
2155 >    public void testApplyToEither_actionFailed1() {
2156 >        for (ExecutionMode m : ExecutionMode.values())
2157 >        for (Integer v1 : new Integer[] { 1, null })
2158 >        for (Integer v2 : new Integer[] { 2, null }) {
2159 >
2160 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2161 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2162 >        final FailingFunction r = new FailingFunction();
2163 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2164 >
2165 >        f.complete(v1);
2166 >        checkCompletedWithWrappedCFException(h);
2167 >        g.complete(v2);
2168 >        checkCompletedNormally(f, v1);
2169 >        checkCompletedNormally(g, v2);
2170 >        }
2171 >    }
2172 >
2173 >    public void testApplyToEither_actionFailed2() {
2174 >        for (ExecutionMode m : ExecutionMode.values())
2175 >        for (Integer v1 : new Integer[] { 1, null })
2176 >        for (Integer v2 : new Integer[] { 2, null }) {
2177 >
2178 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180 >        final FailingFunction r = new FailingFunction();
2181 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2182 >
2183 >        g.complete(v2);
2184 >        checkCompletedWithWrappedCFException(h);
2185 >        f.complete(v1);
2186 >        checkCompletedNormally(f, v1);
2187 >        checkCompletedNormally(g, v2);
2188 >        }
2189      }
2190  
2191      /**
2192       * applyToEither result completes exceptionally if either source cancelled
2193       */
2194 <    public void testApplyToEither4() {
2195 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2196 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2197 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2198 <        assertTrue(f.cancel(true));
2199 <        checkCompletedWithWrappedCancellationException(g);
2200 <        f = new CompletableFuture<>();
2201 <        f2 = new CompletableFuture<>();
2202 <        assertTrue(f2.cancel(true));
2203 <        checkCompletedWithWrappedCancellationException(g);
2194 >    public void testApplyToEither_sourceCancelled1() {
2195 >        for (ExecutionMode m : ExecutionMode.values())
2196 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2197 >        for (Integer v1 : new Integer[] { 1, null }) {
2198 >
2199 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2200 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2201 >        final IncFunction r = new IncFunction();
2202 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2203 >
2204 >        assertTrue(f.cancel(mayInterruptIfRunning));
2205 >        checkCompletedWithWrappedCancellationException(h);
2206 >        g.complete(v1);
2207 >
2208 >        checkCancelled(f);
2209 >        assertEquals(0, r.invocationCount);
2210 >        checkCompletedNormally(g, v1);
2211 >        checkCompletedWithWrappedCancellationException(h);
2212 >        }
2213 >    }
2214 >
2215 >    public void testApplyToEither_sourceCancelled2() {
2216 >        for (ExecutionMode m : ExecutionMode.values())
2217 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2218 >        for (Integer v1 : new Integer[] { 1, null }) {
2219 >
2220 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2221 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2222 >        final IncFunction r = new IncFunction();
2223 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2224 >
2225 >        assertTrue(g.cancel(mayInterruptIfRunning));
2226 >        checkCompletedWithWrappedCancellationException(h);
2227 >        f.complete(v1);
2228 >
2229 >        checkCancelled(g);
2230 >        assertEquals(0, r.invocationCount);
2231 >        checkCompletedNormally(f, v1);
2232 >        checkCompletedWithWrappedCancellationException(h);
2233 >        }
2234 >    }
2235 >
2236 >    public void testApplyToEither_sourceCancelled3() {
2237 >        for (ExecutionMode m : ExecutionMode.values())
2238 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2239 >        for (Integer v1 : new Integer[] { 1, null }) {
2240 >
2241 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 >        final IncFunction r = new IncFunction();
2244 >
2245 >        assertTrue(g.cancel(mayInterruptIfRunning));
2246 >        f.complete(v1);
2247 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2248 >
2249 >        // unspecified behavior
2250 >        Integer v;
2251 >        try {
2252 >            assertEquals(inc(v1), h.join());
2253 >            assertEquals(1, r.invocationCount);
2254 >        } catch (CompletionException ok) {
2255 >            checkCompletedWithWrappedCancellationException(h);
2256 >            assertEquals(0, r.invocationCount);
2257 >        }
2258 >
2259 >        checkCancelled(g);
2260 >        checkCompletedNormally(f, v1);
2261 >        }
2262 >    }
2263 >
2264 >    public void testApplyToEither_sourceCancelled4() {
2265 >        for (ExecutionMode m : ExecutionMode.values())
2266 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2267 >        for (Integer v1 : new Integer[] { 1, null }) {
2268 >
2269 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2270 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2271 >        final IncFunction r = new IncFunction();
2272 >
2273 >        assertTrue(f.cancel(mayInterruptIfRunning));
2274 >        g.complete(v1);
2275 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2276 >
2277 >        // unspecified behavior
2278 >        Integer v;
2279 >        try {
2280 >            assertEquals(inc(v1), h.join());
2281 >            assertEquals(1, r.invocationCount);
2282 >        } catch (CompletionException ok) {
2283 >            checkCompletedWithWrappedCancellationException(h);
2284 >            assertEquals(0, r.invocationCount);
2285 >        }
2286 >
2287 >        checkCancelled(f);
2288 >        checkCompletedNormally(g, v1);
2289 >        }
2290      }
2291  
2292      /**
2293       * acceptEither result completes normally after normal completion
2294       * of either source
2295       */
2296 <    public void testAcceptEither() {
2297 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2298 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2299 <        IncAction r = new IncAction();
1793 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1794 <        f.complete(one);
1795 <        checkCompletedNormally(g, null);
1796 <        f2.complete(one);
1797 <        checkCompletedNormally(g, null);
1798 <        assertEquals(r.value, 2);
2296 >    public void testAcceptEither_normalCompletion1() {
2297 >        for (ExecutionMode m : ExecutionMode.values())
2298 >        for (Integer v1 : new Integer[] { 1, null })
2299 >        for (Integer v2 : new Integer[] { 2, null }) {
2300  
2301 <        r = new IncAction();
2302 <        f = new CompletableFuture<>();
2303 <        f.complete(one);
2304 <        f2 = new CompletableFuture<>();
2305 <        g = f.acceptEither(f2, r);
2306 <        checkCompletedNormally(g, null);
2307 <        assertEquals(r.value, 2);
2301 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2302 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2303 >        final IncAction r = new IncAction();
2304 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2305 >
2306 >        f.complete(v1);
2307 >        checkCompletedNormally(h, null);
2308 >        assertEquals(inc(v1), r.value);
2309 >        g.complete(v2);
2310 >
2311 >        checkCompletedNormally(f, v1);
2312 >        checkCompletedNormally(g, v2);
2313 >        checkCompletedNormally(h, null);
2314 >        }
2315 >    }
2316 >
2317 >    public void testAcceptEither_normalCompletion2() {
2318 >        for (ExecutionMode m : ExecutionMode.values())
2319 >        for (Integer v1 : new Integer[] { 1, null })
2320 >        for (Integer v2 : new Integer[] { 2, null }) {
2321 >
2322 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2323 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 >        final IncAction r = new IncAction();
2325 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2326 >
2327 >        g.complete(v2);
2328 >        checkCompletedNormally(h, null);
2329 >        assertEquals(inc(v2), r.value);
2330 >        f.complete(v1);
2331 >
2332 >        checkCompletedNormally(f, v1);
2333 >        checkCompletedNormally(g, v2);
2334 >        checkCompletedNormally(h, null);
2335 >        }
2336 >    }
2337 >    public void testAcceptEither_normalCompletion3() {
2338 >        for (ExecutionMode m : ExecutionMode.values())
2339 >        for (Integer v1 : new Integer[] { 1, null })
2340 >        for (Integer v2 : new Integer[] { 2, null }) {
2341 >
2342 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2343 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2344 >        final IncAction r = new IncAction();
2345 >
2346 >        f.complete(v1);
2347 >        g.complete(v2);
2348 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2349 >
2350 >        checkCompletedNormally(h, null);
2351 >        checkCompletedNormally(f, v1);
2352 >        checkCompletedNormally(g, v2);
2353 >
2354 >        // unspecified behavior
2355 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2356 >                   Objects.equals(r.value, inc(v2)));
2357 >        }
2358      }
2359  
2360      /**
2361       * acceptEither result completes exceptionally after exceptional
2362       * completion of either source
2363       */
2364 <    public void testAcceptEither2() {
2365 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2366 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1816 <        IncAction r = new IncAction();
1817 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1818 <        f.completeExceptionally(new CFException());
1819 <        f2.complete(one);
1820 <        checkCompletedWithWrappedCFException(g);
2364 >    public void testAcceptEither_exceptionalCompletion1() {
2365 >        for (ExecutionMode m : ExecutionMode.values())
2366 >        for (Integer v1 : new Integer[] { 1, null }) {
2367  
2368 <        r = new IncAction();
2369 <        f = new CompletableFuture<>();
2370 <        f2 = new CompletableFuture<>();
2371 <        f2.completeExceptionally(new CFException());
2372 <        g = f.acceptEither(f2, r);
2373 <        checkCompletedWithWrappedCFException(g);
2368 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2369 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2370 >        final IncAction r = new IncAction();
2371 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2372 >        final CFException ex = new CFException();
2373 >
2374 >        f.completeExceptionally(ex);
2375 >        checkCompletedWithWrappedCFException(h, ex);
2376 >        g.complete(v1);
2377 >
2378 >        assertEquals(0, r.invocationCount);
2379 >        checkCompletedNormally(g, v1);
2380 >        checkCompletedWithWrappedCFException(f, ex);
2381 >        checkCompletedWithWrappedCFException(h, ex);
2382 >        }
2383 >    }
2384 >
2385 >    public void testAcceptEither_exceptionalCompletion2() {
2386 >        for (ExecutionMode m : ExecutionMode.values())
2387 >        for (Integer v1 : new Integer[] { 1, null }) {
2388 >
2389 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2390 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2391 >        final IncAction r = new IncAction();
2392 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2393 >        final CFException ex = new CFException();
2394 >
2395 >        g.completeExceptionally(ex);
2396 >        checkCompletedWithWrappedCFException(h, ex);
2397 >        f.complete(v1);
2398 >
2399 >        assertEquals(0, r.invocationCount);
2400 >        checkCompletedNormally(f, v1);
2401 >        checkCompletedWithWrappedCFException(g, ex);
2402 >        checkCompletedWithWrappedCFException(h, ex);
2403 >        }
2404 >    }
2405 >
2406 >    public void testAcceptEither_exceptionalCompletion3() {
2407 >        for (ExecutionMode m : ExecutionMode.values())
2408 >        for (Integer v1 : new Integer[] { 1, null }) {
2409 >
2410 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2411 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2412 >        final IncAction r = new IncAction();
2413 >        final CFException ex = new CFException();
2414 >
2415 >        g.completeExceptionally(ex);
2416 >        f.complete(v1);
2417 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2418 >
2419 >        // unspecified behavior
2420 >        Integer v;
2421 >        try {
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(0, r.invocationCount);
2428 >        }
2429 >
2430 >        checkCompletedWithWrappedCFException(g, ex);
2431 >        checkCompletedNormally(f, v1);
2432 >        }
2433 >    }
2434 >
2435 >    public void testAcceptEither_exceptionalCompletion4() {
2436 >        for (ExecutionMode m : ExecutionMode.values())
2437 >        for (Integer v1 : new Integer[] { 1, null }) {
2438 >
2439 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2440 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2441 >        final IncAction r = new IncAction();
2442 >        final CFException ex = new CFException();
2443 >
2444 >        f.completeExceptionally(ex);
2445 >        g.complete(v1);
2446 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2447 >
2448 >        // unspecified behavior
2449 >        Integer v;
2450 >        try {
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(0, r.invocationCount);
2457 >        }
2458 >
2459 >        checkCompletedWithWrappedCFException(f, ex);
2460 >        checkCompletedNormally(g, v1);
2461 >        }
2462      }
2463  
2464      /**
2465       * acceptEither result completes exceptionally if action does
2466       */
2467 <    public void testAcceptEither3() {
2468 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2469 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2470 <        FailingConsumer r = new FailingConsumer();
2471 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2472 <        f2.complete(two);
2473 <        checkCompletedWithWrappedCFException(g);
2467 >    public void testAcceptEither_actionFailed1() {
2468 >        for (ExecutionMode m : ExecutionMode.values())
2469 >        for (Integer v1 : new Integer[] { 1, null })
2470 >        for (Integer v2 : new Integer[] { 2, null }) {
2471 >
2472 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 >        final FailingConsumer r = new FailingConsumer();
2475 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2476 >
2477 >        f.complete(v1);
2478 >        checkCompletedWithWrappedCFException(h);
2479 >        g.complete(v2);
2480 >        checkCompletedNormally(f, v1);
2481 >        checkCompletedNormally(g, v2);
2482 >        }
2483 >    }
2484 >
2485 >    public void testAcceptEither_actionFailed2() {
2486 >        for (ExecutionMode m : ExecutionMode.values())
2487 >        for (Integer v1 : new Integer[] { 1, null })
2488 >        for (Integer v2 : new Integer[] { 2, null }) {
2489 >
2490 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2491 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2492 >        final FailingConsumer r = new FailingConsumer();
2493 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2494 >
2495 >        g.complete(v2);
2496 >        checkCompletedWithWrappedCFException(h);
2497 >        f.complete(v1);
2498 >        checkCompletedNormally(f, v1);
2499 >        checkCompletedNormally(g, v2);
2500 >        }
2501      }
2502  
2503      /**
2504       * acceptEither result completes exceptionally if either source cancelled
2505       */
2506 <    public void testAcceptEither4() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2508 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2509 <        IncAction r = new IncAction();
2510 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2511 <        assertTrue(f.cancel(true));
2512 <        checkCompletedWithWrappedCancellationException(g);
2513 <        f = new CompletableFuture<>();
2514 <        f2 = new CompletableFuture<>();
2515 <        assertTrue(f2.cancel(true));
2516 <        checkCompletedWithWrappedCancellationException(g);
2506 >    public void testAcceptEither_sourceCancelled1() {
2507 >        for (ExecutionMode m : ExecutionMode.values())
2508 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2509 >        for (Integer v1 : new Integer[] { 1, null }) {
2510 >
2511 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2513 >        final IncAction r = new IncAction();
2514 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2515 >
2516 >        assertTrue(f.cancel(mayInterruptIfRunning));
2517 >        checkCompletedWithWrappedCancellationException(h);
2518 >        g.complete(v1);
2519 >
2520 >        checkCancelled(f);
2521 >        assertEquals(0, r.invocationCount);
2522 >        checkCompletedNormally(g, v1);
2523 >        checkCompletedWithWrappedCancellationException(h);
2524 >        }
2525 >    }
2526 >
2527 >    public void testAcceptEither_sourceCancelled2() {
2528 >        for (ExecutionMode m : ExecutionMode.values())
2529 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2530 >        for (Integer v1 : new Integer[] { 1, null }) {
2531 >
2532 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2533 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2534 >        final IncAction r = new IncAction();
2535 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2536 >
2537 >        assertTrue(g.cancel(mayInterruptIfRunning));
2538 >        checkCompletedWithWrappedCancellationException(h);
2539 >        f.complete(v1);
2540 >
2541 >        checkCancelled(g);
2542 >        assertEquals(0, r.invocationCount);
2543 >        checkCompletedNormally(f, v1);
2544 >        checkCompletedWithWrappedCancellationException(h);
2545 >        }
2546 >    }
2547 >
2548 >    public void testAcceptEither_sourceCancelled3() {
2549 >        for (ExecutionMode m : ExecutionMode.values())
2550 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2551 >        for (Integer v1 : new Integer[] { 1, null }) {
2552 >
2553 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2554 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2555 >        final IncAction r = new IncAction();
2556 >
2557 >        assertTrue(g.cancel(mayInterruptIfRunning));
2558 >        f.complete(v1);
2559 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2560 >
2561 >        // unspecified behavior
2562 >        Integer v;
2563 >        try {
2564 >            assertNull(h.join());
2565 >            assertEquals(1, r.invocationCount);
2566 >            assertEquals(inc(v1), r.value);
2567 >        } catch (CompletionException ok) {
2568 >            checkCompletedWithWrappedCancellationException(h);
2569 >            assertEquals(0, r.invocationCount);
2570 >        }
2571 >
2572 >        checkCancelled(g);
2573 >        checkCompletedNormally(f, v1);
2574 >        }
2575 >    }
2576 >
2577 >    public void testAcceptEither_sourceCancelled4() {
2578 >        for (ExecutionMode m : ExecutionMode.values())
2579 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2580 >        for (Integer v1 : new Integer[] { 1, null }) {
2581 >
2582 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2583 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2584 >        final IncAction r = new IncAction();
2585 >
2586 >        assertTrue(f.cancel(mayInterruptIfRunning));
2587 >        g.complete(v1);
2588 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2589 >
2590 >        // unspecified behavior
2591 >        Integer v;
2592 >        try {
2593 >            assertNull(h.join());
2594 >            assertEquals(1, r.invocationCount);
2595 >            assertEquals(inc(v1), r.value);
2596 >        } catch (CompletionException ok) {
2597 >            checkCompletedWithWrappedCancellationException(h);
2598 >            assertEquals(0, r.invocationCount);
2599 >        }
2600 >
2601 >        checkCancelled(f);
2602 >        checkCompletedNormally(g, v1);
2603 >        }
2604      }
2605  
2606      /**
2607       * runAfterEither result completes normally after normal completion
2608       * of either source
2609       */
2610 <    public void testRunAfterEither() {
2611 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2612 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2613 <        Noop r = new Noop();
1866 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1867 <        f.complete(one);
1868 <        checkCompletedNormally(g, null);
1869 <        f2.complete(one);
1870 <        checkCompletedNormally(g, null);
1871 <        assertTrue(r.ran);
2610 >    public void testRunAfterEither_normalCompletion1() {
2611 >        for (ExecutionMode m : ExecutionMode.values())
2612 >        for (Integer v1 : new Integer[] { 1, null })
2613 >        for (Integer v2 : new Integer[] { 2, null }) {
2614  
2615 <        r = new Noop();
2616 <        f = new CompletableFuture<>();
2617 <        f.complete(one);
2618 <        f2 = new CompletableFuture<>();
2619 <        g = f.runAfterEither(f2, r);
2620 <        checkCompletedNormally(g, null);
2621 <        assertTrue(r.ran);
2615 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2616 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2617 >        final Noop r = new Noop();
2618 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2619 >
2620 >        f.complete(v1);
2621 >        checkCompletedNormally(h, null);
2622 >        assertEquals(1, r.invocationCount);
2623 >        g.complete(v2);
2624 >
2625 >        checkCompletedNormally(f, v1);
2626 >        checkCompletedNormally(g, v2);
2627 >        checkCompletedNormally(h, null);
2628 >        assertEquals(1, r.invocationCount);
2629 >        }
2630 >    }
2631 >
2632 >    public void testRunAfterEither_normalCompletion2() {
2633 >        for (ExecutionMode m : ExecutionMode.values())
2634 >        for (Integer v1 : new Integer[] { 1, null })
2635 >        for (Integer v2 : new Integer[] { 2, null }) {
2636 >
2637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2639 >        final Noop r = new Noop();
2640 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2641 >
2642 >        g.complete(v2);
2643 >        checkCompletedNormally(h, null);
2644 >        assertEquals(1, r.invocationCount);
2645 >        f.complete(v1);
2646 >
2647 >        checkCompletedNormally(f, v1);
2648 >        checkCompletedNormally(g, v2);
2649 >        checkCompletedNormally(h, null);
2650 >        assertEquals(1, r.invocationCount);
2651 >        }
2652 >    }
2653 >    public void testRunAfterEither_normalCompletion3() {
2654 >        for (ExecutionMode m : ExecutionMode.values())
2655 >        for (Integer v1 : new Integer[] { 1, null })
2656 >        for (Integer v2 : new Integer[] { 2, null }) {
2657 >
2658 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2659 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2660 >        final Noop r = new Noop();
2661 >
2662 >        f.complete(v1);
2663 >        g.complete(v2);
2664 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2665 >
2666 >        checkCompletedNormally(h, null);
2667 >        checkCompletedNormally(f, v1);
2668 >        checkCompletedNormally(g, v2);
2669 >        assertEquals(1, r.invocationCount);
2670 >        }
2671      }
2672  
2673      /**
2674       * runAfterEither result completes exceptionally after exceptional
2675       * completion of either source
2676       */
2677 <    public void testRunAfterEither2() {
2678 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2679 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1889 <        Noop r = new Noop();
1890 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1891 <        f.completeExceptionally(new CFException());
1892 <        f2.complete(one);
1893 <        checkCompletedWithWrappedCFException(g);
2677 >    public void testRunAfterEither_exceptionalCompletion1() {
2678 >        for (ExecutionMode m : ExecutionMode.values())
2679 >        for (Integer v1 : new Integer[] { 1, null }) {
2680  
2681 <        r = new Noop();
2682 <        f = new CompletableFuture<>();
2683 <        f2 = new CompletableFuture<>();
2684 <        f2.completeExceptionally(new CFException());
2685 <        g = f.runAfterEither(f2, r);
2686 <        checkCompletedWithWrappedCFException(g);
2681 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2682 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2683 >        final Noop r = new Noop();
2684 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2685 >        final CFException ex = new CFException();
2686 >
2687 >        f.completeExceptionally(ex);
2688 >        checkCompletedWithWrappedCFException(h, ex);
2689 >        g.complete(v1);
2690 >
2691 >        assertEquals(0, r.invocationCount);
2692 >        checkCompletedNormally(g, v1);
2693 >        checkCompletedWithWrappedCFException(f, ex);
2694 >        checkCompletedWithWrappedCFException(h, ex);
2695 >        }
2696 >    }
2697 >
2698 >    public void testRunAfterEither_exceptionalCompletion2() {
2699 >        for (ExecutionMode m : ExecutionMode.values())
2700 >        for (Integer v1 : new Integer[] { 1, null }) {
2701 >
2702 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2703 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2704 >        final Noop r = new Noop();
2705 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2706 >        final CFException ex = new CFException();
2707 >
2708 >        g.completeExceptionally(ex);
2709 >        checkCompletedWithWrappedCFException(h, ex);
2710 >        f.complete(v1);
2711 >
2712 >        assertEquals(0, r.invocationCount);
2713 >        checkCompletedNormally(f, v1);
2714 >        checkCompletedWithWrappedCFException(g, ex);
2715 >        checkCompletedWithWrappedCFException(h, ex);
2716 >        }
2717 >    }
2718 >
2719 >    public void testRunAfterEither_exceptionalCompletion3() {
2720 >        for (ExecutionMode m : ExecutionMode.values())
2721 >        for (Integer v1 : new Integer[] { 1, null }) {
2722 >
2723 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2724 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2725 >        final Noop r = new Noop();
2726 >        final CFException ex = new CFException();
2727 >
2728 >        g.completeExceptionally(ex);
2729 >        f.complete(v1);
2730 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2731 >
2732 >        // unspecified behavior
2733 >        Integer v;
2734 >        try {
2735 >            assertNull(h.join());
2736 >            assertEquals(1, r.invocationCount);
2737 >        } catch (CompletionException ok) {
2738 >            checkCompletedWithWrappedCFException(h, ex);
2739 >            assertEquals(0, r.invocationCount);
2740 >        }
2741 >
2742 >        checkCompletedWithWrappedCFException(g, ex);
2743 >        checkCompletedNormally(f, v1);
2744 >        }
2745 >    }
2746 >
2747 >    public void testRunAfterEither_exceptionalCompletion4() {
2748 >        for (ExecutionMode m : ExecutionMode.values())
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();
2754 >        final CFException ex = new CFException();
2755 >
2756 >        f.completeExceptionally(ex);
2757 >        g.complete(v1);
2758 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2759 >
2760 >        // unspecified behavior
2761 >        Integer v;
2762 >        try {
2763 >            assertNull(h.join());
2764 >            assertEquals(1, r.invocationCount);
2765 >        } catch (CompletionException ok) {
2766 >            checkCompletedWithWrappedCFException(h, ex);
2767 >            assertEquals(0, r.invocationCount);
2768 >        }
2769 >
2770 >        checkCompletedWithWrappedCFException(f, ex);
2771 >        checkCompletedNormally(g, v1);
2772 >        }
2773      }
2774  
2775      /**
2776       * runAfterEither result completes exceptionally if action does
2777       */
2778 <    public void testRunAfterEither3() {
2779 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2780 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2781 <        FailingNoop r = new FailingNoop();
2782 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2783 <        f2.complete(two);
2784 <        checkCompletedWithWrappedCFException(g);
2778 >    public void testRunAfterEither_actionFailed1() {
2779 >        for (ExecutionMode m : ExecutionMode.values())
2780 >        for (Integer v1 : new Integer[] { 1, null })
2781 >        for (Integer v2 : new Integer[] { 2, null }) {
2782 >
2783 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2784 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2785 >        final FailingNoop r = new FailingNoop();
2786 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2787 >
2788 >        f.complete(v1);
2789 >        checkCompletedWithWrappedCFException(h);
2790 >        g.complete(v2);
2791 >        checkCompletedNormally(f, v1);
2792 >        checkCompletedNormally(g, v2);
2793 >        }
2794 >    }
2795 >
2796 >    public void testRunAfterEither_actionFailed2() {
2797 >        for (ExecutionMode m : ExecutionMode.values())
2798 >        for (Integer v1 : new Integer[] { 1, null })
2799 >        for (Integer v2 : new Integer[] { 2, null }) {
2800 >
2801 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2802 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2803 >        final FailingNoop r = new FailingNoop();
2804 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2805 >
2806 >        g.complete(v2);
2807 >        checkCompletedWithWrappedCFException(h);
2808 >        f.complete(v1);
2809 >        checkCompletedNormally(f, v1);
2810 >        checkCompletedNormally(g, v2);
2811 >        }
2812      }
2813  
2814      /**
2815       * runAfterEither result completes exceptionally if either source cancelled
2816       */
2817 <    public void testRunAfterEither4() {
2818 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2819 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2820 <        Noop r = new Noop();
2821 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2822 <        assertTrue(f.cancel(true));
2823 <        checkCompletedWithWrappedCancellationException(g);
2824 <        f = new CompletableFuture<>();
2825 <        f2 = new CompletableFuture<>();
2826 <        assertTrue(f2.cancel(true));
2827 <        checkCompletedWithWrappedCancellationException(g);
2817 >    public void testRunAfterEither_sourceCancelled1() {
2818 >        for (ExecutionMode m : ExecutionMode.values())
2819 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2820 >        for (Integer v1 : new Integer[] { 1, null }) {
2821 >
2822 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2823 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2824 >        final Noop r = new Noop();
2825 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2826 >
2827 >        assertTrue(f.cancel(mayInterruptIfRunning));
2828 >        checkCompletedWithWrappedCancellationException(h);
2829 >        g.complete(v1);
2830 >
2831 >        checkCancelled(f);
2832 >        assertEquals(0, r.invocationCount);
2833 >        checkCompletedNormally(g, v1);
2834 >        checkCompletedWithWrappedCancellationException(h);
2835 >        }
2836 >    }
2837 >
2838 >    public void testRunAfterEither_sourceCancelled2() {
2839 >        for (ExecutionMode m : ExecutionMode.values())
2840 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2841 >        for (Integer v1 : new Integer[] { 1, null }) {
2842 >
2843 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2844 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2845 >        final Noop r = new Noop();
2846 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2847 >
2848 >        assertTrue(g.cancel(mayInterruptIfRunning));
2849 >        checkCompletedWithWrappedCancellationException(h);
2850 >        f.complete(v1);
2851 >
2852 >        checkCancelled(g);
2853 >        assertEquals(0, r.invocationCount);
2854 >        checkCompletedNormally(f, v1);
2855 >        checkCompletedWithWrappedCancellationException(h);
2856 >        }
2857 >    }
2858 >
2859 >    public void testRunAfterEither_sourceCancelled3() {
2860 >        for (ExecutionMode m : ExecutionMode.values())
2861 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2862 >        for (Integer v1 : new Integer[] { 1, null }) {
2863 >
2864 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2865 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2866 >        final Noop r = new Noop();
2867 >
2868 >        assertTrue(g.cancel(mayInterruptIfRunning));
2869 >        f.complete(v1);
2870 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2871 >
2872 >        // unspecified behavior
2873 >        Integer v;
2874 >        try {
2875 >            assertNull(h.join());
2876 >            assertEquals(1, r.invocationCount);
2877 >        } catch (CompletionException ok) {
2878 >            checkCompletedWithWrappedCancellationException(h);
2879 >            assertEquals(0, r.invocationCount);
2880 >        }
2881 >
2882 >        checkCancelled(g);
2883 >        checkCompletedNormally(f, v1);
2884 >        }
2885 >    }
2886 >
2887 >    public void testRunAfterEither_sourceCancelled4() {
2888 >        for (ExecutionMode m : ExecutionMode.values())
2889 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2890 >        for (Integer v1 : new Integer[] { 1, null }) {
2891 >
2892 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2893 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2894 >        final Noop r = new Noop();
2895 >
2896 >        assertTrue(f.cancel(mayInterruptIfRunning));
2897 >        g.complete(v1);
2898 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2899 >
2900 >        // unspecified behavior
2901 >        Integer v;
2902 >        try {
2903 >            assertNull(h.join());
2904 >            assertEquals(1, r.invocationCount);
2905 >        } catch (CompletionException ok) {
2906 >            checkCompletedWithWrappedCancellationException(h);
2907 >            assertEquals(0, r.invocationCount);
2908 >        }
2909 >
2910 >        checkCancelled(f);
2911 >        checkCompletedNormally(g, v1);
2912 >        }
2913      }
2914  
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;
1957 <        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 2113 | Line 3143 | public class CompletableFutureTest exten
3143          CompletableFuture<Void> g = f.thenAcceptAsync(r);
3144          f.complete(one);
3145          checkCompletedNormally(g, null);
3146 <        assertEquals(r.value, 2);
3146 >        assertEquals(r.value, (Integer) 2);
3147      }
3148  
3149      /**
# Line 2150 | Line 3180 | public class CompletableFutureTest exten
3180          checkCompletedWithWrappedCancellationException(g);
3181      }
3182  
2153    /**
2154     * applyToEitherAsync result completes normally after normal
2155     * completion of sources
2156     */
2157    public void testApplyToEitherAsync() {
2158        CompletableFuture<Integer> f = new CompletableFuture<>();
2159        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2160        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2161        f.complete(one);
2162        checkCompletedNormally(g, two);
2163
2164        f = new CompletableFuture<>();
2165        f.complete(one);
2166        f2 = new CompletableFuture<>();
2167        g = f.applyToEitherAsync(f2, inc);
2168        checkCompletedNormally(g, two);
2169    }
2170
2171    /**
2172     * applyToEitherAsync result completes exceptionally after exceptional
2173     * completion of source
2174     */
2175    public void testApplyToEitherAsync2() {
2176        CompletableFuture<Integer> f = new CompletableFuture<>();
2177        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2178        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2179        f.completeExceptionally(new CFException());
2180        checkCompletedWithWrappedCFException(g);
2181
2182        f = new CompletableFuture<>();
2183        f2 = new CompletableFuture<>();
2184        f2.completeExceptionally(new CFException());
2185        g = f.applyToEitherAsync(f2, inc);
2186        f.complete(one);
2187        checkCompletedWithWrappedCFException(g);
2188    }
2189
2190    /**
2191     * applyToEitherAsync result completes exceptionally if action does
2192     */
2193    public void testApplyToEitherAsync3() {
2194        CompletableFuture<Integer> f = new CompletableFuture<>();
2195        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2196        FailingFunction r = new FailingFunction();
2197        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2198        f.complete(one);
2199        checkCompletedWithWrappedCFException(g);
2200    }
2201
2202    /**
2203     * applyToEitherAsync result completes exceptionally if either source cancelled
2204     */
2205    public void testApplyToEitherAsync4() {
2206        CompletableFuture<Integer> f = new CompletableFuture<>();
2207        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2208        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2209        assertTrue(f.cancel(true));
2210        checkCompletedWithWrappedCancellationException(g);
2211
2212        f = new CompletableFuture<>();
2213        f2 = new CompletableFuture<>();
2214        assertTrue(f2.cancel(true));
2215        g = f.applyToEitherAsync(f2, inc);
2216        checkCompletedWithWrappedCancellationException(g);
2217    }
2218
2219    /**
2220     * acceptEitherAsync result completes normally after normal
2221     * completion of sources
2222     */
2223    public void testAcceptEitherAsync() {
2224        CompletableFuture<Integer> f = new CompletableFuture<>();
2225        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226        IncAction r = new IncAction();
2227        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2228        f.complete(one);
2229        checkCompletedNormally(g, null);
2230        assertEquals(r.value, 2);
2231
2232        r = new IncAction();
2233        f = new CompletableFuture<>();
2234        f.complete(one);
2235        f2 = new CompletableFuture<>();
2236        g = f.acceptEitherAsync(f2, r);
2237        checkCompletedNormally(g, null);
2238        assertEquals(r.value, 2);
2239    }
2240
2241    /**
2242     * acceptEitherAsync result completes exceptionally after exceptional
2243     * completion of source
2244     */
2245    public void testAcceptEitherAsync2() {
2246        CompletableFuture<Integer> f = new CompletableFuture<>();
2247        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248        IncAction r = new IncAction();
2249        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2250        f.completeExceptionally(new CFException());
2251        checkCompletedWithWrappedCFException(g);
2252
2253        r = new IncAction();
2254        f = new CompletableFuture<>();
2255        f2 = new CompletableFuture<>();
2256        f2.completeExceptionally(new CFException());
2257        g = f.acceptEitherAsync(f2, r);
2258        f.complete(one);
2259        checkCompletedWithWrappedCFException(g);
2260    }
2261
2262    /**
2263     * acceptEitherAsync result completes exceptionally if action does
2264     */
2265    public void testAcceptEitherAsync3() {
2266        CompletableFuture<Integer> f = new CompletableFuture<>();
2267        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2268        FailingConsumer r = new FailingConsumer();
2269        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2270        f.complete(one);
2271        checkCompletedWithWrappedCFException(g);
2272    }
2273
2274    /**
2275     * acceptEitherAsync result completes exceptionally if either
2276     * source cancelled
2277     */
2278    public void testAcceptEitherAsync4() {
2279        CompletableFuture<Integer> f = new CompletableFuture<>();
2280        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2281        IncAction r = new IncAction();
2282        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2283        assertTrue(f.cancel(true));
2284        checkCompletedWithWrappedCancellationException(g);
2285
2286        r = new IncAction();
2287        f = new CompletableFuture<>();
2288        f2 = new CompletableFuture<>();
2289        assertTrue(f2.cancel(true));
2290        g = f.acceptEitherAsync(f2, r);
2291        checkCompletedWithWrappedCancellationException(g);
2292    }
2293
2294    /**
2295     * runAfterEitherAsync result completes normally after normal
2296     * completion of sources
2297     */
2298    public void testRunAfterEitherAsync() {
2299        CompletableFuture<Integer> f = new CompletableFuture<>();
2300        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2301        Noop r = new Noop();
2302        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2303        f.complete(one);
2304        checkCompletedNormally(g, null);
2305        assertTrue(r.ran);
2306
2307        r = new Noop();
2308        f = new CompletableFuture<>();
2309        f.complete(one);
2310        f2 = new CompletableFuture<>();
2311        g = f.runAfterEitherAsync(f2, r);
2312        checkCompletedNormally(g, null);
2313        assertTrue(r.ran);
2314    }
2315
2316    /**
2317     * runAfterEitherAsync result completes exceptionally after exceptional
2318     * completion of source
2319     */
2320    public void testRunAfterEitherAsync2() {
2321        CompletableFuture<Integer> f = new CompletableFuture<>();
2322        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2323        Noop r = new Noop();
2324        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2325        f.completeExceptionally(new CFException());
2326        checkCompletedWithWrappedCFException(g);
2327
2328        r = new Noop();
2329        f = new CompletableFuture<>();
2330        f2 = new CompletableFuture<>();
2331        f2.completeExceptionally(new CFException());
2332        g = f.runAfterEitherAsync(f2, r);
2333        f.complete(one);
2334        checkCompletedWithWrappedCFException(g);
2335    }
2336
2337    /**
2338     * runAfterEitherAsync result completes exceptionally if action does
2339     */
2340    public void testRunAfterEitherAsync3() {
2341        CompletableFuture<Integer> f = new CompletableFuture<>();
2342        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2343        FailingNoop r = new FailingNoop();
2344        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2345        f.complete(one);
2346        checkCompletedWithWrappedCFException(g);
2347    }
2348
2349    /**
2350     * runAfterEitherAsync result completes exceptionally if either
2351     * source cancelled
2352     */
2353    public void testRunAfterEitherAsync4() {
2354        CompletableFuture<Integer> f = new CompletableFuture<>();
2355        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356        Noop r = new Noop();
2357        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2358        assertTrue(f.cancel(true));
2359        checkCompletedWithWrappedCancellationException(g);
2360
2361        r = new Noop();
2362        f = new CompletableFuture<>();
2363        f2 = new CompletableFuture<>();
2364        assertTrue(f2.cancel(true));
2365        g = f.runAfterEitherAsync(f2, r);
2366        checkCompletedWithWrappedCancellationException(g);
2367    }
2368
2369    /**
2370     * thenComposeAsync result completes normally after normal
2371     * completion of source
2372     */
2373    public void testThenComposeAsync() {
2374        CompletableFuture<Integer> f, g;
2375        CompletableFutureInc r;
2376
2377        f = new CompletableFuture<>();
2378        g = f.thenComposeAsync(r = new CompletableFutureInc());
2379        f.complete(one);
2380        checkCompletedNormally(g, two);
2381
2382        f = new CompletableFuture<>();
2383        f.complete(one);
2384        g = f.thenComposeAsync(r = new CompletableFutureInc());
2385        checkCompletedNormally(g, two);
2386    }
2387
2388    /**
2389     * thenComposeAsync result completes exceptionally after
2390     * exceptional completion of source
2391     */
2392    public void testThenComposeAsync2() {
2393        CompletableFuture<Integer> f, g;
2394        CompletableFutureInc r;
2395
2396        f = new CompletableFuture<>();
2397        g = f.thenComposeAsync(r = new CompletableFutureInc());
2398        f.completeExceptionally(new CFException());
2399        checkCompletedWithWrappedCFException(g);
2400        assertFalse(r.ran);
2401
2402        f = new CompletableFuture<>();
2403        f.completeExceptionally(new CFException());
2404        g = f.thenComposeAsync(r = new CompletableFutureInc());
2405        checkCompletedWithWrappedCFException(g);
2406        assertFalse(r.ran);
2407    }
2408
2409    /**
2410     * thenComposeAsync result completes exceptionally if action does
2411     */
2412    public void testThenComposeAsync3() {
2413        CompletableFuture<Integer> f, g;
2414        FailingCompletableFutureFunction r;
2415
2416        f = new CompletableFuture<>();
2417        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2418        f.complete(one);
2419        checkCompletedWithWrappedCFException(g);
2420
2421        f = new CompletableFuture<>();
2422        f.complete(one);
2423        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2424        checkCompletedWithWrappedCFException(g);
2425    }
2426
2427    /**
2428     * thenComposeAsync result completes exceptionally if source cancelled
2429     */
2430    public void testThenComposeAsync4() {
2431        CompletableFuture<Integer> f, g;
2432        CompletableFutureInc r;
2433
2434        f = new CompletableFuture<>();
2435        g = f.thenComposeAsync(r = new CompletableFutureInc());
2436        assertTrue(f.cancel(true));
2437        checkCompletedWithWrappedCancellationException(g);
2438
2439        f = new CompletableFuture<>();
2440        assertTrue(f.cancel(true));
2441        g = f.thenComposeAsync(r = new CompletableFutureInc());
2442        checkCompletedWithWrappedCancellationException(g);
2443    }
2444
3183      // async with explicit executors
3184  
3185      /**
# Line 2552 | Line 3290 | public class CompletableFutureTest exten
3290          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3291          f.complete(one);
3292          checkCompletedNormally(g, null);
3293 <        assertEquals(r.value, 2);
3293 >        assertEquals(r.value, (Integer) 2);
3294      }
3295  
3296      /**
# Line 2589 | Line 3327 | public class CompletableFutureTest exten
3327          checkCompletedWithWrappedCancellationException(g);
3328      }
3329  
2592    /**
2593     * applyToEitherAsync result completes normally after normal
2594     * completion of sources
2595     */
2596    public void testApplyToEitherAsyncE() {
2597        CompletableFuture<Integer> f = new CompletableFuture<>();
2598        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2599        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2600        f.complete(one);
2601        checkCompletedNormally(g, two);
2602
2603        f = new CompletableFuture<>();
2604        f.complete(one);
2605        f2 = new CompletableFuture<>();
2606        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2607        checkCompletedNormally(g, two);
2608    }
2609
2610    /**
2611     * applyToEitherAsync result completes exceptionally after exceptional
2612     * completion of source
2613     */
2614    public void testApplyToEitherAsync2E() {
2615        CompletableFuture<Integer> f = new CompletableFuture<>();
2616        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2617        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618        f.completeExceptionally(new CFException());
2619        checkCompletedWithWrappedCFException(g);
2620
2621        f = new CompletableFuture<>();
2622        f2 = new CompletableFuture<>();
2623        f2.completeExceptionally(new CFException());
2624        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625        f.complete(one);
2626        checkCompletedWithWrappedCFException(g);
2627    }
2628
2629    /**
2630     * applyToEitherAsync result completes exceptionally if action does
2631     */
2632    public void testApplyToEitherAsync3E() {
2633        CompletableFuture<Integer> f = new CompletableFuture<>();
2634        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635        FailingFunction r = new FailingFunction();
2636        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2637        f.complete(one);
2638        checkCompletedWithWrappedCFException(g);
2639    }
2640
2641    /**
2642     * applyToEitherAsync result completes exceptionally if either source cancelled
2643     */
2644    public void testApplyToEitherAsync4E() {
2645        CompletableFuture<Integer> f = new CompletableFuture<>();
2646        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2647        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2648        assertTrue(f.cancel(true));
2649        checkCompletedWithWrappedCancellationException(g);
2650
2651        f = new CompletableFuture<>();
2652        f2 = new CompletableFuture<>();
2653        assertTrue(f2.cancel(true));
2654        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2655        checkCompletedWithWrappedCancellationException(g);
2656    }
2657
2658    /**
2659     * acceptEitherAsync result completes normally after normal
2660     * completion of sources
2661     */
2662    public void testAcceptEitherAsyncE() {
2663        CompletableFuture<Integer> f = new CompletableFuture<>();
2664        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2665        IncAction r = new IncAction();
2666        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2667        f.complete(one);
2668        checkCompletedNormally(g, null);
2669        assertEquals(r.value, 2);
2670
2671        r = new IncAction();
2672        f = new CompletableFuture<>();
2673        f.complete(one);
2674        f2 = new CompletableFuture<>();
2675        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2676        checkCompletedNormally(g, null);
2677        assertEquals(r.value, 2);
2678    }
2679
2680    /**
2681     * acceptEitherAsync result completes exceptionally after exceptional
2682     * completion of source
2683     */
2684    public void testAcceptEitherAsync2E() {
2685        CompletableFuture<Integer> f = new CompletableFuture<>();
2686        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2687        IncAction r = new IncAction();
2688        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2689        f.completeExceptionally(new CFException());
2690        checkCompletedWithWrappedCFException(g);
2691
2692        r = new IncAction();
2693        f = new CompletableFuture<>();
2694        f2 = new CompletableFuture<>();
2695        f2.completeExceptionally(new CFException());
2696        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2697        f.complete(one);
2698        checkCompletedWithWrappedCFException(g);
2699    }
2700
2701    /**
2702     * acceptEitherAsync result completes exceptionally if action does
2703     */
2704    public void testAcceptEitherAsync3E() {
2705        CompletableFuture<Integer> f = new CompletableFuture<>();
2706        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2707        FailingConsumer r = new FailingConsumer();
2708        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2709        f.complete(one);
2710        checkCompletedWithWrappedCFException(g);
2711    }
2712
2713    /**
2714     * acceptEitherAsync result completes exceptionally if either
2715     * source cancelled
2716     */
2717    public void testAcceptEitherAsync4E() {
2718        CompletableFuture<Integer> f = new CompletableFuture<>();
2719        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2720        IncAction r = new IncAction();
2721        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722        assertTrue(f.cancel(true));
2723        checkCompletedWithWrappedCancellationException(g);
2724
2725        r = new IncAction();
2726        f = new CompletableFuture<>();
2727        f2 = new CompletableFuture<>();
2728        assertTrue(f2.cancel(true));
2729        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2730        checkCompletedWithWrappedCancellationException(g);
2731    }
2732
2733    /**
2734     * runAfterEitherAsync result completes normally after normal
2735     * completion of sources
2736     */
2737    public void testRunAfterEitherAsyncE() {
2738        CompletableFuture<Integer> f = new CompletableFuture<>();
2739        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740        Noop r = new Noop();
2741        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2742        f.complete(one);
2743        checkCompletedNormally(g, null);
2744        assertTrue(r.ran);
2745
2746        r = new Noop();
2747        f = new CompletableFuture<>();
2748        f.complete(one);
2749        f2 = new CompletableFuture<>();
2750        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2751        checkCompletedNormally(g, null);
2752        assertTrue(r.ran);
2753    }
2754
2755    /**
2756     * runAfterEitherAsync result completes exceptionally after exceptional
2757     * completion of source
2758     */
2759    public void testRunAfterEitherAsync2E() {
2760        CompletableFuture<Integer> f = new CompletableFuture<>();
2761        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2762        Noop r = new Noop();
2763        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2764        f.completeExceptionally(new CFException());
2765        checkCompletedWithWrappedCFException(g);
2766
2767        r = new Noop();
2768        f = new CompletableFuture<>();
2769        f2 = new CompletableFuture<>();
2770        f2.completeExceptionally(new CFException());
2771        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2772        f.complete(one);
2773        checkCompletedWithWrappedCFException(g);
2774    }
2775
2776    /**
2777     * runAfterEitherAsync result completes exceptionally if action does
2778     */
2779    public void testRunAfterEitherAsync3E() {
2780        CompletableFuture<Integer> f = new CompletableFuture<>();
2781        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2782        FailingNoop r = new FailingNoop();
2783        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2784        f.complete(one);
2785        checkCompletedWithWrappedCFException(g);
2786    }
2787
2788    /**
2789     * runAfterEitherAsync result completes exceptionally if either
2790     * source cancelled
2791     */
2792    public void testRunAfterEitherAsync4E() {
2793        CompletableFuture<Integer> f = new CompletableFuture<>();
2794        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2795        Noop r = new Noop();
2796        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797        assertTrue(f.cancel(true));
2798        checkCompletedWithWrappedCancellationException(g);
2799
2800        r = new Noop();
2801        f = new CompletableFuture<>();
2802        f2 = new CompletableFuture<>();
2803        assertTrue(f2.cancel(true));
2804        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2805        checkCompletedWithWrappedCancellationException(g);
2806    }
2807
2808    /**
2809     * thenComposeAsync result completes normally after normal
2810     * completion of source
2811     */
2812    public void testThenComposeAsyncE() {
2813        CompletableFuture<Integer> f = new CompletableFuture<>();
2814        CompletableFutureInc r = new CompletableFutureInc();
2815        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2816        f.complete(one);
2817        checkCompletedNormally(g, two);
2818    }
2819
2820    /**
2821     * thenComposeAsync result completes exceptionally after
2822     * exceptional completion of source
2823     */
2824    public void testThenComposeAsync2E() {
2825        CompletableFuture<Integer> f = new CompletableFuture<>();
2826        CompletableFutureInc r = new CompletableFutureInc();
2827        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2828        f.completeExceptionally(new CFException());
2829        checkCompletedWithWrappedCFException(g);
2830    }
2831
2832    /**
2833     * thenComposeAsync result completes exceptionally if action does
2834     */
2835    public void testThenComposeAsync3E() {
2836        CompletableFuture<Integer> f = new CompletableFuture<>();
2837        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2838        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2839        f.complete(one);
2840        checkCompletedWithWrappedCFException(g);
2841    }
2842
2843    /**
2844     * thenComposeAsync result completes exceptionally if source cancelled
2845     */
2846    public void testThenComposeAsync4E() {
2847        CompletableFuture<Integer> f = new CompletableFuture<>();
2848        CompletableFutureInc r = new CompletableFutureInc();
2849        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2850        assertTrue(f.cancel(true));
2851        checkCompletedWithWrappedCancellationException(g);
2852    }
2853
3330      // other static methods
3331  
3332      /**
# Line 3049 | Line 3525 | public class CompletableFutureTest exten
3525       * whenComplete action executes on normal completion, propagating
3526       * source result.
3527       */
3528 <    public void testWhenComplete1() {
3529 <        final AtomicInteger a = new AtomicInteger();
3530 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3531 <        CompletableFuture<Integer> g =
3532 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3533 <        f.complete(three);
3534 <        checkCompletedNormally(f, three);
3535 <        checkCompletedNormally(g, three);
3536 <        assertEquals(a.get(), 1);
3537 <    }
3538 <
3539 <    /**
3540 <     * whenComplete action executes on exceptional completion, propagating
3541 <     * source result.
3542 <     */
3543 <    public void testWhenComplete2() {
3068 <        final AtomicInteger a = new AtomicInteger();
3069 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3070 <        CompletableFuture<Integer> g =
3071 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3072 <        f.completeExceptionally(new CFException());
3073 <        assertTrue(f.isCompletedExceptionally());
3074 <        assertTrue(g.isCompletedExceptionally());
3075 <        assertEquals(a.get(), 1);
3076 <    }
3077 <
3078 <    /**
3079 <     * If a whenComplete action throws an exception when triggered by
3080 <     * a normal completion, it completes exceptionally
3081 <     */
3082 <    public void testWhenComplete3() {
3083 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3084 <        CompletableFuture<Integer> g =
3085 <            f.whenComplete((Integer x, Throwable t) ->
3086 <                           { throw new CFException(); } );
3087 <        f.complete(three);
3088 <        checkCompletedNormally(f, three);
3089 <        assertTrue(g.isCompletedExceptionally());
3090 <        checkCompletedWithWrappedCFException(g);
3091 <    }
3092 <
3093 <    /**
3094 <     * whenCompleteAsync action executes on normal completion, propagating
3095 <     * source result.
3096 <     */
3097 <    public void testWhenCompleteAsync1() {
3098 <        final AtomicInteger a = new AtomicInteger();
3099 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3100 <        CompletableFuture<Integer> g =
3101 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3102 <        f.complete(three);
3103 <        checkCompletedNormally(f, three);
3104 <        checkCompletedNormally(g, three);
3105 <        assertEquals(a.get(), 1);
3106 <    }
3107 <
3108 <    /**
3109 <     * whenCompleteAsync action executes on exceptional completion, propagating
3110 <     * source result.
3111 <     */
3112 <    public void testWhenCompleteAsync2() {
3113 <        final AtomicInteger a = new AtomicInteger();
3114 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3115 <        CompletableFuture<Integer> g =
3116 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3117 <        f.completeExceptionally(new CFException());
3118 <        checkCompletedWithWrappedCFException(f);
3119 <        checkCompletedWithWrappedCFException(g);
3120 <    }
3528 >    public void testWhenComplete_normalCompletion1() {
3529 >        for (ExecutionMode m : ExecutionMode.values())
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 >        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 <    /**
3546 <     * If a whenCompleteAsync action throws an exception when
3547 <     * triggered by a normal completion, it completes exceptionally
3548 <     */
3126 <    public void testWhenCompleteAsync3() {
3127 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3128 <        CompletableFuture<Integer> g =
3129 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3130 <                           { throw new CFException(); } );
3131 <        f.complete(three);
3132 <        checkCompletedNormally(f, three);
3133 <        checkCompletedWithWrappedCFException(g);
3134 <    }
3545 >        checkCompletedNormally(g, v1);
3546 >        checkCompletedNormally(f, v1);
3547 >        assertEquals(1, a.get());
3548 >    }}
3549  
3550      /**
3551 <     * whenCompleteAsync action executes on normal completion, propagating
3551 >     * whenComplete action executes on exceptional completion, propagating
3552       * source result.
3553       */
3554 <    public void testWhenCompleteAsync1e() {
3555 <        final AtomicInteger a = new AtomicInteger();
3556 <        ThreadExecutor exec = new ThreadExecutor();
3557 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3558 <        CompletableFuture<Integer> g =
3559 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3560 <                                exec);
3561 <        f.complete(three);
3562 <        checkCompletedNormally(f, three);
3563 <        checkCompletedNormally(g, three);
3564 <        assertEquals(a.get(), 1);
3565 <    }
3554 >    public void testWhenComplete_exceptionalCompletion() {
3555 >        for (ExecutionMode m : ExecutionMode.values())
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) -> {
3566 >                threadAssertNull(x);
3567 >                threadAssertSame(t, ex);
3568 >                a.getAndIncrement();
3569 >            });
3570 >        if (createIncomplete) f.completeExceptionally(ex);
3571 >        checkCompletedWithWrappedCFException(f, ex);
3572 >        checkCompletedWithWrappedCFException(g, ex);
3573 >        assertEquals(1, a.get());
3574 >    }}
3575  
3576      /**
3577 <     * whenCompleteAsync action executes on exceptional completion, propagating
3578 <     * source result.
3577 >     * whenComplete action executes on cancelled source, propagating
3578 >     * CancellationException.
3579       */
3580 <    public void testWhenCompleteAsync2e() {
3581 <        final AtomicInteger a = new AtomicInteger();
3582 <        ThreadExecutor exec = new ThreadExecutor();
3583 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3584 <        CompletableFuture<Integer> g =
3585 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3586 <                                exec);
3587 <        f.completeExceptionally(new CFException());
3588 <        checkCompletedWithWrappedCFException(f);
3589 <        checkCompletedWithWrappedCFException(g);
3590 <    }
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 <    /**
3598 <     * If a whenCompleteAsync action throws an exception when triggered
3599 <     * by a normal completion, it completes exceptionally
3600 <     */
3601 <    public void testWhenCompleteAsync3e() {
3174 <        ThreadExecutor exec = new ThreadExecutor();
3175 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3176 <        CompletableFuture<Integer> g =
3177 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3178 <                                { throw new CFException(); },
3179 <                                exec);
3180 <        f.complete(three);
3181 <        checkCompletedNormally(f, three);
3182 <        checkCompletedWithWrappedCFException(g);
3183 <    }
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 <     * handleAsync action completes normally with function value on
3605 <     * either normal or exceptional completion of source
3604 >     * If a whenComplete action throws an exception when triggered by
3605 >     * a normal completion, it completes exceptionally
3606       */
3607 <    public void testHandleAsync() {
3608 <        CompletableFuture<Integer> f, g;
3609 <        IntegerHandler r;
3610 <
3193 <        f = new CompletableFuture<>();
3194 <        g = f.handleAsync(r = new IntegerHandler());
3195 <        assertFalse(r.ran);
3196 <        f.completeExceptionally(new CFException());
3197 <        checkCompletedWithWrappedCFException(f);
3198 <        checkCompletedNormally(g, three);
3199 <        assertTrue(r.ran);
3200 <
3201 <        f = new CompletableFuture<>();
3202 <        g = f.handleAsync(r = new IntegerHandler());
3203 <        assertFalse(r.ran);
3204 <        f.completeExceptionally(new CFException());
3205 <        checkCompletedWithWrappedCFException(f);
3206 <        checkCompletedNormally(g, three);
3207 <        assertTrue(r.ran);
3208 <
3209 <        f = new CompletableFuture<>();
3210 <        g = f.handleAsync(r = new IntegerHandler());
3211 <        assertFalse(r.ran);
3212 <        f.complete(one);
3213 <        checkCompletedNormally(f, one);
3214 <        checkCompletedNormally(g, two);
3215 <        assertTrue(r.ran);
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 <        f = new CompletableFuture<>();
3613 <        g = f.handleAsync(r = new IntegerHandler());
3614 <        assertFalse(r.ran);
3615 <        f.complete(one);
3616 <        checkCompletedNormally(f, one);
3617 <        checkCompletedNormally(g, two);
3618 <        assertTrue(r.ran);
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 >        if (createIncomplete) f.complete(v1);
3625 >        checkCompletedNormally(f, v1);
3626 >        checkCompletedWithWrappedCFException(g, ex);
3627 >        assertEquals(1, a.get());
3628 >        }
3629      }
3630  
3631      /**
3632 <     * handleAsync action with Executor completes normally with
3633 <     * function value on either normal or exceptional completion of
3634 <     * source
3632 >     * If a whenComplete action throws an exception when triggered by
3633 >     * a source completion that also throws an exception, the source
3634 >     * exception takes precedence.
3635       */
3636 <    public void testHandleAsync2() {
3637 <        CompletableFuture<Integer> f, g;
3638 <        ThreadExecutor exec = new ThreadExecutor();
3639 <        IntegerHandler r;
3235 <
3236 <        f = new CompletableFuture<>();
3237 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3238 <        assertFalse(r.ran);
3239 <        f.completeExceptionally(new CFException());
3240 <        checkCompletedWithWrappedCFException(f);
3241 <        checkCompletedNormally(g, three);
3242 <        assertTrue(r.ran);
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 <        f = new CompletableFuture<>();
3642 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3643 <        assertFalse(r.ran);
3644 <        f.completeExceptionally(new CFException());
3248 <        checkCompletedWithWrappedCFException(f);
3249 <        checkCompletedNormally(g, three);
3250 <        assertTrue(r.ran);
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 <        f = new CompletableFuture<>();
3647 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3648 <        assertFalse(r.ran);
3649 <        f.complete(one);
3650 <        checkCompletedNormally(f, one);
3651 <        checkCompletedNormally(g, two);
3652 <        assertTrue(r.ran);
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 >        if (createIncomplete) f.completeExceptionally(ex1);
3656  
3657 <        f = new CompletableFuture<>();
3658 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3659 <        assertFalse(r.ran);
3660 <        f.complete(one);
3264 <        checkCompletedNormally(f, one);
3265 <        checkCompletedNormally(g, two);
3266 <        assertTrue(r.ran);
3657 >        checkCompletedWithWrappedCFException(f, ex1);
3658 >        checkCompletedWithWrappedCFException(g, ex1);
3659 >        assertEquals(1, a.get());
3660 >        }
3661      }
3662  
3663   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines