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.35 by jsr166, Sun Jun 1 22:22:49 2014 UTC vs.
Revision 1.44 by jsr166, Mon Jun 2 06:06:30 2014 UTC

# Line 318 | Line 318 | public class CompletableFutureTest exten
318  
319      // Choose non-commutative actions for better coverage
320  
321 +    // A non-commutative function that handles and produces null values as well.
322 +    static Integer subtract(Integer x, Integer y) {
323 +        return (x == null && y == null) ? null :
324 +            ((x == null) ? 42 : x.intValue())
325 +            - ((y == null) ? 99 : y.intValue());
326 +    }
327 +
328 +    // A function that handles and produces null values as well.
329 +    static Integer inc(Integer x) {
330 +        return (x == null) ? null : x + 1;
331 +    }
332 +
333      static final Supplier<Integer> supplyOne =
334          () -> Integer.valueOf(1);
335      static final Function<Integer, Integer> inc =
336          (Integer x) -> Integer.valueOf(x.intValue() + 1);
337      static final BiFunction<Integer, Integer, Integer> subtract =
338 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
338 >        (Integer x, Integer y) -> subtract(x, y);
339      static final class IncAction implements Consumer<Integer> {
340 <        int value;
341 <        public void accept(Integer x) { value = x.intValue() + 1; }
340 >        int invocationCount = 0;
341 >        Integer value;
342 >        public void accept(Integer x) {
343 >            invocationCount++;
344 >            value = inc(x);
345 >        }
346      }
347 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
348 <        // Handle null values as well
349 <        public int subtract(Integer x, Integer y) {
350 <            return ((x == null) ? 42 : x.intValue())
351 <                - ((y == null) ? 99 : y.intValue());
347 >    static final class IncFunction implements Function<Integer,Integer> {
348 >        int invocationCount = 0;
349 >        Integer value;
350 >        public Integer apply(Integer x) {
351 >            invocationCount++;
352 >            return value = inc(x);
353          }
354 <        int value;
355 <        public boolean ran() { return value != 0; }
354 >    }
355 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
356 >        int invocationCount = 0;
357 >        Integer value;
358 >        // Check this action was invoked exactly once when result is computed.
359          public void accept(Integer x, Integer y) {
360 +            invocationCount++;
361              value = subtract(x, y);
362          }
363      }
364 +    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
365 +        int invocationCount = 0;
366 +        Integer value;
367 +        // Check this action was invoked exactly once when result is computed.
368 +        public Integer apply(Integer x, Integer y) {
369 +            invocationCount++;
370 +            return value = subtract(x, y);
371 +        }
372 +    }
373      static final class Noop implements Runnable {
374 <        boolean ran;
375 <        public void run() { ran = true; }
374 >        int invocationCount = 0;
375 >        public void run() {
376 >            invocationCount++;
377 >        }
378      }
379  
380      static final class FailingSupplier implements Supplier<Integer> {
381 <        boolean ran;
382 <        public Integer get() { ran = true; throw new CFException(); }
381 >        int invocationCount = 0;
382 >        public Integer get() {
383 >            invocationCount++;
384 >            throw new CFException();
385 >        }
386      }
387      static final class FailingConsumer implements Consumer<Integer> {
388 <        boolean ran;
389 <        public void accept(Integer x) { ran = true; throw new CFException(); }
388 >        int invocationCount = 0;
389 >        public void accept(Integer x) {
390 >            invocationCount++;
391 >            throw new CFException();
392 >        }
393      }
394      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
395 <        boolean ran;
396 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
395 >        int invocationCount = 0;
396 >        public void accept(Integer x, Integer y) {
397 >            invocationCount++;
398 >            throw new CFException();
399 >        }
400      }
401      static final class FailingFunction implements Function<Integer, Integer> {
402 <        boolean ran;
403 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
402 >        int invocationCount = 0;
403 >        public Integer apply(Integer x) {
404 >            invocationCount++;
405 >            throw new CFException();
406 >        }
407      }
408      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
409 <        boolean ran;
410 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
409 >        int invocationCount = 0;
410 >        public Integer apply(Integer x, Integer y) {
411 >            invocationCount++;
412 >            throw new CFException();
413 >        }
414      }
415      static final class FailingNoop implements Runnable {
416 <        boolean ran;
417 <        public void run() { ran = true; throw new CFException(); }
416 >        int invocationCount = 0;
417 >        public void run() {
418 >            invocationCount++;
419 >            throw new CFException();
420 >        }
421      }
422  
423      static final class CompletableFutureInc
424          implements Function<Integer, CompletableFuture<Integer>> {
425 <        boolean ran;
425 >        int invocationCount = 0;
426          public CompletableFuture<Integer> apply(Integer x) {
427 <            ran = true;
427 >            invocationCount++;
428              CompletableFuture<Integer> f = new CompletableFuture<>();
429 <            f.complete(Integer.valueOf(x.intValue() + 1));
429 >            f.complete(inc(x));
430              return f;
431          }
432      }
433  
434      static final class FailingCompletableFutureFunction
435          implements Function<Integer, CompletableFuture<Integer>> {
436 <        boolean ran;
436 >        int invocationCount = 0;
437          public CompletableFuture<Integer> apply(Integer x) {
438 <            ran = true; throw new CFException();
438 >            invocationCount++;
439 >            throw new CFException();
440          }
441      }
442  
# Line 404 | Line 455 | public class CompletableFutureTest exten
455      }
456  
457      static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
458 <        boolean ran;
458 >        int invocationCount = 0;
459          public Integer apply(Integer x, Throwable t) {
460 <            ran = true;
460 >            invocationCount++;
461              return (t == null) ? two : three;
462          }
463      }
# Line 427 | Line 478 | public class CompletableFutureTest exten
478                   BiConsumer<? super T,? super U> a) {
479                  return f.thenAcceptBoth(g, a);
480              }
481 +            public <T,U,V> CompletableFuture<V> thenCombine
482 +                (CompletableFuture<T> f,
483 +                 CompletionStage<? extends U> g,
484 +                 BiFunction<? super T,? super U,? extends V> a) {
485 +                return f.thenCombine(g, a);
486 +            }
487 +            public <T,U> CompletableFuture<U> applyToEither
488 +                (CompletableFuture<T> f,
489 +                 CompletionStage<? extends T> g,
490 +                 Function<? super T,U> a) {
491 +                return f.applyToEither(g, a);
492 +            }
493 +            public <T> CompletableFuture<Void> acceptEither
494 +                (CompletableFuture<T> f,
495 +                 CompletionStage<? extends T> g,
496 +                 Consumer<? super T> a) {
497 +                return f.acceptEither(g, a);
498 +            }
499 +            public <T> CompletableFuture<Void> runAfterEither
500 +                (CompletableFuture<T> f,
501 +                 CompletionStage<?> g,
502 +                 java.lang.Runnable a) {
503 +                return f.runAfterEither(g, a);
504 +            }
505 +            public <T,U> CompletableFuture<U> thenCompose
506 +                (CompletableFuture<T> f,
507 +                 Function<? super T,? extends CompletionStage<U>> a) {
508 +                return f.thenCompose(a);
509 +            }
510 +            public <T> CompletableFuture<T> whenComplete
511 +                (CompletableFuture<T> f,
512 +                 BiConsumer<? super T,? super Throwable> a) {
513 +                return f.whenComplete(a);
514 +            }
515          },
516  
517   //             /** Experimental way to do more testing */
# Line 454 | Line 539 | public class CompletableFutureTest exten
539                   BiConsumer<? super T,? super U> a) {
540                  return f.thenAcceptBothAsync(g, a);
541              }
542 +            public <T,U,V> CompletableFuture<V> thenCombine
543 +                (CompletableFuture<T> f,
544 +                 CompletionStage<? extends U> g,
545 +                 BiFunction<? super T,? super U,? extends V> a) {
546 +                return f.thenCombineAsync(g, a);
547 +            }
548 +            public <T,U> CompletableFuture<U> applyToEither
549 +                (CompletableFuture<T> f,
550 +                 CompletionStage<? extends T> g,
551 +                 Function<? super T,U> a) {
552 +                return f.applyToEitherAsync(g, a);
553 +            }
554 +            public <T> CompletableFuture<Void> acceptEither
555 +                (CompletableFuture<T> f,
556 +                 CompletionStage<? extends T> g,
557 +                 Consumer<? super T> a) {
558 +                return f.acceptEitherAsync(g, a);
559 +            }
560 +            public <T> CompletableFuture<Void> runAfterEither
561 +                (CompletableFuture<T> f,
562 +                 CompletionStage<?> g,
563 +                 java.lang.Runnable a) {
564 +                return f.runAfterEitherAsync(g, a);
565 +            }
566 +            public <T,U> CompletableFuture<U> thenCompose
567 +                (CompletableFuture<T> f,
568 +                 Function<? super T,? extends CompletionStage<U>> a) {
569 +                return f.thenComposeAsync(a);
570 +            }
571 +            public <T> CompletableFuture<T> whenComplete
572 +                (CompletableFuture<T> f,
573 +                 BiConsumer<? super T,? super Throwable> a) {
574 +                return f.whenCompleteAsync(a);
575 +            }
576          },
577  
578   //         REVERSE_DEFAULT_ASYNC {
# Line 480 | Line 599 | public class CompletableFutureTest exten
599                   BiConsumer<? super T,? super U> a) {
600                  return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
601              }
602 +            public <T,U,V> CompletableFuture<V> thenCombine
603 +                (CompletableFuture<T> f,
604 +                 CompletionStage<? extends U> g,
605 +                 BiFunction<? super T,? super U,? extends V> a) {
606 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
607 +            }
608 +            public <T,U> CompletableFuture<U> applyToEither
609 +                (CompletableFuture<T> f,
610 +                 CompletionStage<? extends T> g,
611 +                 Function<? super T,U> a) {
612 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
613 +            }
614 +            public <T> CompletableFuture<Void> acceptEither
615 +                (CompletableFuture<T> f,
616 +                 CompletionStage<? extends T> g,
617 +                 Consumer<? super T> a) {
618 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
619 +            }
620 +            public <T> CompletableFuture<Void> runAfterEither
621 +                (CompletableFuture<T> f,
622 +                 CompletionStage<?> g,
623 +                 java.lang.Runnable a) {
624 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
625 +            }
626 +            public <T,U> CompletableFuture<U> thenCompose
627 +                (CompletableFuture<T> f,
628 +                 Function<? super T,? extends CompletionStage<U>> a) {
629 +                return f.thenComposeAsync(a, new ThreadExecutor());
630 +            }
631 +            public <T> CompletableFuture<T> whenComplete
632 +                (CompletableFuture<T> f,
633 +                 BiConsumer<? super T,? super Throwable> a) {
634 +                return f.whenCompleteAsync(a, new ThreadExecutor());
635 +            }
636          };
637  
638          public abstract <T,U> CompletableFuture<Void> runAfterBoth
# Line 488 | Line 641 | public class CompletableFutureTest exten
641              (CompletableFuture<T> f,
642               CompletionStage<? extends U> g,
643               BiConsumer<? super T,? super U> a);
644 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
645 +            (CompletableFuture<T> f,
646 +             CompletionStage<? extends U> g,
647 +             BiFunction<? super T,? super U,? extends V> a);
648 +        public abstract <T,U> CompletableFuture<U> applyToEither
649 +            (CompletableFuture<T> f,
650 +             CompletionStage<? extends T> g,
651 +             Function<? super T,U> a);
652 +        public abstract <T> CompletableFuture<Void> acceptEither
653 +            (CompletableFuture<T> f,
654 +             CompletionStage<? extends T> g,
655 +             Consumer<? super T> a);
656 +        public abstract <T> CompletableFuture<Void> runAfterEither
657 +            (CompletableFuture<T> f,
658 +             CompletionStage<?> g,
659 +             java.lang.Runnable a);
660 +        public abstract <T,U> CompletableFuture<U> thenCompose
661 +            (CompletableFuture<T> f,
662 +             Function<? super T,? extends CompletionStage<U>> a);
663 +        public abstract <T> CompletableFuture<T> whenComplete
664 +            (CompletableFuture<T> f,
665 +             BiConsumer<? super T,? super Throwable> a);
666 +
667 +
668      }
669  
670      /**
# Line 519 | Line 696 | public class CompletableFutureTest exten
696          f = new CompletableFuture<>();
697          f.completeExceptionally(new CFException());
698          g = f.handle(r = new IntegerHandler());
699 <        assertTrue(r.ran);
699 >        assertEquals(1, r.invocationCount);
700 >        assertEquals(1, r.invocationCount);
701          checkCompletedNormally(g, three);
702  
703          f = new CompletableFuture<>();
704          g = f.handle(r = new IntegerHandler());
705 <        assertFalse(r.ran);
705 >        assertEquals(0, r.invocationCount);
706          f.completeExceptionally(new CFException());
707          checkCompletedNormally(g, three);
708 <        assertTrue(r.ran);
708 >        assertEquals(1, r.invocationCount);
709  
710          f = new CompletableFuture<>();
711          f.complete(one);
712          g = f.handle(r = new IntegerHandler());
713 <        assertTrue(r.ran);
713 >        assertEquals(1, r.invocationCount);
714          checkCompletedNormally(g, two);
715  
716          f = new CompletableFuture<>();
717          g = f.handle(r = new IntegerHandler());
718 <        assertFalse(r.ran);
718 >        assertEquals(0, r.invocationCount);
719          f.complete(one);
720 <        assertTrue(r.ran);
720 >        assertEquals(1, r.invocationCount);
721          checkCompletedNormally(g, two);
722      }
723  
# Line 550 | Line 728 | public class CompletableFutureTest exten
728          Noop r = new Noop();
729          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
730          assertNull(f.join());
731 <        assertTrue(r.ran);
731 >        assertEquals(1, r.invocationCount);
732          checkCompletedNormally(f, null);
733      }
734  
# Line 562 | Line 740 | public class CompletableFutureTest exten
740          ThreadExecutor exec = new ThreadExecutor();
741          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
742          assertNull(f.join());
743 <        assertTrue(r.ran);
743 >        assertEquals(1, r.invocationCount);
744          checkCompletedNormally(f, null);
745          assertEquals(1, exec.count.get());
746      }
# Line 574 | Line 752 | public class CompletableFutureTest exten
752          FailingNoop r = new FailingNoop();
753          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
754          checkCompletedWithWrappedCFException(f);
755 <        assertTrue(r.ran);
755 >        assertEquals(1, r.invocationCount);
756      }
757  
758      /**
# Line 604 | Line 782 | public class CompletableFutureTest exten
782          FailingSupplier r = new FailingSupplier();
783          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
784          checkCompletedWithWrappedCFException(f);
785 <        assertTrue(r.ran);
785 >        assertEquals(1, r.invocationCount);
786      }
787  
788      // seq completion methods
# Line 621 | Line 799 | public class CompletableFutureTest exten
799          g = f.thenRun(r = new Noop());
800          f.complete(null);
801          checkCompletedNormally(g, null);
802 <        assertTrue(r.ran);
802 >        assertEquals(1, r.invocationCount);
803  
804          f = new CompletableFuture<>();
805          f.complete(null);
806          g = f.thenRun(r = new Noop());
807          checkCompletedNormally(g, null);
808 <        assertTrue(r.ran);
808 >        assertEquals(1, r.invocationCount);
809      }
810  
811      /**
# Line 643 | Line 821 | public class CompletableFutureTest exten
821          g = f.thenRun(r = new Noop());
822          f.completeExceptionally(new CFException());
823          checkCompletedWithWrappedCFException(g);
824 <        assertFalse(r.ran);
824 >        assertEquals(0, r.invocationCount);
825  
826          f = new CompletableFuture<>();
827          f.completeExceptionally(new CFException());
828          g = f.thenRun(r = new Noop());
829          checkCompletedWithWrappedCFException(g);
830 <        assertFalse(r.ran);
830 >        assertEquals(0, r.invocationCount);
831      }
832  
833      /**
# Line 740 | Line 918 | public class CompletableFutureTest exten
918          CompletableFuture<Void> g = f.thenAccept(r);
919          f.complete(one);
920          checkCompletedNormally(g, null);
921 <        assertEquals(r.value, 2);
921 >        assertEquals(r.value, (Integer) 2);
922      }
923  
924      /**
# Line 764 | Line 942 | public class CompletableFutureTest exten
942          CompletableFuture<Void> g = f.thenAccept(r);
943          f.complete(one);
944          checkCompletedWithWrappedCFException(g);
945 <        assertTrue(r.ran);
945 >        assertEquals(1, r.invocationCount);
946      }
947  
948      /**
# Line 782 | Line 960 | public class CompletableFutureTest exten
960       * thenCombine result completes normally after normal completion
961       * of sources
962       */
963 <    public void testThenCombine() {
964 <        CompletableFuture<Integer> f, g, h;
965 <
966 <        f = new CompletableFuture<>();
967 <        g = new CompletableFuture<>();
968 <        h = f.thenCombine(g, subtract);
791 <        f.complete(3);
792 <        checkIncomplete(h);
793 <        g.complete(1);
794 <        checkCompletedNormally(h, 2);
963 >    public void testThenCombine_normalCompletion1() {
964 >        for (boolean createIncomplete : new boolean[] { true, false })
965 >        for (boolean fFirst : new boolean[] { true, false })
966 >        for (ExecutionMode m : ExecutionMode.values())
967 >        for (Integer v1 : new Integer[] { 1, null })
968 >        for (Integer v2 : new Integer[] { 2, null }) {
969  
970 <        f = new CompletableFuture<>();
971 <        g = new CompletableFuture<>();
972 <        h = f.thenCombine(g, subtract);
973 <        g.complete(1);
974 <        checkIncomplete(h);
975 <        f.complete(3);
976 <        checkCompletedNormally(h, 2);
970 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
971 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
972 >        final SubtractFunction r = new SubtractFunction();
973 >        CompletableFuture<Integer> h = null;
974 >        if (createIncomplete) h = m.thenCombine(f, g, r);
975 >
976 >        if (fFirst)
977 >            f.complete(v1);
978 >        else
979 >            g.complete(v2);
980 >        if (createIncomplete) checkIncomplete(h);
981 >        assertEquals(0, r.invocationCount);
982 >        if (!fFirst)
983 >            f.complete(v1);
984 >        else
985 >            g.complete(v2);
986 >        if (!createIncomplete) h = m.thenCombine(f, g, r);
987  
988 <        f = new CompletableFuture<>();
989 <        g = new CompletableFuture<>();
990 <        g.complete(1);
991 <        f.complete(3);
992 <        h = f.thenCombine(g, subtract);
809 <        checkCompletedNormally(h, 2);
988 >        checkCompletedNormally(h, subtract(v1, v2));
989 >        checkCompletedNormally(f, v1);
990 >        checkCompletedNormally(g, v2);
991 >        assertEquals(1, r.invocationCount);
992 >        }
993      }
994  
995      /**
996       * thenCombine result completes exceptionally after exceptional
997       * completion of either source
998       */
999 <    public void testThenCombine2() {
1000 <        CompletableFuture<Integer> f, g, h;
999 >    public void testThenCombine_exceptionalCompletion1() {
1000 >        for (ExecutionMode m : ExecutionMode.values())
1001 >        for (Integer v1 : new Integer[] { 1, null }) {
1002  
1003 <        f = new CompletableFuture<>();
1004 <        g = new CompletableFuture<>();
1005 <        h = f.thenCombine(g, subtract);
1006 <        f.completeExceptionally(new CFException());
1003 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1004 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1005 >        final SubtractFunction r = new SubtractFunction();
1006 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1007 >        final CFException ex = new CFException();
1008 >
1009 >        f.completeExceptionally(ex);
1010          checkIncomplete(h);
1011 <        g.complete(1);
825 <        checkCompletedWithWrappedCFException(h);
1011 >        g.complete(v1);
1012  
1013 <        f = new CompletableFuture<>();
1014 <        g = new CompletableFuture<>();
1015 <        h = f.thenCombine(g, subtract);
1016 <        g.completeExceptionally(new CFException());
1013 >        checkCompletedWithWrappedCFException(h, ex);
1014 >        checkCompletedWithWrappedCFException(f, ex);
1015 >        assertEquals(0, r.invocationCount);
1016 >        checkCompletedNormally(g, v1);
1017 >        }
1018 >    }
1019 >
1020 >    public void testThenCombine_exceptionalCompletion2() {
1021 >        for (ExecutionMode m : ExecutionMode.values())
1022 >        for (Integer v1 : new Integer[] { 1, null }) {
1023 >
1024 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1025 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1026 >        final SubtractFunction r = new SubtractFunction();
1027 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1028 >        final CFException ex = new CFException();
1029 >
1030 >        g.completeExceptionally(ex);
1031          checkIncomplete(h);
1032 <        f.complete(3);
833 <        checkCompletedWithWrappedCFException(h);
1032 >        f.complete(v1);
1033  
1034 <        f = new CompletableFuture<>();
1035 <        g = new CompletableFuture<>();
1036 <        f.complete(3);
1037 <        g.completeExceptionally(new CFException());
1038 <        h = f.thenCombine(g, subtract);
1039 <        checkCompletedWithWrappedCFException(h);
1034 >        checkCompletedWithWrappedCFException(h, ex);
1035 >        checkCompletedWithWrappedCFException(g, ex);
1036 >        assertEquals(0, r.invocationCount);
1037 >        checkCompletedNormally(f, v1);
1038 >        }
1039 >    }
1040  
1041 <        f = new CompletableFuture<>();
1042 <        g = new CompletableFuture<>();
1043 <        f.completeExceptionally(new CFException());
1044 <        g.complete(3);
1045 <        h = f.thenCombine(g, subtract);
1046 <        checkCompletedWithWrappedCFException(h);
1041 >    public void testThenCombine_exceptionalCompletion3() {
1042 >        for (ExecutionMode m : ExecutionMode.values())
1043 >        for (Integer v1 : new Integer[] { 1, null }) {
1044 >
1045 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1046 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1047 >        final SubtractFunction r = new SubtractFunction();
1048 >        final CFException ex = new CFException();
1049 >
1050 >        g.completeExceptionally(ex);
1051 >        f.complete(v1);
1052 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1053 >
1054 >        checkCompletedWithWrappedCFException(h, ex);
1055 >        checkCompletedWithWrappedCFException(g, ex);
1056 >        assertEquals(0, r.invocationCount);
1057 >        checkCompletedNormally(f, v1);
1058 >        }
1059 >    }
1060 >
1061 >    public void testThenCombine_exceptionalCompletion4() {
1062 >        for (ExecutionMode m : ExecutionMode.values())
1063 >        for (Integer v1 : new Integer[] { 1, null }) {
1064 >
1065 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1066 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1067 >        final SubtractFunction r = new SubtractFunction();
1068 >        final CFException ex = new CFException();
1069 >
1070 >        f.completeExceptionally(ex);
1071 >        g.complete(v1);
1072 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1073 >
1074 >        checkCompletedWithWrappedCFException(h, ex);
1075 >        checkCompletedWithWrappedCFException(f, ex);
1076 >        assertEquals(0, r.invocationCount);
1077 >        checkCompletedNormally(g, v1);
1078 >        }
1079      }
1080  
1081      /**
1082       * thenCombine result completes exceptionally if action does
1083       */
1084 <    public void testThenCombine3() {
1085 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1086 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1087 <        FailingBiFunction r = new FailingBiFunction();
1088 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1089 <        f.complete(one);
1090 <        checkIncomplete(g);
1091 <        assertFalse(r.ran);
1092 <        f2.complete(two);
1093 <        checkCompletedWithWrappedCFException(g);
1094 <        assertTrue(r.ran);
1084 >    public void testThenCombine_actionFailed1() {
1085 >        for (ExecutionMode m : ExecutionMode.values())
1086 >        for (Integer v1 : new Integer[] { 1, null })
1087 >        for (Integer v2 : new Integer[] { 2, null }) {
1088 >
1089 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1090 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1091 >        final FailingBiFunction r = new FailingBiFunction();
1092 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1093 >
1094 >        f.complete(v1);
1095 >        checkIncomplete(h);
1096 >        g.complete(v2);
1097 >
1098 >        checkCompletedWithWrappedCFException(h);
1099 >        checkCompletedNormally(f, v1);
1100 >        checkCompletedNormally(g, v2);
1101 >        }
1102 >    }
1103 >
1104 >    public void testThenCombine_actionFailed2() {
1105 >        for (ExecutionMode m : ExecutionMode.values())
1106 >        for (Integer v1 : new Integer[] { 1, null })
1107 >        for (Integer v2 : new Integer[] { 2, null }) {
1108 >
1109 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1110 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1111 >        final FailingBiFunction r = new FailingBiFunction();
1112 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1113 >
1114 >        g.complete(v2);
1115 >        checkIncomplete(h);
1116 >        f.complete(v1);
1117 >
1118 >        checkCompletedWithWrappedCFException(h);
1119 >        checkCompletedNormally(f, v1);
1120 >        checkCompletedNormally(g, v2);
1121 >        }
1122      }
1123  
1124      /**
1125       * thenCombine result completes exceptionally if either source cancelled
1126       */
1127 <    public void testThenCombine4() {
1128 <        CompletableFuture<Integer> f, g, h;
1127 >    public void testThenCombine_sourceCancelled1() {
1128 >        for (ExecutionMode m : ExecutionMode.values())
1129 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1130 >        for (Integer v1 : new Integer[] { 1, null }) {
1131  
1132 <        f = new CompletableFuture<>();
1133 <        g = new CompletableFuture<>();
1134 <        h = f.thenCombine(g, subtract);
1135 <        assertTrue(f.cancel(true));
1132 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1133 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1134 >        final SubtractFunction r = new SubtractFunction();
1135 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1136 >
1137 >        assertTrue(f.cancel(mayInterruptIfRunning));
1138          checkIncomplete(h);
1139 <        g.complete(1);
1139 >        g.complete(v1);
1140 >
1141          checkCompletedWithWrappedCancellationException(h);
1142 +        checkCancelled(f);
1143 +        assertEquals(0, r.invocationCount);
1144 +        checkCompletedNormally(g, v1);
1145 +        }
1146 +    }
1147  
1148 <        f = new CompletableFuture<>();
1149 <        g = new CompletableFuture<>();
1150 <        h = f.thenCombine(g, subtract);
1151 <        assertTrue(g.cancel(true));
1148 >    public void testThenCombine_sourceCancelled2() {
1149 >        for (ExecutionMode m : ExecutionMode.values())
1150 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1151 >        for (Integer v1 : new Integer[] { 1, null }) {
1152 >
1153 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1154 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1155 >        final SubtractFunction r = new SubtractFunction();
1156 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1157 >
1158 >        assertTrue(g.cancel(mayInterruptIfRunning));
1159          checkIncomplete(h);
1160 <        f.complete(3);
1160 >        f.complete(v1);
1161 >
1162          checkCompletedWithWrappedCancellationException(h);
1163 +        checkCancelled(g);
1164 +        assertEquals(0, r.invocationCount);
1165 +        checkCompletedNormally(f, v1);
1166 +        }
1167 +    }
1168 +
1169 +    public void testThenCombine_sourceCancelled3() {
1170 +        for (ExecutionMode m : ExecutionMode.values())
1171 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1172 +        for (Integer v1 : new Integer[] { 1, null }) {
1173 +
1174 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1175 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1176 +        final SubtractFunction r = new SubtractFunction();
1177 +
1178 +        assertTrue(g.cancel(mayInterruptIfRunning));
1179 +        f.complete(v1);
1180 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1181  
888        f = new CompletableFuture<>();
889        g = new CompletableFuture<>();
890        assertTrue(f.cancel(true));
891        assertTrue(g.cancel(true));
892        h = f.thenCombine(g, subtract);
1182          checkCompletedWithWrappedCancellationException(h);
1183 +        checkCancelled(g);
1184 +        assertEquals(0, r.invocationCount);
1185 +        checkCompletedNormally(f, v1);
1186 +        }
1187 +    }
1188 +
1189 +    public void testThenCombine_sourceCancelled4() {
1190 +        for (ExecutionMode m : ExecutionMode.values())
1191 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1192 +        for (Integer v1 : new Integer[] { 1, null }) {
1193 +
1194 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1195 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1196 +        final SubtractFunction r = new SubtractFunction();
1197 +
1198 +        assertTrue(f.cancel(mayInterruptIfRunning));
1199 +        g.complete(v1);
1200 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1201 +
1202 +        checkCompletedWithWrappedCancellationException(h);
1203 +        checkCancelled(f);
1204 +        assertEquals(0, r.invocationCount);
1205 +        checkCompletedNormally(g, v1);
1206 +        }
1207      }
1208  
1209      /**
# Line 909 | Line 1222 | public class CompletableFutureTest exten
1222  
1223          f.complete(v1);
1224          checkIncomplete(h);
1225 <        assertEquals(r.value, 0);
1225 >        assertEquals(0, r.invocationCount);
1226          g.complete(v2);
1227  
1228          checkCompletedNormally(h, null);
1229 <        assertEquals(r.value, r.subtract(v1, v2));
1229 >        assertEquals(r.value, subtract(v1, v2));
1230          checkCompletedNormally(f, v1);
1231          checkCompletedNormally(g, v2);
1232          }
# Line 931 | Line 1244 | public class CompletableFutureTest exten
1244  
1245          g.complete(v2);
1246          checkIncomplete(h);
1247 <        assertEquals(r.value, 0);
1247 >        assertEquals(0, r.invocationCount);
1248          f.complete(v1);
1249  
1250          checkCompletedNormally(h, null);
1251 <        assertEquals(r.value, r.subtract(v1, v2));
1251 >        assertEquals(r.value, subtract(v1, v2));
1252          checkCompletedNormally(f, v1);
1253          checkCompletedNormally(g, v2);
1254          }
# Line 955 | Line 1268 | public class CompletableFutureTest exten
1268          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1269  
1270          checkCompletedNormally(h, null);
1271 <        assertEquals(r.value, r.subtract(v1, v2));
1271 >        assertEquals(r.value, subtract(v1, v2));
1272          checkCompletedNormally(f, v1);
1273          checkCompletedNormally(g, v2);
1274          }
# Line 975 | Line 1288 | public class CompletableFutureTest exten
1288          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1289  
1290          checkCompletedNormally(h, null);
1291 <        assertEquals(r.value, r.subtract(v1, v2));
1291 >        assertEquals(r.value, subtract(v1, v2));
1292          checkCompletedNormally(f, v1);
1293          checkCompletedNormally(g, v2);
1294          }
# Line 1001 | Line 1314 | public class CompletableFutureTest exten
1314  
1315          checkCompletedWithWrappedCFException(h, ex);
1316          checkCompletedWithWrappedCFException(f, ex);
1317 <        assertFalse(r.ran());
1317 >        assertEquals(0, r.invocationCount);
1318          checkCompletedNormally(g, v1);
1319          }
1320      }
# Line 1022 | Line 1335 | public class CompletableFutureTest exten
1335  
1336          checkCompletedWithWrappedCFException(h, ex);
1337          checkCompletedWithWrappedCFException(g, ex);
1338 <        assertFalse(r.ran());
1338 >        assertEquals(0, r.invocationCount);
1339          checkCompletedNormally(f, v1);
1340          }
1341      }
# Line 1042 | Line 1355 | public class CompletableFutureTest exten
1355  
1356          checkCompletedWithWrappedCFException(h, ex);
1357          checkCompletedWithWrappedCFException(g, ex);
1358 <        assertFalse(r.ran());
1358 >        assertEquals(0, r.invocationCount);
1359          checkCompletedNormally(f, v1);
1360          }
1361      }
# Line 1062 | Line 1375 | public class CompletableFutureTest exten
1375  
1376          checkCompletedWithWrappedCFException(h, ex);
1377          checkCompletedWithWrappedCFException(f, ex);
1378 <        assertFalse(r.ran());
1378 >        assertEquals(0, r.invocationCount);
1379          checkCompletedNormally(g, v1);
1380          }
1381      }
# Line 1129 | Line 1442 | public class CompletableFutureTest exten
1442  
1443          checkCompletedWithWrappedCancellationException(h);
1444          checkCancelled(f);
1445 <        assertFalse(r.ran());
1445 >        assertEquals(0, r.invocationCount);
1446          checkCompletedNormally(g, v1);
1447          }
1448      }
# Line 1150 | Line 1463 | public class CompletableFutureTest exten
1463  
1464          checkCompletedWithWrappedCancellationException(h);
1465          checkCancelled(g);
1466 <        assertFalse(r.ran());
1466 >        assertEquals(0, r.invocationCount);
1467          checkCompletedNormally(f, v1);
1468          }
1469      }
# Line 1170 | Line 1483 | public class CompletableFutureTest exten
1483  
1484          checkCompletedWithWrappedCancellationException(h);
1485          checkCancelled(g);
1486 <        assertFalse(r.ran());
1486 >        assertEquals(0, r.invocationCount);
1487          checkCompletedNormally(f, v1);
1488          }
1489      }
# Line 1190 | Line 1503 | public class CompletableFutureTest exten
1503  
1504          checkCompletedWithWrappedCancellationException(h);
1505          checkCancelled(f);
1506 <        assertFalse(r.ran());
1506 >        assertEquals(0, r.invocationCount);
1507          checkCompletedNormally(g, v1);
1508          }
1509      }
# Line 1211 | Line 1524 | public class CompletableFutureTest exten
1524  
1525          f.complete(v1);
1526          checkIncomplete(h);
1527 <        assertFalse(r.ran);
1527 >        assertEquals(0, r.invocationCount);
1528          g.complete(v2);
1529  
1530          checkCompletedNormally(h, null);
1531 <        assertTrue(r.ran);
1531 >        assertEquals(1, r.invocationCount);
1532          checkCompletedNormally(f, v1);
1533          checkCompletedNormally(g, v2);
1534          }
# Line 1233 | Line 1546 | public class CompletableFutureTest exten
1546  
1547          g.complete(v2);
1548          checkIncomplete(h);
1549 <        assertFalse(r.ran);
1549 >        assertEquals(0, r.invocationCount);
1550          f.complete(v1);
1551  
1552          checkCompletedNormally(h, null);
1553 <        assertTrue(r.ran);
1553 >        assertEquals(1, r.invocationCount);
1554          checkCompletedNormally(f, v1);
1555          checkCompletedNormally(g, v2);
1556          }
# Line 1257 | Line 1570 | public class CompletableFutureTest exten
1570          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1571  
1572          checkCompletedNormally(h, null);
1573 <        assertTrue(r.ran);
1573 >        assertEquals(1, r.invocationCount);
1574          checkCompletedNormally(f, v1);
1575          checkCompletedNormally(g, v2);
1576          }
# Line 1277 | Line 1590 | public class CompletableFutureTest exten
1590          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1591  
1592          checkCompletedNormally(h, null);
1593 <        assertTrue(r.ran);
1593 >        assertEquals(1, r.invocationCount);
1594          checkCompletedNormally(f, v1);
1595          checkCompletedNormally(g, v2);
1596          }
# Line 1303 | Line 1616 | public class CompletableFutureTest exten
1616  
1617          checkCompletedWithWrappedCFException(h, ex);
1618          checkCompletedWithWrappedCFException(f, ex);
1619 <        assertFalse(r.ran);
1619 >        assertEquals(0, r.invocationCount);
1620          checkCompletedNormally(g, v1);
1621          }
1622      }
# Line 1324 | Line 1637 | public class CompletableFutureTest exten
1637  
1638          checkCompletedWithWrappedCFException(h, ex);
1639          checkCompletedWithWrappedCFException(g, ex);
1640 <        assertFalse(r.ran);
1640 >        assertEquals(0, r.invocationCount);
1641          checkCompletedNormally(f, v1);
1642          }
1643      }
# Line 1344 | Line 1657 | public class CompletableFutureTest exten
1657  
1658          checkCompletedWithWrappedCFException(h, ex);
1659          checkCompletedWithWrappedCFException(g, ex);
1660 <        assertFalse(r.ran);
1660 >        assertEquals(0, r.invocationCount);
1661          checkCompletedNormally(f, v1);
1662          }
1663      }
# Line 1364 | Line 1677 | public class CompletableFutureTest exten
1677  
1678          checkCompletedWithWrappedCFException(h, ex);
1679          checkCompletedWithWrappedCFException(f, ex);
1680 <        assertFalse(r.ran);
1680 >        assertEquals(0, r.invocationCount);
1681          checkCompletedNormally(g, v1);
1682          }
1683      }
# Line 1431 | Line 1744 | public class CompletableFutureTest exten
1744  
1745          checkCompletedWithWrappedCancellationException(h);
1746          checkCancelled(f);
1747 <        assertFalse(r.ran);
1747 >        assertEquals(0, r.invocationCount);
1748          checkCompletedNormally(g, v1);
1749          }
1750      }
# Line 1452 | Line 1765 | public class CompletableFutureTest exten
1765  
1766          checkCompletedWithWrappedCancellationException(h);
1767          checkCancelled(g);
1768 <        assertFalse(r.ran);
1768 >        assertEquals(0, r.invocationCount);
1769          checkCompletedNormally(f, v1);
1770          }
1771      }
# Line 1472 | Line 1785 | public class CompletableFutureTest exten
1785  
1786          checkCompletedWithWrappedCancellationException(h);
1787          checkCancelled(g);
1788 <        assertFalse(r.ran);
1788 >        assertEquals(0, r.invocationCount);
1789          checkCompletedNormally(f, v1);
1790          }
1791      }
# Line 1492 | Line 1805 | public class CompletableFutureTest exten
1805  
1806          checkCompletedWithWrappedCancellationException(h);
1807          checkCancelled(f);
1808 <        assertFalse(r.ran);
1808 >        assertEquals(0, r.invocationCount);
1809          checkCompletedNormally(g, v1);
1810          }
1811      }
# Line 1501 | Line 1814 | public class CompletableFutureTest exten
1814       * applyToEither result completes normally after normal completion
1815       * of either source
1816       */
1817 <    public void testApplyToEither() {
1818 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1819 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1820 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1508 <        f.complete(one);
1509 <        checkCompletedNormally(g, two);
1510 <        f2.complete(one);
1511 <        checkCompletedNormally(g, two);
1817 >    public void testApplyToEither_normalCompletion1() {
1818 >        for (ExecutionMode m : ExecutionMode.values())
1819 >        for (Integer v1 : new Integer[] { 1, null })
1820 >        for (Integer v2 : new Integer[] { 2, null }) {
1821  
1822 <        f = new CompletableFuture<>();
1823 <        f.complete(one);
1824 <        f2 = new CompletableFuture<>();
1825 <        g = f.applyToEither(f2, inc);
1826 <        checkCompletedNormally(g, two);
1822 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1823 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1824 >        final IncFunction r = new IncFunction();
1825 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1826 >
1827 >        f.complete(v1);
1828 >        checkCompletedNormally(h, inc(v1));
1829 >        g.complete(v2);
1830 >
1831 >        checkCompletedNormally(f, v1);
1832 >        checkCompletedNormally(g, v2);
1833 >        checkCompletedNormally(h, inc(v1));
1834 >        }
1835 >    }
1836 >
1837 >    public void testApplyToEither_normalCompletion2() {
1838 >        for (ExecutionMode m : ExecutionMode.values())
1839 >        for (Integer v1 : new Integer[] { 1, null })
1840 >        for (Integer v2 : new Integer[] { 2, null }) {
1841 >
1842 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1843 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1844 >        final IncFunction r = new IncFunction();
1845 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1846 >
1847 >        g.complete(v2);
1848 >        checkCompletedNormally(h, inc(v2));
1849 >        f.complete(v1);
1850 >
1851 >        checkCompletedNormally(f, v1);
1852 >        checkCompletedNormally(g, v2);
1853 >        checkCompletedNormally(h, inc(v2));
1854 >        }
1855 >    }
1856 >    public void testApplyToEither_normalCompletion3() {
1857 >        for (ExecutionMode m : ExecutionMode.values())
1858 >        for (Integer v1 : new Integer[] { 1, null })
1859 >        for (Integer v2 : new Integer[] { 2, null }) {
1860 >
1861 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1862 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1863 >        final IncFunction r = new IncFunction();
1864 >
1865 >        f.complete(v1);
1866 >        g.complete(v2);
1867 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1868 >
1869 >        checkCompletedNormally(f, v1);
1870 >        checkCompletedNormally(g, v2);
1871 >
1872 >        // unspecified behavior
1873 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1874 >                   Objects.equals(h.join(), inc(v2)));
1875 >        assertEquals(1, r.invocationCount);
1876 >        }
1877      }
1878  
1879      /**
1880       * applyToEither result completes exceptionally after exceptional
1881       * completion of either source
1882       */
1883 <    public void testApplyToEither2() {
1884 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1885 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1527 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1528 <        f.completeExceptionally(new CFException());
1529 <        f2.complete(one);
1530 <        checkCompletedWithWrappedCFException(g);
1883 >    public void testApplyToEither_exceptionalCompletion1() {
1884 >        for (ExecutionMode m : ExecutionMode.values())
1885 >        for (Integer v1 : new Integer[] { 1, null }) {
1886  
1887 <        f = new CompletableFuture<>();
1888 <        f2 = new CompletableFuture<>();
1889 <        f2.completeExceptionally(new CFException());
1890 <        g = f.applyToEither(f2, inc);
1891 <        checkCompletedWithWrappedCFException(g);
1887 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1888 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1889 >        final IncFunction r = new IncFunction();
1890 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1891 >        final CFException ex = new CFException();
1892 >
1893 >        f.completeExceptionally(ex);
1894 >        checkCompletedWithWrappedCFException(h, ex);
1895 >        g.complete(v1);
1896 >
1897 >        assertEquals(0, r.invocationCount);
1898 >        checkCompletedNormally(g, v1);
1899 >        checkCompletedWithWrappedCFException(f, ex);
1900 >        checkCompletedWithWrappedCFException(h, ex);
1901 >        }
1902 >    }
1903 >
1904 >    public void testApplyToEither_exceptionalCompletion2() {
1905 >        for (ExecutionMode m : ExecutionMode.values())
1906 >        for (Integer v1 : new Integer[] { 1, null }) {
1907 >
1908 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1909 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1910 >        final IncFunction r = new IncFunction();
1911 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1912 >        final CFException ex = new CFException();
1913 >
1914 >        g.completeExceptionally(ex);
1915 >        checkCompletedWithWrappedCFException(h, ex);
1916 >        f.complete(v1);
1917 >
1918 >        assertEquals(0, r.invocationCount);
1919 >        checkCompletedNormally(f, v1);
1920 >        checkCompletedWithWrappedCFException(g, ex);
1921 >        checkCompletedWithWrappedCFException(h, ex);
1922 >        }
1923 >    }
1924 >
1925 >    public void testApplyToEither_exceptionalCompletion3() {
1926 >        for (ExecutionMode m : ExecutionMode.values())
1927 >        for (Integer v1 : new Integer[] { 1, null }) {
1928 >
1929 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1930 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1931 >        final IncFunction r = new IncFunction();
1932 >        final CFException ex = new CFException();
1933 >
1934 >        g.completeExceptionally(ex);
1935 >        f.complete(v1);
1936 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1937 >
1938 >        // unspecified behavior
1939 >        Integer v;
1940 >        try {
1941 >            assertEquals(h.join(), inc(v1));
1942 >            assertEquals(1, r.invocationCount);
1943 >        } catch (CompletionException ok) {
1944 >            checkCompletedWithWrappedCFException(h, ex);
1945 >            assertEquals(0, r.invocationCount);
1946 >        }
1947 >
1948 >        checkCompletedWithWrappedCFException(g, ex);
1949 >        checkCompletedNormally(f, v1);
1950 >        }
1951 >    }
1952 >
1953 >    public void testApplyToEither_exceptionalCompletion4() {
1954 >        for (ExecutionMode m : ExecutionMode.values())
1955 >        for (Integer v1 : new Integer[] { 1, null }) {
1956 >
1957 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1958 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1959 >        final IncFunction r = new IncFunction();
1960 >        final CFException ex = new CFException();
1961 >
1962 >        f.completeExceptionally(ex);
1963 >        g.complete(v1);
1964 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1965 >
1966 >        // unspecified behavior
1967 >        Integer v;
1968 >        try {
1969 >            assertEquals(h.join(), inc(v1));
1970 >            assertEquals(1, r.invocationCount);
1971 >        } catch (CompletionException ok) {
1972 >            checkCompletedWithWrappedCFException(h, ex);
1973 >            assertEquals(0, r.invocationCount);
1974 >        }
1975 >
1976 >        checkCompletedWithWrappedCFException(f, ex);
1977 >        checkCompletedNormally(g, v1);
1978 >        }
1979      }
1980  
1981      /**
1982       * applyToEither result completes exceptionally if action does
1983       */
1984 <    public void testApplyToEither3() {
1985 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1986 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1987 <        FailingFunction r = new FailingFunction();
1988 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1989 <        f2.complete(two);
1990 <        checkCompletedWithWrappedCFException(g);
1984 >    public void testApplyToEither_actionFailed1() {
1985 >        for (ExecutionMode m : ExecutionMode.values())
1986 >        for (Integer v1 : new Integer[] { 1, null })
1987 >        for (Integer v2 : new Integer[] { 2, null }) {
1988 >
1989 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1990 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1991 >        final FailingFunction r = new FailingFunction();
1992 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1993 >
1994 >        f.complete(v1);
1995 >        checkCompletedWithWrappedCFException(h);
1996 >        g.complete(v2);
1997 >        checkCompletedNormally(f, v1);
1998 >        checkCompletedNormally(g, v2);
1999 >        }
2000 >    }
2001 >
2002 >    public void testApplyToEither_actionFailed2() {
2003 >        for (ExecutionMode m : ExecutionMode.values())
2004 >        for (Integer v1 : new Integer[] { 1, null })
2005 >        for (Integer v2 : new Integer[] { 2, null }) {
2006 >
2007 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2008 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2009 >        final FailingFunction r = new FailingFunction();
2010 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2011 >
2012 >        g.complete(v2);
2013 >        checkCompletedWithWrappedCFException(h);
2014 >        f.complete(v1);
2015 >        checkCompletedNormally(f, v1);
2016 >        checkCompletedNormally(g, v2);
2017 >        }
2018      }
2019  
2020      /**
2021       * applyToEither result completes exceptionally if either source cancelled
2022       */
2023 <    public void testApplyToEither4() {
2024 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2025 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2026 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2027 <        assertTrue(f.cancel(true));
2028 <        checkCompletedWithWrappedCancellationException(g);
2029 <        f = new CompletableFuture<>();
2030 <        f2 = new CompletableFuture<>();
2031 <        assertTrue(f2.cancel(true));
2032 <        checkCompletedWithWrappedCancellationException(g);
2023 >    public void testApplyToEither_sourceCancelled1() {
2024 >        for (ExecutionMode m : ExecutionMode.values())
2025 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2026 >        for (Integer v1 : new Integer[] { 1, null }) {
2027 >
2028 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2029 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2030 >        final IncFunction r = new IncFunction();
2031 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2032 >
2033 >        assertTrue(f.cancel(mayInterruptIfRunning));
2034 >        checkCompletedWithWrappedCancellationException(h);
2035 >        g.complete(v1);
2036 >
2037 >        checkCancelled(f);
2038 >        assertEquals(0, r.invocationCount);
2039 >        checkCompletedNormally(g, v1);
2040 >        checkCompletedWithWrappedCancellationException(h);
2041 >        }
2042 >    }
2043 >
2044 >    public void testApplyToEither_sourceCancelled2() {
2045 >        for (ExecutionMode m : ExecutionMode.values())
2046 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2047 >        for (Integer v1 : new Integer[] { 1, null }) {
2048 >
2049 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2050 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2051 >        final IncFunction r = new IncFunction();
2052 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2053 >
2054 >        assertTrue(g.cancel(mayInterruptIfRunning));
2055 >        checkCompletedWithWrappedCancellationException(h);
2056 >        f.complete(v1);
2057 >
2058 >        checkCancelled(g);
2059 >        assertEquals(0, r.invocationCount);
2060 >        checkCompletedNormally(f, v1);
2061 >        checkCompletedWithWrappedCancellationException(h);
2062 >        }
2063 >    }
2064 >
2065 >    public void testApplyToEither_sourceCancelled3() {
2066 >        for (ExecutionMode m : ExecutionMode.values())
2067 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2068 >        for (Integer v1 : new Integer[] { 1, null }) {
2069 >
2070 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2071 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2072 >        final IncFunction r = new IncFunction();
2073 >
2074 >        assertTrue(g.cancel(mayInterruptIfRunning));
2075 >        f.complete(v1);
2076 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2077 >
2078 >        // unspecified behavior
2079 >        Integer v;
2080 >        try {
2081 >            assertEquals(h.join(), inc(v1));
2082 >            assertEquals(1, r.invocationCount);
2083 >        } catch (CompletionException ok) {
2084 >            checkCompletedWithWrappedCancellationException(h);
2085 >            assertEquals(0, r.invocationCount);
2086 >        }
2087 >
2088 >        checkCancelled(g);
2089 >        checkCompletedNormally(f, v1);
2090 >        }
2091 >    }
2092 >
2093 >    public void testApplyToEither_sourceCancelled4() {
2094 >        for (ExecutionMode m : ExecutionMode.values())
2095 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2096 >        for (Integer v1 : new Integer[] { 1, null }) {
2097 >
2098 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2099 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2100 >        final IncFunction r = new IncFunction();
2101 >
2102 >        assertTrue(f.cancel(mayInterruptIfRunning));
2103 >        g.complete(v1);
2104 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2105 >
2106 >        // unspecified behavior
2107 >        Integer v;
2108 >        try {
2109 >            assertEquals(h.join(), inc(v1));
2110 >            assertEquals(1, r.invocationCount);
2111 >        } catch (CompletionException ok) {
2112 >            checkCompletedWithWrappedCancellationException(h);
2113 >            assertEquals(0, r.invocationCount);
2114 >        }
2115 >
2116 >        checkCancelled(f);
2117 >        checkCompletedNormally(g, v1);
2118 >        }
2119      }
2120  
2121      /**
2122       * acceptEither result completes normally after normal completion
2123       * of either source
2124       */
2125 <    public void testAcceptEither() {
2126 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2127 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2128 <        IncAction r = new IncAction();
1574 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1575 <        f.complete(one);
1576 <        checkCompletedNormally(g, null);
1577 <        f2.complete(one);
1578 <        checkCompletedNormally(g, null);
1579 <        assertEquals(r.value, 2);
2125 >    public void testAcceptEither_normalCompletion1() {
2126 >        for (ExecutionMode m : ExecutionMode.values())
2127 >        for (Integer v1 : new Integer[] { 1, null })
2128 >        for (Integer v2 : new Integer[] { 2, null }) {
2129  
2130 <        r = new IncAction();
2131 <        f = new CompletableFuture<>();
2132 <        f.complete(one);
2133 <        f2 = new CompletableFuture<>();
2134 <        g = f.acceptEither(f2, r);
2135 <        checkCompletedNormally(g, null);
2136 <        assertEquals(r.value, 2);
2130 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2131 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2132 >        final IncAction r = new IncAction();
2133 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2134 >
2135 >        f.complete(v1);
2136 >        checkCompletedNormally(h, null);
2137 >        assertEquals(r.value, inc(v1));
2138 >        g.complete(v2);
2139 >
2140 >        checkCompletedNormally(f, v1);
2141 >        checkCompletedNormally(g, v2);
2142 >        checkCompletedNormally(h, null);
2143 >        }
2144 >    }
2145 >
2146 >    public void testAcceptEither_normalCompletion2() {
2147 >        for (ExecutionMode m : ExecutionMode.values())
2148 >        for (Integer v1 : new Integer[] { 1, null })
2149 >        for (Integer v2 : new Integer[] { 2, null }) {
2150 >
2151 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2152 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2153 >        final IncAction r = new IncAction();
2154 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2155 >
2156 >        g.complete(v2);
2157 >        checkCompletedNormally(h, null);
2158 >        assertEquals(r.value, inc(v2));
2159 >        f.complete(v1);
2160 >
2161 >        checkCompletedNormally(f, v1);
2162 >        checkCompletedNormally(g, v2);
2163 >        checkCompletedNormally(h, null);
2164 >        }
2165 >    }
2166 >    public void testAcceptEither_normalCompletion3() {
2167 >        for (ExecutionMode m : ExecutionMode.values())
2168 >        for (Integer v1 : new Integer[] { 1, null })
2169 >        for (Integer v2 : new Integer[] { 2, null }) {
2170 >
2171 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2172 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2173 >        final IncAction r = new IncAction();
2174 >
2175 >        f.complete(v1);
2176 >        g.complete(v2);
2177 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2178 >
2179 >        checkCompletedNormally(h, null);
2180 >        checkCompletedNormally(f, v1);
2181 >        checkCompletedNormally(g, v2);
2182 >
2183 >        // unspecified behavior
2184 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2185 >                   Objects.equals(r.value, inc(v2)));
2186 >        }
2187      }
2188  
2189      /**
2190       * acceptEither result completes exceptionally after exceptional
2191       * completion of either source
2192       */
2193 <    public void testAcceptEither2() {
2194 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2195 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1597 <        IncAction r = new IncAction();
1598 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1599 <        f.completeExceptionally(new CFException());
1600 <        f2.complete(one);
1601 <        checkCompletedWithWrappedCFException(g);
2193 >    public void testAcceptEither_exceptionalCompletion1() {
2194 >        for (ExecutionMode m : ExecutionMode.values())
2195 >        for (Integer v1 : new Integer[] { 1, null }) {
2196  
2197 <        r = new IncAction();
2198 <        f = new CompletableFuture<>();
2199 <        f2 = new CompletableFuture<>();
2200 <        f2.completeExceptionally(new CFException());
2201 <        g = f.acceptEither(f2, r);
2202 <        checkCompletedWithWrappedCFException(g);
2197 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2198 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2199 >        final IncAction r = new IncAction();
2200 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2201 >        final CFException ex = new CFException();
2202 >
2203 >        f.completeExceptionally(ex);
2204 >        checkCompletedWithWrappedCFException(h, ex);
2205 >        g.complete(v1);
2206 >
2207 >        assertEquals(0, r.invocationCount);
2208 >        checkCompletedNormally(g, v1);
2209 >        checkCompletedWithWrappedCFException(f, ex);
2210 >        checkCompletedWithWrappedCFException(h, ex);
2211 >        }
2212 >    }
2213 >
2214 >    public void testAcceptEither_exceptionalCompletion2() {
2215 >        for (ExecutionMode m : ExecutionMode.values())
2216 >        for (Integer v1 : new Integer[] { 1, null }) {
2217 >
2218 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2219 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2220 >        final IncAction r = new IncAction();
2221 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2222 >        final CFException ex = new CFException();
2223 >
2224 >        g.completeExceptionally(ex);
2225 >        checkCompletedWithWrappedCFException(h, ex);
2226 >        f.complete(v1);
2227 >
2228 >        assertEquals(0, r.invocationCount);
2229 >        checkCompletedNormally(f, v1);
2230 >        checkCompletedWithWrappedCFException(g, ex);
2231 >        checkCompletedWithWrappedCFException(h, ex);
2232 >        }
2233 >    }
2234 >
2235 >    public void testAcceptEither_exceptionalCompletion3() {
2236 >        for (ExecutionMode m : ExecutionMode.values())
2237 >        for (Integer v1 : new Integer[] { 1, null }) {
2238 >
2239 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2240 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2241 >        final IncAction r = new IncAction();
2242 >        final CFException ex = new CFException();
2243 >
2244 >        g.completeExceptionally(ex);
2245 >        f.complete(v1);
2246 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2247 >
2248 >        // unspecified behavior
2249 >        Integer v;
2250 >        try {
2251 >            assertEquals(h.join(), null);
2252 >            assertEquals(1, r.invocationCount);
2253 >            assertEquals(inc(v1), r.value);
2254 >        } catch (CompletionException ok) {
2255 >            checkCompletedWithWrappedCFException(h, ex);
2256 >            assertEquals(0, r.invocationCount);
2257 >        }
2258 >
2259 >        checkCompletedWithWrappedCFException(g, ex);
2260 >        checkCompletedNormally(f, v1);
2261 >        }
2262 >    }
2263 >
2264 >    public void testAcceptEither_exceptionalCompletion4() {
2265 >        for (ExecutionMode m : ExecutionMode.values())
2266 >        for (Integer v1 : new Integer[] { 1, null }) {
2267 >
2268 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2269 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2270 >        final IncAction r = new IncAction();
2271 >        final CFException ex = new CFException();
2272 >
2273 >        f.completeExceptionally(ex);
2274 >        g.complete(v1);
2275 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2276 >
2277 >        // unspecified behavior
2278 >        Integer v;
2279 >        try {
2280 >            assertEquals(h.join(), null);
2281 >            assertEquals(1, r.invocationCount);
2282 >            assertEquals(inc(v1), r.value);
2283 >        } catch (CompletionException ok) {
2284 >            checkCompletedWithWrappedCFException(h, ex);
2285 >            assertEquals(0, r.invocationCount);
2286 >        }
2287 >
2288 >        checkCompletedWithWrappedCFException(f, ex);
2289 >        checkCompletedNormally(g, v1);
2290 >        }
2291      }
2292  
2293      /**
2294       * acceptEither result completes exceptionally if action does
2295       */
2296 <    public void testAcceptEither3() {
2297 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2298 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2299 <        FailingConsumer r = new FailingConsumer();
2300 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2301 <        f2.complete(two);
2302 <        checkCompletedWithWrappedCFException(g);
2296 >    public void testAcceptEither_actionFailed1() {
2297 >        for (ExecutionMode m : ExecutionMode.values())
2298 >        for (Integer v1 : new Integer[] { 1, null })
2299 >        for (Integer v2 : new Integer[] { 2, null }) {
2300 >
2301 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2302 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2303 >        final FailingConsumer r = new FailingConsumer();
2304 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2305 >
2306 >        f.complete(v1);
2307 >        checkCompletedWithWrappedCFException(h);
2308 >        g.complete(v2);
2309 >        checkCompletedNormally(f, v1);
2310 >        checkCompletedNormally(g, v2);
2311 >        }
2312 >    }
2313 >
2314 >    public void testAcceptEither_actionFailed2() {
2315 >        for (ExecutionMode m : ExecutionMode.values())
2316 >        for (Integer v1 : new Integer[] { 1, null })
2317 >        for (Integer v2 : new Integer[] { 2, null }) {
2318 >
2319 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2320 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 >        final FailingConsumer r = new FailingConsumer();
2322 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2323 >
2324 >        g.complete(v2);
2325 >        checkCompletedWithWrappedCFException(h);
2326 >        f.complete(v1);
2327 >        checkCompletedNormally(f, v1);
2328 >        checkCompletedNormally(g, v2);
2329 >        }
2330      }
2331  
2332      /**
2333       * acceptEither result completes exceptionally if either source cancelled
2334       */
2335 <    public void testAcceptEither4() {
2336 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2337 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2338 <        IncAction r = new IncAction();
2339 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2340 <        assertTrue(f.cancel(true));
2341 <        checkCompletedWithWrappedCancellationException(g);
2342 <        f = new CompletableFuture<>();
2343 <        f2 = new CompletableFuture<>();
2344 <        assertTrue(f2.cancel(true));
2345 <        checkCompletedWithWrappedCancellationException(g);
2335 >    public void testAcceptEither_sourceCancelled1() {
2336 >        for (ExecutionMode m : ExecutionMode.values())
2337 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2338 >        for (Integer v1 : new Integer[] { 1, null }) {
2339 >
2340 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2341 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2342 >        final IncAction r = new IncAction();
2343 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2344 >
2345 >        assertTrue(f.cancel(mayInterruptIfRunning));
2346 >        checkCompletedWithWrappedCancellationException(h);
2347 >        g.complete(v1);
2348 >
2349 >        checkCancelled(f);
2350 >        assertEquals(0, r.invocationCount);
2351 >        checkCompletedNormally(g, v1);
2352 >        checkCompletedWithWrappedCancellationException(h);
2353 >        }
2354 >    }
2355 >
2356 >    public void testAcceptEither_sourceCancelled2() {
2357 >        for (ExecutionMode m : ExecutionMode.values())
2358 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2359 >        for (Integer v1 : new Integer[] { 1, null }) {
2360 >
2361 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2362 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2363 >        final IncAction r = new IncAction();
2364 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2365 >
2366 >        assertTrue(g.cancel(mayInterruptIfRunning));
2367 >        checkCompletedWithWrappedCancellationException(h);
2368 >        f.complete(v1);
2369 >
2370 >        checkCancelled(g);
2371 >        assertEquals(0, r.invocationCount);
2372 >        checkCompletedNormally(f, v1);
2373 >        checkCompletedWithWrappedCancellationException(h);
2374 >        }
2375 >    }
2376 >
2377 >    public void testAcceptEither_sourceCancelled3() {
2378 >        for (ExecutionMode m : ExecutionMode.values())
2379 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2380 >        for (Integer v1 : new Integer[] { 1, null }) {
2381 >
2382 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2383 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2384 >        final IncAction r = new IncAction();
2385 >
2386 >        assertTrue(g.cancel(mayInterruptIfRunning));
2387 >        f.complete(v1);
2388 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2389 >
2390 >        // unspecified behavior
2391 >        Integer v;
2392 >        try {
2393 >            assertEquals(h.join(), null);
2394 >            assertEquals(1, r.invocationCount);
2395 >            assertEquals(inc(v1), r.value);
2396 >        } catch (CompletionException ok) {
2397 >            checkCompletedWithWrappedCancellationException(h);
2398 >            assertEquals(0, r.invocationCount);
2399 >        }
2400 >
2401 >        checkCancelled(g);
2402 >        checkCompletedNormally(f, v1);
2403 >        }
2404 >    }
2405 >
2406 >    public void testAcceptEither_sourceCancelled4() {
2407 >        for (ExecutionMode m : ExecutionMode.values())
2408 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2409 >        for (Integer v1 : new Integer[] { 1, null }) {
2410 >
2411 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2412 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2413 >        final IncAction r = new IncAction();
2414 >
2415 >        assertTrue(f.cancel(mayInterruptIfRunning));
2416 >        g.complete(v1);
2417 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2418 >
2419 >        // unspecified behavior
2420 >        Integer v;
2421 >        try {
2422 >            assertEquals(h.join(), null);
2423 >            assertEquals(1, r.invocationCount);
2424 >            assertEquals(inc(v1), r.value);
2425 >        } catch (CompletionException ok) {
2426 >            checkCompletedWithWrappedCancellationException(h);
2427 >            assertEquals(0, r.invocationCount);
2428 >        }
2429 >
2430 >        checkCancelled(f);
2431 >        checkCompletedNormally(g, v1);
2432 >        }
2433      }
2434  
2435      /**
2436       * runAfterEither result completes normally after normal completion
2437       * of either source
2438       */
2439 <    public void testRunAfterEither() {
2440 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2441 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2442 <        Noop r = new Noop();
1647 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1648 <        f.complete(one);
1649 <        checkCompletedNormally(g, null);
1650 <        f2.complete(one);
1651 <        checkCompletedNormally(g, null);
1652 <        assertTrue(r.ran);
2439 >    public void testRunAfterEither_normalCompletion1() {
2440 >        for (ExecutionMode m : ExecutionMode.values())
2441 >        for (Integer v1 : new Integer[] { 1, null })
2442 >        for (Integer v2 : new Integer[] { 2, null }) {
2443  
2444 <        r = new Noop();
2445 <        f = new CompletableFuture<>();
2446 <        f.complete(one);
2447 <        f2 = new CompletableFuture<>();
2448 <        g = f.runAfterEither(f2, r);
2449 <        checkCompletedNormally(g, null);
2450 <        assertTrue(r.ran);
2444 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2445 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2446 >        final Noop r = new Noop();
2447 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2448 >
2449 >        f.complete(v1);
2450 >        checkCompletedNormally(h, null);
2451 >        assertEquals(1, r.invocationCount);
2452 >        g.complete(v2);
2453 >
2454 >        checkCompletedNormally(f, v1);
2455 >        checkCompletedNormally(g, v2);
2456 >        checkCompletedNormally(h, null);
2457 >        assertEquals(1, r.invocationCount);
2458 >        }
2459 >    }
2460 >
2461 >    public void testRunAfterEither_normalCompletion2() {
2462 >        for (ExecutionMode m : ExecutionMode.values())
2463 >        for (Integer v1 : new Integer[] { 1, null })
2464 >        for (Integer v2 : new Integer[] { 2, null }) {
2465 >
2466 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2467 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2468 >        final Noop r = new Noop();
2469 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2470 >
2471 >        g.complete(v2);
2472 >        checkCompletedNormally(h, null);
2473 >        assertEquals(1, r.invocationCount);
2474 >        f.complete(v1);
2475 >
2476 >        checkCompletedNormally(f, v1);
2477 >        checkCompletedNormally(g, v2);
2478 >        checkCompletedNormally(h, null);
2479 >        assertEquals(1, r.invocationCount);
2480 >        }
2481 >    }
2482 >    public void testRunAfterEither_normalCompletion3() {
2483 >        for (ExecutionMode m : ExecutionMode.values())
2484 >        for (Integer v1 : new Integer[] { 1, null })
2485 >        for (Integer v2 : new Integer[] { 2, null }) {
2486 >
2487 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2488 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2489 >        final Noop r = new Noop();
2490 >
2491 >        f.complete(v1);
2492 >        g.complete(v2);
2493 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2494 >
2495 >        checkCompletedNormally(h, null);
2496 >        checkCompletedNormally(f, v1);
2497 >        checkCompletedNormally(g, v2);
2498 >        assertEquals(1, r.invocationCount);
2499 >        }
2500      }
2501  
2502      /**
2503       * runAfterEither result completes exceptionally after exceptional
2504       * completion of either source
2505       */
2506 <    public void testRunAfterEither2() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2508 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1670 <        Noop r = new Noop();
1671 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1672 <        f.completeExceptionally(new CFException());
1673 <        f2.complete(one);
1674 <        checkCompletedWithWrappedCFException(g);
2506 >    public void testRunAfterEither_exceptionalCompletion1() {
2507 >        for (ExecutionMode m : ExecutionMode.values())
2508 >        for (Integer v1 : new Integer[] { 1, null }) {
2509  
2510 <        r = new Noop();
2511 <        f = new CompletableFuture<>();
2512 <        f2 = new CompletableFuture<>();
2513 <        f2.completeExceptionally(new CFException());
2514 <        g = f.runAfterEither(f2, r);
2515 <        checkCompletedWithWrappedCFException(g);
2510 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2511 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2512 >        final Noop r = new Noop();
2513 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2514 >        final CFException ex = new CFException();
2515 >
2516 >        f.completeExceptionally(ex);
2517 >        checkCompletedWithWrappedCFException(h, ex);
2518 >        g.complete(v1);
2519 >
2520 >        assertEquals(0, r.invocationCount);
2521 >        checkCompletedNormally(g, v1);
2522 >        checkCompletedWithWrappedCFException(f, ex);
2523 >        checkCompletedWithWrappedCFException(h, ex);
2524 >        }
2525 >    }
2526 >
2527 >    public void testRunAfterEither_exceptionalCompletion2() {
2528 >        for (ExecutionMode m : ExecutionMode.values())
2529 >        for (Integer v1 : new Integer[] { 1, null }) {
2530 >
2531 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2532 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2533 >        final Noop r = new Noop();
2534 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2535 >        final CFException ex = new CFException();
2536 >
2537 >        g.completeExceptionally(ex);
2538 >        checkCompletedWithWrappedCFException(h, ex);
2539 >        f.complete(v1);
2540 >
2541 >        assertEquals(0, r.invocationCount);
2542 >        checkCompletedNormally(f, v1);
2543 >        checkCompletedWithWrappedCFException(g, ex);
2544 >        checkCompletedWithWrappedCFException(h, ex);
2545 >        }
2546 >    }
2547 >
2548 >    public void testRunAfterEither_exceptionalCompletion3() {
2549 >        for (ExecutionMode m : ExecutionMode.values())
2550 >        for (Integer v1 : new Integer[] { 1, null }) {
2551 >
2552 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2553 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2554 >        final Noop r = new Noop();
2555 >        final CFException ex = new CFException();
2556 >
2557 >        g.completeExceptionally(ex);
2558 >        f.complete(v1);
2559 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2560 >
2561 >        // unspecified behavior
2562 >        Integer v;
2563 >        try {
2564 >            assertEquals(h.join(), null);
2565 >            assertEquals(1, r.invocationCount);
2566 >        } catch (CompletionException ok) {
2567 >            checkCompletedWithWrappedCFException(h, ex);
2568 >            assertEquals(0, r.invocationCount);
2569 >        }
2570 >
2571 >        checkCompletedWithWrappedCFException(g, ex);
2572 >        checkCompletedNormally(f, v1);
2573 >        }
2574 >    }
2575 >
2576 >    public void testRunAfterEither_exceptionalCompletion4() {
2577 >        for (ExecutionMode m : ExecutionMode.values())
2578 >        for (Integer v1 : new Integer[] { 1, null }) {
2579 >
2580 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2581 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2582 >        final Noop r = new Noop();
2583 >        final CFException ex = new CFException();
2584 >
2585 >        f.completeExceptionally(ex);
2586 >        g.complete(v1);
2587 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2588 >
2589 >        // unspecified behavior
2590 >        Integer v;
2591 >        try {
2592 >            assertEquals(h.join(), null);
2593 >            assertEquals(1, r.invocationCount);
2594 >        } catch (CompletionException ok) {
2595 >            checkCompletedWithWrappedCFException(h, ex);
2596 >            assertEquals(0, r.invocationCount);
2597 >        }
2598 >
2599 >        checkCompletedWithWrappedCFException(f, ex);
2600 >        checkCompletedNormally(g, v1);
2601 >        }
2602      }
2603  
2604      /**
2605       * runAfterEither result completes exceptionally if action does
2606       */
2607 <    public void testRunAfterEither3() {
2608 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2609 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2610 <        FailingNoop r = new FailingNoop();
2611 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2612 <        f2.complete(two);
2613 <        checkCompletedWithWrappedCFException(g);
2607 >    public void testRunAfterEither_actionFailed1() {
2608 >        for (ExecutionMode m : ExecutionMode.values())
2609 >        for (Integer v1 : new Integer[] { 1, null })
2610 >        for (Integer v2 : new Integer[] { 2, null }) {
2611 >
2612 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2613 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2614 >        final FailingNoop r = new FailingNoop();
2615 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2616 >
2617 >        f.complete(v1);
2618 >        checkCompletedWithWrappedCFException(h);
2619 >        g.complete(v2);
2620 >        checkCompletedNormally(f, v1);
2621 >        checkCompletedNormally(g, v2);
2622 >        }
2623 >    }
2624 >
2625 >    public void testRunAfterEither_actionFailed2() {
2626 >        for (ExecutionMode m : ExecutionMode.values())
2627 >        for (Integer v1 : new Integer[] { 1, null })
2628 >        for (Integer v2 : new Integer[] { 2, null }) {
2629 >
2630 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2631 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2632 >        final FailingNoop r = new FailingNoop();
2633 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2634 >
2635 >        g.complete(v2);
2636 >        checkCompletedWithWrappedCFException(h);
2637 >        f.complete(v1);
2638 >        checkCompletedNormally(f, v1);
2639 >        checkCompletedNormally(g, v2);
2640 >        }
2641      }
2642  
2643      /**
2644       * runAfterEither result completes exceptionally if either source cancelled
2645       */
2646 <    public void testRunAfterEither4() {
2647 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2648 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2649 <        Noop r = new Noop();
2650 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2651 <        assertTrue(f.cancel(true));
2652 <        checkCompletedWithWrappedCancellationException(g);
2653 <        f = new CompletableFuture<>();
2654 <        f2 = new CompletableFuture<>();
2655 <        assertTrue(f2.cancel(true));
2656 <        checkCompletedWithWrappedCancellationException(g);
2646 >    public void testRunAfterEither_sourceCancelled1() {
2647 >        for (ExecutionMode m : ExecutionMode.values())
2648 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2649 >        for (Integer v1 : new Integer[] { 1, null }) {
2650 >
2651 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2652 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2653 >        final Noop r = new Noop();
2654 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2655 >
2656 >        assertTrue(f.cancel(mayInterruptIfRunning));
2657 >        checkCompletedWithWrappedCancellationException(h);
2658 >        g.complete(v1);
2659 >
2660 >        checkCancelled(f);
2661 >        assertEquals(0, r.invocationCount);
2662 >        checkCompletedNormally(g, v1);
2663 >        checkCompletedWithWrappedCancellationException(h);
2664 >        }
2665 >    }
2666 >
2667 >    public void testRunAfterEither_sourceCancelled2() {
2668 >        for (ExecutionMode m : ExecutionMode.values())
2669 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2670 >        for (Integer v1 : new Integer[] { 1, null }) {
2671 >
2672 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2673 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2674 >        final Noop r = new Noop();
2675 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2676 >
2677 >        assertTrue(g.cancel(mayInterruptIfRunning));
2678 >        checkCompletedWithWrappedCancellationException(h);
2679 >        f.complete(v1);
2680 >
2681 >        checkCancelled(g);
2682 >        assertEquals(0, r.invocationCount);
2683 >        checkCompletedNormally(f, v1);
2684 >        checkCompletedWithWrappedCancellationException(h);
2685 >        }
2686 >    }
2687 >
2688 >    public void testRunAfterEither_sourceCancelled3() {
2689 >        for (ExecutionMode m : ExecutionMode.values())
2690 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2691 >        for (Integer v1 : new Integer[] { 1, null }) {
2692 >
2693 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2694 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2695 >        final Noop r = new Noop();
2696 >
2697 >        assertTrue(g.cancel(mayInterruptIfRunning));
2698 >        f.complete(v1);
2699 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2700 >
2701 >        // unspecified behavior
2702 >        Integer v;
2703 >        try {
2704 >            assertEquals(h.join(), null);
2705 >            assertEquals(1, r.invocationCount);
2706 >        } catch (CompletionException ok) {
2707 >            checkCompletedWithWrappedCancellationException(h);
2708 >            assertEquals(0, r.invocationCount);
2709 >        }
2710 >
2711 >        checkCancelled(g);
2712 >        checkCompletedNormally(f, v1);
2713 >        }
2714 >    }
2715 >
2716 >    public void testRunAfterEither_sourceCancelled4() {
2717 >        for (ExecutionMode m : ExecutionMode.values())
2718 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2719 >        for (Integer v1 : new Integer[] { 1, null }) {
2720 >
2721 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2722 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2723 >        final Noop r = new Noop();
2724 >
2725 >        assertTrue(f.cancel(mayInterruptIfRunning));
2726 >        g.complete(v1);
2727 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2728 >
2729 >        // unspecified behavior
2730 >        Integer v;
2731 >        try {
2732 >            assertEquals(h.join(), null);
2733 >            assertEquals(1, r.invocationCount);
2734 >        } catch (CompletionException ok) {
2735 >            checkCompletedWithWrappedCancellationException(h);
2736 >            assertEquals(0, r.invocationCount);
2737 >        }
2738 >
2739 >        checkCancelled(f);
2740 >        checkCompletedNormally(g, v1);
2741 >        }
2742      }
2743  
2744      /**
2745       * thenCompose result completes normally after normal completion of source
2746       */
2747 <    public void testThenCompose() {
2748 <        CompletableFuture<Integer> f, g;
2749 <        CompletableFutureInc r;
2747 >    public void testThenCompose_normalCompletion1() {
2748 >        for (ExecutionMode m : ExecutionMode.values())
2749 >        for (Integer v1 : new Integer[] { 1, null }) {
2750  
2751 <        f = new CompletableFuture<>();
2752 <        g = f.thenCompose(r = new CompletableFutureInc());
2753 <        f.complete(one);
2754 <        checkCompletedNormally(g, two);
2755 <        assertTrue(r.ran);
2751 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2752 >        final CompletableFutureInc r = new CompletableFutureInc();
2753 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2754 >        f.complete(v1);
2755 >        checkCompletedNormally(g, inc(v1));
2756 >        checkCompletedNormally(f, v1);
2757 >        assertEquals(1, r.invocationCount);
2758 >        }
2759 >    }
2760  
2761 <        f = new CompletableFuture<>();
2762 <        f.complete(one);
2763 <        g = f.thenCompose(r = new CompletableFutureInc());
2764 <        checkCompletedNormally(g, two);
2765 <        assertTrue(r.ran);
2761 >    public void testThenCompose_normalCompletion2() {
2762 >        for (ExecutionMode m : ExecutionMode.values())
2763 >        for (Integer v1 : new Integer[] { 1, null }) {
2764 >
2765 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2766 >        final CompletableFutureInc r = new CompletableFutureInc();
2767 >        f.complete(v1);
2768 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2769 >        checkCompletedNormally(g, inc(v1));
2770 >        checkCompletedNormally(f, v1);
2771 >        assertEquals(1, r.invocationCount);
2772 >        }
2773      }
2774  
2775      /**
2776       * thenCompose result completes exceptionally after exceptional
2777       * completion of source
2778       */
2779 <    public void testThenCompose2() {
2780 <        CompletableFuture<Integer> f, g;
1738 <        CompletableFutureInc r;
2779 >    public void testThenCompose_exceptionalCompletion1() {
2780 >        for (ExecutionMode m : ExecutionMode.values()) {
2781  
2782 <        f = new CompletableFuture<>();
2783 <        g = f.thenCompose(r = new CompletableFutureInc());
2784 <        f.completeExceptionally(new CFException());
2785 <        checkCompletedWithWrappedCFException(g);
2782 >        final CFException ex = new CFException();
2783 >        final CompletableFutureInc r = new CompletableFutureInc();
2784 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2785 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2786 >        f.completeExceptionally(ex);
2787 >        checkCompletedWithWrappedCFException(g, ex);
2788 >        checkCompletedWithWrappedCFException(f, ex);
2789 >        }
2790 >    }
2791  
2792 <        f = new CompletableFuture<>();
2793 <        f.completeExceptionally(new CFException());
2794 <        g = f.thenCompose(r = new CompletableFutureInc());
2795 <        checkCompletedWithWrappedCFException(g);
2792 >    public void testThenCompose_exceptionalCompletion2() {
2793 >        for (ExecutionMode m : ExecutionMode.values()) {
2794 >
2795 >        final CFException ex = new CFException();
2796 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2797 >        f.completeExceptionally(ex);
2798 >        final CompletableFutureInc r = new CompletableFutureInc();
2799 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2800 >        checkCompletedWithWrappedCFException(g, ex);
2801 >        checkCompletedWithWrappedCFException(f, ex);
2802 >        }
2803      }
2804  
2805      /**
2806       * thenCompose result completes exceptionally if action does
2807       */
2808 <    public void testThenCompose3() {
2809 <        CompletableFuture<Integer> f, g;
2810 <        FailingCompletableFutureFunction r;
2808 >    public void testThenCompose_actionFailed1() {
2809 >        for (ExecutionMode m : ExecutionMode.values())
2810 >        for (Integer v1 : new Integer[] { 1, null }) {
2811  
2812 <        f = new CompletableFuture<>();
2813 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2814 <        f.complete(one);
2812 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2813 >        final FailingCompletableFutureFunction r
2814 >            = new FailingCompletableFutureFunction();
2815 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2816 >        f.complete(v1);
2817          checkCompletedWithWrappedCFException(g);
2818 +        checkCompletedNormally(f, v1);
2819 +        }
2820 +    }
2821  
2822 <        f = new CompletableFuture<>();
2823 <        f.complete(one);
2824 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2822 >    public void testThenCompose_actionFailed2() {
2823 >        for (ExecutionMode m : ExecutionMode.values())
2824 >        for (Integer v1 : new Integer[] { 1, null }) {
2825 >
2826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2827 >        f.complete(v1);
2828 >        final FailingCompletableFutureFunction r
2829 >            = new FailingCompletableFutureFunction();
2830 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2831          checkCompletedWithWrappedCFException(g);
2832 +        checkCompletedNormally(f, v1);
2833 +        }
2834      }
2835  
2836      /**
2837       * thenCompose result completes exceptionally if source cancelled
2838       */
2839 <    public void testThenCompose4() {
2840 <        CompletableFuture<Integer> f, g;
2841 <        CompletableFutureInc r;
2839 >    public void testThenCompose_sourceCancelled1() {
2840 >        for (ExecutionMode m : ExecutionMode.values())
2841 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2842  
2843 <        f = new CompletableFuture<>();
2844 <        g = f.thenCompose(r = new CompletableFutureInc());
2845 <        assertTrue(f.cancel(true));
2843 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2844 >        final CompletableFutureInc r = new CompletableFutureInc();
2845 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2846 >        assertTrue(f.cancel(mayInterruptIfRunning));
2847          checkCompletedWithWrappedCancellationException(g);
2848 +        checkCancelled(f);
2849 +        }
2850 +    }
2851  
2852 <        f = new CompletableFuture<>();
2853 <        assertTrue(f.cancel(true));
2854 <        g = f.thenCompose(r = new CompletableFutureInc());
2852 >    public void testThenCompose_sourceCancelled2() {
2853 >        for (ExecutionMode m : ExecutionMode.values())
2854 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
2855 >
2856 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2857 >        assertTrue(f.cancel(mayInterruptIfRunning));
2858 >        final CompletableFutureInc r = new CompletableFutureInc();
2859 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2860          checkCompletedWithWrappedCancellationException(g);
2861 +        checkCancelled(f);
2862 +        }
2863      }
2864  
2865      // asyncs
# Line 1894 | Line 2972 | public class CompletableFutureTest exten
2972          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2973          f.complete(one);
2974          checkCompletedNormally(g, null);
2975 <        assertEquals(r.value, 2);
2975 >        assertEquals(r.value, (Integer) 2);
2976      }
2977  
2978      /**
# Line 1931 | Line 3009 | public class CompletableFutureTest exten
3009          checkCompletedWithWrappedCancellationException(g);
3010      }
3011  
1934    /**
1935     * thenCombineAsync result completes normally after normal
1936     * completion of sources
1937     */
1938    public void testThenCombineAsync() {
1939        CompletableFuture<Integer> f, g, h;
1940
1941        f = new CompletableFuture<>();
1942        g = new CompletableFuture<>();
1943        h = f.thenCombineAsync(g, subtract);
1944        f.complete(3);
1945        checkIncomplete(h);
1946        g.complete(1);
1947        checkCompletedNormally(h, 2);
1948
1949        f = new CompletableFuture<>();
1950        g = new CompletableFuture<>();
1951        h = f.thenCombineAsync(g, subtract);
1952        g.complete(1);
1953        checkIncomplete(h);
1954        f.complete(3);
1955        checkCompletedNormally(h, 2);
1956
1957        f = new CompletableFuture<>();
1958        g = new CompletableFuture<>();
1959        g.complete(1);
1960        f.complete(3);
1961        h = f.thenCombineAsync(g, subtract);
1962        checkCompletedNormally(h, 2);
1963    }
1964
1965    /**
1966     * thenCombineAsync result completes exceptionally after exceptional
1967     * completion of either source
1968     */
1969    public void testThenCombineAsync2() {
1970        CompletableFuture<Integer> f, g, h;
1971
1972        f = new CompletableFuture<>();
1973        g = new CompletableFuture<>();
1974        h = f.thenCombineAsync(g, subtract);
1975        f.completeExceptionally(new CFException());
1976        checkIncomplete(h);
1977        g.complete(1);
1978        checkCompletedWithWrappedCFException(h);
1979
1980        f = new CompletableFuture<>();
1981        g = new CompletableFuture<>();
1982        h = f.thenCombineAsync(g, subtract);
1983        g.completeExceptionally(new CFException());
1984        checkIncomplete(h);
1985        f.complete(3);
1986        checkCompletedWithWrappedCFException(h);
1987
1988        f = new CompletableFuture<>();
1989        g = new CompletableFuture<>();
1990        g.completeExceptionally(new CFException());
1991        f.complete(3);
1992        h = f.thenCombineAsync(g, subtract);
1993        checkCompletedWithWrappedCFException(h);
1994    }
1995
1996    /**
1997     * thenCombineAsync result completes exceptionally if action does
1998     */
1999    public void testThenCombineAsync3() {
2000        CompletableFuture<Integer> f = new CompletableFuture<>();
2001        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2002        FailingBiFunction r = new FailingBiFunction();
2003        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
2004        f.complete(one);
2005        checkIncomplete(g);
2006        assertFalse(r.ran);
2007        f2.complete(two);
2008        checkCompletedWithWrappedCFException(g);
2009        assertTrue(r.ran);
2010    }
2011
2012    /**
2013     * thenCombineAsync result completes exceptionally if either source cancelled
2014     */
2015    public void testThenCombineAsync4() {
2016        CompletableFuture<Integer> f, g, h;
2017
2018        f = new CompletableFuture<>();
2019        g = new CompletableFuture<>();
2020        h = f.thenCombineAsync(g, subtract);
2021        assertTrue(f.cancel(true));
2022        checkIncomplete(h);
2023        g.complete(1);
2024        checkCompletedWithWrappedCancellationException(h);
2025
2026        f = new CompletableFuture<>();
2027        g = new CompletableFuture<>();
2028        h = f.thenCombineAsync(g, subtract);
2029        assertTrue(g.cancel(true));
2030        checkIncomplete(h);
2031        f.complete(3);
2032        checkCompletedWithWrappedCancellationException(h);
2033
2034        f = new CompletableFuture<>();
2035        g = new CompletableFuture<>();
2036        g.complete(3);
2037        assertTrue(f.cancel(true));
2038        h = f.thenCombineAsync(g, subtract);
2039        checkCompletedWithWrappedCancellationException(h);
2040
2041        f = new CompletableFuture<>();
2042        g = new CompletableFuture<>();
2043        f.complete(3);
2044        assertTrue(g.cancel(true));
2045        h = f.thenCombineAsync(g, subtract);
2046        checkCompletedWithWrappedCancellationException(h);
2047    }
2048
2049    /**
2050     * applyToEitherAsync result completes normally after normal
2051     * completion of sources
2052     */
2053    public void testApplyToEitherAsync() {
2054        CompletableFuture<Integer> f = new CompletableFuture<>();
2055        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2056        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2057        f.complete(one);
2058        checkCompletedNormally(g, two);
2059
2060        f = new CompletableFuture<>();
2061        f.complete(one);
2062        f2 = new CompletableFuture<>();
2063        g = f.applyToEitherAsync(f2, inc);
2064        checkCompletedNormally(g, two);
2065    }
2066
2067    /**
2068     * applyToEitherAsync result completes exceptionally after exceptional
2069     * completion of source
2070     */
2071    public void testApplyToEitherAsync2() {
2072        CompletableFuture<Integer> f = new CompletableFuture<>();
2073        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2074        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2075        f.completeExceptionally(new CFException());
2076        checkCompletedWithWrappedCFException(g);
2077
2078        f = new CompletableFuture<>();
2079        f2 = new CompletableFuture<>();
2080        f2.completeExceptionally(new CFException());
2081        g = f.applyToEitherAsync(f2, inc);
2082        f.complete(one);
2083        checkCompletedWithWrappedCFException(g);
2084    }
2085
2086    /**
2087     * applyToEitherAsync result completes exceptionally if action does
2088     */
2089    public void testApplyToEitherAsync3() {
2090        CompletableFuture<Integer> f = new CompletableFuture<>();
2091        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2092        FailingFunction r = new FailingFunction();
2093        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2094        f.complete(one);
2095        checkCompletedWithWrappedCFException(g);
2096    }
2097
2098    /**
2099     * applyToEitherAsync result completes exceptionally if either source cancelled
2100     */
2101    public void testApplyToEitherAsync4() {
2102        CompletableFuture<Integer> f = new CompletableFuture<>();
2103        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2104        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2105        assertTrue(f.cancel(true));
2106        checkCompletedWithWrappedCancellationException(g);
2107
2108        f = new CompletableFuture<>();
2109        f2 = new CompletableFuture<>();
2110        assertTrue(f2.cancel(true));
2111        g = f.applyToEitherAsync(f2, inc);
2112        checkCompletedWithWrappedCancellationException(g);
2113    }
2114
2115    /**
2116     * acceptEitherAsync result completes normally after normal
2117     * completion of sources
2118     */
2119    public void testAcceptEitherAsync() {
2120        CompletableFuture<Integer> f = new CompletableFuture<>();
2121        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2122        IncAction r = new IncAction();
2123        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2124        f.complete(one);
2125        checkCompletedNormally(g, null);
2126        assertEquals(r.value, 2);
2127
2128        r = new IncAction();
2129        f = new CompletableFuture<>();
2130        f.complete(one);
2131        f2 = new CompletableFuture<>();
2132        g = f.acceptEitherAsync(f2, r);
2133        checkCompletedNormally(g, null);
2134        assertEquals(r.value, 2);
2135    }
2136
2137    /**
2138     * acceptEitherAsync result completes exceptionally after exceptional
2139     * completion of source
2140     */
2141    public void testAcceptEitherAsync2() {
2142        CompletableFuture<Integer> f = new CompletableFuture<>();
2143        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2144        IncAction r = new IncAction();
2145        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2146        f.completeExceptionally(new CFException());
2147        checkCompletedWithWrappedCFException(g);
2148
2149        r = new IncAction();
2150        f = new CompletableFuture<>();
2151        f2 = new CompletableFuture<>();
2152        f2.completeExceptionally(new CFException());
2153        g = f.acceptEitherAsync(f2, r);
2154        f.complete(one);
2155        checkCompletedWithWrappedCFException(g);
2156    }
2157
2158    /**
2159     * acceptEitherAsync result completes exceptionally if action does
2160     */
2161    public void testAcceptEitherAsync3() {
2162        CompletableFuture<Integer> f = new CompletableFuture<>();
2163        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2164        FailingConsumer r = new FailingConsumer();
2165        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2166        f.complete(one);
2167        checkCompletedWithWrappedCFException(g);
2168    }
2169
2170    /**
2171     * acceptEitherAsync result completes exceptionally if either
2172     * source cancelled
2173     */
2174    public void testAcceptEitherAsync4() {
2175        CompletableFuture<Integer> f = new CompletableFuture<>();
2176        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2177        IncAction r = new IncAction();
2178        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2179        assertTrue(f.cancel(true));
2180        checkCompletedWithWrappedCancellationException(g);
2181
2182        r = new IncAction();
2183        f = new CompletableFuture<>();
2184        f2 = new CompletableFuture<>();
2185        assertTrue(f2.cancel(true));
2186        g = f.acceptEitherAsync(f2, r);
2187        checkCompletedWithWrappedCancellationException(g);
2188    }
2189
2190    /**
2191     * runAfterEitherAsync result completes normally after normal
2192     * completion of sources
2193     */
2194    public void testRunAfterEitherAsync() {
2195        CompletableFuture<Integer> f = new CompletableFuture<>();
2196        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2197        Noop r = new Noop();
2198        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2199        f.complete(one);
2200        checkCompletedNormally(g, null);
2201        assertTrue(r.ran);
2202
2203        r = new Noop();
2204        f = new CompletableFuture<>();
2205        f.complete(one);
2206        f2 = new CompletableFuture<>();
2207        g = f.runAfterEitherAsync(f2, r);
2208        checkCompletedNormally(g, null);
2209        assertTrue(r.ran);
2210    }
2211
2212    /**
2213     * runAfterEitherAsync result completes exceptionally after exceptional
2214     * completion of source
2215     */
2216    public void testRunAfterEitherAsync2() {
2217        CompletableFuture<Integer> f = new CompletableFuture<>();
2218        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2219        Noop r = new Noop();
2220        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2221        f.completeExceptionally(new CFException());
2222        checkCompletedWithWrappedCFException(g);
2223
2224        r = new Noop();
2225        f = new CompletableFuture<>();
2226        f2 = new CompletableFuture<>();
2227        f2.completeExceptionally(new CFException());
2228        g = f.runAfterEitherAsync(f2, r);
2229        f.complete(one);
2230        checkCompletedWithWrappedCFException(g);
2231    }
2232
2233    /**
2234     * runAfterEitherAsync result completes exceptionally if action does
2235     */
2236    public void testRunAfterEitherAsync3() {
2237        CompletableFuture<Integer> f = new CompletableFuture<>();
2238        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2239        FailingNoop r = new FailingNoop();
2240        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2241        f.complete(one);
2242        checkCompletedWithWrappedCFException(g);
2243    }
2244
2245    /**
2246     * runAfterEitherAsync result completes exceptionally if either
2247     * source cancelled
2248     */
2249    public void testRunAfterEitherAsync4() {
2250        CompletableFuture<Integer> f = new CompletableFuture<>();
2251        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2252        Noop r = new Noop();
2253        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2254        assertTrue(f.cancel(true));
2255        checkCompletedWithWrappedCancellationException(g);
2256
2257        r = new Noop();
2258        f = new CompletableFuture<>();
2259        f2 = new CompletableFuture<>();
2260        assertTrue(f2.cancel(true));
2261        g = f.runAfterEitherAsync(f2, r);
2262        checkCompletedWithWrappedCancellationException(g);
2263    }
2264
2265    /**
2266     * thenComposeAsync result completes normally after normal
2267     * completion of source
2268     */
2269    public void testThenComposeAsync() {
2270        CompletableFuture<Integer> f, g;
2271        CompletableFutureInc r;
2272
2273        f = new CompletableFuture<>();
2274        g = f.thenComposeAsync(r = new CompletableFutureInc());
2275        f.complete(one);
2276        checkCompletedNormally(g, two);
2277
2278        f = new CompletableFuture<>();
2279        f.complete(one);
2280        g = f.thenComposeAsync(r = new CompletableFutureInc());
2281        checkCompletedNormally(g, two);
2282    }
2283
2284    /**
2285     * thenComposeAsync result completes exceptionally after
2286     * exceptional completion of source
2287     */
2288    public void testThenComposeAsync2() {
2289        CompletableFuture<Integer> f, g;
2290        CompletableFutureInc r;
2291
2292        f = new CompletableFuture<>();
2293        g = f.thenComposeAsync(r = new CompletableFutureInc());
2294        f.completeExceptionally(new CFException());
2295        checkCompletedWithWrappedCFException(g);
2296        assertFalse(r.ran);
2297
2298        f = new CompletableFuture<>();
2299        f.completeExceptionally(new CFException());
2300        g = f.thenComposeAsync(r = new CompletableFutureInc());
2301        checkCompletedWithWrappedCFException(g);
2302        assertFalse(r.ran);
2303    }
2304
2305    /**
2306     * thenComposeAsync result completes exceptionally if action does
2307     */
2308    public void testThenComposeAsync3() {
2309        CompletableFuture<Integer> f, g;
2310        FailingCompletableFutureFunction r;
2311
2312        f = new CompletableFuture<>();
2313        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2314        f.complete(one);
2315        checkCompletedWithWrappedCFException(g);
2316
2317        f = new CompletableFuture<>();
2318        f.complete(one);
2319        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2320        checkCompletedWithWrappedCFException(g);
2321    }
2322
2323    /**
2324     * thenComposeAsync result completes exceptionally if source cancelled
2325     */
2326    public void testThenComposeAsync4() {
2327        CompletableFuture<Integer> f, g;
2328        CompletableFutureInc r;
2329
2330        f = new CompletableFuture<>();
2331        g = f.thenComposeAsync(r = new CompletableFutureInc());
2332        assertTrue(f.cancel(true));
2333        checkCompletedWithWrappedCancellationException(g);
2334
2335        f = new CompletableFuture<>();
2336        assertTrue(f.cancel(true));
2337        g = f.thenComposeAsync(r = new CompletableFutureInc());
2338        checkCompletedWithWrappedCancellationException(g);
2339    }
2340
3012      // async with explicit executors
3013  
3014      /**
# Line 2448 | Line 3119 | public class CompletableFutureTest exten
3119          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3120          f.complete(one);
3121          checkCompletedNormally(g, null);
3122 <        assertEquals(r.value, 2);
3122 >        assertEquals(r.value, (Integer) 2);
3123      }
3124  
3125      /**
# Line 2485 | Line 3156 | public class CompletableFutureTest exten
3156          checkCompletedWithWrappedCancellationException(g);
3157      }
3158  
2488    /**
2489     * thenCombineAsync result completes normally after normal
2490     * completion of sources
2491     */
2492    public void testThenCombineAsyncE() {
2493        CompletableFuture<Integer> f, g, h;
2494        ThreadExecutor e = new ThreadExecutor();
2495        int count = 0;
2496
2497        f = new CompletableFuture<>();
2498        g = new CompletableFuture<>();
2499        h = f.thenCombineAsync(g, subtract, e);
2500        f.complete(3);
2501        checkIncomplete(h);
2502        g.complete(1);
2503        checkCompletedNormally(h, 2);
2504        assertEquals(++count, e.count.get());
2505
2506        f = new CompletableFuture<>();
2507        g = new CompletableFuture<>();
2508        h = f.thenCombineAsync(g, subtract, e);
2509        g.complete(1);
2510        checkIncomplete(h);
2511        f.complete(3);
2512        checkCompletedNormally(h, 2);
2513        assertEquals(++count, e.count.get());
2514
2515        f = new CompletableFuture<>();
2516        g = new CompletableFuture<>();
2517        g.complete(1);
2518        f.complete(3);
2519        h = f.thenCombineAsync(g, subtract, e);
2520        checkCompletedNormally(h, 2);
2521        assertEquals(++count, e.count.get());
2522    }
2523
2524    /**
2525     * thenCombineAsync result completes exceptionally after exceptional
2526     * completion of either source
2527     */
2528    public void testThenCombineAsync2E() {
2529        CompletableFuture<Integer> f, g, h;
2530        ThreadExecutor e = new ThreadExecutor();
2531        int count = 0;
2532
2533        f = new CompletableFuture<>();
2534        g = new CompletableFuture<>();
2535        h = f.thenCombineAsync(g, subtract, e);
2536        f.completeExceptionally(new CFException());
2537        checkIncomplete(h);
2538        g.complete(1);
2539        checkCompletedWithWrappedCFException(h);
2540
2541        f = new CompletableFuture<>();
2542        g = new CompletableFuture<>();
2543        h = f.thenCombineAsync(g, subtract, e);
2544        g.completeExceptionally(new CFException());
2545        checkIncomplete(h);
2546        f.complete(3);
2547        checkCompletedWithWrappedCFException(h);
2548
2549        f = new CompletableFuture<>();
2550        g = new CompletableFuture<>();
2551        g.completeExceptionally(new CFException());
2552        h = f.thenCombineAsync(g, subtract, e);
2553        checkIncomplete(h);
2554        f.complete(3);
2555        checkCompletedWithWrappedCFException(h);
2556
2557        assertEquals(0, e.count.get());
2558    }
2559
2560    /**
2561     * thenCombineAsync result completes exceptionally if action does
2562     */
2563    public void testThenCombineAsync3E() {
2564        CompletableFuture<Integer> f = new CompletableFuture<>();
2565        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2566        FailingBiFunction r = new FailingBiFunction();
2567        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2568        f.complete(one);
2569        checkIncomplete(g);
2570        assertFalse(r.ran);
2571        f2.complete(two);
2572        checkCompletedWithWrappedCFException(g);
2573        assertTrue(r.ran);
2574    }
2575
2576    /**
2577     * thenCombineAsync result completes exceptionally if either source cancelled
2578     */
2579    public void testThenCombineAsync4E() {
2580        CompletableFuture<Integer> f, g, h;
2581        ThreadExecutor e = new ThreadExecutor();
2582
2583        f = new CompletableFuture<>();
2584        g = new CompletableFuture<>();
2585        h = f.thenCombineAsync(g, subtract, e);
2586        assertTrue(f.cancel(true));
2587        checkIncomplete(h);
2588        g.complete(1);
2589        checkCompletedWithWrappedCancellationException(h);
2590
2591        f = new CompletableFuture<>();
2592        g = new CompletableFuture<>();
2593        h = f.thenCombineAsync(g, subtract, e);
2594        assertTrue(g.cancel(true));
2595        checkIncomplete(h);
2596        f.complete(3);
2597        checkCompletedWithWrappedCancellationException(h);
2598
2599        f = new CompletableFuture<>();
2600        g = new CompletableFuture<>();
2601        assertTrue(g.cancel(true));
2602        h = f.thenCombineAsync(g, subtract, e);
2603        checkIncomplete(h);
2604        f.complete(3);
2605        checkCompletedWithWrappedCancellationException(h);
2606
2607        f = new CompletableFuture<>();
2608        g = new CompletableFuture<>();
2609        assertTrue(f.cancel(true));
2610        assertTrue(g.cancel(true));
2611        h = f.thenCombineAsync(g, subtract, e);
2612        checkCompletedWithWrappedCancellationException(h);
2613
2614        assertEquals(0, e.count.get());
2615    }
2616
2617    /**
2618     * applyToEitherAsync result completes normally after normal
2619     * completion of sources
2620     */
2621    public void testApplyToEitherAsyncE() {
2622        CompletableFuture<Integer> f = new CompletableFuture<>();
2623        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2624        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625        f.complete(one);
2626        checkCompletedNormally(g, two);
2627
2628        f = new CompletableFuture<>();
2629        f.complete(one);
2630        f2 = new CompletableFuture<>();
2631        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2632        checkCompletedNormally(g, two);
2633    }
2634
2635    /**
2636     * applyToEitherAsync result completes exceptionally after exceptional
2637     * completion of source
2638     */
2639    public void testApplyToEitherAsync2E() {
2640        CompletableFuture<Integer> f = new CompletableFuture<>();
2641        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2642        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2643        f.completeExceptionally(new CFException());
2644        checkCompletedWithWrappedCFException(g);
2645
2646        f = new CompletableFuture<>();
2647        f2 = new CompletableFuture<>();
2648        f2.completeExceptionally(new CFException());
2649        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2650        f.complete(one);
2651        checkCompletedWithWrappedCFException(g);
2652    }
2653
2654    /**
2655     * applyToEitherAsync result completes exceptionally if action does
2656     */
2657    public void testApplyToEitherAsync3E() {
2658        CompletableFuture<Integer> f = new CompletableFuture<>();
2659        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2660        FailingFunction r = new FailingFunction();
2661        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2662        f.complete(one);
2663        checkCompletedWithWrappedCFException(g);
2664    }
2665
2666    /**
2667     * applyToEitherAsync result completes exceptionally if either source cancelled
2668     */
2669    public void testApplyToEitherAsync4E() {
2670        CompletableFuture<Integer> f = new CompletableFuture<>();
2671        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2672        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2673        assertTrue(f.cancel(true));
2674        checkCompletedWithWrappedCancellationException(g);
2675
2676        f = new CompletableFuture<>();
2677        f2 = new CompletableFuture<>();
2678        assertTrue(f2.cancel(true));
2679        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2680        checkCompletedWithWrappedCancellationException(g);
2681    }
2682
2683    /**
2684     * acceptEitherAsync result completes normally after normal
2685     * completion of sources
2686     */
2687    public void testAcceptEitherAsyncE() {
2688        CompletableFuture<Integer> f = new CompletableFuture<>();
2689        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2690        IncAction r = new IncAction();
2691        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2692        f.complete(one);
2693        checkCompletedNormally(g, null);
2694        assertEquals(r.value, 2);
2695
2696        r = new IncAction();
2697        f = new CompletableFuture<>();
2698        f.complete(one);
2699        f2 = new CompletableFuture<>();
2700        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2701        checkCompletedNormally(g, null);
2702        assertEquals(r.value, 2);
2703    }
2704
2705    /**
2706     * acceptEitherAsync result completes exceptionally after exceptional
2707     * completion of source
2708     */
2709    public void testAcceptEitherAsync2E() {
2710        CompletableFuture<Integer> f = new CompletableFuture<>();
2711        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2712        IncAction r = new IncAction();
2713        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2714        f.completeExceptionally(new CFException());
2715        checkCompletedWithWrappedCFException(g);
2716
2717        r = new IncAction();
2718        f = new CompletableFuture<>();
2719        f2 = new CompletableFuture<>();
2720        f2.completeExceptionally(new CFException());
2721        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722        f.complete(one);
2723        checkCompletedWithWrappedCFException(g);
2724    }
2725
2726    /**
2727     * acceptEitherAsync result completes exceptionally if action does
2728     */
2729    public void testAcceptEitherAsync3E() {
2730        CompletableFuture<Integer> f = new CompletableFuture<>();
2731        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2732        FailingConsumer r = new FailingConsumer();
2733        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2734        f.complete(one);
2735        checkCompletedWithWrappedCFException(g);
2736    }
2737
2738    /**
2739     * acceptEitherAsync result completes exceptionally if either
2740     * source cancelled
2741     */
2742    public void testAcceptEitherAsync4E() {
2743        CompletableFuture<Integer> f = new CompletableFuture<>();
2744        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2745        IncAction r = new IncAction();
2746        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2747        assertTrue(f.cancel(true));
2748        checkCompletedWithWrappedCancellationException(g);
2749
2750        r = new IncAction();
2751        f = new CompletableFuture<>();
2752        f2 = new CompletableFuture<>();
2753        assertTrue(f2.cancel(true));
2754        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2755        checkCompletedWithWrappedCancellationException(g);
2756    }
2757
2758    /**
2759     * runAfterEitherAsync result completes normally after normal
2760     * completion of sources
2761     */
2762    public void testRunAfterEitherAsyncE() {
2763        CompletableFuture<Integer> f = new CompletableFuture<>();
2764        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2765        Noop r = new Noop();
2766        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2767        f.complete(one);
2768        checkCompletedNormally(g, null);
2769        assertTrue(r.ran);
2770
2771        r = new Noop();
2772        f = new CompletableFuture<>();
2773        f.complete(one);
2774        f2 = new CompletableFuture<>();
2775        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2776        checkCompletedNormally(g, null);
2777        assertTrue(r.ran);
2778    }
2779
2780    /**
2781     * runAfterEitherAsync result completes exceptionally after exceptional
2782     * completion of source
2783     */
2784    public void testRunAfterEitherAsync2E() {
2785        CompletableFuture<Integer> f = new CompletableFuture<>();
2786        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2787        Noop r = new Noop();
2788        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2789        f.completeExceptionally(new CFException());
2790        checkCompletedWithWrappedCFException(g);
2791
2792        r = new Noop();
2793        f = new CompletableFuture<>();
2794        f2 = new CompletableFuture<>();
2795        f2.completeExceptionally(new CFException());
2796        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797        f.complete(one);
2798        checkCompletedWithWrappedCFException(g);
2799    }
2800
2801    /**
2802     * runAfterEitherAsync result completes exceptionally if action does
2803     */
2804    public void testRunAfterEitherAsync3E() {
2805        CompletableFuture<Integer> f = new CompletableFuture<>();
2806        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2807        FailingNoop r = new FailingNoop();
2808        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2809        f.complete(one);
2810        checkCompletedWithWrappedCFException(g);
2811    }
2812
2813    /**
2814     * runAfterEitherAsync result completes exceptionally if either
2815     * source cancelled
2816     */
2817    public void testRunAfterEitherAsync4E() {
2818        CompletableFuture<Integer> f = new CompletableFuture<>();
2819        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2820        Noop r = new Noop();
2821        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2822        assertTrue(f.cancel(true));
2823        checkCompletedWithWrappedCancellationException(g);
2824
2825        r = new Noop();
2826        f = new CompletableFuture<>();
2827        f2 = new CompletableFuture<>();
2828        assertTrue(f2.cancel(true));
2829        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2830        checkCompletedWithWrappedCancellationException(g);
2831    }
2832
2833    /**
2834     * thenComposeAsync result completes normally after normal
2835     * completion of source
2836     */
2837    public void testThenComposeAsyncE() {
2838        CompletableFuture<Integer> f = new CompletableFuture<>();
2839        CompletableFutureInc r = new CompletableFutureInc();
2840        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2841        f.complete(one);
2842        checkCompletedNormally(g, two);
2843    }
2844
2845    /**
2846     * thenComposeAsync result completes exceptionally after
2847     * exceptional completion of source
2848     */
2849    public void testThenComposeAsync2E() {
2850        CompletableFuture<Integer> f = new CompletableFuture<>();
2851        CompletableFutureInc r = new CompletableFutureInc();
2852        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2853        f.completeExceptionally(new CFException());
2854        checkCompletedWithWrappedCFException(g);
2855    }
2856
2857    /**
2858     * thenComposeAsync result completes exceptionally if action does
2859     */
2860    public void testThenComposeAsync3E() {
2861        CompletableFuture<Integer> f = new CompletableFuture<>();
2862        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2863        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2864        f.complete(one);
2865        checkCompletedWithWrappedCFException(g);
2866    }
2867
2868    /**
2869     * thenComposeAsync result completes exceptionally if source cancelled
2870     */
2871    public void testThenComposeAsync4E() {
2872        CompletableFuture<Integer> f = new CompletableFuture<>();
2873        CompletableFutureInc r = new CompletableFutureInc();
2874        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2875        assertTrue(f.cancel(true));
2876        checkCompletedWithWrappedCancellationException(g);
2877    }
2878
3159      // other static methods
3160  
3161      /**
# Line 3074 | Line 3354 | public class CompletableFutureTest exten
3354       * whenComplete action executes on normal completion, propagating
3355       * source result.
3356       */
3357 <    public void testWhenComplete1() {
3357 >    public void testWhenComplete_normalCompletion1() {
3358 >        for (boolean createIncomplete : new boolean[] { true, false })
3359 >        for (ExecutionMode m : ExecutionMode.values())
3360 >        for (Integer v1 : new Integer[] { 1, null }) {
3361 >
3362          final AtomicInteger a = new AtomicInteger();
3363 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3364 <        CompletableFuture<Integer> g =
3365 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3366 <        f.complete(three);
3367 <        checkCompletedNormally(f, three);
3368 <        checkCompletedNormally(g, three);
3363 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3364 >        if (!createIncomplete) f.complete(v1);
3365 >        final CompletableFuture<Integer> g =
3366 >            m.whenComplete(f,
3367 >                           (Integer x, Throwable t) -> {
3368 >                               threadAssertSame(x, v1);
3369 >                               threadAssertNull(t);
3370 >                               a.getAndIncrement();
3371 >                           });
3372 >        if (createIncomplete) f.complete(v1);
3373 >        checkCompletedNormally(f, v1);
3374 >        checkCompletedNormally(g, v1);
3375          assertEquals(a.get(), 1);
3376 +        }
3377      }
3378  
3379      /**
3380       * whenComplete action executes on exceptional completion, propagating
3381       * source result.
3382       */
3383 <    public void testWhenComplete2() {
3383 >    public void testWhenComplete_exceptionalCompletion() {
3384 >        for (boolean createIncomplete : new boolean[] { true, false })
3385 >        for (ExecutionMode m : ExecutionMode.values())
3386 >        for (Integer v1 : new Integer[] { 1, null }) {
3387 >
3388          final AtomicInteger a = new AtomicInteger();
3389 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3390 <        CompletableFuture<Integer> g =
3391 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3392 <        f.completeExceptionally(new CFException());
3393 <        assertTrue(f.isCompletedExceptionally());
3394 <        assertTrue(g.isCompletedExceptionally());
3389 >        final CFException ex = new CFException();
3390 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3391 >        if (!createIncomplete) f.completeExceptionally(ex);
3392 >        final CompletableFuture<Integer> g = m.whenComplete
3393 >            (f,
3394 >             (Integer x, Throwable t) -> {
3395 >                threadAssertNull(x);
3396 >                threadAssertSame(t, ex);
3397 >                a.getAndIncrement();
3398 >            });
3399 >        if (createIncomplete) f.completeExceptionally(ex);
3400 >        checkCompletedWithWrappedCFException(f, ex);
3401 >        checkCompletedWithWrappedCFException(g, ex);
3402          assertEquals(a.get(), 1);
3403 +        }
3404      }
3405  
3406      /**
3407       * If a whenComplete action throws an exception when triggered by
3408       * a normal completion, it completes exceptionally
3409       */
3410 <    public void testWhenComplete3() {
3411 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3412 <        CompletableFuture<Integer> g =
3413 <            f.whenComplete((Integer x, Throwable t) ->
3111 <                           { throw new CFException(); } );
3112 <        f.complete(three);
3113 <        checkCompletedNormally(f, three);
3114 <        assertTrue(g.isCompletedExceptionally());
3115 <        checkCompletedWithWrappedCFException(g);
3116 <    }
3117 <
3118 <    /**
3119 <     * whenCompleteAsync action executes on normal completion, propagating
3120 <     * source result.
3121 <     */
3122 <    public void testWhenCompleteAsync1() {
3123 <        final AtomicInteger a = new AtomicInteger();
3124 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3125 <        CompletableFuture<Integer> g =
3126 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3127 <        f.complete(three);
3128 <        checkCompletedNormally(f, three);
3129 <        checkCompletedNormally(g, three);
3130 <        assertEquals(a.get(), 1);
3131 <    }
3410 >    public void testWhenComplete_actionFailed() {
3411 >        for (boolean createIncomplete : new boolean[] { true, false })
3412 >        for (ExecutionMode m : ExecutionMode.values())
3413 >        for (Integer v1 : new Integer[] { 1, null }) {
3414  
3415 <    /**
3416 <     * whenCompleteAsync action executes on exceptional completion, propagating
3417 <     * source result.
3418 <     */
3419 <    public void testWhenCompleteAsync2() {
3420 <        final AtomicInteger a = new AtomicInteger();
3421 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3422 <        CompletableFuture<Integer> g =
3423 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3424 <        f.completeExceptionally(new CFException());
3425 <        checkCompletedWithWrappedCFException(f);
3426 <        checkCompletedWithWrappedCFException(g);
3415 >        final CFException ex = new CFException();
3416 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3417 >        if (!createIncomplete) f.complete(v1);
3418 >        final CompletableFuture<Integer> g = m.whenComplete
3419 >            (f,
3420 >             (Integer x, Throwable t) -> {
3421 >                threadAssertSame(x, v1);
3422 >                threadAssertNull(t);
3423 >                throw ex;
3424 >            });
3425 >        if (createIncomplete) f.complete(v1);
3426 >        checkCompletedNormally(f, v1);
3427 >        checkCompletedWithWrappedCFException(g, ex);
3428 >        }
3429      }
3430  
3431      /**
3432 <     * If a whenCompleteAsync action throws an exception when
3433 <     * triggered by a normal completion, it completes exceptionally
3432 >     * If a whenComplete action throws an exception when triggered by
3433 >     * a source completion that also throws an exception, the source
3434 >     * exception takes precedence.
3435       */
3436 <    public void testWhenCompleteAsync3() {
3437 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3438 <        CompletableFuture<Integer> g =
3439 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3155 <                           { throw new CFException(); } );
3156 <        f.complete(three);
3157 <        checkCompletedNormally(f, three);
3158 <        checkCompletedWithWrappedCFException(g);
3159 <    }
3436 >    public void testWhenComplete_actionFailedSourceFailed() {
3437 >        for (boolean createIncomplete : new boolean[] { true, false })
3438 >        for (ExecutionMode m : ExecutionMode.values())
3439 >        for (Integer v1 : new Integer[] { 1, null }) {
3440  
3441 <    /**
3442 <     * whenCompleteAsync action executes on normal completion, propagating
3443 <     * source result.
3164 <     */
3165 <    public void testWhenCompleteAsync1e() {
3166 <        final AtomicInteger a = new AtomicInteger();
3167 <        ThreadExecutor exec = new ThreadExecutor();
3168 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3169 <        CompletableFuture<Integer> g =
3170 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3171 <                                exec);
3172 <        f.complete(three);
3173 <        checkCompletedNormally(f, three);
3174 <        checkCompletedNormally(g, three);
3175 <        assertEquals(a.get(), 1);
3176 <    }
3441 >        final CFException ex1 = new CFException();
3442 >        final CFException ex2 = new CFException();
3443 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3444  
3445 <    /**
3446 <     * whenCompleteAsync action executes on exceptional completion, propagating
3447 <     * source result.
3448 <     */
3449 <    public void testWhenCompleteAsync2e() {
3450 <        final AtomicInteger a = new AtomicInteger();
3451 <        ThreadExecutor exec = new ThreadExecutor();
3452 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3453 <        CompletableFuture<Integer> g =
3187 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3188 <                                exec);
3189 <        f.completeExceptionally(new CFException());
3190 <        checkCompletedWithWrappedCFException(f);
3191 <        checkCompletedWithWrappedCFException(g);
3192 <    }
3445 >        if (!createIncomplete) f.completeExceptionally(ex1);
3446 >        final CompletableFuture<Integer> g = m.whenComplete
3447 >            (f,
3448 >             (Integer x, Throwable t) -> {
3449 >                threadAssertSame(t, ex1);
3450 >                threadAssertNull(x);
3451 >                throw ex2;
3452 >            });
3453 >        if (createIncomplete) f.completeExceptionally(ex1);
3454  
3455 <    /**
3456 <     * If a whenCompleteAsync action throws an exception when triggered
3457 <     * by a normal completion, it completes exceptionally
3197 <     */
3198 <    public void testWhenCompleteAsync3e() {
3199 <        ThreadExecutor exec = new ThreadExecutor();
3200 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3201 <        CompletableFuture<Integer> g =
3202 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3203 <                                { throw new CFException(); },
3204 <                                exec);
3205 <        f.complete(three);
3206 <        checkCompletedNormally(f, three);
3207 <        checkCompletedWithWrappedCFException(g);
3455 >        checkCompletedWithWrappedCFException(f, ex1);
3456 >        checkCompletedWithWrappedCFException(g, ex1);
3457 >        }
3458      }
3459  
3460      /**
# Line 3217 | Line 3467 | public class CompletableFutureTest exten
3467  
3468          f = new CompletableFuture<>();
3469          g = f.handleAsync(r = new IntegerHandler());
3470 <        assertFalse(r.ran);
3470 >        assertEquals(0, r.invocationCount);
3471          f.completeExceptionally(new CFException());
3472          checkCompletedWithWrappedCFException(f);
3473          checkCompletedNormally(g, three);
3474 <        assertTrue(r.ran);
3474 >        assertEquals(1, r.invocationCount);
3475  
3476          f = new CompletableFuture<>();
3477          g = f.handleAsync(r = new IntegerHandler());
3478 <        assertFalse(r.ran);
3478 >        assertEquals(0, r.invocationCount);
3479          f.completeExceptionally(new CFException());
3480          checkCompletedWithWrappedCFException(f);
3481          checkCompletedNormally(g, three);
3482 <        assertTrue(r.ran);
3482 >        assertEquals(1, r.invocationCount);
3483  
3484          f = new CompletableFuture<>();
3485          g = f.handleAsync(r = new IntegerHandler());
3486 <        assertFalse(r.ran);
3486 >        assertEquals(0, r.invocationCount);
3487          f.complete(one);
3488          checkCompletedNormally(f, one);
3489          checkCompletedNormally(g, two);
3490 <        assertTrue(r.ran);
3490 >        assertEquals(1, r.invocationCount);
3491  
3492          f = new CompletableFuture<>();
3493          g = f.handleAsync(r = new IntegerHandler());
3494 <        assertFalse(r.ran);
3494 >        assertEquals(0, r.invocationCount);
3495          f.complete(one);
3496          checkCompletedNormally(f, one);
3497          checkCompletedNormally(g, two);
3498 <        assertTrue(r.ran);
3498 >        assertEquals(1, r.invocationCount);
3499      }
3500  
3501      /**
# Line 3260 | Line 3510 | public class CompletableFutureTest exten
3510  
3511          f = new CompletableFuture<>();
3512          g = f.handleAsync(r = new IntegerHandler(), exec);
3513 <        assertFalse(r.ran);
3513 >        assertEquals(0, r.invocationCount);
3514          f.completeExceptionally(new CFException());
3515          checkCompletedWithWrappedCFException(f);
3516          checkCompletedNormally(g, three);
3517 <        assertTrue(r.ran);
3517 >        assertEquals(1, r.invocationCount);
3518  
3519          f = new CompletableFuture<>();
3520          g = f.handleAsync(r = new IntegerHandler(), exec);
3521 <        assertFalse(r.ran);
3521 >        assertEquals(0, r.invocationCount);
3522          f.completeExceptionally(new CFException());
3523          checkCompletedWithWrappedCFException(f);
3524          checkCompletedNormally(g, three);
3525 <        assertTrue(r.ran);
3525 >        assertEquals(1, r.invocationCount);
3526  
3527          f = new CompletableFuture<>();
3528          g = f.handleAsync(r = new IntegerHandler(), exec);
3529 <        assertFalse(r.ran);
3529 >        assertEquals(0, r.invocationCount);
3530          f.complete(one);
3531          checkCompletedNormally(f, one);
3532          checkCompletedNormally(g, two);
3533 <        assertTrue(r.ran);
3533 >        assertEquals(1, r.invocationCount);
3534  
3535          f = new CompletableFuture<>();
3536          g = f.handleAsync(r = new IntegerHandler(), exec);
3537 <        assertFalse(r.ran);
3537 >        assertEquals(0, r.invocationCount);
3538          f.complete(one);
3539          checkCompletedNormally(f, one);
3540          checkCompletedNormally(g, two);
3541 <        assertTrue(r.ran);
3541 >        assertEquals(1, r.invocationCount);
3542      }
3543  
3544   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines