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.25 by jsr166, Sat Apr 20 23:28:35 2013 UTC vs.
Revision 1.43 by jsr166, Mon Jun 2 04:13:54 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19 + import java.util.concurrent.CompletionStage;
20   import java.util.concurrent.TimeoutException;
21   import java.util.concurrent.atomic.AtomicInteger;
22   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 68 | Line 69 | public class CompletableFutureTest exten
69          } catch (Throwable fail) { threadUnexpectedException(fail); }
70          assertTrue(f.isDone());
71          assertFalse(f.isCancelled());
72 +        assertFalse(f.isCompletedExceptionally());
73          assertTrue(f.toString().contains("[Completed normally]"));
74      }
75  
# Line 101 | Line 103 | public class CompletableFutureTest exten
103          assertTrue(f.toString().contains("[Completed exceptionally]"));
104      }
105  
106 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
107 +                                              CFException ex) {
108 +        try {
109 +            f.get(LONG_DELAY_MS, MILLISECONDS);
110 +            shouldThrow();
111 +        } catch (ExecutionException success) {
112 +            assertSame(ex, success.getCause());
113 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
114 +        try {
115 +            f.join();
116 +            shouldThrow();
117 +        } catch (CompletionException success) {
118 +            assertSame(ex, success.getCause());
119 +        }
120 +        try {
121 +            f.getNow(null);
122 +            shouldThrow();
123 +        } catch (CompletionException success) {
124 +            assertSame(ex, success.getCause());
125 +        }
126 +        try {
127 +            f.get();
128 +            shouldThrow();
129 +        } catch (ExecutionException success) {
130 +            assertSame(ex, success.getCause());
131 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
132 +        assertTrue(f.isDone());
133 +        assertFalse(f.isCancelled());
134 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
135 +    }
136 +
137      void checkCancelled(CompletableFuture<?> f) {
138          try {
139              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 121 | Line 154 | public class CompletableFutureTest exten
154          } catch (CancellationException success) {
155          } catch (Throwable fail) { threadUnexpectedException(fail); }
156          assertTrue(f.isDone());
157 +        assertTrue(f.isCompletedExceptionally());
158          assertTrue(f.isCancelled());
159          assertTrue(f.toString().contains("[Completed exceptionally]"));
160      }
# Line 152 | Line 186 | public class CompletableFutureTest exten
186          } catch (Throwable fail) { threadUnexpectedException(fail); }
187          assertTrue(f.isDone());
188          assertFalse(f.isCancelled());
189 +        assertTrue(f.isCompletedExceptionally());
190          assertTrue(f.toString().contains("[Completed exceptionally]"));
191      }
192  
# Line 256 | Line 291 | public class CompletableFutureTest exten
291          assertEquals(g.getNumberOfDependents(), 0);
292      }
293  
259
294      /**
295       * toString indicates current completion state
296       */
# Line 284 | 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 boolean ran() { return invocationCount == 1; }
343 >        public void accept(Integer x) {
344 >            invocationCount++;
345 >            value = inc(x);
346 >        }
347 >    }
348 >    static final class IncFunction implements Function<Integer,Integer> {
349 >        int invocationCount = 0;
350 >        Integer value;
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356      }
357      static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 <        int value;
358 >        int invocationCount = 0;
359 >        Integer value;
360 >        // Check this action was invoked exactly once when result is computed.
361 >        public boolean ran() { return invocationCount == 1; }
362          public void accept(Integer x, Integer y) {
363 <            value = x.intValue() - y.intValue();
363 >            invocationCount++;
364 >            value = subtract(x, y);
365 >        }
366 >    }
367 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
368 >        int invocationCount = 0;
369 >        Integer value;
370 >        // Check this action was invoked exactly once when result is computed.
371 >        public boolean ran() { return invocationCount == 1; }
372 >        public Integer apply(Integer x, Integer y) {
373 >            invocationCount++;
374 >            return value = subtract(x, y);
375          }
376      }
377      static final class Noop implements Runnable {
378 +        int invocationCount = 0;
379          boolean ran;
380 <        public void run() { ran = true; }
380 >        public void run() {
381 >            invocationCount++;
382 >            ran = true;
383 >        }
384      }
385  
386      static final class FailingSupplier implements Supplier<Integer> {
# Line 371 | Line 449 | public class CompletableFutureTest exten
449          }
450      }
451  
452 +    /**
453 +     * Permits the testing of parallel code for the 3 different
454 +     * execution modes without repeating all the testing code.
455 +     */
456 +    enum ExecutionMode {
457 +        DEFAULT {
458 +            public <T,U> CompletableFuture<Void> runAfterBoth
459 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
460 +                return f.runAfterBoth(g, a);
461 +            }
462 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
463 +                (CompletableFuture<T> f,
464 +                 CompletionStage<? extends U> g,
465 +                 BiConsumer<? super T,? super U> a) {
466 +                return f.thenAcceptBoth(g, a);
467 +            }
468 +            public <T,U,V> CompletableFuture<V> thenCombine
469 +                (CompletableFuture<T> f,
470 +                 CompletionStage<? extends U> g,
471 +                 BiFunction<? super T,? super U,? extends V> a) {
472 +                return f.thenCombine(g, a);
473 +            }
474 +            public <T,U> CompletableFuture<U> applyToEither
475 +                (CompletableFuture<T> f,
476 +                 CompletionStage<? extends T> g,
477 +                 Function<? super T,U> a) {
478 +                return f.applyToEither(g, a);
479 +            }
480 +            public <T> CompletableFuture<Void> acceptEither
481 +                (CompletableFuture<T> f,
482 +                 CompletionStage<? extends T> g,
483 +                 Consumer<? super T> a) {
484 +                return f.acceptEither(g, a);
485 +            }
486 +            public <T> CompletableFuture<Void> runAfterEither
487 +                (CompletableFuture<T> f,
488 +                 CompletionStage<?> g,
489 +                 java.lang.Runnable a) {
490 +                return f.runAfterEither(g, a);
491 +            }
492 +            public <T,U> CompletableFuture<U> thenCompose
493 +                (CompletableFuture<T> f,
494 +                 Function<? super T,? extends CompletionStage<U>> a) {
495 +                return f.thenCompose(a);
496 +            }
497 +            public <T> CompletableFuture<T> whenComplete
498 +                (CompletableFuture<T> f,
499 +                 BiConsumer<? super T,? super Throwable> a) {
500 +                return f.whenComplete(a);
501 +            }
502 +        },
503 +
504 + //             /** Experimental way to do more testing */
505 + //         REVERSE_DEFAULT {
506 + //             public <T,U> CompletableFuture<Void> runAfterBoth
507 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
508 + //                 return g.runAfterBoth(f, a);
509 + //             }
510 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
511 + //                 (CompletableFuture<T> f,
512 + //                  CompletionStage<? extends U> g,
513 + //                  BiConsumer<? super T,? super U> a) {
514 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
515 + //             }
516 + //         },
517 +
518 +        DEFAULT_ASYNC {
519 +            public <T,U> CompletableFuture<Void> runAfterBoth
520 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
521 +                return f.runAfterBothAsync(g, a);
522 +            }
523 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
524 +                (CompletableFuture<T> f,
525 +                 CompletionStage<? extends U> g,
526 +                 BiConsumer<? super T,? super U> a) {
527 +                return f.thenAcceptBothAsync(g, a);
528 +            }
529 +            public <T,U,V> CompletableFuture<V> thenCombine
530 +                (CompletableFuture<T> f,
531 +                 CompletionStage<? extends U> g,
532 +                 BiFunction<? super T,? super U,? extends V> a) {
533 +                return f.thenCombineAsync(g, a);
534 +            }
535 +            public <T,U> CompletableFuture<U> applyToEither
536 +                (CompletableFuture<T> f,
537 +                 CompletionStage<? extends T> g,
538 +                 Function<? super T,U> a) {
539 +                return f.applyToEitherAsync(g, a);
540 +            }
541 +            public <T> CompletableFuture<Void> acceptEither
542 +                (CompletableFuture<T> f,
543 +                 CompletionStage<? extends T> g,
544 +                 Consumer<? super T> a) {
545 +                return f.acceptEitherAsync(g, a);
546 +            }
547 +            public <T> CompletableFuture<Void> runAfterEither
548 +                (CompletableFuture<T> f,
549 +                 CompletionStage<?> g,
550 +                 java.lang.Runnable a) {
551 +                return f.runAfterEitherAsync(g, a);
552 +            }
553 +            public <T,U> CompletableFuture<U> thenCompose
554 +                (CompletableFuture<T> f,
555 +                 Function<? super T,? extends CompletionStage<U>> a) {
556 +                return f.thenComposeAsync(a);
557 +            }
558 +            public <T> CompletableFuture<T> whenComplete
559 +                (CompletableFuture<T> f,
560 +                 BiConsumer<? super T,? super Throwable> a) {
561 +                return f.whenCompleteAsync(a);
562 +            }
563 +        },
564 +
565 + //         REVERSE_DEFAULT_ASYNC {
566 + //             public <T,U> CompletableFuture<Void> runAfterBoth
567 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
568 + //                 return f.runAfterBothAsync(g, a);
569 + //             }
570 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
571 + //                 (CompletableFuture<T> f,
572 + //                  CompletionStage<? extends U> g,
573 + //                  BiConsumer<? super T,? super U> a) {
574 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
575 + //             }
576 + //         },
577 +
578 +        EXECUTOR {
579 +            public <T,U> CompletableFuture<Void> runAfterBoth
580 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
581 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
582 +            }
583 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
584 +                (CompletableFuture<T> f,
585 +                 CompletionStage<? extends U> g,
586 +                 BiConsumer<? super T,? super U> a) {
587 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
588 +            }
589 +            public <T,U,V> CompletableFuture<V> thenCombine
590 +                (CompletableFuture<T> f,
591 +                 CompletionStage<? extends U> g,
592 +                 BiFunction<? super T,? super U,? extends V> a) {
593 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
594 +            }
595 +            public <T,U> CompletableFuture<U> applyToEither
596 +                (CompletableFuture<T> f,
597 +                 CompletionStage<? extends T> g,
598 +                 Function<? super T,U> a) {
599 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
600 +            }
601 +            public <T> CompletableFuture<Void> acceptEither
602 +                (CompletableFuture<T> f,
603 +                 CompletionStage<? extends T> g,
604 +                 Consumer<? super T> a) {
605 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
606 +            }
607 +            public <T> CompletableFuture<Void> runAfterEither
608 +                (CompletableFuture<T> f,
609 +                 CompletionStage<?> g,
610 +                 java.lang.Runnable a) {
611 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
612 +            }
613 +            public <T,U> CompletableFuture<U> thenCompose
614 +                (CompletableFuture<T> f,
615 +                 Function<? super T,? extends CompletionStage<U>> a) {
616 +                return f.thenComposeAsync(a, new ThreadExecutor());
617 +            }
618 +            public <T> CompletableFuture<T> whenComplete
619 +                (CompletableFuture<T> f,
620 +                 BiConsumer<? super T,? super Throwable> a) {
621 +                return f.whenCompleteAsync(a, new ThreadExecutor());
622 +            }
623 +        };
624 +
625 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
626 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
627 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
628 +            (CompletableFuture<T> f,
629 +             CompletionStage<? extends U> g,
630 +             BiConsumer<? super T,? super U> a);
631 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
632 +            (CompletableFuture<T> f,
633 +             CompletionStage<? extends U> g,
634 +             BiFunction<? super T,? super U,? extends V> a);
635 +        public abstract <T,U> CompletableFuture<U> applyToEither
636 +            (CompletableFuture<T> f,
637 +             CompletionStage<? extends T> g,
638 +             Function<? super T,U> a);
639 +        public abstract <T> CompletableFuture<Void> acceptEither
640 +            (CompletableFuture<T> f,
641 +             CompletionStage<? extends T> g,
642 +             Consumer<? super T> a);
643 +        public abstract <T> CompletableFuture<Void> runAfterEither
644 +            (CompletableFuture<T> f,
645 +             CompletionStage<?> g,
646 +             java.lang.Runnable a);
647 +        public abstract <T,U> CompletableFuture<U> thenCompose
648 +            (CompletableFuture<T> f,
649 +             Function<? super T,? extends CompletionStage<U>> a);
650 +        public abstract <T> CompletableFuture<T> whenComplete
651 +            (CompletableFuture<T> f,
652 +             BiConsumer<? super T,? super Throwable> a);
653 +
654 +
655 +    }
656  
657      /**
658       * exceptionally action completes with function value on source
659 <     * exception;  otherwise with source value
659 >     * exception; otherwise with source value
660       */
661      public void testExceptionally() {
662          CompletableFuture<Integer> f = new CompletableFuture<>();
# Line 622 | Line 904 | public class CompletableFutureTest exten
904          CompletableFuture<Void> g = f.thenAccept(r);
905          f.complete(one);
906          checkCompletedNormally(g, null);
907 <        assertEquals(r.value, 2);
907 >        assertEquals(r.value, (Integer) 2);
908      }
909  
910      /**
# Line 660 | Line 942 | public class CompletableFutureTest exten
942          checkCompletedWithWrappedCancellationException(g);
943      }
944  
663
945      /**
946       * thenCombine result completes normally after normal completion
947       * of sources
948       */
949 <    public void testThenCombine() {
950 <        CompletableFuture<Integer> f, g, h;
951 <
952 <        f = new CompletableFuture<>();
953 <        g = new CompletableFuture<>();
954 <        h = f.thenCombine(g, subtract);
955 <        f.complete(3);
956 <        checkIncomplete(h);
957 <        g.complete(1);
958 <        checkCompletedNormally(h, 2);
959 <
960 <        f = new CompletableFuture<>();
961 <        g = new CompletableFuture<>();
962 <        h = f.thenCombine(g, subtract);
963 <        g.complete(1);
964 <        checkIncomplete(h);
965 <        f.complete(3);
966 <        checkCompletedNormally(h, 2);
967 <
968 <        f = new CompletableFuture<>();
969 <        g = new CompletableFuture<>();
970 <        g.complete(1);
971 <        f.complete(3);
972 <        h = f.thenCombine(g, subtract);
973 <        checkCompletedNormally(h, 2);
949 >    public void testThenCombine_normalCompletion1() {
950 >        for (boolean createdIncomplete : new boolean[] { true, false })
951 >        for (boolean fFirst : new boolean[] { true, false })
952 >        for (ExecutionMode m : ExecutionMode.values())
953 >        for (Integer v1 : new Integer[] { 1, null })
954 >        for (Integer v2 : new Integer[] { 2, null }) {
955 >
956 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
957 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
958 >        final SubtractFunction r = new SubtractFunction();
959 >        CompletableFuture<Integer> h = null;
960 >        if (createdIncomplete) h = m.thenCombine(f, g, r);
961 >
962 >        if (fFirst)
963 >            f.complete(v1);
964 >        else
965 >            g.complete(v2);
966 >        if (createdIncomplete) checkIncomplete(h);
967 >        assertEquals(r.invocationCount, 0);
968 >        if (!fFirst)
969 >            f.complete(v1);
970 >        else
971 >            g.complete(v2);
972 >        if (!createdIncomplete) h = m.thenCombine(f, g, r);
973 >
974 >        checkCompletedNormally(h, subtract(v1, v2));
975 >        checkCompletedNormally(f, v1);
976 >        checkCompletedNormally(g, v2);
977 >        assertEquals(r.invocationCount, 1);
978 >        }
979      }
980  
981      /**
982       * thenCombine result completes exceptionally after exceptional
983       * completion of either source
984       */
985 <    public void testThenCombine2() {
986 <        CompletableFuture<Integer> f, g, h;
987 <
988 <        f = new CompletableFuture<>();
989 <        g = new CompletableFuture<>();
990 <        h = f.thenCombine(g, subtract);
991 <        f.completeExceptionally(new CFException());
992 <        checkIncomplete(h);
993 <        g.complete(1);
994 <        checkCompletedWithWrappedCFException(h);
985 >    public void testThenCombine_exceptionalCompletion1() {
986 >        for (ExecutionMode m : ExecutionMode.values())
987 >        for (Integer v1 : new Integer[] { 1, null }) {
988 >
989 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
990 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
991 >        final SubtractFunction r = new SubtractFunction();
992 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
993 >        final CFException ex = new CFException();
994 >
995 >        f.completeExceptionally(ex);
996 >        checkIncomplete(h);
997 >        g.complete(v1);
998 >
999 >        checkCompletedWithWrappedCFException(h, ex);
1000 >        checkCompletedWithWrappedCFException(f, ex);
1001 >        assertEquals(r.invocationCount, 0);
1002 >        checkCompletedNormally(g, v1);
1003 >        }
1004 >    }
1005  
1006 <        f = new CompletableFuture<>();
1007 <        g = new CompletableFuture<>();
1008 <        h = f.thenCombine(g, subtract);
1009 <        g.completeExceptionally(new CFException());
1010 <        checkIncomplete(h);
1011 <        f.complete(3);
1012 <        checkCompletedWithWrappedCFException(h);
1006 >    public void testThenCombine_exceptionalCompletion2() {
1007 >        for (ExecutionMode m : ExecutionMode.values())
1008 >        for (Integer v1 : new Integer[] { 1, null }) {
1009 >
1010 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1011 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1012 >        final SubtractFunction r = new SubtractFunction();
1013 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1014 >        final CFException ex = new CFException();
1015 >
1016 >        g.completeExceptionally(ex);
1017 >        checkIncomplete(h);
1018 >        f.complete(v1);
1019 >
1020 >        checkCompletedWithWrappedCFException(h, ex);
1021 >        checkCompletedWithWrappedCFException(g, ex);
1022 >        assertEquals(r.invocationCount, 0);
1023 >        checkCompletedNormally(f, v1);
1024 >        }
1025 >    }
1026  
1027 <        f = new CompletableFuture<>();
1028 <        g = new CompletableFuture<>();
1029 <        f.complete(3);
1030 <        g.completeExceptionally(new CFException());
1031 <        h = f.thenCombine(g, subtract);
1032 <        checkCompletedWithWrappedCFException(h);
1027 >    public void testThenCombine_exceptionalCompletion3() {
1028 >        for (ExecutionMode m : ExecutionMode.values())
1029 >        for (Integer v1 : new Integer[] { 1, null }) {
1030 >
1031 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1032 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1033 >        final SubtractFunction r = new SubtractFunction();
1034 >        final CFException ex = new CFException();
1035 >
1036 >        g.completeExceptionally(ex);
1037 >        f.complete(v1);
1038 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1039 >
1040 >        checkCompletedWithWrappedCFException(h, ex);
1041 >        checkCompletedWithWrappedCFException(g, ex);
1042 >        assertEquals(r.invocationCount, 0);
1043 >        checkCompletedNormally(f, v1);
1044 >        }
1045 >    }
1046  
1047 <        f = new CompletableFuture<>();
1048 <        g = new CompletableFuture<>();
1049 <        f.completeExceptionally(new CFException());
1050 <        g.complete(3);
1051 <        h = f.thenCombine(g, subtract);
1052 <        checkCompletedWithWrappedCFException(h);
1047 >    public void testThenCombine_exceptionalCompletion4() {
1048 >        for (ExecutionMode m : ExecutionMode.values())
1049 >        for (Integer v1 : new Integer[] { 1, null }) {
1050 >
1051 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1052 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1053 >        final SubtractFunction r = new SubtractFunction();
1054 >        final CFException ex = new CFException();
1055 >
1056 >        f.completeExceptionally(ex);
1057 >        g.complete(v1);
1058 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1059 >
1060 >        checkCompletedWithWrappedCFException(h, ex);
1061 >        checkCompletedWithWrappedCFException(f, ex);
1062 >        assertEquals(r.invocationCount, 0);
1063 >        checkCompletedNormally(g, v1);
1064 >        }
1065      }
1066  
1067      /**
1068       * thenCombine result completes exceptionally if action does
1069       */
1070 <    public void testThenCombine3() {
1071 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1072 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1073 <        FailingBiFunction r = new FailingBiFunction();
1074 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1075 <        f.complete(one);
1076 <        checkIncomplete(g);
1077 <        assertFalse(r.ran);
1078 <        f2.complete(two);
1079 <        checkCompletedWithWrappedCFException(g);
1080 <        assertTrue(r.ran);
1070 >    public void testThenCombine_actionFailed1() {
1071 >        for (ExecutionMode m : ExecutionMode.values())
1072 >        for (Integer v1 : new Integer[] { 1, null })
1073 >        for (Integer v2 : new Integer[] { 2, null }) {
1074 >
1075 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1076 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1077 >        final FailingBiFunction r = new FailingBiFunction();
1078 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1079 >
1080 >        f.complete(v1);
1081 >        checkIncomplete(h);
1082 >        g.complete(v2);
1083 >
1084 >        checkCompletedWithWrappedCFException(h);
1085 >        checkCompletedNormally(f, v1);
1086 >        checkCompletedNormally(g, v2);
1087 >        }
1088 >    }
1089 >
1090 >    public void testThenCombine_actionFailed2() {
1091 >        for (ExecutionMode m : ExecutionMode.values())
1092 >        for (Integer v1 : new Integer[] { 1, null })
1093 >        for (Integer v2 : new Integer[] { 2, null }) {
1094 >
1095 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1096 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1097 >        final FailingBiFunction r = new FailingBiFunction();
1098 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1099 >
1100 >        g.complete(v2);
1101 >        checkIncomplete(h);
1102 >        f.complete(v1);
1103 >
1104 >        checkCompletedWithWrappedCFException(h);
1105 >        checkCompletedNormally(f, v1);
1106 >        checkCompletedNormally(g, v2);
1107 >        }
1108      }
1109  
1110      /**
1111       * thenCombine result completes exceptionally if either source cancelled
1112       */
1113 <    public void testThenCombine4() {
1114 <        CompletableFuture<Integer> f, g, h;
1113 >    public void testThenCombine_sourceCancelled1() {
1114 >        for (ExecutionMode m : ExecutionMode.values())
1115 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1116 >        for (Integer v1 : new Integer[] { 1, null }) {
1117 >
1118 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1119 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1120 >        final SubtractFunction r = new SubtractFunction();
1121 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1122  
1123 <        f = new CompletableFuture<>();
756 <        g = new CompletableFuture<>();
757 <        h = f.thenCombine(g, subtract);
758 <        assertTrue(f.cancel(true));
1123 >        assertTrue(f.cancel(mayInterruptIfRunning));
1124          checkIncomplete(h);
1125 <        g.complete(1);
1125 >        g.complete(v1);
1126 >
1127          checkCompletedWithWrappedCancellationException(h);
1128 +        checkCancelled(f);
1129 +        assertEquals(r.invocationCount, 0);
1130 +        checkCompletedNormally(g, v1);
1131 +        }
1132 +    }
1133  
1134 <        f = new CompletableFuture<>();
1135 <        g = new CompletableFuture<>();
1136 <        h = f.thenCombine(g, subtract);
1137 <        assertTrue(g.cancel(true));
1134 >    public void testThenCombine_sourceCancelled2() {
1135 >        for (ExecutionMode m : ExecutionMode.values())
1136 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1137 >        for (Integer v1 : new Integer[] { 1, null }) {
1138 >
1139 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1140 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1141 >        final SubtractFunction r = new SubtractFunction();
1142 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1143 >
1144 >        assertTrue(g.cancel(mayInterruptIfRunning));
1145          checkIncomplete(h);
1146 <        f.complete(3);
1146 >        f.complete(v1);
1147 >
1148          checkCompletedWithWrappedCancellationException(h);
1149 +        checkCancelled(g);
1150 +        assertEquals(r.invocationCount, 0);
1151 +        checkCompletedNormally(f, v1);
1152 +        }
1153 +    }
1154 +
1155 +    public void testThenCombine_sourceCancelled3() {
1156 +        for (ExecutionMode m : ExecutionMode.values())
1157 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1158 +        for (Integer v1 : new Integer[] { 1, null }) {
1159 +
1160 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1161 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1162 +        final SubtractFunction r = new SubtractFunction();
1163 +
1164 +        assertTrue(g.cancel(mayInterruptIfRunning));
1165 +        f.complete(v1);
1166 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1167 +
1168 +        checkCompletedWithWrappedCancellationException(h);
1169 +        checkCancelled(g);
1170 +        assertEquals(r.invocationCount, 0);
1171 +        checkCompletedNormally(f, v1);
1172 +        }
1173 +    }
1174 +
1175 +    public void testThenCombine_sourceCancelled4() {
1176 +        for (ExecutionMode m : ExecutionMode.values())
1177 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1178 +        for (Integer v1 : new Integer[] { 1, null }) {
1179 +
1180 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1181 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1182 +        final SubtractFunction r = new SubtractFunction();
1183 +
1184 +        assertTrue(f.cancel(mayInterruptIfRunning));
1185 +        g.complete(v1);
1186 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1187  
771        f = new CompletableFuture<>();
772        g = new CompletableFuture<>();
773        assertTrue(f.cancel(true));
774        assertTrue(g.cancel(true));
775        h = f.thenCombine(g, subtract);
1188          checkCompletedWithWrappedCancellationException(h);
1189 +        checkCancelled(f);
1190 +        assertEquals(r.invocationCount, 0);
1191 +        checkCompletedNormally(g, v1);
1192 +        }
1193      }
1194  
1195      /**
1196       * thenAcceptBoth result completes normally after normal
1197       * completion of sources
1198       */
1199 <    public void testThenAcceptBoth() {
1200 <        CompletableFuture<Integer> f, g;
1201 <        CompletableFuture<Void> h;
1202 <        SubtractAction r;
1199 >    public void testThenAcceptBoth_normalCompletion1() {
1200 >        for (ExecutionMode m : ExecutionMode.values())
1201 >        for (Integer v1 : new Integer[] { 1, null })
1202 >        for (Integer v2 : new Integer[] { 2, null }) {
1203 >
1204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1206 >        final SubtractAction r = new SubtractAction();
1207 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1208  
1209 <        f = new CompletableFuture<>();
789 <        g = new CompletableFuture<>();
790 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
791 <        f.complete(3);
1209 >        f.complete(v1);
1210          checkIncomplete(h);
1211 <        g.complete(1);
1211 >        assertFalse(r.ran());
1212 >        g.complete(v2);
1213 >
1214          checkCompletedNormally(h, null);
1215 <        assertEquals(r.value, 2);
1215 >        assertEquals(r.value, subtract(v1, v2));
1216 >        checkCompletedNormally(f, v1);
1217 >        checkCompletedNormally(g, v2);
1218 >        }
1219 >    }
1220  
1221 <        f = new CompletableFuture<>();
1222 <        g = new CompletableFuture<>();
1223 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1224 <        g.complete(1);
1221 >    public void testThenAcceptBoth_normalCompletion2() {
1222 >        for (ExecutionMode m : ExecutionMode.values())
1223 >        for (Integer v1 : new Integer[] { 1, null })
1224 >        for (Integer v2 : new Integer[] { 2, null }) {
1225 >
1226 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1227 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1228 >        final SubtractAction r = new SubtractAction();
1229 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1230 >
1231 >        g.complete(v2);
1232          checkIncomplete(h);
1233 <        f.complete(3);
1233 >        assertFalse(r.ran());
1234 >        f.complete(v1);
1235 >
1236          checkCompletedNormally(h, null);
1237 <        assertEquals(r.value, 2);
1237 >        assertEquals(r.value, subtract(v1, v2));
1238 >        checkCompletedNormally(f, v1);
1239 >        checkCompletedNormally(g, v2);
1240 >        }
1241 >    }
1242 >
1243 >    public void testThenAcceptBoth_normalCompletion3() {
1244 >        for (ExecutionMode m : ExecutionMode.values())
1245 >        for (Integer v1 : new Integer[] { 1, null })
1246 >        for (Integer v2 : new Integer[] { 2, null }) {
1247 >
1248 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1249 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1250 >        final SubtractAction r = new SubtractAction();
1251 >
1252 >        g.complete(v2);
1253 >        f.complete(v1);
1254 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1255  
806        f = new CompletableFuture<>();
807        g = new CompletableFuture<>();
808        g.complete(1);
809        f.complete(3);
810        h = f.thenAcceptBoth(g, r = new SubtractAction());
1256          checkCompletedNormally(h, null);
1257 <        assertEquals(r.value, 2);
1257 >        assertEquals(r.value, subtract(v1, v2));
1258 >        checkCompletedNormally(f, v1);
1259 >        checkCompletedNormally(g, v2);
1260 >        }
1261 >    }
1262 >
1263 >    public void testThenAcceptBoth_normalCompletion4() {
1264 >        for (ExecutionMode m : ExecutionMode.values())
1265 >        for (Integer v1 : new Integer[] { 1, null })
1266 >        for (Integer v2 : new Integer[] { 2, null }) {
1267 >
1268 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1269 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1270 >        final SubtractAction r = new SubtractAction();
1271 >
1272 >        f.complete(v1);
1273 >        g.complete(v2);
1274 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1275 >
1276 >        checkCompletedNormally(h, null);
1277 >        assertEquals(r.value, subtract(v1, v2));
1278 >        checkCompletedNormally(f, v1);
1279 >        checkCompletedNormally(g, v2);
1280 >        }
1281      }
1282  
1283      /**
1284       * thenAcceptBoth result completes exceptionally after exceptional
1285       * completion of either source
1286       */
1287 <    public void testThenAcceptBoth2() {
1288 <        CompletableFuture<Integer> f, g;
1289 <        CompletableFuture<Void> h;
1290 <        SubtractAction r;
1291 <
1292 <        f = new CompletableFuture<>();
1293 <        g = new CompletableFuture<>();
1294 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1295 <        f.completeExceptionally(new CFException());
1296 <        checkIncomplete(h);
1297 <        g.complete(1);
1298 <        checkCompletedWithWrappedCFException(h);
1287 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1288 >        for (ExecutionMode m : ExecutionMode.values())
1289 >        for (Integer v1 : new Integer[] { 1, null }) {
1290 >
1291 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1292 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1293 >        final SubtractAction r = new SubtractAction();
1294 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1295 >        final CFException ex = new CFException();
1296 >
1297 >        f.completeExceptionally(ex);
1298 >        checkIncomplete(h);
1299 >        g.complete(v1);
1300 >
1301 >        checkCompletedWithWrappedCFException(h, ex);
1302 >        checkCompletedWithWrappedCFException(f, ex);
1303 >        assertEquals(r.invocationCount, 0);
1304 >        checkCompletedNormally(g, v1);
1305 >        }
1306 >    }
1307  
1308 <        f = new CompletableFuture<>();
1309 <        g = new CompletableFuture<>();
1310 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1311 <        g.completeExceptionally(new CFException());
1312 <        checkIncomplete(h);
1313 <        f.complete(3);
1314 <        checkCompletedWithWrappedCFException(h);
1308 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1309 >        for (ExecutionMode m : ExecutionMode.values())
1310 >        for (Integer v1 : new Integer[] { 1, null }) {
1311 >
1312 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1313 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1314 >        final SubtractAction r = new SubtractAction();
1315 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1316 >        final CFException ex = new CFException();
1317 >
1318 >        g.completeExceptionally(ex);
1319 >        checkIncomplete(h);
1320 >        f.complete(v1);
1321 >
1322 >        checkCompletedWithWrappedCFException(h, ex);
1323 >        checkCompletedWithWrappedCFException(g, ex);
1324 >        assertEquals(r.invocationCount, 0);
1325 >        checkCompletedNormally(f, v1);
1326 >        }
1327 >    }
1328  
1329 <        f = new CompletableFuture<>();
1330 <        g = new CompletableFuture<>();
1331 <        f.complete(3);
1332 <        g.completeExceptionally(new CFException());
1333 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1334 <        checkCompletedWithWrappedCFException(h);
1329 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1330 >        for (ExecutionMode m : ExecutionMode.values())
1331 >        for (Integer v1 : new Integer[] { 1, null }) {
1332 >
1333 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1334 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1335 >        final SubtractAction r = new SubtractAction();
1336 >        final CFException ex = new CFException();
1337 >
1338 >        g.completeExceptionally(ex);
1339 >        f.complete(v1);
1340 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1341 >
1342 >        checkCompletedWithWrappedCFException(h, ex);
1343 >        checkCompletedWithWrappedCFException(g, ex);
1344 >        assertEquals(r.invocationCount, 0);
1345 >        checkCompletedNormally(f, v1);
1346 >        }
1347 >    }
1348  
1349 <        f = new CompletableFuture<>();
1350 <        g = new CompletableFuture<>();
1351 <        f.completeExceptionally(new CFException());
1352 <        g.complete(3);
1353 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1354 <        checkCompletedWithWrappedCFException(h);
1349 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1350 >        for (ExecutionMode m : ExecutionMode.values())
1351 >        for (Integer v1 : new Integer[] { 1, null }) {
1352 >
1353 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1355 >        final SubtractAction r = new SubtractAction();
1356 >        final CFException ex = new CFException();
1357 >
1358 >        f.completeExceptionally(ex);
1359 >        g.complete(v1);
1360 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1361 >
1362 >        checkCompletedWithWrappedCFException(h, ex);
1363 >        checkCompletedWithWrappedCFException(f, ex);
1364 >        assertFalse(r.ran());
1365 >        checkCompletedNormally(g, v1);
1366 >        }
1367      }
1368  
1369      /**
1370       * thenAcceptBoth result completes exceptionally if action does
1371       */
1372 <    public void testThenAcceptBoth3() {
1373 <        CompletableFuture<Integer> f, g;
1374 <        CompletableFuture<Void> h;
1375 <        FailingBiConsumer r;
1372 >    public void testThenAcceptBoth_actionFailed1() {
1373 >        for (ExecutionMode m : ExecutionMode.values())
1374 >        for (Integer v1 : new Integer[] { 1, null })
1375 >        for (Integer v2 : new Integer[] { 2, null }) {
1376  
1377 <        f = new CompletableFuture<>();
1378 <        g = new CompletableFuture<>();
1379 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1380 <        f.complete(3);
1377 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1378 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 >        final FailingBiConsumer r = new FailingBiConsumer();
1380 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1381 >
1382 >        f.complete(v1);
1383          checkIncomplete(h);
1384 <        g.complete(1);
1384 >        g.complete(v2);
1385 >
1386          checkCompletedWithWrappedCFException(h);
1387 +        checkCompletedNormally(f, v1);
1388 +        checkCompletedNormally(g, v2);
1389 +        }
1390 +    }
1391 +
1392 +    public void testThenAcceptBoth_actionFailed2() {
1393 +        for (ExecutionMode m : ExecutionMode.values())
1394 +        for (Integer v1 : new Integer[] { 1, null })
1395 +        for (Integer v2 : new Integer[] { 2, null }) {
1396 +
1397 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1398 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1399 +        final FailingBiConsumer r = new FailingBiConsumer();
1400 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1401 +
1402 +        g.complete(v2);
1403 +        checkIncomplete(h);
1404 +        f.complete(v1);
1405  
871        f = new CompletableFuture<>();
872        g = new CompletableFuture<>();
873        f.complete(3);
874        g.complete(1);
875        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1406          checkCompletedWithWrappedCFException(h);
1407 +        checkCompletedNormally(f, v1);
1408 +        checkCompletedNormally(g, v2);
1409 +        }
1410      }
1411  
1412      /**
1413       * thenAcceptBoth result completes exceptionally if either source cancelled
1414       */
1415 <    public void testThenAcceptBoth4() {
1416 <        CompletableFuture<Integer> f, g;
1417 <        CompletableFuture<Void> h;
1418 <        SubtractAction r;
1415 >    public void testThenAcceptBoth_sourceCancelled1() {
1416 >        for (ExecutionMode m : ExecutionMode.values())
1417 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1418 >        for (Integer v1 : new Integer[] { 1, null }) {
1419 >
1420 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1421 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1422 >        final SubtractAction r = new SubtractAction();
1423 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1424  
1425 <        f = new CompletableFuture<>();
888 <        g = new CompletableFuture<>();
889 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
890 <        assertTrue(f.cancel(true));
1425 >        assertTrue(f.cancel(mayInterruptIfRunning));
1426          checkIncomplete(h);
1427 <        g.complete(1);
1427 >        g.complete(v1);
1428 >
1429          checkCompletedWithWrappedCancellationException(h);
1430 +        checkCancelled(f);
1431 +        assertEquals(r.invocationCount, 0);
1432 +        checkCompletedNormally(g, v1);
1433 +        }
1434 +    }
1435  
1436 <        f = new CompletableFuture<>();
1437 <        g = new CompletableFuture<>();
1438 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1439 <        assertTrue(g.cancel(true));
1436 >    public void testThenAcceptBoth_sourceCancelled2() {
1437 >        for (ExecutionMode m : ExecutionMode.values())
1438 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1439 >        for (Integer v1 : new Integer[] { 1, null }) {
1440 >
1441 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1442 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1443 >        final SubtractAction r = new SubtractAction();
1444 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1445 >
1446 >        assertTrue(g.cancel(mayInterruptIfRunning));
1447          checkIncomplete(h);
1448 <        f.complete(3);
901 <        checkCompletedWithWrappedCancellationException(h);
1448 >        f.complete(v1);
1449  
903        f = new CompletableFuture<>();
904        g = new CompletableFuture<>();
905        f.complete(3);
906        assertTrue(g.cancel(true));
907        h = f.thenAcceptBoth(g, r = new SubtractAction());
1450          checkCompletedWithWrappedCancellationException(h);
1451 +        checkCancelled(g);
1452 +        assertEquals(r.invocationCount, 0);
1453 +        checkCompletedNormally(f, v1);
1454 +        }
1455 +    }
1456 +
1457 +    public void testThenAcceptBoth_sourceCancelled3() {
1458 +        for (ExecutionMode m : ExecutionMode.values())
1459 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1460 +        for (Integer v1 : new Integer[] { 1, null }) {
1461 +
1462 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1463 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1464 +        final SubtractAction r = new SubtractAction();
1465 +
1466 +        assertTrue(g.cancel(mayInterruptIfRunning));
1467 +        f.complete(v1);
1468 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1469 +
1470 +        checkCompletedWithWrappedCancellationException(h);
1471 +        checkCancelled(g);
1472 +        assertEquals(r.invocationCount, 0);
1473 +        checkCompletedNormally(f, v1);
1474 +        }
1475 +    }
1476 +
1477 +    public void testThenAcceptBoth_sourceCancelled4() {
1478 +        for (ExecutionMode m : ExecutionMode.values())
1479 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1480 +        for (Integer v1 : new Integer[] { 1, null }) {
1481 +
1482 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1483 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1484 +        final SubtractAction r = new SubtractAction();
1485 +
1486 +        assertTrue(f.cancel(mayInterruptIfRunning));
1487 +        g.complete(v1);
1488 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1489  
910        f = new CompletableFuture<>();
911        g = new CompletableFuture<>();
912        assertTrue(f.cancel(true));
913        g.complete(3);
914        h = f.thenAcceptBoth(g, r = new SubtractAction());
1490          checkCompletedWithWrappedCancellationException(h);
1491 +        checkCancelled(f);
1492 +        assertEquals(r.invocationCount, 0);
1493 +        checkCompletedNormally(g, v1);
1494 +        }
1495      }
1496  
1497      /**
1498       * runAfterBoth result completes normally after normal
1499       * completion of sources
1500       */
1501 <    public void testRunAfterBoth() {
1502 <        CompletableFuture<Integer> f, g;
1503 <        CompletableFuture<Void> h;
1504 <        Noop r;
1501 >    public void testRunAfterBoth_normalCompletion1() {
1502 >        for (ExecutionMode m : ExecutionMode.values())
1503 >        for (Integer v1 : new Integer[] { 1, null })
1504 >        for (Integer v2 : new Integer[] { 2, null }) {
1505 >
1506 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1507 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1508 >        final Noop r = new Noop();
1509 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1510  
1511 <        f = new CompletableFuture<>();
928 <        g = new CompletableFuture<>();
929 <        h = f.runAfterBoth(g, r = new Noop());
930 <        f.complete(3);
1511 >        f.complete(v1);
1512          checkIncomplete(h);
1513 <        g.complete(1);
1513 >        assertFalse(r.ran);
1514 >        g.complete(v2);
1515 >
1516          checkCompletedNormally(h, null);
1517 <        assertTrue(r.ran);
1517 >        assertEquals(r.invocationCount, 1);
1518 >        checkCompletedNormally(f, v1);
1519 >        checkCompletedNormally(g, v2);
1520 >        }
1521 >    }
1522  
1523 <        f = new CompletableFuture<>();
1524 <        g = new CompletableFuture<>();
1525 <        h = f.runAfterBoth(g, r = new Noop());
1526 <        g.complete(1);
1523 >    public void testRunAfterBoth_normalCompletion2() {
1524 >        for (ExecutionMode m : ExecutionMode.values())
1525 >        for (Integer v1 : new Integer[] { 1, null })
1526 >        for (Integer v2 : new Integer[] { 2, null }) {
1527 >
1528 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1529 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1530 >        final Noop r = new Noop();
1531 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1532 >
1533 >        g.complete(v2);
1534          checkIncomplete(h);
1535 <        f.complete(3);
1535 >        assertFalse(r.ran);
1536 >        f.complete(v1);
1537 >
1538          checkCompletedNormally(h, null);
1539 <        assertTrue(r.ran);
1539 >        assertEquals(r.invocationCount, 1);
1540 >        checkCompletedNormally(f, v1);
1541 >        checkCompletedNormally(g, v2);
1542 >        }
1543 >    }
1544 >
1545 >    public void testRunAfterBoth_normalCompletion3() {
1546 >        for (ExecutionMode m : ExecutionMode.values())
1547 >        for (Integer v1 : new Integer[] { 1, null })
1548 >        for (Integer v2 : new Integer[] { 2, null }) {
1549 >
1550 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1551 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1552 >        final Noop r = new Noop();
1553 >
1554 >        g.complete(v2);
1555 >        f.complete(v1);
1556 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1557  
945        f = new CompletableFuture<>();
946        g = new CompletableFuture<>();
947        g.complete(1);
948        f.complete(3);
949        h = f.runAfterBoth(g, r = new Noop());
1558          checkCompletedNormally(h, null);
1559          assertTrue(r.ran);
1560 +        checkCompletedNormally(f, v1);
1561 +        checkCompletedNormally(g, v2);
1562 +        }
1563 +    }
1564 +
1565 +    public void testRunAfterBoth_normalCompletion4() {
1566 +        for (ExecutionMode m : ExecutionMode.values())
1567 +        for (Integer v1 : new Integer[] { 1, null })
1568 +        for (Integer v2 : new Integer[] { 2, null }) {
1569 +
1570 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1571 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1572 +        final Noop r = new Noop();
1573 +
1574 +        f.complete(v1);
1575 +        g.complete(v2);
1576 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1577 +
1578 +        checkCompletedNormally(h, null);
1579 +        assertEquals(r.invocationCount, 1);
1580 +        checkCompletedNormally(f, v1);
1581 +        checkCompletedNormally(g, v2);
1582 +        }
1583      }
1584  
1585      /**
1586       * runAfterBoth result completes exceptionally after exceptional
1587       * completion of either source
1588       */
1589 <    public void testRunAfterBoth2() {
1590 <        CompletableFuture<Integer> f, g;
1591 <        CompletableFuture<Void> h;
1592 <        Noop r;
1593 <
1594 <        f = new CompletableFuture<>();
1595 <        g = new CompletableFuture<>();
1596 <        h = f.runAfterBoth(g, r = new Noop());
1597 <        f.completeExceptionally(new CFException());
1598 <        checkIncomplete(h);
1599 <        g.complete(1);
1600 <        checkCompletedWithWrappedCFException(h);
1601 <        assertFalse(r.ran);
1589 >    public void testRunAfterBoth_exceptionalCompletion1() {
1590 >        for (ExecutionMode m : ExecutionMode.values())
1591 >        for (Integer v1 : new Integer[] { 1, null }) {
1592 >
1593 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1594 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1595 >        final Noop r = new Noop();
1596 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1597 >        final CFException ex = new CFException();
1598 >
1599 >        f.completeExceptionally(ex);
1600 >        checkIncomplete(h);
1601 >        g.complete(v1);
1602 >
1603 >        checkCompletedWithWrappedCFException(h, ex);
1604 >        checkCompletedWithWrappedCFException(f, ex);
1605 >        assertEquals(r.invocationCount, 0);
1606 >        checkCompletedNormally(g, v1);
1607 >        }
1608 >    }
1609  
1610 <        f = new CompletableFuture<>();
1611 <        g = new CompletableFuture<>();
1612 <        h = f.runAfterBoth(g, r = new Noop());
1613 <        g.completeExceptionally(new CFException());
1614 <        checkIncomplete(h);
1615 <        f.complete(3);
1616 <        checkCompletedWithWrappedCFException(h);
1617 <        assertFalse(r.ran);
1610 >    public void testRunAfterBoth_exceptionalCompletion2() {
1611 >        for (ExecutionMode m : ExecutionMode.values())
1612 >        for (Integer v1 : new Integer[] { 1, null }) {
1613 >
1614 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1615 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1616 >        final Noop r = new Noop();
1617 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1618 >        final CFException ex = new CFException();
1619 >
1620 >        g.completeExceptionally(ex);
1621 >        checkIncomplete(h);
1622 >        f.complete(v1);
1623 >
1624 >        checkCompletedWithWrappedCFException(h, ex);
1625 >        checkCompletedWithWrappedCFException(g, ex);
1626 >        assertEquals(r.invocationCount, 0);
1627 >        checkCompletedNormally(f, v1);
1628 >        }
1629 >    }
1630  
1631 <        f = new CompletableFuture<>();
1632 <        g = new CompletableFuture<>();
1633 <        g.completeExceptionally(new CFException());
1634 <        f.complete(3);
1635 <        h = f.runAfterBoth(g, r = new Noop());
1636 <        checkCompletedWithWrappedCFException(h);
1637 <        assertFalse(r.ran);
1631 >    public void testRunAfterBoth_exceptionalCompletion3() {
1632 >        for (ExecutionMode m : ExecutionMode.values())
1633 >        for (Integer v1 : new Integer[] { 1, null }) {
1634 >
1635 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1636 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1637 >        final Noop r = new Noop();
1638 >        final CFException ex = new CFException();
1639 >
1640 >        g.completeExceptionally(ex);
1641 >        f.complete(v1);
1642 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1643 >
1644 >        checkCompletedWithWrappedCFException(h, ex);
1645 >        checkCompletedWithWrappedCFException(g, ex);
1646 >        assertEquals(r.invocationCount, 0);
1647 >        checkCompletedNormally(f, v1);
1648 >        }
1649 >    }
1650  
1651 <        f = new CompletableFuture<>();
1652 <        g = new CompletableFuture<>();
1653 <        f.completeExceptionally(new CFException());
1654 <        g.complete(1);
1655 <        h = f.runAfterBoth(g, r = new Noop());
1656 <        checkCompletedWithWrappedCFException(h);
1657 <        assertFalse(r.ran);
1651 >    public void testRunAfterBoth_exceptionalCompletion4() {
1652 >        for (ExecutionMode m : ExecutionMode.values())
1653 >        for (Integer v1 : new Integer[] { 1, null }) {
1654 >
1655 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1656 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1657 >        final Noop r = new Noop();
1658 >        final CFException ex = new CFException();
1659 >
1660 >        f.completeExceptionally(ex);
1661 >        g.complete(v1);
1662 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1663 >
1664 >        checkCompletedWithWrappedCFException(h, ex);
1665 >        checkCompletedWithWrappedCFException(f, ex);
1666 >        assertEquals(r.invocationCount, 0);
1667 >        checkCompletedNormally(g, v1);
1668 >        }
1669      }
1670  
1671      /**
1672       * runAfterBoth result completes exceptionally if action does
1673       */
1674 <    public void testRunAfterBoth3() {
1675 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1676 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1677 <        FailingNoop r = new FailingNoop();
1678 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1679 <        f.complete(one);
1680 <        checkIncomplete(g);
1681 <        f2.complete(two);
1682 <        checkCompletedWithWrappedCFException(g);
1674 >    public void testRunAfterBoth_actionFailed1() {
1675 >        for (ExecutionMode m : ExecutionMode.values())
1676 >        for (Integer v1 : new Integer[] { 1, null })
1677 >        for (Integer v2 : new Integer[] { 2, null }) {
1678 >
1679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1681 >        final FailingNoop r = new FailingNoop();
1682 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1683 >
1684 >        f.complete(v1);
1685 >        checkIncomplete(h);
1686 >        g.complete(v2);
1687 >
1688 >        checkCompletedWithWrappedCFException(h);
1689 >        checkCompletedNormally(f, v1);
1690 >        checkCompletedNormally(g, v2);
1691 >        }
1692 >    }
1693 >
1694 >    public void testRunAfterBoth_actionFailed2() {
1695 >        for (ExecutionMode m : ExecutionMode.values())
1696 >        for (Integer v1 : new Integer[] { 1, null })
1697 >        for (Integer v2 : new Integer[] { 2, null }) {
1698 >
1699 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1700 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1701 >        final FailingNoop r = new FailingNoop();
1702 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1703 >
1704 >        g.complete(v2);
1705 >        checkIncomplete(h);
1706 >        f.complete(v1);
1707 >
1708 >        checkCompletedWithWrappedCFException(h);
1709 >        checkCompletedNormally(f, v1);
1710 >        checkCompletedNormally(g, v2);
1711 >        }
1712      }
1713  
1714      /**
1715       * runAfterBoth result completes exceptionally if either source cancelled
1716       */
1717 <    public void testRunAfterBoth4() {
1718 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1719 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1720 <        Noop r = new Noop();
1721 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1722 <        assertTrue(f.cancel(true));
1723 <        f2.complete(two);
1724 <        checkCompletedWithWrappedCancellationException(g);
1725 <        f = new CompletableFuture<>();
1726 <        f2 = new CompletableFuture<>();
1727 <        r = new Noop();
1728 <        g = f.runAfterBoth(f2, r);
1729 <        f.complete(one);
1730 <        assertTrue(f2.cancel(true));
1731 <        checkCompletedWithWrappedCancellationException(g);
1717 >    public void testRunAfterBoth_sourceCancelled1() {
1718 >        for (ExecutionMode m : ExecutionMode.values())
1719 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1720 >        for (Integer v1 : new Integer[] { 1, null }) {
1721 >
1722 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1723 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1724 >        final Noop r = new Noop();
1725 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1726 >
1727 >        assertTrue(f.cancel(mayInterruptIfRunning));
1728 >        checkIncomplete(h);
1729 >        g.complete(v1);
1730 >
1731 >        checkCompletedWithWrappedCancellationException(h);
1732 >        checkCancelled(f);
1733 >        assertEquals(r.invocationCount, 0);
1734 >        checkCompletedNormally(g, v1);
1735 >        }
1736 >    }
1737 >
1738 >    public void testRunAfterBoth_sourceCancelled2() {
1739 >        for (ExecutionMode m : ExecutionMode.values())
1740 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1741 >        for (Integer v1 : new Integer[] { 1, null }) {
1742 >
1743 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1744 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1745 >        final Noop r = new Noop();
1746 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1747 >
1748 >        assertTrue(g.cancel(mayInterruptIfRunning));
1749 >        checkIncomplete(h);
1750 >        f.complete(v1);
1751 >
1752 >        checkCompletedWithWrappedCancellationException(h);
1753 >        checkCancelled(g);
1754 >        assertEquals(r.invocationCount, 0);
1755 >        checkCompletedNormally(f, v1);
1756 >        }
1757 >    }
1758 >
1759 >    public void testRunAfterBoth_sourceCancelled3() {
1760 >        for (ExecutionMode m : ExecutionMode.values())
1761 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1762 >        for (Integer v1 : new Integer[] { 1, null }) {
1763 >
1764 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1765 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1766 >        final Noop r = new Noop();
1767 >
1768 >        assertTrue(g.cancel(mayInterruptIfRunning));
1769 >        f.complete(v1);
1770 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1771 >
1772 >        checkCompletedWithWrappedCancellationException(h);
1773 >        checkCancelled(g);
1774 >        assertEquals(r.invocationCount, 0);
1775 >        checkCompletedNormally(f, v1);
1776 >        }
1777 >    }
1778 >
1779 >    public void testRunAfterBoth_sourceCancelled4() {
1780 >        for (ExecutionMode m : ExecutionMode.values())
1781 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1782 >        for (Integer v1 : new Integer[] { 1, null }) {
1783 >
1784 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1785 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1786 >        final Noop r = new Noop();
1787 >
1788 >        assertTrue(f.cancel(mayInterruptIfRunning));
1789 >        g.complete(v1);
1790 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1791 >
1792 >        checkCompletedWithWrappedCancellationException(h);
1793 >        checkCancelled(f);
1794 >        assertEquals(r.invocationCount, 0);
1795 >        checkCompletedNormally(g, v1);
1796 >        }
1797      }
1798  
1799      /**
1800       * applyToEither result completes normally after normal completion
1801       * of either source
1802       */
1803 <    public void testApplyToEither() {
1804 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1805 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1806 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1807 <        f.complete(one);
1808 <        checkCompletedNormally(g, two);
1809 <        f2.complete(one);
1810 <        checkCompletedNormally(g, two);
1803 >    public void testApplyToEither_normalCompletion1() {
1804 >        for (ExecutionMode m : ExecutionMode.values())
1805 >        for (Integer v1 : new Integer[] { 1, null })
1806 >        for (Integer v2 : new Integer[] { 2, null }) {
1807 >
1808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 >        final IncFunction r = new IncFunction();
1811 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1812 >
1813 >        f.complete(v1);
1814 >        checkCompletedNormally(h, inc(v1));
1815 >        g.complete(v2);
1816 >
1817 >        checkCompletedNormally(f, v1);
1818 >        checkCompletedNormally(g, v2);
1819 >        checkCompletedNormally(h, inc(v1));
1820 >        }
1821 >    }
1822  
1823 <        f = new CompletableFuture<>();
1824 <        f.complete(one);
1825 <        f2 = new CompletableFuture<>();
1826 <        g = f.applyToEither(f2, inc);
1827 <        checkCompletedNormally(g, two);
1823 >    public void testApplyToEither_normalCompletion2() {
1824 >        for (ExecutionMode m : ExecutionMode.values())
1825 >        for (Integer v1 : new Integer[] { 1, null })
1826 >        for (Integer v2 : new Integer[] { 2, null }) {
1827 >
1828 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1829 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1830 >        final IncFunction r = new IncFunction();
1831 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1832 >
1833 >        g.complete(v2);
1834 >        checkCompletedNormally(h, inc(v2));
1835 >        f.complete(v1);
1836 >
1837 >        checkCompletedNormally(f, v1);
1838 >        checkCompletedNormally(g, v2);
1839 >        checkCompletedNormally(h, inc(v2));
1840 >        }
1841 >    }
1842 >    public void testApplyToEither_normalCompletion3() {
1843 >        for (ExecutionMode m : ExecutionMode.values())
1844 >        for (Integer v1 : new Integer[] { 1, null })
1845 >        for (Integer v2 : new Integer[] { 2, null }) {
1846 >
1847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1848 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1849 >        final IncFunction r = new IncFunction();
1850 >
1851 >        f.complete(v1);
1852 >        g.complete(v2);
1853 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1854 >
1855 >        checkCompletedNormally(f, v1);
1856 >        checkCompletedNormally(g, v2);
1857 >
1858 >        // unspecified behavior
1859 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1860 >                   Objects.equals(h.join(), inc(v2)));
1861 >        assertEquals(r.invocationCount, 1);
1862 >        }
1863      }
1864  
1865      /**
1866       * applyToEither result completes exceptionally after exceptional
1867       * completion of either source
1868       */
1869 <    public void testApplyToEither2() {
1870 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1871 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1872 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1873 <        f.completeExceptionally(new CFException());
1874 <        f2.complete(one);
1875 <        checkCompletedWithWrappedCFException(g);
1869 >    public void testApplyToEither_exceptionalCompletion1() {
1870 >        for (ExecutionMode m : ExecutionMode.values())
1871 >        for (Integer v1 : new Integer[] { 1, null }) {
1872 >
1873 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1874 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1875 >        final IncFunction r = new IncFunction();
1876 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1877 >        final CFException ex = new CFException();
1878 >
1879 >        f.completeExceptionally(ex);
1880 >        checkCompletedWithWrappedCFException(h, ex);
1881 >        g.complete(v1);
1882 >
1883 >        assertEquals(r.invocationCount, 0);
1884 >        checkCompletedNormally(g, v1);
1885 >        checkCompletedWithWrappedCFException(f, ex);
1886 >        checkCompletedWithWrappedCFException(h, ex);
1887 >        }
1888 >    }
1889  
1890 <        f = new CompletableFuture<>();
1891 <        f2 = new CompletableFuture<>();
1892 <        f2.completeExceptionally(new CFException());
1893 <        g = f.applyToEither(f2, inc);
1894 <        checkCompletedWithWrappedCFException(g);
1890 >    public void testApplyToEither_exceptionalCompletion2() {
1891 >        for (ExecutionMode m : ExecutionMode.values())
1892 >        for (Integer v1 : new Integer[] { 1, null }) {
1893 >
1894 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1895 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1896 >        final IncFunction r = new IncFunction();
1897 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1898 >        final CFException ex = new CFException();
1899 >
1900 >        g.completeExceptionally(ex);
1901 >        checkCompletedWithWrappedCFException(h, ex);
1902 >        f.complete(v1);
1903 >
1904 >        assertEquals(r.invocationCount, 0);
1905 >        checkCompletedNormally(f, v1);
1906 >        checkCompletedWithWrappedCFException(g, ex);
1907 >        checkCompletedWithWrappedCFException(h, ex);
1908 >        }
1909 >    }
1910 >
1911 >    public void testApplyToEither_exceptionalCompletion3() {
1912 >        for (ExecutionMode m : ExecutionMode.values())
1913 >        for (Integer v1 : new Integer[] { 1, null }) {
1914 >
1915 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1916 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1917 >        final IncFunction r = new IncFunction();
1918 >        final CFException ex = new CFException();
1919 >
1920 >        g.completeExceptionally(ex);
1921 >        f.complete(v1);
1922 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1923 >
1924 >        // unspecified behavior
1925 >        Integer v;
1926 >        try {
1927 >            assertEquals(h.join(), inc(v1));
1928 >            assertEquals(r.invocationCount, 1);
1929 >        } catch (CompletionException ok) {
1930 >            checkCompletedWithWrappedCFException(h, ex);
1931 >            assertEquals(r.invocationCount, 0);
1932 >        }
1933 >
1934 >        checkCompletedWithWrappedCFException(g, ex);
1935 >        checkCompletedNormally(f, v1);
1936 >        }
1937 >    }
1938 >
1939 >    public void testApplyToEither_exceptionalCompletion4() {
1940 >        for (ExecutionMode m : ExecutionMode.values())
1941 >        for (Integer v1 : new Integer[] { 1, null }) {
1942 >
1943 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1944 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1945 >        final IncFunction r = new IncFunction();
1946 >        final CFException ex = new CFException();
1947 >
1948 >        f.completeExceptionally(ex);
1949 >        g.complete(v1);
1950 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1951 >
1952 >        // unspecified behavior
1953 >        Integer v;
1954 >        try {
1955 >            assertEquals(h.join(), inc(v1));
1956 >            assertEquals(r.invocationCount, 1);
1957 >        } catch (CompletionException ok) {
1958 >            checkCompletedWithWrappedCFException(h, ex);
1959 >            assertEquals(r.invocationCount, 0);
1960 >        }
1961 >
1962 >        checkCompletedWithWrappedCFException(f, ex);
1963 >        checkCompletedNormally(g, v1);
1964 >        }
1965      }
1966  
1967      /**
1968       * applyToEither result completes exceptionally if action does
1969       */
1970 <    public void testApplyToEither3() {
1971 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1972 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1973 <        FailingFunction r = new FailingFunction();
1974 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1975 <        f2.complete(two);
1976 <        checkCompletedWithWrappedCFException(g);
1970 >    public void testApplyToEither_actionFailed1() {
1971 >        for (ExecutionMode m : ExecutionMode.values())
1972 >        for (Integer v1 : new Integer[] { 1, null })
1973 >        for (Integer v2 : new Integer[] { 2, null }) {
1974 >
1975 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1976 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1977 >        final FailingFunction r = new FailingFunction();
1978 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1979 >
1980 >        f.complete(v1);
1981 >        checkCompletedWithWrappedCFException(h);
1982 >        g.complete(v2);
1983 >        checkCompletedNormally(f, v1);
1984 >        checkCompletedNormally(g, v2);
1985 >        }
1986 >    }
1987 >
1988 >    public void testApplyToEither_actionFailed2() {
1989 >        for (ExecutionMode m : ExecutionMode.values())
1990 >        for (Integer v1 : new Integer[] { 1, null })
1991 >        for (Integer v2 : new Integer[] { 2, null }) {
1992 >
1993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1994 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1995 >        final FailingFunction r = new FailingFunction();
1996 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1997 >
1998 >        g.complete(v2);
1999 >        checkCompletedWithWrappedCFException(h);
2000 >        f.complete(v1);
2001 >        checkCompletedNormally(f, v1);
2002 >        checkCompletedNormally(g, v2);
2003 >        }
2004      }
2005  
2006      /**
2007       * applyToEither result completes exceptionally if either source cancelled
2008       */
2009 <    public void testApplyToEither4() {
2010 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2011 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2012 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2013 <        assertTrue(f.cancel(true));
2014 <        checkCompletedWithWrappedCancellationException(g);
2015 <        f = new CompletableFuture<>();
2016 <        f2 = new CompletableFuture<>();
2017 <        assertTrue(f2.cancel(true));
2018 <        checkCompletedWithWrappedCancellationException(g);
2009 >    public void testApplyToEither_sourceCancelled1() {
2010 >        for (ExecutionMode m : ExecutionMode.values())
2011 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2012 >        for (Integer v1 : new Integer[] { 1, null }) {
2013 >
2014 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2015 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2016 >        final IncFunction r = new IncFunction();
2017 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2018 >
2019 >        assertTrue(f.cancel(mayInterruptIfRunning));
2020 >        checkCompletedWithWrappedCancellationException(h);
2021 >        g.complete(v1);
2022 >
2023 >        checkCancelled(f);
2024 >        assertEquals(r.invocationCount, 0);
2025 >        checkCompletedNormally(g, v1);
2026 >        checkCompletedWithWrappedCancellationException(h);
2027 >        }
2028 >    }
2029 >
2030 >    public void testApplyToEither_sourceCancelled2() {
2031 >        for (ExecutionMode m : ExecutionMode.values())
2032 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2033 >        for (Integer v1 : new Integer[] { 1, null }) {
2034 >
2035 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2036 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2037 >        final IncFunction r = new IncFunction();
2038 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2039 >
2040 >        assertTrue(g.cancel(mayInterruptIfRunning));
2041 >        checkCompletedWithWrappedCancellationException(h);
2042 >        f.complete(v1);
2043 >
2044 >        checkCancelled(g);
2045 >        assertEquals(r.invocationCount, 0);
2046 >        checkCompletedNormally(f, v1);
2047 >        checkCompletedWithWrappedCancellationException(h);
2048 >        }
2049 >    }
2050 >
2051 >    public void testApplyToEither_sourceCancelled3() {
2052 >        for (ExecutionMode m : ExecutionMode.values())
2053 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2054 >        for (Integer v1 : new Integer[] { 1, null }) {
2055 >
2056 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2057 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2058 >        final IncFunction r = new IncFunction();
2059 >
2060 >        assertTrue(g.cancel(mayInterruptIfRunning));
2061 >        f.complete(v1);
2062 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2063 >
2064 >        // unspecified behavior
2065 >        Integer v;
2066 >        try {
2067 >            assertEquals(h.join(), inc(v1));
2068 >            assertEquals(r.invocationCount, 1);
2069 >        } catch (CompletionException ok) {
2070 >            checkCompletedWithWrappedCancellationException(h);
2071 >            assertEquals(r.invocationCount, 0);
2072 >        }
2073 >
2074 >        checkCancelled(g);
2075 >        checkCompletedNormally(f, v1);
2076 >        }
2077 >    }
2078 >
2079 >    public void testApplyToEither_sourceCancelled4() {
2080 >        for (ExecutionMode m : ExecutionMode.values())
2081 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2082 >        for (Integer v1 : new Integer[] { 1, null }) {
2083 >
2084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086 >        final IncFunction r = new IncFunction();
2087 >
2088 >        assertTrue(f.cancel(mayInterruptIfRunning));
2089 >        g.complete(v1);
2090 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 >
2092 >        // unspecified behavior
2093 >        Integer v;
2094 >        try {
2095 >            assertEquals(h.join(), inc(v1));
2096 >            assertEquals(r.invocationCount, 1);
2097 >        } catch (CompletionException ok) {
2098 >            checkCompletedWithWrappedCancellationException(h);
2099 >            assertEquals(r.invocationCount, 0);
2100 >        }
2101 >
2102 >        checkCancelled(f);
2103 >        checkCompletedNormally(g, v1);
2104 >        }
2105      }
2106  
2107      /**
2108       * acceptEither result completes normally after normal completion
2109       * of either source
2110       */
2111 <    public void testAcceptEither() {
2112 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2113 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2114 <        IncAction r = new IncAction();
2115 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2116 <        f.complete(one);
2117 <        checkCompletedNormally(g, null);
2118 <        f2.complete(one);
2119 <        checkCompletedNormally(g, null);
1111 <        assertEquals(r.value, 2);
2111 >    public void testAcceptEither_normalCompletion1() {
2112 >        for (ExecutionMode m : ExecutionMode.values())
2113 >        for (Integer v1 : new Integer[] { 1, null })
2114 >        for (Integer v2 : new Integer[] { 2, null }) {
2115 >
2116 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2117 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2118 >        final IncAction r = new IncAction();
2119 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2120  
2121 <        r = new IncAction();
2122 <        f = new CompletableFuture<>();
2123 <        f.complete(one);
2124 <        f2 = new CompletableFuture<>();
2125 <        g = f.acceptEither(f2, r);
2126 <        checkCompletedNormally(g, null);
2127 <        assertEquals(r.value, 2);
2121 >        f.complete(v1);
2122 >        checkCompletedNormally(h, null);
2123 >        assertEquals(r.value, inc(v1));
2124 >        g.complete(v2);
2125 >
2126 >        checkCompletedNormally(f, v1);
2127 >        checkCompletedNormally(g, v2);
2128 >        checkCompletedNormally(h, null);
2129 >        }
2130 >    }
2131 >
2132 >    public void testAcceptEither_normalCompletion2() {
2133 >        for (ExecutionMode m : ExecutionMode.values())
2134 >        for (Integer v1 : new Integer[] { 1, null })
2135 >        for (Integer v2 : new Integer[] { 2, null }) {
2136 >
2137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2138 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2139 >        final IncAction r = new IncAction();
2140 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2141 >
2142 >        g.complete(v2);
2143 >        checkCompletedNormally(h, null);
2144 >        assertEquals(r.value, inc(v2));
2145 >        f.complete(v1);
2146 >
2147 >        checkCompletedNormally(f, v1);
2148 >        checkCompletedNormally(g, v2);
2149 >        checkCompletedNormally(h, null);
2150 >        }
2151 >    }
2152 >    public void testAcceptEither_normalCompletion3() {
2153 >        for (ExecutionMode m : ExecutionMode.values())
2154 >        for (Integer v1 : new Integer[] { 1, null })
2155 >        for (Integer v2 : new Integer[] { 2, null }) {
2156 >
2157 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2158 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 >        final IncAction r = new IncAction();
2160 >
2161 >        f.complete(v1);
2162 >        g.complete(v2);
2163 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2164 >
2165 >        checkCompletedNormally(h, null);
2166 >        checkCompletedNormally(f, v1);
2167 >        checkCompletedNormally(g, v2);
2168 >
2169 >        // unspecified behavior
2170 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2171 >                   Objects.equals(r.value, inc(v2)));
2172 >        }
2173      }
2174  
2175      /**
2176       * acceptEither result completes exceptionally after exceptional
2177       * completion of either source
2178       */
2179 <    public void testAcceptEither2() {
2180 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2181 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2182 <        IncAction r = new IncAction();
2183 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2184 <        f.completeExceptionally(new CFException());
2185 <        f2.complete(one);
2186 <        checkCompletedWithWrappedCFException(g);
2179 >    public void testAcceptEither_exceptionalCompletion1() {
2180 >        for (ExecutionMode m : ExecutionMode.values())
2181 >        for (Integer v1 : new Integer[] { 1, null }) {
2182 >
2183 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2184 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2185 >        final IncAction r = new IncAction();
2186 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2187 >        final CFException ex = new CFException();
2188 >
2189 >        f.completeExceptionally(ex);
2190 >        checkCompletedWithWrappedCFException(h, ex);
2191 >        g.complete(v1);
2192 >
2193 >        assertEquals(r.invocationCount, 0);
2194 >        checkCompletedNormally(g, v1);
2195 >        checkCompletedWithWrappedCFException(f, ex);
2196 >        checkCompletedWithWrappedCFException(h, ex);
2197 >        }
2198 >    }
2199  
2200 <        r = new IncAction();
2201 <        f = new CompletableFuture<>();
2202 <        f2 = new CompletableFuture<>();
2203 <        f2.completeExceptionally(new CFException());
2204 <        g = f.acceptEither(f2, r);
2205 <        checkCompletedWithWrappedCFException(g);
2200 >    public void testAcceptEither_exceptionalCompletion2() {
2201 >        for (ExecutionMode m : ExecutionMode.values())
2202 >        for (Integer v1 : new Integer[] { 1, null }) {
2203 >
2204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2205 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2206 >        final IncAction r = new IncAction();
2207 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2208 >        final CFException ex = new CFException();
2209 >
2210 >        g.completeExceptionally(ex);
2211 >        checkCompletedWithWrappedCFException(h, ex);
2212 >        f.complete(v1);
2213 >
2214 >        assertEquals(r.invocationCount, 0);
2215 >        checkCompletedNormally(f, v1);
2216 >        checkCompletedWithWrappedCFException(g, ex);
2217 >        checkCompletedWithWrappedCFException(h, ex);
2218 >        }
2219 >    }
2220 >
2221 >    public void testAcceptEither_exceptionalCompletion3() {
2222 >        for (ExecutionMode m : ExecutionMode.values())
2223 >        for (Integer v1 : new Integer[] { 1, null }) {
2224 >
2225 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2226 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2227 >        final IncAction r = new IncAction();
2228 >        final CFException ex = new CFException();
2229 >
2230 >        g.completeExceptionally(ex);
2231 >        f.complete(v1);
2232 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2233 >
2234 >        // unspecified behavior
2235 >        Integer v;
2236 >        try {
2237 >            assertEquals(h.join(), null);
2238 >            assertEquals(r.invocationCount, 1);
2239 >            assertEquals(inc(v1), r.value);
2240 >        } catch (CompletionException ok) {
2241 >            checkCompletedWithWrappedCFException(h, ex);
2242 >            assertEquals(r.invocationCount, 0);
2243 >        }
2244 >
2245 >        checkCompletedWithWrappedCFException(g, ex);
2246 >        checkCompletedNormally(f, v1);
2247 >        }
2248 >    }
2249 >
2250 >    public void testAcceptEither_exceptionalCompletion4() {
2251 >        for (ExecutionMode m : ExecutionMode.values())
2252 >        for (Integer v1 : new Integer[] { 1, null }) {
2253 >
2254 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2255 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2256 >        final IncAction r = new IncAction();
2257 >        final CFException ex = new CFException();
2258 >
2259 >        f.completeExceptionally(ex);
2260 >        g.complete(v1);
2261 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2262 >
2263 >        // unspecified behavior
2264 >        Integer v;
2265 >        try {
2266 >            assertEquals(h.join(), null);
2267 >            assertEquals(r.invocationCount, 1);
2268 >            assertEquals(inc(v1), r.value);
2269 >        } catch (CompletionException ok) {
2270 >            checkCompletedWithWrappedCFException(h, ex);
2271 >            assertEquals(r.invocationCount, 0);
2272 >        }
2273 >
2274 >        checkCompletedWithWrappedCFException(f, ex);
2275 >        checkCompletedNormally(g, v1);
2276 >        }
2277      }
2278  
2279      /**
2280       * acceptEither result completes exceptionally if action does
2281       */
2282 <    public void testAcceptEither3() {
2283 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2284 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2285 <        FailingConsumer r = new FailingConsumer();
2286 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2287 <        f2.complete(two);
2288 <        checkCompletedWithWrappedCFException(g);
2282 >    public void testAcceptEither_actionFailed1() {
2283 >        for (ExecutionMode m : ExecutionMode.values())
2284 >        for (Integer v1 : new Integer[] { 1, null })
2285 >        for (Integer v2 : new Integer[] { 2, null }) {
2286 >
2287 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2288 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2289 >        final FailingConsumer r = new FailingConsumer();
2290 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2291 >
2292 >        f.complete(v1);
2293 >        checkCompletedWithWrappedCFException(h);
2294 >        g.complete(v2);
2295 >        checkCompletedNormally(f, v1);
2296 >        checkCompletedNormally(g, v2);
2297 >        }
2298 >    }
2299 >
2300 >    public void testAcceptEither_actionFailed2() {
2301 >        for (ExecutionMode m : ExecutionMode.values())
2302 >        for (Integer v1 : new Integer[] { 1, null })
2303 >        for (Integer v2 : new Integer[] { 2, null }) {
2304 >
2305 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2306 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2307 >        final FailingConsumer r = new FailingConsumer();
2308 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2309 >
2310 >        g.complete(v2);
2311 >        checkCompletedWithWrappedCFException(h);
2312 >        f.complete(v1);
2313 >        checkCompletedNormally(f, v1);
2314 >        checkCompletedNormally(g, v2);
2315 >        }
2316      }
2317  
2318      /**
2319       * acceptEither result completes exceptionally if either source cancelled
2320       */
2321 <    public void testAcceptEither4() {
2322 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2324 <        IncAction r = new IncAction();
2325 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2326 <        assertTrue(f.cancel(true));
2327 <        checkCompletedWithWrappedCancellationException(g);
2328 <        f = new CompletableFuture<>();
2329 <        f2 = new CompletableFuture<>();
2330 <        assertTrue(f2.cancel(true));
2331 <        checkCompletedWithWrappedCancellationException(g);
2321 >    public void testAcceptEither_sourceCancelled1() {
2322 >        for (ExecutionMode m : ExecutionMode.values())
2323 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2324 >        for (Integer v1 : new Integer[] { 1, null }) {
2325 >
2326 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2327 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2328 >        final IncAction r = new IncAction();
2329 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2330 >
2331 >        assertTrue(f.cancel(mayInterruptIfRunning));
2332 >        checkCompletedWithWrappedCancellationException(h);
2333 >        g.complete(v1);
2334 >
2335 >        checkCancelled(f);
2336 >        assertEquals(r.invocationCount, 0);
2337 >        checkCompletedNormally(g, v1);
2338 >        checkCompletedWithWrappedCancellationException(h);
2339 >        }
2340 >    }
2341 >
2342 >    public void testAcceptEither_sourceCancelled2() {
2343 >        for (ExecutionMode m : ExecutionMode.values())
2344 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2345 >        for (Integer v1 : new Integer[] { 1, null }) {
2346 >
2347 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2348 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2349 >        final IncAction r = new IncAction();
2350 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2351 >
2352 >        assertTrue(g.cancel(mayInterruptIfRunning));
2353 >        checkCompletedWithWrappedCancellationException(h);
2354 >        f.complete(v1);
2355 >
2356 >        checkCancelled(g);
2357 >        assertEquals(r.invocationCount, 0);
2358 >        checkCompletedNormally(f, v1);
2359 >        checkCompletedWithWrappedCancellationException(h);
2360 >        }
2361 >    }
2362 >
2363 >    public void testAcceptEither_sourceCancelled3() {
2364 >        for (ExecutionMode m : ExecutionMode.values())
2365 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2366 >        for (Integer v1 : new Integer[] { 1, null }) {
2367 >
2368 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2369 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2370 >        final IncAction r = new IncAction();
2371 >
2372 >        assertTrue(g.cancel(mayInterruptIfRunning));
2373 >        f.complete(v1);
2374 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2375 >
2376 >        // unspecified behavior
2377 >        Integer v;
2378 >        try {
2379 >            assertEquals(h.join(), null);
2380 >            assertEquals(r.invocationCount, 1);
2381 >            assertEquals(inc(v1), r.value);
2382 >        } catch (CompletionException ok) {
2383 >            checkCompletedWithWrappedCancellationException(h);
2384 >            assertEquals(r.invocationCount, 0);
2385 >        }
2386 >
2387 >        checkCancelled(g);
2388 >        checkCompletedNormally(f, v1);
2389 >        }
2390      }
2391  
2392 +    public void testAcceptEither_sourceCancelled4() {
2393 +        for (ExecutionMode m : ExecutionMode.values())
2394 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2395 +        for (Integer v1 : new Integer[] { 1, null }) {
2396 +
2397 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2398 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2399 +        final IncAction r = new IncAction();
2400 +
2401 +        assertTrue(f.cancel(mayInterruptIfRunning));
2402 +        g.complete(v1);
2403 +        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2404 +
2405 +        // unspecified behavior
2406 +        Integer v;
2407 +        try {
2408 +            assertEquals(h.join(), null);
2409 +            assertEquals(r.invocationCount, 1);
2410 +            assertEquals(inc(v1), r.value);
2411 +        } catch (CompletionException ok) {
2412 +            checkCompletedWithWrappedCancellationException(h);
2413 +            assertEquals(r.invocationCount, 0);
2414 +        }
2415 +
2416 +        checkCancelled(f);
2417 +        checkCompletedNormally(g, v1);
2418 +        }
2419 +    }
2420  
2421      /**
2422       * runAfterEither result completes normally after normal completion
2423       * of either source
2424       */
2425 <    public void testRunAfterEither() {
2426 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2427 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2428 <        Noop r = new Noop();
2429 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2430 <        f.complete(one);
2431 <        checkCompletedNormally(g, null);
2432 <        f2.complete(one);
2433 <        checkCompletedNormally(g, null);
1185 <        assertTrue(r.ran);
2425 >    public void testRunAfterEither_normalCompletion1() {
2426 >        for (ExecutionMode m : ExecutionMode.values())
2427 >        for (Integer v1 : new Integer[] { 1, null })
2428 >        for (Integer v2 : new Integer[] { 2, null }) {
2429 >
2430 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2431 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2432 >        final Noop r = new Noop();
2433 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2434  
2435 <        r = new Noop();
2436 <        f = new CompletableFuture<>();
2437 <        f.complete(one);
2438 <        f2 = new CompletableFuture<>();
2439 <        g = f.runAfterEither(f2, r);
2440 <        checkCompletedNormally(g, null);
2441 <        assertTrue(r.ran);
2435 >        f.complete(v1);
2436 >        checkCompletedNormally(h, null);
2437 >        assertEquals(r.invocationCount, 1);
2438 >        g.complete(v2);
2439 >
2440 >        checkCompletedNormally(f, v1);
2441 >        checkCompletedNormally(g, v2);
2442 >        checkCompletedNormally(h, null);
2443 >        assertEquals(r.invocationCount, 1);
2444 >        }
2445 >    }
2446 >
2447 >    public void testRunAfterEither_normalCompletion2() {
2448 >        for (ExecutionMode m : ExecutionMode.values())
2449 >        for (Integer v1 : new Integer[] { 1, null })
2450 >        for (Integer v2 : new Integer[] { 2, null }) {
2451 >
2452 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2453 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 >        final Noop r = new Noop();
2455 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456 >
2457 >        g.complete(v2);
2458 >        checkCompletedNormally(h, null);
2459 >        assertEquals(r.invocationCount, 1);
2460 >        f.complete(v1);
2461 >
2462 >        checkCompletedNormally(f, v1);
2463 >        checkCompletedNormally(g, v2);
2464 >        checkCompletedNormally(h, null);
2465 >        assertEquals(r.invocationCount, 1);
2466 >        }
2467 >    }
2468 >    public void testRunAfterEither_normalCompletion3() {
2469 >        for (ExecutionMode m : ExecutionMode.values())
2470 >        for (Integer v1 : new Integer[] { 1, null })
2471 >        for (Integer v2 : new Integer[] { 2, null }) {
2472 >
2473 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2474 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2475 >        final Noop r = new Noop();
2476 >
2477 >        f.complete(v1);
2478 >        g.complete(v2);
2479 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2480 >
2481 >        checkCompletedNormally(h, null);
2482 >        checkCompletedNormally(f, v1);
2483 >        checkCompletedNormally(g, v2);
2484 >        assertEquals(r.invocationCount, 1);
2485 >        }
2486      }
2487  
2488      /**
2489       * runAfterEither result completes exceptionally after exceptional
2490       * completion of either source
2491       */
2492 <    public void testRunAfterEither2() {
2493 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2494 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2495 <        Noop r = new Noop();
2496 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2497 <        f.completeExceptionally(new CFException());
2498 <        f2.complete(one);
2499 <        checkCompletedWithWrappedCFException(g);
2492 >    public void testRunAfterEither_exceptionalCompletion1() {
2493 >        for (ExecutionMode m : ExecutionMode.values())
2494 >        for (Integer v1 : new Integer[] { 1, null }) {
2495 >
2496 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2497 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2498 >        final Noop r = new Noop();
2499 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2500 >        final CFException ex = new CFException();
2501 >
2502 >        f.completeExceptionally(ex);
2503 >        checkCompletedWithWrappedCFException(h, ex);
2504 >        g.complete(v1);
2505 >
2506 >        assertEquals(r.invocationCount, 0);
2507 >        checkCompletedNormally(g, v1);
2508 >        checkCompletedWithWrappedCFException(f, ex);
2509 >        checkCompletedWithWrappedCFException(h, ex);
2510 >        }
2511 >    }
2512  
2513 <        r = new Noop();
2514 <        f = new CompletableFuture<>();
2515 <        f2 = new CompletableFuture<>();
2516 <        f2.completeExceptionally(new CFException());
2517 <        g = f.runAfterEither(f2, r);
2518 <        checkCompletedWithWrappedCFException(g);
2513 >    public void testRunAfterEither_exceptionalCompletion2() {
2514 >        for (ExecutionMode m : ExecutionMode.values())
2515 >        for (Integer v1 : new Integer[] { 1, null }) {
2516 >
2517 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2518 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2519 >        final Noop r = new Noop();
2520 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2521 >        final CFException ex = new CFException();
2522 >
2523 >        g.completeExceptionally(ex);
2524 >        checkCompletedWithWrappedCFException(h, ex);
2525 >        f.complete(v1);
2526 >
2527 >        assertEquals(r.invocationCount, 0);
2528 >        checkCompletedNormally(f, v1);
2529 >        checkCompletedWithWrappedCFException(g, ex);
2530 >        checkCompletedWithWrappedCFException(h, ex);
2531 >        }
2532 >    }
2533 >
2534 >    public void testRunAfterEither_exceptionalCompletion3() {
2535 >        for (ExecutionMode m : ExecutionMode.values())
2536 >        for (Integer v1 : new Integer[] { 1, null }) {
2537 >
2538 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2539 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2540 >        final Noop r = new Noop();
2541 >        final CFException ex = new CFException();
2542 >
2543 >        g.completeExceptionally(ex);
2544 >        f.complete(v1);
2545 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2546 >
2547 >        // unspecified behavior
2548 >        Integer v;
2549 >        try {
2550 >            assertEquals(h.join(), null);
2551 >            assertEquals(r.invocationCount, 1);
2552 >        } catch (CompletionException ok) {
2553 >            checkCompletedWithWrappedCFException(h, ex);
2554 >            assertEquals(r.invocationCount, 0);
2555 >        }
2556 >
2557 >        checkCompletedWithWrappedCFException(g, ex);
2558 >        checkCompletedNormally(f, v1);
2559 >        }
2560 >    }
2561 >
2562 >    public void testRunAfterEither_exceptionalCompletion4() {
2563 >        for (ExecutionMode m : ExecutionMode.values())
2564 >        for (Integer v1 : new Integer[] { 1, null }) {
2565 >
2566 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2567 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2568 >        final Noop r = new Noop();
2569 >        final CFException ex = new CFException();
2570 >
2571 >        f.completeExceptionally(ex);
2572 >        g.complete(v1);
2573 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2574 >
2575 >        // unspecified behavior
2576 >        Integer v;
2577 >        try {
2578 >            assertEquals(h.join(), null);
2579 >            assertEquals(r.invocationCount, 1);
2580 >        } catch (CompletionException ok) {
2581 >            checkCompletedWithWrappedCFException(h, ex);
2582 >            assertEquals(r.invocationCount, 0);
2583 >        }
2584 >
2585 >        checkCompletedWithWrappedCFException(f, ex);
2586 >        checkCompletedNormally(g, v1);
2587 >        }
2588      }
2589  
2590      /**
2591       * runAfterEither result completes exceptionally if action does
2592       */
2593 <    public void testRunAfterEither3() {
2594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2595 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2596 <        FailingNoop r = new FailingNoop();
2597 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2598 <        f2.complete(two);
2599 <        checkCompletedWithWrappedCFException(g);
2593 >    public void testRunAfterEither_actionFailed1() {
2594 >        for (ExecutionMode m : ExecutionMode.values())
2595 >        for (Integer v1 : new Integer[] { 1, null })
2596 >        for (Integer v2 : new Integer[] { 2, null }) {
2597 >
2598 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2599 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2600 >        final FailingNoop r = new FailingNoop();
2601 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2602 >
2603 >        f.complete(v1);
2604 >        checkCompletedWithWrappedCFException(h);
2605 >        g.complete(v2);
2606 >        checkCompletedNormally(f, v1);
2607 >        checkCompletedNormally(g, v2);
2608 >        }
2609 >    }
2610 >
2611 >    public void testRunAfterEither_actionFailed2() {
2612 >        for (ExecutionMode m : ExecutionMode.values())
2613 >        for (Integer v1 : new Integer[] { 1, null })
2614 >        for (Integer v2 : new Integer[] { 2, null }) {
2615 >
2616 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2617 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2618 >        final FailingNoop r = new FailingNoop();
2619 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2620 >
2621 >        g.complete(v2);
2622 >        checkCompletedWithWrappedCFException(h);
2623 >        f.complete(v1);
2624 >        checkCompletedNormally(f, v1);
2625 >        checkCompletedNormally(g, v2);
2626 >        }
2627      }
2628  
2629      /**
2630       * runAfterEither result completes exceptionally if either source cancelled
2631       */
2632 <    public void testRunAfterEither4() {
2633 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2634 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635 <        Noop r = new Noop();
2636 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2637 <        assertTrue(f.cancel(true));
2638 <        checkCompletedWithWrappedCancellationException(g);
2639 <        f = new CompletableFuture<>();
2640 <        f2 = new CompletableFuture<>();
2641 <        assertTrue(f2.cancel(true));
2642 <        checkCompletedWithWrappedCancellationException(g);
2632 >    public void testRunAfterEither_sourceCancelled1() {
2633 >        for (ExecutionMode m : ExecutionMode.values())
2634 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2635 >        for (Integer v1 : new Integer[] { 1, null }) {
2636 >
2637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2639 >        final Noop r = new Noop();
2640 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2641 >
2642 >        assertTrue(f.cancel(mayInterruptIfRunning));
2643 >        checkCompletedWithWrappedCancellationException(h);
2644 >        g.complete(v1);
2645 >
2646 >        checkCancelled(f);
2647 >        assertEquals(r.invocationCount, 0);
2648 >        checkCompletedNormally(g, v1);
2649 >        checkCompletedWithWrappedCancellationException(h);
2650 >        }
2651 >    }
2652 >
2653 >    public void testRunAfterEither_sourceCancelled2() {
2654 >        for (ExecutionMode m : ExecutionMode.values())
2655 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2656 >        for (Integer v1 : new Integer[] { 1, null }) {
2657 >
2658 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2659 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2660 >        final Noop r = new Noop();
2661 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2662 >
2663 >        assertTrue(g.cancel(mayInterruptIfRunning));
2664 >        checkCompletedWithWrappedCancellationException(h);
2665 >        f.complete(v1);
2666 >
2667 >        checkCancelled(g);
2668 >        assertEquals(r.invocationCount, 0);
2669 >        checkCompletedNormally(f, v1);
2670 >        checkCompletedWithWrappedCancellationException(h);
2671 >        }
2672 >    }
2673 >
2674 >    public void testRunAfterEither_sourceCancelled3() {
2675 >        for (ExecutionMode m : ExecutionMode.values())
2676 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2677 >        for (Integer v1 : new Integer[] { 1, null }) {
2678 >
2679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 >        final Noop r = new Noop();
2682 >
2683 >        assertTrue(g.cancel(mayInterruptIfRunning));
2684 >        f.complete(v1);
2685 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2686 >
2687 >        // unspecified behavior
2688 >        Integer v;
2689 >        try {
2690 >            assertEquals(h.join(), null);
2691 >            assertEquals(r.invocationCount, 1);
2692 >        } catch (CompletionException ok) {
2693 >            checkCompletedWithWrappedCancellationException(h);
2694 >            assertEquals(r.invocationCount, 0);
2695 >        }
2696 >
2697 >        checkCancelled(g);
2698 >        checkCompletedNormally(f, v1);
2699 >        }
2700 >    }
2701 >
2702 >    public void testRunAfterEither_sourceCancelled4() {
2703 >        for (ExecutionMode m : ExecutionMode.values())
2704 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2705 >        for (Integer v1 : new Integer[] { 1, null }) {
2706 >
2707 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2708 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2709 >        final Noop r = new Noop();
2710 >
2711 >        assertTrue(f.cancel(mayInterruptIfRunning));
2712 >        g.complete(v1);
2713 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 >
2715 >        // unspecified behavior
2716 >        Integer v;
2717 >        try {
2718 >            assertEquals(h.join(), null);
2719 >            assertEquals(r.invocationCount, 1);
2720 >        } catch (CompletionException ok) {
2721 >            checkCompletedWithWrappedCancellationException(h);
2722 >            assertEquals(r.invocationCount, 0);
2723 >        }
2724 >
2725 >        checkCancelled(f);
2726 >        checkCompletedNormally(g, v1);
2727 >        }
2728      }
2729  
2730      /**
# Line 1317 | Line 2802 | public class CompletableFutureTest exten
2802          checkCompletedWithWrappedCancellationException(g);
2803      }
2804  
1320
2805      // asyncs
2806  
2807      /**
# Line 1350 | Line 2834 | public class CompletableFutureTest exten
2834          try {
2835              g.join();
2836              shouldThrow();
2837 <        } catch (Exception ok) {
1354 <        }
2837 >        } catch (CompletionException success) {}
2838          checkCompletedWithWrappedCFException(g);
2839      }
2840  
# Line 1429 | Line 2912 | public class CompletableFutureTest exten
2912          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2913          f.complete(one);
2914          checkCompletedNormally(g, null);
2915 <        assertEquals(r.value, 2);
2915 >        assertEquals(r.value, (Integer) 2);
2916      }
2917  
2918      /**
# Line 1467 | Line 2950 | public class CompletableFutureTest exten
2950      }
2951  
2952      /**
1470     * thenCombineAsync result completes normally after normal
1471     * completion of sources
1472     */
1473    public void testThenCombineAsync() {
1474        CompletableFuture<Integer> f, g, h;
1475
1476        f = new CompletableFuture<>();
1477        g = new CompletableFuture<>();
1478        h = f.thenCombineAsync(g, subtract);
1479        f.complete(3);
1480        checkIncomplete(h);
1481        g.complete(1);
1482        checkCompletedNormally(h, 2);
1483
1484        f = new CompletableFuture<>();
1485        g = new CompletableFuture<>();
1486        h = f.thenCombineAsync(g, subtract);
1487        g.complete(1);
1488        checkIncomplete(h);
1489        f.complete(3);
1490        checkCompletedNormally(h, 2);
1491
1492        f = new CompletableFuture<>();
1493        g = new CompletableFuture<>();
1494        g.complete(1);
1495        f.complete(3);
1496        h = f.thenCombineAsync(g, subtract);
1497        checkCompletedNormally(h, 2);
1498    }
1499
1500    /**
1501     * thenCombineAsync result completes exceptionally after exceptional
1502     * completion of either source
1503     */
1504    public void testThenCombineAsync2() {
1505        CompletableFuture<Integer> f, g, h;
1506
1507        f = new CompletableFuture<>();
1508        g = new CompletableFuture<>();
1509        h = f.thenCombineAsync(g, subtract);
1510        f.completeExceptionally(new CFException());
1511        checkIncomplete(h);
1512        g.complete(1);
1513        checkCompletedWithWrappedCFException(h);
1514
1515        f = new CompletableFuture<>();
1516        g = new CompletableFuture<>();
1517        h = f.thenCombineAsync(g, subtract);
1518        g.completeExceptionally(new CFException());
1519        checkIncomplete(h);
1520        f.complete(3);
1521        checkCompletedWithWrappedCFException(h);
1522
1523        f = new CompletableFuture<>();
1524        g = new CompletableFuture<>();
1525        g.completeExceptionally(new CFException());
1526        f.complete(3);
1527        h = f.thenCombineAsync(g, subtract);
1528        checkCompletedWithWrappedCFException(h);
1529    }
1530
1531    /**
1532     * thenCombineAsync result completes exceptionally if action does
1533     */
1534    public void testThenCombineAsync3() {
1535        CompletableFuture<Integer> f = new CompletableFuture<>();
1536        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1537        FailingBiFunction r = new FailingBiFunction();
1538        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1539        f.complete(one);
1540        checkIncomplete(g);
1541        assertFalse(r.ran);
1542        f2.complete(two);
1543        checkCompletedWithWrappedCFException(g);
1544        assertTrue(r.ran);
1545    }
1546
1547    /**
1548     * thenCombineAsync result completes exceptionally if either source cancelled
1549     */
1550    public void testThenCombineAsync4() {
1551        CompletableFuture<Integer> f, g, h;
1552
1553        f = new CompletableFuture<>();
1554        g = new CompletableFuture<>();
1555        h = f.thenCombineAsync(g, subtract);
1556        assertTrue(f.cancel(true));
1557        checkIncomplete(h);
1558        g.complete(1);
1559        checkCompletedWithWrappedCancellationException(h);
1560
1561        f = new CompletableFuture<>();
1562        g = new CompletableFuture<>();
1563        h = f.thenCombineAsync(g, subtract);
1564        assertTrue(g.cancel(true));
1565        checkIncomplete(h);
1566        f.complete(3);
1567        checkCompletedWithWrappedCancellationException(h);
1568
1569        f = new CompletableFuture<>();
1570        g = new CompletableFuture<>();
1571        g.complete(3);
1572        assertTrue(f.cancel(true));
1573        h = f.thenCombineAsync(g, subtract);
1574        checkCompletedWithWrappedCancellationException(h);
1575
1576        f = new CompletableFuture<>();
1577        g = new CompletableFuture<>();
1578        f.complete(3);
1579        assertTrue(g.cancel(true));
1580        h = f.thenCombineAsync(g, subtract);
1581        checkCompletedWithWrappedCancellationException(h);
1582    }
1583
1584    /**
1585     * thenAcceptBothAsync result completes normally after normal
1586     * completion of sources
1587     */
1588    public void testThenAcceptBothAsync() {
1589        CompletableFuture<Integer> f, g;
1590        CompletableFuture<Void> h;
1591        SubtractAction r;
1592
1593        f = new CompletableFuture<>();
1594        g = new CompletableFuture<>();
1595        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1596        f.complete(3);
1597        checkIncomplete(h);
1598        g.complete(1);
1599        checkCompletedNormally(h, null);
1600        assertEquals(r.value, 2);
1601
1602        f = new CompletableFuture<>();
1603        g = new CompletableFuture<>();
1604        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1605        g.complete(1);
1606        checkIncomplete(h);
1607        f.complete(3);
1608        checkCompletedNormally(h, null);
1609        assertEquals(r.value, 2);
1610
1611        f = new CompletableFuture<>();
1612        g = new CompletableFuture<>();
1613        g.complete(1);
1614        f.complete(3);
1615        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1616        checkCompletedNormally(h, null);
1617        assertEquals(r.value, 2);
1618    }
1619
1620    /**
1621     * thenAcceptBothAsync result completes exceptionally after exceptional
1622     * completion of source
1623     */
1624    public void testThenAcceptBothAsync2() {
1625        CompletableFuture<Integer> f, g;
1626        CompletableFuture<Void> h;
1627        SubtractAction r;
1628
1629        f = new CompletableFuture<>();
1630        g = new CompletableFuture<>();
1631        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1632        f.completeExceptionally(new CFException());
1633        checkIncomplete(h);
1634        g.complete(1);
1635        checkCompletedWithWrappedCFException(h);
1636
1637        f = new CompletableFuture<>();
1638        g = new CompletableFuture<>();
1639        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1640        g.completeExceptionally(new CFException());
1641        checkIncomplete(h);
1642        f.complete(3);
1643        checkCompletedWithWrappedCFException(h);
1644
1645        f = new CompletableFuture<>();
1646        g = new CompletableFuture<>();
1647        f.complete(3);
1648        g.completeExceptionally(new CFException());
1649        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1650        checkCompletedWithWrappedCFException(h);
1651
1652        f = new CompletableFuture<>();
1653        g = new CompletableFuture<>();
1654        f.completeExceptionally(new CFException());
1655        g.complete(3);
1656        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1657        checkCompletedWithWrappedCFException(h);
1658    }
1659
1660    /**
1661     * thenAcceptBothAsync result completes exceptionally if action does
1662     */
1663    public void testThenAcceptBothAsync3() {
1664        CompletableFuture<Integer> f, g;
1665        CompletableFuture<Void> h;
1666        FailingBiConsumer r;
1667
1668        f = new CompletableFuture<>();
1669        g = new CompletableFuture<>();
1670        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1671        f.complete(3);
1672        checkIncomplete(h);
1673        g.complete(1);
1674        checkCompletedWithWrappedCFException(h);
1675
1676        f = new CompletableFuture<>();
1677        g = new CompletableFuture<>();
1678        f.complete(3);
1679        g.complete(1);
1680        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1681        checkCompletedWithWrappedCFException(h);
1682    }
1683
1684    /**
1685     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1686     */
1687    public void testThenAcceptBothAsync4() {
1688        CompletableFuture<Integer> f, g;
1689        CompletableFuture<Void> h;
1690        SubtractAction r;
1691
1692        f = new CompletableFuture<>();
1693        g = new CompletableFuture<>();
1694        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1695        assertTrue(f.cancel(true));
1696        checkIncomplete(h);
1697        g.complete(1);
1698        checkCompletedWithWrappedCancellationException(h);
1699
1700        f = new CompletableFuture<>();
1701        g = new CompletableFuture<>();
1702        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1703        assertTrue(g.cancel(true));
1704        checkIncomplete(h);
1705        f.complete(3);
1706        checkCompletedWithWrappedCancellationException(h);
1707
1708        f = new CompletableFuture<>();
1709        g = new CompletableFuture<>();
1710        f.complete(3);
1711        assertTrue(g.cancel(true));
1712        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1713        checkCompletedWithWrappedCancellationException(h);
1714
1715        f = new CompletableFuture<>();
1716        g = new CompletableFuture<>();
1717        assertTrue(f.cancel(true));
1718        g.complete(3);
1719        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1720        checkCompletedWithWrappedCancellationException(h);
1721    }
1722
1723    /**
1724     * runAfterBothAsync result completes normally after normal
1725     * completion of sources
1726     */
1727    public void testRunAfterBothAsync() {
1728        CompletableFuture<Integer> f = new CompletableFuture<>();
1729        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1730        Noop r = new Noop();
1731        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1732        f.complete(one);
1733        checkIncomplete(g);
1734        f2.complete(two);
1735        checkCompletedNormally(g, null);
1736        assertTrue(r.ran);
1737    }
1738
1739    /**
1740     * runAfterBothAsync result completes exceptionally after exceptional
1741     * completion of source
1742     */
1743    public void testRunAfterBothAsync2() {
1744        CompletableFuture<Integer> f = new CompletableFuture<>();
1745        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746        Noop r = new Noop();
1747        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1748        f.completeExceptionally(new CFException());
1749        f2.complete(two);
1750        checkCompletedWithWrappedCFException(g);
1751
1752        r = new Noop();
1753        f = new CompletableFuture<>();
1754        f2 = new CompletableFuture<>();
1755        g = f.runAfterBothAsync(f2, r);
1756        f.complete(one);
1757        f2.completeExceptionally(new CFException());
1758        checkCompletedWithWrappedCFException(g);
1759    }
1760
1761    /**
1762     * runAfterBothAsync result completes exceptionally if action does
1763     */
1764    public void testRunAfterBothAsync3() {
1765        CompletableFuture<Integer> f = new CompletableFuture<>();
1766        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1767        FailingNoop r = new FailingNoop();
1768        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1769        f.complete(one);
1770        checkIncomplete(g);
1771        f2.complete(two);
1772        checkCompletedWithWrappedCFException(g);
1773    }
1774
1775    /**
1776     * runAfterBothAsync result completes exceptionally if either source cancelled
1777     */
1778    public void testRunAfterBothAsync4() {
1779        CompletableFuture<Integer> f = new CompletableFuture<>();
1780        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1781        Noop r = new Noop();
1782        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1783        assertTrue(f.cancel(true));
1784        f2.complete(two);
1785        checkCompletedWithWrappedCancellationException(g);
1786
1787        r = new Noop();
1788        f = new CompletableFuture<>();
1789        f2 = new CompletableFuture<>();
1790        g = f.runAfterBothAsync(f2, r);
1791        f.complete(one);
1792        assertTrue(f2.cancel(true));
1793        checkCompletedWithWrappedCancellationException(g);
1794    }
1795
1796    /**
1797     * applyToEitherAsync result completes normally after normal
1798     * completion of sources
1799     */
1800    public void testApplyToEitherAsync() {
1801        CompletableFuture<Integer> f = new CompletableFuture<>();
1802        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1803        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1804        f.complete(one);
1805        checkCompletedNormally(g, two);
1806
1807        f = new CompletableFuture<>();
1808        f.complete(one);
1809        f2 = new CompletableFuture<>();
1810        g = f.applyToEitherAsync(f2, inc);
1811        checkCompletedNormally(g, two);
1812    }
1813
1814    /**
1815     * applyToEitherAsync result completes exceptionally after exceptional
1816     * completion of source
1817     */
1818    public void testApplyToEitherAsync2() {
1819        CompletableFuture<Integer> f = new CompletableFuture<>();
1820        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1821        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1822        f.completeExceptionally(new CFException());
1823        checkCompletedWithWrappedCFException(g);
1824
1825        f = new CompletableFuture<>();
1826        f2 = new CompletableFuture<>();
1827        f2.completeExceptionally(new CFException());
1828        g = f.applyToEitherAsync(f2, inc);
1829        f.complete(one);
1830        checkCompletedWithWrappedCFException(g);
1831    }
1832
1833    /**
1834     * applyToEitherAsync result completes exceptionally if action does
1835     */
1836    public void testApplyToEitherAsync3() {
1837        CompletableFuture<Integer> f = new CompletableFuture<>();
1838        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1839        FailingFunction r = new FailingFunction();
1840        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1841        f.complete(one);
1842        checkCompletedWithWrappedCFException(g);
1843    }
1844
1845    /**
1846     * applyToEitherAsync result completes exceptionally if either source cancelled
1847     */
1848    public void testApplyToEitherAsync4() {
1849        CompletableFuture<Integer> f = new CompletableFuture<>();
1850        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1851        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1852        assertTrue(f.cancel(true));
1853        checkCompletedWithWrappedCancellationException(g);
1854
1855        f = new CompletableFuture<>();
1856        f2 = new CompletableFuture<>();
1857        assertTrue(f2.cancel(true));
1858        g = f.applyToEitherAsync(f2, inc);
1859        checkCompletedWithWrappedCancellationException(g);
1860    }
1861
1862    /**
1863     * acceptEitherAsync result completes normally after normal
1864     * completion of sources
1865     */
1866    public void testAcceptEitherAsync() {
1867        CompletableFuture<Integer> f = new CompletableFuture<>();
1868        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1869        IncAction r = new IncAction();
1870        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1871        f.complete(one);
1872        checkCompletedNormally(g, null);
1873        assertEquals(r.value, 2);
1874
1875        r = new IncAction();
1876        f = new CompletableFuture<>();
1877        f.complete(one);
1878        f2 = new CompletableFuture<>();
1879        g = f.acceptEitherAsync(f2, r);
1880        checkCompletedNormally(g, null);
1881        assertEquals(r.value, 2);
1882    }
1883
1884    /**
1885     * acceptEitherAsync result completes exceptionally after exceptional
1886     * completion of source
1887     */
1888    public void testAcceptEitherAsync2() {
1889        CompletableFuture<Integer> f = new CompletableFuture<>();
1890        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1891        IncAction r = new IncAction();
1892        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1893        f.completeExceptionally(new CFException());
1894        checkCompletedWithWrappedCFException(g);
1895
1896        r = new IncAction();
1897        f = new CompletableFuture<>();
1898        f2 = new CompletableFuture<>();
1899        f2.completeExceptionally(new CFException());
1900        g = f.acceptEitherAsync(f2, r);
1901        f.complete(one);
1902        checkCompletedWithWrappedCFException(g);
1903    }
1904
1905    /**
1906     * acceptEitherAsync result completes exceptionally if action does
1907     */
1908    public void testAcceptEitherAsync3() {
1909        CompletableFuture<Integer> f = new CompletableFuture<>();
1910        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1911        FailingConsumer r = new FailingConsumer();
1912        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1913        f.complete(one);
1914        checkCompletedWithWrappedCFException(g);
1915    }
1916
1917    /**
1918     * acceptEitherAsync result completes exceptionally if either
1919     * source cancelled
1920     */
1921    public void testAcceptEitherAsync4() {
1922        CompletableFuture<Integer> f = new CompletableFuture<>();
1923        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1924        IncAction r = new IncAction();
1925        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1926        assertTrue(f.cancel(true));
1927        checkCompletedWithWrappedCancellationException(g);
1928
1929        r = new IncAction();
1930        f = new CompletableFuture<>();
1931        f2 = new CompletableFuture<>();
1932        assertTrue(f2.cancel(true));
1933        g = f.acceptEitherAsync(f2, r);
1934        checkCompletedWithWrappedCancellationException(g);
1935    }
1936
1937    /**
1938     * runAfterEitherAsync result completes normally after normal
1939     * completion of sources
1940     */
1941    public void testRunAfterEitherAsync() {
1942        CompletableFuture<Integer> f = new CompletableFuture<>();
1943        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1944        Noop r = new Noop();
1945        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1946        f.complete(one);
1947        checkCompletedNormally(g, null);
1948        assertTrue(r.ran);
1949
1950        r = new Noop();
1951        f = new CompletableFuture<>();
1952        f.complete(one);
1953        f2 = new CompletableFuture<>();
1954        g = f.runAfterEitherAsync(f2, r);
1955        checkCompletedNormally(g, null);
1956        assertTrue(r.ran);
1957    }
1958
1959    /**
1960     * runAfterEitherAsync result completes exceptionally after exceptional
1961     * completion of source
1962     */
1963    public void testRunAfterEitherAsync2() {
1964        CompletableFuture<Integer> f = new CompletableFuture<>();
1965        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1966        Noop r = new Noop();
1967        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1968        f.completeExceptionally(new CFException());
1969        checkCompletedWithWrappedCFException(g);
1970
1971        r = new Noop();
1972        f = new CompletableFuture<>();
1973        f2 = new CompletableFuture<>();
1974        f2.completeExceptionally(new CFException());
1975        g = f.runAfterEitherAsync(f2, r);
1976        f.complete(one);
1977        checkCompletedWithWrappedCFException(g);
1978    }
1979
1980    /**
1981     * runAfterEitherAsync result completes exceptionally if action does
1982     */
1983    public void testRunAfterEitherAsync3() {
1984        CompletableFuture<Integer> f = new CompletableFuture<>();
1985        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1986        FailingNoop r = new FailingNoop();
1987        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1988        f.complete(one);
1989        checkCompletedWithWrappedCFException(g);
1990    }
1991
1992    /**
1993     * runAfterEitherAsync result completes exceptionally if either
1994     * source cancelled
1995     */
1996    public void testRunAfterEitherAsync4() {
1997        CompletableFuture<Integer> f = new CompletableFuture<>();
1998        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1999        Noop r = new Noop();
2000        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2001        assertTrue(f.cancel(true));
2002        checkCompletedWithWrappedCancellationException(g);
2003
2004        r = new Noop();
2005        f = new CompletableFuture<>();
2006        f2 = new CompletableFuture<>();
2007        assertTrue(f2.cancel(true));
2008        g = f.runAfterEitherAsync(f2, r);
2009        checkCompletedWithWrappedCancellationException(g);
2010    }
2011
2012    /**
2953       * thenComposeAsync result completes normally after normal
2954       * completion of source
2955       */
# Line 2085 | Line 3025 | public class CompletableFutureTest exten
3025          checkCompletedWithWrappedCancellationException(g);
3026      }
3027  
2088
3028      // async with explicit executors
3029  
3030      /**
# Line 2118 | Line 3057 | public class CompletableFutureTest exten
3057          try {
3058              g.join();
3059              shouldThrow();
3060 <        } catch (Exception ok) {
2122 <        }
3060 >        } catch (CompletionException success) {}
3061          checkCompletedWithWrappedCFException(g);
3062      }
3063  
# Line 2197 | Line 3135 | public class CompletableFutureTest exten
3135          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3136          f.complete(one);
3137          checkCompletedNormally(g, null);
3138 <        assertEquals(r.value, 2);
3138 >        assertEquals(r.value, (Integer) 2);
3139      }
3140  
3141      /**
# Line 2235 | Line 3173 | public class CompletableFutureTest exten
3173      }
3174  
3175      /**
2238     * thenCombineAsync result completes normally after normal
2239     * completion of sources
2240     */
2241    public void testThenCombineAsyncE() {
2242        CompletableFuture<Integer> f, g, h;
2243        ThreadExecutor e = new ThreadExecutor();
2244        int count = 0;
2245
2246        f = new CompletableFuture<>();
2247        g = new CompletableFuture<>();
2248        h = f.thenCombineAsync(g, subtract, e);
2249        f.complete(3);
2250        checkIncomplete(h);
2251        g.complete(1);
2252        checkCompletedNormally(h, 2);
2253        assertEquals(++count, e.count.get());
2254
2255        f = new CompletableFuture<>();
2256        g = new CompletableFuture<>();
2257        h = f.thenCombineAsync(g, subtract, e);
2258        g.complete(1);
2259        checkIncomplete(h);
2260        f.complete(3);
2261        checkCompletedNormally(h, 2);
2262        assertEquals(++count, e.count.get());
2263
2264        f = new CompletableFuture<>();
2265        g = new CompletableFuture<>();
2266        g.complete(1);
2267        f.complete(3);
2268        h = f.thenCombineAsync(g, subtract, e);
2269        checkCompletedNormally(h, 2);
2270        assertEquals(++count, e.count.get());
2271    }
2272
2273    /**
2274     * thenCombineAsync result completes exceptionally after exceptional
2275     * completion of either source
2276     */
2277    public void testThenCombineAsync2E() {
2278        CompletableFuture<Integer> f, g, h;
2279        ThreadExecutor e = new ThreadExecutor();
2280        int count = 0;
2281
2282        f = new CompletableFuture<>();
2283        g = new CompletableFuture<>();
2284        h = f.thenCombineAsync(g, subtract, e);
2285        f.completeExceptionally(new CFException());
2286        checkIncomplete(h);
2287        g.complete(1);
2288        checkCompletedWithWrappedCFException(h);
2289
2290        f = new CompletableFuture<>();
2291        g = new CompletableFuture<>();
2292        h = f.thenCombineAsync(g, subtract, e);
2293        g.completeExceptionally(new CFException());
2294        checkIncomplete(h);
2295        f.complete(3);
2296        checkCompletedWithWrappedCFException(h);
2297
2298        f = new CompletableFuture<>();
2299        g = new CompletableFuture<>();
2300        g.completeExceptionally(new CFException());
2301        h = f.thenCombineAsync(g, subtract, e);
2302        checkIncomplete(h);
2303        f.complete(3);
2304        checkCompletedWithWrappedCFException(h);
2305
2306        assertEquals(0, e.count.get());
2307    }
2308
2309    /**
2310     * thenCombineAsync result completes exceptionally if action does
2311     */
2312    public void testThenCombineAsync3E() {
2313        CompletableFuture<Integer> f = new CompletableFuture<>();
2314        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2315        FailingBiFunction r = new FailingBiFunction();
2316        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2317        f.complete(one);
2318        checkIncomplete(g);
2319        assertFalse(r.ran);
2320        f2.complete(two);
2321        checkCompletedWithWrappedCFException(g);
2322        assertTrue(r.ran);
2323    }
2324
2325    /**
2326     * thenCombineAsync result completes exceptionally if either source cancelled
2327     */
2328    public void testThenCombineAsync4E() {
2329        CompletableFuture<Integer> f, g, h;
2330        ThreadExecutor e = new ThreadExecutor();
2331
2332        f = new CompletableFuture<>();
2333        g = new CompletableFuture<>();
2334        h = f.thenCombineAsync(g, subtract, e);
2335        assertTrue(f.cancel(true));
2336        checkIncomplete(h);
2337        g.complete(1);
2338        checkCompletedWithWrappedCancellationException(h);
2339
2340        f = new CompletableFuture<>();
2341        g = new CompletableFuture<>();
2342        h = f.thenCombineAsync(g, subtract, e);
2343        assertTrue(g.cancel(true));
2344        checkIncomplete(h);
2345        f.complete(3);
2346        checkCompletedWithWrappedCancellationException(h);
2347
2348        f = new CompletableFuture<>();
2349        g = new CompletableFuture<>();
2350        assertTrue(g.cancel(true));
2351        h = f.thenCombineAsync(g, subtract, e);
2352        checkIncomplete(h);
2353        f.complete(3);
2354        checkCompletedWithWrappedCancellationException(h);
2355
2356        f = new CompletableFuture<>();
2357        g = new CompletableFuture<>();
2358        assertTrue(f.cancel(true));
2359        assertTrue(g.cancel(true));
2360        h = f.thenCombineAsync(g, subtract, e);
2361        checkCompletedWithWrappedCancellationException(h);
2362
2363        assertEquals(0, e.count.get());
2364    }
2365
2366    /**
2367     * thenAcceptBothAsync result completes normally after normal
2368     * completion of sources
2369     */
2370    public void testThenAcceptBothAsyncE() {
2371        CompletableFuture<Integer> f, g;
2372        CompletableFuture<Void> h;
2373        SubtractAction r;
2374        ThreadExecutor e = new ThreadExecutor();
2375
2376        f = new CompletableFuture<>();
2377        g = new CompletableFuture<>();
2378        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2379        f.complete(3);
2380        checkIncomplete(h);
2381        g.complete(1);
2382        checkCompletedNormally(h, null);
2383        assertEquals(r.value, 2);
2384
2385        f = new CompletableFuture<>();
2386        g = new CompletableFuture<>();
2387        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2388        g.complete(1);
2389        checkIncomplete(h);
2390        f.complete(3);
2391        checkCompletedNormally(h, null);
2392        assertEquals(r.value, 2);
2393
2394        f = new CompletableFuture<>();
2395        g = new CompletableFuture<>();
2396        g.complete(1);
2397        f.complete(3);
2398        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2399        checkCompletedNormally(h, null);
2400        assertEquals(r.value, 2);
2401
2402        assertEquals(3, e.count.get());
2403    }
2404
2405    /**
2406     * thenAcceptBothAsync result completes exceptionally after exceptional
2407     * completion of source
2408     */
2409    public void testThenAcceptBothAsync2E() {
2410        CompletableFuture<Integer> f, g;
2411        CompletableFuture<Void> h;
2412        SubtractAction r;
2413        ThreadExecutor e = new ThreadExecutor();
2414
2415        f = new CompletableFuture<>();
2416        g = new CompletableFuture<>();
2417        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2418        f.completeExceptionally(new CFException());
2419        checkIncomplete(h);
2420        g.complete(1);
2421        checkCompletedWithWrappedCFException(h);
2422
2423        f = new CompletableFuture<>();
2424        g = new CompletableFuture<>();
2425        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2426        g.completeExceptionally(new CFException());
2427        checkIncomplete(h);
2428        f.complete(3);
2429        checkCompletedWithWrappedCFException(h);
2430
2431        f = new CompletableFuture<>();
2432        g = new CompletableFuture<>();
2433        f.complete(3);
2434        g.completeExceptionally(new CFException());
2435        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2436        checkCompletedWithWrappedCFException(h);
2437
2438        f = new CompletableFuture<>();
2439        g = new CompletableFuture<>();
2440        f.completeExceptionally(new CFException());
2441        g.complete(3);
2442        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2443        checkCompletedWithWrappedCFException(h);
2444
2445        assertEquals(0, e.count.get());
2446    }
2447
2448    /**
2449     * thenAcceptBothAsync result completes exceptionally if action does
2450     */
2451    public void testThenAcceptBothAsync3E() {
2452        CompletableFuture<Integer> f, g;
2453        CompletableFuture<Void> h;
2454        FailingBiConsumer r;
2455        ThreadExecutor e = new ThreadExecutor();
2456
2457        f = new CompletableFuture<>();
2458        g = new CompletableFuture<>();
2459        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2460        f.complete(3);
2461        checkIncomplete(h);
2462        g.complete(1);
2463        checkCompletedWithWrappedCFException(h);
2464
2465        f = new CompletableFuture<>();
2466        g = new CompletableFuture<>();
2467        f.complete(3);
2468        g.complete(1);
2469        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2470        checkCompletedWithWrappedCFException(h);
2471
2472        assertEquals(2, e.count.get());
2473    }
2474
2475    /**
2476     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2477     */
2478    public void testThenAcceptBothAsync4E() {
2479        CompletableFuture<Integer> f, g;
2480        CompletableFuture<Void> h;
2481        SubtractAction r;
2482        ThreadExecutor e = new ThreadExecutor();
2483
2484        f = new CompletableFuture<>();
2485        g = new CompletableFuture<>();
2486        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2487        assertTrue(f.cancel(true));
2488        checkIncomplete(h);
2489        g.complete(1);
2490        checkCompletedWithWrappedCancellationException(h);
2491
2492        f = new CompletableFuture<>();
2493        g = new CompletableFuture<>();
2494        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2495        assertTrue(g.cancel(true));
2496        checkIncomplete(h);
2497        f.complete(3);
2498        checkCompletedWithWrappedCancellationException(h);
2499
2500        f = new CompletableFuture<>();
2501        g = new CompletableFuture<>();
2502        f.complete(3);
2503        assertTrue(g.cancel(true));
2504        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2505        checkCompletedWithWrappedCancellationException(h);
2506
2507        f = new CompletableFuture<>();
2508        g = new CompletableFuture<>();
2509        assertTrue(f.cancel(true));
2510        g.complete(3);
2511        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2512        checkCompletedWithWrappedCancellationException(h);
2513
2514        assertEquals(0, e.count.get());
2515    }
2516
2517    /**
2518     * runAfterBothAsync result completes normally after normal
2519     * completion of sources
2520     */
2521    public void testRunAfterBothAsyncE() {
2522        CompletableFuture<Integer> f = new CompletableFuture<>();
2523        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2524        Noop r = new Noop();
2525        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2526        f.complete(one);
2527        checkIncomplete(g);
2528        f2.complete(two);
2529        checkCompletedNormally(g, null);
2530        assertTrue(r.ran);
2531    }
2532
2533    /**
2534     * runAfterBothAsync result completes exceptionally after exceptional
2535     * completion of source
2536     */
2537    public void testRunAfterBothAsync2E() {
2538        CompletableFuture<Integer> f = new CompletableFuture<>();
2539        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2540        Noop r = new Noop();
2541        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2542        f.completeExceptionally(new CFException());
2543        f2.complete(two);
2544        checkCompletedWithWrappedCFException(g);
2545
2546        r = new Noop();
2547        f = new CompletableFuture<>();
2548        f2 = new CompletableFuture<>();
2549        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2550        f.complete(one);
2551        f2.completeExceptionally(new CFException());
2552        checkCompletedWithWrappedCFException(g);
2553    }
2554
2555    /**
2556     * runAfterBothAsync result completes exceptionally if action does
2557     */
2558    public void testRunAfterBothAsync3E() {
2559        CompletableFuture<Integer> f = new CompletableFuture<>();
2560        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2561        FailingNoop r = new FailingNoop();
2562        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2563        f.complete(one);
2564        checkIncomplete(g);
2565        f2.complete(two);
2566        checkCompletedWithWrappedCFException(g);
2567    }
2568
2569    /**
2570     * runAfterBothAsync result completes exceptionally if either source cancelled
2571     */
2572    public void testRunAfterBothAsync4E() {
2573        CompletableFuture<Integer> f = new CompletableFuture<>();
2574        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2575        Noop r = new Noop();
2576        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2577        assertTrue(f.cancel(true));
2578        f2.complete(two);
2579        checkCompletedWithWrappedCancellationException(g);
2580
2581        r = new Noop();
2582        f = new CompletableFuture<>();
2583        f2 = new CompletableFuture<>();
2584        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2585        f.complete(one);
2586        assertTrue(f2.cancel(true));
2587        checkCompletedWithWrappedCancellationException(g);
2588    }
2589
2590    /**
2591     * applyToEitherAsync result completes normally after normal
2592     * completion of sources
2593     */
2594    public void testApplyToEitherAsyncE() {
2595        CompletableFuture<Integer> f = new CompletableFuture<>();
2596        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2597        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2598        f.complete(one);
2599        checkCompletedNormally(g, two);
2600
2601        f = new CompletableFuture<>();
2602        f.complete(one);
2603        f2 = new CompletableFuture<>();
2604        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2605        checkCompletedNormally(g, two);
2606    }
2607
2608    /**
2609     * applyToEitherAsync result completes exceptionally after exceptional
2610     * completion of source
2611     */
2612    public void testApplyToEitherAsync2E() {
2613        CompletableFuture<Integer> f = new CompletableFuture<>();
2614        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2615        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2616        f.completeExceptionally(new CFException());
2617        checkCompletedWithWrappedCFException(g);
2618
2619        f = new CompletableFuture<>();
2620        f2 = new CompletableFuture<>();
2621        f2.completeExceptionally(new CFException());
2622        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2623        f.complete(one);
2624        checkCompletedWithWrappedCFException(g);
2625    }
2626
2627    /**
2628     * applyToEitherAsync result completes exceptionally if action does
2629     */
2630    public void testApplyToEitherAsync3E() {
2631        CompletableFuture<Integer> f = new CompletableFuture<>();
2632        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2633        FailingFunction r = new FailingFunction();
2634        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2635        f.complete(one);
2636        checkCompletedWithWrappedCFException(g);
2637    }
2638
2639    /**
2640     * applyToEitherAsync result completes exceptionally if either source cancelled
2641     */
2642    public void testApplyToEitherAsync4E() {
2643        CompletableFuture<Integer> f = new CompletableFuture<>();
2644        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2645        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2646        assertTrue(f.cancel(true));
2647        checkCompletedWithWrappedCancellationException(g);
2648
2649        f = new CompletableFuture<>();
2650        f2 = new CompletableFuture<>();
2651        assertTrue(f2.cancel(true));
2652        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2653        checkCompletedWithWrappedCancellationException(g);
2654    }
2655
2656    /**
2657     * acceptEitherAsync result completes normally after normal
2658     * completion of sources
2659     */
2660    public void testAcceptEitherAsyncE() {
2661        CompletableFuture<Integer> f = new CompletableFuture<>();
2662        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2663        IncAction r = new IncAction();
2664        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2665        f.complete(one);
2666        checkCompletedNormally(g, null);
2667        assertEquals(r.value, 2);
2668
2669        r = new IncAction();
2670        f = new CompletableFuture<>();
2671        f.complete(one);
2672        f2 = new CompletableFuture<>();
2673        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2674        checkCompletedNormally(g, null);
2675        assertEquals(r.value, 2);
2676    }
2677
2678    /**
2679     * acceptEitherAsync result completes exceptionally after exceptional
2680     * completion of source
2681     */
2682    public void testAcceptEitherAsync2E() {
2683        CompletableFuture<Integer> f = new CompletableFuture<>();
2684        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2685        IncAction r = new IncAction();
2686        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2687        f.completeExceptionally(new CFException());
2688        checkCompletedWithWrappedCFException(g);
2689
2690        r = new IncAction();
2691        f = new CompletableFuture<>();
2692        f2 = new CompletableFuture<>();
2693        f2.completeExceptionally(new CFException());
2694        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2695        f.complete(one);
2696        checkCompletedWithWrappedCFException(g);
2697    }
2698
2699    /**
2700     * acceptEitherAsync result completes exceptionally if action does
2701     */
2702    public void testAcceptEitherAsync3E() {
2703        CompletableFuture<Integer> f = new CompletableFuture<>();
2704        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2705        FailingConsumer r = new FailingConsumer();
2706        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2707        f.complete(one);
2708        checkCompletedWithWrappedCFException(g);
2709    }
2710
2711    /**
2712     * acceptEitherAsync result completes exceptionally if either
2713     * source cancelled
2714     */
2715    public void testAcceptEitherAsync4E() {
2716        CompletableFuture<Integer> f = new CompletableFuture<>();
2717        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2718        IncAction r = new IncAction();
2719        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2720        assertTrue(f.cancel(true));
2721        checkCompletedWithWrappedCancellationException(g);
2722
2723        r = new IncAction();
2724        f = new CompletableFuture<>();
2725        f2 = new CompletableFuture<>();
2726        assertTrue(f2.cancel(true));
2727        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2728        checkCompletedWithWrappedCancellationException(g);
2729    }
2730
2731    /**
2732     * runAfterEitherAsync result completes normally after normal
2733     * completion of sources
2734     */
2735    public void testRunAfterEitherAsyncE() {
2736        CompletableFuture<Integer> f = new CompletableFuture<>();
2737        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2738        Noop r = new Noop();
2739        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2740        f.complete(one);
2741        checkCompletedNormally(g, null);
2742        assertTrue(r.ran);
2743
2744        r = new Noop();
2745        f = new CompletableFuture<>();
2746        f.complete(one);
2747        f2 = new CompletableFuture<>();
2748        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2749        checkCompletedNormally(g, null);
2750        assertTrue(r.ran);
2751    }
2752
2753    /**
2754     * runAfterEitherAsync result completes exceptionally after exceptional
2755     * completion of source
2756     */
2757    public void testRunAfterEitherAsync2E() {
2758        CompletableFuture<Integer> f = new CompletableFuture<>();
2759        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2760        Noop r = new Noop();
2761        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2762        f.completeExceptionally(new CFException());
2763        checkCompletedWithWrappedCFException(g);
2764
2765        r = new Noop();
2766        f = new CompletableFuture<>();
2767        f2 = new CompletableFuture<>();
2768        f2.completeExceptionally(new CFException());
2769        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2770        f.complete(one);
2771        checkCompletedWithWrappedCFException(g);
2772    }
2773
2774    /**
2775     * runAfterEitherAsync result completes exceptionally if action does
2776     */
2777    public void testRunAfterEitherAsync3E() {
2778        CompletableFuture<Integer> f = new CompletableFuture<>();
2779        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2780        FailingNoop r = new FailingNoop();
2781        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2782        f.complete(one);
2783        checkCompletedWithWrappedCFException(g);
2784    }
2785
2786    /**
2787     * runAfterEitherAsync result completes exceptionally if either
2788     * source cancelled
2789     */
2790    public void testRunAfterEitherAsync4E() {
2791        CompletableFuture<Integer> f = new CompletableFuture<>();
2792        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2793        Noop r = new Noop();
2794        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2795        assertTrue(f.cancel(true));
2796        checkCompletedWithWrappedCancellationException(g);
2797
2798        r = new Noop();
2799        f = new CompletableFuture<>();
2800        f2 = new CompletableFuture<>();
2801        assertTrue(f2.cancel(true));
2802        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2803        checkCompletedWithWrappedCancellationException(g);
2804    }
2805
2806    /**
3176       * thenComposeAsync result completes normally after normal
3177       * completion of source
3178       */
# Line 2936 | Line 3305 | public class CompletableFutureTest exten
3305          ThreadExecutor exec = new ThreadExecutor();
3306  
3307          Runnable[] throwingActions = {
3308 <            () -> { CompletableFuture.supplyAsync(null); },
3309 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3310 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3311 <
3312 <            () -> { CompletableFuture.runAsync(null); },
3313 <            () -> { CompletableFuture.runAsync(null, exec); },
3314 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3315 <
3316 <            () -> { f.completeExceptionally(null); },
3317 <
3318 <            () -> { f.thenApply(null); },
3319 <            () -> { f.thenApplyAsync(null); },
3320 <            () -> { f.thenApplyAsync((x) -> x, null); },
3321 <            () -> { f.thenApplyAsync(null, exec); },
3322 <
3323 <            () -> { f.thenAccept(null); },
3324 <            () -> { f.thenAcceptAsync(null); },
3325 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3326 <            () -> { f.thenAcceptAsync(null, exec); },
3327 <
3328 <            () -> { f.thenRun(null); },
3329 <            () -> { f.thenRunAsync(null); },
3330 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3331 <            () -> { f.thenRunAsync(null, exec); },
3332 <
3333 <            () -> { f.thenCombine(g, null); },
3334 <            () -> { f.thenCombineAsync(g, null); },
3335 <            () -> { f.thenCombineAsync(g, null, exec); },
3336 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3337 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3338 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3339 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3340 <
3341 <            () -> { f.thenAcceptBoth(g, null); },
3342 <            () -> { f.thenAcceptBothAsync(g, null); },
3343 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3344 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3345 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3346 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3347 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3348 <
3349 <            () -> { f.runAfterBoth(g, null); },
3350 <            () -> { f.runAfterBothAsync(g, null); },
3351 <            () -> { f.runAfterBothAsync(g, null, exec); },
3352 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3353 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3354 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3355 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3356 <
3357 <            () -> { f.applyToEither(g, null); },
3358 <            () -> { f.applyToEitherAsync(g, null); },
3359 <            () -> { f.applyToEitherAsync(g, null, exec); },
3360 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3361 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3362 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3363 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3364 <
3365 <            () -> { f.acceptEither(g, null); },
3366 <            () -> { f.acceptEitherAsync(g, null); },
3367 <            () -> { f.acceptEitherAsync(g, null, exec); },
3368 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3369 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3370 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3371 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3372 <
3373 <            () -> { f.runAfterEither(g, null); },
3374 <            () -> { f.runAfterEitherAsync(g, null); },
3375 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3376 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3377 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3378 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3379 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3380 <
3381 <            () -> { f.thenCompose(null); },
3382 <            () -> { f.thenComposeAsync(null); },
3383 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3384 <            () -> { f.thenComposeAsync(null, exec); },
3385 <
3386 <            () -> { f.exceptionally(null); },
3387 <
3388 <            () -> { f.handle(null); },
3389 <
3390 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3391 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3392 <            () -> { CompletableFuture.allOf(f, null); },
3393 <            () -> { CompletableFuture.allOf(null, f); },
3394 <
3395 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3396 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3397 <            () -> { CompletableFuture.anyOf(f, null); },
3398 <            () -> { CompletableFuture.anyOf(null, f); },
3308 >            () -> CompletableFuture.supplyAsync(null),
3309 >            () -> CompletableFuture.supplyAsync(null, exec),
3310 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
3311 >
3312 >            () -> CompletableFuture.runAsync(null),
3313 >            () -> CompletableFuture.runAsync(null, exec),
3314 >            () -> CompletableFuture.runAsync(() -> {}, null),
3315 >
3316 >            () -> f.completeExceptionally(null),
3317 >
3318 >            () -> f.thenApply(null),
3319 >            () -> f.thenApplyAsync(null),
3320 >            () -> f.thenApplyAsync((x) -> x, null),
3321 >            () -> f.thenApplyAsync(null, exec),
3322 >
3323 >            () -> f.thenAccept(null),
3324 >            () -> f.thenAcceptAsync(null),
3325 >            () -> f.thenAcceptAsync((x) -> {} , null),
3326 >            () -> f.thenAcceptAsync(null, exec),
3327 >
3328 >            () -> f.thenRun(null),
3329 >            () -> f.thenRunAsync(null),
3330 >            () -> f.thenRunAsync(() -> {} , null),
3331 >            () -> f.thenRunAsync(null, exec),
3332 >
3333 >            () -> f.thenCombine(g, null),
3334 >            () -> f.thenCombineAsync(g, null),
3335 >            () -> f.thenCombineAsync(g, null, exec),
3336 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3337 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3338 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3339 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3340 >
3341 >            () -> f.thenAcceptBoth(g, null),
3342 >            () -> f.thenAcceptBothAsync(g, null),
3343 >            () -> f.thenAcceptBothAsync(g, null, exec),
3344 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3345 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3346 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3347 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3348 >
3349 >            () -> f.runAfterBoth(g, null),
3350 >            () -> f.runAfterBothAsync(g, null),
3351 >            () -> f.runAfterBothAsync(g, null, exec),
3352 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3353 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3354 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3355 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3356 >
3357 >            () -> f.applyToEither(g, null),
3358 >            () -> f.applyToEitherAsync(g, null),
3359 >            () -> f.applyToEitherAsync(g, null, exec),
3360 >            () -> f.applyToEither(nullFuture, (x) -> x),
3361 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3362 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3363 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3364 >
3365 >            () -> f.acceptEither(g, null),
3366 >            () -> f.acceptEitherAsync(g, null),
3367 >            () -> f.acceptEitherAsync(g, null, exec),
3368 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3369 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3370 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3371 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3372 >
3373 >            () -> f.runAfterEither(g, null),
3374 >            () -> f.runAfterEitherAsync(g, null),
3375 >            () -> f.runAfterEitherAsync(g, null, exec),
3376 >            () -> f.runAfterEither(nullFuture, () -> {}),
3377 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3378 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3379 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3380 >
3381 >            () -> f.thenCompose(null),
3382 >            () -> f.thenComposeAsync(null),
3383 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3384 >            () -> f.thenComposeAsync(null, exec),
3385 >
3386 >            () -> f.exceptionally(null),
3387 >
3388 >            () -> f.handle(null),
3389 >
3390 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3391 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3392 >            () -> CompletableFuture.allOf(f, null),
3393 >            () -> CompletableFuture.allOf(null, f),
3394 >
3395 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3396 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3397 >            () -> CompletableFuture.anyOf(f, null),
3398 >            () -> CompletableFuture.anyOf(null, f),
3399 >
3400 >            () -> f.obtrudeException(null),
3401          };
3402  
3403          assertThrows(NullPointerException.class, throwingActions);
3404          assertEquals(0, exec.count.get());
3405      }
3406  
3407 +    /**
3408 +     * toCompletableFuture returns this CompletableFuture.
3409 +     */
3410 +    public void testToCompletableFuture() {
3411 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3412 +        assertSame(f, f.toCompletableFuture());
3413 +    }
3414 +
3415 +    /**
3416 +     * whenComplete action executes on normal completion, propagating
3417 +     * source result.
3418 +     */
3419 +    public void testWhenComplete_normalCompletion1() {
3420 +        for (ExecutionMode m : ExecutionMode.values())
3421 +        for (Integer v1 : new Integer[] { 1, null }) {
3422 +
3423 +        final AtomicInteger a = new AtomicInteger();
3424 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3425 +        final CompletableFuture<Integer> g =
3426 +            m.whenComplete(f,
3427 +                           (Integer x, Throwable t) -> {
3428 +                               threadAssertSame(x, v1);
3429 +                               threadAssertNull(t);
3430 +                               a.getAndIncrement();
3431 +                           });
3432 +        f.complete(v1);
3433 +        checkCompletedNormally(f, v1);
3434 +        checkCompletedNormally(g, v1);
3435 +        assertEquals(a.get(), 1);
3436 +        }
3437 +    }
3438 +
3439 +    /**
3440 +     * whenComplete action executes on exceptional completion, propagating
3441 +     * source result.
3442 +     */
3443 +    public void testWhenComplete_exceptionalCompletion() {
3444 +        for (ExecutionMode m : ExecutionMode.values())
3445 +        for (Integer v1 : new Integer[] { 1, null }) {
3446 +
3447 +        final AtomicInteger a = new AtomicInteger();
3448 +        final CFException ex = new CFException();
3449 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3450 +        final CompletableFuture<Integer> g = m.whenComplete
3451 +            (f,
3452 +             (Integer x, Throwable t) -> {
3453 +                threadAssertNull(x);
3454 +                threadAssertSame(t, ex);
3455 +                a.getAndIncrement();
3456 +            });
3457 +        f.completeExceptionally(ex);
3458 +        checkCompletedWithWrappedCFException(f, ex);
3459 +        checkCompletedWithWrappedCFException(g, ex);
3460 +        assertEquals(a.get(), 1);
3461 +        }
3462 +    }
3463 +
3464 +    /**
3465 +     * If a whenComplete action throws an exception when triggered by
3466 +     * a normal completion, it completes exceptionally
3467 +     */
3468 +    public void testWhenComplete_actionFailed() {
3469 +        for (ExecutionMode m : ExecutionMode.values())
3470 +        for (Integer v1 : new Integer[] { 1, null }) {
3471 +
3472 +        final CFException ex = new CFException();
3473 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3474 +        final CompletableFuture<Integer> g = m.whenComplete
3475 +            (f,
3476 +             (Integer x, Throwable t) -> {
3477 +                threadAssertSame(x, v1);
3478 +                threadAssertNull(t);
3479 +                throw ex;
3480 +            });
3481 +        f.complete(v1);
3482 +        checkCompletedNormally(f, v1);
3483 +        checkCompletedWithWrappedCFException(g, ex);
3484 +        }
3485 +    }
3486 +
3487 +    /**
3488 +     * If a whenComplete action throws an exception when triggered by
3489 +     * a source completion that also throws an exception, the source
3490 +     * exception takes precedence.
3491 +     */
3492 +    public void testWhenComplete_actionFailedSourceFailed() {
3493 +        for (ExecutionMode m : ExecutionMode.values())
3494 +        for (Integer v1 : new Integer[] { 1, null }) {
3495 +
3496 +        final CFException ex1 = new CFException();
3497 +        final CFException ex2 = new CFException();
3498 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3499 +        final CompletableFuture<Integer> g = m.whenComplete
3500 +            (f,
3501 +             (Integer x, Throwable t) -> {
3502 +                threadAssertSame(t, ex1);
3503 +                threadAssertNull(x);
3504 +                throw ex2;
3505 +            });
3506 +        f.completeExceptionally(ex1);
3507 +        checkCompletedWithWrappedCFException(f, ex1);
3508 +        checkCompletedWithWrappedCFException(g, ex1);
3509 +        }
3510 +    }
3511 +
3512 +    /**
3513 +     * handleAsync action completes normally with function value on
3514 +     * either normal or exceptional completion of source
3515 +     */
3516 +    public void testHandleAsync() {
3517 +        CompletableFuture<Integer> f, g;
3518 +        IntegerHandler r;
3519 +
3520 +        f = new CompletableFuture<>();
3521 +        g = f.handleAsync(r = new IntegerHandler());
3522 +        assertFalse(r.ran);
3523 +        f.completeExceptionally(new CFException());
3524 +        checkCompletedWithWrappedCFException(f);
3525 +        checkCompletedNormally(g, three);
3526 +        assertTrue(r.ran);
3527 +
3528 +        f = new CompletableFuture<>();
3529 +        g = f.handleAsync(r = new IntegerHandler());
3530 +        assertFalse(r.ran);
3531 +        f.completeExceptionally(new CFException());
3532 +        checkCompletedWithWrappedCFException(f);
3533 +        checkCompletedNormally(g, three);
3534 +        assertTrue(r.ran);
3535 +
3536 +        f = new CompletableFuture<>();
3537 +        g = f.handleAsync(r = new IntegerHandler());
3538 +        assertFalse(r.ran);
3539 +        f.complete(one);
3540 +        checkCompletedNormally(f, one);
3541 +        checkCompletedNormally(g, two);
3542 +        assertTrue(r.ran);
3543 +
3544 +        f = new CompletableFuture<>();
3545 +        g = f.handleAsync(r = new IntegerHandler());
3546 +        assertFalse(r.ran);
3547 +        f.complete(one);
3548 +        checkCompletedNormally(f, one);
3549 +        checkCompletedNormally(g, two);
3550 +        assertTrue(r.ran);
3551 +    }
3552 +
3553 +    /**
3554 +     * handleAsync action with Executor completes normally with
3555 +     * function value on either normal or exceptional completion of
3556 +     * source
3557 +     */
3558 +    public void testHandleAsync2() {
3559 +        CompletableFuture<Integer> f, g;
3560 +        ThreadExecutor exec = new ThreadExecutor();
3561 +        IntegerHandler r;
3562 +
3563 +        f = new CompletableFuture<>();
3564 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3565 +        assertFalse(r.ran);
3566 +        f.completeExceptionally(new CFException());
3567 +        checkCompletedWithWrappedCFException(f);
3568 +        checkCompletedNormally(g, three);
3569 +        assertTrue(r.ran);
3570 +
3571 +        f = new CompletableFuture<>();
3572 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3573 +        assertFalse(r.ran);
3574 +        f.completeExceptionally(new CFException());
3575 +        checkCompletedWithWrappedCFException(f);
3576 +        checkCompletedNormally(g, three);
3577 +        assertTrue(r.ran);
3578 +
3579 +        f = new CompletableFuture<>();
3580 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3581 +        assertFalse(r.ran);
3582 +        f.complete(one);
3583 +        checkCompletedNormally(f, one);
3584 +        checkCompletedNormally(g, two);
3585 +        assertTrue(r.ran);
3586 +
3587 +        f = new CompletableFuture<>();
3588 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3589 +        assertFalse(r.ran);
3590 +        f.complete(one);
3591 +        checkCompletedNormally(f, one);
3592 +        checkCompletedNormally(g, two);
3593 +        assertTrue(r.ran);
3594 +    }
3595 +
3596   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines