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.20 by jsr166, Mon Apr 8 16:45:15 2013 UTC vs.
Revision 1.42 by jsr166, Mon Jun 2 02:19:23 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 160 | Line 195 | public class CompletableFutureTest exten
195       * by methods isDone, isCancelled, and getNow
196       */
197      public void testConstructor() {
198 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
198 >        CompletableFuture<Integer> f = new CompletableFuture<>();
199          checkIncomplete(f);
200      }
201  
# Line 169 | Line 204 | public class CompletableFutureTest exten
204       * isCancelled, join, get, and getNow
205       */
206      public void testComplete() {
207 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
207 >        CompletableFuture<Integer> f = new CompletableFuture<>();
208          checkIncomplete(f);
209          f.complete(one);
210          checkCompletedNormally(f, one);
# Line 180 | Line 215 | public class CompletableFutureTest exten
215       * methods isDone, isCancelled, join, get, and getNow
216       */
217      public void testCompleteExceptionally() {
218 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
218 >        CompletableFuture<Integer> f = new CompletableFuture<>();
219          checkIncomplete(f);
220          f.completeExceptionally(new CFException());
221          checkCompletedWithWrappedCFException(f);
# Line 191 | Line 226 | public class CompletableFutureTest exten
226       * methods isDone, isCancelled, join, get, and getNow
227       */
228      public void testCancel() {
229 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
229 >        CompletableFuture<Integer> f = new CompletableFuture<>();
230          checkIncomplete(f);
231          assertTrue(f.cancel(true));
232          checkCancelled(f);
# Line 201 | Line 236 | public class CompletableFutureTest exten
236       * obtrudeValue forces completion with given value
237       */
238      public void testObtrudeValue() {
239 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
239 >        CompletableFuture<Integer> f = new CompletableFuture<>();
240          checkIncomplete(f);
241          f.complete(one);
242          checkCompletedNormally(f, one);
# Line 209 | Line 244 | public class CompletableFutureTest exten
244          checkCompletedNormally(f, three);
245          f.obtrudeValue(two);
246          checkCompletedNormally(f, two);
247 <        f = new CompletableFuture<Integer>();
247 >        f = new CompletableFuture<>();
248          f.obtrudeValue(three);
249          checkCompletedNormally(f, three);
250 <        f = new CompletableFuture<Integer>();
250 >        f = new CompletableFuture<>();
251          f.completeExceptionally(new CFException());
252          f.obtrudeValue(four);
253          checkCompletedNormally(f, four);
# Line 222 | Line 257 | public class CompletableFutureTest exten
257       * obtrudeException forces completion with given exception
258       */
259      public void testObtrudeException() {
260 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
260 >        CompletableFuture<Integer> f = new CompletableFuture<>();
261          checkIncomplete(f);
262          f.complete(one);
263          checkCompletedNormally(f, one);
264          f.obtrudeException(new CFException());
265          checkCompletedWithWrappedCFException(f);
266 <        f = new CompletableFuture<Integer>();
266 >        f = new CompletableFuture<>();
267          f.obtrudeException(new CFException());
268          checkCompletedWithWrappedCFException(f);
269 <        f = new CompletableFuture<Integer>();
269 >        f = new CompletableFuture<>();
270          f.completeExceptionally(new CFException());
271          f.obtrudeValue(four);
272          checkCompletedNormally(f, four);
# Line 243 | Line 278 | public class CompletableFutureTest exten
278       * getNumberOfDependents returns number of dependent tasks
279       */
280      public void testGetNumberOfDependents() {
281 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
281 >        CompletableFuture<Integer> f = new CompletableFuture<>();
282          assertEquals(f.getNumberOfDependents(), 0);
283          CompletableFuture g = f.thenRun(new Noop());
284          assertEquals(f.getNumberOfDependents(), 1);
# 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 AddAction implements BiConsumer<Integer, Integer> {
358 <        int value;
357 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
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 335 | Line 413 | public class CompletableFutureTest exten
413          boolean ran;
414          public CompletableFuture<Integer> apply(Integer x) {
415              ran = true;
416 <            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
416 >            CompletableFuture<Integer> f = new CompletableFuture<>();
417              f.complete(Integer.valueOf(x.intValue() + 1));
418              return f;
419          }
# 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<Integer>();
662 >        CompletableFuture<Integer> f = new CompletableFuture<>();
663          ExceptionToInteger r = new ExceptionToInteger();
664          CompletableFuture<Integer> g = f.exceptionally(r);
665          f.completeExceptionally(new CFException());
666          checkCompletedNormally(g, three);
667  
668 <        f = new CompletableFuture<Integer>();
668 >        f = new CompletableFuture<>();
669          r = new ExceptionToInteger();
670          g = f.exceptionally(r);
671          f.complete(one);
# Line 398 | Line 680 | public class CompletableFutureTest exten
680          CompletableFuture<Integer> f, g;
681          IntegerHandler r;
682  
683 <        f = new CompletableFuture<Integer>();
683 >        f = new CompletableFuture<>();
684          f.completeExceptionally(new CFException());
685          g = f.handle(r = new IntegerHandler());
686          assertTrue(r.ran);
687          checkCompletedNormally(g, three);
688  
689 <        f = new CompletableFuture<Integer>();
689 >        f = new CompletableFuture<>();
690          g = f.handle(r = new IntegerHandler());
691          assertFalse(r.ran);
692          f.completeExceptionally(new CFException());
693          checkCompletedNormally(g, three);
694          assertTrue(r.ran);
695  
696 <        f = new CompletableFuture<Integer>();
696 >        f = new CompletableFuture<>();
697          f.complete(one);
698          g = f.handle(r = new IntegerHandler());
699          assertTrue(r.ran);
700          checkCompletedNormally(g, two);
701  
702 <        f = new CompletableFuture<Integer>();
702 >        f = new CompletableFuture<>();
703          g = f.handle(r = new IntegerHandler());
704          assertFalse(r.ran);
705          f.complete(one);
# Line 495 | Line 777 | public class CompletableFutureTest exten
777       * thenRun result completes normally after normal completion of source
778       */
779      public void testThenRun() {
780 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
781 <        Noop r = new Noop();
782 <        CompletableFuture<Void> g = f.thenRun(r);
780 >        CompletableFuture<Integer> f;
781 >        CompletableFuture<Void> g;
782 >        Noop r;
783 >
784 >        f = new CompletableFuture<>();
785 >        g = f.thenRun(r = new Noop());
786          f.complete(null);
787          checkCompletedNormally(g, null);
788 <        // reordered version
789 <        f = new CompletableFuture<Integer>();
788 >        assertTrue(r.ran);
789 >
790 >        f = new CompletableFuture<>();
791          f.complete(null);
792 <        r = new Noop();
507 <        g = f.thenRun(r);
792 >        g = f.thenRun(r = new Noop());
793          checkCompletedNormally(g, null);
794 +        assertTrue(r.ran);
795      }
796  
797      /**
# Line 513 | Line 799 | public class CompletableFutureTest exten
799       * completion of source
800       */
801      public void testThenRun2() {
802 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
803 <        Noop r = new Noop();
804 <        CompletableFuture<Void> g = f.thenRun(r);
802 >        CompletableFuture<Integer> f;
803 >        CompletableFuture<Void> g;
804 >        Noop r;
805 >
806 >        f = new CompletableFuture<>();
807 >        g = f.thenRun(r = new Noop());
808 >        f.completeExceptionally(new CFException());
809 >        checkCompletedWithWrappedCFException(g);
810 >        assertFalse(r.ran);
811 >
812 >        f = new CompletableFuture<>();
813          f.completeExceptionally(new CFException());
814 +        g = f.thenRun(r = new Noop());
815          checkCompletedWithWrappedCFException(g);
816 +        assertFalse(r.ran);
817      }
818  
819      /**
820       * thenRun result completes exceptionally if action does
821       */
822      public void testThenRun3() {
823 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
824 <        FailingNoop r = new FailingNoop();
825 <        CompletableFuture<Void> g = f.thenRun(r);
823 >        CompletableFuture<Integer> f;
824 >        CompletableFuture<Void> g;
825 >        FailingNoop r;
826 >
827 >        f = new CompletableFuture<>();
828 >        g = f.thenRun(r = new FailingNoop());
829 >        f.complete(null);
830 >        checkCompletedWithWrappedCFException(g);
831 >
832 >        f = new CompletableFuture<>();
833          f.complete(null);
834 +        g = f.thenRun(r = new FailingNoop());
835          checkCompletedWithWrappedCFException(g);
836      }
837  
# Line 535 | Line 839 | public class CompletableFutureTest exten
839       * thenRun result completes exceptionally if source cancelled
840       */
841      public void testThenRun4() {
842 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
843 <        Noop r = new Noop();
844 <        CompletableFuture<Void> g = f.thenRun(r);
842 >        CompletableFuture<Integer> f;
843 >        CompletableFuture<Void> g;
844 >        Noop r;
845 >
846 >        f = new CompletableFuture<>();
847 >        g = f.thenRun(r = new Noop());
848          assertTrue(f.cancel(true));
849          checkCompletedWithWrappedCancellationException(g);
850 +
851 +        f = new CompletableFuture<>();
852 +        assertTrue(f.cancel(true));
853 +        g = f.thenRun(r = new Noop());
854 +        checkCompletedWithWrappedCancellationException(g);
855      }
856  
857      /**
858       * thenApply result completes normally after normal completion of source
859       */
860      public void testThenApply() {
861 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
861 >        CompletableFuture<Integer> f = new CompletableFuture<>();
862          CompletableFuture<Integer> g = f.thenApply(inc);
863          f.complete(one);
864          checkCompletedNormally(g, two);
# Line 557 | Line 869 | public class CompletableFutureTest exten
869       * completion of source
870       */
871      public void testThenApply2() {
872 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
872 >        CompletableFuture<Integer> f = new CompletableFuture<>();
873          CompletableFuture<Integer> g = f.thenApply(inc);
874          f.completeExceptionally(new CFException());
875          checkCompletedWithWrappedCFException(g);
# Line 567 | Line 879 | public class CompletableFutureTest exten
879       * thenApply result completes exceptionally if action does
880       */
881      public void testThenApply3() {
882 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
882 >        CompletableFuture<Integer> f = new CompletableFuture<>();
883          CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
884          f.complete(one);
885          checkCompletedWithWrappedCFException(g);
# Line 577 | Line 889 | public class CompletableFutureTest exten
889       * thenApply result completes exceptionally if source cancelled
890       */
891      public void testThenApply4() {
892 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
892 >        CompletableFuture<Integer> f = new CompletableFuture<>();
893          CompletableFuture<Integer> g = f.thenApply(inc);
894          assertTrue(f.cancel(true));
895          checkCompletedWithWrappedCancellationException(g);
# Line 587 | Line 899 | public class CompletableFutureTest exten
899       * thenAccept result completes normally after normal completion of source
900       */
901      public void testThenAccept() {
902 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
902 >        CompletableFuture<Integer> f = new CompletableFuture<>();
903          IncAction r = new IncAction();
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 600 | Line 912 | public class CompletableFutureTest exten
912       * completion of source
913       */
914      public void testThenAccept2() {
915 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
915 >        CompletableFuture<Integer> f = new CompletableFuture<>();
916          IncAction r = new IncAction();
917          CompletableFuture<Void> g = f.thenAccept(r);
918          f.completeExceptionally(new CFException());
# Line 611 | Line 923 | public class CompletableFutureTest exten
923       * thenAccept result completes exceptionally if action does
924       */
925      public void testThenAccept3() {
926 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
926 >        CompletableFuture<Integer> f = new CompletableFuture<>();
927          FailingConsumer r = new FailingConsumer();
928          CompletableFuture<Void> g = f.thenAccept(r);
929          f.complete(one);
# Line 623 | Line 935 | public class CompletableFutureTest exten
935       * thenAccept result completes exceptionally if source cancelled
936       */
937      public void testThenAccept4() {
938 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
938 >        CompletableFuture<Integer> f = new CompletableFuture<>();
939          IncAction r = new IncAction();
940          CompletableFuture<Void> g = f.thenAccept(r);
941          assertTrue(f.cancel(true));
942          checkCompletedWithWrappedCancellationException(g);
943      }
944  
633
945      /**
946       * thenCombine result completes normally after normal completion
947       * of sources
948       */
949 <    public void testThenCombine() {
950 <        CompletableFuture<Integer> f, g, h;
949 >    public void testThenCombine_normalCompletion1() {
950 >        for (ExecutionMode m : ExecutionMode.values())
951 >        for (Integer v1 : new Integer[] { 1, null })
952 >        for (Integer v2 : new Integer[] { 2, null }) {
953 >
954 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
955 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
956 >        final SubtractFunction r = new SubtractFunction();
957 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
958 >
959 >        f.complete(v1);
960 >        checkIncomplete(h);
961 >        assertEquals(r.invocationCount, 0);
962 >        g.complete(v2);
963 >
964 >        checkCompletedNormally(h, subtract(v1, v2));
965 >        checkCompletedNormally(f, v1);
966 >        checkCompletedNormally(g, v2);
967 >        assertEquals(r.invocationCount, 1);
968 >        }
969 >    }
970 >
971 >    public void testThenCombine_normalCompletion2() {
972 >        for (ExecutionMode m : ExecutionMode.values())
973 >        for (Integer v1 : new Integer[] { 1, null })
974 >        for (Integer v2 : new Integer[] { 2, null }) {
975 >
976 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
977 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
978 >        final SubtractFunction r = new SubtractFunction();
979 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
980 >
981 >        g.complete(v2);
982 >        checkIncomplete(h);
983 >        assertEquals(r.invocationCount, 0);
984 >        f.complete(v1);
985 >
986 >        checkCompletedNormally(h, subtract(v1, v2));
987 >        checkCompletedNormally(f, v1);
988 >        checkCompletedNormally(g, v2);
989 >        assertEquals(r.invocationCount, 1);
990 >        }
991 >    }
992  
993 <        f = new CompletableFuture<Integer>();
994 <        g = new CompletableFuture<Integer>();
995 <        h = f.thenCombine(g, subtract);
996 <        f.complete(3);
997 <        checkIncomplete(h);
998 <        g.complete(1);
999 <        checkCompletedNormally(h, 2);
1000 <
1001 <        f = new CompletableFuture<Integer>();
1002 <        g = new CompletableFuture<Integer>();
1003 <        h = f.thenCombine(g, subtract);
1004 <        g.complete(1);
1005 <        checkIncomplete(h);
1006 <        f.complete(3);
1007 <        checkCompletedNormally(h, 2);
1008 <
1009 <        f = new CompletableFuture<Integer>();
1010 <        g = new CompletableFuture<Integer>();
1011 <        g.complete(1);
1012 <        f.complete(3);
1013 <        h = f.thenCombine(g, subtract);
1014 <        checkCompletedNormally(h, 2);
993 >    public void testThenCombine_normalCompletion3() {
994 >        for (ExecutionMode m : ExecutionMode.values())
995 >        for (Integer v1 : new Integer[] { 1, null })
996 >        for (Integer v2 : new Integer[] { 2, null }) {
997 >
998 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
999 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1000 >        final SubtractFunction r = new SubtractFunction();
1001 >
1002 >        g.complete(v2);
1003 >        f.complete(v1);
1004 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1005 >
1006 >        checkCompletedNormally(h, subtract(v1, v2));
1007 >        checkCompletedNormally(f, v1);
1008 >        checkCompletedNormally(g, v2);
1009 >        assertEquals(r.invocationCount, 1);
1010 >        }
1011 >    }
1012 >
1013 >    public void testThenCombine_normalCompletion4() {
1014 >        for (ExecutionMode m : ExecutionMode.values())
1015 >        for (Integer v1 : new Integer[] { 1, null })
1016 >        for (Integer v2 : new Integer[] { 2, null }) {
1017 >
1018 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1020 >        final SubtractFunction r = new SubtractFunction();
1021 >
1022 >        f.complete(v1);
1023 >        g.complete(v2);
1024 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1025 >
1026 >        checkCompletedNormally(h, subtract(v1, v2));
1027 >        checkCompletedNormally(f, v1);
1028 >        checkCompletedNormally(g, v2);
1029 >        assertEquals(r.invocationCount, 1);
1030 >        }
1031      }
1032  
1033      /**
1034       * thenCombine result completes exceptionally after exceptional
1035       * completion of either source
1036       */
1037 <    public void testThenCombine2() {
1038 <        CompletableFuture<Integer> f, g, h;
1037 >    public void testThenCombine_exceptionalCompletion1() {
1038 >        for (ExecutionMode m : ExecutionMode.values())
1039 >        for (Integer v1 : new Integer[] { 1, null }) {
1040 >
1041 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1042 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1043 >        final SubtractFunction r = new SubtractFunction();
1044 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1045 >        final CFException ex = new CFException();
1046  
1047 <        f = new CompletableFuture<Integer>();
673 <        g = new CompletableFuture<Integer>();
674 <        h = f.thenCombine(g, subtract);
675 <        f.completeExceptionally(new CFException());
1047 >        f.completeExceptionally(ex);
1048          checkIncomplete(h);
1049 <        g.complete(1);
1050 <        checkCompletedWithWrappedCFException(h);
1049 >        g.complete(v1);
1050 >
1051 >        checkCompletedWithWrappedCFException(h, ex);
1052 >        checkCompletedWithWrappedCFException(f, ex);
1053 >        assertEquals(r.invocationCount, 0);
1054 >        checkCompletedNormally(g, v1);
1055 >        }
1056 >    }
1057 >
1058 >    public void testThenCombine_exceptionalCompletion2() {
1059 >        for (ExecutionMode m : ExecutionMode.values())
1060 >        for (Integer v1 : new Integer[] { 1, null }) {
1061 >
1062 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1063 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1064 >        final SubtractFunction r = new SubtractFunction();
1065 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1066 >        final CFException ex = new CFException();
1067  
1068 <        f = new CompletableFuture<Integer>();
681 <        g = new CompletableFuture<Integer>();
682 <        h = f.thenCombine(g, subtract);
683 <        g.completeExceptionally(new CFException());
1068 >        g.completeExceptionally(ex);
1069          checkIncomplete(h);
1070 <        f.complete(3);
686 <        checkCompletedWithWrappedCFException(h);
1070 >        f.complete(v1);
1071  
1072 <        f = new CompletableFuture<Integer>();
1073 <        g = new CompletableFuture<Integer>();
1074 <        f.complete(3);
1075 <        g.completeExceptionally(new CFException());
1076 <        h = f.thenCombine(g, subtract);
1077 <        checkCompletedWithWrappedCFException(h);
1072 >        checkCompletedWithWrappedCFException(h, ex);
1073 >        checkCompletedWithWrappedCFException(g, ex);
1074 >        assertEquals(r.invocationCount, 0);
1075 >        checkCompletedNormally(f, v1);
1076 >        }
1077 >    }
1078  
1079 <        f = new CompletableFuture<Integer>();
1080 <        g = new CompletableFuture<Integer>();
1081 <        f.completeExceptionally(new CFException());
1082 <        g.complete(3);
1083 <        h = f.thenCombine(g, subtract);
1084 <        checkCompletedWithWrappedCFException(h);
1079 >    public void testThenCombine_exceptionalCompletion3() {
1080 >        for (ExecutionMode m : ExecutionMode.values())
1081 >        for (Integer v1 : new Integer[] { 1, null }) {
1082 >
1083 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1085 >        final SubtractFunction r = new SubtractFunction();
1086 >        final CFException ex = new CFException();
1087 >
1088 >        g.completeExceptionally(ex);
1089 >        f.complete(v1);
1090 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1091 >
1092 >        checkCompletedWithWrappedCFException(h, ex);
1093 >        checkCompletedWithWrappedCFException(g, ex);
1094 >        assertEquals(r.invocationCount, 0);
1095 >        checkCompletedNormally(f, v1);
1096 >        }
1097 >    }
1098 >
1099 >    public void testThenCombine_exceptionalCompletion4() {
1100 >        for (ExecutionMode m : ExecutionMode.values())
1101 >        for (Integer v1 : new Integer[] { 1, null }) {
1102 >
1103 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1104 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1105 >        final SubtractFunction r = new SubtractFunction();
1106 >        final CFException ex = new CFException();
1107 >
1108 >        f.completeExceptionally(ex);
1109 >        g.complete(v1);
1110 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1111 >
1112 >        checkCompletedWithWrappedCFException(h, ex);
1113 >        checkCompletedWithWrappedCFException(f, ex);
1114 >        assertEquals(r.invocationCount, 0);
1115 >        checkCompletedNormally(g, v1);
1116 >        }
1117      }
1118  
1119      /**
1120       * thenCombine result completes exceptionally if action does
1121       */
1122 <    public void testThenCombine3() {
1123 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1124 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1125 <        FailingBiFunction r = new FailingBiFunction();
1126 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1127 <        f.complete(one);
1128 <        checkIncomplete(g);
1129 <        assertFalse(r.ran);
1130 <        f2.complete(two);
1131 <        checkCompletedWithWrappedCFException(g);
1132 <        assertTrue(r.ran);
1122 >    public void testThenCombine_actionFailed1() {
1123 >        for (ExecutionMode m : ExecutionMode.values())
1124 >        for (Integer v1 : new Integer[] { 1, null })
1125 >        for (Integer v2 : new Integer[] { 2, null }) {
1126 >
1127 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1128 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1129 >        final FailingBiFunction r = new FailingBiFunction();
1130 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1131 >
1132 >        f.complete(v1);
1133 >        checkIncomplete(h);
1134 >        g.complete(v2);
1135 >
1136 >        checkCompletedWithWrappedCFException(h);
1137 >        checkCompletedNormally(f, v1);
1138 >        checkCompletedNormally(g, v2);
1139 >        }
1140 >    }
1141 >
1142 >    public void testThenCombine_actionFailed2() {
1143 >        for (ExecutionMode m : ExecutionMode.values())
1144 >        for (Integer v1 : new Integer[] { 1, null })
1145 >        for (Integer v2 : new Integer[] { 2, null }) {
1146 >
1147 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1148 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1149 >        final FailingBiFunction r = new FailingBiFunction();
1150 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1151 >
1152 >        g.complete(v2);
1153 >        checkIncomplete(h);
1154 >        f.complete(v1);
1155 >
1156 >        checkCompletedWithWrappedCFException(h);
1157 >        checkCompletedNormally(f, v1);
1158 >        checkCompletedNormally(g, v2);
1159 >        }
1160      }
1161  
1162      /**
1163       * thenCombine result completes exceptionally if either source cancelled
1164       */
1165 <    public void testThenCombine4() {
1166 <        CompletableFuture<Integer> f, g, h;
1165 >    public void testThenCombine_sourceCancelled1() {
1166 >        for (ExecutionMode m : ExecutionMode.values())
1167 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1168 >        for (Integer v1 : new Integer[] { 1, null }) {
1169 >
1170 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1171 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1172 >        final SubtractFunction r = new SubtractFunction();
1173 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1174  
1175 <        f = new CompletableFuture<Integer>();
726 <        g = new CompletableFuture<Integer>();
727 <        h = f.thenCombine(g, subtract);
728 <        assertTrue(f.cancel(true));
1175 >        assertTrue(f.cancel(mayInterruptIfRunning));
1176          checkIncomplete(h);
1177 <        g.complete(1);
1177 >        g.complete(v1);
1178 >
1179          checkCompletedWithWrappedCancellationException(h);
1180 +        checkCancelled(f);
1181 +        assertEquals(r.invocationCount, 0);
1182 +        checkCompletedNormally(g, v1);
1183 +        }
1184 +    }
1185 +
1186 +    public void testThenCombine_sourceCancelled2() {
1187 +        for (ExecutionMode m : ExecutionMode.values())
1188 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1189 +        for (Integer v1 : new Integer[] { 1, null }) {
1190 +
1191 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1192 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1193 +        final SubtractFunction r = new SubtractFunction();
1194 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1195  
1196 <        f = new CompletableFuture<Integer>();
734 <        g = new CompletableFuture<Integer>();
735 <        h = f.thenCombine(g, subtract);
736 <        assertTrue(g.cancel(true));
1196 >        assertTrue(g.cancel(mayInterruptIfRunning));
1197          checkIncomplete(h);
1198 <        f.complete(3);
1198 >        f.complete(v1);
1199 >
1200          checkCompletedWithWrappedCancellationException(h);
1201 +        checkCancelled(g);
1202 +        assertEquals(r.invocationCount, 0);
1203 +        checkCompletedNormally(f, v1);
1204 +        }
1205 +    }
1206 +
1207 +    public void testThenCombine_sourceCancelled3() {
1208 +        for (ExecutionMode m : ExecutionMode.values())
1209 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1210 +        for (Integer v1 : new Integer[] { 1, null }) {
1211 +
1212 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1213 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1214 +        final SubtractFunction r = new SubtractFunction();
1215 +
1216 +        assertTrue(g.cancel(mayInterruptIfRunning));
1217 +        f.complete(v1);
1218 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1219  
741        f = new CompletableFuture<Integer>();
742        g = new CompletableFuture<Integer>();
743        assertTrue(f.cancel(true));
744        assertTrue(g.cancel(true));
745        h = f.thenCombine(g, subtract);
1220          checkCompletedWithWrappedCancellationException(h);
1221 +        checkCancelled(g);
1222 +        assertEquals(r.invocationCount, 0);
1223 +        checkCompletedNormally(f, v1);
1224 +        }
1225 +    }
1226 +
1227 +    public void testThenCombine_sourceCancelled4() {
1228 +        for (ExecutionMode m : ExecutionMode.values())
1229 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1230 +        for (Integer v1 : new Integer[] { 1, null }) {
1231 +
1232 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1233 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1234 +        final SubtractFunction r = new SubtractFunction();
1235 +
1236 +        assertTrue(f.cancel(mayInterruptIfRunning));
1237 +        g.complete(v1);
1238 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1239 +
1240 +        checkCompletedWithWrappedCancellationException(h);
1241 +        checkCancelled(f);
1242 +        assertEquals(r.invocationCount, 0);
1243 +        checkCompletedNormally(g, v1);
1244 +        }
1245      }
1246  
1247      /**
1248       * thenAcceptBoth result completes normally after normal
1249       * completion of sources
1250       */
1251 <    public void testThenAcceptBoth() {
1252 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1253 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1254 <        AddAction r = new AddAction();
1255 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1256 <        f.complete(one);
1257 <        checkIncomplete(g);
1258 <        f2.complete(two);
1259 <        checkCompletedNormally(g, null);
762 <        assertEquals(r.value, 3);
1251 >    public void testThenAcceptBoth_normalCompletion1() {
1252 >        for (ExecutionMode m : ExecutionMode.values())
1253 >        for (Integer v1 : new Integer[] { 1, null })
1254 >        for (Integer v2 : new Integer[] { 2, null }) {
1255 >
1256 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1257 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1258 >        final SubtractAction r = new SubtractAction();
1259 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1260  
1261 <        r = new AddAction();
1262 <        f = new CompletableFuture<Integer>();
1263 <        f.complete(one);
1264 <        f2 = new CompletableFuture<Integer>();
1265 <        g = f.thenAcceptBoth(f2, r);
1266 <        checkIncomplete(g);
1267 <        f2.complete(two);
1268 <        checkCompletedNormally(g, null);
1269 <        assertEquals(r.value, 3);
1261 >        f.complete(v1);
1262 >        checkIncomplete(h);
1263 >        assertFalse(r.ran());
1264 >        g.complete(v2);
1265 >
1266 >        checkCompletedNormally(h, null);
1267 >        assertEquals(r.value, subtract(v1, v2));
1268 >        checkCompletedNormally(f, v1);
1269 >        checkCompletedNormally(g, v2);
1270 >        }
1271 >    }
1272 >
1273 >    public void testThenAcceptBoth_normalCompletion2() {
1274 >        for (ExecutionMode m : ExecutionMode.values())
1275 >        for (Integer v1 : new Integer[] { 1, null })
1276 >        for (Integer v2 : new Integer[] { 2, null }) {
1277 >
1278 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1279 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1280 >        final SubtractAction r = new SubtractAction();
1281 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1282 >
1283 >        g.complete(v2);
1284 >        checkIncomplete(h);
1285 >        assertFalse(r.ran());
1286 >        f.complete(v1);
1287 >
1288 >        checkCompletedNormally(h, null);
1289 >        assertEquals(r.value, subtract(v1, v2));
1290 >        checkCompletedNormally(f, v1);
1291 >        checkCompletedNormally(g, v2);
1292 >        }
1293 >    }
1294 >
1295 >    public void testThenAcceptBoth_normalCompletion3() {
1296 >        for (ExecutionMode m : ExecutionMode.values())
1297 >        for (Integer v1 : new Integer[] { 1, null })
1298 >        for (Integer v2 : new Integer[] { 2, null }) {
1299 >
1300 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1301 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1302 >        final SubtractAction r = new SubtractAction();
1303 >
1304 >        g.complete(v2);
1305 >        f.complete(v1);
1306 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1307 >
1308 >        checkCompletedNormally(h, null);
1309 >        assertEquals(r.value, subtract(v1, v2));
1310 >        checkCompletedNormally(f, v1);
1311 >        checkCompletedNormally(g, v2);
1312 >        }
1313 >    }
1314 >
1315 >    public void testThenAcceptBoth_normalCompletion4() {
1316 >        for (ExecutionMode m : ExecutionMode.values())
1317 >        for (Integer v1 : new Integer[] { 1, null })
1318 >        for (Integer v2 : new Integer[] { 2, null }) {
1319 >
1320 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 >        final SubtractAction r = new SubtractAction();
1323 >
1324 >        f.complete(v1);
1325 >        g.complete(v2);
1326 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1327 >
1328 >        checkCompletedNormally(h, null);
1329 >        assertEquals(r.value, subtract(v1, v2));
1330 >        checkCompletedNormally(f, v1);
1331 >        checkCompletedNormally(g, v2);
1332 >        }
1333      }
1334  
1335      /**
1336       * thenAcceptBoth result completes exceptionally after exceptional
1337       * completion of either source
1338       */
1339 <    public void testThenAcceptBoth2() {
1340 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1341 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1342 <        AddAction r = new AddAction();
1343 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1344 <        f.completeExceptionally(new CFException());
1345 <        f2.complete(two);
1346 <        checkCompletedWithWrappedCFException(g);
1339 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1340 >        for (ExecutionMode m : ExecutionMode.values())
1341 >        for (Integer v1 : new Integer[] { 1, null }) {
1342 >
1343 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1344 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1345 >        final SubtractAction r = new SubtractAction();
1346 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1347 >        final CFException ex = new CFException();
1348  
1349 <        r = new AddAction();
1350 <        f = new CompletableFuture<Integer>();
1351 <        f.complete(one);
1352 <        f2 = new CompletableFuture<Integer>();
1353 <        g = f.thenAcceptBoth(f2, r);
1354 <        f2.completeExceptionally(new CFException());
1355 <        checkCompletedWithWrappedCFException(g);
1349 >        f.completeExceptionally(ex);
1350 >        checkIncomplete(h);
1351 >        g.complete(v1);
1352 >
1353 >        checkCompletedWithWrappedCFException(h, ex);
1354 >        checkCompletedWithWrappedCFException(f, ex);
1355 >        assertEquals(r.invocationCount, 0);
1356 >        checkCompletedNormally(g, v1);
1357 >        }
1358 >    }
1359 >
1360 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1361 >        for (ExecutionMode m : ExecutionMode.values())
1362 >        for (Integer v1 : new Integer[] { 1, null }) {
1363 >
1364 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1365 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1366 >        final SubtractAction r = new SubtractAction();
1367 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1368 >        final CFException ex = new CFException();
1369 >
1370 >        g.completeExceptionally(ex);
1371 >        checkIncomplete(h);
1372 >        f.complete(v1);
1373 >
1374 >        checkCompletedWithWrappedCFException(h, ex);
1375 >        checkCompletedWithWrappedCFException(g, ex);
1376 >        assertEquals(r.invocationCount, 0);
1377 >        checkCompletedNormally(f, v1);
1378 >        }
1379 >    }
1380 >
1381 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1382 >        for (ExecutionMode m : ExecutionMode.values())
1383 >        for (Integer v1 : new Integer[] { 1, null }) {
1384 >
1385 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1386 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1387 >        final SubtractAction r = new SubtractAction();
1388 >        final CFException ex = new CFException();
1389 >
1390 >        g.completeExceptionally(ex);
1391 >        f.complete(v1);
1392 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1393 >
1394 >        checkCompletedWithWrappedCFException(h, ex);
1395 >        checkCompletedWithWrappedCFException(g, ex);
1396 >        assertEquals(r.invocationCount, 0);
1397 >        checkCompletedNormally(f, v1);
1398 >        }
1399 >    }
1400 >
1401 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1402 >        for (ExecutionMode m : ExecutionMode.values())
1403 >        for (Integer v1 : new Integer[] { 1, null }) {
1404 >
1405 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1406 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1407 >        final SubtractAction r = new SubtractAction();
1408 >        final CFException ex = new CFException();
1409 >
1410 >        f.completeExceptionally(ex);
1411 >        g.complete(v1);
1412 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1413 >
1414 >        checkCompletedWithWrappedCFException(h, ex);
1415 >        checkCompletedWithWrappedCFException(f, ex);
1416 >        assertFalse(r.ran());
1417 >        checkCompletedNormally(g, v1);
1418 >        }
1419      }
1420  
1421      /**
1422       * thenAcceptBoth result completes exceptionally if action does
1423       */
1424 <    public void testThenAcceptBoth3() {
1425 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1426 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1427 <        FailingBiConsumer r = new FailingBiConsumer();
1428 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1429 <        f.complete(one);
1430 <        checkIncomplete(g);
1431 <        f2.complete(two);
1432 <        checkCompletedWithWrappedCFException(g);
1424 >    public void testThenAcceptBoth_actionFailed1() {
1425 >        for (ExecutionMode m : ExecutionMode.values())
1426 >        for (Integer v1 : new Integer[] { 1, null })
1427 >        for (Integer v2 : new Integer[] { 2, null }) {
1428 >
1429 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1430 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1431 >        final FailingBiConsumer r = new FailingBiConsumer();
1432 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1433 >
1434 >        f.complete(v1);
1435 >        checkIncomplete(h);
1436 >        g.complete(v2);
1437 >
1438 >        checkCompletedWithWrappedCFException(h);
1439 >        checkCompletedNormally(f, v1);
1440 >        checkCompletedNormally(g, v2);
1441 >        }
1442 >    }
1443 >
1444 >    public void testThenAcceptBoth_actionFailed2() {
1445 >        for (ExecutionMode m : ExecutionMode.values())
1446 >        for (Integer v1 : new Integer[] { 1, null })
1447 >        for (Integer v2 : new Integer[] { 2, null }) {
1448 >
1449 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1450 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1451 >        final FailingBiConsumer r = new FailingBiConsumer();
1452 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1453 >
1454 >        g.complete(v2);
1455 >        checkIncomplete(h);
1456 >        f.complete(v1);
1457 >
1458 >        checkCompletedWithWrappedCFException(h);
1459 >        checkCompletedNormally(f, v1);
1460 >        checkCompletedNormally(g, v2);
1461 >        }
1462      }
1463  
1464      /**
1465       * thenAcceptBoth result completes exceptionally if either source cancelled
1466       */
1467 <    public void testThenAcceptBoth4() {
1468 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1469 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1470 <        AddAction r = new AddAction();
1471 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1472 <        assertTrue(f.cancel(true));
1473 <        f2.complete(two);
1474 <        checkCompletedWithWrappedCancellationException(g);
1475 <        f = new CompletableFuture<Integer>();
1476 <        f2 = new CompletableFuture<Integer>();
1477 <        r = new AddAction();
1478 <        g = f.thenAcceptBoth(f2, r);
1479 <        f.complete(one);
1480 <        assertTrue(f2.cancel(true));
1481 <        checkCompletedWithWrappedCancellationException(g);
1467 >    public void testThenAcceptBoth_sourceCancelled1() {
1468 >        for (ExecutionMode m : ExecutionMode.values())
1469 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1470 >        for (Integer v1 : new Integer[] { 1, null }) {
1471 >
1472 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1473 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1474 >        final SubtractAction r = new SubtractAction();
1475 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1476 >
1477 >        assertTrue(f.cancel(mayInterruptIfRunning));
1478 >        checkIncomplete(h);
1479 >        g.complete(v1);
1480 >
1481 >        checkCompletedWithWrappedCancellationException(h);
1482 >        checkCancelled(f);
1483 >        assertEquals(r.invocationCount, 0);
1484 >        checkCompletedNormally(g, v1);
1485 >        }
1486 >    }
1487 >
1488 >    public void testThenAcceptBoth_sourceCancelled2() {
1489 >        for (ExecutionMode m : ExecutionMode.values())
1490 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1491 >        for (Integer v1 : new Integer[] { 1, null }) {
1492 >
1493 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1494 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1495 >        final SubtractAction r = new SubtractAction();
1496 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1497 >
1498 >        assertTrue(g.cancel(mayInterruptIfRunning));
1499 >        checkIncomplete(h);
1500 >        f.complete(v1);
1501 >
1502 >        checkCompletedWithWrappedCancellationException(h);
1503 >        checkCancelled(g);
1504 >        assertEquals(r.invocationCount, 0);
1505 >        checkCompletedNormally(f, v1);
1506 >        }
1507 >    }
1508 >
1509 >    public void testThenAcceptBoth_sourceCancelled3() {
1510 >        for (ExecutionMode m : ExecutionMode.values())
1511 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1512 >        for (Integer v1 : new Integer[] { 1, null }) {
1513 >
1514 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1515 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1516 >        final SubtractAction r = new SubtractAction();
1517 >
1518 >        assertTrue(g.cancel(mayInterruptIfRunning));
1519 >        f.complete(v1);
1520 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1521 >
1522 >        checkCompletedWithWrappedCancellationException(h);
1523 >        checkCancelled(g);
1524 >        assertEquals(r.invocationCount, 0);
1525 >        checkCompletedNormally(f, v1);
1526 >        }
1527 >    }
1528 >
1529 >    public void testThenAcceptBoth_sourceCancelled4() {
1530 >        for (ExecutionMode m : ExecutionMode.values())
1531 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1532 >        for (Integer v1 : new Integer[] { 1, null }) {
1533 >
1534 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1535 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1536 >        final SubtractAction r = new SubtractAction();
1537 >
1538 >        assertTrue(f.cancel(mayInterruptIfRunning));
1539 >        g.complete(v1);
1540 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1541 >
1542 >        checkCompletedWithWrappedCancellationException(h);
1543 >        checkCancelled(f);
1544 >        assertEquals(r.invocationCount, 0);
1545 >        checkCompletedNormally(g, v1);
1546 >        }
1547      }
1548  
1549      /**
1550       * runAfterBoth result completes normally after normal
1551       * completion of sources
1552       */
1553 <    public void testRunAfterBoth() {
1554 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1555 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1556 <        Noop r = new Noop();
1557 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1558 <        f.complete(one);
1559 <        checkIncomplete(g);
1560 <        f2.complete(two);
1561 <        checkCompletedNormally(g, null);
844 <        assertTrue(r.ran);
1553 >    public void testRunAfterBoth_normalCompletion1() {
1554 >        for (ExecutionMode m : ExecutionMode.values())
1555 >        for (Integer v1 : new Integer[] { 1, null })
1556 >        for (Integer v2 : new Integer[] { 2, null }) {
1557 >
1558 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1559 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1560 >        final Noop r = new Noop();
1561 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1562  
1563 <        r = new Noop();
1564 <        f = new CompletableFuture<Integer>();
1565 <        f.complete(one);
1566 <        f2 = new CompletableFuture<Integer>();
1567 <        g = f.runAfterBoth(f2, r);
1568 <        checkIncomplete(g);
1569 <        f2.complete(two);
1570 <        checkCompletedNormally(g, null);
1563 >        f.complete(v1);
1564 >        checkIncomplete(h);
1565 >        assertFalse(r.ran);
1566 >        g.complete(v2);
1567 >
1568 >        checkCompletedNormally(h, null);
1569 >        assertEquals(r.invocationCount, 1);
1570 >        checkCompletedNormally(f, v1);
1571 >        checkCompletedNormally(g, v2);
1572 >        }
1573 >    }
1574 >
1575 >    public void testRunAfterBoth_normalCompletion2() {
1576 >        for (ExecutionMode m : ExecutionMode.values())
1577 >        for (Integer v1 : new Integer[] { 1, null })
1578 >        for (Integer v2 : new Integer[] { 2, null }) {
1579 >
1580 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1581 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1582 >        final Noop r = new Noop();
1583 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1584 >
1585 >        g.complete(v2);
1586 >        checkIncomplete(h);
1587 >        assertFalse(r.ran);
1588 >        f.complete(v1);
1589 >
1590 >        checkCompletedNormally(h, null);
1591 >        assertEquals(r.invocationCount, 1);
1592 >        checkCompletedNormally(f, v1);
1593 >        checkCompletedNormally(g, v2);
1594 >        }
1595 >    }
1596 >
1597 >    public void testRunAfterBoth_normalCompletion3() {
1598 >        for (ExecutionMode m : ExecutionMode.values())
1599 >        for (Integer v1 : new Integer[] { 1, null })
1600 >        for (Integer v2 : new Integer[] { 2, null }) {
1601 >
1602 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1603 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1604 >        final Noop r = new Noop();
1605 >
1606 >        g.complete(v2);
1607 >        f.complete(v1);
1608 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1609 >
1610 >        checkCompletedNormally(h, null);
1611          assertTrue(r.ran);
1612 +        checkCompletedNormally(f, v1);
1613 +        checkCompletedNormally(g, v2);
1614 +        }
1615 +    }
1616 +
1617 +    public void testRunAfterBoth_normalCompletion4() {
1618 +        for (ExecutionMode m : ExecutionMode.values())
1619 +        for (Integer v1 : new Integer[] { 1, null })
1620 +        for (Integer v2 : new Integer[] { 2, null }) {
1621 +
1622 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1624 +        final Noop r = new Noop();
1625 +
1626 +        f.complete(v1);
1627 +        g.complete(v2);
1628 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1629 +
1630 +        checkCompletedNormally(h, null);
1631 +        assertEquals(r.invocationCount, 1);
1632 +        checkCompletedNormally(f, v1);
1633 +        checkCompletedNormally(g, v2);
1634 +        }
1635      }
1636  
1637      /**
1638       * runAfterBoth result completes exceptionally after exceptional
1639       * completion of either source
1640       */
1641 <    public void testRunAfterBoth2() {
1642 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1643 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1644 <        Noop r = new Noop();
1645 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1646 <        f.completeExceptionally(new CFException());
1647 <        f2.complete(two);
1648 <        checkCompletedWithWrappedCFException(g);
1641 >    public void testRunAfterBoth_exceptionalCompletion1() {
1642 >        for (ExecutionMode m : ExecutionMode.values())
1643 >        for (Integer v1 : new Integer[] { 1, null }) {
1644 >
1645 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1646 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1647 >        final Noop r = new Noop();
1648 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1649 >        final CFException ex = new CFException();
1650  
1651 <        r = new Noop();
1652 <        f = new CompletableFuture<Integer>();
1653 <        f.complete(one);
1654 <        f2 = new CompletableFuture<Integer>();
1655 <        g = f.runAfterBoth(f2, r);
1656 <        f2.completeExceptionally(new CFException());
1657 <        checkCompletedWithWrappedCFException(g);
1651 >        f.completeExceptionally(ex);
1652 >        checkIncomplete(h);
1653 >        g.complete(v1);
1654 >
1655 >        checkCompletedWithWrappedCFException(h, ex);
1656 >        checkCompletedWithWrappedCFException(f, ex);
1657 >        assertEquals(r.invocationCount, 0);
1658 >        checkCompletedNormally(g, v1);
1659 >        }
1660 >    }
1661 >
1662 >    public void testRunAfterBoth_exceptionalCompletion2() {
1663 >        for (ExecutionMode m : ExecutionMode.values())
1664 >        for (Integer v1 : new Integer[] { 1, null }) {
1665 >
1666 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 >        final Noop r = new Noop();
1669 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1670 >        final CFException ex = new CFException();
1671 >
1672 >        g.completeExceptionally(ex);
1673 >        checkIncomplete(h);
1674 >        f.complete(v1);
1675 >
1676 >        checkCompletedWithWrappedCFException(h, ex);
1677 >        checkCompletedWithWrappedCFException(g, ex);
1678 >        assertEquals(r.invocationCount, 0);
1679 >        checkCompletedNormally(f, v1);
1680 >        }
1681 >    }
1682 >
1683 >    public void testRunAfterBoth_exceptionalCompletion3() {
1684 >        for (ExecutionMode m : ExecutionMode.values())
1685 >        for (Integer v1 : new Integer[] { 1, null }) {
1686 >
1687 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1688 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1689 >        final Noop r = new Noop();
1690 >        final CFException ex = new CFException();
1691 >
1692 >        g.completeExceptionally(ex);
1693 >        f.complete(v1);
1694 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1695 >
1696 >        checkCompletedWithWrappedCFException(h, ex);
1697 >        checkCompletedWithWrappedCFException(g, ex);
1698 >        assertEquals(r.invocationCount, 0);
1699 >        checkCompletedNormally(f, v1);
1700 >        }
1701 >    }
1702 >
1703 >    public void testRunAfterBoth_exceptionalCompletion4() {
1704 >        for (ExecutionMode m : ExecutionMode.values())
1705 >        for (Integer v1 : new Integer[] { 1, null }) {
1706 >
1707 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1708 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1709 >        final Noop r = new Noop();
1710 >        final CFException ex = new CFException();
1711 >
1712 >        f.completeExceptionally(ex);
1713 >        g.complete(v1);
1714 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715 >
1716 >        checkCompletedWithWrappedCFException(h, ex);
1717 >        checkCompletedWithWrappedCFException(f, ex);
1718 >        assertEquals(r.invocationCount, 0);
1719 >        checkCompletedNormally(g, v1);
1720 >        }
1721      }
1722  
1723      /**
1724       * runAfterBoth result completes exceptionally if action does
1725       */
1726 <    public void testRunAfterBoth3() {
1727 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1728 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1729 <        FailingNoop r = new FailingNoop();
1730 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1731 <        f.complete(one);
1732 <        checkIncomplete(g);
1733 <        f2.complete(two);
1734 <        checkCompletedWithWrappedCFException(g);
1726 >    public void testRunAfterBoth_actionFailed1() {
1727 >        for (ExecutionMode m : ExecutionMode.values())
1728 >        for (Integer v1 : new Integer[] { 1, null })
1729 >        for (Integer v2 : new Integer[] { 2, null }) {
1730 >
1731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1733 >        final FailingNoop r = new FailingNoop();
1734 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1735 >
1736 >        f.complete(v1);
1737 >        checkIncomplete(h);
1738 >        g.complete(v2);
1739 >
1740 >        checkCompletedWithWrappedCFException(h);
1741 >        checkCompletedNormally(f, v1);
1742 >        checkCompletedNormally(g, v2);
1743 >        }
1744 >    }
1745 >
1746 >    public void testRunAfterBoth_actionFailed2() {
1747 >        for (ExecutionMode m : ExecutionMode.values())
1748 >        for (Integer v1 : new Integer[] { 1, null })
1749 >        for (Integer v2 : new Integer[] { 2, null }) {
1750 >
1751 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1752 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1753 >        final FailingNoop r = new FailingNoop();
1754 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1755 >
1756 >        g.complete(v2);
1757 >        checkIncomplete(h);
1758 >        f.complete(v1);
1759 >
1760 >        checkCompletedWithWrappedCFException(h);
1761 >        checkCompletedNormally(f, v1);
1762 >        checkCompletedNormally(g, v2);
1763 >        }
1764      }
1765  
1766      /**
1767       * runAfterBoth result completes exceptionally if either source cancelled
1768       */
1769 <    public void testRunAfterBoth4() {
1770 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1771 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1772 <        Noop r = new Noop();
1773 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1774 <        assertTrue(f.cancel(true));
1775 <        f2.complete(two);
1776 <        checkCompletedWithWrappedCancellationException(g);
1777 <        f = new CompletableFuture<Integer>();
1778 <        f2 = new CompletableFuture<Integer>();
1779 <        r = new Noop();
1780 <        g = f.runAfterBoth(f2, r);
1781 <        f.complete(one);
1782 <        assertTrue(f2.cancel(true));
1783 <        checkCompletedWithWrappedCancellationException(g);
1769 >    public void testRunAfterBoth_sourceCancelled1() {
1770 >        for (ExecutionMode m : ExecutionMode.values())
1771 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1772 >        for (Integer v1 : new Integer[] { 1, null }) {
1773 >
1774 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1775 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1776 >        final Noop r = new Noop();
1777 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1778 >
1779 >        assertTrue(f.cancel(mayInterruptIfRunning));
1780 >        checkIncomplete(h);
1781 >        g.complete(v1);
1782 >
1783 >        checkCompletedWithWrappedCancellationException(h);
1784 >        checkCancelled(f);
1785 >        assertEquals(r.invocationCount, 0);
1786 >        checkCompletedNormally(g, v1);
1787 >        }
1788 >    }
1789 >
1790 >    public void testRunAfterBoth_sourceCancelled2() {
1791 >        for (ExecutionMode m : ExecutionMode.values())
1792 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1793 >        for (Integer v1 : new Integer[] { 1, null }) {
1794 >
1795 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1796 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1797 >        final Noop r = new Noop();
1798 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1799 >
1800 >        assertTrue(g.cancel(mayInterruptIfRunning));
1801 >        checkIncomplete(h);
1802 >        f.complete(v1);
1803 >
1804 >        checkCompletedWithWrappedCancellationException(h);
1805 >        checkCancelled(g);
1806 >        assertEquals(r.invocationCount, 0);
1807 >        checkCompletedNormally(f, v1);
1808 >        }
1809 >    }
1810 >
1811 >    public void testRunAfterBoth_sourceCancelled3() {
1812 >        for (ExecutionMode m : ExecutionMode.values())
1813 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1814 >        for (Integer v1 : new Integer[] { 1, null }) {
1815 >
1816 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1817 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 >        final Noop r = new Noop();
1819 >
1820 >        assertTrue(g.cancel(mayInterruptIfRunning));
1821 >        f.complete(v1);
1822 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1823 >
1824 >        checkCompletedWithWrappedCancellationException(h);
1825 >        checkCancelled(g);
1826 >        assertEquals(r.invocationCount, 0);
1827 >        checkCompletedNormally(f, v1);
1828 >        }
1829 >    }
1830 >
1831 >    public void testRunAfterBoth_sourceCancelled4() {
1832 >        for (ExecutionMode m : ExecutionMode.values())
1833 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1834 >        for (Integer v1 : new Integer[] { 1, null }) {
1835 >
1836 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1837 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1838 >        final Noop r = new Noop();
1839 >
1840 >        assertTrue(f.cancel(mayInterruptIfRunning));
1841 >        g.complete(v1);
1842 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843 >
1844 >        checkCompletedWithWrappedCancellationException(h);
1845 >        checkCancelled(f);
1846 >        assertEquals(r.invocationCount, 0);
1847 >        checkCompletedNormally(g, v1);
1848 >        }
1849      }
1850  
1851      /**
1852       * applyToEither result completes normally after normal completion
1853       * of either source
1854       */
1855 <    public void testApplyToEither() {
1856 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1857 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1858 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1859 <        f.complete(one);
1860 <        checkCompletedNormally(g, two);
1861 <        f2.complete(one);
1862 <        checkCompletedNormally(g, two);
1855 >    public void testApplyToEither_normalCompletion1() {
1856 >        for (ExecutionMode m : ExecutionMode.values())
1857 >        for (Integer v1 : new Integer[] { 1, null })
1858 >        for (Integer v2 : new Integer[] { 2, null }) {
1859 >
1860 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1861 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1862 >        final IncFunction r = new IncFunction();
1863 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1864 >
1865 >        f.complete(v1);
1866 >        checkCompletedNormally(h, inc(v1));
1867 >        g.complete(v2);
1868 >
1869 >        checkCompletedNormally(f, v1);
1870 >        checkCompletedNormally(g, v2);
1871 >        checkCompletedNormally(h, inc(v1));
1872 >        }
1873 >    }
1874  
1875 <        f = new CompletableFuture<Integer>();
1876 <        f.complete(one);
1877 <        f2 = new CompletableFuture<Integer>();
1878 <        g = f.applyToEither(f2, inc);
1879 <        checkCompletedNormally(g, two);
1875 >    public void testApplyToEither_normalCompletion2() {
1876 >        for (ExecutionMode m : ExecutionMode.values())
1877 >        for (Integer v1 : new Integer[] { 1, null })
1878 >        for (Integer v2 : new Integer[] { 2, null }) {
1879 >
1880 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1881 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1882 >        final IncFunction r = new IncFunction();
1883 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1884 >
1885 >        g.complete(v2);
1886 >        checkCompletedNormally(h, inc(v2));
1887 >        f.complete(v1);
1888 >
1889 >        checkCompletedNormally(f, v1);
1890 >        checkCompletedNormally(g, v2);
1891 >        checkCompletedNormally(h, inc(v2));
1892 >        }
1893 >    }
1894 >    public void testApplyToEither_normalCompletion3() {
1895 >        for (ExecutionMode m : ExecutionMode.values())
1896 >        for (Integer v1 : new Integer[] { 1, null })
1897 >        for (Integer v2 : new Integer[] { 2, null }) {
1898 >
1899 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1900 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1901 >        final IncFunction r = new IncFunction();
1902 >
1903 >        f.complete(v1);
1904 >        g.complete(v2);
1905 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1906 >
1907 >        checkCompletedNormally(f, v1);
1908 >        checkCompletedNormally(g, v2);
1909 >
1910 >        // unspecified behavior
1911 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1912 >                   Objects.equals(h.join(), inc(v2)));
1913 >        assertEquals(r.invocationCount, 1);
1914 >        }
1915      }
1916  
1917      /**
1918       * applyToEither result completes exceptionally after exceptional
1919       * completion of either source
1920       */
1921 <    public void testApplyToEither2() {
1922 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1923 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1924 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1925 <        f.completeExceptionally(new CFException());
1926 <        f2.complete(one);
1927 <        checkCompletedWithWrappedCFException(g);
1921 >    public void testApplyToEither_exceptionalCompletion1() {
1922 >        for (ExecutionMode m : ExecutionMode.values())
1923 >        for (Integer v1 : new Integer[] { 1, null }) {
1924 >
1925 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1926 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1927 >        final IncFunction r = new IncFunction();
1928 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1929 >        final CFException ex = new CFException();
1930 >
1931 >        f.completeExceptionally(ex);
1932 >        checkCompletedWithWrappedCFException(h, ex);
1933 >        g.complete(v1);
1934 >
1935 >        assertEquals(r.invocationCount, 0);
1936 >        checkCompletedNormally(g, v1);
1937 >        checkCompletedWithWrappedCFException(f, ex);
1938 >        checkCompletedWithWrappedCFException(h, ex);
1939 >        }
1940 >    }
1941  
1942 <        f = new CompletableFuture<Integer>();
1943 <        f2 = new CompletableFuture<Integer>();
1944 <        f2.completeExceptionally(new CFException());
1945 <        g = f.applyToEither(f2, inc);
1946 <        checkCompletedWithWrappedCFException(g);
1942 >    public void testApplyToEither_exceptionalCompletion2() {
1943 >        for (ExecutionMode m : ExecutionMode.values())
1944 >        for (Integer v1 : new Integer[] { 1, null }) {
1945 >
1946 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1947 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1948 >        final IncFunction r = new IncFunction();
1949 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1950 >        final CFException ex = new CFException();
1951 >
1952 >        g.completeExceptionally(ex);
1953 >        checkCompletedWithWrappedCFException(h, ex);
1954 >        f.complete(v1);
1955 >
1956 >        assertEquals(r.invocationCount, 0);
1957 >        checkCompletedNormally(f, v1);
1958 >        checkCompletedWithWrappedCFException(g, ex);
1959 >        checkCompletedWithWrappedCFException(h, ex);
1960 >        }
1961 >    }
1962 >
1963 >    public void testApplyToEither_exceptionalCompletion3() {
1964 >        for (ExecutionMode m : ExecutionMode.values())
1965 >        for (Integer v1 : new Integer[] { 1, null }) {
1966 >
1967 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1968 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1969 >        final IncFunction r = new IncFunction();
1970 >        final CFException ex = new CFException();
1971 >
1972 >        g.completeExceptionally(ex);
1973 >        f.complete(v1);
1974 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1975 >
1976 >        // unspecified behavior
1977 >        Integer v;
1978 >        try {
1979 >            assertEquals(h.join(), inc(v1));
1980 >            assertEquals(r.invocationCount, 1);
1981 >        } catch (CompletionException ok) {
1982 >            checkCompletedWithWrappedCFException(h, ex);
1983 >            assertEquals(r.invocationCount, 0);
1984 >        }
1985 >
1986 >        checkCompletedWithWrappedCFException(g, ex);
1987 >        checkCompletedNormally(f, v1);
1988 >        }
1989 >    }
1990 >
1991 >    public void testApplyToEither_exceptionalCompletion4() {
1992 >        for (ExecutionMode m : ExecutionMode.values())
1993 >        for (Integer v1 : new Integer[] { 1, null }) {
1994 >
1995 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1996 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1997 >        final IncFunction r = new IncFunction();
1998 >        final CFException ex = new CFException();
1999 >
2000 >        f.completeExceptionally(ex);
2001 >        g.complete(v1);
2002 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2003 >
2004 >        // unspecified behavior
2005 >        Integer v;
2006 >        try {
2007 >            assertEquals(h.join(), inc(v1));
2008 >            assertEquals(r.invocationCount, 1);
2009 >        } catch (CompletionException ok) {
2010 >            checkCompletedWithWrappedCFException(h, ex);
2011 >            assertEquals(r.invocationCount, 0);
2012 >        }
2013 >
2014 >        checkCompletedWithWrappedCFException(f, ex);
2015 >        checkCompletedNormally(g, v1);
2016 >        }
2017      }
2018  
2019      /**
2020       * applyToEither result completes exceptionally if action does
2021       */
2022 <    public void testApplyToEither3() {
2023 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2024 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2025 <        FailingFunction r = new FailingFunction();
2026 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2027 <        f2.complete(two);
2028 <        checkCompletedWithWrappedCFException(g);
2022 >    public void testApplyToEither_actionFailed1() {
2023 >        for (ExecutionMode m : ExecutionMode.values())
2024 >        for (Integer v1 : new Integer[] { 1, null })
2025 >        for (Integer v2 : new Integer[] { 2, null }) {
2026 >
2027 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2028 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2029 >        final FailingFunction r = new FailingFunction();
2030 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2031 >
2032 >        f.complete(v1);
2033 >        checkCompletedWithWrappedCFException(h);
2034 >        g.complete(v2);
2035 >        checkCompletedNormally(f, v1);
2036 >        checkCompletedNormally(g, v2);
2037 >        }
2038 >    }
2039 >
2040 >    public void testApplyToEither_actionFailed2() {
2041 >        for (ExecutionMode m : ExecutionMode.values())
2042 >        for (Integer v1 : new Integer[] { 1, null })
2043 >        for (Integer v2 : new Integer[] { 2, null }) {
2044 >
2045 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2046 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2047 >        final FailingFunction r = new FailingFunction();
2048 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2049 >
2050 >        g.complete(v2);
2051 >        checkCompletedWithWrappedCFException(h);
2052 >        f.complete(v1);
2053 >        checkCompletedNormally(f, v1);
2054 >        checkCompletedNormally(g, v2);
2055 >        }
2056      }
2057  
2058      /**
2059       * applyToEither result completes exceptionally if either source cancelled
2060       */
2061 <    public void testApplyToEither4() {
2062 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2063 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2064 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2065 <        assertTrue(f.cancel(true));
2066 <        checkCompletedWithWrappedCancellationException(g);
2067 <        f = new CompletableFuture<Integer>();
2068 <        f2 = new CompletableFuture<Integer>();
2069 <        assertTrue(f2.cancel(true));
2070 <        checkCompletedWithWrappedCancellationException(g);
2061 >    public void testApplyToEither_sourceCancelled1() {
2062 >        for (ExecutionMode m : ExecutionMode.values())
2063 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2064 >        for (Integer v1 : new Integer[] { 1, null }) {
2065 >
2066 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2067 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2068 >        final IncFunction r = new IncFunction();
2069 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2070 >
2071 >        assertTrue(f.cancel(mayInterruptIfRunning));
2072 >        checkCompletedWithWrappedCancellationException(h);
2073 >        g.complete(v1);
2074 >
2075 >        checkCancelled(f);
2076 >        assertEquals(r.invocationCount, 0);
2077 >        checkCompletedNormally(g, v1);
2078 >        checkCompletedWithWrappedCancellationException(h);
2079 >        }
2080 >    }
2081 >
2082 >    public void testApplyToEither_sourceCancelled2() {
2083 >        for (ExecutionMode m : ExecutionMode.values())
2084 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2085 >        for (Integer v1 : new Integer[] { 1, null }) {
2086 >
2087 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2088 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2089 >        final IncFunction r = new IncFunction();
2090 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 >
2092 >        assertTrue(g.cancel(mayInterruptIfRunning));
2093 >        checkCompletedWithWrappedCancellationException(h);
2094 >        f.complete(v1);
2095 >
2096 >        checkCancelled(g);
2097 >        assertEquals(r.invocationCount, 0);
2098 >        checkCompletedNormally(f, v1);
2099 >        checkCompletedWithWrappedCancellationException(h);
2100 >        }
2101 >    }
2102 >
2103 >    public void testApplyToEither_sourceCancelled3() {
2104 >        for (ExecutionMode m : ExecutionMode.values())
2105 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2106 >        for (Integer v1 : new Integer[] { 1, null }) {
2107 >
2108 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2109 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2110 >        final IncFunction r = new IncFunction();
2111 >
2112 >        assertTrue(g.cancel(mayInterruptIfRunning));
2113 >        f.complete(v1);
2114 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2115 >
2116 >        // unspecified behavior
2117 >        Integer v;
2118 >        try {
2119 >            assertEquals(h.join(), inc(v1));
2120 >            assertEquals(r.invocationCount, 1);
2121 >        } catch (CompletionException ok) {
2122 >            checkCompletedWithWrappedCancellationException(h);
2123 >            assertEquals(r.invocationCount, 0);
2124 >        }
2125 >
2126 >        checkCancelled(g);
2127 >        checkCompletedNormally(f, v1);
2128 >        }
2129 >    }
2130 >
2131 >    public void testApplyToEither_sourceCancelled4() {
2132 >        for (ExecutionMode m : ExecutionMode.values())
2133 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2134 >        for (Integer v1 : new Integer[] { 1, null }) {
2135 >
2136 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2137 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2138 >        final IncFunction r = new IncFunction();
2139 >
2140 >        assertTrue(f.cancel(mayInterruptIfRunning));
2141 >        g.complete(v1);
2142 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2143 >
2144 >        // unspecified behavior
2145 >        Integer v;
2146 >        try {
2147 >            assertEquals(h.join(), inc(v1));
2148 >            assertEquals(r.invocationCount, 1);
2149 >        } catch (CompletionException ok) {
2150 >            checkCompletedWithWrappedCancellationException(h);
2151 >            assertEquals(r.invocationCount, 0);
2152 >        }
2153 >
2154 >        checkCancelled(f);
2155 >        checkCompletedNormally(g, v1);
2156 >        }
2157      }
2158  
2159      /**
2160       * acceptEither result completes normally after normal completion
2161       * of either source
2162       */
2163 <    public void testAcceptEither() {
2164 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2165 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2166 <        IncAction r = new IncAction();
2167 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2168 <        f.complete(one);
2169 <        checkCompletedNormally(g, null);
2170 <        f2.complete(one);
2171 <        checkCompletedNormally(g, null);
2172 <        assertEquals(r.value, 2);
2163 >    public void testAcceptEither_normalCompletion1() {
2164 >        for (ExecutionMode m : ExecutionMode.values())
2165 >        for (Integer v1 : new Integer[] { 1, null })
2166 >        for (Integer v2 : new Integer[] { 2, null }) {
2167 >
2168 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2169 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2170 >        final IncAction r = new IncAction();
2171 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2172 >
2173 >        f.complete(v1);
2174 >        checkCompletedNormally(h, null);
2175 >        assertEquals(r.value, inc(v1));
2176 >        g.complete(v2);
2177 >
2178 >        checkCompletedNormally(f, v1);
2179 >        checkCompletedNormally(g, v2);
2180 >        checkCompletedNormally(h, null);
2181 >        }
2182 >    }
2183  
2184 <        r = new IncAction();
2185 <        f = new CompletableFuture<Integer>();
2186 <        f.complete(one);
2187 <        f2 = new CompletableFuture<Integer>();
2188 <        g = f.acceptEither(f2, r);
2189 <        checkCompletedNormally(g, null);
2190 <        assertEquals(r.value, 2);
2184 >    public void testAcceptEither_normalCompletion2() {
2185 >        for (ExecutionMode m : ExecutionMode.values())
2186 >        for (Integer v1 : new Integer[] { 1, null })
2187 >        for (Integer v2 : new Integer[] { 2, null }) {
2188 >
2189 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2190 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2191 >        final IncAction r = new IncAction();
2192 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2193 >
2194 >        g.complete(v2);
2195 >        checkCompletedNormally(h, null);
2196 >        assertEquals(r.value, inc(v2));
2197 >        f.complete(v1);
2198 >
2199 >        checkCompletedNormally(f, v1);
2200 >        checkCompletedNormally(g, v2);
2201 >        checkCompletedNormally(h, null);
2202 >        }
2203 >    }
2204 >    public void testAcceptEither_normalCompletion3() {
2205 >        for (ExecutionMode m : ExecutionMode.values())
2206 >        for (Integer v1 : new Integer[] { 1, null })
2207 >        for (Integer v2 : new Integer[] { 2, null }) {
2208 >
2209 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2210 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2211 >        final IncAction r = new IncAction();
2212 >
2213 >        f.complete(v1);
2214 >        g.complete(v2);
2215 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2216 >
2217 >        checkCompletedNormally(h, null);
2218 >        checkCompletedNormally(f, v1);
2219 >        checkCompletedNormally(g, v2);
2220 >
2221 >        // unspecified behavior
2222 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2223 >                   Objects.equals(r.value, inc(v2)));
2224 >        }
2225      }
2226  
2227      /**
2228       * acceptEither result completes exceptionally after exceptional
2229       * completion of either source
2230       */
2231 <    public void testAcceptEither2() {
2232 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2233 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2234 <        IncAction r = new IncAction();
2235 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2236 <        f.completeExceptionally(new CFException());
2237 <        f2.complete(one);
2238 <        checkCompletedWithWrappedCFException(g);
2231 >    public void testAcceptEither_exceptionalCompletion1() {
2232 >        for (ExecutionMode m : ExecutionMode.values())
2233 >        for (Integer v1 : new Integer[] { 1, null }) {
2234 >
2235 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2236 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2237 >        final IncAction r = new IncAction();
2238 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2239 >        final CFException ex = new CFException();
2240 >
2241 >        f.completeExceptionally(ex);
2242 >        checkCompletedWithWrappedCFException(h, ex);
2243 >        g.complete(v1);
2244 >
2245 >        assertEquals(r.invocationCount, 0);
2246 >        checkCompletedNormally(g, v1);
2247 >        checkCompletedWithWrappedCFException(f, ex);
2248 >        checkCompletedWithWrappedCFException(h, ex);
2249 >        }
2250 >    }
2251  
2252 <        r = new IncAction();
2253 <        f = new CompletableFuture<Integer>();
2254 <        f2 = new CompletableFuture<Integer>();
2255 <        f2.completeExceptionally(new CFException());
2256 <        g = f.acceptEither(f2, r);
2257 <        checkCompletedWithWrappedCFException(g);
2252 >    public void testAcceptEither_exceptionalCompletion2() {
2253 >        for (ExecutionMode m : ExecutionMode.values())
2254 >        for (Integer v1 : new Integer[] { 1, null }) {
2255 >
2256 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2257 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2258 >        final IncAction r = new IncAction();
2259 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2260 >        final CFException ex = new CFException();
2261 >
2262 >        g.completeExceptionally(ex);
2263 >        checkCompletedWithWrappedCFException(h, ex);
2264 >        f.complete(v1);
2265 >
2266 >        assertEquals(r.invocationCount, 0);
2267 >        checkCompletedNormally(f, v1);
2268 >        checkCompletedWithWrappedCFException(g, ex);
2269 >        checkCompletedWithWrappedCFException(h, ex);
2270 >        }
2271 >    }
2272 >
2273 >    public void testAcceptEither_exceptionalCompletion3() {
2274 >        for (ExecutionMode m : ExecutionMode.values())
2275 >        for (Integer v1 : new Integer[] { 1, null }) {
2276 >
2277 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2278 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2279 >        final IncAction r = new IncAction();
2280 >        final CFException ex = new CFException();
2281 >
2282 >        g.completeExceptionally(ex);
2283 >        f.complete(v1);
2284 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2285 >
2286 >        // unspecified behavior
2287 >        Integer v;
2288 >        try {
2289 >            assertEquals(h.join(), null);
2290 >            assertEquals(r.invocationCount, 1);
2291 >            assertEquals(inc(v1), r.value);
2292 >        } catch (CompletionException ok) {
2293 >            checkCompletedWithWrappedCFException(h, ex);
2294 >            assertEquals(r.invocationCount, 0);
2295 >        }
2296 >
2297 >        checkCompletedWithWrappedCFException(g, ex);
2298 >        checkCompletedNormally(f, v1);
2299 >        }
2300 >    }
2301 >
2302 >    public void testAcceptEither_exceptionalCompletion4() {
2303 >        for (ExecutionMode m : ExecutionMode.values())
2304 >        for (Integer v1 : new Integer[] { 1, null }) {
2305 >
2306 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2307 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2308 >        final IncAction r = new IncAction();
2309 >        final CFException ex = new CFException();
2310 >
2311 >        f.completeExceptionally(ex);
2312 >        g.complete(v1);
2313 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2314 >
2315 >        // unspecified behavior
2316 >        Integer v;
2317 >        try {
2318 >            assertEquals(h.join(), null);
2319 >            assertEquals(r.invocationCount, 1);
2320 >            assertEquals(inc(v1), r.value);
2321 >        } catch (CompletionException ok) {
2322 >            checkCompletedWithWrappedCFException(h, ex);
2323 >            assertEquals(r.invocationCount, 0);
2324 >        }
2325 >
2326 >        checkCompletedWithWrappedCFException(f, ex);
2327 >        checkCompletedNormally(g, v1);
2328 >        }
2329      }
2330  
2331      /**
2332       * acceptEither result completes exceptionally if action does
2333       */
2334 <    public void testAcceptEither3() {
2335 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2336 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2337 <        FailingConsumer r = new FailingConsumer();
2338 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2339 <        f2.complete(two);
2340 <        checkCompletedWithWrappedCFException(g);
2334 >    public void testAcceptEither_actionFailed1() {
2335 >        for (ExecutionMode m : ExecutionMode.values())
2336 >        for (Integer v1 : new Integer[] { 1, null })
2337 >        for (Integer v2 : new Integer[] { 2, null }) {
2338 >
2339 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2340 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2341 >        final FailingConsumer r = new FailingConsumer();
2342 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2343 >
2344 >        f.complete(v1);
2345 >        checkCompletedWithWrappedCFException(h);
2346 >        g.complete(v2);
2347 >        checkCompletedNormally(f, v1);
2348 >        checkCompletedNormally(g, v2);
2349 >        }
2350 >    }
2351 >
2352 >    public void testAcceptEither_actionFailed2() {
2353 >        for (ExecutionMode m : ExecutionMode.values())
2354 >        for (Integer v1 : new Integer[] { 1, null })
2355 >        for (Integer v2 : new Integer[] { 2, null }) {
2356 >
2357 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2358 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2359 >        final FailingConsumer r = new FailingConsumer();
2360 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2361 >
2362 >        g.complete(v2);
2363 >        checkCompletedWithWrappedCFException(h);
2364 >        f.complete(v1);
2365 >        checkCompletedNormally(f, v1);
2366 >        checkCompletedNormally(g, v2);
2367 >        }
2368      }
2369  
2370      /**
2371       * acceptEither result completes exceptionally if either source cancelled
2372       */
2373 <    public void testAcceptEither4() {
2374 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2375 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2376 <        IncAction r = new IncAction();
2377 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2378 <        assertTrue(f.cancel(true));
2379 <        checkCompletedWithWrappedCancellationException(g);
2380 <        f = new CompletableFuture<Integer>();
2381 <        f2 = new CompletableFuture<Integer>();
2382 <        assertTrue(f2.cancel(true));
2383 <        checkCompletedWithWrappedCancellationException(g);
2373 >    public void testAcceptEither_sourceCancelled1() {
2374 >        for (ExecutionMode m : ExecutionMode.values())
2375 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2376 >        for (Integer v1 : new Integer[] { 1, null }) {
2377 >
2378 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2379 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2380 >        final IncAction r = new IncAction();
2381 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2382 >
2383 >        assertTrue(f.cancel(mayInterruptIfRunning));
2384 >        checkCompletedWithWrappedCancellationException(h);
2385 >        g.complete(v1);
2386 >
2387 >        checkCancelled(f);
2388 >        assertEquals(r.invocationCount, 0);
2389 >        checkCompletedNormally(g, v1);
2390 >        checkCompletedWithWrappedCancellationException(h);
2391 >        }
2392 >    }
2393 >
2394 >    public void testAcceptEither_sourceCancelled2() {
2395 >        for (ExecutionMode m : ExecutionMode.values())
2396 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2397 >        for (Integer v1 : new Integer[] { 1, null }) {
2398 >
2399 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2400 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2401 >        final IncAction r = new IncAction();
2402 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2403 >
2404 >        assertTrue(g.cancel(mayInterruptIfRunning));
2405 >        checkCompletedWithWrappedCancellationException(h);
2406 >        f.complete(v1);
2407 >
2408 >        checkCancelled(g);
2409 >        assertEquals(r.invocationCount, 0);
2410 >        checkCompletedNormally(f, v1);
2411 >        checkCompletedWithWrappedCancellationException(h);
2412 >        }
2413 >    }
2414 >
2415 >    public void testAcceptEither_sourceCancelled3() {
2416 >        for (ExecutionMode m : ExecutionMode.values())
2417 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2418 >        for (Integer v1 : new Integer[] { 1, null }) {
2419 >
2420 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2421 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2422 >        final IncAction r = new IncAction();
2423 >
2424 >        assertTrue(g.cancel(mayInterruptIfRunning));
2425 >        f.complete(v1);
2426 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2427 >
2428 >        // unspecified behavior
2429 >        Integer v;
2430 >        try {
2431 >            assertEquals(h.join(), null);
2432 >            assertEquals(r.invocationCount, 1);
2433 >            assertEquals(inc(v1), r.value);
2434 >        } catch (CompletionException ok) {
2435 >            checkCompletedWithWrappedCancellationException(h);
2436 >            assertEquals(r.invocationCount, 0);
2437 >        }
2438 >
2439 >        checkCancelled(g);
2440 >        checkCompletedNormally(f, v1);
2441 >        }
2442      }
2443  
2444 +    public void testAcceptEither_sourceCancelled4() {
2445 +        for (ExecutionMode m : ExecutionMode.values())
2446 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2447 +        for (Integer v1 : new Integer[] { 1, null }) {
2448 +
2449 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2450 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2451 +        final IncAction r = new IncAction();
2452 +
2453 +        assertTrue(f.cancel(mayInterruptIfRunning));
2454 +        g.complete(v1);
2455 +        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456 +
2457 +        // unspecified behavior
2458 +        Integer v;
2459 +        try {
2460 +            assertEquals(h.join(), null);
2461 +            assertEquals(r.invocationCount, 1);
2462 +            assertEquals(inc(v1), r.value);
2463 +        } catch (CompletionException ok) {
2464 +            checkCompletedWithWrappedCancellationException(h);
2465 +            assertEquals(r.invocationCount, 0);
2466 +        }
2467 +
2468 +        checkCancelled(f);
2469 +        checkCompletedNormally(g, v1);
2470 +        }
2471 +    }
2472  
2473      /**
2474       * runAfterEither result completes normally after normal completion
2475       * of either source
2476       */
2477 <    public void testRunAfterEither() {
2478 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2479 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2480 <        Noop r = new Noop();
2481 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2482 <        f.complete(one);
2483 <        checkCompletedNormally(g, null);
2484 <        f2.complete(one);
2485 <        checkCompletedNormally(g, null);
2486 <        assertTrue(r.ran);
2477 >    public void testRunAfterEither_normalCompletion1() {
2478 >        for (ExecutionMode m : ExecutionMode.values())
2479 >        for (Integer v1 : new Integer[] { 1, null })
2480 >        for (Integer v2 : new Integer[] { 2, null }) {
2481 >
2482 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2483 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2484 >        final Noop r = new Noop();
2485 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2486 >
2487 >        f.complete(v1);
2488 >        checkCompletedNormally(h, null);
2489 >        assertEquals(r.invocationCount, 1);
2490 >        g.complete(v2);
2491 >
2492 >        checkCompletedNormally(f, v1);
2493 >        checkCompletedNormally(g, v2);
2494 >        checkCompletedNormally(h, null);
2495 >        assertEquals(r.invocationCount, 1);
2496 >        }
2497 >    }
2498  
2499 <        r = new Noop();
2500 <        f = new CompletableFuture<Integer>();
2501 <        f.complete(one);
2502 <        f2 = new CompletableFuture<Integer>();
2503 <        g = f.runAfterEither(f2, r);
2504 <        checkCompletedNormally(g, null);
2505 <        assertTrue(r.ran);
2499 >    public void testRunAfterEither_normalCompletion2() {
2500 >        for (ExecutionMode m : ExecutionMode.values())
2501 >        for (Integer v1 : new Integer[] { 1, null })
2502 >        for (Integer v2 : new Integer[] { 2, null }) {
2503 >
2504 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2505 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2506 >        final Noop r = new Noop();
2507 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2508 >
2509 >        g.complete(v2);
2510 >        checkCompletedNormally(h, null);
2511 >        assertEquals(r.invocationCount, 1);
2512 >        f.complete(v1);
2513 >
2514 >        checkCompletedNormally(f, v1);
2515 >        checkCompletedNormally(g, v2);
2516 >        checkCompletedNormally(h, null);
2517 >        assertEquals(r.invocationCount, 1);
2518 >        }
2519 >    }
2520 >    public void testRunAfterEither_normalCompletion3() {
2521 >        for (ExecutionMode m : ExecutionMode.values())
2522 >        for (Integer v1 : new Integer[] { 1, null })
2523 >        for (Integer v2 : new Integer[] { 2, null }) {
2524 >
2525 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2526 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2527 >        final Noop r = new Noop();
2528 >
2529 >        f.complete(v1);
2530 >        g.complete(v2);
2531 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2532 >
2533 >        checkCompletedNormally(h, null);
2534 >        checkCompletedNormally(f, v1);
2535 >        checkCompletedNormally(g, v2);
2536 >        assertEquals(r.invocationCount, 1);
2537 >        }
2538      }
2539  
2540      /**
2541       * runAfterEither result completes exceptionally after exceptional
2542       * completion of either source
2543       */
2544 <    public void testRunAfterEither2() {
2545 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2546 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2547 <        Noop r = new Noop();
2548 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2549 <        f.completeExceptionally(new CFException());
2550 <        f2.complete(one);
2551 <        checkCompletedWithWrappedCFException(g);
2544 >    public void testRunAfterEither_exceptionalCompletion1() {
2545 >        for (ExecutionMode m : ExecutionMode.values())
2546 >        for (Integer v1 : new Integer[] { 1, null }) {
2547 >
2548 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2549 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2550 >        final Noop r = new Noop();
2551 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2552 >        final CFException ex = new CFException();
2553 >
2554 >        f.completeExceptionally(ex);
2555 >        checkCompletedWithWrappedCFException(h, ex);
2556 >        g.complete(v1);
2557 >
2558 >        assertEquals(r.invocationCount, 0);
2559 >        checkCompletedNormally(g, v1);
2560 >        checkCompletedWithWrappedCFException(f, ex);
2561 >        checkCompletedWithWrappedCFException(h, ex);
2562 >        }
2563 >    }
2564  
2565 <        r = new Noop();
2566 <        f = new CompletableFuture<Integer>();
2567 <        f2 = new CompletableFuture<Integer>();
2568 <        f2.completeExceptionally(new CFException());
2569 <        g = f.runAfterEither(f2, r);
2570 <        checkCompletedWithWrappedCFException(g);
2565 >    public void testRunAfterEither_exceptionalCompletion2() {
2566 >        for (ExecutionMode m : ExecutionMode.values())
2567 >        for (Integer v1 : new Integer[] { 1, null }) {
2568 >
2569 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2570 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2571 >        final Noop r = new Noop();
2572 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2573 >        final CFException ex = new CFException();
2574 >
2575 >        g.completeExceptionally(ex);
2576 >        checkCompletedWithWrappedCFException(h, ex);
2577 >        f.complete(v1);
2578 >
2579 >        assertEquals(r.invocationCount, 0);
2580 >        checkCompletedNormally(f, v1);
2581 >        checkCompletedWithWrappedCFException(g, ex);
2582 >        checkCompletedWithWrappedCFException(h, ex);
2583 >        }
2584 >    }
2585 >
2586 >    public void testRunAfterEither_exceptionalCompletion3() {
2587 >        for (ExecutionMode m : ExecutionMode.values())
2588 >        for (Integer v1 : new Integer[] { 1, null }) {
2589 >
2590 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2591 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2592 >        final Noop r = new Noop();
2593 >        final CFException ex = new CFException();
2594 >
2595 >        g.completeExceptionally(ex);
2596 >        f.complete(v1);
2597 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2598 >
2599 >        // unspecified behavior
2600 >        Integer v;
2601 >        try {
2602 >            assertEquals(h.join(), null);
2603 >            assertEquals(r.invocationCount, 1);
2604 >        } catch (CompletionException ok) {
2605 >            checkCompletedWithWrappedCFException(h, ex);
2606 >            assertEquals(r.invocationCount, 0);
2607 >        }
2608 >
2609 >        checkCompletedWithWrappedCFException(g, ex);
2610 >        checkCompletedNormally(f, v1);
2611 >        }
2612 >    }
2613 >
2614 >    public void testRunAfterEither_exceptionalCompletion4() {
2615 >        for (ExecutionMode m : ExecutionMode.values())
2616 >        for (Integer v1 : new Integer[] { 1, null }) {
2617 >
2618 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2619 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2620 >        final Noop r = new Noop();
2621 >        final CFException ex = new CFException();
2622 >
2623 >        f.completeExceptionally(ex);
2624 >        g.complete(v1);
2625 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2626 >
2627 >        // unspecified behavior
2628 >        Integer v;
2629 >        try {
2630 >            assertEquals(h.join(), null);
2631 >            assertEquals(r.invocationCount, 1);
2632 >        } catch (CompletionException ok) {
2633 >            checkCompletedWithWrappedCFException(h, ex);
2634 >            assertEquals(r.invocationCount, 0);
2635 >        }
2636 >
2637 >        checkCompletedWithWrappedCFException(f, ex);
2638 >        checkCompletedNormally(g, v1);
2639 >        }
2640      }
2641  
2642      /**
2643       * runAfterEither result completes exceptionally if action does
2644       */
2645 <    public void testRunAfterEither3() {
2646 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2647 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2648 <        FailingNoop r = new FailingNoop();
2649 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2650 <        f2.complete(two);
2651 <        checkCompletedWithWrappedCFException(g);
2645 >    public void testRunAfterEither_actionFailed1() {
2646 >        for (ExecutionMode m : ExecutionMode.values())
2647 >        for (Integer v1 : new Integer[] { 1, null })
2648 >        for (Integer v2 : new Integer[] { 2, null }) {
2649 >
2650 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2651 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2652 >        final FailingNoop r = new FailingNoop();
2653 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2654 >
2655 >        f.complete(v1);
2656 >        checkCompletedWithWrappedCFException(h);
2657 >        g.complete(v2);
2658 >        checkCompletedNormally(f, v1);
2659 >        checkCompletedNormally(g, v2);
2660 >        }
2661 >    }
2662 >
2663 >    public void testRunAfterEither_actionFailed2() {
2664 >        for (ExecutionMode m : ExecutionMode.values())
2665 >        for (Integer v1 : new Integer[] { 1, null })
2666 >        for (Integer v2 : new Integer[] { 2, null }) {
2667 >
2668 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2669 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2670 >        final FailingNoop r = new FailingNoop();
2671 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2672 >
2673 >        g.complete(v2);
2674 >        checkCompletedWithWrappedCFException(h);
2675 >        f.complete(v1);
2676 >        checkCompletedNormally(f, v1);
2677 >        checkCompletedNormally(g, v2);
2678 >        }
2679      }
2680  
2681      /**
2682       * runAfterEither result completes exceptionally if either source cancelled
2683       */
2684 <    public void testRunAfterEither4() {
2685 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2686 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2687 <        Noop r = new Noop();
2688 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2689 <        assertTrue(f.cancel(true));
2690 <        checkCompletedWithWrappedCancellationException(g);
2691 <        f = new CompletableFuture<Integer>();
2692 <        f2 = new CompletableFuture<Integer>();
2693 <        assertTrue(f2.cancel(true));
2694 <        checkCompletedWithWrappedCancellationException(g);
2684 >    public void testRunAfterEither_sourceCancelled1() {
2685 >        for (ExecutionMode m : ExecutionMode.values())
2686 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2687 >        for (Integer v1 : new Integer[] { 1, null }) {
2688 >
2689 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2690 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2691 >        final Noop r = new Noop();
2692 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2693 >
2694 >        assertTrue(f.cancel(mayInterruptIfRunning));
2695 >        checkCompletedWithWrappedCancellationException(h);
2696 >        g.complete(v1);
2697 >
2698 >        checkCancelled(f);
2699 >        assertEquals(r.invocationCount, 0);
2700 >        checkCompletedNormally(g, v1);
2701 >        checkCompletedWithWrappedCancellationException(h);
2702 >        }
2703 >    }
2704 >
2705 >    public void testRunAfterEither_sourceCancelled2() {
2706 >        for (ExecutionMode m : ExecutionMode.values())
2707 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2708 >        for (Integer v1 : new Integer[] { 1, null }) {
2709 >
2710 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2712 >        final Noop r = new Noop();
2713 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 >
2715 >        assertTrue(g.cancel(mayInterruptIfRunning));
2716 >        checkCompletedWithWrappedCancellationException(h);
2717 >        f.complete(v1);
2718 >
2719 >        checkCancelled(g);
2720 >        assertEquals(r.invocationCount, 0);
2721 >        checkCompletedNormally(f, v1);
2722 >        checkCompletedWithWrappedCancellationException(h);
2723 >        }
2724 >    }
2725 >
2726 >    public void testRunAfterEither_sourceCancelled3() {
2727 >        for (ExecutionMode m : ExecutionMode.values())
2728 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2729 >        for (Integer v1 : new Integer[] { 1, null }) {
2730 >
2731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733 >        final Noop r = new Noop();
2734 >
2735 >        assertTrue(g.cancel(mayInterruptIfRunning));
2736 >        f.complete(v1);
2737 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2738 >
2739 >        // unspecified behavior
2740 >        Integer v;
2741 >        try {
2742 >            assertEquals(h.join(), null);
2743 >            assertEquals(r.invocationCount, 1);
2744 >        } catch (CompletionException ok) {
2745 >            checkCompletedWithWrappedCancellationException(h);
2746 >            assertEquals(r.invocationCount, 0);
2747 >        }
2748 >
2749 >        checkCancelled(g);
2750 >        checkCompletedNormally(f, v1);
2751 >        }
2752 >    }
2753 >
2754 >    public void testRunAfterEither_sourceCancelled4() {
2755 >        for (ExecutionMode m : ExecutionMode.values())
2756 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2757 >        for (Integer v1 : new Integer[] { 1, null }) {
2758 >
2759 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2760 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2761 >        final Noop r = new Noop();
2762 >
2763 >        assertTrue(f.cancel(mayInterruptIfRunning));
2764 >        g.complete(v1);
2765 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2766 >
2767 >        // unspecified behavior
2768 >        Integer v;
2769 >        try {
2770 >            assertEquals(h.join(), null);
2771 >            assertEquals(r.invocationCount, 1);
2772 >        } catch (CompletionException ok) {
2773 >            checkCompletedWithWrappedCancellationException(h);
2774 >            assertEquals(r.invocationCount, 0);
2775 >        }
2776 >
2777 >        checkCancelled(f);
2778 >        checkCompletedNormally(g, v1);
2779 >        }
2780      }
2781  
2782      /**
2783       * thenCompose result completes normally after normal completion of source
2784       */
2785      public void testThenCompose() {
2786 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2787 <        CompletableFutureInc r = new CompletableFutureInc();
2788 <        CompletableFuture<Integer> g = f.thenCompose(r);
2786 >        CompletableFuture<Integer> f, g;
2787 >        CompletableFutureInc r;
2788 >
2789 >        f = new CompletableFuture<>();
2790 >        g = f.thenCompose(r = new CompletableFutureInc());
2791 >        f.complete(one);
2792 >        checkCompletedNormally(g, two);
2793 >        assertTrue(r.ran);
2794 >
2795 >        f = new CompletableFuture<>();
2796          f.complete(one);
2797 +        g = f.thenCompose(r = new CompletableFutureInc());
2798          checkCompletedNormally(g, two);
2799 +        assertTrue(r.ran);
2800      }
2801  
2802      /**
# Line 1139 | Line 2804 | public class CompletableFutureTest exten
2804       * completion of source
2805       */
2806      public void testThenCompose2() {
2807 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2808 <        CompletableFutureInc r = new CompletableFutureInc();
2809 <        CompletableFuture<Integer> g = f.thenCompose(r);
2807 >        CompletableFuture<Integer> f, g;
2808 >        CompletableFutureInc r;
2809 >
2810 >        f = new CompletableFuture<>();
2811 >        g = f.thenCompose(r = new CompletableFutureInc());
2812 >        f.completeExceptionally(new CFException());
2813 >        checkCompletedWithWrappedCFException(g);
2814 >
2815 >        f = new CompletableFuture<>();
2816          f.completeExceptionally(new CFException());
2817 +        g = f.thenCompose(r = new CompletableFutureInc());
2818          checkCompletedWithWrappedCFException(g);
2819      }
2820  
# Line 1150 | Line 2822 | public class CompletableFutureTest exten
2822       * thenCompose result completes exceptionally if action does
2823       */
2824      public void testThenCompose3() {
2825 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2826 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2827 <        CompletableFuture<Integer> g = f.thenCompose(r);
2825 >        CompletableFuture<Integer> f, g;
2826 >        FailingCompletableFutureFunction r;
2827 >
2828 >        f = new CompletableFuture<>();
2829 >        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2830 >        f.complete(one);
2831 >        checkCompletedWithWrappedCFException(g);
2832 >
2833 >        f = new CompletableFuture<>();
2834          f.complete(one);
2835 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2836          checkCompletedWithWrappedCFException(g);
2837      }
2838  
# Line 1161 | Line 2840 | public class CompletableFutureTest exten
2840       * thenCompose result completes exceptionally if source cancelled
2841       */
2842      public void testThenCompose4() {
2843 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2844 <        CompletableFutureInc r = new CompletableFutureInc();
2845 <        CompletableFuture<Integer> g = f.thenCompose(r);
2843 >        CompletableFuture<Integer> f, g;
2844 >        CompletableFutureInc r;
2845 >
2846 >        f = new CompletableFuture<>();
2847 >        g = f.thenCompose(r = new CompletableFutureInc());
2848          assertTrue(f.cancel(true));
2849          checkCompletedWithWrappedCancellationException(g);
1169    }
2850  
2851 +        f = new CompletableFuture<>();
2852 +        assertTrue(f.cancel(true));
2853 +        g = f.thenCompose(r = new CompletableFutureInc());
2854 +        checkCompletedWithWrappedCancellationException(g);
2855 +    }
2856  
2857      // asyncs
2858  
# Line 1175 | Line 2860 | public class CompletableFutureTest exten
2860       * thenRunAsync result completes normally after normal completion of source
2861       */
2862      public void testThenRunAsync() {
2863 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2863 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2864          Noop r = new Noop();
2865          CompletableFuture<Void> g = f.thenRunAsync(r);
2866          f.complete(null);
2867          checkCompletedNormally(g, null);
2868  
2869          // reordered version
2870 <        f = new CompletableFuture<Integer>();
2870 >        f = new CompletableFuture<>();
2871          f.complete(null);
2872          r = new Noop();
2873          g = f.thenRunAsync(r);
# Line 1194 | Line 2879 | public class CompletableFutureTest exten
2879       * completion of source
2880       */
2881      public void testThenRunAsync2() {
2882 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2882 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2883          Noop r = new Noop();
2884          CompletableFuture<Void> g = f.thenRunAsync(r);
2885          f.completeExceptionally(new CFException());
2886          try {
2887              g.join();
2888              shouldThrow();
2889 <        } catch (Exception ok) {
1205 <        }
2889 >        } catch (CompletionException success) {}
2890          checkCompletedWithWrappedCFException(g);
2891      }
2892  
# Line 1210 | Line 2894 | public class CompletableFutureTest exten
2894       * thenRunAsync result completes exceptionally if action does
2895       */
2896      public void testThenRunAsync3() {
2897 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2897 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2898          FailingNoop r = new FailingNoop();
2899          CompletableFuture<Void> g = f.thenRunAsync(r);
2900          f.complete(null);
# Line 1221 | Line 2905 | public class CompletableFutureTest exten
2905       * thenRunAsync result completes exceptionally if source cancelled
2906       */
2907      public void testThenRunAsync4() {
2908 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2908 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2909          Noop r = new Noop();
2910          CompletableFuture<Void> g = f.thenRunAsync(r);
2911          assertTrue(f.cancel(true));
# Line 1232 | Line 2916 | public class CompletableFutureTest exten
2916       * thenApplyAsync result completes normally after normal completion of source
2917       */
2918      public void testThenApplyAsync() {
2919 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2919 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2920          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2921          f.complete(one);
2922          checkCompletedNormally(g, two);
# Line 1243 | Line 2927 | public class CompletableFutureTest exten
2927       * completion of source
2928       */
2929      public void testThenApplyAsync2() {
2930 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2930 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2931          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2932          f.completeExceptionally(new CFException());
2933          checkCompletedWithWrappedCFException(g);
# Line 1253 | Line 2937 | public class CompletableFutureTest exten
2937       * thenApplyAsync result completes exceptionally if action does
2938       */
2939      public void testThenApplyAsync3() {
2940 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2940 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2941          FailingFunction r = new FailingFunction();
2942          CompletableFuture<Integer> g = f.thenApplyAsync(r);
2943          f.complete(null);
# Line 1264 | Line 2948 | public class CompletableFutureTest exten
2948       * thenApplyAsync result completes exceptionally if source cancelled
2949       */
2950      public void testThenApplyAsync4() {
2951 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2951 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2952          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2953          assertTrue(f.cancel(true));
2954          checkCompletedWithWrappedCancellationException(g);
# Line 1275 | Line 2959 | public class CompletableFutureTest exten
2959       * completion of source
2960       */
2961      public void testThenAcceptAsync() {
2962 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2962 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2963          IncAction r = new IncAction();
2964          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2965          f.complete(one);
2966          checkCompletedNormally(g, null);
2967 <        assertEquals(r.value, 2);
2967 >        assertEquals(r.value, (Integer) 2);
2968      }
2969  
2970      /**
# Line 1288 | Line 2972 | public class CompletableFutureTest exten
2972       * completion of source
2973       */
2974      public void testThenAcceptAsync2() {
2975 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2975 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2976          IncAction r = new IncAction();
2977          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2978          f.completeExceptionally(new CFException());
# Line 1299 | Line 2983 | public class CompletableFutureTest exten
2983       * thenAcceptAsync result completes exceptionally if action does
2984       */
2985      public void testThenAcceptAsync3() {
2986 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2986 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2987          FailingConsumer r = new FailingConsumer();
2988          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2989          f.complete(null);
# Line 1310 | Line 2994 | public class CompletableFutureTest exten
2994       * thenAcceptAsync result completes exceptionally if source cancelled
2995       */
2996      public void testThenAcceptAsync4() {
2997 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2997 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2998          IncAction r = new IncAction();
2999          CompletableFuture<Void> g = f.thenAcceptAsync(r);
3000          assertTrue(f.cancel(true));
# Line 1318 | Line 3002 | public class CompletableFutureTest exten
3002      }
3003  
3004      /**
1321     * thenCombineAsync result completes normally after normal
1322     * completion of sources
1323     */
1324    public void testThenCombineAsync() {
1325        CompletableFuture<Integer> f, g, h;
1326
1327        f = new CompletableFuture<Integer>();
1328        g = new CompletableFuture<Integer>();
1329        h = f.thenCombineAsync(g, subtract);
1330        f.complete(3);
1331        checkIncomplete(h);
1332        g.complete(1);
1333        checkCompletedNormally(h, 2);
1334
1335        f = new CompletableFuture<Integer>();
1336        g = new CompletableFuture<Integer>();
1337        h = f.thenCombineAsync(g, subtract);
1338        g.complete(1);
1339        checkIncomplete(h);
1340        f.complete(3);
1341        checkCompletedNormally(h, 2);
1342
1343        f = new CompletableFuture<Integer>();
1344        g = new CompletableFuture<Integer>();
1345        g.complete(1);
1346        f.complete(3);
1347        h = f.thenCombineAsync(g, subtract);
1348        checkCompletedNormally(h, 2);
1349    }
1350
1351    /**
1352     * thenCombineAsync result completes exceptionally after exceptional
1353     * completion of either source
1354     */
1355    public void testThenCombineAsync2() {
1356        CompletableFuture<Integer> f, g, h;
1357
1358        f = new CompletableFuture<Integer>();
1359        g = new CompletableFuture<Integer>();
1360        h = f.thenCombineAsync(g, subtract);
1361        f.completeExceptionally(new CFException());
1362        checkIncomplete(h);
1363        g.complete(1);
1364        checkCompletedWithWrappedCFException(h);
1365
1366        f = new CompletableFuture<Integer>();
1367        g = new CompletableFuture<Integer>();
1368        h = f.thenCombineAsync(g, subtract);
1369        g.completeExceptionally(new CFException());
1370        checkIncomplete(h);
1371        f.complete(3);
1372        checkCompletedWithWrappedCFException(h);
1373
1374        f = new CompletableFuture<Integer>();
1375        g = new CompletableFuture<Integer>();
1376        g.completeExceptionally(new CFException());
1377        f.complete(3);
1378        h = f.thenCombineAsync(g, subtract);
1379        checkCompletedWithWrappedCFException(h);
1380    }
1381
1382    /**
1383     * thenCombineAsync result completes exceptionally if action does
1384     */
1385    public void testThenCombineAsync3() {
1386        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1387        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1388        FailingBiFunction r = new FailingBiFunction();
1389        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1390        f.complete(one);
1391        checkIncomplete(g);
1392        assertFalse(r.ran);
1393        f2.complete(two);
1394        checkCompletedWithWrappedCFException(g);
1395        assertTrue(r.ran);
1396    }
1397
1398    /**
1399     * thenCombineAsync result completes exceptionally if either source cancelled
1400     */
1401    public void testThenCombineAsync4() {
1402        CompletableFuture<Integer> f, g, h;
1403
1404        f = new CompletableFuture<Integer>();
1405        g = new CompletableFuture<Integer>();
1406        h = f.thenCombineAsync(g, subtract);
1407        assertTrue(f.cancel(true));
1408        checkIncomplete(h);
1409        g.complete(1);
1410        checkCompletedWithWrappedCancellationException(h);
1411
1412        f = new CompletableFuture<Integer>();
1413        g = new CompletableFuture<Integer>();
1414        h = f.thenCombineAsync(g, subtract);
1415        assertTrue(g.cancel(true));
1416        checkIncomplete(h);
1417        f.complete(3);
1418        checkCompletedWithWrappedCancellationException(h);
1419
1420        f = new CompletableFuture<Integer>();
1421        g = new CompletableFuture<Integer>();
1422        g.complete(3);
1423        assertTrue(f.cancel(true));
1424        h = f.thenCombineAsync(g, subtract);
1425        checkCompletedWithWrappedCancellationException(h);
1426
1427        f = new CompletableFuture<Integer>();
1428        g = new CompletableFuture<Integer>();
1429        f.complete(3);
1430        assertTrue(g.cancel(true));
1431        h = f.thenCombineAsync(g, subtract);
1432        checkCompletedWithWrappedCancellationException(h);
1433    }
1434
1435    /**
1436     * thenAcceptBothAsync result completes normally after normal
1437     * completion of sources
1438     */
1439    public void testThenAcceptBothAsync() {
1440        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1441        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1442        AddAction r = new AddAction();
1443        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1444        f.complete(one);
1445        checkIncomplete(g);
1446        f2.complete(two);
1447        checkCompletedNormally(g, null);
1448        assertEquals(r.value, 3);
1449    }
1450
1451    /**
1452     * thenAcceptBothAsync result completes exceptionally after exceptional
1453     * completion of source
1454     */
1455    public void testThenAcceptBothAsync2() {
1456        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1457        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1458        AddAction r = new AddAction();
1459        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1460        f.completeExceptionally(new CFException());
1461        f2.complete(two);
1462        checkCompletedWithWrappedCFException(g);
1463
1464        r = new AddAction();
1465        f = new CompletableFuture<Integer>();
1466        f2 = new CompletableFuture<Integer>();
1467        g = f.thenAcceptBothAsync(f2, r);
1468        f.complete(one);
1469        f2.completeExceptionally(new CFException());
1470        checkCompletedWithWrappedCFException(g);
1471    }
1472
1473    /**
1474     * thenAcceptBothAsync result completes exceptionally if action does
1475     */
1476    public void testThenAcceptBothAsync3() {
1477        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1478        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1479        FailingBiConsumer r = new FailingBiConsumer();
1480        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1481        f.complete(one);
1482        checkIncomplete(g);
1483        f2.complete(two);
1484        checkCompletedWithWrappedCFException(g);
1485    }
1486
1487    /**
1488     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1489     */
1490    public void testThenAcceptBothAsync4() {
1491        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1492        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1493        AddAction r = new AddAction();
1494        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1495        assertTrue(f.cancel(true));
1496        f2.complete(two);
1497        checkCompletedWithWrappedCancellationException(g);
1498
1499        r = new AddAction();
1500        f = new CompletableFuture<Integer>();
1501        f2 = new CompletableFuture<Integer>();
1502        g = f.thenAcceptBothAsync(f2, r);
1503        f.complete(one);
1504        assertTrue(f2.cancel(true));
1505        checkCompletedWithWrappedCancellationException(g);
1506    }
1507
1508    /**
1509     * runAfterBothAsync result completes normally after normal
1510     * completion of sources
1511     */
1512    public void testRunAfterBothAsync() {
1513        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1514        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1515        Noop r = new Noop();
1516        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1517        f.complete(one);
1518        checkIncomplete(g);
1519        f2.complete(two);
1520        checkCompletedNormally(g, null);
1521        assertTrue(r.ran);
1522    }
1523
1524    /**
1525     * runAfterBothAsync result completes exceptionally after exceptional
1526     * completion of source
1527     */
1528    public void testRunAfterBothAsync2() {
1529        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1530        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1531        Noop r = new Noop();
1532        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1533        f.completeExceptionally(new CFException());
1534        f2.complete(two);
1535        checkCompletedWithWrappedCFException(g);
1536
1537        r = new Noop();
1538        f = new CompletableFuture<Integer>();
1539        f2 = new CompletableFuture<Integer>();
1540        g = f.runAfterBothAsync(f2, r);
1541        f.complete(one);
1542        f2.completeExceptionally(new CFException());
1543        checkCompletedWithWrappedCFException(g);
1544    }
1545
1546    /**
1547     * runAfterBothAsync result completes exceptionally if action does
1548     */
1549    public void testRunAfterBothAsync3() {
1550        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1551        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1552        FailingNoop r = new FailingNoop();
1553        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1554        f.complete(one);
1555        checkIncomplete(g);
1556        f2.complete(two);
1557        checkCompletedWithWrappedCFException(g);
1558    }
1559
1560    /**
1561     * runAfterBothAsync result completes exceptionally if either source cancelled
1562     */
1563    public void testRunAfterBothAsync4() {
1564        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1565        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1566        Noop r = new Noop();
1567        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1568        assertTrue(f.cancel(true));
1569        f2.complete(two);
1570        checkCompletedWithWrappedCancellationException(g);
1571
1572        r = new Noop();
1573        f = new CompletableFuture<Integer>();
1574        f2 = new CompletableFuture<Integer>();
1575        g = f.runAfterBothAsync(f2, r);
1576        f.complete(one);
1577        assertTrue(f2.cancel(true));
1578        checkCompletedWithWrappedCancellationException(g);
1579    }
1580
1581    /**
1582     * applyToEitherAsync result completes normally after normal
1583     * completion of sources
1584     */
1585    public void testApplyToEitherAsync() {
1586        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1587        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1588        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1589        f.complete(one);
1590        checkCompletedNormally(g, two);
1591
1592        f = new CompletableFuture<Integer>();
1593        f.complete(one);
1594        f2 = new CompletableFuture<Integer>();
1595        g = f.applyToEitherAsync(f2, inc);
1596        checkCompletedNormally(g, two);
1597    }
1598
1599    /**
1600     * applyToEitherAsync result completes exceptionally after exceptional
1601     * completion of source
1602     */
1603    public void testApplyToEitherAsync2() {
1604        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1605        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1606        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1607        f.completeExceptionally(new CFException());
1608        checkCompletedWithWrappedCFException(g);
1609
1610        f = new CompletableFuture<Integer>();
1611        f2 = new CompletableFuture<Integer>();
1612        f2.completeExceptionally(new CFException());
1613        g = f.applyToEitherAsync(f2, inc);
1614        f.complete(one);
1615        checkCompletedWithWrappedCFException(g);
1616    }
1617
1618    /**
1619     * applyToEitherAsync result completes exceptionally if action does
1620     */
1621    public void testApplyToEitherAsync3() {
1622        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1623        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1624        FailingFunction r = new FailingFunction();
1625        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1626        f.complete(one);
1627        checkCompletedWithWrappedCFException(g);
1628    }
1629
1630    /**
1631     * applyToEitherAsync result completes exceptionally if either source cancelled
1632     */
1633    public void testApplyToEitherAsync4() {
1634        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1635        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1636        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1637        assertTrue(f.cancel(true));
1638        checkCompletedWithWrappedCancellationException(g);
1639
1640        f = new CompletableFuture<Integer>();
1641        f2 = new CompletableFuture<Integer>();
1642        assertTrue(f2.cancel(true));
1643        g = f.applyToEitherAsync(f2, inc);
1644        checkCompletedWithWrappedCancellationException(g);
1645    }
1646
1647    /**
1648     * acceptEitherAsync result completes normally after normal
1649     * completion of sources
1650     */
1651    public void testAcceptEitherAsync() {
1652        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1653        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1654        IncAction r = new IncAction();
1655        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1656        f.complete(one);
1657        checkCompletedNormally(g, null);
1658        assertEquals(r.value, 2);
1659
1660        r = new IncAction();
1661        f = new CompletableFuture<Integer>();
1662        f.complete(one);
1663        f2 = new CompletableFuture<Integer>();
1664        g = f.acceptEitherAsync(f2, r);
1665        checkCompletedNormally(g, null);
1666        assertEquals(r.value, 2);
1667    }
1668
1669    /**
1670     * acceptEitherAsync result completes exceptionally after exceptional
1671     * completion of source
1672     */
1673    public void testAcceptEitherAsync2() {
1674        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1675        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1676        IncAction r = new IncAction();
1677        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1678        f.completeExceptionally(new CFException());
1679        checkCompletedWithWrappedCFException(g);
1680
1681        r = new IncAction();
1682        f = new CompletableFuture<Integer>();
1683        f2 = new CompletableFuture<Integer>();
1684        f2.completeExceptionally(new CFException());
1685        g = f.acceptEitherAsync(f2, r);
1686        f.complete(one);
1687        checkCompletedWithWrappedCFException(g);
1688    }
1689
1690    /**
1691     * acceptEitherAsync result completes exceptionally if action does
1692     */
1693    public void testAcceptEitherAsync3() {
1694        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1695        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1696        FailingConsumer r = new FailingConsumer();
1697        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1698        f.complete(one);
1699        checkCompletedWithWrappedCFException(g);
1700    }
1701
1702    /**
1703     * acceptEitherAsync result completes exceptionally if either
1704     * source cancelled
1705     */
1706    public void testAcceptEitherAsync4() {
1707        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1708        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1709        IncAction r = new IncAction();
1710        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1711        assertTrue(f.cancel(true));
1712        checkCompletedWithWrappedCancellationException(g);
1713
1714        r = new IncAction();
1715        f = new CompletableFuture<Integer>();
1716        f2 = new CompletableFuture<Integer>();
1717        assertTrue(f2.cancel(true));
1718        g = f.acceptEitherAsync(f2, r);
1719        checkCompletedWithWrappedCancellationException(g);
1720    }
1721
1722    /**
1723     * runAfterEitherAsync result completes normally after normal
1724     * completion of sources
1725     */
1726    public void testRunAfterEitherAsync() {
1727        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1728        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1729        Noop r = new Noop();
1730        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1731        f.complete(one);
1732        checkCompletedNormally(g, null);
1733        assertTrue(r.ran);
1734
1735        r = new Noop();
1736        f = new CompletableFuture<Integer>();
1737        f.complete(one);
1738        f2 = new CompletableFuture<Integer>();
1739        g = f.runAfterEitherAsync(f2, r);
1740        checkCompletedNormally(g, null);
1741        assertTrue(r.ran);
1742    }
1743
1744    /**
1745     * runAfterEitherAsync result completes exceptionally after exceptional
1746     * completion of source
1747     */
1748    public void testRunAfterEitherAsync2() {
1749        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1750        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1751        Noop r = new Noop();
1752        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1753        f.completeExceptionally(new CFException());
1754        checkCompletedWithWrappedCFException(g);
1755
1756        r = new Noop();
1757        f = new CompletableFuture<Integer>();
1758        f2 = new CompletableFuture<Integer>();
1759        f2.completeExceptionally(new CFException());
1760        g = f.runAfterEitherAsync(f2, r);
1761        f.complete(one);
1762        checkCompletedWithWrappedCFException(g);
1763    }
1764
1765    /**
1766     * runAfterEitherAsync result completes exceptionally if action does
1767     */
1768    public void testRunAfterEitherAsync3() {
1769        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1770        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1771        FailingNoop r = new FailingNoop();
1772        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1773        f.complete(one);
1774        checkCompletedWithWrappedCFException(g);
1775    }
1776
1777    /**
1778     * runAfterEitherAsync result completes exceptionally if either
1779     * source cancelled
1780     */
1781    public void testRunAfterEitherAsync4() {
1782        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1783        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1784        Noop r = new Noop();
1785        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1786        assertTrue(f.cancel(true));
1787        checkCompletedWithWrappedCancellationException(g);
1788
1789        r = new Noop();
1790        f = new CompletableFuture<Integer>();
1791        f2 = new CompletableFuture<Integer>();
1792        assertTrue(f2.cancel(true));
1793        g = f.runAfterEitherAsync(f2, r);
1794        checkCompletedWithWrappedCancellationException(g);
1795    }
1796
1797    /**
3005       * thenComposeAsync result completes normally after normal
3006       * completion of source
3007       */
# Line 1802 | Line 3009 | public class CompletableFutureTest exten
3009          CompletableFuture<Integer> f, g;
3010          CompletableFutureInc r;
3011  
3012 <        f = new CompletableFuture<Integer>();
3012 >        f = new CompletableFuture<>();
3013          g = f.thenComposeAsync(r = new CompletableFutureInc());
3014          f.complete(one);
3015          checkCompletedNormally(g, two);
3016  
3017 <        f = new CompletableFuture<Integer>();
3017 >        f = new CompletableFuture<>();
3018          f.complete(one);
3019          g = f.thenComposeAsync(r = new CompletableFutureInc());
3020          checkCompletedNormally(g, two);
# Line 1821 | Line 3028 | public class CompletableFutureTest exten
3028          CompletableFuture<Integer> f, g;
3029          CompletableFutureInc r;
3030  
3031 <        f = new CompletableFuture<Integer>();
3031 >        f = new CompletableFuture<>();
3032          g = f.thenComposeAsync(r = new CompletableFutureInc());
3033          f.completeExceptionally(new CFException());
3034          checkCompletedWithWrappedCFException(g);
3035          assertFalse(r.ran);
3036  
3037 <        f = new CompletableFuture<Integer>();
3037 >        f = new CompletableFuture<>();
3038          f.completeExceptionally(new CFException());
3039          g = f.thenComposeAsync(r = new CompletableFutureInc());
3040          checkCompletedWithWrappedCFException(g);
# Line 1840 | Line 3047 | public class CompletableFutureTest exten
3047      public void testThenComposeAsync3() {
3048          CompletableFuture<Integer> f, g;
3049          FailingCompletableFutureFunction r;
3050 <        
3051 <        f = new CompletableFuture<Integer>();
3050 >
3051 >        f = new CompletableFuture<>();
3052          g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3053          f.complete(one);
3054          checkCompletedWithWrappedCFException(g);
3055 <        
3056 <        f = new CompletableFuture<Integer>();
3055 >
3056 >        f = new CompletableFuture<>();
3057          f.complete(one);
3058          g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
3059          checkCompletedWithWrappedCFException(g);
# Line 1859 | Line 3066 | public class CompletableFutureTest exten
3066          CompletableFuture<Integer> f, g;
3067          CompletableFutureInc r;
3068  
3069 <        f = new CompletableFuture<Integer>();
3069 >        f = new CompletableFuture<>();
3070          g = f.thenComposeAsync(r = new CompletableFutureInc());
3071          assertTrue(f.cancel(true));
3072          checkCompletedWithWrappedCancellationException(g);
3073  
3074 <        f = new CompletableFuture<Integer>();
3074 >        f = new CompletableFuture<>();
3075          assertTrue(f.cancel(true));
3076          g = f.thenComposeAsync(r = new CompletableFutureInc());
3077          checkCompletedWithWrappedCancellationException(g);
3078      }
3079  
1873
3080      // async with explicit executors
3081  
3082      /**
3083       * thenRunAsync result completes normally after normal completion of source
3084       */
3085      public void testThenRunAsyncE() {
3086 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3086 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3087          Noop r = new Noop();
3088          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3089          f.complete(null);
3090          checkCompletedNormally(g, null);
3091  
3092          // reordered version
3093 <        f = new CompletableFuture<Integer>();
3093 >        f = new CompletableFuture<>();
3094          f.complete(null);
3095          r = new Noop();
3096          g = f.thenRunAsync(r, new ThreadExecutor());
# Line 1896 | Line 3102 | public class CompletableFutureTest exten
3102       * completion of source
3103       */
3104      public void testThenRunAsync2E() {
3105 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3105 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3106          Noop r = new Noop();
3107          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3108          f.completeExceptionally(new CFException());
3109          try {
3110              g.join();
3111              shouldThrow();
3112 <        } catch (Exception ok) {
1907 <        }
3112 >        } catch (CompletionException success) {}
3113          checkCompletedWithWrappedCFException(g);
3114      }
3115  
# Line 1912 | Line 3117 | public class CompletableFutureTest exten
3117       * thenRunAsync result completes exceptionally if action does
3118       */
3119      public void testThenRunAsync3E() {
3120 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3120 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3121          FailingNoop r = new FailingNoop();
3122          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3123          f.complete(null);
# Line 1923 | Line 3128 | public class CompletableFutureTest exten
3128       * thenRunAsync result completes exceptionally if source cancelled
3129       */
3130      public void testThenRunAsync4E() {
3131 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3131 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3132          Noop r = new Noop();
3133          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3134          assertTrue(f.cancel(true));
# Line 1934 | Line 3139 | public class CompletableFutureTest exten
3139       * thenApplyAsync result completes normally after normal completion of source
3140       */
3141      public void testThenApplyAsyncE() {
3142 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3142 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3143          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3144          f.complete(one);
3145          checkCompletedNormally(g, two);
# Line 1945 | Line 3150 | public class CompletableFutureTest exten
3150       * completion of source
3151       */
3152      public void testThenApplyAsync2E() {
3153 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3153 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3154          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3155          f.completeExceptionally(new CFException());
3156          checkCompletedWithWrappedCFException(g);
# Line 1955 | Line 3160 | public class CompletableFutureTest exten
3160       * thenApplyAsync result completes exceptionally if action does
3161       */
3162      public void testThenApplyAsync3E() {
3163 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3163 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3164          FailingFunction r = new FailingFunction();
3165          CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3166          f.complete(null);
# Line 1966 | Line 3171 | public class CompletableFutureTest exten
3171       * thenApplyAsync result completes exceptionally if source cancelled
3172       */
3173      public void testThenApplyAsync4E() {
3174 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3174 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3175          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3176          assertTrue(f.cancel(true));
3177          checkCompletedWithWrappedCancellationException(g);
# Line 1977 | Line 3182 | public class CompletableFutureTest exten
3182       * completion of source
3183       */
3184      public void testThenAcceptAsyncE() {
3185 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3185 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3186          IncAction r = new IncAction();
3187          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3188          f.complete(one);
3189          checkCompletedNormally(g, null);
3190 <        assertEquals(r.value, 2);
3190 >        assertEquals(r.value, (Integer) 2);
3191      }
3192  
3193      /**
# Line 1990 | Line 3195 | public class CompletableFutureTest exten
3195       * completion of source
3196       */
3197      public void testThenAcceptAsync2E() {
3198 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3198 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3199          IncAction r = new IncAction();
3200          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3201          f.completeExceptionally(new CFException());
# Line 2001 | Line 3206 | public class CompletableFutureTest exten
3206       * thenAcceptAsync result completes exceptionally if action does
3207       */
3208      public void testThenAcceptAsync3E() {
3209 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3209 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3210          FailingConsumer r = new FailingConsumer();
3211          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3212          f.complete(null);
# Line 2012 | Line 3217 | public class CompletableFutureTest exten
3217       * thenAcceptAsync result completes exceptionally if source cancelled
3218       */
3219      public void testThenAcceptAsync4E() {
3220 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3220 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3221          IncAction r = new IncAction();
3222          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3223          assertTrue(f.cancel(true));
# Line 2020 | Line 3225 | public class CompletableFutureTest exten
3225      }
3226  
3227      /**
2023     * thenCombineAsync result completes normally after normal
2024     * completion of sources
2025     */
2026    public void testThenCombineAsyncE() {
2027        CompletableFuture<Integer> f, g, h;
2028        ThreadExecutor e = new ThreadExecutor();
2029        int count = 0;
2030
2031        f = new CompletableFuture<Integer>();
2032        g = new CompletableFuture<Integer>();
2033        h = f.thenCombineAsync(g, subtract, e);
2034        f.complete(3);
2035        checkIncomplete(h);
2036        g.complete(1);
2037        checkCompletedNormally(h, 2);
2038        assertEquals(++count, e.count.get());
2039
2040        f = new CompletableFuture<Integer>();
2041        g = new CompletableFuture<Integer>();
2042        h = f.thenCombineAsync(g, subtract, e);
2043        g.complete(1);
2044        checkIncomplete(h);
2045        f.complete(3);
2046        checkCompletedNormally(h, 2);
2047        assertEquals(++count, e.count.get());
2048
2049        f = new CompletableFuture<Integer>();
2050        g = new CompletableFuture<Integer>();
2051        g.complete(1);
2052        f.complete(3);
2053        h = f.thenCombineAsync(g, subtract, e);
2054        checkCompletedNormally(h, 2);
2055        assertEquals(++count, e.count.get());
2056    }
2057
2058    /**
2059     * thenCombineAsync result completes exceptionally after exceptional
2060     * completion of either source
2061     */
2062    public void testThenCombineAsync2E() {
2063        CompletableFuture<Integer> f, g, h;
2064        ThreadExecutor e = new ThreadExecutor();
2065        int count = 0;
2066
2067        f = new CompletableFuture<Integer>();
2068        g = new CompletableFuture<Integer>();
2069        h = f.thenCombineAsync(g, subtract, e);
2070        f.completeExceptionally(new CFException());
2071        checkIncomplete(h);
2072        g.complete(1);
2073        checkCompletedWithWrappedCFException(h);
2074
2075        f = new CompletableFuture<Integer>();
2076        g = new CompletableFuture<Integer>();
2077        h = f.thenCombineAsync(g, subtract, e);
2078        g.completeExceptionally(new CFException());
2079        checkIncomplete(h);
2080        f.complete(3);
2081        checkCompletedWithWrappedCFException(h);
2082
2083        f = new CompletableFuture<Integer>();
2084        g = new CompletableFuture<Integer>();
2085        g.completeExceptionally(new CFException());
2086        h = f.thenCombineAsync(g, subtract, e);
2087        checkIncomplete(h);
2088        f.complete(3);
2089        checkCompletedWithWrappedCFException(h);
2090
2091        assertEquals(0, e.count.get());
2092    }
2093
2094    /**
2095     * thenCombineAsync result completes exceptionally if action does
2096     */
2097    public void testThenCombineAsync3E() {
2098        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2099        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2100        FailingBiFunction r = new FailingBiFunction();
2101        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2102        f.complete(one);
2103        checkIncomplete(g);
2104        assertFalse(r.ran);
2105        f2.complete(two);
2106        checkCompletedWithWrappedCFException(g);
2107        assertTrue(r.ran);
2108    }
2109
2110    /**
2111     * thenCombineAsync result completes exceptionally if either source cancelled
2112     */
2113    public void testThenCombineAsync4E() {
2114        CompletableFuture<Integer> f, g, h;
2115        ThreadExecutor e = new ThreadExecutor();
2116
2117        f = new CompletableFuture<Integer>();
2118        g = new CompletableFuture<Integer>();
2119        h = f.thenCombineAsync(g, subtract, e);
2120        assertTrue(f.cancel(true));
2121        checkIncomplete(h);
2122        g.complete(1);
2123        checkCompletedWithWrappedCancellationException(h);
2124
2125        f = new CompletableFuture<Integer>();
2126        g = new CompletableFuture<Integer>();
2127        h = f.thenCombineAsync(g, subtract, e);
2128        assertTrue(g.cancel(true));
2129        checkIncomplete(h);
2130        f.complete(3);
2131        checkCompletedWithWrappedCancellationException(h);
2132
2133        f = new CompletableFuture<Integer>();
2134        g = new CompletableFuture<Integer>();
2135        assertTrue(g.cancel(true));
2136        h = f.thenCombineAsync(g, subtract, e);
2137        checkIncomplete(h);
2138        f.complete(3);
2139        checkCompletedWithWrappedCancellationException(h);
2140
2141        f = new CompletableFuture<Integer>();
2142        g = new CompletableFuture<Integer>();
2143        assertTrue(f.cancel(true));
2144        assertTrue(g.cancel(true));
2145        h = f.thenCombineAsync(g, subtract, e);
2146        checkCompletedWithWrappedCancellationException(h);
2147
2148        assertEquals(0, e.count.get());
2149    }
2150
2151    /**
2152     * thenAcceptBothAsync result completes normally after normal
2153     * completion of sources
2154     */
2155    public void testThenAcceptBothAsyncE() {
2156        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2157        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2158        AddAction r = new AddAction();
2159        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2160        f.complete(one);
2161        checkIncomplete(g);
2162        f2.complete(two);
2163        checkCompletedNormally(g, null);
2164        assertEquals(r.value, 3);
2165    }
2166
2167    /**
2168     * thenAcceptBothAsync result completes exceptionally after exceptional
2169     * completion of source
2170     */
2171    public void testThenAcceptBothAsync2E() {
2172        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2173        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2174        AddAction r = new AddAction();
2175        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2176        f.completeExceptionally(new CFException());
2177        f2.complete(two);
2178        checkCompletedWithWrappedCFException(g);
2179
2180        r = new AddAction();
2181        f = new CompletableFuture<Integer>();
2182        f2 = new CompletableFuture<Integer>();
2183        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2184        f.complete(one);
2185        f2.completeExceptionally(new CFException());
2186        checkCompletedWithWrappedCFException(g);
2187    }
2188
2189    /**
2190     * thenAcceptBothAsync result completes exceptionally if action does
2191     */
2192    public void testThenAcceptBothAsync3E() {
2193        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2194        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2195        FailingBiConsumer r = new FailingBiConsumer();
2196        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2197        f.complete(one);
2198        checkIncomplete(g);
2199        f2.complete(two);
2200        checkCompletedWithWrappedCFException(g);
2201    }
2202
2203    /**
2204     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2205     */
2206    public void testThenAcceptBothAsync4E() {
2207        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2208        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2209        AddAction r = new AddAction();
2210        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2211        assertTrue(f.cancel(true));
2212        f2.complete(two);
2213        checkCompletedWithWrappedCancellationException(g);
2214
2215        r = new AddAction();
2216        f = new CompletableFuture<Integer>();
2217        f2 = new CompletableFuture<Integer>();
2218        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2219        f.complete(one);
2220        assertTrue(f2.cancel(true));
2221        checkCompletedWithWrappedCancellationException(g);
2222    }
2223
2224    /**
2225     * runAfterBothAsync result completes normally after normal
2226     * completion of sources
2227     */
2228    public void testRunAfterBothAsyncE() {
2229        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2230        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2231        Noop r = new Noop();
2232        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2233        f.complete(one);
2234        checkIncomplete(g);
2235        f2.complete(two);
2236        checkCompletedNormally(g, null);
2237        assertTrue(r.ran);
2238    }
2239
2240    /**
2241     * runAfterBothAsync result completes exceptionally after exceptional
2242     * completion of source
2243     */
2244    public void testRunAfterBothAsync2E() {
2245        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2246        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2247        Noop r = new Noop();
2248        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2249        f.completeExceptionally(new CFException());
2250        f2.complete(two);
2251        checkCompletedWithWrappedCFException(g);
2252
2253        r = new Noop();
2254        f = new CompletableFuture<Integer>();
2255        f2 = new CompletableFuture<Integer>();
2256        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2257        f.complete(one);
2258        f2.completeExceptionally(new CFException());
2259        checkCompletedWithWrappedCFException(g);
2260    }
2261
2262    /**
2263     * runAfterBothAsync result completes exceptionally if action does
2264     */
2265    public void testRunAfterBothAsync3E() {
2266        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2267        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2268        FailingNoop r = new FailingNoop();
2269        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2270        f.complete(one);
2271        checkIncomplete(g);
2272        f2.complete(two);
2273        checkCompletedWithWrappedCFException(g);
2274    }
2275
2276    /**
2277     * runAfterBothAsync result completes exceptionally if either source cancelled
2278     */
2279    public void testRunAfterBothAsync4E() {
2280        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2281        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2282        Noop r = new Noop();
2283        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2284        assertTrue(f.cancel(true));
2285        f2.complete(two);
2286        checkCompletedWithWrappedCancellationException(g);
2287
2288        r = new Noop();
2289        f = new CompletableFuture<Integer>();
2290        f2 = new CompletableFuture<Integer>();
2291        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2292        f.complete(one);
2293        assertTrue(f2.cancel(true));
2294        checkCompletedWithWrappedCancellationException(g);
2295    }
2296
2297    /**
2298     * applyToEitherAsync result completes normally after normal
2299     * completion of sources
2300     */
2301    public void testApplyToEitherAsyncE() {
2302        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2303        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2304        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2305        f.complete(one);
2306        checkCompletedNormally(g, two);
2307
2308        f = new CompletableFuture<Integer>();
2309        f.complete(one);
2310        f2 = new CompletableFuture<Integer>();
2311        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2312        checkCompletedNormally(g, two);
2313    }
2314
2315    /**
2316     * applyToEitherAsync result completes exceptionally after exceptional
2317     * completion of source
2318     */
2319    public void testApplyToEitherAsync2E() {
2320        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2321        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2322        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2323        f.completeExceptionally(new CFException());
2324        checkCompletedWithWrappedCFException(g);
2325
2326        f = new CompletableFuture<Integer>();
2327        f2 = new CompletableFuture<Integer>();
2328        f2.completeExceptionally(new CFException());
2329        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2330        f.complete(one);
2331        checkCompletedWithWrappedCFException(g);
2332    }
2333
2334    /**
2335     * applyToEitherAsync result completes exceptionally if action does
2336     */
2337    public void testApplyToEitherAsync3E() {
2338        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2339        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2340        FailingFunction r = new FailingFunction();
2341        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2342        f.complete(one);
2343        checkCompletedWithWrappedCFException(g);
2344    }
2345
2346    /**
2347     * applyToEitherAsync result completes exceptionally if either source cancelled
2348     */
2349    public void testApplyToEitherAsync4E() {
2350        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2351        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2352        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2353        assertTrue(f.cancel(true));
2354        checkCompletedWithWrappedCancellationException(g);
2355
2356        f = new CompletableFuture<Integer>();
2357        f2 = new CompletableFuture<Integer>();
2358        assertTrue(f2.cancel(true));
2359        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2360        checkCompletedWithWrappedCancellationException(g);
2361    }
2362
2363    /**
2364     * acceptEitherAsync result completes normally after normal
2365     * completion of sources
2366     */
2367    public void testAcceptEitherAsyncE() {
2368        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2369        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2370        IncAction r = new IncAction();
2371        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2372        f.complete(one);
2373        checkCompletedNormally(g, null);
2374        assertEquals(r.value, 2);
2375
2376        r = new IncAction();
2377        f = new CompletableFuture<Integer>();
2378        f.complete(one);
2379        f2 = new CompletableFuture<Integer>();
2380        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2381        checkCompletedNormally(g, null);
2382        assertEquals(r.value, 2);
2383    }
2384
2385    /**
2386     * acceptEitherAsync result completes exceptionally after exceptional
2387     * completion of source
2388     */
2389    public void testAcceptEitherAsync2E() {
2390        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2391        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2392        IncAction r = new IncAction();
2393        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2394        f.completeExceptionally(new CFException());
2395        checkCompletedWithWrappedCFException(g);
2396
2397        r = new IncAction();
2398        f = new CompletableFuture<Integer>();
2399        f2 = new CompletableFuture<Integer>();
2400        f2.completeExceptionally(new CFException());
2401        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2402        f.complete(one);
2403        checkCompletedWithWrappedCFException(g);
2404    }
2405
2406    /**
2407     * acceptEitherAsync result completes exceptionally if action does
2408     */
2409    public void testAcceptEitherAsync3E() {
2410        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2411        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2412        FailingConsumer r = new FailingConsumer();
2413        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2414        f.complete(one);
2415        checkCompletedWithWrappedCFException(g);
2416    }
2417
2418    /**
2419     * acceptEitherAsync result completes exceptionally if either
2420     * source cancelled
2421     */
2422    public void testAcceptEitherAsync4E() {
2423        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2424        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2425        IncAction r = new IncAction();
2426        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2427        assertTrue(f.cancel(true));
2428        checkCompletedWithWrappedCancellationException(g);
2429
2430        r = new IncAction();
2431        f = new CompletableFuture<Integer>();
2432        f2 = new CompletableFuture<Integer>();
2433        assertTrue(f2.cancel(true));
2434        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2435        checkCompletedWithWrappedCancellationException(g);
2436    }
2437
2438    /**
2439     * runAfterEitherAsync result completes normally after normal
2440     * completion of sources
2441     */
2442    public void testRunAfterEitherAsyncE() {
2443        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2444        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2445        Noop r = new Noop();
2446        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2447        f.complete(one);
2448        checkCompletedNormally(g, null);
2449        assertTrue(r.ran);
2450
2451        r = new Noop();
2452        f = new CompletableFuture<Integer>();
2453        f.complete(one);
2454        f2 = new CompletableFuture<Integer>();
2455        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2456        checkCompletedNormally(g, null);
2457        assertTrue(r.ran);
2458    }
2459
2460    /**
2461     * runAfterEitherAsync result completes exceptionally after exceptional
2462     * completion of source
2463     */
2464    public void testRunAfterEitherAsync2E() {
2465        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2466        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2467        Noop r = new Noop();
2468        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2469        f.completeExceptionally(new CFException());
2470        checkCompletedWithWrappedCFException(g);
2471
2472        r = new Noop();
2473        f = new CompletableFuture<Integer>();
2474        f2 = new CompletableFuture<Integer>();
2475        f2.completeExceptionally(new CFException());
2476        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2477        f.complete(one);
2478        checkCompletedWithWrappedCFException(g);
2479    }
2480
2481    /**
2482     * runAfterEitherAsync result completes exceptionally if action does
2483     */
2484    public void testRunAfterEitherAsync3E() {
2485        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2486        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2487        FailingNoop r = new FailingNoop();
2488        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2489        f.complete(one);
2490        checkCompletedWithWrappedCFException(g);
2491    }
2492
2493    /**
2494     * runAfterEitherAsync result completes exceptionally if either
2495     * source cancelled
2496     */
2497    public void testRunAfterEitherAsync4E() {
2498        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2499        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2500        Noop r = new Noop();
2501        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2502        assertTrue(f.cancel(true));
2503        checkCompletedWithWrappedCancellationException(g);
2504
2505        r = new Noop();
2506        f = new CompletableFuture<Integer>();
2507        f2 = new CompletableFuture<Integer>();
2508        assertTrue(f2.cancel(true));
2509        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2510        checkCompletedWithWrappedCancellationException(g);
2511    }
2512
2513    /**
3228       * thenComposeAsync result completes normally after normal
3229       * completion of source
3230       */
3231      public void testThenComposeAsyncE() {
3232 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3232 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3233          CompletableFutureInc r = new CompletableFutureInc();
3234          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3235          f.complete(one);
# Line 2527 | Line 3241 | public class CompletableFutureTest exten
3241       * exceptional completion of source
3242       */
3243      public void testThenComposeAsync2E() {
3244 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3244 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3245          CompletableFutureInc r = new CompletableFutureInc();
3246          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3247          f.completeExceptionally(new CFException());
# Line 2538 | Line 3252 | public class CompletableFutureTest exten
3252       * thenComposeAsync result completes exceptionally if action does
3253       */
3254      public void testThenComposeAsync3E() {
3255 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3255 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3256          FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3257          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3258          f.complete(one);
# Line 2549 | Line 3263 | public class CompletableFutureTest exten
3263       * thenComposeAsync result completes exceptionally if source cancelled
3264       */
3265      public void testThenComposeAsync4E() {
3266 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3266 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3267          CompletableFutureInc r = new CompletableFutureInc();
3268          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3269          assertTrue(f.cancel(true));
# Line 2563 | Line 3277 | public class CompletableFutureTest exten
3277       * with the value null
3278       */
3279      public void testAllOf_empty() throws Exception {
3280 <        CompletableFuture<?> f = CompletableFuture.allOf();
3280 >        CompletableFuture<Void> f = CompletableFuture.allOf();
3281          checkCompletedNormally(f, null);
3282      }
3283  
3284      /**
3285 <     * allOf returns a future completed when all components complete
3285 >     * allOf returns a future completed normally with the value null
3286 >     * when all components complete normally
3287       */
3288 <    public void testAllOf() throws Exception {
3288 >    public void testAllOf_normal() throws Exception {
3289          for (int k = 1; k < 20; ++k) {
3290 <            CompletableFuture[] fs = new CompletableFuture[k];
3290 >            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3291              for (int i = 0; i < k; ++i)
3292 <                fs[i] = new CompletableFuture<Integer>();
3292 >                fs[i] = new CompletableFuture<>();
3293              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3294              for (int i = 0; i < k; ++i) {
3295                  checkIncomplete(f);
3296 +                checkIncomplete(CompletableFuture.allOf(fs));
3297                  fs[i].complete(one);
3298              }
3299              checkCompletedNormally(f, null);
3300 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3301          }
3302      }
3303  
# Line 2588 | Line 3305 | public class CompletableFutureTest exten
3305       * anyOf(no component futures) returns an incomplete future
3306       */
3307      public void testAnyOf_empty() throws Exception {
3308 <        CompletableFuture<?> f = CompletableFuture.anyOf();
3308 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3309          checkIncomplete(f);
3310      }
3311  
3312      /**
3313 <     * anyOf returns a future completed when any components complete
3313 >     * anyOf returns a future completed normally with a value when
3314 >     * a component future does
3315       */
3316 <    public void testAnyOf() throws Exception {
3317 <        for (int k = 1; k < 20; ++k) {
3316 >    public void testAnyOf_normal() throws Exception {
3317 >        for (int k = 0; k < 10; ++k) {
3318              CompletableFuture[] fs = new CompletableFuture[k];
3319              for (int i = 0; i < k; ++i)
3320 <                fs[i] = new CompletableFuture<Integer>();
3320 >                fs[i] = new CompletableFuture<>();
3321              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3322              checkIncomplete(f);
3323              for (int i = 0; i < k; ++i) {
3324                  fs[i].complete(one);
3325                  checkCompletedNormally(f, one);
3326 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3327 +            }
3328 +        }
3329 +    }
3330 +
3331 +    /**
3332 +     * anyOf result completes exceptionally when any component does.
3333 +     */
3334 +    public void testAnyOf_exceptional() throws Exception {
3335 +        for (int k = 0; k < 10; ++k) {
3336 +            CompletableFuture[] fs = new CompletableFuture[k];
3337 +            for (int i = 0; i < k; ++i)
3338 +                fs[i] = new CompletableFuture<>();
3339 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3340 +            checkIncomplete(f);
3341 +            for (int i = 0; i < k; ++i) {
3342 +                fs[i].completeExceptionally(new CFException());
3343 +                checkCompletedWithWrappedCFException(f);
3344 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3345              }
3346          }
3347      }
# Line 2613 | Line 3350 | public class CompletableFutureTest exten
3350       * Completion methods throw NullPointerException with null arguments
3351       */
3352      public void testNPE() {
3353 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3354 <        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
3353 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3354 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3355          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3356          CompletableFuture<?> h;
3357          ThreadExecutor exec = new ThreadExecutor();
3358  
3359          Runnable[] throwingActions = {
3360 <            () -> { CompletableFuture.supplyAsync(null); },
3361 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3362 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3363 <
3364 <            () -> { CompletableFuture.runAsync(null); },
3365 <            () -> { CompletableFuture.runAsync(null, exec); },
3366 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3367 <
3368 <            () -> { f.completeExceptionally(null); },
3369 <
3370 <            () -> { f.thenApply(null); },
3371 <            () -> { f.thenApplyAsync(null); },
3372 <            () -> { f.thenApplyAsync((x) -> x, null); },
3373 <            () -> { f.thenApplyAsync(null, exec); },
3374 <
3375 <            () -> { f.thenAccept(null); },
3376 <            () -> { f.thenAcceptAsync(null); },
3377 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3378 <            () -> { f.thenAcceptAsync(null, exec); },
3379 <
3380 <            () -> { f.thenRun(null); },
3381 <            () -> { f.thenRunAsync(null); },
3382 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3383 <            () -> { f.thenRunAsync(null, exec); },
3384 <
3385 <            () -> { f.thenCombine(g, null); },
3386 <            () -> { f.thenCombineAsync(g, null); },
3387 <            () -> { f.thenCombineAsync(g, null, exec); },
3388 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3389 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3390 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3391 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3392 <
3393 <            () -> { f.thenAcceptBoth(g, null); },
3394 <            () -> { f.thenAcceptBothAsync(g, null); },
3395 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3396 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3397 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3398 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3399 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3400 <
3401 <            () -> { f.runAfterBoth(g, null); },
3402 <            () -> { f.runAfterBothAsync(g, null); },
3403 <            () -> { f.runAfterBothAsync(g, null, exec); },
3404 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3405 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3406 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3407 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3408 <
3409 <            () -> { f.applyToEither(g, null); },
3410 <            () -> { f.applyToEitherAsync(g, null); },
3411 <            () -> { f.applyToEitherAsync(g, null, exec); },
3412 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3413 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3414 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3415 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3416 <
3417 <            () -> { f.acceptEither(g, null); },
3418 <            () -> { f.acceptEitherAsync(g, null); },
3419 <            () -> { f.acceptEitherAsync(g, null, exec); },
3420 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3421 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3422 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3423 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3424 <
3425 <            () -> { f.runAfterEither(g, null); },
3426 <            () -> { f.runAfterEitherAsync(g, null); },
3427 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3428 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3429 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3430 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3431 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3432 <
3433 <            () -> { f.thenCompose(null); },
3434 <            () -> { f.thenComposeAsync(null); },
3435 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3436 <            () -> { f.thenComposeAsync(null, exec); },
3437 <
3438 <            () -> { f.exceptionally(null); },
3439 <
3440 <            () -> { f.handle(null); },
3441 <
3442 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3443 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3444 <            () -> { CompletableFuture.allOf(f, null); },
3445 <            () -> { CompletableFuture.allOf(null, f); },
3446 <
3447 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3448 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3449 <            () -> { CompletableFuture.anyOf(f, null); },
3450 <            () -> { CompletableFuture.anyOf(null, f); },
3360 >            () -> CompletableFuture.supplyAsync(null),
3361 >            () -> CompletableFuture.supplyAsync(null, exec),
3362 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
3363 >
3364 >            () -> CompletableFuture.runAsync(null),
3365 >            () -> CompletableFuture.runAsync(null, exec),
3366 >            () -> CompletableFuture.runAsync(() -> {}, null),
3367 >
3368 >            () -> f.completeExceptionally(null),
3369 >
3370 >            () -> f.thenApply(null),
3371 >            () -> f.thenApplyAsync(null),
3372 >            () -> f.thenApplyAsync((x) -> x, null),
3373 >            () -> f.thenApplyAsync(null, exec),
3374 >
3375 >            () -> f.thenAccept(null),
3376 >            () -> f.thenAcceptAsync(null),
3377 >            () -> f.thenAcceptAsync((x) -> {} , null),
3378 >            () -> f.thenAcceptAsync(null, exec),
3379 >
3380 >            () -> f.thenRun(null),
3381 >            () -> f.thenRunAsync(null),
3382 >            () -> f.thenRunAsync(() -> {} , null),
3383 >            () -> f.thenRunAsync(null, exec),
3384 >
3385 >            () -> f.thenCombine(g, null),
3386 >            () -> f.thenCombineAsync(g, null),
3387 >            () -> f.thenCombineAsync(g, null, exec),
3388 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3389 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3390 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3391 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3392 >
3393 >            () -> f.thenAcceptBoth(g, null),
3394 >            () -> f.thenAcceptBothAsync(g, null),
3395 >            () -> f.thenAcceptBothAsync(g, null, exec),
3396 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3397 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3398 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3399 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3400 >
3401 >            () -> f.runAfterBoth(g, null),
3402 >            () -> f.runAfterBothAsync(g, null),
3403 >            () -> f.runAfterBothAsync(g, null, exec),
3404 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3405 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3406 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3407 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3408 >
3409 >            () -> f.applyToEither(g, null),
3410 >            () -> f.applyToEitherAsync(g, null),
3411 >            () -> f.applyToEitherAsync(g, null, exec),
3412 >            () -> f.applyToEither(nullFuture, (x) -> x),
3413 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3414 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3415 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3416 >
3417 >            () -> f.acceptEither(g, null),
3418 >            () -> f.acceptEitherAsync(g, null),
3419 >            () -> f.acceptEitherAsync(g, null, exec),
3420 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3421 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3422 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3423 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3424 >
3425 >            () -> f.runAfterEither(g, null),
3426 >            () -> f.runAfterEitherAsync(g, null),
3427 >            () -> f.runAfterEitherAsync(g, null, exec),
3428 >            () -> f.runAfterEither(nullFuture, () -> {}),
3429 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3430 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3431 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3432 >
3433 >            () -> f.thenCompose(null),
3434 >            () -> f.thenComposeAsync(null),
3435 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3436 >            () -> f.thenComposeAsync(null, exec),
3437 >
3438 >            () -> f.exceptionally(null),
3439 >
3440 >            () -> f.handle(null),
3441 >
3442 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3443 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3444 >            () -> CompletableFuture.allOf(f, null),
3445 >            () -> CompletableFuture.allOf(null, f),
3446 >
3447 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3448 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3449 >            () -> CompletableFuture.anyOf(f, null),
3450 >            () -> CompletableFuture.anyOf(null, f),
3451 >
3452 >            () -> f.obtrudeException(null),
3453          };
3454  
3455          assertThrows(NullPointerException.class, throwingActions);
3456          assertEquals(0, exec.count.get());
3457      }
3458  
3459 +    /**
3460 +     * toCompletableFuture returns this CompletableFuture.
3461 +     */
3462 +    public void testToCompletableFuture() {
3463 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3464 +        assertSame(f, f.toCompletableFuture());
3465 +    }
3466 +
3467 +    /**
3468 +     * whenComplete action executes on normal completion, propagating
3469 +     * source result.
3470 +     */
3471 +    public void testWhenComplete_normalCompletion1() {
3472 +        for (ExecutionMode m : ExecutionMode.values())
3473 +        for (Integer v1 : new Integer[] { 1, null }) {
3474 +
3475 +        final AtomicInteger a = new AtomicInteger();
3476 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3477 +        final CompletableFuture<Integer> g =
3478 +            m.whenComplete(f,
3479 +                           (Integer x, Throwable t) -> {
3480 +                               threadAssertSame(x, v1);
3481 +                               threadAssertNull(t);
3482 +                               a.getAndIncrement();
3483 +                           });
3484 +        f.complete(v1);
3485 +        checkCompletedNormally(f, v1);
3486 +        checkCompletedNormally(g, v1);
3487 +        assertEquals(a.get(), 1);
3488 +        }
3489 +    }
3490 +
3491 +    /**
3492 +     * whenComplete action executes on exceptional completion, propagating
3493 +     * source result.
3494 +     */
3495 +    public void testWhenComplete_exceptionalCompletion() {
3496 +        for (ExecutionMode m : ExecutionMode.values())
3497 +        for (Integer v1 : new Integer[] { 1, null }) {
3498 +
3499 +        final AtomicInteger a = new AtomicInteger();
3500 +        final CFException ex = new CFException();
3501 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3502 +        final CompletableFuture<Integer> g = m.whenComplete
3503 +            (f,
3504 +             (Integer x, Throwable t) -> {
3505 +                threadAssertNull(x);
3506 +                threadAssertSame(t, ex);
3507 +                a.getAndIncrement();
3508 +            });
3509 +        f.completeExceptionally(ex);
3510 +        checkCompletedWithWrappedCFException(f, ex);
3511 +        checkCompletedWithWrappedCFException(g, ex);
3512 +        assertEquals(a.get(), 1);
3513 +        }
3514 +    }
3515 +
3516 +    /**
3517 +     * If a whenComplete action throws an exception when triggered by
3518 +     * a normal completion, it completes exceptionally
3519 +     */
3520 +    public void testWhenComplete_actionFailed() {
3521 +        for (ExecutionMode m : ExecutionMode.values())
3522 +        for (Integer v1 : new Integer[] { 1, null }) {
3523 +
3524 +        final CFException ex = new CFException();
3525 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3526 +        final CompletableFuture<Integer> g = m.whenComplete
3527 +            (f,
3528 +             (Integer x, Throwable t) -> {
3529 +                threadAssertSame(x, v1);
3530 +                threadAssertNull(t);
3531 +                throw ex;
3532 +            });
3533 +        f.complete(v1);
3534 +        checkCompletedNormally(f, v1);
3535 +        checkCompletedWithWrappedCFException(g, ex);
3536 +        }
3537 +    }
3538 +
3539 +    /**
3540 +     * If a whenComplete action throws an exception when triggered by
3541 +     * a source completion that also throws an exception, the source
3542 +     * exception takes precedence.
3543 +     */
3544 +    public void testWhenComplete_actionFailedSourceFailed() {
3545 +        for (ExecutionMode m : ExecutionMode.values())
3546 +        for (Integer v1 : new Integer[] { 1, null }) {
3547 +
3548 +        final CFException ex1 = new CFException();
3549 +        final CFException ex2 = new CFException();
3550 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3551 +        final CompletableFuture<Integer> g = m.whenComplete
3552 +            (f,
3553 +             (Integer x, Throwable t) -> {
3554 +                threadAssertSame(t, ex1);
3555 +                threadAssertNull(x);
3556 +                throw ex2;
3557 +            });
3558 +        f.completeExceptionally(ex1);
3559 +        checkCompletedWithWrappedCFException(f, ex1);
3560 +        checkCompletedWithWrappedCFException(g, ex1);
3561 +        }
3562 +    }
3563 +
3564 +    /**
3565 +     * handleAsync action completes normally with function value on
3566 +     * either normal or exceptional completion of source
3567 +     */
3568 +    public void testHandleAsync() {
3569 +        CompletableFuture<Integer> f, g;
3570 +        IntegerHandler r;
3571 +
3572 +        f = new CompletableFuture<>();
3573 +        g = f.handleAsync(r = new IntegerHandler());
3574 +        assertFalse(r.ran);
3575 +        f.completeExceptionally(new CFException());
3576 +        checkCompletedWithWrappedCFException(f);
3577 +        checkCompletedNormally(g, three);
3578 +        assertTrue(r.ran);
3579 +
3580 +        f = new CompletableFuture<>();
3581 +        g = f.handleAsync(r = new IntegerHandler());
3582 +        assertFalse(r.ran);
3583 +        f.completeExceptionally(new CFException());
3584 +        checkCompletedWithWrappedCFException(f);
3585 +        checkCompletedNormally(g, three);
3586 +        assertTrue(r.ran);
3587 +
3588 +        f = new CompletableFuture<>();
3589 +        g = f.handleAsync(r = new IntegerHandler());
3590 +        assertFalse(r.ran);
3591 +        f.complete(one);
3592 +        checkCompletedNormally(f, one);
3593 +        checkCompletedNormally(g, two);
3594 +        assertTrue(r.ran);
3595 +
3596 +        f = new CompletableFuture<>();
3597 +        g = f.handleAsync(r = new IntegerHandler());
3598 +        assertFalse(r.ran);
3599 +        f.complete(one);
3600 +        checkCompletedNormally(f, one);
3601 +        checkCompletedNormally(g, two);
3602 +        assertTrue(r.ran);
3603 +    }
3604 +
3605 +    /**
3606 +     * handleAsync action with Executor completes normally with
3607 +     * function value on either normal or exceptional completion of
3608 +     * source
3609 +     */
3610 +    public void testHandleAsync2() {
3611 +        CompletableFuture<Integer> f, g;
3612 +        ThreadExecutor exec = new ThreadExecutor();
3613 +        IntegerHandler r;
3614 +
3615 +        f = new CompletableFuture<>();
3616 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3617 +        assertFalse(r.ran);
3618 +        f.completeExceptionally(new CFException());
3619 +        checkCompletedWithWrappedCFException(f);
3620 +        checkCompletedNormally(g, three);
3621 +        assertTrue(r.ran);
3622 +
3623 +        f = new CompletableFuture<>();
3624 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3625 +        assertFalse(r.ran);
3626 +        f.completeExceptionally(new CFException());
3627 +        checkCompletedWithWrappedCFException(f);
3628 +        checkCompletedNormally(g, three);
3629 +        assertTrue(r.ran);
3630 +
3631 +        f = new CompletableFuture<>();
3632 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3633 +        assertFalse(r.ran);
3634 +        f.complete(one);
3635 +        checkCompletedNormally(f, one);
3636 +        checkCompletedNormally(g, two);
3637 +        assertTrue(r.ran);
3638 +
3639 +        f = new CompletableFuture<>();
3640 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3641 +        assertFalse(r.ran);
3642 +        f.complete(one);
3643 +        checkCompletedNormally(f, one);
3644 +        checkCompletedNormally(g, two);
3645 +        assertTrue(r.ran);
3646 +    }
3647 +
3648   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines