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.22 by jsr166, Mon Apr 8 20:46:59 2013 UTC vs.
Revision 1.47 by jsr166, Mon Jun 2 18:21:34 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 212 | Line 247 | public class CompletableFutureTest exten
247          f = new CompletableFuture<>();
248          f.obtrudeValue(three);
249          checkCompletedNormally(f, three);
250 +        f.obtrudeValue(null);
251 +        checkCompletedNormally(f, null);
252          f = new CompletableFuture<>();
253          f.completeExceptionally(new CFException());
254          f.obtrudeValue(four);
# Line 244 | Line 281 | public class CompletableFutureTest exten
281       */
282      public void testGetNumberOfDependents() {
283          CompletableFuture<Integer> f = new CompletableFuture<>();
284 <        assertEquals(f.getNumberOfDependents(), 0);
284 >        assertEquals(0, f.getNumberOfDependents());
285          CompletableFuture g = f.thenRun(new Noop());
286 <        assertEquals(f.getNumberOfDependents(), 1);
287 <        assertEquals(g.getNumberOfDependents(), 0);
286 >        assertEquals(1, f.getNumberOfDependents());
287 >        assertEquals(0, g.getNumberOfDependents());
288          CompletableFuture h = f.thenRun(new Noop());
289 <        assertEquals(f.getNumberOfDependents(), 2);
289 >        assertEquals(2, f.getNumberOfDependents());
290          f.complete(1);
291          checkCompletedNormally(g, null);
292 <        assertEquals(f.getNumberOfDependents(), 0);
293 <        assertEquals(g.getNumberOfDependents(), 0);
292 >        assertEquals(0, f.getNumberOfDependents());
293 >        assertEquals(0, g.getNumberOfDependents());
294      }
295  
259
296      /**
297       * toString indicates current completion state
298       */
# Line 284 | Line 320 | public class CompletableFutureTest exten
320  
321      // Choose non-commutative actions for better coverage
322  
323 +    // A non-commutative function that handles and produces null values as well.
324 +    static Integer subtract(Integer x, Integer y) {
325 +        return (x == null && y == null) ? null :
326 +            ((x == null) ? 42 : x.intValue())
327 +            - ((y == null) ? 99 : y.intValue());
328 +    }
329 +
330 +    // A function that handles and produces null values as well.
331 +    static Integer inc(Integer x) {
332 +        return (x == null) ? null : x + 1;
333 +    }
334 +
335      static final Supplier<Integer> supplyOne =
336          () -> Integer.valueOf(1);
337      static final Function<Integer, Integer> inc =
338          (Integer x) -> Integer.valueOf(x.intValue() + 1);
339      static final BiFunction<Integer, Integer, Integer> subtract =
340 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
340 >        (Integer x, Integer y) -> subtract(x, y);
341      static final class IncAction implements Consumer<Integer> {
342 <        int value;
343 <        public void accept(Integer x) { value = x.intValue() + 1; }
342 >        int invocationCount = 0;
343 >        Integer value;
344 >        public void accept(Integer x) {
345 >            invocationCount++;
346 >            value = inc(x);
347 >        }
348 >    }
349 >    static final class IncFunction implements Function<Integer,Integer> {
350 >        int invocationCount = 0;
351 >        Integer value;
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356      }
357      static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 <        int value;
358 >        int invocationCount = 0;
359 >        Integer value;
360 >        // Check this action was invoked exactly once when result is computed.
361          public void accept(Integer x, Integer y) {
362 <            value = x.intValue() - y.intValue();
362 >            invocationCount++;
363 >            value = subtract(x, y);
364 >        }
365 >    }
366 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
367 >        int invocationCount = 0;
368 >        Integer value;
369 >        // Check this action was invoked exactly once when result is computed.
370 >        public Integer apply(Integer x, Integer y) {
371 >            invocationCount++;
372 >            return value = subtract(x, y);
373          }
374      }
375      static final class Noop implements Runnable {
376 <        boolean ran;
377 <        public void run() { ran = true; }
376 >        int invocationCount = 0;
377 >        public void run() {
378 >            invocationCount++;
379 >        }
380      }
381  
382      static final class FailingSupplier implements Supplier<Integer> {
383 <        boolean ran;
384 <        public Integer get() { ran = true; throw new CFException(); }
383 >        int invocationCount = 0;
384 >        public Integer get() {
385 >            invocationCount++;
386 >            throw new CFException();
387 >        }
388      }
389      static final class FailingConsumer implements Consumer<Integer> {
390 <        boolean ran;
391 <        public void accept(Integer x) { ran = true; throw new CFException(); }
390 >        int invocationCount = 0;
391 >        public void accept(Integer x) {
392 >            invocationCount++;
393 >            throw new CFException();
394 >        }
395      }
396      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
397 <        boolean ran;
398 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
397 >        int invocationCount = 0;
398 >        public void accept(Integer x, Integer y) {
399 >            invocationCount++;
400 >            throw new CFException();
401 >        }
402      }
403      static final class FailingFunction implements Function<Integer, Integer> {
404 <        boolean ran;
405 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
404 >        int invocationCount = 0;
405 >        public Integer apply(Integer x) {
406 >            invocationCount++;
407 >            throw new CFException();
408 >        }
409      }
410      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
411 <        boolean ran;
412 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
411 >        int invocationCount = 0;
412 >        public Integer apply(Integer x, Integer y) {
413 >            invocationCount++;
414 >            throw new CFException();
415 >        }
416      }
417      static final class FailingNoop implements Runnable {
418 <        boolean ran;
419 <        public void run() { ran = true; throw new CFException(); }
418 >        int invocationCount = 0;
419 >        public void run() {
420 >            invocationCount++;
421 >            throw new CFException();
422 >        }
423      }
424  
425      static final class CompletableFutureInc
426          implements Function<Integer, CompletableFuture<Integer>> {
427 <        boolean ran;
427 >        int invocationCount = 0;
428          public CompletableFuture<Integer> apply(Integer x) {
429 <            ran = true;
429 >            invocationCount++;
430              CompletableFuture<Integer> f = new CompletableFuture<>();
431 <            f.complete(Integer.valueOf(x.intValue() + 1));
431 >            f.complete(inc(x));
432              return f;
433          }
434      }
435  
436      static final class FailingCompletableFutureFunction
437          implements Function<Integer, CompletableFuture<Integer>> {
438 <        boolean ran;
438 >        int invocationCount = 0;
439          public CompletableFuture<Integer> apply(Integer x) {
440 <            ran = true; throw new CFException();
440 >            invocationCount++;
441 >            throw new CFException();
442          }
443      }
444  
# Line 359 | Line 452 | public class CompletableFutureTest exten
452          }
453      }
454  
455 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
456 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
457 <    }
455 >    /**
456 >     * Permits the testing of parallel code for the 3 different
457 >     * execution modes without repeating all the testing code.
458 >     */
459 >    enum ExecutionMode {
460 >        DEFAULT {
461 >            public <T> CompletableFuture<Void> thenRun
462 >                (CompletableFuture<T> f, Runnable a) {
463 >                return f.thenRun(a);
464 >            }
465 >            public <T> CompletableFuture<Void> thenAccept
466 >                (CompletableFuture<T> f, Consumer<? super T> a) {
467 >                return f.thenAccept(a);
468 >            }
469 >            public <T,U> CompletableFuture<U> thenApply
470 >                (CompletableFuture<T> f, Function<? super T,U> a) {
471 >                return f.thenApply(a);
472 >            }
473 >            public <T,U> CompletableFuture<U> thenCompose
474 >                (CompletableFuture<T> f,
475 >                 Function<? super T,? extends CompletionStage<U>> a) {
476 >                return f.thenCompose(a);
477 >            }
478 >            public <T,U> CompletableFuture<U> handle
479 >                (CompletableFuture<T> f,
480 >                 BiFunction<? super T,Throwable,? extends U> a) {
481 >                return f.handle(a);
482 >            }
483 >            public <T> CompletableFuture<T> whenComplete
484 >                (CompletableFuture<T> f,
485 >                 BiConsumer<? super T,? super Throwable> a) {
486 >                return f.whenComplete(a);
487 >            }
488 >            public <T,U> CompletableFuture<Void> runAfterBoth
489 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
490 >                return f.runAfterBoth(g, a);
491 >            }
492 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
493 >                (CompletableFuture<T> f,
494 >                 CompletionStage<? extends U> g,
495 >                 BiConsumer<? super T,? super U> a) {
496 >                return f.thenAcceptBoth(g, a);
497 >            }
498 >            public <T,U,V> CompletableFuture<V> thenCombine
499 >                (CompletableFuture<T> f,
500 >                 CompletionStage<? extends U> g,
501 >                 BiFunction<? super T,? super U,? extends V> a) {
502 >                return f.thenCombine(g, a);
503 >            }
504 >            public <T> CompletableFuture<Void> runAfterEither
505 >                (CompletableFuture<T> f,
506 >                 CompletionStage<?> g,
507 >                 java.lang.Runnable a) {
508 >                return f.runAfterEither(g, a);
509 >            }
510 >            public <T> CompletableFuture<Void> acceptEither
511 >                (CompletableFuture<T> f,
512 >                 CompletionStage<? extends T> g,
513 >                 Consumer<? super T> a) {
514 >                return f.acceptEither(g, a);
515 >            }
516 >            public <T,U> CompletableFuture<U> applyToEither
517 >                (CompletableFuture<T> f,
518 >                 CompletionStage<? extends T> g,
519 >                 Function<? super T,U> a) {
520 >                return f.applyToEither(g, a);
521 >            }
522 >        },
523  
524 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
525 <        boolean ran;
526 <        public Integer apply(Integer x, Throwable t) {
527 <            ran = true;
528 <            return (t == null) ? two : three;
529 <        }
524 >        DEFAULT_ASYNC {
525 >            public <T> CompletableFuture<Void> thenRun
526 >                (CompletableFuture<T> f, Runnable a) {
527 >                return f.thenRunAsync(a);
528 >            }
529 >            public <T> CompletableFuture<Void> thenAccept
530 >                (CompletableFuture<T> f, Consumer<? super T> a) {
531 >                return f.thenAcceptAsync(a);
532 >            }
533 >            public <T,U> CompletableFuture<U> thenApply
534 >                (CompletableFuture<T> f, Function<? super T,U> a) {
535 >                return f.thenApplyAsync(a);
536 >            }
537 >            public <T,U> CompletableFuture<U> thenCompose
538 >                (CompletableFuture<T> f,
539 >                 Function<? super T,? extends CompletionStage<U>> a) {
540 >                return f.thenComposeAsync(a);
541 >            }
542 >            public <T,U> CompletableFuture<U> handle
543 >                (CompletableFuture<T> f,
544 >                 BiFunction<? super T,Throwable,? extends U> a) {
545 >                return f.handleAsync(a);
546 >            }
547 >            public <T> CompletableFuture<T> whenComplete
548 >                (CompletableFuture<T> f,
549 >                 BiConsumer<? super T,? super Throwable> a) {
550 >                return f.whenCompleteAsync(a);
551 >            }
552 >            public <T,U> CompletableFuture<Void> runAfterBoth
553 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
554 >                return f.runAfterBothAsync(g, a);
555 >            }
556 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
557 >                (CompletableFuture<T> f,
558 >                 CompletionStage<? extends U> g,
559 >                 BiConsumer<? super T,? super U> a) {
560 >                return f.thenAcceptBothAsync(g, a);
561 >            }
562 >            public <T,U,V> CompletableFuture<V> thenCombine
563 >                (CompletableFuture<T> f,
564 >                 CompletionStage<? extends U> g,
565 >                 BiFunction<? super T,? super U,? extends V> a) {
566 >                return f.thenCombineAsync(g, a);
567 >            }
568 >            public <T> CompletableFuture<Void> runAfterEither
569 >                (CompletableFuture<T> f,
570 >                 CompletionStage<?> g,
571 >                 java.lang.Runnable a) {
572 >                return f.runAfterEitherAsync(g, a);
573 >            }
574 >            public <T> CompletableFuture<Void> acceptEither
575 >                (CompletableFuture<T> f,
576 >                 CompletionStage<? extends T> g,
577 >                 Consumer<? super T> a) {
578 >                return f.acceptEitherAsync(g, a);
579 >            }
580 >            public <T,U> CompletableFuture<U> applyToEither
581 >                (CompletableFuture<T> f,
582 >                 CompletionStage<? extends T> g,
583 >                 Function<? super T,U> a) {
584 >                return f.applyToEitherAsync(g, a);
585 >            }
586 >        },
587 >
588 >        EXECUTOR {
589 >            public <T> CompletableFuture<Void> thenRun
590 >                (CompletableFuture<T> f, Runnable a) {
591 >                return f.thenRunAsync(a, new ThreadExecutor());
592 >            }
593 >            public <T> CompletableFuture<Void> thenAccept
594 >                (CompletableFuture<T> f, Consumer<? super T> a) {
595 >                return f.thenAcceptAsync(a, new ThreadExecutor());
596 >            }
597 >            public <T,U> CompletableFuture<U> thenApply
598 >                (CompletableFuture<T> f, Function<? super T,U> a) {
599 >                return f.thenApplyAsync(a, new ThreadExecutor());
600 >            }
601 >            public <T,U> CompletableFuture<U> thenCompose
602 >                (CompletableFuture<T> f,
603 >                 Function<? super T,? extends CompletionStage<U>> a) {
604 >                return f.thenComposeAsync(a, new ThreadExecutor());
605 >            }
606 >            public <T,U> CompletableFuture<U> handle
607 >                (CompletableFuture<T> f,
608 >                 BiFunction<? super T,Throwable,? extends U> a) {
609 >                return f.handleAsync(a, new ThreadExecutor());
610 >            }
611 >            public <T> CompletableFuture<T> whenComplete
612 >                (CompletableFuture<T> f,
613 >                 BiConsumer<? super T,? super Throwable> a) {
614 >                return f.whenCompleteAsync(a, new ThreadExecutor());
615 >            }
616 >            public <T,U> CompletableFuture<Void> runAfterBoth
617 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
618 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
619 >            }
620 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
621 >                (CompletableFuture<T> f,
622 >                 CompletionStage<? extends U> g,
623 >                 BiConsumer<? super T,? super U> a) {
624 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
625 >            }
626 >            public <T,U,V> CompletableFuture<V> thenCombine
627 >                (CompletableFuture<T> f,
628 >                 CompletionStage<? extends U> g,
629 >                 BiFunction<? super T,? super U,? extends V> a) {
630 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
631 >            }
632 >            public <T> CompletableFuture<Void> runAfterEither
633 >                (CompletableFuture<T> f,
634 >                 CompletionStage<?> g,
635 >                 java.lang.Runnable a) {
636 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
637 >            }
638 >            public <T> CompletableFuture<Void> acceptEither
639 >                (CompletableFuture<T> f,
640 >                 CompletionStage<? extends T> g,
641 >                 Consumer<? super T> a) {
642 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
643 >            }
644 >            public <T,U> CompletableFuture<U> applyToEither
645 >                (CompletableFuture<T> f,
646 >                 CompletionStage<? extends T> g,
647 >                 Function<? super T,U> a) {
648 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
649 >            }
650 >        };
651 >
652 >        public abstract <T> CompletableFuture<Void> thenRun
653 >            (CompletableFuture<T> f, Runnable a);
654 >        public abstract <T> CompletableFuture<Void> thenAccept
655 >            (CompletableFuture<T> f, Consumer<? super T> a);
656 >        public abstract <T,U> CompletableFuture<U> thenApply
657 >            (CompletableFuture<T> f, Function<? super T,U> a);
658 >        public abstract <T,U> CompletableFuture<U> thenCompose
659 >            (CompletableFuture<T> f,
660 >             Function<? super T,? extends CompletionStage<U>> a);
661 >        public abstract <T,U> CompletableFuture<U> handle
662 >            (CompletableFuture<T> f,
663 >             BiFunction<? super T,Throwable,? extends U> a);
664 >        public abstract <T> CompletableFuture<T> whenComplete
665 >            (CompletableFuture<T> f,
666 >             BiConsumer<? super T,? super Throwable> a);
667 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
668 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
669 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
670 >            (CompletableFuture<T> f,
671 >             CompletionStage<? extends U> g,
672 >             BiConsumer<? super T,? super U> a);
673 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
674 >            (CompletableFuture<T> f,
675 >             CompletionStage<? extends U> g,
676 >             BiFunction<? super T,? super U,? extends V> a);
677 >        public abstract <T> CompletableFuture<Void> runAfterEither
678 >            (CompletableFuture<T> f,
679 >             CompletionStage<?> g,
680 >             java.lang.Runnable a);
681 >        public abstract <T> CompletableFuture<Void> acceptEither
682 >            (CompletableFuture<T> f,
683 >             CompletionStage<? extends T> g,
684 >             Consumer<? super T> a);
685 >        public abstract <T,U> CompletableFuture<U> applyToEither
686 >            (CompletableFuture<T> f,
687 >             CompletionStage<? extends T> g,
688 >             Function<? super T,U> a);
689      }
690  
691 +    /**
692 +     * exceptionally action is not invoked when source completes
693 +     * normally, and source result is propagated
694 +     */
695 +    public void testExceptionally_normalCompletion() {
696 +        for (boolean createIncomplete : new boolean[] { true, false })
697 +        for (Integer v1 : new Integer[] { 1, null })
698 +    {
699 +        final AtomicInteger a = new AtomicInteger(0);
700 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
701 +        if (!createIncomplete) f.complete(v1);
702 +        final CompletableFuture<Integer> g = f.exceptionally
703 +            ((Throwable t) -> {
704 +                // Should not be called
705 +                a.getAndIncrement();
706 +                throw new AssertionError();
707 +            });
708 +        if (createIncomplete) f.complete(v1);
709 +
710 +        checkCompletedNormally(g, v1);
711 +        checkCompletedNormally(f, v1);
712 +        assertEquals(0, a.get());
713 +    }}
714 +
715  
716      /**
717       * exceptionally action completes with function value on source
718 <     * exception;  otherwise with source value
718 >     * exception
719       */
720 <    public void testExceptionally() {
721 <        CompletableFuture<Integer> f = new CompletableFuture<>();
722 <        ExceptionToInteger r = new ExceptionToInteger();
723 <        CompletableFuture<Integer> g = f.exceptionally(r);
724 <        f.completeExceptionally(new CFException());
725 <        checkCompletedNormally(g, three);
726 <
727 <        f = new CompletableFuture<>();
728 <        r = new ExceptionToInteger();
729 <        g = f.exceptionally(r);
730 <        f.complete(one);
731 <        checkCompletedNormally(g, one);
732 <    }
720 >    public void testExceptionally_exceptionalCompletion() {
721 >        for (boolean createIncomplete : new boolean[] { true, false })
722 >        for (Integer v1 : new Integer[] { 1, null })
723 >    {
724 >        final AtomicInteger a = new AtomicInteger(0);
725 >        final CFException ex = new CFException();
726 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
727 >        if (!createIncomplete) f.completeExceptionally(ex);
728 >        final CompletableFuture<Integer> g = f.exceptionally
729 >            ((Throwable t) -> {
730 >                threadAssertSame(t, ex);
731 >                a.getAndIncrement();
732 >                return v1;
733 >            });
734 >        if (createIncomplete) f.completeExceptionally(ex);
735 >
736 >        checkCompletedNormally(g, v1);
737 >        assertEquals(1, a.get());
738 >    }}
739 >
740 >    public void testExceptionally_exceptionalCompletionActionFailed() {
741 >        for (boolean createIncomplete : new boolean[] { true, false })
742 >        for (Integer v1 : new Integer[] { 1, null })
743 >    {
744 >        final AtomicInteger a = new AtomicInteger(0);
745 >        final CFException ex1 = new CFException();
746 >        final CFException ex2 = new CFException();
747 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
748 >        if (!createIncomplete) f.completeExceptionally(ex1);
749 >        final CompletableFuture<Integer> g = f.exceptionally
750 >            ((Throwable t) -> {
751 >                threadAssertSame(t, ex1);
752 >                a.getAndIncrement();
753 >                throw ex2;
754 >            });
755 >        if (createIncomplete) f.completeExceptionally(ex1);
756 >
757 >        checkCompletedWithWrappedCFException(g, ex2);
758 >        assertEquals(1, a.get());
759 >    }}
760 >
761 >    /**
762 >     * handle action completes normally with function value on normal
763 >     * completion of source
764 >     */
765 >    public void testHandle_normalCompletion() {
766 >        for (ExecutionMode m : ExecutionMode.values())
767 >        for (boolean createIncomplete : new boolean[] { true, false })
768 >        for (Integer v1 : new Integer[] { 1, null })
769 >    {
770 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
771 >        final AtomicInteger a = new AtomicInteger(0);
772 >        if (!createIncomplete) f.complete(v1);
773 >        final CompletableFuture<Integer> g = m.handle
774 >            (f,
775 >             (Integer x, Throwable t) -> {
776 >                threadAssertSame(x, v1);
777 >                threadAssertNull(t);
778 >                a.getAndIncrement();
779 >                return inc(v1);
780 >            });
781 >        if (createIncomplete) f.complete(v1);
782 >
783 >        checkCompletedNormally(g, inc(v1));
784 >        checkCompletedNormally(f, v1);
785 >        assertEquals(1, a.get());
786 >    }}
787  
788      /**
789 <     * handle action completes normally with function value on either
790 <     * normal or exceptional completion of source
789 >     * handle action completes normally with function value on
790 >     * exceptional completion of source
791       */
792 <    public void testHandle() {
793 <        CompletableFuture<Integer> f, g;
794 <        IntegerHandler r;
795 <
796 <        f = new CompletableFuture<>();
797 <        f.completeExceptionally(new CFException());
798 <        g = f.handle(r = new IntegerHandler());
799 <        assertTrue(r.ran);
800 <        checkCompletedNormally(g, three);
801 <
802 <        f = new CompletableFuture<>();
803 <        g = f.handle(r = new IntegerHandler());
804 <        assertFalse(r.ran);
805 <        f.completeExceptionally(new CFException());
806 <        checkCompletedNormally(g, three);
807 <        assertTrue(r.ran);
792 >    public void testHandle_exceptionalCompletion() {
793 >        for (ExecutionMode m : ExecutionMode.values())
794 >        for (boolean createIncomplete : new boolean[] { true, false })
795 >        for (Integer v1 : new Integer[] { 1, null })
796 >    {
797 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
798 >        final AtomicInteger a = new AtomicInteger(0);
799 >        final CFException ex = new CFException();
800 >        if (!createIncomplete) f.completeExceptionally(ex);
801 >        final CompletableFuture<Integer> g = m.handle
802 >            (f,
803 >             (Integer x, Throwable t) -> {
804 >                threadAssertNull(x);
805 >                threadAssertSame(t, ex);
806 >                a.getAndIncrement();
807 >                return v1;
808 >            });
809 >        if (createIncomplete) f.completeExceptionally(ex);
810 >
811 >        checkCompletedNormally(g, v1);
812 >        checkCompletedWithWrappedCFException(f, ex);
813 >        assertEquals(1, a.get());
814 >    }}
815 >
816 >    /**
817 >     * handle action completes normally with function value on
818 >     * cancelled source
819 >     */
820 >    public void testHandle_sourceCancelled() {
821 >        for (ExecutionMode m : ExecutionMode.values())
822 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
823 >        for (boolean createIncomplete : new boolean[] { true, false })
824 >        for (Integer v1 : new Integer[] { 1, null })
825 >    {
826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
827 >        final AtomicInteger a = new AtomicInteger(0);
828 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
829 >        final CompletableFuture<Integer> g = m.handle
830 >            (f,
831 >             (Integer x, Throwable t) -> {
832 >                threadAssertNull(x);
833 >                threadAssertTrue(t instanceof CancellationException);
834 >                a.getAndIncrement();
835 >                return v1;
836 >            });
837 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
838  
839 <        f = new CompletableFuture<>();
840 <        f.complete(one);
841 <        g = f.handle(r = new IntegerHandler());
842 <        assertTrue(r.ran);
418 <        checkCompletedNormally(g, two);
839 >        checkCompletedNormally(g, v1);
840 >        checkCancelled(f);
841 >        assertEquals(1, a.get());
842 >    }}
843  
844 <        f = new CompletableFuture<>();
845 <        g = f.handle(r = new IntegerHandler());
846 <        assertFalse(r.ran);
847 <        f.complete(one);
848 <        assertTrue(r.ran);
849 <        checkCompletedNormally(g, two);
850 <    }
844 >    /**
845 >     * handle result completes exceptionally if action does
846 >     */
847 >    public void testHandle_sourceFailedActionFailed() {
848 >        for (ExecutionMode m : ExecutionMode.values())
849 >        for (boolean createIncomplete : new boolean[] { true, false })
850 >    {
851 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
852 >        final AtomicInteger a = new AtomicInteger(0);
853 >        final CFException ex1 = new CFException();
854 >        final CFException ex2 = new CFException();
855 >        if (!createIncomplete) f.completeExceptionally(ex1);
856 >        final CompletableFuture<Integer> g = m.handle
857 >            (f,
858 >             (Integer x, Throwable t) -> {
859 >                threadAssertNull(x);
860 >                threadAssertSame(ex1, t);
861 >                a.getAndIncrement();
862 >                throw ex2;
863 >            });
864 >        if (createIncomplete) f.completeExceptionally(ex1);
865 >
866 >        checkCompletedWithWrappedCFException(g, ex2);
867 >        checkCompletedWithWrappedCFException(f, ex1);
868 >        assertEquals(1, a.get());
869 >    }}
870 >
871 >    public void testHandle_sourceCompletedNormallyActionFailed() {
872 >        for (ExecutionMode m : ExecutionMode.values())
873 >        for (boolean createIncomplete : new boolean[] { true, false })
874 >        for (Integer v1 : new Integer[] { 1, null })
875 >    {
876 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
877 >        final AtomicInteger a = new AtomicInteger(0);
878 >        final CFException ex = new CFException();
879 >        if (!createIncomplete) f.complete(v1);
880 >        final CompletableFuture<Integer> g = m.handle
881 >            (f,
882 >             (Integer x, Throwable t) -> {
883 >                threadAssertSame(x, v1);
884 >                threadAssertNull(t);
885 >                a.getAndIncrement();
886 >                throw ex;
887 >            });
888 >        if (createIncomplete) f.complete(v1);
889 >
890 >        checkCompletedWithWrappedCFException(g, ex);
891 >        checkCompletedNormally(f, v1);
892 >        assertEquals(1, a.get());
893 >    }}
894  
895      /**
896       * runAsync completes after running Runnable
# Line 432 | Line 899 | public class CompletableFutureTest exten
899          Noop r = new Noop();
900          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
901          assertNull(f.join());
902 <        assertTrue(r.ran);
902 >        assertEquals(1, r.invocationCount);
903          checkCompletedNormally(f, null);
904      }
905  
# Line 444 | Line 911 | public class CompletableFutureTest exten
911          ThreadExecutor exec = new ThreadExecutor();
912          CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
913          assertNull(f.join());
914 <        assertTrue(r.ran);
914 >        assertEquals(1, r.invocationCount);
915          checkCompletedNormally(f, null);
916          assertEquals(1, exec.count.get());
917      }
# Line 456 | Line 923 | public class CompletableFutureTest exten
923          FailingNoop r = new FailingNoop();
924          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
925          checkCompletedWithWrappedCFException(f);
926 <        assertTrue(r.ran);
926 >        assertEquals(1, r.invocationCount);
927      }
928  
929      /**
# Line 486 | Line 953 | public class CompletableFutureTest exten
953          FailingSupplier r = new FailingSupplier();
954          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
955          checkCompletedWithWrappedCFException(f);
956 <        assertTrue(r.ran);
956 >        assertEquals(1, r.invocationCount);
957      }
958  
959      // seq completion methods
# Line 495 | Line 962 | public class CompletableFutureTest exten
962       * thenRun result completes normally after normal completion of source
963       */
964      public void testThenRun() {
965 <        CompletableFuture<Integer> f = new CompletableFuture<>();
966 <        Noop r = new Noop();
967 <        CompletableFuture<Void> g = f.thenRun(r);
965 >        CompletableFuture<Integer> f;
966 >        CompletableFuture<Void> g;
967 >        Noop r;
968 >
969 >        f = new CompletableFuture<>();
970 >        g = f.thenRun(r = new Noop());
971          f.complete(null);
972          checkCompletedNormally(g, null);
973 <        // reordered version
973 >        assertEquals(1, r.invocationCount);
974 >
975          f = new CompletableFuture<>();
976          f.complete(null);
977 <        r = new Noop();
507 <        g = f.thenRun(r);
977 >        g = f.thenRun(r = new Noop());
978          checkCompletedNormally(g, null);
979 +        assertEquals(1, r.invocationCount);
980      }
981  
982      /**
# Line 513 | Line 984 | public class CompletableFutureTest exten
984       * completion of source
985       */
986      public void testThenRun2() {
987 <        CompletableFuture<Integer> f = new CompletableFuture<>();
988 <        Noop r = new Noop();
989 <        CompletableFuture<Void> g = f.thenRun(r);
987 >        CompletableFuture<Integer> f;
988 >        CompletableFuture<Void> g;
989 >        Noop r;
990 >
991 >        f = new CompletableFuture<>();
992 >        g = f.thenRun(r = new Noop());
993          f.completeExceptionally(new CFException());
994          checkCompletedWithWrappedCFException(g);
995 +        assertEquals(0, r.invocationCount);
996 +
997 +        f = new CompletableFuture<>();
998 +        f.completeExceptionally(new CFException());
999 +        g = f.thenRun(r = new Noop());
1000 +        checkCompletedWithWrappedCFException(g);
1001 +        assertEquals(0, r.invocationCount);
1002      }
1003  
1004      /**
1005       * thenRun result completes exceptionally if action does
1006       */
1007      public void testThenRun3() {
1008 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1009 <        FailingNoop r = new FailingNoop();
1010 <        CompletableFuture<Void> g = f.thenRun(r);
1008 >        CompletableFuture<Integer> f;
1009 >        CompletableFuture<Void> g;
1010 >        FailingNoop r;
1011 >
1012 >        f = new CompletableFuture<>();
1013 >        g = f.thenRun(r = new FailingNoop());
1014 >        f.complete(null);
1015 >        checkCompletedWithWrappedCFException(g);
1016 >
1017 >        f = new CompletableFuture<>();
1018          f.complete(null);
1019 +        g = f.thenRun(r = new FailingNoop());
1020          checkCompletedWithWrappedCFException(g);
1021      }
1022  
# Line 535 | Line 1024 | public class CompletableFutureTest exten
1024       * thenRun result completes exceptionally if source cancelled
1025       */
1026      public void testThenRun4() {
1027 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1028 <        Noop r = new Noop();
1029 <        CompletableFuture<Void> g = f.thenRun(r);
1027 >        CompletableFuture<Integer> f;
1028 >        CompletableFuture<Void> g;
1029 >        Noop r;
1030 >
1031 >        f = new CompletableFuture<>();
1032 >        g = f.thenRun(r = new Noop());
1033          assertTrue(f.cancel(true));
1034          checkCompletedWithWrappedCancellationException(g);
1035 +
1036 +        f = new CompletableFuture<>();
1037 +        assertTrue(f.cancel(true));
1038 +        g = f.thenRun(r = new Noop());
1039 +        checkCompletedWithWrappedCancellationException(g);
1040      }
1041  
1042      /**
# Line 592 | Line 1089 | public class CompletableFutureTest exten
1089          CompletableFuture<Void> g = f.thenAccept(r);
1090          f.complete(one);
1091          checkCompletedNormally(g, null);
1092 <        assertEquals(r.value, 2);
1092 >        assertEquals(r.value, (Integer) 2);
1093      }
1094  
1095      /**
# Line 616 | Line 1113 | public class CompletableFutureTest exten
1113          CompletableFuture<Void> g = f.thenAccept(r);
1114          f.complete(one);
1115          checkCompletedWithWrappedCFException(g);
1116 <        assertTrue(r.ran);
1116 >        assertEquals(1, r.invocationCount);
1117      }
1118  
1119      /**
# Line 630 | Line 1127 | public class CompletableFutureTest exten
1127          checkCompletedWithWrappedCancellationException(g);
1128      }
1129  
633
1130      /**
1131       * thenCombine result completes normally after normal completion
1132       * of sources
1133       */
1134 <    public void testThenCombine() {
1135 <        CompletableFuture<Integer> f, g, h;
1136 <
1137 <        f = new CompletableFuture<>();
1138 <        g = new CompletableFuture<>();
1139 <        h = f.thenCombine(g, subtract);
1140 <        f.complete(3);
1141 <        checkIncomplete(h);
1142 <        g.complete(1);
1143 <        checkCompletedNormally(h, 2);
1144 <
1145 <        f = new CompletableFuture<>();
1146 <        g = new CompletableFuture<>();
1147 <        h = f.thenCombine(g, subtract);
1148 <        g.complete(1);
1149 <        checkIncomplete(h);
1150 <        f.complete(3);
1151 <        checkCompletedNormally(h, 2);
1152 <
1153 <        f = new CompletableFuture<>();
1154 <        g = new CompletableFuture<>();
1155 <        g.complete(1);
1156 <        f.complete(3);
1157 <        h = f.thenCombine(g, subtract);
1158 <        checkCompletedNormally(h, 2);
1159 <    }
1134 >    public void testThenCombine_normalCompletion1() {
1135 >        for (boolean createIncomplete : new boolean[] { true, false })
1136 >        for (boolean fFirst : new boolean[] { true, false })
1137 >        for (ExecutionMode m : ExecutionMode.values())
1138 >        for (Integer v1 : new Integer[] { 1, null })
1139 >        for (Integer v2 : new Integer[] { 2, null })
1140 >    {
1141 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1142 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1143 >        final SubtractFunction r = new SubtractFunction();
1144 >        CompletableFuture<Integer> h = null;
1145 >        if (createIncomplete) h = m.thenCombine(f, g, r);
1146 >
1147 >        if (fFirst)
1148 >            f.complete(v1);
1149 >        else
1150 >            g.complete(v2);
1151 >        if (createIncomplete) checkIncomplete(h);
1152 >        assertEquals(0, r.invocationCount);
1153 >        if (!fFirst)
1154 >            f.complete(v1);
1155 >        else
1156 >            g.complete(v2);
1157 >        if (!createIncomplete) h = m.thenCombine(f, g, r);
1158 >
1159 >        checkCompletedNormally(h, subtract(v1, v2));
1160 >        checkCompletedNormally(f, v1);
1161 >        checkCompletedNormally(g, v2);
1162 >        assertEquals(1, r.invocationCount);
1163 >    }}
1164  
1165      /**
1166       * thenCombine result completes exceptionally after exceptional
1167       * completion of either source
1168       */
1169 <    public void testThenCombine2() {
1170 <        CompletableFuture<Integer> f, g, h;
1171 <
1172 <        f = new CompletableFuture<>();
1173 <        g = new CompletableFuture<>();
1174 <        h = f.thenCombine(g, subtract);
1175 <        f.completeExceptionally(new CFException());
1176 <        checkIncomplete(h);
1177 <        g.complete(1);
1178 <        checkCompletedWithWrappedCFException(h);
1179 <
1180 <        f = new CompletableFuture<>();
1181 <        g = new CompletableFuture<>();
1182 <        h = f.thenCombine(g, subtract);
1183 <        g.completeExceptionally(new CFException());
1184 <        checkIncomplete(h);
1185 <        f.complete(3);
1186 <        checkCompletedWithWrappedCFException(h);
1187 <
1188 <        f = new CompletableFuture<>();
1189 <        g = new CompletableFuture<>();
1190 <        f.complete(3);
1191 <        g.completeExceptionally(new CFException());
1192 <        h = f.thenCombine(g, subtract);
1193 <        checkCompletedWithWrappedCFException(h);
1194 <
1195 <        f = new CompletableFuture<>();
1196 <        g = new CompletableFuture<>();
1197 <        f.completeExceptionally(new CFException());
1198 <        g.complete(3);
1199 <        h = f.thenCombine(g, subtract);
1200 <        checkCompletedWithWrappedCFException(h);
1201 <    }
1169 >    public void testThenCombine_exceptionalCompletion1() {
1170 >        for (ExecutionMode m : ExecutionMode.values())
1171 >        for (Integer v1 : new Integer[] { 1, null })
1172 >    {
1173 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1175 >        final SubtractFunction r = new SubtractFunction();
1176 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1177 >        final CFException ex = new CFException();
1178 >
1179 >        f.completeExceptionally(ex);
1180 >        checkIncomplete(h);
1181 >        g.complete(v1);
1182 >
1183 >        checkCompletedWithWrappedCFException(h, ex);
1184 >        checkCompletedWithWrappedCFException(f, ex);
1185 >        assertEquals(0, r.invocationCount);
1186 >        checkCompletedNormally(g, v1);
1187 >    }}
1188 >
1189 >    public void testThenCombine_exceptionalCompletion2() {
1190 >        for (ExecutionMode m : ExecutionMode.values())
1191 >        for (Integer v1 : new Integer[] { 1, null })
1192 >    {
1193 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1194 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1195 >        final SubtractFunction r = new SubtractFunction();
1196 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1197 >        final CFException ex = new CFException();
1198 >
1199 >        g.completeExceptionally(ex);
1200 >        checkIncomplete(h);
1201 >        f.complete(v1);
1202 >
1203 >        checkCompletedWithWrappedCFException(h, ex);
1204 >        checkCompletedWithWrappedCFException(g, ex);
1205 >        assertEquals(0, r.invocationCount);
1206 >        checkCompletedNormally(f, v1);
1207 >    }}
1208 >
1209 >    public void testThenCombine_exceptionalCompletion3() {
1210 >        for (ExecutionMode m : ExecutionMode.values())
1211 >        for (Integer v1 : new Integer[] { 1, null })
1212 >    {
1213 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1214 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1215 >        final SubtractFunction r = new SubtractFunction();
1216 >        final CFException ex = new CFException();
1217 >
1218 >        g.completeExceptionally(ex);
1219 >        f.complete(v1);
1220 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1221 >
1222 >        checkCompletedWithWrappedCFException(h, ex);
1223 >        checkCompletedWithWrappedCFException(g, ex);
1224 >        assertEquals(0, r.invocationCount);
1225 >        checkCompletedNormally(f, v1);
1226 >    }}
1227 >
1228 >    public void testThenCombine_exceptionalCompletion4() {
1229 >        for (ExecutionMode m : ExecutionMode.values())
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 >        final CFException ex = new CFException();
1236 >
1237 >        f.completeExceptionally(ex);
1238 >        g.complete(v1);
1239 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1240 >
1241 >        checkCompletedWithWrappedCFException(h, ex);
1242 >        checkCompletedWithWrappedCFException(f, ex);
1243 >        assertEquals(0, r.invocationCount);
1244 >        checkCompletedNormally(g, v1);
1245 >    }}
1246  
1247      /**
1248       * thenCombine result completes exceptionally if action does
1249       */
1250 <    public void testThenCombine3() {
1251 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1252 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1253 <        FailingBiFunction r = new FailingBiFunction();
1254 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1255 <        f.complete(one);
1256 <        checkIncomplete(g);
1257 <        assertFalse(r.ran);
1258 <        f2.complete(two);
1259 <        checkCompletedWithWrappedCFException(g);
1260 <        assertTrue(r.ran);
1261 <    }
1250 >    public void testThenCombine_actionFailed1() {
1251 >        for (ExecutionMode m : ExecutionMode.values())
1252 >        for (Integer v1 : new Integer[] { 1, null })
1253 >        for (Integer v2 : new Integer[] { 2, null })
1254 >    {
1255 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1256 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1257 >        final FailingBiFunction r = new FailingBiFunction();
1258 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1259 >
1260 >        f.complete(v1);
1261 >        checkIncomplete(h);
1262 >        g.complete(v2);
1263 >
1264 >        checkCompletedWithWrappedCFException(h);
1265 >        checkCompletedNormally(f, v1);
1266 >        checkCompletedNormally(g, v2);
1267 >    }}
1268 >
1269 >    public void testThenCombine_actionFailed2() {
1270 >        for (ExecutionMode m : ExecutionMode.values())
1271 >        for (Integer v1 : new Integer[] { 1, null })
1272 >        for (Integer v2 : new Integer[] { 2, null })
1273 >    {
1274 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1275 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1276 >        final FailingBiFunction r = new FailingBiFunction();
1277 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1278 >
1279 >        g.complete(v2);
1280 >        checkIncomplete(h);
1281 >        f.complete(v1);
1282 >
1283 >        checkCompletedWithWrappedCFException(h);
1284 >        checkCompletedNormally(f, v1);
1285 >        checkCompletedNormally(g, v2);
1286 >    }}
1287  
1288      /**
1289       * thenCombine result completes exceptionally if either source cancelled
1290       */
1291 <    public void testThenCombine4() {
1292 <        CompletableFuture<Integer> f, g, h;
1291 >    public void testThenCombine_sourceCancelled1() {
1292 >        for (ExecutionMode m : ExecutionMode.values())
1293 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1294 >        for (Integer v1 : new Integer[] { 1, null })
1295 >    {
1296 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1297 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1298 >        final SubtractFunction r = new SubtractFunction();
1299 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1300  
1301 <        f = new CompletableFuture<>();
726 <        g = new CompletableFuture<>();
727 <        h = f.thenCombine(g, subtract);
728 <        assertTrue(f.cancel(true));
1301 >        assertTrue(f.cancel(mayInterruptIfRunning));
1302          checkIncomplete(h);
1303 <        g.complete(1);
731 <        checkCompletedWithWrappedCancellationException(h);
1303 >        g.complete(v1);
1304  
733        f = new CompletableFuture<>();
734        g = new CompletableFuture<>();
735        h = f.thenCombine(g, subtract);
736        assertTrue(g.cancel(true));
737        checkIncomplete(h);
738        f.complete(3);
1305          checkCompletedWithWrappedCancellationException(h);
1306 +        checkCancelled(f);
1307 +        assertEquals(0, r.invocationCount);
1308 +        checkCompletedNormally(g, v1);
1309 +    }}
1310 +
1311 +    public void testThenCombine_sourceCancelled2() {
1312 +        for (ExecutionMode m : ExecutionMode.values())
1313 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1314 +        for (Integer v1 : new Integer[] { 1, null })
1315 +    {
1316 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1317 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1318 +        final SubtractFunction r = new SubtractFunction();
1319 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1320 +
1321 +        assertTrue(g.cancel(mayInterruptIfRunning));
1322 +        checkIncomplete(h);
1323 +        f.complete(v1);
1324 +
1325 +        checkCompletedWithWrappedCancellationException(h);
1326 +        checkCancelled(g);
1327 +        assertEquals(0, r.invocationCount);
1328 +        checkCompletedNormally(f, v1);
1329 +    }}
1330 +
1331 +    public void testThenCombine_sourceCancelled3() {
1332 +        for (ExecutionMode m : ExecutionMode.values())
1333 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1334 +        for (Integer v1 : new Integer[] { 1, null })
1335 +    {
1336 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1337 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1338 +        final SubtractFunction r = new SubtractFunction();
1339 +
1340 +        assertTrue(g.cancel(mayInterruptIfRunning));
1341 +        f.complete(v1);
1342 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1343 +
1344 +        checkCompletedWithWrappedCancellationException(h);
1345 +        checkCancelled(g);
1346 +        assertEquals(0, r.invocationCount);
1347 +        checkCompletedNormally(f, v1);
1348 +    }}
1349 +
1350 +    public void testThenCombine_sourceCancelled4() {
1351 +        for (ExecutionMode m : ExecutionMode.values())
1352 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1353 +        for (Integer v1 : new Integer[] { 1, null })
1354 +    {
1355 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1356 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1357 +        final SubtractFunction r = new SubtractFunction();
1358 +
1359 +        assertTrue(f.cancel(mayInterruptIfRunning));
1360 +        g.complete(v1);
1361 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1362  
741        f = new CompletableFuture<>();
742        g = new CompletableFuture<>();
743        assertTrue(f.cancel(true));
744        assertTrue(g.cancel(true));
745        h = f.thenCombine(g, subtract);
1363          checkCompletedWithWrappedCancellationException(h);
1364 <    }
1364 >        checkCancelled(f);
1365 >        assertEquals(0, r.invocationCount);
1366 >        checkCompletedNormally(g, v1);
1367 >    }}
1368  
1369      /**
1370       * thenAcceptBoth result completes normally after normal
1371       * completion of sources
1372       */
1373 <    public void testThenAcceptBoth() {
1374 <        CompletableFuture<Integer> f, g;
1375 <        CompletableFuture<Void> h;
1376 <        SubtractAction r;
1373 >    public void testThenAcceptBoth_normalCompletion1() {
1374 >        for (ExecutionMode m : ExecutionMode.values())
1375 >        for (Integer v1 : new Integer[] { 1, null })
1376 >        for (Integer v2 : new Integer[] { 2, null })
1377 >    {
1378 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1379 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1380 >        final SubtractAction r = new SubtractAction();
1381 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1382  
1383 <        f = new CompletableFuture<>();
759 <        g = new CompletableFuture<>();
760 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
761 <        f.complete(3);
1383 >        f.complete(v1);
1384          checkIncomplete(h);
1385 <        g.complete(1);
1385 >        assertEquals(0, r.invocationCount);
1386 >        g.complete(v2);
1387 >
1388          checkCompletedNormally(h, null);
1389 <        assertEquals(r.value, 2);
1389 >        assertEquals(subtract(v1, v2), r.value);
1390 >        checkCompletedNormally(f, v1);
1391 >        checkCompletedNormally(g, v2);
1392 >    }}
1393 >
1394 >    public void testThenAcceptBoth_normalCompletion2() {
1395 >        for (ExecutionMode m : ExecutionMode.values())
1396 >        for (Integer v1 : new Integer[] { 1, null })
1397 >        for (Integer v2 : new Integer[] { 2, null })
1398 >    {
1399 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1400 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1401 >        final SubtractAction r = new SubtractAction();
1402 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1403  
1404 <        f = new CompletableFuture<>();
768 <        g = new CompletableFuture<>();
769 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
770 <        g.complete(1);
1404 >        g.complete(v2);
1405          checkIncomplete(h);
1406 <        f.complete(3);
1406 >        assertEquals(0, r.invocationCount);
1407 >        f.complete(v1);
1408 >
1409          checkCompletedNormally(h, null);
1410 <        assertEquals(r.value, 2);
1410 >        assertEquals(subtract(v1, v2), r.value);
1411 >        checkCompletedNormally(f, v1);
1412 >        checkCompletedNormally(g, v2);
1413 >    }}
1414 >
1415 >    public void testThenAcceptBoth_normalCompletion3() {
1416 >        for (ExecutionMode m : ExecutionMode.values())
1417 >        for (Integer v1 : new Integer[] { 1, null })
1418 >        for (Integer v2 : new Integer[] { 2, null })
1419 >    {
1420 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1421 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1422 >        final SubtractAction r = new SubtractAction();
1423 >
1424 >        g.complete(v2);
1425 >        f.complete(v1);
1426 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1427  
776        f = new CompletableFuture<>();
777        g = new CompletableFuture<>();
778        g.complete(1);
779        f.complete(3);
780        h = f.thenAcceptBoth(g, r = new SubtractAction());
1428          checkCompletedNormally(h, null);
1429 <        assertEquals(r.value, 2);
1430 <    }
1429 >        assertEquals(subtract(v1, v2), r.value);
1430 >        checkCompletedNormally(f, v1);
1431 >        checkCompletedNormally(g, v2);
1432 >    }}
1433 >
1434 >    public void testThenAcceptBoth_normalCompletion4() {
1435 >        for (ExecutionMode m : ExecutionMode.values())
1436 >        for (Integer v1 : new Integer[] { 1, null })
1437 >        for (Integer v2 : new Integer[] { 2, null })
1438 >    {
1439 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1440 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1441 >        final SubtractAction r = new SubtractAction();
1442 >
1443 >        f.complete(v1);
1444 >        g.complete(v2);
1445 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1446 >
1447 >        checkCompletedNormally(h, null);
1448 >        assertEquals(subtract(v1, v2), r.value);
1449 >        checkCompletedNormally(f, v1);
1450 >        checkCompletedNormally(g, v2);
1451 >    }}
1452  
1453      /**
1454       * thenAcceptBoth result completes exceptionally after exceptional
1455       * completion of either source
1456       */
1457 <    public void testThenAcceptBoth2() {
1458 <        CompletableFuture<Integer> f, g;
1459 <        CompletableFuture<Void> h;
1460 <        SubtractAction r;
1461 <
1462 <        f = new CompletableFuture<>();
1463 <        g = new CompletableFuture<>();
1464 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1465 <        f.completeExceptionally(new CFException());
1466 <        checkIncomplete(h);
1467 <        g.complete(1);
1468 <        checkCompletedWithWrappedCFException(h);
1469 <
1470 <        f = new CompletableFuture<>();
1471 <        g = new CompletableFuture<>();
1472 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1473 <        g.completeExceptionally(new CFException());
1474 <        checkIncomplete(h);
1475 <        f.complete(3);
1476 <        checkCompletedWithWrappedCFException(h);
1477 <
1478 <        f = new CompletableFuture<>();
1479 <        g = new CompletableFuture<>();
1480 <        f.complete(3);
1481 <        g.completeExceptionally(new CFException());
1482 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1483 <        checkCompletedWithWrappedCFException(h);
1484 <
1485 <        f = new CompletableFuture<>();
1486 <        g = new CompletableFuture<>();
1487 <        f.completeExceptionally(new CFException());
1488 <        g.complete(3);
1489 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1490 <        checkCompletedWithWrappedCFException(h);
1491 <    }
1457 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1458 >        for (ExecutionMode m : ExecutionMode.values())
1459 >        for (Integer v1 : new Integer[] { 1, null })
1460 >    {
1461 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1462 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1463 >        final SubtractAction r = new SubtractAction();
1464 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1465 >        final CFException ex = new CFException();
1466 >
1467 >        f.completeExceptionally(ex);
1468 >        checkIncomplete(h);
1469 >        g.complete(v1);
1470 >
1471 >        checkCompletedWithWrappedCFException(h, ex);
1472 >        checkCompletedWithWrappedCFException(f, ex);
1473 >        assertEquals(0, r.invocationCount);
1474 >        checkCompletedNormally(g, v1);
1475 >    }}
1476 >
1477 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1478 >        for (ExecutionMode m : ExecutionMode.values())
1479 >        for (Integer v1 : new Integer[] { 1, null })
1480 >    {
1481 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1483 >        final SubtractAction r = new SubtractAction();
1484 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1485 >        final CFException ex = new CFException();
1486 >
1487 >        g.completeExceptionally(ex);
1488 >        checkIncomplete(h);
1489 >        f.complete(v1);
1490 >
1491 >        checkCompletedWithWrappedCFException(h, ex);
1492 >        checkCompletedWithWrappedCFException(g, ex);
1493 >        assertEquals(0, r.invocationCount);
1494 >        checkCompletedNormally(f, v1);
1495 >    }}
1496 >
1497 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1498 >        for (ExecutionMode m : ExecutionMode.values())
1499 >        for (Integer v1 : new Integer[] { 1, null })
1500 >    {
1501 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1502 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1503 >        final SubtractAction r = new SubtractAction();
1504 >        final CFException ex = new CFException();
1505 >
1506 >        g.completeExceptionally(ex);
1507 >        f.complete(v1);
1508 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1509 >
1510 >        checkCompletedWithWrappedCFException(h, ex);
1511 >        checkCompletedWithWrappedCFException(g, ex);
1512 >        assertEquals(0, r.invocationCount);
1513 >        checkCompletedNormally(f, v1);
1514 >    }}
1515 >
1516 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1517 >        for (ExecutionMode m : ExecutionMode.values())
1518 >        for (Integer v1 : new Integer[] { 1, null })
1519 >    {
1520 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1521 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1522 >        final SubtractAction r = new SubtractAction();
1523 >        final CFException ex = new CFException();
1524 >
1525 >        f.completeExceptionally(ex);
1526 >        g.complete(v1);
1527 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1528 >
1529 >        checkCompletedWithWrappedCFException(h, ex);
1530 >        checkCompletedWithWrappedCFException(f, ex);
1531 >        assertEquals(0, r.invocationCount);
1532 >        checkCompletedNormally(g, v1);
1533 >    }}
1534  
1535      /**
1536       * thenAcceptBoth result completes exceptionally if action does
1537       */
1538 <    public void testThenAcceptBoth3() {
1539 <        CompletableFuture<Integer> f, g;
1540 <        CompletableFuture<Void> h;
1541 <        FailingBiConsumer r;
1542 <
1543 <        f = new CompletableFuture<>();
1544 <        g = new CompletableFuture<>();
1545 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1546 <        f.complete(3);
1547 <        checkIncomplete(h);
1548 <        g.complete(1);
1549 <        checkCompletedWithWrappedCFException(h);
1550 <
1551 <        f = new CompletableFuture<>();
1552 <        g = new CompletableFuture<>();
1553 <        f.complete(3);
1554 <        g.complete(1);
1555 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1556 <        checkCompletedWithWrappedCFException(h);
1557 <    }
1538 >    public void testThenAcceptBoth_actionFailed1() {
1539 >        for (ExecutionMode m : ExecutionMode.values())
1540 >        for (Integer v1 : new Integer[] { 1, null })
1541 >        for (Integer v2 : new Integer[] { 2, null })
1542 >    {
1543 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1544 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1545 >        final FailingBiConsumer r = new FailingBiConsumer();
1546 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1547 >
1548 >        f.complete(v1);
1549 >        checkIncomplete(h);
1550 >        g.complete(v2);
1551 >
1552 >        checkCompletedWithWrappedCFException(h);
1553 >        checkCompletedNormally(f, v1);
1554 >        checkCompletedNormally(g, v2);
1555 >    }}
1556 >
1557 >    public void testThenAcceptBoth_actionFailed2() {
1558 >        for (ExecutionMode m : ExecutionMode.values())
1559 >        for (Integer v1 : new Integer[] { 1, null })
1560 >        for (Integer v2 : new Integer[] { 2, null })
1561 >    {
1562 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1563 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1564 >        final FailingBiConsumer r = new FailingBiConsumer();
1565 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1566 >
1567 >        g.complete(v2);
1568 >        checkIncomplete(h);
1569 >        f.complete(v1);
1570 >
1571 >        checkCompletedWithWrappedCFException(h);
1572 >        checkCompletedNormally(f, v1);
1573 >        checkCompletedNormally(g, v2);
1574 >    }}
1575  
1576      /**
1577       * thenAcceptBoth result completes exceptionally if either source cancelled
1578       */
1579 <    public void testThenAcceptBoth4() {
1580 <        CompletableFuture<Integer> f, g;
1581 <        CompletableFuture<Void> h;
1582 <        SubtractAction r;
1583 <
1584 <        f = new CompletableFuture<>();
1585 <        g = new CompletableFuture<>();
1586 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1587 <        assertTrue(f.cancel(true));
861 <        checkIncomplete(h);
862 <        g.complete(1);
863 <        checkCompletedWithWrappedCancellationException(h);
1579 >    public void testThenAcceptBoth_sourceCancelled1() {
1580 >        for (ExecutionMode m : ExecutionMode.values())
1581 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1582 >        for (Integer v1 : new Integer[] { 1, null })
1583 >    {
1584 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1585 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1586 >        final SubtractAction r = new SubtractAction();
1587 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1588  
1589 <        f = new CompletableFuture<>();
866 <        g = new CompletableFuture<>();
867 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
868 <        assertTrue(g.cancel(true));
1589 >        assertTrue(f.cancel(mayInterruptIfRunning));
1590          checkIncomplete(h);
1591 <        f.complete(3);
871 <        checkCompletedWithWrappedCancellationException(h);
1591 >        g.complete(v1);
1592  
873        f = new CompletableFuture<>();
874        g = new CompletableFuture<>();
875        f.complete(3);
876        assertTrue(g.cancel(true));
877        h = f.thenAcceptBoth(g, r = new SubtractAction());
1593          checkCompletedWithWrappedCancellationException(h);
1594 +        checkCancelled(f);
1595 +        assertEquals(0, r.invocationCount);
1596 +        checkCompletedNormally(g, v1);
1597 +    }}
1598 +
1599 +    public void testThenAcceptBoth_sourceCancelled2() {
1600 +        for (ExecutionMode m : ExecutionMode.values())
1601 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1602 +        for (Integer v1 : new Integer[] { 1, null })
1603 +    {
1604 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1605 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1606 +        final SubtractAction r = new SubtractAction();
1607 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1608 +
1609 +        assertTrue(g.cancel(mayInterruptIfRunning));
1610 +        checkIncomplete(h);
1611 +        f.complete(v1);
1612 +
1613 +        checkCompletedWithWrappedCancellationException(h);
1614 +        checkCancelled(g);
1615 +        assertEquals(0, r.invocationCount);
1616 +        checkCompletedNormally(f, v1);
1617 +    }}
1618 +
1619 +    public void testThenAcceptBoth_sourceCancelled3() {
1620 +        for (ExecutionMode m : ExecutionMode.values())
1621 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1622 +        for (Integer v1 : new Integer[] { 1, null })
1623 +    {
1624 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1625 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1626 +        final SubtractAction r = new SubtractAction();
1627 +
1628 +        assertTrue(g.cancel(mayInterruptIfRunning));
1629 +        f.complete(v1);
1630 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1631 +
1632 +        checkCompletedWithWrappedCancellationException(h);
1633 +        checkCancelled(g);
1634 +        assertEquals(0, r.invocationCount);
1635 +        checkCompletedNormally(f, v1);
1636 +    }}
1637 +
1638 +    public void testThenAcceptBoth_sourceCancelled4() {
1639 +        for (ExecutionMode m : ExecutionMode.values())
1640 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1641 +        for (Integer v1 : new Integer[] { 1, null })
1642 +    {
1643 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1644 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1645 +        final SubtractAction r = new SubtractAction();
1646 +
1647 +        assertTrue(f.cancel(mayInterruptIfRunning));
1648 +        g.complete(v1);
1649 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1650  
880        f = new CompletableFuture<>();
881        g = new CompletableFuture<>();
882        assertTrue(f.cancel(true));
883        g.complete(3);
884        h = f.thenAcceptBoth(g, r = new SubtractAction());
1651          checkCompletedWithWrappedCancellationException(h);
1652 <    }
1652 >        checkCancelled(f);
1653 >        assertEquals(0, r.invocationCount);
1654 >        checkCompletedNormally(g, v1);
1655 >    }}
1656  
1657      /**
1658       * runAfterBoth result completes normally after normal
1659       * completion of sources
1660       */
1661 <    public void testRunAfterBoth() {
1662 <        CompletableFuture<Integer> f, g;
1663 <        CompletableFuture<Void> h;
1664 <        Noop r;
1661 >    public void testRunAfterBoth_normalCompletion1() {
1662 >        for (ExecutionMode m : ExecutionMode.values())
1663 >        for (Integer v1 : new Integer[] { 1, null })
1664 >        for (Integer v2 : new Integer[] { 2, 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  
1671 <        f = new CompletableFuture<>();
898 <        g = new CompletableFuture<>();
899 <        h = f.runAfterBoth(g, r = new Noop());
900 <        f.complete(3);
1671 >        f.complete(v1);
1672          checkIncomplete(h);
1673 <        g.complete(1);
1673 >        assertEquals(0, r.invocationCount);
1674 >        g.complete(v2);
1675 >
1676          checkCompletedNormally(h, null);
1677 <        assertTrue(r.ran);
1677 >        assertEquals(1, r.invocationCount);
1678 >        checkCompletedNormally(f, v1);
1679 >        checkCompletedNormally(g, v2);
1680 >    }}
1681 >
1682 >    public void testRunAfterBoth_normalCompletion2() {
1683 >        for (ExecutionMode m : ExecutionMode.values())
1684 >        for (Integer v1 : new Integer[] { 1, null })
1685 >        for (Integer v2 : new Integer[] { 2, null })
1686 >    {
1687 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1688 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1689 >        final Noop r = new Noop();
1690 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1691  
1692 <        f = new CompletableFuture<>();
907 <        g = new CompletableFuture<>();
908 <        h = f.runAfterBoth(g, r = new Noop());
909 <        g.complete(1);
1692 >        g.complete(v2);
1693          checkIncomplete(h);
1694 <        f.complete(3);
1694 >        assertEquals(0, r.invocationCount);
1695 >        f.complete(v1);
1696 >
1697          checkCompletedNormally(h, null);
1698 <        assertTrue(r.ran);
1698 >        assertEquals(1, r.invocationCount);
1699 >        checkCompletedNormally(f, v1);
1700 >        checkCompletedNormally(g, v2);
1701 >    }}
1702 >
1703 >    public void testRunAfterBoth_normalCompletion3() {
1704 >        for (ExecutionMode m : ExecutionMode.values())
1705 >        for (Integer v1 : new Integer[] { 1, null })
1706 >        for (Integer v2 : new Integer[] { 2, null })
1707 >    {
1708 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1709 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1710 >        final Noop r = new Noop();
1711 >
1712 >        g.complete(v2);
1713 >        f.complete(v1);
1714 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1715  
915        f = new CompletableFuture<>();
916        g = new CompletableFuture<>();
917        g.complete(1);
918        f.complete(3);
919        h = f.runAfterBoth(g, r = new Noop());
1716          checkCompletedNormally(h, null);
1717 <        assertTrue(r.ran);
1718 <    }
1717 >        assertEquals(1, r.invocationCount);
1718 >        checkCompletedNormally(f, v1);
1719 >        checkCompletedNormally(g, v2);
1720 >    }}
1721 >
1722 >    public void testRunAfterBoth_normalCompletion4() {
1723 >        for (ExecutionMode m : ExecutionMode.values())
1724 >        for (Integer v1 : new Integer[] { 1, null })
1725 >        for (Integer v2 : new Integer[] { 2, null })
1726 >    {
1727 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1728 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1729 >        final Noop r = new Noop();
1730 >
1731 >        f.complete(v1);
1732 >        g.complete(v2);
1733 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1734 >
1735 >        checkCompletedNormally(h, null);
1736 >        assertEquals(1, r.invocationCount);
1737 >        checkCompletedNormally(f, v1);
1738 >        checkCompletedNormally(g, v2);
1739 >    }}
1740  
1741      /**
1742       * runAfterBoth result completes exceptionally after exceptional
1743       * completion of either source
1744       */
1745 <    public void testRunAfterBoth2() {
1746 <        CompletableFuture<Integer> f, g;
1747 <        CompletableFuture<Void> h;
1748 <        Noop r;
1749 <
1750 <        f = new CompletableFuture<>();
1751 <        g = new CompletableFuture<>();
1752 <        h = f.runAfterBoth(g, r = new Noop());
1753 <        f.completeExceptionally(new CFException());
1754 <        checkIncomplete(h);
1755 <        g.complete(1);
1756 <        checkCompletedWithWrappedCFException(h);
1757 <        assertFalse(r.ran);
1758 <
1759 <        f = new CompletableFuture<>();
1760 <        g = new CompletableFuture<>();
1761 <        h = f.runAfterBoth(g, r = new Noop());
1762 <        g.completeExceptionally(new CFException());
1763 <        checkIncomplete(h);
1764 <        f.complete(3);
1765 <        checkCompletedWithWrappedCFException(h);
1766 <        assertFalse(r.ran);
1767 <
1768 <        f = new CompletableFuture<>();
1769 <        g = new CompletableFuture<>();
1770 <        g.completeExceptionally(new CFException());
1771 <        f.complete(3);
1772 <        h = f.runAfterBoth(g, r = new Noop());
1773 <        checkCompletedWithWrappedCFException(h);
1774 <        assertFalse(r.ran);
1775 <
1776 <        f = new CompletableFuture<>();
1777 <        g = new CompletableFuture<>();
1778 <        f.completeExceptionally(new CFException());
1779 <        g.complete(1);
1780 <        h = f.runAfterBoth(g, r = new Noop());
1781 <        checkCompletedWithWrappedCFException(h);
1782 <        assertFalse(r.ran);
1783 <    }
1745 >    public void testRunAfterBoth_exceptionalCompletion1() {
1746 >        for (ExecutionMode m : ExecutionMode.values())
1747 >        for (Integer v1 : new Integer[] { 1, null })
1748 >    {
1749 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1750 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1751 >        final Noop r = new Noop();
1752 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1753 >        final CFException ex = new CFException();
1754 >
1755 >        f.completeExceptionally(ex);
1756 >        checkIncomplete(h);
1757 >        g.complete(v1);
1758 >
1759 >        checkCompletedWithWrappedCFException(h, ex);
1760 >        checkCompletedWithWrappedCFException(f, ex);
1761 >        assertEquals(0, r.invocationCount);
1762 >        checkCompletedNormally(g, v1);
1763 >    }}
1764 >
1765 >    public void testRunAfterBoth_exceptionalCompletion2() {
1766 >        for (ExecutionMode m : ExecutionMode.values())
1767 >        for (Integer v1 : new Integer[] { 1, null })
1768 >    {
1769 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1770 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1771 >        final Noop r = new Noop();
1772 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1773 >        final CFException ex = new CFException();
1774 >
1775 >        g.completeExceptionally(ex);
1776 >        checkIncomplete(h);
1777 >        f.complete(v1);
1778 >
1779 >        checkCompletedWithWrappedCFException(h, ex);
1780 >        checkCompletedWithWrappedCFException(g, ex);
1781 >        assertEquals(0, r.invocationCount);
1782 >        checkCompletedNormally(f, v1);
1783 >    }}
1784 >
1785 >    public void testRunAfterBoth_exceptionalCompletion3() {
1786 >        for (ExecutionMode m : ExecutionMode.values())
1787 >        for (Integer v1 : new Integer[] { 1, null })
1788 >    {
1789 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1790 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1791 >        final Noop r = new Noop();
1792 >        final CFException ex = new CFException();
1793 >
1794 >        g.completeExceptionally(ex);
1795 >        f.complete(v1);
1796 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1797 >
1798 >        checkCompletedWithWrappedCFException(h, ex);
1799 >        checkCompletedWithWrappedCFException(g, ex);
1800 >        assertEquals(0, r.invocationCount);
1801 >        checkCompletedNormally(f, v1);
1802 >    }}
1803 >
1804 >    public void testRunAfterBoth_exceptionalCompletion4() {
1805 >        for (ExecutionMode m : ExecutionMode.values())
1806 >        for (Integer v1 : new Integer[] { 1, null })
1807 >    {
1808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 >        final Noop r = new Noop();
1811 >        final CFException ex = new CFException();
1812 >
1813 >        f.completeExceptionally(ex);
1814 >        g.complete(v1);
1815 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1816 >
1817 >        checkCompletedWithWrappedCFException(h, ex);
1818 >        checkCompletedWithWrappedCFException(f, ex);
1819 >        assertEquals(0, r.invocationCount);
1820 >        checkCompletedNormally(g, v1);
1821 >    }}
1822  
1823      /**
1824       * runAfterBoth result completes exceptionally if action does
1825       */
1826 <    public void testRunAfterBoth3() {
1827 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1828 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1829 <        FailingNoop r = new FailingNoop();
1830 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1831 <        f.complete(one);
1832 <        checkIncomplete(g);
1833 <        f2.complete(two);
1834 <        checkCompletedWithWrappedCFException(g);
1835 <    }
1826 >    public void testRunAfterBoth_actionFailed1() {
1827 >        for (ExecutionMode m : ExecutionMode.values())
1828 >        for (Integer v1 : new Integer[] { 1, null })
1829 >        for (Integer v2 : new Integer[] { 2, null })
1830 >    {
1831 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1832 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1833 >        final FailingNoop r = new FailingNoop();
1834 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1835 >
1836 >        f.complete(v1);
1837 >        checkIncomplete(h);
1838 >        g.complete(v2);
1839 >
1840 >        checkCompletedWithWrappedCFException(h);
1841 >        checkCompletedNormally(f, v1);
1842 >        checkCompletedNormally(g, v2);
1843 >    }}
1844 >
1845 >    public void testRunAfterBoth_actionFailed2() {
1846 >        for (ExecutionMode m : ExecutionMode.values())
1847 >        for (Integer v1 : new Integer[] { 1, null })
1848 >        for (Integer v2 : new Integer[] { 2, null })
1849 >    {
1850 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1851 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1852 >        final FailingNoop r = new FailingNoop();
1853 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1854 >
1855 >        g.complete(v2);
1856 >        checkIncomplete(h);
1857 >        f.complete(v1);
1858 >
1859 >        checkCompletedWithWrappedCFException(h);
1860 >        checkCompletedNormally(f, v1);
1861 >        checkCompletedNormally(g, v2);
1862 >    }}
1863  
1864      /**
1865       * runAfterBoth result completes exceptionally if either source cancelled
1866       */
1867 <    public void testRunAfterBoth4() {
1868 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1869 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1870 <        Noop r = new Noop();
1871 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1872 <        assertTrue(f.cancel(true));
1873 <        f2.complete(two);
1874 <        checkCompletedWithWrappedCancellationException(g);
1875 <        f = new CompletableFuture<>();
1876 <        f2 = new CompletableFuture<>();
1877 <        r = new Noop();
1878 <        g = f.runAfterBoth(f2, r);
1879 <        f.complete(one);
1880 <        assertTrue(f2.cancel(true));
1881 <        checkCompletedWithWrappedCancellationException(g);
1882 <    }
1867 >    public void testRunAfterBoth_sourceCancelled1() {
1868 >        for (ExecutionMode m : ExecutionMode.values())
1869 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1870 >        for (Integer v1 : new Integer[] { 1, null })
1871 >    {
1872 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1873 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1874 >        final Noop r = new Noop();
1875 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1876 >
1877 >        assertTrue(f.cancel(mayInterruptIfRunning));
1878 >        checkIncomplete(h);
1879 >        g.complete(v1);
1880 >
1881 >        checkCompletedWithWrappedCancellationException(h);
1882 >        checkCancelled(f);
1883 >        assertEquals(0, r.invocationCount);
1884 >        checkCompletedNormally(g, v1);
1885 >    }}
1886 >
1887 >    public void testRunAfterBoth_sourceCancelled2() {
1888 >        for (ExecutionMode m : ExecutionMode.values())
1889 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1890 >        for (Integer v1 : new Integer[] { 1, null })
1891 >    {
1892 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1893 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1894 >        final Noop r = new Noop();
1895 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1896 >
1897 >        assertTrue(g.cancel(mayInterruptIfRunning));
1898 >        checkIncomplete(h);
1899 >        f.complete(v1);
1900 >
1901 >        checkCompletedWithWrappedCancellationException(h);
1902 >        checkCancelled(g);
1903 >        assertEquals(0, r.invocationCount);
1904 >        checkCompletedNormally(f, v1);
1905 >    }}
1906 >
1907 >    public void testRunAfterBoth_sourceCancelled3() {
1908 >        for (ExecutionMode m : ExecutionMode.values())
1909 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1910 >        for (Integer v1 : new Integer[] { 1, null })
1911 >    {
1912 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1913 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1914 >        final Noop r = new Noop();
1915 >
1916 >        assertTrue(g.cancel(mayInterruptIfRunning));
1917 >        f.complete(v1);
1918 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1919 >
1920 >        checkCompletedWithWrappedCancellationException(h);
1921 >        checkCancelled(g);
1922 >        assertEquals(0, r.invocationCount);
1923 >        checkCompletedNormally(f, v1);
1924 >    }}
1925 >
1926 >    public void testRunAfterBoth_sourceCancelled4() {
1927 >        for (ExecutionMode m : ExecutionMode.values())
1928 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1929 >        for (Integer v1 : new Integer[] { 1, null })
1930 >    {
1931 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1932 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1933 >        final Noop r = new Noop();
1934 >
1935 >        assertTrue(f.cancel(mayInterruptIfRunning));
1936 >        g.complete(v1);
1937 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1938 >
1939 >        checkCompletedWithWrappedCancellationException(h);
1940 >        checkCancelled(f);
1941 >        assertEquals(0, r.invocationCount);
1942 >        checkCompletedNormally(g, v1);
1943 >    }}
1944  
1945      /**
1946       * applyToEither result completes normally after normal completion
1947       * of either source
1948       */
1949 <    public void testApplyToEither() {
1950 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1951 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1952 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1953 <        f.complete(one);
1954 <        checkCompletedNormally(g, two);
1955 <        f2.complete(one);
1956 <        checkCompletedNormally(g, two);
1957 <
1958 <        f = new CompletableFuture<>();
1959 <        f.complete(one);
1960 <        f2 = new CompletableFuture<>();
1961 <        g = f.applyToEither(f2, inc);
1962 <        checkCompletedNormally(g, two);
1963 <    }
1949 >    public void testApplyToEither_normalCompletion1() {
1950 >        for (ExecutionMode m : ExecutionMode.values())
1951 >        for (Integer v1 : new Integer[] { 1, null })
1952 >        for (Integer v2 : new Integer[] { 2, null })
1953 >    {
1954 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1955 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1956 >        final IncFunction r = new IncFunction();
1957 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1958 >
1959 >        f.complete(v1);
1960 >        checkCompletedNormally(h, inc(v1));
1961 >        g.complete(v2);
1962 >
1963 >        checkCompletedNormally(f, v1);
1964 >        checkCompletedNormally(g, v2);
1965 >        checkCompletedNormally(h, inc(v1));
1966 >    }}
1967 >
1968 >    public void testApplyToEither_normalCompletion2() {
1969 >        for (ExecutionMode m : ExecutionMode.values())
1970 >        for (Integer v1 : new Integer[] { 1, null })
1971 >        for (Integer v2 : new Integer[] { 2, null })
1972 >    {
1973 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1974 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1975 >        final IncFunction r = new IncFunction();
1976 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1977 >
1978 >        g.complete(v2);
1979 >        checkCompletedNormally(h, inc(v2));
1980 >        f.complete(v1);
1981 >
1982 >        checkCompletedNormally(f, v1);
1983 >        checkCompletedNormally(g, v2);
1984 >        checkCompletedNormally(h, inc(v2));
1985 >        }}
1986 >
1987 >    public void testApplyToEither_normalCompletion3() {
1988 >        for (ExecutionMode m : ExecutionMode.values())
1989 >        for (Integer v1 : new Integer[] { 1, null })
1990 >        for (Integer v2 : new Integer[] { 2, null })
1991 >    {
1992 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1993 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1994 >        final IncFunction r = new IncFunction();
1995 >
1996 >        f.complete(v1);
1997 >        g.complete(v2);
1998 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1999 >
2000 >        checkCompletedNormally(f, v1);
2001 >        checkCompletedNormally(g, v2);
2002 >
2003 >        // unspecified behavior
2004 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
2005 >                   Objects.equals(h.join(), inc(v2)));
2006 >        assertEquals(1, r.invocationCount);
2007 >    }}
2008  
2009      /**
2010       * applyToEither result completes exceptionally after exceptional
2011       * completion of either source
2012       */
2013 <    public void testApplyToEither2() {
2014 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2015 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2016 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2017 <        f.completeExceptionally(new CFException());
2018 <        f2.complete(one);
2019 <        checkCompletedWithWrappedCFException(g);
2013 >    public void testApplyToEither_exceptionalCompletion1() {
2014 >        for (ExecutionMode m : ExecutionMode.values())
2015 >        for (Integer v1 : new Integer[] { 1, null })
2016 >    {
2017 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2018 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2019 >        final IncFunction r = new IncFunction();
2020 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2021 >        final CFException ex = new CFException();
2022 >
2023 >        f.completeExceptionally(ex);
2024 >        checkCompletedWithWrappedCFException(h, ex);
2025 >        g.complete(v1);
2026 >
2027 >        assertEquals(0, r.invocationCount);
2028 >        checkCompletedNormally(g, v1);
2029 >        checkCompletedWithWrappedCFException(f, ex);
2030 >        checkCompletedWithWrappedCFException(h, ex);
2031 >    }}
2032 >
2033 >    public void testApplyToEither_exceptionalCompletion2() {
2034 >        for (ExecutionMode m : ExecutionMode.values())
2035 >        for (Integer v1 : new Integer[] { 1, null })
2036 >    {
2037 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2038 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 >        final IncFunction r = new IncFunction();
2040 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2041 >        final CFException ex = new CFException();
2042 >
2043 >        g.completeExceptionally(ex);
2044 >        checkCompletedWithWrappedCFException(h, ex);
2045 >        f.complete(v1);
2046 >
2047 >        assertEquals(0, r.invocationCount);
2048 >        checkCompletedNormally(f, v1);
2049 >        checkCompletedWithWrappedCFException(g, ex);
2050 >        checkCompletedWithWrappedCFException(h, ex);
2051 >    }}
2052 >
2053 >    public void testApplyToEither_exceptionalCompletion3() {
2054 >        for (ExecutionMode m : ExecutionMode.values())
2055 >        for (Integer v1 : new Integer[] { 1, null })
2056 >    {
2057 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 >        final IncFunction r = new IncFunction();
2060 >        final CFException ex = new CFException();
2061 >
2062 >        g.completeExceptionally(ex);
2063 >        f.complete(v1);
2064 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2065  
2066 <        f = new CompletableFuture<>();
2067 <        f2 = new CompletableFuture<>();
2068 <        f2.completeExceptionally(new CFException());
2069 <        g = f.applyToEither(f2, inc);
2070 <        checkCompletedWithWrappedCFException(g);
2071 <    }
2066 >        // unspecified behavior
2067 >        Integer v;
2068 >        try {
2069 >            assertEquals(inc(v1), h.join());
2070 >            assertEquals(1, r.invocationCount);
2071 >        } catch (CompletionException ok) {
2072 >            checkCompletedWithWrappedCFException(h, ex);
2073 >            assertEquals(0, r.invocationCount);
2074 >        }
2075 >
2076 >        checkCompletedWithWrappedCFException(g, ex);
2077 >        checkCompletedNormally(f, v1);
2078 >    }}
2079 >
2080 >    public void testApplyToEither_exceptionalCompletion4() {
2081 >        for (ExecutionMode m : ExecutionMode.values())
2082 >        for (Integer v1 : new Integer[] { 1, null })
2083 >    {
2084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086 >        final IncFunction r = new IncFunction();
2087 >        final CFException ex = new CFException();
2088 >
2089 >        f.completeExceptionally(ex);
2090 >        g.complete(v1);
2091 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2092 >
2093 >        // unspecified behavior
2094 >        Integer v;
2095 >        try {
2096 >            assertEquals(inc(v1), h.join());
2097 >            assertEquals(1, r.invocationCount);
2098 >        } catch (CompletionException ok) {
2099 >            checkCompletedWithWrappedCFException(h, ex);
2100 >            assertEquals(0, r.invocationCount);
2101 >        }
2102 >
2103 >        checkCompletedWithWrappedCFException(f, ex);
2104 >        checkCompletedNormally(g, v1);
2105 >    }}
2106  
2107      /**
2108       * applyToEither result completes exceptionally if action does
2109       */
2110 <    public void testApplyToEither3() {
2111 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2112 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2113 <        FailingFunction r = new FailingFunction();
2114 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2115 <        f2.complete(two);
2116 <        checkCompletedWithWrappedCFException(g);
2117 <    }
2110 >    public void testApplyToEither_actionFailed1() {
2111 >        for (ExecutionMode m : ExecutionMode.values())
2112 >        for (Integer v1 : new Integer[] { 1, null })
2113 >        for (Integer v2 : new Integer[] { 2, null })
2114 >    {
2115 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2116 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2117 >        final FailingFunction r = new FailingFunction();
2118 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2119 >
2120 >        f.complete(v1);
2121 >        checkCompletedWithWrappedCFException(h);
2122 >        g.complete(v2);
2123 >        checkCompletedNormally(f, v1);
2124 >        checkCompletedNormally(g, v2);
2125 >    }}
2126 >
2127 >    public void testApplyToEither_actionFailed2() {
2128 >        for (ExecutionMode m : ExecutionMode.values())
2129 >        for (Integer v1 : new Integer[] { 1, null })
2130 >        for (Integer v2 : new Integer[] { 2, null })
2131 >    {
2132 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2133 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2134 >        final FailingFunction r = new FailingFunction();
2135 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2136 >
2137 >        g.complete(v2);
2138 >        checkCompletedWithWrappedCFException(h);
2139 >        f.complete(v1);
2140 >        checkCompletedNormally(f, v1);
2141 >        checkCompletedNormally(g, v2);
2142 >    }}
2143  
2144      /**
2145       * applyToEither result completes exceptionally if either source cancelled
2146       */
2147 <    public void testApplyToEither4() {
2148 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2149 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2150 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2151 <        assertTrue(f.cancel(true));
2152 <        checkCompletedWithWrappedCancellationException(g);
2153 <        f = new CompletableFuture<>();
2154 <        f2 = new CompletableFuture<>();
2155 <        assertTrue(f2.cancel(true));
2156 <        checkCompletedWithWrappedCancellationException(g);
2157 <    }
2147 >    public void testApplyToEither_sourceCancelled1() {
2148 >        for (ExecutionMode m : ExecutionMode.values())
2149 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2150 >        for (Integer v1 : new Integer[] { 1, null })
2151 >    {
2152 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2153 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2154 >        final IncFunction r = new IncFunction();
2155 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2156 >
2157 >        assertTrue(f.cancel(mayInterruptIfRunning));
2158 >        checkCompletedWithWrappedCancellationException(h);
2159 >        g.complete(v1);
2160 >
2161 >        checkCancelled(f);
2162 >        assertEquals(0, r.invocationCount);
2163 >        checkCompletedNormally(g, v1);
2164 >        checkCompletedWithWrappedCancellationException(h);
2165 >    }}
2166 >
2167 >    public void testApplyToEither_sourceCancelled2() {
2168 >        for (ExecutionMode m : ExecutionMode.values())
2169 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2170 >        for (Integer v1 : new Integer[] { 1, null })
2171 >    {
2172 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2173 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2174 >        final IncFunction r = new IncFunction();
2175 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2176 >
2177 >        assertTrue(g.cancel(mayInterruptIfRunning));
2178 >        checkCompletedWithWrappedCancellationException(h);
2179 >        f.complete(v1);
2180 >
2181 >        checkCancelled(g);
2182 >        assertEquals(0, r.invocationCount);
2183 >        checkCompletedNormally(f, v1);
2184 >        checkCompletedWithWrappedCancellationException(h);
2185 >    }}
2186 >
2187 >    public void testApplyToEither_sourceCancelled3() {
2188 >        for (ExecutionMode m : ExecutionMode.values())
2189 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2190 >        for (Integer v1 : new Integer[] { 1, null })
2191 >    {
2192 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2193 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2194 >        final IncFunction r = new IncFunction();
2195 >
2196 >        assertTrue(g.cancel(mayInterruptIfRunning));
2197 >        f.complete(v1);
2198 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2199 >
2200 >        // unspecified behavior
2201 >        Integer v;
2202 >        try {
2203 >            assertEquals(inc(v1), h.join());
2204 >            assertEquals(1, r.invocationCount);
2205 >        } catch (CompletionException ok) {
2206 >            checkCompletedWithWrappedCancellationException(h);
2207 >            assertEquals(0, r.invocationCount);
2208 >        }
2209 >
2210 >        checkCancelled(g);
2211 >        checkCompletedNormally(f, v1);
2212 >    }}
2213 >
2214 >    public void testApplyToEither_sourceCancelled4() {
2215 >        for (ExecutionMode m : ExecutionMode.values())
2216 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2217 >        for (Integer v1 : new Integer[] { 1, null })
2218 >    {
2219 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2220 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2221 >        final IncFunction r = new IncFunction();
2222 >
2223 >        assertTrue(f.cancel(mayInterruptIfRunning));
2224 >        g.complete(v1);
2225 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2226 >
2227 >        // unspecified behavior
2228 >        Integer v;
2229 >        try {
2230 >            assertEquals(inc(v1), h.join());
2231 >            assertEquals(1, r.invocationCount);
2232 >        } catch (CompletionException ok) {
2233 >            checkCompletedWithWrappedCancellationException(h);
2234 >            assertEquals(0, r.invocationCount);
2235 >        }
2236 >
2237 >        checkCancelled(f);
2238 >        checkCompletedNormally(g, v1);
2239 >    }}
2240  
2241      /**
2242       * acceptEither result completes normally after normal completion
2243       * of either source
2244       */
2245 <    public void testAcceptEither() {
2246 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2247 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248 <        IncAction r = new IncAction();
2249 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2250 <        f.complete(one);
2251 <        checkCompletedNormally(g, null);
2252 <        f2.complete(one);
2253 <        checkCompletedNormally(g, null);
1081 <        assertEquals(r.value, 2);
2245 >    public void testAcceptEither_normalCompletion1() {
2246 >        for (ExecutionMode m : ExecutionMode.values())
2247 >        for (Integer v1 : new Integer[] { 1, null })
2248 >        for (Integer v2 : new Integer[] { 2, null })
2249 >    {
2250 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2251 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2252 >        final IncAction r = new IncAction();
2253 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2254  
2255 <        r = new IncAction();
2256 <        f = new CompletableFuture<>();
2257 <        f.complete(one);
2258 <        f2 = new CompletableFuture<>();
2259 <        g = f.acceptEither(f2, r);
2260 <        checkCompletedNormally(g, null);
2261 <        assertEquals(r.value, 2);
2262 <    }
2255 >        f.complete(v1);
2256 >        checkCompletedNormally(h, null);
2257 >        assertEquals(inc(v1), r.value);
2258 >        g.complete(v2);
2259 >
2260 >        checkCompletedNormally(f, v1);
2261 >        checkCompletedNormally(g, v2);
2262 >        checkCompletedNormally(h, null);
2263 >    }}
2264 >
2265 >    public void testAcceptEither_normalCompletion2() {
2266 >        for (ExecutionMode m : ExecutionMode.values())
2267 >        for (Integer v1 : new Integer[] { 1, null })
2268 >        for (Integer v2 : new Integer[] { 2, null })
2269 >    {
2270 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2271 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2272 >        final IncAction r = new IncAction();
2273 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2274 >
2275 >        g.complete(v2);
2276 >        checkCompletedNormally(h, null);
2277 >        assertEquals(inc(v2), r.value);
2278 >        f.complete(v1);
2279 >
2280 >        checkCompletedNormally(f, v1);
2281 >        checkCompletedNormally(g, v2);
2282 >        checkCompletedNormally(h, null);
2283 >    }}
2284 >
2285 >    public void testAcceptEither_normalCompletion3() {
2286 >        for (ExecutionMode m : ExecutionMode.values())
2287 >        for (Integer v1 : new Integer[] { 1, null })
2288 >        for (Integer v2 : new Integer[] { 2, null })
2289 >    {
2290 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2291 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2292 >        final IncAction r = new IncAction();
2293 >
2294 >        f.complete(v1);
2295 >        g.complete(v2);
2296 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2297 >
2298 >        checkCompletedNormally(h, null);
2299 >        checkCompletedNormally(f, v1);
2300 >        checkCompletedNormally(g, v2);
2301 >
2302 >        // unspecified behavior
2303 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2304 >                   Objects.equals(r.value, inc(v2)));
2305 >    }}
2306  
2307      /**
2308       * acceptEither result completes exceptionally after exceptional
2309       * completion of either source
2310       */
2311 <    public void testAcceptEither2() {
2312 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2313 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2314 <        IncAction r = new IncAction();
2315 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2316 <        f.completeExceptionally(new CFException());
2317 <        f2.complete(one);
2318 <        checkCompletedWithWrappedCFException(g);
2311 >    public void testAcceptEither_exceptionalCompletion1() {
2312 >        for (ExecutionMode m : ExecutionMode.values())
2313 >        for (Integer v1 : new Integer[] { 1, null })
2314 >    {
2315 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2316 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2317 >        final IncAction r = new IncAction();
2318 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2319 >        final CFException ex = new CFException();
2320 >
2321 >        f.completeExceptionally(ex);
2322 >        checkCompletedWithWrappedCFException(h, ex);
2323 >        g.complete(v1);
2324 >
2325 >        assertEquals(0, r.invocationCount);
2326 >        checkCompletedNormally(g, v1);
2327 >        checkCompletedWithWrappedCFException(f, ex);
2328 >        checkCompletedWithWrappedCFException(h, ex);
2329 >    }}
2330 >
2331 >    public void testAcceptEither_exceptionalCompletion2() {
2332 >        for (ExecutionMode m : ExecutionMode.values())
2333 >        for (Integer v1 : new Integer[] { 1, null })
2334 >    {
2335 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2336 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2337 >        final IncAction r = new IncAction();
2338 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2339 >        final CFException ex = new CFException();
2340 >
2341 >        g.completeExceptionally(ex);
2342 >        checkCompletedWithWrappedCFException(h, ex);
2343 >        f.complete(v1);
2344 >
2345 >        assertEquals(0, r.invocationCount);
2346 >        checkCompletedNormally(f, v1);
2347 >        checkCompletedWithWrappedCFException(g, ex);
2348 >        checkCompletedWithWrappedCFException(h, ex);
2349 >    }}
2350 >
2351 >    public void testAcceptEither_exceptionalCompletion3() {
2352 >        for (ExecutionMode m : ExecutionMode.values())
2353 >        for (Integer v1 : new Integer[] { 1, null })
2354 >    {
2355 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2356 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2357 >        final IncAction r = new IncAction();
2358 >        final CFException ex = new CFException();
2359 >
2360 >        g.completeExceptionally(ex);
2361 >        f.complete(v1);
2362 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2363  
2364 <        r = new IncAction();
2365 <        f = new CompletableFuture<>();
2366 <        f2 = new CompletableFuture<>();
2367 <        f2.completeExceptionally(new CFException());
2368 <        g = f.acceptEither(f2, r);
2369 <        checkCompletedWithWrappedCFException(g);
2370 <    }
2364 >        // unspecified behavior
2365 >        Integer v;
2366 >        try {
2367 >            assertNull(h.join());
2368 >            assertEquals(1, r.invocationCount);
2369 >            assertEquals(inc(v1), r.value);
2370 >        } catch (CompletionException ok) {
2371 >            checkCompletedWithWrappedCFException(h, ex);
2372 >            assertEquals(0, r.invocationCount);
2373 >        }
2374 >
2375 >        checkCompletedWithWrappedCFException(g, ex);
2376 >        checkCompletedNormally(f, v1);
2377 >    }}
2378 >
2379 >    public void testAcceptEither_exceptionalCompletion4() {
2380 >        for (ExecutionMode m : ExecutionMode.values())
2381 >        for (Integer v1 : new Integer[] { 1, null })
2382 >    {
2383 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2384 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2385 >        final IncAction r = new IncAction();
2386 >        final CFException ex = new CFException();
2387 >
2388 >        f.completeExceptionally(ex);
2389 >        g.complete(v1);
2390 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2391 >
2392 >        // unspecified behavior
2393 >        Integer v;
2394 >        try {
2395 >            assertNull(h.join());
2396 >            assertEquals(1, r.invocationCount);
2397 >            assertEquals(inc(v1), r.value);
2398 >        } catch (CompletionException ok) {
2399 >            checkCompletedWithWrappedCFException(h, ex);
2400 >            assertEquals(0, r.invocationCount);
2401 >        }
2402 >
2403 >        checkCompletedWithWrappedCFException(f, ex);
2404 >        checkCompletedNormally(g, v1);
2405 >    }}
2406  
2407      /**
2408       * acceptEither result completes exceptionally if action does
2409       */
2410 <    public void testAcceptEither3() {
2411 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2412 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2413 <        FailingConsumer r = new FailingConsumer();
2414 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2415 <        f2.complete(two);
2416 <        checkCompletedWithWrappedCFException(g);
2417 <    }
2410 >    public void testAcceptEither_actionFailed1() {
2411 >        for (ExecutionMode m : ExecutionMode.values())
2412 >        for (Integer v1 : new Integer[] { 1, null })
2413 >        for (Integer v2 : new Integer[] { 2, null })
2414 >    {
2415 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2416 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2417 >        final FailingConsumer r = new FailingConsumer();
2418 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2419 >
2420 >        f.complete(v1);
2421 >        checkCompletedWithWrappedCFException(h);
2422 >        g.complete(v2);
2423 >        checkCompletedNormally(f, v1);
2424 >        checkCompletedNormally(g, v2);
2425 >    }}
2426 >
2427 >    public void testAcceptEither_actionFailed2() {
2428 >        for (ExecutionMode m : ExecutionMode.values())
2429 >        for (Integer v1 : new Integer[] { 1, null })
2430 >        for (Integer v2 : new Integer[] { 2, null })
2431 >    {
2432 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2433 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2434 >        final FailingConsumer r = new FailingConsumer();
2435 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2436 >
2437 >        g.complete(v2);
2438 >        checkCompletedWithWrappedCFException(h);
2439 >        f.complete(v1);
2440 >        checkCompletedNormally(f, v1);
2441 >        checkCompletedNormally(g, v2);
2442 >    }}
2443  
2444      /**
2445       * acceptEither result completes exceptionally if either source cancelled
2446       */
2447 <    public void testAcceptEither4() {
2448 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2449 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2450 <        IncAction r = new IncAction();
2451 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2452 <        assertTrue(f.cancel(true));
2453 <        checkCompletedWithWrappedCancellationException(g);
2454 <        f = new CompletableFuture<>();
2455 <        f2 = new CompletableFuture<>();
2456 <        assertTrue(f2.cancel(true));
2457 <        checkCompletedWithWrappedCancellationException(g);
2458 <    }
2447 >    public void testAcceptEither_sourceCancelled1() {
2448 >        for (ExecutionMode m : ExecutionMode.values())
2449 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2450 >        for (Integer v1 : new Integer[] { 1, null })
2451 >    {
2452 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2453 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 >        final IncAction r = new IncAction();
2455 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2456 >
2457 >        assertTrue(f.cancel(mayInterruptIfRunning));
2458 >        checkCompletedWithWrappedCancellationException(h);
2459 >        g.complete(v1);
2460 >
2461 >        checkCancelled(f);
2462 >        assertEquals(0, r.invocationCount);
2463 >        checkCompletedNormally(g, v1);
2464 >        checkCompletedWithWrappedCancellationException(h);
2465 >    }}
2466 >
2467 >    public void testAcceptEither_sourceCancelled2() {
2468 >        for (ExecutionMode m : ExecutionMode.values())
2469 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2470 >        for (Integer v1 : new Integer[] { 1, null })
2471 >    {
2472 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2473 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2474 >        final IncAction r = new IncAction();
2475 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2476 >
2477 >        assertTrue(g.cancel(mayInterruptIfRunning));
2478 >        checkCompletedWithWrappedCancellationException(h);
2479 >        f.complete(v1);
2480 >
2481 >        checkCancelled(g);
2482 >        assertEquals(0, r.invocationCount);
2483 >        checkCompletedNormally(f, v1);
2484 >        checkCompletedWithWrappedCancellationException(h);
2485 >    }}
2486 >
2487 >    public void testAcceptEither_sourceCancelled3() {
2488 >        for (ExecutionMode m : ExecutionMode.values())
2489 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2490 >        for (Integer v1 : new Integer[] { 1, null })
2491 >    {
2492 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2493 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2494 >        final IncAction r = new IncAction();
2495 >
2496 >        assertTrue(g.cancel(mayInterruptIfRunning));
2497 >        f.complete(v1);
2498 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2499  
2500 +        // unspecified behavior
2501 +        Integer v;
2502 +        try {
2503 +            assertNull(h.join());
2504 +            assertEquals(1, r.invocationCount);
2505 +            assertEquals(inc(v1), r.value);
2506 +        } catch (CompletionException ok) {
2507 +            checkCompletedWithWrappedCancellationException(h);
2508 +            assertEquals(0, r.invocationCount);
2509 +        }
2510 +
2511 +        checkCancelled(g);
2512 +        checkCompletedNormally(f, v1);
2513 +    }}
2514 +
2515 +    public void testAcceptEither_sourceCancelled4() {
2516 +        for (ExecutionMode m : ExecutionMode.values())
2517 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2518 +        for (Integer v1 : new Integer[] { 1, null })
2519 +    {
2520 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2521 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2522 +        final IncAction r = new IncAction();
2523 +
2524 +        assertTrue(f.cancel(mayInterruptIfRunning));
2525 +        g.complete(v1);
2526 +        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2527 +
2528 +        // unspecified behavior
2529 +        Integer v;
2530 +        try {
2531 +            assertNull(h.join());
2532 +            assertEquals(1, r.invocationCount);
2533 +            assertEquals(inc(v1), r.value);
2534 +        } catch (CompletionException ok) {
2535 +            checkCompletedWithWrappedCancellationException(h);
2536 +            assertEquals(0, r.invocationCount);
2537 +        }
2538 +
2539 +        checkCancelled(f);
2540 +        checkCompletedNormally(g, v1);
2541 +    }}
2542  
2543      /**
2544       * runAfterEither result completes normally after normal completion
2545       * of either source
2546       */
2547 <    public void testRunAfterEither() {
2548 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2549 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2550 <        Noop r = new Noop();
2551 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2552 <        f.complete(one);
2553 <        checkCompletedNormally(g, null);
2554 <        f2.complete(one);
2555 <        checkCompletedNormally(g, null);
1155 <        assertTrue(r.ran);
2547 >    public void testRunAfterEither_normalCompletion1() {
2548 >        for (ExecutionMode m : ExecutionMode.values())
2549 >        for (Integer v1 : new Integer[] { 1, null })
2550 >        for (Integer v2 : new Integer[] { 2, null })
2551 >    {
2552 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2553 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2554 >        final Noop r = new Noop();
2555 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2556  
2557 <        r = new Noop();
2558 <        f = new CompletableFuture<>();
2559 <        f.complete(one);
2560 <        f2 = new CompletableFuture<>();
2561 <        g = f.runAfterEither(f2, r);
2562 <        checkCompletedNormally(g, null);
2563 <        assertTrue(r.ran);
2564 <    }
2557 >        f.complete(v1);
2558 >        checkCompletedNormally(h, null);
2559 >        assertEquals(1, r.invocationCount);
2560 >        g.complete(v2);
2561 >
2562 >        checkCompletedNormally(f, v1);
2563 >        checkCompletedNormally(g, v2);
2564 >        checkCompletedNormally(h, null);
2565 >        assertEquals(1, r.invocationCount);
2566 >    }}
2567 >
2568 >    public void testRunAfterEither_normalCompletion2() {
2569 >        for (ExecutionMode m : ExecutionMode.values())
2570 >        for (Integer v1 : new Integer[] { 1, null })
2571 >        for (Integer v2 : new Integer[] { 2, null })
2572 >    {
2573 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2574 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2575 >        final Noop r = new Noop();
2576 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2577 >
2578 >        g.complete(v2);
2579 >        checkCompletedNormally(h, null);
2580 >        assertEquals(1, r.invocationCount);
2581 >        f.complete(v1);
2582 >
2583 >        checkCompletedNormally(f, v1);
2584 >        checkCompletedNormally(g, v2);
2585 >        checkCompletedNormally(h, null);
2586 >        assertEquals(1, r.invocationCount);
2587 >        }}
2588 >
2589 >    public void testRunAfterEither_normalCompletion3() {
2590 >        for (ExecutionMode m : ExecutionMode.values())
2591 >        for (Integer v1 : new Integer[] { 1, null })
2592 >        for (Integer v2 : new Integer[] { 2, null })
2593 >    {
2594 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2595 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2596 >        final Noop r = new Noop();
2597 >
2598 >        f.complete(v1);
2599 >        g.complete(v2);
2600 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2601 >
2602 >        checkCompletedNormally(h, null);
2603 >        checkCompletedNormally(f, v1);
2604 >        checkCompletedNormally(g, v2);
2605 >        assertEquals(1, r.invocationCount);
2606 >    }}
2607  
2608      /**
2609       * runAfterEither result completes exceptionally after exceptional
2610       * completion of either source
2611       */
2612 <    public void testRunAfterEither2() {
2613 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2614 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2615 <        Noop r = new Noop();
2616 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2617 <        f.completeExceptionally(new CFException());
2618 <        f2.complete(one);
2619 <        checkCompletedWithWrappedCFException(g);
2612 >    public void testRunAfterEither_exceptionalCompletion1() {
2613 >        for (ExecutionMode m : ExecutionMode.values())
2614 >        for (Integer v1 : new Integer[] { 1, null })
2615 >    {
2616 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2617 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2618 >        final Noop r = new Noop();
2619 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2620 >        final CFException ex = new CFException();
2621 >
2622 >        f.completeExceptionally(ex);
2623 >        checkCompletedWithWrappedCFException(h, ex);
2624 >        g.complete(v1);
2625 >
2626 >        assertEquals(0, r.invocationCount);
2627 >        checkCompletedNormally(g, v1);
2628 >        checkCompletedWithWrappedCFException(f, ex);
2629 >        checkCompletedWithWrappedCFException(h, ex);
2630 >    }}
2631 >
2632 >    public void testRunAfterEither_exceptionalCompletion2() {
2633 >        for (ExecutionMode m : ExecutionMode.values())
2634 >        for (Integer v1 : new Integer[] { 1, null })
2635 >    {
2636 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2637 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2638 >        final Noop r = new Noop();
2639 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640 >        final CFException ex = new CFException();
2641 >
2642 >        g.completeExceptionally(ex);
2643 >        checkCompletedWithWrappedCFException(h, ex);
2644 >        f.complete(v1);
2645 >
2646 >        assertEquals(0, r.invocationCount);
2647 >        checkCompletedNormally(f, v1);
2648 >        checkCompletedWithWrappedCFException(g, ex);
2649 >        checkCompletedWithWrappedCFException(h, ex);
2650 >    }}
2651 >
2652 >    public void testRunAfterEither_exceptionalCompletion3() {
2653 >        for (ExecutionMode m : ExecutionMode.values())
2654 >        for (Integer v1 : new Integer[] { 1, null })
2655 >    {
2656 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2657 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2658 >        final Noop r = new Noop();
2659 >        final CFException ex = new CFException();
2660 >
2661 >        g.completeExceptionally(ex);
2662 >        f.complete(v1);
2663 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2664  
2665 <        r = new Noop();
2666 <        f = new CompletableFuture<>();
2667 <        f2 = new CompletableFuture<>();
2668 <        f2.completeExceptionally(new CFException());
2669 <        g = f.runAfterEither(f2, r);
2670 <        checkCompletedWithWrappedCFException(g);
2671 <    }
2665 >        // unspecified behavior
2666 >        Integer v;
2667 >        try {
2668 >            assertNull(h.join());
2669 >            assertEquals(1, r.invocationCount);
2670 >        } catch (CompletionException ok) {
2671 >            checkCompletedWithWrappedCFException(h, ex);
2672 >            assertEquals(0, r.invocationCount);
2673 >        }
2674 >
2675 >        checkCompletedWithWrappedCFException(g, ex);
2676 >        checkCompletedNormally(f, v1);
2677 >    }}
2678 >
2679 >    public void testRunAfterEither_exceptionalCompletion4() {
2680 >        for (ExecutionMode m : ExecutionMode.values())
2681 >        for (Integer v1 : new Integer[] { 1, null })
2682 >    {
2683 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2684 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2685 >        final Noop r = new Noop();
2686 >        final CFException ex = new CFException();
2687 >
2688 >        f.completeExceptionally(ex);
2689 >        g.complete(v1);
2690 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2691 >
2692 >        // unspecified behavior
2693 >        Integer v;
2694 >        try {
2695 >            assertNull(h.join());
2696 >            assertEquals(1, r.invocationCount);
2697 >        } catch (CompletionException ok) {
2698 >            checkCompletedWithWrappedCFException(h, ex);
2699 >            assertEquals(0, r.invocationCount);
2700 >        }
2701 >
2702 >        checkCompletedWithWrappedCFException(f, ex);
2703 >        checkCompletedNormally(g, v1);
2704 >    }}
2705  
2706      /**
2707       * runAfterEither result completes exceptionally if action does
2708       */
2709 <    public void testRunAfterEither3() {
2710 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2711 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2712 <        FailingNoop r = new FailingNoop();
2713 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2714 <        f2.complete(two);
2715 <        checkCompletedWithWrappedCFException(g);
2716 <    }
2709 >    public void testRunAfterEither_actionFailed1() {
2710 >        for (ExecutionMode m : ExecutionMode.values())
2711 >        for (Integer v1 : new Integer[] { 1, null })
2712 >        for (Integer v2 : new Integer[] { 2, null })
2713 >    {
2714 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2715 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2716 >        final FailingNoop r = new FailingNoop();
2717 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2718 >
2719 >        f.complete(v1);
2720 >        checkCompletedWithWrappedCFException(h);
2721 >        g.complete(v2);
2722 >        checkCompletedNormally(f, v1);
2723 >        checkCompletedNormally(g, v2);
2724 >    }}
2725 >
2726 >    public void testRunAfterEither_actionFailed2() {
2727 >        for (ExecutionMode m : ExecutionMode.values())
2728 >        for (Integer v1 : new Integer[] { 1, null })
2729 >        for (Integer v2 : new Integer[] { 2, null })
2730 >    {
2731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2733 >        final FailingNoop r = new FailingNoop();
2734 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2735 >
2736 >        g.complete(v2);
2737 >        checkCompletedWithWrappedCFException(h);
2738 >        f.complete(v1);
2739 >        checkCompletedNormally(f, v1);
2740 >        checkCompletedNormally(g, v2);
2741 >    }}
2742  
2743      /**
2744       * runAfterEither result completes exceptionally if either source cancelled
2745       */
2746 <    public void testRunAfterEither4() {
2747 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2748 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2749 <        Noop r = new Noop();
2750 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2751 <        assertTrue(f.cancel(true));
2752 <        checkCompletedWithWrappedCancellationException(g);
2753 <        f = new CompletableFuture<>();
2754 <        f2 = new CompletableFuture<>();
2755 <        assertTrue(f2.cancel(true));
2756 <        checkCompletedWithWrappedCancellationException(g);
2757 <    }
2746 >    public void testRunAfterEither_sourceCancelled1() {
2747 >        for (ExecutionMode m : ExecutionMode.values())
2748 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2749 >        for (Integer v1 : new Integer[] { 1, null })
2750 >    {
2751 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2752 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2753 >        final Noop r = new Noop();
2754 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2755 >
2756 >        assertTrue(f.cancel(mayInterruptIfRunning));
2757 >        checkCompletedWithWrappedCancellationException(h);
2758 >        g.complete(v1);
2759 >
2760 >        checkCancelled(f);
2761 >        assertEquals(0, r.invocationCount);
2762 >        checkCompletedNormally(g, v1);
2763 >        checkCompletedWithWrappedCancellationException(h);
2764 >    }}
2765 >
2766 >    public void testRunAfterEither_sourceCancelled2() {
2767 >        for (ExecutionMode m : ExecutionMode.values())
2768 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2769 >        for (Integer v1 : new Integer[] { 1, null })
2770 >    {
2771 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2772 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2773 >        final Noop r = new Noop();
2774 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2775 >
2776 >        assertTrue(g.cancel(mayInterruptIfRunning));
2777 >        checkCompletedWithWrappedCancellationException(h);
2778 >        f.complete(v1);
2779 >
2780 >        checkCancelled(g);
2781 >        assertEquals(0, r.invocationCount);
2782 >        checkCompletedNormally(f, v1);
2783 >        checkCompletedWithWrappedCancellationException(h);
2784 >    }}
2785 >
2786 >    public void testRunAfterEither_sourceCancelled3() {
2787 >        for (ExecutionMode m : ExecutionMode.values())
2788 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2789 >        for (Integer v1 : new Integer[] { 1, null })
2790 >    {
2791 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2792 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2793 >        final Noop r = new Noop();
2794 >
2795 >        assertTrue(g.cancel(mayInterruptIfRunning));
2796 >        f.complete(v1);
2797 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2798 >
2799 >        // unspecified behavior
2800 >        Integer v;
2801 >        try {
2802 >            assertNull(h.join());
2803 >            assertEquals(1, r.invocationCount);
2804 >        } catch (CompletionException ok) {
2805 >            checkCompletedWithWrappedCancellationException(h);
2806 >            assertEquals(0, r.invocationCount);
2807 >        }
2808 >
2809 >        checkCancelled(g);
2810 >        checkCompletedNormally(f, v1);
2811 >    }}
2812 >
2813 >    public void testRunAfterEither_sourceCancelled4() {
2814 >        for (ExecutionMode m : ExecutionMode.values())
2815 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2816 >        for (Integer v1 : new Integer[] { 1, null })
2817 >    {
2818 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2819 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2820 >        final Noop r = new Noop();
2821 >
2822 >        assertTrue(f.cancel(mayInterruptIfRunning));
2823 >        g.complete(v1);
2824 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2825 >
2826 >        // unspecified behavior
2827 >        Integer v;
2828 >        try {
2829 >            assertNull(h.join());
2830 >            assertEquals(1, r.invocationCount);
2831 >        } catch (CompletionException ok) {
2832 >            checkCompletedWithWrappedCancellationException(h);
2833 >            assertEquals(0, r.invocationCount);
2834 >        }
2835 >
2836 >        checkCancelled(f);
2837 >        checkCompletedNormally(g, v1);
2838 >    }}
2839  
2840      /**
2841       * thenCompose result completes normally after normal completion of source
2842       */
2843 <    public void testThenCompose() {
2844 <        CompletableFuture<Integer> f, g;
2845 <        CompletableFutureInc r;
2846 <
2847 <        f = new CompletableFuture<>();
2848 <        g = f.thenCompose(r = new CompletableFutureInc());
2849 <        f.complete(one);
2850 <        checkCompletedNormally(g, two);
2851 <        assertTrue(r.ran);
2852 <
2853 <        f = new CompletableFuture<>();
2854 <        f.complete(one);
2855 <        g = f.thenCompose(r = new CompletableFutureInc());
2856 <        checkCompletedNormally(g, two);
2857 <        assertTrue(r.ran);
2858 <    }
2843 >    public void testThenCompose_normalCompletion1() {
2844 >        for (ExecutionMode m : ExecutionMode.values())
2845 >        for (Integer v1 : new Integer[] { 1, null })
2846 >    {
2847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2848 >        final CompletableFutureInc r = new CompletableFutureInc();
2849 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2850 >        f.complete(v1);
2851 >        checkCompletedNormally(g, inc(v1));
2852 >        checkCompletedNormally(f, v1);
2853 >        assertEquals(1, r.invocationCount);
2854 >    }}
2855 >
2856 >    public void testThenCompose_normalCompletion2() {
2857 >        for (ExecutionMode m : ExecutionMode.values())
2858 >        for (Integer v1 : new Integer[] { 1, null })
2859 >    {
2860 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2861 >        final CompletableFutureInc r = new CompletableFutureInc();
2862 >        f.complete(v1);
2863 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2864 >        checkCompletedNormally(g, inc(v1));
2865 >        checkCompletedNormally(f, v1);
2866 >        assertEquals(1, r.invocationCount);
2867 >    }}
2868  
2869      /**
2870       * thenCompose result completes exceptionally after exceptional
2871       * completion of source
2872       */
2873 <    public void testThenCompose2() {
2874 <        CompletableFuture<Integer> f, g;
2875 <        CompletableFutureInc r;
2876 <
2877 <        f = new CompletableFuture<>();
2878 <        g = f.thenCompose(r = new CompletableFutureInc());
2879 <        f.completeExceptionally(new CFException());
2880 <        checkCompletedWithWrappedCFException(g);
2881 <
2882 <        f = new CompletableFuture<>();
2883 <        f.completeExceptionally(new CFException());
2884 <        g = f.thenCompose(r = new CompletableFutureInc());
2885 <        checkCompletedWithWrappedCFException(g);
2886 <    }
2873 >    public void testThenCompose_exceptionalCompletion1() {
2874 >        for (ExecutionMode m : ExecutionMode.values())
2875 >    {
2876 >        final CFException ex = new CFException();
2877 >        final CompletableFutureInc r = new CompletableFutureInc();
2878 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2879 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2880 >        f.completeExceptionally(ex);
2881 >        checkCompletedWithWrappedCFException(g, ex);
2882 >        checkCompletedWithWrappedCFException(f, ex);
2883 >    }}
2884 >
2885 >    public void testThenCompose_exceptionalCompletion2() {
2886 >        for (ExecutionMode m : ExecutionMode.values())
2887 >    {
2888 >        final CFException ex = new CFException();
2889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2890 >        f.completeExceptionally(ex);
2891 >        final CompletableFutureInc r = new CompletableFutureInc();
2892 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2893 >        checkCompletedWithWrappedCFException(g, ex);
2894 >        checkCompletedWithWrappedCFException(f, ex);
2895 >    }}
2896  
2897      /**
2898       * thenCompose result completes exceptionally if action does
2899       */
2900 <    public void testThenCompose3() {
2901 <        CompletableFuture<Integer> f, g;
2902 <        FailingCompletableFutureFunction r;
2903 <
2904 <        f = new CompletableFuture<>();
2905 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2906 <        f.complete(one);
2907 <        checkCompletedWithWrappedCFException(g);
2908 <
2909 <        f = new CompletableFuture<>();
2910 <        f.complete(one);
2911 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2900 >    public void testThenCompose_actionFailed1() {
2901 >        for (ExecutionMode m : ExecutionMode.values())
2902 >        for (Integer v1 : new Integer[] { 1, null })
2903 >    {
2904 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2905 >        final FailingCompletableFutureFunction r
2906 >            = new FailingCompletableFutureFunction();
2907 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2908 >        f.complete(v1);
2909 >        checkCompletedWithWrappedCFException(g);
2910 >        checkCompletedNormally(f, v1);
2911 >    }}
2912 >
2913 >    public void testThenCompose_actionFailed2() {
2914 >        for (ExecutionMode m : ExecutionMode.values())
2915 >        for (Integer v1 : new Integer[] { 1, null })
2916 >    {
2917 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2918 >        f.complete(v1);
2919 >        final FailingCompletableFutureFunction r
2920 >            = new FailingCompletableFutureFunction();
2921 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2922          checkCompletedWithWrappedCFException(g);
2923 <    }
2923 >        checkCompletedNormally(f, v1);
2924 >    }}
2925  
2926      /**
2927       * thenCompose result completes exceptionally if source cancelled
2928       */
2929 <    public void testThenCompose4() {
2930 <        CompletableFuture<Integer> f, g;
2931 <        CompletableFutureInc r;
2932 <
2933 <        f = new CompletableFuture<>();
2934 <        g = f.thenCompose(r = new CompletableFutureInc());
2935 <        assertTrue(f.cancel(true));
2929 >    public void testThenCompose_sourceCancelled1() {
2930 >        for (ExecutionMode m : ExecutionMode.values())
2931 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2932 >    {
2933 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2934 >        final CompletableFutureInc r = new CompletableFutureInc();
2935 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2936 >        assertTrue(f.cancel(mayInterruptIfRunning));
2937          checkCompletedWithWrappedCancellationException(g);
2938 +        checkCancelled(f);
2939 +    }}
2940  
2941 <        f = new CompletableFuture<>();
2942 <        assertTrue(f.cancel(true));
2943 <        g = f.thenCompose(r = new CompletableFutureInc());
2941 >    public void testThenCompose_sourceCancelled2() {
2942 >        for (ExecutionMode m : ExecutionMode.values())
2943 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2944 >    {
2945 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2946 >        assertTrue(f.cancel(mayInterruptIfRunning));
2947 >        final CompletableFutureInc r = new CompletableFutureInc();
2948 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2949          checkCompletedWithWrappedCancellationException(g);
2950 <    }
2951 <
2950 >        checkCancelled(f);
2951 >    }}
2952  
2953      // asyncs
2954  
# Line 1320 | Line 2982 | public class CompletableFutureTest exten
2982          try {
2983              g.join();
2984              shouldThrow();
2985 <        } catch (Exception ok) {
1324 <        }
2985 >        } catch (CompletionException success) {}
2986          checkCompletedWithWrappedCFException(g);
2987      }
2988  
# Line 1399 | Line 3060 | public class CompletableFutureTest exten
3060          CompletableFuture<Void> g = f.thenAcceptAsync(r);
3061          f.complete(one);
3062          checkCompletedNormally(g, null);
3063 <        assertEquals(r.value, 2);
3063 >        assertEquals(r.value, (Integer) 2);
3064      }
3065  
3066      /**
# Line 1436 | Line 3097 | public class CompletableFutureTest exten
3097          checkCompletedWithWrappedCancellationException(g);
3098      }
3099  
1439    /**
1440     * thenCombineAsync result completes normally after normal
1441     * completion of sources
1442     */
1443    public void testThenCombineAsync() {
1444        CompletableFuture<Integer> f, g, h;
1445
1446        f = new CompletableFuture<>();
1447        g = new CompletableFuture<>();
1448        h = f.thenCombineAsync(g, subtract);
1449        f.complete(3);
1450        checkIncomplete(h);
1451        g.complete(1);
1452        checkCompletedNormally(h, 2);
1453
1454        f = new CompletableFuture<>();
1455        g = new CompletableFuture<>();
1456        h = f.thenCombineAsync(g, subtract);
1457        g.complete(1);
1458        checkIncomplete(h);
1459        f.complete(3);
1460        checkCompletedNormally(h, 2);
1461
1462        f = new CompletableFuture<>();
1463        g = new CompletableFuture<>();
1464        g.complete(1);
1465        f.complete(3);
1466        h = f.thenCombineAsync(g, subtract);
1467        checkCompletedNormally(h, 2);
1468    }
1469
1470    /**
1471     * thenCombineAsync result completes exceptionally after exceptional
1472     * completion of either source
1473     */
1474    public void testThenCombineAsync2() {
1475        CompletableFuture<Integer> f, g, h;
1476
1477        f = new CompletableFuture<>();
1478        g = new CompletableFuture<>();
1479        h = f.thenCombineAsync(g, subtract);
1480        f.completeExceptionally(new CFException());
1481        checkIncomplete(h);
1482        g.complete(1);
1483        checkCompletedWithWrappedCFException(h);
1484
1485        f = new CompletableFuture<>();
1486        g = new CompletableFuture<>();
1487        h = f.thenCombineAsync(g, subtract);
1488        g.completeExceptionally(new CFException());
1489        checkIncomplete(h);
1490        f.complete(3);
1491        checkCompletedWithWrappedCFException(h);
1492
1493        f = new CompletableFuture<>();
1494        g = new CompletableFuture<>();
1495        g.completeExceptionally(new CFException());
1496        f.complete(3);
1497        h = f.thenCombineAsync(g, subtract);
1498        checkCompletedWithWrappedCFException(h);
1499    }
1500
1501    /**
1502     * thenCombineAsync result completes exceptionally if action does
1503     */
1504    public void testThenCombineAsync3() {
1505        CompletableFuture<Integer> f = new CompletableFuture<>();
1506        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1507        FailingBiFunction r = new FailingBiFunction();
1508        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1509        f.complete(one);
1510        checkIncomplete(g);
1511        assertFalse(r.ran);
1512        f2.complete(two);
1513        checkCompletedWithWrappedCFException(g);
1514        assertTrue(r.ran);
1515    }
1516
1517    /**
1518     * thenCombineAsync result completes exceptionally if either source cancelled
1519     */
1520    public void testThenCombineAsync4() {
1521        CompletableFuture<Integer> f, g, h;
1522
1523        f = new CompletableFuture<>();
1524        g = new CompletableFuture<>();
1525        h = f.thenCombineAsync(g, subtract);
1526        assertTrue(f.cancel(true));
1527        checkIncomplete(h);
1528        g.complete(1);
1529        checkCompletedWithWrappedCancellationException(h);
1530
1531        f = new CompletableFuture<>();
1532        g = new CompletableFuture<>();
1533        h = f.thenCombineAsync(g, subtract);
1534        assertTrue(g.cancel(true));
1535        checkIncomplete(h);
1536        f.complete(3);
1537        checkCompletedWithWrappedCancellationException(h);
1538
1539        f = new CompletableFuture<>();
1540        g = new CompletableFuture<>();
1541        g.complete(3);
1542        assertTrue(f.cancel(true));
1543        h = f.thenCombineAsync(g, subtract);
1544        checkCompletedWithWrappedCancellationException(h);
1545
1546        f = new CompletableFuture<>();
1547        g = new CompletableFuture<>();
1548        f.complete(3);
1549        assertTrue(g.cancel(true));
1550        h = f.thenCombineAsync(g, subtract);
1551        checkCompletedWithWrappedCancellationException(h);
1552    }
1553
1554    /**
1555     * thenAcceptBothAsync result completes normally after normal
1556     * completion of sources
1557     */
1558    public void testThenAcceptBothAsync() {
1559        CompletableFuture<Integer> f, g;
1560        CompletableFuture<Void> h;
1561        SubtractAction r;
1562
1563        f = new CompletableFuture<>();
1564        g = new CompletableFuture<>();
1565        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1566        f.complete(3);
1567        checkIncomplete(h);
1568        g.complete(1);
1569        checkCompletedNormally(h, null);
1570        assertEquals(r.value, 2);
1571
1572        f = new CompletableFuture<>();
1573        g = new CompletableFuture<>();
1574        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1575        g.complete(1);
1576        checkIncomplete(h);
1577        f.complete(3);
1578        checkCompletedNormally(h, null);
1579        assertEquals(r.value, 2);
1580
1581        f = new CompletableFuture<>();
1582        g = new CompletableFuture<>();
1583        g.complete(1);
1584        f.complete(3);
1585        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1586        checkCompletedNormally(h, null);
1587        assertEquals(r.value, 2);
1588    }
1589
1590    /**
1591     * thenAcceptBothAsync result completes exceptionally after exceptional
1592     * completion of source
1593     */
1594    public void testThenAcceptBothAsync2() {
1595        CompletableFuture<Integer> f, g;
1596        CompletableFuture<Void> h;
1597        SubtractAction r;
1598
1599        f = new CompletableFuture<>();
1600        g = new CompletableFuture<>();
1601        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1602        f.completeExceptionally(new CFException());
1603        checkIncomplete(h);
1604        g.complete(1);
1605        checkCompletedWithWrappedCFException(h);
1606
1607        f = new CompletableFuture<>();
1608        g = new CompletableFuture<>();
1609        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1610        g.completeExceptionally(new CFException());
1611        checkIncomplete(h);
1612        f.complete(3);
1613        checkCompletedWithWrappedCFException(h);
1614
1615        f = new CompletableFuture<>();
1616        g = new CompletableFuture<>();
1617        f.complete(3);
1618        g.completeExceptionally(new CFException());
1619        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1620        checkCompletedWithWrappedCFException(h);
1621
1622        f = new CompletableFuture<>();
1623        g = new CompletableFuture<>();
1624        f.completeExceptionally(new CFException());
1625        g.complete(3);
1626        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1627        checkCompletedWithWrappedCFException(h);
1628    }
1629
1630    /**
1631     * thenAcceptBothAsync result completes exceptionally if action does
1632     */
1633    public void testThenAcceptBothAsync3() {
1634        CompletableFuture<Integer> f, g;
1635        CompletableFuture<Void> h;
1636        FailingBiConsumer r;
1637
1638        f = new CompletableFuture<>();
1639        g = new CompletableFuture<>();
1640        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1641        f.complete(3);
1642        checkIncomplete(h);
1643        g.complete(1);
1644        checkCompletedWithWrappedCFException(h);
1645
1646        f = new CompletableFuture<>();
1647        g = new CompletableFuture<>();
1648        f.complete(3);
1649        g.complete(1);
1650        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1651        checkCompletedWithWrappedCFException(h);
1652    }
1653
1654    /**
1655     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1656     */
1657    public void testThenAcceptBothAsync4() {
1658        CompletableFuture<Integer> f, g;
1659        CompletableFuture<Void> h;
1660        SubtractAction r;
1661
1662        f = new CompletableFuture<>();
1663        g = new CompletableFuture<>();
1664        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1665        assertTrue(f.cancel(true));
1666        checkIncomplete(h);
1667        g.complete(1);
1668        checkCompletedWithWrappedCancellationException(h);
1669
1670        f = new CompletableFuture<>();
1671        g = new CompletableFuture<>();
1672        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1673        assertTrue(g.cancel(true));
1674        checkIncomplete(h);
1675        f.complete(3);
1676        checkCompletedWithWrappedCancellationException(h);
1677
1678        f = new CompletableFuture<>();
1679        g = new CompletableFuture<>();
1680        f.complete(3);
1681        assertTrue(g.cancel(true));
1682        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1683        checkCompletedWithWrappedCancellationException(h);
1684
1685        f = new CompletableFuture<>();
1686        g = new CompletableFuture<>();
1687        assertTrue(f.cancel(true));
1688        g.complete(3);
1689        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1690        checkCompletedWithWrappedCancellationException(h);
1691    }
1692
1693    /**
1694     * runAfterBothAsync result completes normally after normal
1695     * completion of sources
1696     */
1697    public void testRunAfterBothAsync() {
1698        CompletableFuture<Integer> f = new CompletableFuture<>();
1699        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1700        Noop r = new Noop();
1701        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1702        f.complete(one);
1703        checkIncomplete(g);
1704        f2.complete(two);
1705        checkCompletedNormally(g, null);
1706        assertTrue(r.ran);
1707    }
1708
1709    /**
1710     * runAfterBothAsync result completes exceptionally after exceptional
1711     * completion of source
1712     */
1713    public void testRunAfterBothAsync2() {
1714        CompletableFuture<Integer> f = new CompletableFuture<>();
1715        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1716        Noop r = new Noop();
1717        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1718        f.completeExceptionally(new CFException());
1719        f2.complete(two);
1720        checkCompletedWithWrappedCFException(g);
1721
1722        r = new Noop();
1723        f = new CompletableFuture<>();
1724        f2 = new CompletableFuture<>();
1725        g = f.runAfterBothAsync(f2, r);
1726        f.complete(one);
1727        f2.completeExceptionally(new CFException());
1728        checkCompletedWithWrappedCFException(g);
1729    }
1730
1731    /**
1732     * runAfterBothAsync result completes exceptionally if action does
1733     */
1734    public void testRunAfterBothAsync3() {
1735        CompletableFuture<Integer> f = new CompletableFuture<>();
1736        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1737        FailingNoop r = new FailingNoop();
1738        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1739        f.complete(one);
1740        checkIncomplete(g);
1741        f2.complete(two);
1742        checkCompletedWithWrappedCFException(g);
1743    }
1744
1745    /**
1746     * runAfterBothAsync result completes exceptionally if either source cancelled
1747     */
1748    public void testRunAfterBothAsync4() {
1749        CompletableFuture<Integer> f = new CompletableFuture<>();
1750        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1751        Noop r = new Noop();
1752        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1753        assertTrue(f.cancel(true));
1754        f2.complete(two);
1755        checkCompletedWithWrappedCancellationException(g);
1756
1757        r = new Noop();
1758        f = new CompletableFuture<>();
1759        f2 = new CompletableFuture<>();
1760        g = f.runAfterBothAsync(f2, r);
1761        f.complete(one);
1762        assertTrue(f2.cancel(true));
1763        checkCompletedWithWrappedCancellationException(g);
1764    }
1765
1766    /**
1767     * applyToEitherAsync result completes normally after normal
1768     * completion of sources
1769     */
1770    public void testApplyToEitherAsync() {
1771        CompletableFuture<Integer> f = new CompletableFuture<>();
1772        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1773        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1774        f.complete(one);
1775        checkCompletedNormally(g, two);
1776
1777        f = new CompletableFuture<>();
1778        f.complete(one);
1779        f2 = new CompletableFuture<>();
1780        g = f.applyToEitherAsync(f2, inc);
1781        checkCompletedNormally(g, two);
1782    }
1783
1784    /**
1785     * applyToEitherAsync result completes exceptionally after exceptional
1786     * completion of source
1787     */
1788    public void testApplyToEitherAsync2() {
1789        CompletableFuture<Integer> f = new CompletableFuture<>();
1790        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1791        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1792        f.completeExceptionally(new CFException());
1793        checkCompletedWithWrappedCFException(g);
1794
1795        f = new CompletableFuture<>();
1796        f2 = new CompletableFuture<>();
1797        f2.completeExceptionally(new CFException());
1798        g = f.applyToEitherAsync(f2, inc);
1799        f.complete(one);
1800        checkCompletedWithWrappedCFException(g);
1801    }
1802
1803    /**
1804     * applyToEitherAsync result completes exceptionally if action does
1805     */
1806    public void testApplyToEitherAsync3() {
1807        CompletableFuture<Integer> f = new CompletableFuture<>();
1808        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1809        FailingFunction r = new FailingFunction();
1810        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1811        f.complete(one);
1812        checkCompletedWithWrappedCFException(g);
1813    }
1814
1815    /**
1816     * applyToEitherAsync result completes exceptionally if either source cancelled
1817     */
1818    public void testApplyToEitherAsync4() {
1819        CompletableFuture<Integer> f = new CompletableFuture<>();
1820        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1821        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1822        assertTrue(f.cancel(true));
1823        checkCompletedWithWrappedCancellationException(g);
1824
1825        f = new CompletableFuture<>();
1826        f2 = new CompletableFuture<>();
1827        assertTrue(f2.cancel(true));
1828        g = f.applyToEitherAsync(f2, inc);
1829        checkCompletedWithWrappedCancellationException(g);
1830    }
1831
1832    /**
1833     * acceptEitherAsync result completes normally after normal
1834     * completion of sources
1835     */
1836    public void testAcceptEitherAsync() {
1837        CompletableFuture<Integer> f = new CompletableFuture<>();
1838        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1839        IncAction r = new IncAction();
1840        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1841        f.complete(one);
1842        checkCompletedNormally(g, null);
1843        assertEquals(r.value, 2);
1844
1845        r = new IncAction();
1846        f = new CompletableFuture<>();
1847        f.complete(one);
1848        f2 = new CompletableFuture<>();
1849        g = f.acceptEitherAsync(f2, r);
1850        checkCompletedNormally(g, null);
1851        assertEquals(r.value, 2);
1852    }
1853
1854    /**
1855     * acceptEitherAsync result completes exceptionally after exceptional
1856     * completion of source
1857     */
1858    public void testAcceptEitherAsync2() {
1859        CompletableFuture<Integer> f = new CompletableFuture<>();
1860        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1861        IncAction r = new IncAction();
1862        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1863        f.completeExceptionally(new CFException());
1864        checkCompletedWithWrappedCFException(g);
1865
1866        r = new IncAction();
1867        f = new CompletableFuture<>();
1868        f2 = new CompletableFuture<>();
1869        f2.completeExceptionally(new CFException());
1870        g = f.acceptEitherAsync(f2, r);
1871        f.complete(one);
1872        checkCompletedWithWrappedCFException(g);
1873    }
1874
1875    /**
1876     * acceptEitherAsync result completes exceptionally if action does
1877     */
1878    public void testAcceptEitherAsync3() {
1879        CompletableFuture<Integer> f = new CompletableFuture<>();
1880        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1881        FailingConsumer r = new FailingConsumer();
1882        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1883        f.complete(one);
1884        checkCompletedWithWrappedCFException(g);
1885    }
1886
1887    /**
1888     * acceptEitherAsync result completes exceptionally if either
1889     * source cancelled
1890     */
1891    public void testAcceptEitherAsync4() {
1892        CompletableFuture<Integer> f = new CompletableFuture<>();
1893        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1894        IncAction r = new IncAction();
1895        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1896        assertTrue(f.cancel(true));
1897        checkCompletedWithWrappedCancellationException(g);
1898
1899        r = new IncAction();
1900        f = new CompletableFuture<>();
1901        f2 = new CompletableFuture<>();
1902        assertTrue(f2.cancel(true));
1903        g = f.acceptEitherAsync(f2, r);
1904        checkCompletedWithWrappedCancellationException(g);
1905    }
1906
1907    /**
1908     * runAfterEitherAsync result completes normally after normal
1909     * completion of sources
1910     */
1911    public void testRunAfterEitherAsync() {
1912        CompletableFuture<Integer> f = new CompletableFuture<>();
1913        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1914        Noop r = new Noop();
1915        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1916        f.complete(one);
1917        checkCompletedNormally(g, null);
1918        assertTrue(r.ran);
1919
1920        r = new Noop();
1921        f = new CompletableFuture<>();
1922        f.complete(one);
1923        f2 = new CompletableFuture<>();
1924        g = f.runAfterEitherAsync(f2, r);
1925        checkCompletedNormally(g, null);
1926        assertTrue(r.ran);
1927    }
1928
1929    /**
1930     * runAfterEitherAsync result completes exceptionally after exceptional
1931     * completion of source
1932     */
1933    public void testRunAfterEitherAsync2() {
1934        CompletableFuture<Integer> f = new CompletableFuture<>();
1935        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1936        Noop r = new Noop();
1937        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1938        f.completeExceptionally(new CFException());
1939        checkCompletedWithWrappedCFException(g);
1940
1941        r = new Noop();
1942        f = new CompletableFuture<>();
1943        f2 = new CompletableFuture<>();
1944        f2.completeExceptionally(new CFException());
1945        g = f.runAfterEitherAsync(f2, r);
1946        f.complete(one);
1947        checkCompletedWithWrappedCFException(g);
1948    }
1949
1950    /**
1951     * runAfterEitherAsync result completes exceptionally if action does
1952     */
1953    public void testRunAfterEitherAsync3() {
1954        CompletableFuture<Integer> f = new CompletableFuture<>();
1955        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1956        FailingNoop r = new FailingNoop();
1957        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1958        f.complete(one);
1959        checkCompletedWithWrappedCFException(g);
1960    }
1961
1962    /**
1963     * runAfterEitherAsync result completes exceptionally if either
1964     * source cancelled
1965     */
1966    public void testRunAfterEitherAsync4() {
1967        CompletableFuture<Integer> f = new CompletableFuture<>();
1968        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1969        Noop r = new Noop();
1970        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1971        assertTrue(f.cancel(true));
1972        checkCompletedWithWrappedCancellationException(g);
1973
1974        r = new Noop();
1975        f = new CompletableFuture<>();
1976        f2 = new CompletableFuture<>();
1977        assertTrue(f2.cancel(true));
1978        g = f.runAfterEitherAsync(f2, r);
1979        checkCompletedWithWrappedCancellationException(g);
1980    }
1981
1982    /**
1983     * thenComposeAsync result completes normally after normal
1984     * completion of source
1985     */
1986    public void testThenComposeAsync() {
1987        CompletableFuture<Integer> f, g;
1988        CompletableFutureInc r;
1989
1990        f = new CompletableFuture<>();
1991        g = f.thenComposeAsync(r = new CompletableFutureInc());
1992        f.complete(one);
1993        checkCompletedNormally(g, two);
1994
1995        f = new CompletableFuture<>();
1996        f.complete(one);
1997        g = f.thenComposeAsync(r = new CompletableFutureInc());
1998        checkCompletedNormally(g, two);
1999    }
2000
2001    /**
2002     * thenComposeAsync result completes exceptionally after
2003     * exceptional completion of source
2004     */
2005    public void testThenComposeAsync2() {
2006        CompletableFuture<Integer> f, g;
2007        CompletableFutureInc r;
2008
2009        f = new CompletableFuture<>();
2010        g = f.thenComposeAsync(r = new CompletableFutureInc());
2011        f.completeExceptionally(new CFException());
2012        checkCompletedWithWrappedCFException(g);
2013        assertFalse(r.ran);
2014
2015        f = new CompletableFuture<>();
2016        f.completeExceptionally(new CFException());
2017        g = f.thenComposeAsync(r = new CompletableFutureInc());
2018        checkCompletedWithWrappedCFException(g);
2019        assertFalse(r.ran);
2020    }
2021
2022    /**
2023     * thenComposeAsync result completes exceptionally if action does
2024     */
2025    public void testThenComposeAsync3() {
2026        CompletableFuture<Integer> f, g;
2027        FailingCompletableFutureFunction r;
2028
2029        f = new CompletableFuture<>();
2030        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2031        f.complete(one);
2032        checkCompletedWithWrappedCFException(g);
2033
2034        f = new CompletableFuture<>();
2035        f.complete(one);
2036        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2037        checkCompletedWithWrappedCFException(g);
2038    }
2039
2040    /**
2041     * thenComposeAsync result completes exceptionally if source cancelled
2042     */
2043    public void testThenComposeAsync4() {
2044        CompletableFuture<Integer> f, g;
2045        CompletableFutureInc r;
2046
2047        f = new CompletableFuture<>();
2048        g = f.thenComposeAsync(r = new CompletableFutureInc());
2049        assertTrue(f.cancel(true));
2050        checkCompletedWithWrappedCancellationException(g);
2051
2052        f = new CompletableFuture<>();
2053        assertTrue(f.cancel(true));
2054        g = f.thenComposeAsync(r = new CompletableFutureInc());
2055        checkCompletedWithWrappedCancellationException(g);
2056    }
2057
2058
3100      // async with explicit executors
3101  
3102      /**
# Line 2088 | Line 3129 | public class CompletableFutureTest exten
3129          try {
3130              g.join();
3131              shouldThrow();
3132 <        } catch (Exception ok) {
2092 <        }
3132 >        } catch (CompletionException success) {}
3133          checkCompletedWithWrappedCFException(g);
3134      }
3135  
# Line 2167 | Line 3207 | public class CompletableFutureTest exten
3207          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3208          f.complete(one);
3209          checkCompletedNormally(g, null);
3210 <        assertEquals(r.value, 2);
3210 >        assertEquals(r.value, (Integer) 2);
3211      }
3212  
3213      /**
# Line 2204 | Line 3244 | public class CompletableFutureTest exten
3244          checkCompletedWithWrappedCancellationException(g);
3245      }
3246  
2207    /**
2208     * thenCombineAsync result completes normally after normal
2209     * completion of sources
2210     */
2211    public void testThenCombineAsyncE() {
2212        CompletableFuture<Integer> f, g, h;
2213        ThreadExecutor e = new ThreadExecutor();
2214        int count = 0;
2215
2216        f = new CompletableFuture<>();
2217        g = new CompletableFuture<>();
2218        h = f.thenCombineAsync(g, subtract, e);
2219        f.complete(3);
2220        checkIncomplete(h);
2221        g.complete(1);
2222        checkCompletedNormally(h, 2);
2223        assertEquals(++count, e.count.get());
2224
2225        f = new CompletableFuture<>();
2226        g = new CompletableFuture<>();
2227        h = f.thenCombineAsync(g, subtract, e);
2228        g.complete(1);
2229        checkIncomplete(h);
2230        f.complete(3);
2231        checkCompletedNormally(h, 2);
2232        assertEquals(++count, e.count.get());
2233
2234        f = new CompletableFuture<>();
2235        g = new CompletableFuture<>();
2236        g.complete(1);
2237        f.complete(3);
2238        h = f.thenCombineAsync(g, subtract, e);
2239        checkCompletedNormally(h, 2);
2240        assertEquals(++count, e.count.get());
2241    }
2242
2243    /**
2244     * thenCombineAsync result completes exceptionally after exceptional
2245     * completion of either source
2246     */
2247    public void testThenCombineAsync2E() {
2248        CompletableFuture<Integer> f, g, h;
2249        ThreadExecutor e = new ThreadExecutor();
2250        int count = 0;
2251
2252        f = new CompletableFuture<>();
2253        g = new CompletableFuture<>();
2254        h = f.thenCombineAsync(g, subtract, e);
2255        f.completeExceptionally(new CFException());
2256        checkIncomplete(h);
2257        g.complete(1);
2258        checkCompletedWithWrappedCFException(h);
2259
2260        f = new CompletableFuture<>();
2261        g = new CompletableFuture<>();
2262        h = f.thenCombineAsync(g, subtract, e);
2263        g.completeExceptionally(new CFException());
2264        checkIncomplete(h);
2265        f.complete(3);
2266        checkCompletedWithWrappedCFException(h);
2267
2268        f = new CompletableFuture<>();
2269        g = new CompletableFuture<>();
2270        g.completeExceptionally(new CFException());
2271        h = f.thenCombineAsync(g, subtract, e);
2272        checkIncomplete(h);
2273        f.complete(3);
2274        checkCompletedWithWrappedCFException(h);
2275
2276        assertEquals(0, e.count.get());
2277    }
2278
2279    /**
2280     * thenCombineAsync result completes exceptionally if action does
2281     */
2282    public void testThenCombineAsync3E() {
2283        CompletableFuture<Integer> f = new CompletableFuture<>();
2284        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2285        FailingBiFunction r = new FailingBiFunction();
2286        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2287        f.complete(one);
2288        checkIncomplete(g);
2289        assertFalse(r.ran);
2290        f2.complete(two);
2291        checkCompletedWithWrappedCFException(g);
2292        assertTrue(r.ran);
2293    }
2294
2295    /**
2296     * thenCombineAsync result completes exceptionally if either source cancelled
2297     */
2298    public void testThenCombineAsync4E() {
2299        CompletableFuture<Integer> f, g, h;
2300        ThreadExecutor e = new ThreadExecutor();
2301
2302        f = new CompletableFuture<>();
2303        g = new CompletableFuture<>();
2304        h = f.thenCombineAsync(g, subtract, e);
2305        assertTrue(f.cancel(true));
2306        checkIncomplete(h);
2307        g.complete(1);
2308        checkCompletedWithWrappedCancellationException(h);
2309
2310        f = new CompletableFuture<>();
2311        g = new CompletableFuture<>();
2312        h = f.thenCombineAsync(g, subtract, e);
2313        assertTrue(g.cancel(true));
2314        checkIncomplete(h);
2315        f.complete(3);
2316        checkCompletedWithWrappedCancellationException(h);
2317
2318        f = new CompletableFuture<>();
2319        g = new CompletableFuture<>();
2320        assertTrue(g.cancel(true));
2321        h = f.thenCombineAsync(g, subtract, e);
2322        checkIncomplete(h);
2323        f.complete(3);
2324        checkCompletedWithWrappedCancellationException(h);
2325
2326        f = new CompletableFuture<>();
2327        g = new CompletableFuture<>();
2328        assertTrue(f.cancel(true));
2329        assertTrue(g.cancel(true));
2330        h = f.thenCombineAsync(g, subtract, e);
2331        checkCompletedWithWrappedCancellationException(h);
2332
2333        assertEquals(0, e.count.get());
2334    }
2335
2336    /**
2337     * thenAcceptBothAsync result completes normally after normal
2338     * completion of sources
2339     */
2340    public void testThenAcceptBothAsyncE() {
2341        CompletableFuture<Integer> f, g;
2342        CompletableFuture<Void> h;
2343        SubtractAction r;
2344        ThreadExecutor e = new ThreadExecutor();
2345
2346        f = new CompletableFuture<>();
2347        g = new CompletableFuture<>();
2348        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2349        f.complete(3);
2350        checkIncomplete(h);
2351        g.complete(1);
2352        checkCompletedNormally(h, null);
2353        assertEquals(r.value, 2);
2354
2355        f = new CompletableFuture<>();
2356        g = new CompletableFuture<>();
2357        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2358        g.complete(1);
2359        checkIncomplete(h);
2360        f.complete(3);
2361        checkCompletedNormally(h, null);
2362        assertEquals(r.value, 2);
2363
2364        f = new CompletableFuture<>();
2365        g = new CompletableFuture<>();
2366        g.complete(1);
2367        f.complete(3);
2368        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2369        checkCompletedNormally(h, null);
2370        assertEquals(r.value, 2);
2371
2372        assertEquals(3, e.count.get());
2373    }
2374
2375    /**
2376     * thenAcceptBothAsync result completes exceptionally after exceptional
2377     * completion of source
2378     */
2379    public void testThenAcceptBothAsync2E() {
2380        CompletableFuture<Integer> f, g;
2381        CompletableFuture<Void> h;
2382        SubtractAction r;
2383        ThreadExecutor e = new ThreadExecutor();
2384
2385        f = new CompletableFuture<>();
2386        g = new CompletableFuture<>();
2387        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2388        f.completeExceptionally(new CFException());
2389        checkIncomplete(h);
2390        g.complete(1);
2391        checkCompletedWithWrappedCFException(h);
2392
2393        f = new CompletableFuture<>();
2394        g = new CompletableFuture<>();
2395        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2396        g.completeExceptionally(new CFException());
2397        checkIncomplete(h);
2398        f.complete(3);
2399        checkCompletedWithWrappedCFException(h);
2400
2401        f = new CompletableFuture<>();
2402        g = new CompletableFuture<>();
2403        f.complete(3);
2404        g.completeExceptionally(new CFException());
2405        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2406        checkCompletedWithWrappedCFException(h);
2407
2408        f = new CompletableFuture<>();
2409        g = new CompletableFuture<>();
2410        f.completeExceptionally(new CFException());
2411        g.complete(3);
2412        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2413        checkCompletedWithWrappedCFException(h);
2414
2415        assertEquals(0, e.count.get());
2416    }
2417
2418    /**
2419     * thenAcceptBothAsync result completes exceptionally if action does
2420     */
2421    public void testThenAcceptBothAsync3E() {
2422        CompletableFuture<Integer> f, g;
2423        CompletableFuture<Void> h;
2424        FailingBiConsumer r;
2425        ThreadExecutor e = new ThreadExecutor();
2426
2427        f = new CompletableFuture<>();
2428        g = new CompletableFuture<>();
2429        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2430        f.complete(3);
2431        checkIncomplete(h);
2432        g.complete(1);
2433        checkCompletedWithWrappedCFException(h);
2434
2435        f = new CompletableFuture<>();
2436        g = new CompletableFuture<>();
2437        f.complete(3);
2438        g.complete(1);
2439        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2440        checkCompletedWithWrappedCFException(h);
2441
2442        assertEquals(2, e.count.get());
2443    }
2444
2445    /**
2446     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2447     */
2448    public void testThenAcceptBothAsync4E() {
2449        CompletableFuture<Integer> f, g;
2450        CompletableFuture<Void> h;
2451        SubtractAction r;
2452        ThreadExecutor e = new ThreadExecutor();
2453
2454        f = new CompletableFuture<>();
2455        g = new CompletableFuture<>();
2456        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2457        assertTrue(f.cancel(true));
2458        checkIncomplete(h);
2459        g.complete(1);
2460        checkCompletedWithWrappedCancellationException(h);
2461
2462        f = new CompletableFuture<>();
2463        g = new CompletableFuture<>();
2464        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2465        assertTrue(g.cancel(true));
2466        checkIncomplete(h);
2467        f.complete(3);
2468        checkCompletedWithWrappedCancellationException(h);
2469
2470        f = new CompletableFuture<>();
2471        g = new CompletableFuture<>();
2472        f.complete(3);
2473        assertTrue(g.cancel(true));
2474        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2475        checkCompletedWithWrappedCancellationException(h);
2476
2477        f = new CompletableFuture<>();
2478        g = new CompletableFuture<>();
2479        assertTrue(f.cancel(true));
2480        g.complete(3);
2481        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2482        checkCompletedWithWrappedCancellationException(h);
2483
2484        assertEquals(0, e.count.get());
2485    }
2486
2487    /**
2488     * runAfterBothAsync result completes normally after normal
2489     * completion of sources
2490     */
2491    public void testRunAfterBothAsyncE() {
2492        CompletableFuture<Integer> f = new CompletableFuture<>();
2493        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2494        Noop r = new Noop();
2495        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2496        f.complete(one);
2497        checkIncomplete(g);
2498        f2.complete(two);
2499        checkCompletedNormally(g, null);
2500        assertTrue(r.ran);
2501    }
2502
2503    /**
2504     * runAfterBothAsync result completes exceptionally after exceptional
2505     * completion of source
2506     */
2507    public void testRunAfterBothAsync2E() {
2508        CompletableFuture<Integer> f = new CompletableFuture<>();
2509        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2510        Noop r = new Noop();
2511        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2512        f.completeExceptionally(new CFException());
2513        f2.complete(two);
2514        checkCompletedWithWrappedCFException(g);
2515
2516        r = new Noop();
2517        f = new CompletableFuture<>();
2518        f2 = new CompletableFuture<>();
2519        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2520        f.complete(one);
2521        f2.completeExceptionally(new CFException());
2522        checkCompletedWithWrappedCFException(g);
2523    }
2524
2525    /**
2526     * runAfterBothAsync result completes exceptionally if action does
2527     */
2528    public void testRunAfterBothAsync3E() {
2529        CompletableFuture<Integer> f = new CompletableFuture<>();
2530        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2531        FailingNoop r = new FailingNoop();
2532        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2533        f.complete(one);
2534        checkIncomplete(g);
2535        f2.complete(two);
2536        checkCompletedWithWrappedCFException(g);
2537    }
2538
2539    /**
2540     * runAfterBothAsync result completes exceptionally if either source cancelled
2541     */
2542    public void testRunAfterBothAsync4E() {
2543        CompletableFuture<Integer> f = new CompletableFuture<>();
2544        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2545        Noop r = new Noop();
2546        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2547        assertTrue(f.cancel(true));
2548        f2.complete(two);
2549        checkCompletedWithWrappedCancellationException(g);
2550
2551        r = new Noop();
2552        f = new CompletableFuture<>();
2553        f2 = new CompletableFuture<>();
2554        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2555        f.complete(one);
2556        assertTrue(f2.cancel(true));
2557        checkCompletedWithWrappedCancellationException(g);
2558    }
2559
2560    /**
2561     * applyToEitherAsync result completes normally after normal
2562     * completion of sources
2563     */
2564    public void testApplyToEitherAsyncE() {
2565        CompletableFuture<Integer> f = new CompletableFuture<>();
2566        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2567        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2568        f.complete(one);
2569        checkCompletedNormally(g, two);
2570
2571        f = new CompletableFuture<>();
2572        f.complete(one);
2573        f2 = new CompletableFuture<>();
2574        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2575        checkCompletedNormally(g, two);
2576    }
2577
2578    /**
2579     * applyToEitherAsync result completes exceptionally after exceptional
2580     * completion of source
2581     */
2582    public void testApplyToEitherAsync2E() {
2583        CompletableFuture<Integer> f = new CompletableFuture<>();
2584        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2585        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2586        f.completeExceptionally(new CFException());
2587        checkCompletedWithWrappedCFException(g);
2588
2589        f = new CompletableFuture<>();
2590        f2 = new CompletableFuture<>();
2591        f2.completeExceptionally(new CFException());
2592        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2593        f.complete(one);
2594        checkCompletedWithWrappedCFException(g);
2595    }
2596
2597    /**
2598     * applyToEitherAsync result completes exceptionally if action does
2599     */
2600    public void testApplyToEitherAsync3E() {
2601        CompletableFuture<Integer> f = new CompletableFuture<>();
2602        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2603        FailingFunction r = new FailingFunction();
2604        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2605        f.complete(one);
2606        checkCompletedWithWrappedCFException(g);
2607    }
2608
2609    /**
2610     * applyToEitherAsync result completes exceptionally if either source cancelled
2611     */
2612    public void testApplyToEitherAsync4E() {
2613        CompletableFuture<Integer> f = new CompletableFuture<>();
2614        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2615        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2616        assertTrue(f.cancel(true));
2617        checkCompletedWithWrappedCancellationException(g);
2618
2619        f = new CompletableFuture<>();
2620        f2 = new CompletableFuture<>();
2621        assertTrue(f2.cancel(true));
2622        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2623        checkCompletedWithWrappedCancellationException(g);
2624    }
2625
2626    /**
2627     * acceptEitherAsync result completes normally after normal
2628     * completion of sources
2629     */
2630    public void testAcceptEitherAsyncE() {
2631        CompletableFuture<Integer> f = new CompletableFuture<>();
2632        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2633        IncAction r = new IncAction();
2634        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2635        f.complete(one);
2636        checkCompletedNormally(g, null);
2637        assertEquals(r.value, 2);
2638
2639        r = new IncAction();
2640        f = new CompletableFuture<>();
2641        f.complete(one);
2642        f2 = new CompletableFuture<>();
2643        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2644        checkCompletedNormally(g, null);
2645        assertEquals(r.value, 2);
2646    }
2647
2648    /**
2649     * acceptEitherAsync result completes exceptionally after exceptional
2650     * completion of source
2651     */
2652    public void testAcceptEitherAsync2E() {
2653        CompletableFuture<Integer> f = new CompletableFuture<>();
2654        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2655        IncAction r = new IncAction();
2656        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2657        f.completeExceptionally(new CFException());
2658        checkCompletedWithWrappedCFException(g);
2659
2660        r = new IncAction();
2661        f = new CompletableFuture<>();
2662        f2 = new CompletableFuture<>();
2663        f2.completeExceptionally(new CFException());
2664        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2665        f.complete(one);
2666        checkCompletedWithWrappedCFException(g);
2667    }
2668
2669    /**
2670     * acceptEitherAsync result completes exceptionally if action does
2671     */
2672    public void testAcceptEitherAsync3E() {
2673        CompletableFuture<Integer> f = new CompletableFuture<>();
2674        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2675        FailingConsumer r = new FailingConsumer();
2676        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2677        f.complete(one);
2678        checkCompletedWithWrappedCFException(g);
2679    }
2680
2681    /**
2682     * acceptEitherAsync result completes exceptionally if either
2683     * source cancelled
2684     */
2685    public void testAcceptEitherAsync4E() {
2686        CompletableFuture<Integer> f = new CompletableFuture<>();
2687        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2688        IncAction r = new IncAction();
2689        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2690        assertTrue(f.cancel(true));
2691        checkCompletedWithWrappedCancellationException(g);
2692
2693        r = new IncAction();
2694        f = new CompletableFuture<>();
2695        f2 = new CompletableFuture<>();
2696        assertTrue(f2.cancel(true));
2697        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2698        checkCompletedWithWrappedCancellationException(g);
2699    }
2700
2701    /**
2702     * runAfterEitherAsync result completes normally after normal
2703     * completion of sources
2704     */
2705    public void testRunAfterEitherAsyncE() {
2706        CompletableFuture<Integer> f = new CompletableFuture<>();
2707        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2708        Noop r = new Noop();
2709        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2710        f.complete(one);
2711        checkCompletedNormally(g, null);
2712        assertTrue(r.ran);
2713
2714        r = new Noop();
2715        f = new CompletableFuture<>();
2716        f.complete(one);
2717        f2 = new CompletableFuture<>();
2718        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2719        checkCompletedNormally(g, null);
2720        assertTrue(r.ran);
2721    }
2722
2723    /**
2724     * runAfterEitherAsync result completes exceptionally after exceptional
2725     * completion of source
2726     */
2727    public void testRunAfterEitherAsync2E() {
2728        CompletableFuture<Integer> f = new CompletableFuture<>();
2729        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2730        Noop r = new Noop();
2731        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2732        f.completeExceptionally(new CFException());
2733        checkCompletedWithWrappedCFException(g);
2734
2735        r = new Noop();
2736        f = new CompletableFuture<>();
2737        f2 = new CompletableFuture<>();
2738        f2.completeExceptionally(new CFException());
2739        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2740        f.complete(one);
2741        checkCompletedWithWrappedCFException(g);
2742    }
2743
2744    /**
2745     * runAfterEitherAsync result completes exceptionally if action does
2746     */
2747    public void testRunAfterEitherAsync3E() {
2748        CompletableFuture<Integer> f = new CompletableFuture<>();
2749        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2750        FailingNoop r = new FailingNoop();
2751        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2752        f.complete(one);
2753        checkCompletedWithWrappedCFException(g);
2754    }
2755
2756    /**
2757     * runAfterEitherAsync result completes exceptionally if either
2758     * source cancelled
2759     */
2760    public void testRunAfterEitherAsync4E() {
2761        CompletableFuture<Integer> f = new CompletableFuture<>();
2762        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2763        Noop r = new Noop();
2764        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2765        assertTrue(f.cancel(true));
2766        checkCompletedWithWrappedCancellationException(g);
2767
2768        r = new Noop();
2769        f = new CompletableFuture<>();
2770        f2 = new CompletableFuture<>();
2771        assertTrue(f2.cancel(true));
2772        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2773        checkCompletedWithWrappedCancellationException(g);
2774    }
2775
2776    /**
2777     * thenComposeAsync result completes normally after normal
2778     * completion of source
2779     */
2780    public void testThenComposeAsyncE() {
2781        CompletableFuture<Integer> f = new CompletableFuture<>();
2782        CompletableFutureInc r = new CompletableFutureInc();
2783        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2784        f.complete(one);
2785        checkCompletedNormally(g, two);
2786    }
2787
2788    /**
2789     * thenComposeAsync result completes exceptionally after
2790     * exceptional completion of source
2791     */
2792    public void testThenComposeAsync2E() {
2793        CompletableFuture<Integer> f = new CompletableFuture<>();
2794        CompletableFutureInc r = new CompletableFutureInc();
2795        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2796        f.completeExceptionally(new CFException());
2797        checkCompletedWithWrappedCFException(g);
2798    }
2799
2800    /**
2801     * thenComposeAsync result completes exceptionally if action does
2802     */
2803    public void testThenComposeAsync3E() {
2804        CompletableFuture<Integer> f = new CompletableFuture<>();
2805        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2806        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2807        f.complete(one);
2808        checkCompletedWithWrappedCFException(g);
2809    }
2810
2811    /**
2812     * thenComposeAsync result completes exceptionally if source cancelled
2813     */
2814    public void testThenComposeAsync4E() {
2815        CompletableFuture<Integer> f = new CompletableFuture<>();
2816        CompletableFutureInc r = new CompletableFutureInc();
2817        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2818        assertTrue(f.cancel(true));
2819        checkCompletedWithWrappedCancellationException(g);
2820    }
2821
3247      // other static methods
3248  
3249      /**
# Line 2826 | Line 3251 | public class CompletableFutureTest exten
3251       * with the value null
3252       */
3253      public void testAllOf_empty() throws Exception {
3254 <        CompletableFuture<?> f = CompletableFuture.allOf();
3254 >        CompletableFuture<Void> f = CompletableFuture.allOf();
3255          checkCompletedNormally(f, null);
3256      }
3257  
3258      /**
3259 <     * allOf returns a future completed when all components complete
3259 >     * allOf returns a future completed normally with the value null
3260 >     * when all components complete normally
3261       */
3262 <    public void testAllOf() throws Exception {
3262 >    public void testAllOf_normal() throws Exception {
3263          for (int k = 1; k < 20; ++k) {
3264              CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3265              for (int i = 0; i < k; ++i)
# Line 2841 | Line 3267 | public class CompletableFutureTest exten
3267              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3268              for (int i = 0; i < k; ++i) {
3269                  checkIncomplete(f);
3270 +                checkIncomplete(CompletableFuture.allOf(fs));
3271                  fs[i].complete(one);
3272              }
3273              checkCompletedNormally(f, null);
3274 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3275          }
3276      }
3277  
# Line 2851 | Line 3279 | public class CompletableFutureTest exten
3279       * anyOf(no component futures) returns an incomplete future
3280       */
3281      public void testAnyOf_empty() throws Exception {
3282 <        CompletableFuture<?> f = CompletableFuture.anyOf();
3282 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3283          checkIncomplete(f);
3284      }
3285  
3286      /**
3287 <     * anyOf returns a future completed when any components complete
3287 >     * anyOf returns a future completed normally with a value when
3288 >     * a component future does
3289       */
3290 <    public void testAnyOf() throws Exception {
3291 <        for (int k = 1; k < 20; ++k) {
3290 >    public void testAnyOf_normal() throws Exception {
3291 >        for (int k = 0; k < 10; ++k) {
3292              CompletableFuture[] fs = new CompletableFuture[k];
3293              for (int i = 0; i < k; ++i)
3294                  fs[i] = new CompletableFuture<>();
# Line 2868 | Line 3297 | public class CompletableFutureTest exten
3297              for (int i = 0; i < k; ++i) {
3298                  fs[i].complete(one);
3299                  checkCompletedNormally(f, one);
3300 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3301 +            }
3302 +        }
3303 +    }
3304 +
3305 +    /**
3306 +     * anyOf result completes exceptionally when any component does.
3307 +     */
3308 +    public void testAnyOf_exceptional() throws Exception {
3309 +        for (int k = 0; k < 10; ++k) {
3310 +            CompletableFuture[] fs = new CompletableFuture[k];
3311 +            for (int i = 0; i < k; ++i)
3312 +                fs[i] = new CompletableFuture<>();
3313 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3314 +            checkIncomplete(f);
3315 +            for (int i = 0; i < k; ++i) {
3316 +                fs[i].completeExceptionally(new CFException());
3317 +                checkCompletedWithWrappedCFException(f);
3318 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3319              }
3320          }
3321      }
# Line 2883 | Line 3331 | public class CompletableFutureTest exten
3331          ThreadExecutor exec = new ThreadExecutor();
3332  
3333          Runnable[] throwingActions = {
3334 <            () -> { CompletableFuture.supplyAsync(null); },
3335 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3336 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3337 <
3338 <            () -> { CompletableFuture.runAsync(null); },
3339 <            () -> { CompletableFuture.runAsync(null, exec); },
3340 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3341 <
3342 <            () -> { f.completeExceptionally(null); },
3343 <
3344 <            () -> { f.thenApply(null); },
3345 <            () -> { f.thenApplyAsync(null); },
3346 <            () -> { f.thenApplyAsync((x) -> x, null); },
3347 <            () -> { f.thenApplyAsync(null, exec); },
3348 <
3349 <            () -> { f.thenAccept(null); },
3350 <            () -> { f.thenAcceptAsync(null); },
3351 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3352 <            () -> { f.thenAcceptAsync(null, exec); },
3353 <
3354 <            () -> { f.thenRun(null); },
3355 <            () -> { f.thenRunAsync(null); },
3356 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3357 <            () -> { f.thenRunAsync(null, exec); },
3358 <
3359 <            () -> { f.thenCombine(g, null); },
3360 <            () -> { f.thenCombineAsync(g, null); },
3361 <            () -> { f.thenCombineAsync(g, null, exec); },
3362 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3363 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3364 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3365 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3366 <
3367 <            () -> { f.thenAcceptBoth(g, null); },
3368 <            () -> { f.thenAcceptBothAsync(g, null); },
3369 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3370 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3371 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3372 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3373 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3374 <
3375 <            () -> { f.runAfterBoth(g, null); },
3376 <            () -> { f.runAfterBothAsync(g, null); },
3377 <            () -> { f.runAfterBothAsync(g, null, exec); },
3378 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3379 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3380 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3381 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3382 <
3383 <            () -> { f.applyToEither(g, null); },
3384 <            () -> { f.applyToEitherAsync(g, null); },
3385 <            () -> { f.applyToEitherAsync(g, null, exec); },
3386 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3387 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3388 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3389 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3390 <
3391 <            () -> { f.acceptEither(g, null); },
3392 <            () -> { f.acceptEitherAsync(g, null); },
3393 <            () -> { f.acceptEitherAsync(g, null, exec); },
3394 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3395 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3396 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3397 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3398 <
3399 <            () -> { f.runAfterEither(g, null); },
3400 <            () -> { f.runAfterEitherAsync(g, null); },
3401 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3402 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3403 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3404 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3405 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3406 <
3407 <            () -> { f.thenCompose(null); },
3408 <            () -> { f.thenComposeAsync(null); },
3409 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3410 <            () -> { f.thenComposeAsync(null, exec); },
3411 <
3412 <            () -> { f.exceptionally(null); },
3413 <
3414 <            () -> { f.handle(null); },
3415 <
3416 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3417 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3418 <            () -> { CompletableFuture.allOf(f, null); },
3419 <            () -> { CompletableFuture.allOf(null, f); },
3420 <
3421 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3422 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3423 <            () -> { CompletableFuture.anyOf(f, null); },
3424 <            () -> { CompletableFuture.anyOf(null, f); },
3334 >            () -> CompletableFuture.supplyAsync(null),
3335 >            () -> CompletableFuture.supplyAsync(null, exec),
3336 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
3337 >
3338 >            () -> CompletableFuture.runAsync(null),
3339 >            () -> CompletableFuture.runAsync(null, exec),
3340 >            () -> CompletableFuture.runAsync(() -> {}, null),
3341 >
3342 >            () -> f.completeExceptionally(null),
3343 >
3344 >            () -> f.thenApply(null),
3345 >            () -> f.thenApplyAsync(null),
3346 >            () -> f.thenApplyAsync((x) -> x, null),
3347 >            () -> f.thenApplyAsync(null, exec),
3348 >
3349 >            () -> f.thenAccept(null),
3350 >            () -> f.thenAcceptAsync(null),
3351 >            () -> f.thenAcceptAsync((x) -> {} , null),
3352 >            () -> f.thenAcceptAsync(null, exec),
3353 >
3354 >            () -> f.thenRun(null),
3355 >            () -> f.thenRunAsync(null),
3356 >            () -> f.thenRunAsync(() -> {} , null),
3357 >            () -> f.thenRunAsync(null, exec),
3358 >
3359 >            () -> f.thenCombine(g, null),
3360 >            () -> f.thenCombineAsync(g, null),
3361 >            () -> f.thenCombineAsync(g, null, exec),
3362 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3363 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3364 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3365 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3366 >
3367 >            () -> f.thenAcceptBoth(g, null),
3368 >            () -> f.thenAcceptBothAsync(g, null),
3369 >            () -> f.thenAcceptBothAsync(g, null, exec),
3370 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3371 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3372 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3373 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3374 >
3375 >            () -> f.runAfterBoth(g, null),
3376 >            () -> f.runAfterBothAsync(g, null),
3377 >            () -> f.runAfterBothAsync(g, null, exec),
3378 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3379 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3380 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3381 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3382 >
3383 >            () -> f.applyToEither(g, null),
3384 >            () -> f.applyToEitherAsync(g, null),
3385 >            () -> f.applyToEitherAsync(g, null, exec),
3386 >            () -> f.applyToEither(nullFuture, (x) -> x),
3387 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3388 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3389 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3390 >
3391 >            () -> f.acceptEither(g, null),
3392 >            () -> f.acceptEitherAsync(g, null),
3393 >            () -> f.acceptEitherAsync(g, null, exec),
3394 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3395 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3396 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3397 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3398 >
3399 >            () -> f.runAfterEither(g, null),
3400 >            () -> f.runAfterEitherAsync(g, null),
3401 >            () -> f.runAfterEitherAsync(g, null, exec),
3402 >            () -> f.runAfterEither(nullFuture, () -> {}),
3403 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3404 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3405 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3406 >
3407 >            () -> f.thenCompose(null),
3408 >            () -> f.thenComposeAsync(null),
3409 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3410 >            () -> f.thenComposeAsync(null, exec),
3411 >
3412 >            () -> f.exceptionally(null),
3413 >
3414 >            () -> f.handle(null),
3415 >
3416 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3417 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3418 >            () -> CompletableFuture.allOf(f, null),
3419 >            () -> CompletableFuture.allOf(null, f),
3420 >
3421 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3422 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3423 >            () -> CompletableFuture.anyOf(f, null),
3424 >            () -> CompletableFuture.anyOf(null, f),
3425 >
3426 >            () -> f.obtrudeException(null),
3427          };
3428  
3429          assertThrows(NullPointerException.class, throwingActions);
3430          assertEquals(0, exec.count.get());
3431      }
3432  
3433 +    /**
3434 +     * toCompletableFuture returns this CompletableFuture.
3435 +     */
3436 +    public void testToCompletableFuture() {
3437 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3438 +        assertSame(f, f.toCompletableFuture());
3439 +    }
3440 +
3441 +    /**
3442 +     * whenComplete action executes on normal completion, propagating
3443 +     * source result.
3444 +     */
3445 +    public void testWhenComplete_normalCompletion1() {
3446 +        for (ExecutionMode m : ExecutionMode.values())
3447 +        for (boolean createIncomplete : new boolean[] { true, false })
3448 +        for (Integer v1 : new Integer[] { 1, null })
3449 +    {
3450 +        final AtomicInteger a = new AtomicInteger(0);
3451 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3452 +        if (!createIncomplete) f.complete(v1);
3453 +        final CompletableFuture<Integer> g = m.whenComplete
3454 +            (f,
3455 +             (Integer x, Throwable t) -> {
3456 +                threadAssertSame(x, v1);
3457 +                threadAssertNull(t);
3458 +                a.getAndIncrement();
3459 +            });
3460 +        if (createIncomplete) f.complete(v1);
3461 +
3462 +        checkCompletedNormally(g, v1);
3463 +        checkCompletedNormally(f, v1);
3464 +        assertEquals(1, a.get());
3465 +    }}
3466 +
3467 +    /**
3468 +     * whenComplete action executes on exceptional completion, propagating
3469 +     * source result.
3470 +     */
3471 +    public void testWhenComplete_exceptionalCompletion() {
3472 +        for (ExecutionMode m : ExecutionMode.values())
3473 +        for (boolean createIncomplete : new boolean[] { true, false })
3474 +        for (Integer v1 : new Integer[] { 1, null })
3475 +    {
3476 +        final AtomicInteger a = new AtomicInteger(0);
3477 +        final CFException ex = new CFException();
3478 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3479 +        if (!createIncomplete) f.completeExceptionally(ex);
3480 +        final CompletableFuture<Integer> g = m.whenComplete
3481 +            (f,
3482 +             (Integer x, Throwable t) -> {
3483 +                threadAssertNull(x);
3484 +                threadAssertSame(t, ex);
3485 +                a.getAndIncrement();
3486 +            });
3487 +        if (createIncomplete) f.completeExceptionally(ex);
3488 +        checkCompletedWithWrappedCFException(f, ex);
3489 +        checkCompletedWithWrappedCFException(g, ex);
3490 +        assertEquals(1, a.get());
3491 +    }}
3492 +
3493 +    /**
3494 +     * whenComplete action executes on cancelled source, propagating
3495 +     * CancellationException.
3496 +     */
3497 +    public void testWhenComplete_sourceCancelled() {
3498 +        for (ExecutionMode m : ExecutionMode.values())
3499 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3500 +        for (boolean createIncomplete : new boolean[] { true, false })
3501 +    {
3502 +        final AtomicInteger a = new AtomicInteger(0);
3503 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3504 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3505 +        final CompletableFuture<Integer> g = m.whenComplete
3506 +            (f,
3507 +             (Integer x, Throwable t) -> {
3508 +                threadAssertNull(x);
3509 +                threadAssertTrue(t instanceof CancellationException);
3510 +                a.getAndIncrement();
3511 +            });
3512 +        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3513 +
3514 +        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3515 +        checkCompletedWithWrappedCancellationException(g);
3516 +        checkCancelled(f);
3517 +        assertEquals(1, a.get());
3518 +    }}
3519 +
3520 +    /**
3521 +     * If a whenComplete action throws an exception when triggered by
3522 +     * a normal completion, it completes exceptionally
3523 +     */
3524 +    public void testWhenComplete_actionFailed() {
3525 +        for (boolean createIncomplete : new boolean[] { true, false })
3526 +        for (ExecutionMode m : ExecutionMode.values())
3527 +        for (Integer v1 : new Integer[] { 1, null })
3528 +    {
3529 +        final AtomicInteger a = new AtomicInteger(0);
3530 +        final CFException ex = new CFException();
3531 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3532 +        if (!createIncomplete) f.complete(v1);
3533 +        final CompletableFuture<Integer> g = m.whenComplete
3534 +            (f,
3535 +             (Integer x, Throwable t) -> {
3536 +                threadAssertSame(x, v1);
3537 +                threadAssertNull(t);
3538 +                a.getAndIncrement();
3539 +                throw ex;
3540 +            });
3541 +        if (createIncomplete) f.complete(v1);
3542 +        checkCompletedNormally(f, v1);
3543 +        checkCompletedWithWrappedCFException(g, ex);
3544 +        assertEquals(1, a.get());
3545 +    }}
3546 +
3547 +    /**
3548 +     * If a whenComplete action throws an exception when triggered by
3549 +     * a source completion that also throws an exception, the source
3550 +     * exception takes precedence.
3551 +     */
3552 +    public void testWhenComplete_actionFailedSourceFailed() {
3553 +        for (boolean createIncomplete : new boolean[] { true, false })
3554 +        for (ExecutionMode m : ExecutionMode.values())
3555 +        for (Integer v1 : new Integer[] { 1, null })
3556 +    {
3557 +        final AtomicInteger a = new AtomicInteger(0);
3558 +        final CFException ex1 = new CFException();
3559 +        final CFException ex2 = new CFException();
3560 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3561 +
3562 +        if (!createIncomplete) f.completeExceptionally(ex1);
3563 +        final CompletableFuture<Integer> g = m.whenComplete
3564 +            (f,
3565 +             (Integer x, Throwable t) -> {
3566 +                threadAssertSame(t, ex1);
3567 +                threadAssertNull(x);
3568 +                a.getAndIncrement();
3569 +                throw ex2;
3570 +            });
3571 +        if (createIncomplete) f.completeExceptionally(ex1);
3572 +
3573 +        checkCompletedWithWrappedCFException(f, ex1);
3574 +        checkCompletedWithWrappedCFException(g, ex1);
3575 +        assertEquals(1, a.get());
3576 +    }}
3577 +
3578   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines