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.19 by jsr166, Sun Apr 7 15:04:14 2013 UTC vs.
Revision 1.36 by jsr166, Sun Jun 1 23:12:45 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 55 | Line 56 | public class CompletableFutureTest exten
56  
57      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
58          try {
59 +            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
60 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
61 +        try {
62              assertEquals(value, f.join());
63          } catch (Throwable fail) { threadUnexpectedException(fail); }
64          try {
# Line 63 | Line 67 | public class CompletableFutureTest exten
67          try {
68              assertEquals(value, f.get());
69          } catch (Throwable fail) { threadUnexpectedException(fail); }
66        try {
67            assertEquals(value, f.get(0L, SECONDS));
68        } 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  
76      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
77          try {
78 +            f.get(LONG_DELAY_MS, MILLISECONDS);
79 +            shouldThrow();
80 +        } catch (ExecutionException success) {
81 +            assertTrue(success.getCause() instanceof CFException);
82 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 +        try {
84              f.join();
85              shouldThrow();
86          } catch (CompletionException success) {
# Line 90 | Line 98 | public class CompletableFutureTest exten
98          } catch (ExecutionException success) {
99              assertTrue(success.getCause() instanceof CFException);
100          } catch (Throwable fail) { threadUnexpectedException(fail); }
101 +        assertTrue(f.isDone());
102 +        assertFalse(f.isCancelled());
103 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
104 +    }
105 +
106 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
107 +                                              CFException ex) {
108          try {
109 <            f.get(0L, SECONDS);
109 >            f.get(LONG_DELAY_MS, MILLISECONDS);
110              shouldThrow();
111          } catch (ExecutionException success) {
112 <            assertTrue(success.getCause() instanceof CFException);
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());
# Line 103 | Line 136 | public class CompletableFutureTest exten
136  
137      void checkCancelled(CompletableFuture<?> f) {
138          try {
139 +            f.get(LONG_DELAY_MS, MILLISECONDS);
140 +            shouldThrow();
141 +        } catch (CancellationException success) {
142 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
143 +        try {
144              f.join();
145              shouldThrow();
146          } catch (CancellationException success) {}
# Line 115 | Line 153 | public class CompletableFutureTest exten
153              shouldThrow();
154          } catch (CancellationException success) {
155          } catch (Throwable fail) { threadUnexpectedException(fail); }
118        try {
119            f.get(0L, SECONDS);
120            shouldThrow();
121        } catch (CancellationException success) {
122        } 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      }
161  
162      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
163          try {
164 +            f.get(LONG_DELAY_MS, MILLISECONDS);
165 +            shouldThrow();
166 +        } catch (ExecutionException success) {
167 +            assertTrue(success.getCause() instanceof CancellationException);
168 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
169 +        try {
170              f.join();
171              shouldThrow();
172          } catch (CompletionException success) {
# Line 144 | Line 184 | public class CompletableFutureTest exten
184          } catch (ExecutionException success) {
185              assertTrue(success.getCause() instanceof CancellationException);
186          } catch (Throwable fail) { threadUnexpectedException(fail); }
147        try {
148            f.get(0L, SECONDS);
149            shouldThrow();
150        } catch (ExecutionException success) {
151            assertTrue(success.getCause() instanceof CancellationException);
152        } catch (Throwable fail) { threadUnexpectedException(fail); }
187          assertTrue(f.isDone());
188          assertFalse(f.isCancelled());
189 +        assertTrue(f.isCompletedExceptionally());
190          assertTrue(f.toString().contains("[Completed exceptionally]"));
191      }
192  
# Line 160 | Line 195 | public class CompletableFutureTest exten
195       * by methods isDone, isCancelled, and getNow
196       */
197      public void testConstructor() {
198 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
198 >        CompletableFuture<Integer> f = new CompletableFuture<>();
199          checkIncomplete(f);
200      }
201  
# Line 169 | Line 204 | public class CompletableFutureTest exten
204       * isCancelled, join, get, and getNow
205       */
206      public void testComplete() {
207 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
207 >        CompletableFuture<Integer> f = new CompletableFuture<>();
208          checkIncomplete(f);
209          f.complete(one);
210          checkCompletedNormally(f, one);
# Line 180 | Line 215 | public class CompletableFutureTest exten
215       * methods isDone, isCancelled, join, get, and getNow
216       */
217      public void testCompleteExceptionally() {
218 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
218 >        CompletableFuture<Integer> f = new CompletableFuture<>();
219          checkIncomplete(f);
220          f.completeExceptionally(new CFException());
221          checkCompletedWithWrappedCFException(f);
# Line 191 | Line 226 | public class CompletableFutureTest exten
226       * methods isDone, isCancelled, join, get, and getNow
227       */
228      public void testCancel() {
229 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
229 >        CompletableFuture<Integer> f = new CompletableFuture<>();
230          checkIncomplete(f);
231          assertTrue(f.cancel(true));
232          checkCancelled(f);
# Line 201 | Line 236 | public class CompletableFutureTest exten
236       * obtrudeValue forces completion with given value
237       */
238      public void testObtrudeValue() {
239 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
239 >        CompletableFuture<Integer> f = new CompletableFuture<>();
240          checkIncomplete(f);
241          f.complete(one);
242          checkCompletedNormally(f, one);
# Line 209 | Line 244 | public class CompletableFutureTest exten
244          checkCompletedNormally(f, three);
245          f.obtrudeValue(two);
246          checkCompletedNormally(f, two);
247 <        f = new CompletableFuture<Integer>();
247 >        f = new CompletableFuture<>();
248          f.obtrudeValue(three);
249          checkCompletedNormally(f, three);
250 <        f = new CompletableFuture<Integer>();
250 >        f = new CompletableFuture<>();
251          f.completeExceptionally(new CFException());
252          f.obtrudeValue(four);
253          checkCompletedNormally(f, four);
# Line 222 | Line 257 | public class CompletableFutureTest exten
257       * obtrudeException forces completion with given exception
258       */
259      public void testObtrudeException() {
260 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
260 >        CompletableFuture<Integer> f = new CompletableFuture<>();
261          checkIncomplete(f);
262          f.complete(one);
263          checkCompletedNormally(f, one);
264          f.obtrudeException(new CFException());
265          checkCompletedWithWrappedCFException(f);
266 <        f = new CompletableFuture<Integer>();
266 >        f = new CompletableFuture<>();
267          f.obtrudeException(new CFException());
268          checkCompletedWithWrappedCFException(f);
269 <        f = new CompletableFuture<Integer>();
269 >        f = new CompletableFuture<>();
270          f.completeExceptionally(new CFException());
271          f.obtrudeValue(four);
272          checkCompletedNormally(f, four);
# Line 243 | Line 278 | public class CompletableFutureTest exten
278       * getNumberOfDependents returns number of dependent tasks
279       */
280      public void testGetNumberOfDependents() {
281 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
281 >        CompletableFuture<Integer> f = new CompletableFuture<>();
282          assertEquals(f.getNumberOfDependents(), 0);
283          CompletableFuture g = f.thenRun(new Noop());
284          assertEquals(f.getNumberOfDependents(), 1);
# Line 256 | Line 291 | public class CompletableFutureTest exten
291          assertEquals(g.getNumberOfDependents(), 0);
292      }
293  
259
294      /**
295       * toString indicates current completion state
296       */
# Line 284 | Line 318 | public class CompletableFutureTest exten
318  
319      // Choose non-commutative actions for better coverage
320  
321 +    // A non-commutative function that handles null values as well
322 +    public static int subtract(Integer x, Integer y) {
323 +        return ((x == null) ? 42 : x.intValue())
324 +            - ((y == null) ? 99 : y.intValue());
325 +    }
326 +
327      static final Supplier<Integer> supplyOne =
328          () -> Integer.valueOf(1);
329      static final Function<Integer, Integer> inc =
330          (Integer x) -> Integer.valueOf(x.intValue() + 1);
331      static final BiFunction<Integer, Integer, Integer> subtract =
332 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
332 >        (Integer x, Integer y) -> subtract(x, y);
333      static final class IncAction implements Consumer<Integer> {
334          int value;
335          public void accept(Integer x) { value = x.intValue() + 1; }
336      }
337 <    static final class AddAction implements BiConsumer<Integer, Integer> {
337 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
338 >        volatile int invocationCount = 0;
339          int value;
340 +        // Check this action was invoked exactly once when result is computed.
341 +        public boolean ran() { return invocationCount == 1; }
342          public void accept(Integer x, Integer y) {
343 <            value = x.intValue() + y.intValue();
343 >            invocationCount++;
344 >            value = subtract(x, y);
345 >        }
346 >    }
347 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
348 >        volatile int invocationCount = 0;
349 >        int value;
350 >        // Check this action was invoked exactly once when result is computed.
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x, Integer y) {
353 >            invocationCount++;
354 >            return subtract(x, y);
355          }
356      }
357      static final class Noop implements Runnable {
# Line 332 | Line 386 | public class CompletableFutureTest exten
386  
387      static final class CompletableFutureInc
388          implements Function<Integer, CompletableFuture<Integer>> {
389 +        boolean ran;
390          public CompletableFuture<Integer> apply(Integer x) {
391 <            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
391 >            ran = true;
392 >            CompletableFuture<Integer> f = new CompletableFuture<>();
393              f.complete(Integer.valueOf(x.intValue() + 1));
394              return f;
395          }
# Line 369 | Line 425 | public class CompletableFutureTest exten
425          }
426      }
427  
428 +    /**
429 +     * Permits the testing of parallel code for the 3 different
430 +     * execution modes without repeating all the testing code.
431 +     */
432 +    enum ExecutionMode {
433 +        DEFAULT {
434 +            public <T,U> CompletableFuture<Void> runAfterBoth
435 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
436 +                return f.runAfterBoth(g, a);
437 +            }
438 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
439 +                (CompletableFuture<T> f,
440 +                 CompletionStage<? extends U> g,
441 +                 BiConsumer<? super T,? super U> a) {
442 +                return f.thenAcceptBoth(g, a);
443 +            }
444 +            public <T,U,V> CompletableFuture<V> thenCombine
445 +                (CompletableFuture<T> f,
446 +                 CompletionStage<? extends U> g,
447 +                 BiFunction<? super T,? super U,? extends V> a) {
448 +                return f.thenCombine(g, a);
449 +            }
450 +        },
451 +
452 + //             /** Experimental way to do more testing */
453 + //         REVERSE_DEFAULT {
454 + //             public <T,U> CompletableFuture<Void> runAfterBoth
455 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
456 + //                 return g.runAfterBoth(f, a);
457 + //             }
458 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
459 + //                 (CompletableFuture<T> f,
460 + //                  CompletionStage<? extends U> g,
461 + //                  BiConsumer<? super T,? super U> a) {
462 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
463 + //             }
464 + //         },
465 +
466 +        DEFAULT_ASYNC {
467 +            public <T,U> CompletableFuture<Void> runAfterBoth
468 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
469 +                return f.runAfterBothAsync(g, a);
470 +            }
471 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
472 +                (CompletableFuture<T> f,
473 +                 CompletionStage<? extends U> g,
474 +                 BiConsumer<? super T,? super U> a) {
475 +                return f.thenAcceptBothAsync(g, a);
476 +            }
477 +            public <T,U,V> CompletableFuture<V> thenCombine
478 +                (CompletableFuture<T> f,
479 +                 CompletionStage<? extends U> g,
480 +                 BiFunction<? super T,? super U,? extends V> a) {
481 +                return f.thenCombineAsync(g, a);
482 +            }
483 +        },
484 +
485 + //         REVERSE_DEFAULT_ASYNC {
486 + //             public <T,U> CompletableFuture<Void> runAfterBoth
487 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
488 + //                 return f.runAfterBothAsync(g, a);
489 + //             }
490 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
491 + //                 (CompletableFuture<T> f,
492 + //                  CompletionStage<? extends U> g,
493 + //                  BiConsumer<? super T,? super U> a) {
494 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
495 + //             }
496 + //         },
497 +
498 +        EXECUTOR {
499 +            public <T,U> CompletableFuture<Void> runAfterBoth
500 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
501 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
502 +            }
503 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
504 +                (CompletableFuture<T> f,
505 +                 CompletionStage<? extends U> g,
506 +                 BiConsumer<? super T,? super U> a) {
507 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
508 +            }
509 +            public <T,U,V> CompletableFuture<V> thenCombine
510 +                (CompletableFuture<T> f,
511 +                 CompletionStage<? extends U> g,
512 +                 BiFunction<? super T,? super U,? extends V> a) {
513 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
514 +            }
515 +        };
516 +
517 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
518 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
519 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
520 +            (CompletableFuture<T> f,
521 +             CompletionStage<? extends U> g,
522 +             BiConsumer<? super T,? super U> a);
523 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
524 +            (CompletableFuture<T> f,
525 +             CompletionStage<? extends U> g,
526 +             BiFunction<? super T,? super U,? extends V> a);
527 +    }
528  
529      /**
530       * exceptionally action completes with function value on source
531 <     * exception;  otherwise with source value
531 >     * exception; otherwise with source value
532       */
533      public void testExceptionally() {
534 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
534 >        CompletableFuture<Integer> f = new CompletableFuture<>();
535          ExceptionToInteger r = new ExceptionToInteger();
536          CompletableFuture<Integer> g = f.exceptionally(r);
537          f.completeExceptionally(new CFException());
538          checkCompletedNormally(g, three);
539  
540 <        f = new CompletableFuture<Integer>();
540 >        f = new CompletableFuture<>();
541          r = new ExceptionToInteger();
542          g = f.exceptionally(r);
543          f.complete(one);
# Line 396 | Line 552 | public class CompletableFutureTest exten
552          CompletableFuture<Integer> f, g;
553          IntegerHandler r;
554  
555 <        f = new CompletableFuture<Integer>();
555 >        f = new CompletableFuture<>();
556          f.completeExceptionally(new CFException());
557          g = f.handle(r = new IntegerHandler());
558          assertTrue(r.ran);
559          checkCompletedNormally(g, three);
560  
561 <        f = new CompletableFuture<Integer>();
561 >        f = new CompletableFuture<>();
562          g = f.handle(r = new IntegerHandler());
563          assertFalse(r.ran);
564          f.completeExceptionally(new CFException());
565          checkCompletedNormally(g, three);
566          assertTrue(r.ran);
567  
568 <        f = new CompletableFuture<Integer>();
568 >        f = new CompletableFuture<>();
569          f.complete(one);
570          g = f.handle(r = new IntegerHandler());
571          assertTrue(r.ran);
572          checkCompletedNormally(g, two);
573  
574 <        f = new CompletableFuture<Integer>();
574 >        f = new CompletableFuture<>();
575          g = f.handle(r = new IntegerHandler());
576          assertFalse(r.ran);
577          f.complete(one);
# Line 493 | Line 649 | public class CompletableFutureTest exten
649       * thenRun result completes normally after normal completion of source
650       */
651      public void testThenRun() {
652 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
653 <        Noop r = new Noop();
654 <        CompletableFuture<Void> g = f.thenRun(r);
652 >        CompletableFuture<Integer> f;
653 >        CompletableFuture<Void> g;
654 >        Noop r;
655 >
656 >        f = new CompletableFuture<>();
657 >        g = f.thenRun(r = new Noop());
658          f.complete(null);
659          checkCompletedNormally(g, null);
660 <        // reordered version
661 <        f = new CompletableFuture<Integer>();
660 >        assertTrue(r.ran);
661 >
662 >        f = new CompletableFuture<>();
663          f.complete(null);
664 <        r = new Noop();
505 <        g = f.thenRun(r);
664 >        g = f.thenRun(r = new Noop());
665          checkCompletedNormally(g, null);
666 +        assertTrue(r.ran);
667      }
668  
669      /**
# Line 511 | Line 671 | public class CompletableFutureTest exten
671       * completion of source
672       */
673      public void testThenRun2() {
674 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
675 <        Noop r = new Noop();
676 <        CompletableFuture<Void> g = f.thenRun(r);
674 >        CompletableFuture<Integer> f;
675 >        CompletableFuture<Void> g;
676 >        Noop r;
677 >
678 >        f = new CompletableFuture<>();
679 >        g = f.thenRun(r = new Noop());
680 >        f.completeExceptionally(new CFException());
681 >        checkCompletedWithWrappedCFException(g);
682 >        assertFalse(r.ran);
683 >
684 >        f = new CompletableFuture<>();
685          f.completeExceptionally(new CFException());
686 +        g = f.thenRun(r = new Noop());
687          checkCompletedWithWrappedCFException(g);
688 +        assertFalse(r.ran);
689      }
690  
691      /**
692       * thenRun result completes exceptionally if action does
693       */
694      public void testThenRun3() {
695 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
696 <        FailingNoop r = new FailingNoop();
697 <        CompletableFuture<Void> g = f.thenRun(r);
695 >        CompletableFuture<Integer> f;
696 >        CompletableFuture<Void> g;
697 >        FailingNoop r;
698 >
699 >        f = new CompletableFuture<>();
700 >        g = f.thenRun(r = new FailingNoop());
701 >        f.complete(null);
702 >        checkCompletedWithWrappedCFException(g);
703 >
704 >        f = new CompletableFuture<>();
705          f.complete(null);
706 +        g = f.thenRun(r = new FailingNoop());
707          checkCompletedWithWrappedCFException(g);
708      }
709  
# Line 533 | Line 711 | public class CompletableFutureTest exten
711       * thenRun result completes exceptionally if source cancelled
712       */
713      public void testThenRun4() {
714 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
715 <        Noop r = new Noop();
716 <        CompletableFuture<Void> g = f.thenRun(r);
714 >        CompletableFuture<Integer> f;
715 >        CompletableFuture<Void> g;
716 >        Noop r;
717 >
718 >        f = new CompletableFuture<>();
719 >        g = f.thenRun(r = new Noop());
720 >        assertTrue(f.cancel(true));
721 >        checkCompletedWithWrappedCancellationException(g);
722 >
723 >        f = new CompletableFuture<>();
724          assertTrue(f.cancel(true));
725 +        g = f.thenRun(r = new Noop());
726          checkCompletedWithWrappedCancellationException(g);
727      }
728  
# Line 544 | Line 730 | public class CompletableFutureTest exten
730       * thenApply result completes normally after normal completion of source
731       */
732      public void testThenApply() {
733 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
733 >        CompletableFuture<Integer> f = new CompletableFuture<>();
734          CompletableFuture<Integer> g = f.thenApply(inc);
735          f.complete(one);
736          checkCompletedNormally(g, two);
# Line 555 | Line 741 | public class CompletableFutureTest exten
741       * completion of source
742       */
743      public void testThenApply2() {
744 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
744 >        CompletableFuture<Integer> f = new CompletableFuture<>();
745          CompletableFuture<Integer> g = f.thenApply(inc);
746          f.completeExceptionally(new CFException());
747          checkCompletedWithWrappedCFException(g);
# Line 565 | Line 751 | public class CompletableFutureTest exten
751       * thenApply result completes exceptionally if action does
752       */
753      public void testThenApply3() {
754 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
754 >        CompletableFuture<Integer> f = new CompletableFuture<>();
755          CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
756          f.complete(one);
757          checkCompletedWithWrappedCFException(g);
# Line 575 | Line 761 | public class CompletableFutureTest exten
761       * thenApply result completes exceptionally if source cancelled
762       */
763      public void testThenApply4() {
764 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
764 >        CompletableFuture<Integer> f = new CompletableFuture<>();
765          CompletableFuture<Integer> g = f.thenApply(inc);
766          assertTrue(f.cancel(true));
767          checkCompletedWithWrappedCancellationException(g);
# Line 585 | Line 771 | public class CompletableFutureTest exten
771       * thenAccept result completes normally after normal completion of source
772       */
773      public void testThenAccept() {
774 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
774 >        CompletableFuture<Integer> f = new CompletableFuture<>();
775          IncAction r = new IncAction();
776          CompletableFuture<Void> g = f.thenAccept(r);
777          f.complete(one);
# Line 598 | Line 784 | public class CompletableFutureTest exten
784       * completion of source
785       */
786      public void testThenAccept2() {
787 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
787 >        CompletableFuture<Integer> f = new CompletableFuture<>();
788          IncAction r = new IncAction();
789          CompletableFuture<Void> g = f.thenAccept(r);
790          f.completeExceptionally(new CFException());
# Line 609 | Line 795 | public class CompletableFutureTest exten
795       * thenAccept result completes exceptionally if action does
796       */
797      public void testThenAccept3() {
798 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
798 >        CompletableFuture<Integer> f = new CompletableFuture<>();
799          FailingConsumer r = new FailingConsumer();
800          CompletableFuture<Void> g = f.thenAccept(r);
801          f.complete(one);
# Line 621 | Line 807 | public class CompletableFutureTest exten
807       * thenAccept result completes exceptionally if source cancelled
808       */
809      public void testThenAccept4() {
810 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
810 >        CompletableFuture<Integer> f = new CompletableFuture<>();
811          IncAction r = new IncAction();
812          CompletableFuture<Void> g = f.thenAccept(r);
813          assertTrue(f.cancel(true));
814          checkCompletedWithWrappedCancellationException(g);
815      }
816  
631
817      /**
818       * thenCombine result completes normally after normal completion
819       * of sources
820       */
821 <    public void testThenCombine() {
822 <        CompletableFuture<Integer> f, g, h;
821 >    public void testThenCombine_normalCompletion1() {
822 >        for (ExecutionMode m : ExecutionMode.values())
823 >        for (Integer v1 : new Integer[] { 1, null })
824 >        for (Integer v2 : new Integer[] { 2, null }) {
825 >
826 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
827 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
828 >        final SubtractFunction r = new SubtractFunction();
829 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
830 >
831 >        f.complete(v1);
832 >        checkIncomplete(h);
833 >        assertFalse(r.ran());
834 >        g.complete(v2);
835 >
836 >        checkCompletedNormally(h, subtract(v1, v2));
837 >        checkCompletedNormally(f, v1);
838 >        checkCompletedNormally(g, v2);
839 >        }
840 >    }
841 >
842 >    public void testThenCombine_normalCompletion2() {
843 >        for (ExecutionMode m : ExecutionMode.values())
844 >        for (Integer v1 : new Integer[] { 1, null })
845 >        for (Integer v2 : new Integer[] { 2, null }) {
846 >
847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
848 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
849 >        final SubtractFunction r = new SubtractFunction();
850 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
851 >
852 >        g.complete(v2);
853 >        checkIncomplete(h);
854 >        assertFalse(r.ran());
855 >        f.complete(v1);
856 >
857 >        checkCompletedNormally(h, subtract(v1, v2));
858 >        checkCompletedNormally(f, v1);
859 >        checkCompletedNormally(g, v2);
860 >        }
861 >    }
862 >
863 >    public void testThenCombine_normalCompletion3() {
864 >        for (ExecutionMode m : ExecutionMode.values())
865 >        for (Integer v1 : new Integer[] { 1, null })
866 >        for (Integer v2 : new Integer[] { 2, null }) {
867 >
868 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
869 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
870 >        final SubtractFunction r = new SubtractFunction();
871 >
872 >        g.complete(v2);
873 >        f.complete(v1);
874 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
875 >
876 >        checkCompletedNormally(h, subtract(v1, v2));
877 >        checkCompletedNormally(f, v1);
878 >        checkCompletedNormally(g, v2);
879 >        }
880 >    }
881  
882 <        f = new CompletableFuture<Integer>();
883 <        g = new CompletableFuture<Integer>();
884 <        h = f.thenCombine(g, subtract);
885 <        f.complete(3);
886 <        checkIncomplete(h);
887 <        g.complete(1);
888 <        checkCompletedNormally(h, 2);
889 <
890 <        f = new CompletableFuture<Integer>();
891 <        g = new CompletableFuture<Integer>();
892 <        h = f.thenCombine(g, subtract);
893 <        g.complete(1);
894 <        checkIncomplete(h);
895 <        f.complete(3);
896 <        checkCompletedNormally(h, 2);
897 <
898 <        f = new CompletableFuture<Integer>();
656 <        g = new CompletableFuture<Integer>();
657 <        g.complete(1);
658 <        f.complete(3);
659 <        h = f.thenCombine(g, subtract);
660 <        checkCompletedNormally(h, 2);
882 >    public void testThenCombine_normalCompletion4() {
883 >        for (ExecutionMode m : ExecutionMode.values())
884 >        for (Integer v1 : new Integer[] { 1, null })
885 >        for (Integer v2 : new Integer[] { 2, null }) {
886 >
887 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
888 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
889 >        final SubtractFunction r = new SubtractFunction();
890 >
891 >        f.complete(v1);
892 >        g.complete(v2);
893 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
894 >
895 >        checkCompletedNormally(h, subtract(v1, v2));
896 >        checkCompletedNormally(f, v1);
897 >        checkCompletedNormally(g, v2);
898 >        }
899      }
900  
901      /**
902       * thenCombine result completes exceptionally after exceptional
903       * completion of either source
904       */
905 <    public void testThenCombine2() {
906 <        CompletableFuture<Integer> f, g, h;
905 >    public void testThenCombine_exceptionalCompletion1() {
906 >        for (ExecutionMode m : ExecutionMode.values())
907 >        for (Integer v1 : new Integer[] { 1, null }) {
908 >
909 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
910 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
911 >        final SubtractFunction r = new SubtractFunction();
912 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
913 >        final CFException ex = new CFException();
914  
915 <        f = new CompletableFuture<Integer>();
671 <        g = new CompletableFuture<Integer>();
672 <        h = f.thenCombine(g, subtract);
673 <        f.completeExceptionally(new CFException());
915 >        f.completeExceptionally(ex);
916          checkIncomplete(h);
917 <        g.complete(1);
676 <        checkCompletedWithWrappedCFException(h);
917 >        g.complete(v1);
918  
919 <        f = new CompletableFuture<Integer>();
920 <        g = new CompletableFuture<Integer>();
921 <        h = f.thenCombine(g, subtract);
922 <        g.completeExceptionally(new CFException());
919 >        checkCompletedWithWrappedCFException(h, ex);
920 >        checkCompletedWithWrappedCFException(f, ex);
921 >        assertFalse(r.ran());
922 >        checkCompletedNormally(g, v1);
923 >        }
924 >    }
925 >
926 >    public void testThenCombine_exceptionalCompletion2() {
927 >        for (ExecutionMode m : ExecutionMode.values())
928 >        for (Integer v1 : new Integer[] { 1, null }) {
929 >
930 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
931 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
932 >        final SubtractFunction r = new SubtractFunction();
933 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
934 >        final CFException ex = new CFException();
935 >
936 >        g.completeExceptionally(ex);
937          checkIncomplete(h);
938 <        f.complete(3);
684 <        checkCompletedWithWrappedCFException(h);
938 >        f.complete(v1);
939  
940 <        f = new CompletableFuture<Integer>();
941 <        g = new CompletableFuture<Integer>();
942 <        f.complete(3);
943 <        g.completeExceptionally(new CFException());
944 <        h = f.thenCombine(g, subtract);
945 <        checkCompletedWithWrappedCFException(h);
940 >        checkCompletedWithWrappedCFException(h, ex);
941 >        checkCompletedWithWrappedCFException(g, ex);
942 >        assertFalse(r.ran());
943 >        checkCompletedNormally(f, v1);
944 >        }
945 >    }
946  
947 <        f = new CompletableFuture<Integer>();
948 <        g = new CompletableFuture<Integer>();
949 <        f.completeExceptionally(new CFException());
950 <        g.complete(3);
951 <        h = f.thenCombine(g, subtract);
952 <        checkCompletedWithWrappedCFException(h);
947 >    public void testThenCombine_exceptionalCompletion3() {
948 >        for (ExecutionMode m : ExecutionMode.values())
949 >        for (Integer v1 : new Integer[] { 1, null }) {
950 >
951 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
952 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
953 >        final SubtractFunction r = new SubtractFunction();
954 >        final CFException ex = new CFException();
955 >
956 >        g.completeExceptionally(ex);
957 >        f.complete(v1);
958 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
959 >
960 >        checkCompletedWithWrappedCFException(h, ex);
961 >        checkCompletedWithWrappedCFException(g, ex);
962 >        assertFalse(r.ran());
963 >        checkCompletedNormally(f, v1);
964 >        }
965 >    }
966 >
967 >    public void testThenCombine_exceptionalCompletion4() {
968 >        for (ExecutionMode m : ExecutionMode.values())
969 >        for (Integer v1 : new Integer[] { 1, null }) {
970 >
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
973 >        final SubtractFunction r = new SubtractFunction();
974 >        final CFException ex = new CFException();
975 >
976 >        f.completeExceptionally(ex);
977 >        g.complete(v1);
978 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, subtract);
979 >
980 >        checkCompletedWithWrappedCFException(h, ex);
981 >        checkCompletedWithWrappedCFException(f, ex);
982 >        assertFalse(r.ran());
983 >        checkCompletedNormally(g, v1);
984 >        }
985      }
986  
987      /**
988       * thenCombine result completes exceptionally if action does
989       */
990 <    public void testThenCombine3() {
991 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
992 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
993 <        FailingBiFunction r = new FailingBiFunction();
994 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
995 <        f.complete(one);
996 <        checkIncomplete(g);
997 <        assertFalse(r.ran);
998 <        f2.complete(two);
999 <        checkCompletedWithWrappedCFException(g);
1000 <        assertTrue(r.ran);
990 >    public void testThenCombine_actionFailed1() {
991 >        for (ExecutionMode m : ExecutionMode.values())
992 >        for (Integer v1 : new Integer[] { 1, null })
993 >        for (Integer v2 : new Integer[] { 2, null }) {
994 >
995 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
996 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
997 >        final FailingBiFunction r = new FailingBiFunction();
998 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
999 >
1000 >        f.complete(v1);
1001 >        checkIncomplete(h);
1002 >        g.complete(v2);
1003 >
1004 >        checkCompletedWithWrappedCFException(h);
1005 >        checkCompletedNormally(f, v1);
1006 >        checkCompletedNormally(g, v2);
1007 >        }
1008 >    }
1009 >
1010 >    public void testThenCombine_actionFailed2() {
1011 >        for (ExecutionMode m : ExecutionMode.values())
1012 >        for (Integer v1 : new Integer[] { 1, null })
1013 >        for (Integer v2 : new Integer[] { 2, null }) {
1014 >
1015 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1016 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1017 >        final FailingBiFunction r = new FailingBiFunction();
1018 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1019 >
1020 >        g.complete(v2);
1021 >        checkIncomplete(h);
1022 >        f.complete(v1);
1023 >
1024 >        checkCompletedWithWrappedCFException(h);
1025 >        checkCompletedNormally(f, v1);
1026 >        checkCompletedNormally(g, v2);
1027 >        }
1028      }
1029  
1030      /**
1031       * thenCombine result completes exceptionally if either source cancelled
1032       */
1033 <    public void testThenCombine4() {
1034 <        CompletableFuture<Integer> f, g, h;
1033 >    public void testThenCombine_sourceCancelled1() {
1034 >        for (ExecutionMode m : ExecutionMode.values())
1035 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1036 >        for (Integer v1 : new Integer[] { 1, null }) {
1037 >
1038 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1039 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1040 >        final SubtractFunction r = new SubtractFunction();
1041 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1042  
1043 <        f = new CompletableFuture<Integer>();
724 <        g = new CompletableFuture<Integer>();
725 <        h = f.thenCombine(g, subtract);
726 <        assertTrue(f.cancel(true));
1043 >        assertTrue(f.cancel(mayInterruptIfRunning));
1044          checkIncomplete(h);
1045 <        g.complete(1);
1045 >        g.complete(v1);
1046 >
1047          checkCompletedWithWrappedCancellationException(h);
1048 +        checkCancelled(f);
1049 +        assertFalse(r.ran());
1050 +        checkCompletedNormally(g, v1);
1051 +        }
1052 +    }
1053  
1054 <        f = new CompletableFuture<Integer>();
1055 <        g = new CompletableFuture<Integer>();
1056 <        h = f.thenCombine(g, subtract);
1057 <        assertTrue(g.cancel(true));
1054 >    public void testThenCombine_sourceCancelled2() {
1055 >        for (ExecutionMode m : ExecutionMode.values())
1056 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1057 >        for (Integer v1 : new Integer[] { 1, null }) {
1058 >
1059 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1060 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1061 >        final SubtractFunction r = new SubtractFunction();
1062 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1063 >
1064 >        assertTrue(g.cancel(mayInterruptIfRunning));
1065          checkIncomplete(h);
1066 <        f.complete(3);
1066 >        f.complete(v1);
1067 >
1068          checkCompletedWithWrappedCancellationException(h);
1069 +        checkCancelled(g);
1070 +        assertFalse(r.ran());
1071 +        checkCompletedNormally(f, v1);
1072 +        }
1073 +    }
1074 +
1075 +    public void testThenCombine_sourceCancelled3() {
1076 +        for (ExecutionMode m : ExecutionMode.values())
1077 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1078 +        for (Integer v1 : new Integer[] { 1, null }) {
1079 +
1080 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1081 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1082 +        final SubtractFunction r = new SubtractFunction();
1083 +
1084 +        assertTrue(g.cancel(mayInterruptIfRunning));
1085 +        f.complete(v1);
1086 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1087 +
1088 +        checkCompletedWithWrappedCancellationException(h);
1089 +        checkCancelled(g);
1090 +        assertFalse(r.ran());
1091 +        checkCompletedNormally(f, v1);
1092 +        }
1093 +    }
1094 +
1095 +    public void testThenCombine_sourceCancelled4() {
1096 +        for (ExecutionMode m : ExecutionMode.values())
1097 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1098 +        for (Integer v1 : new Integer[] { 1, null }) {
1099 +
1100 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1101 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1102 +        final SubtractFunction r = new SubtractFunction();
1103 +
1104 +        assertTrue(f.cancel(mayInterruptIfRunning));
1105 +        g.complete(v1);
1106 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1107  
739        f = new CompletableFuture<Integer>();
740        g = new CompletableFuture<Integer>();
741        assertTrue(f.cancel(true));
742        assertTrue(g.cancel(true));
743        h = f.thenCombine(g, subtract);
1108          checkCompletedWithWrappedCancellationException(h);
1109 +        checkCancelled(f);
1110 +        assertFalse(r.ran());
1111 +        checkCompletedNormally(g, v1);
1112 +        }
1113      }
1114  
1115      /**
1116       * thenAcceptBoth result completes normally after normal
1117       * completion of sources
1118       */
1119 <    public void testThenAcceptBoth() {
1120 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1121 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1122 <        AddAction r = new AddAction();
1123 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1124 <        f.complete(one);
1125 <        checkIncomplete(g);
1126 <        f2.complete(two);
1127 <        checkCompletedNormally(g, null);
760 <        assertEquals(r.value, 3);
1119 >    public void testThenAcceptBoth_normalCompletion1() {
1120 >        for (ExecutionMode m : ExecutionMode.values())
1121 >        for (Integer v1 : new Integer[] { 1, null })
1122 >        for (Integer v2 : new Integer[] { 2, null }) {
1123 >
1124 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1125 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1126 >        final SubtractAction r = new SubtractAction();
1127 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1128  
1129 <        r = new AddAction();
1130 <        f = new CompletableFuture<Integer>();
1131 <        f.complete(one);
1132 <        f2 = new CompletableFuture<Integer>();
1133 <        g = f.thenAcceptBoth(f2, r);
1134 <        checkIncomplete(g);
1135 <        f2.complete(two);
1136 <        checkCompletedNormally(g, null);
1137 <        assertEquals(r.value, 3);
1129 >        f.complete(v1);
1130 >        checkIncomplete(h);
1131 >        assertEquals(r.value, 0);
1132 >        g.complete(v2);
1133 >
1134 >        checkCompletedNormally(h, null);
1135 >        assertEquals(r.value, subtract(v1, v2));
1136 >        checkCompletedNormally(f, v1);
1137 >        checkCompletedNormally(g, v2);
1138 >        }
1139 >    }
1140 >
1141 >    public void testThenAcceptBoth_normalCompletion2() {
1142 >        for (ExecutionMode m : ExecutionMode.values())
1143 >        for (Integer v1 : new Integer[] { 1, null })
1144 >        for (Integer v2 : new Integer[] { 2, null }) {
1145 >
1146 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1147 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1148 >        final SubtractAction r = new SubtractAction();
1149 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1150 >
1151 >        g.complete(v2);
1152 >        checkIncomplete(h);
1153 >        assertEquals(r.value, 0);
1154 >        f.complete(v1);
1155 >
1156 >        checkCompletedNormally(h, null);
1157 >        assertEquals(r.value, subtract(v1, v2));
1158 >        checkCompletedNormally(f, v1);
1159 >        checkCompletedNormally(g, v2);
1160 >        }
1161 >    }
1162 >
1163 >    public void testThenAcceptBoth_normalCompletion3() {
1164 >        for (ExecutionMode m : ExecutionMode.values())
1165 >        for (Integer v1 : new Integer[] { 1, null })
1166 >        for (Integer v2 : new Integer[] { 2, null }) {
1167 >
1168 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1169 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1170 >        final SubtractAction r = new SubtractAction();
1171 >
1172 >        g.complete(v2);
1173 >        f.complete(v1);
1174 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1175 >
1176 >        checkCompletedNormally(h, null);
1177 >        assertEquals(r.value, subtract(v1, v2));
1178 >        checkCompletedNormally(f, v1);
1179 >        checkCompletedNormally(g, v2);
1180 >        }
1181 >    }
1182 >
1183 >    public void testThenAcceptBoth_normalCompletion4() {
1184 >        for (ExecutionMode m : ExecutionMode.values())
1185 >        for (Integer v1 : new Integer[] { 1, null })
1186 >        for (Integer v2 : new Integer[] { 2, null }) {
1187 >
1188 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1189 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1190 >        final SubtractAction r = new SubtractAction();
1191 >
1192 >        f.complete(v1);
1193 >        g.complete(v2);
1194 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1195 >
1196 >        checkCompletedNormally(h, null);
1197 >        assertEquals(r.value, subtract(v1, v2));
1198 >        checkCompletedNormally(f, v1);
1199 >        checkCompletedNormally(g, v2);
1200 >        }
1201      }
1202  
1203      /**
1204       * thenAcceptBoth result completes exceptionally after exceptional
1205       * completion of either source
1206       */
1207 <    public void testThenAcceptBoth2() {
1208 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1209 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1210 <        AddAction r = new AddAction();
1211 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1212 <        f.completeExceptionally(new CFException());
1213 <        f2.complete(two);
1214 <        checkCompletedWithWrappedCFException(g);
1207 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1208 >        for (ExecutionMode m : ExecutionMode.values())
1209 >        for (Integer v1 : new Integer[] { 1, null }) {
1210 >
1211 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1212 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1213 >        final SubtractAction r = new SubtractAction();
1214 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1215 >        final CFException ex = new CFException();
1216  
1217 <        r = new AddAction();
1218 <        f = new CompletableFuture<Integer>();
1219 <        f.complete(one);
1220 <        f2 = new CompletableFuture<Integer>();
1221 <        g = f.thenAcceptBoth(f2, r);
1222 <        f2.completeExceptionally(new CFException());
1223 <        checkCompletedWithWrappedCFException(g);
1217 >        f.completeExceptionally(ex);
1218 >        checkIncomplete(h);
1219 >        g.complete(v1);
1220 >
1221 >        checkCompletedWithWrappedCFException(h, ex);
1222 >        checkCompletedWithWrappedCFException(f, ex);
1223 >        assertFalse(r.ran());
1224 >        checkCompletedNormally(g, v1);
1225 >        }
1226 >    }
1227 >
1228 >    public void testThenAcceptBoth_exceptionalCompletion2() {
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 SubtractAction r = new SubtractAction();
1235 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1236 >        final CFException ex = new CFException();
1237 >
1238 >        g.completeExceptionally(ex);
1239 >        checkIncomplete(h);
1240 >        f.complete(v1);
1241 >
1242 >        checkCompletedWithWrappedCFException(h, ex);
1243 >        checkCompletedWithWrappedCFException(g, ex);
1244 >        assertFalse(r.ran());
1245 >        checkCompletedNormally(f, v1);
1246 >        }
1247 >    }
1248 >
1249 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1250 >        for (ExecutionMode m : ExecutionMode.values())
1251 >        for (Integer v1 : new Integer[] { 1, null }) {
1252 >
1253 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1254 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1255 >        final SubtractAction r = new SubtractAction();
1256 >        final CFException ex = new CFException();
1257 >
1258 >        g.completeExceptionally(ex);
1259 >        f.complete(v1);
1260 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1261 >
1262 >        checkCompletedWithWrappedCFException(h, ex);
1263 >        checkCompletedWithWrappedCFException(g, ex);
1264 >        assertFalse(r.ran());
1265 >        checkCompletedNormally(f, v1);
1266 >        }
1267 >    }
1268 >
1269 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1270 >        for (ExecutionMode m : ExecutionMode.values())
1271 >        for (Integer v1 : new Integer[] { 1, null }) {
1272 >
1273 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1274 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1275 >        final SubtractAction r = new SubtractAction();
1276 >        final CFException ex = new CFException();
1277 >
1278 >        f.completeExceptionally(ex);
1279 >        g.complete(v1);
1280 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1281 >
1282 >        checkCompletedWithWrappedCFException(h, ex);
1283 >        checkCompletedWithWrappedCFException(f, ex);
1284 >        assertFalse(r.ran());
1285 >        checkCompletedNormally(g, v1);
1286 >        }
1287      }
1288  
1289      /**
1290       * thenAcceptBoth result completes exceptionally if action does
1291       */
1292 <    public void testThenAcceptBoth3() {
1293 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1294 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1295 <        FailingBiConsumer r = new FailingBiConsumer();
1296 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1297 <        f.complete(one);
1298 <        checkIncomplete(g);
1299 <        f2.complete(two);
1300 <        checkCompletedWithWrappedCFException(g);
1292 >    public void testThenAcceptBoth_actionFailed1() {
1293 >        for (ExecutionMode m : ExecutionMode.values())
1294 >        for (Integer v1 : new Integer[] { 1, null })
1295 >        for (Integer v2 : new Integer[] { 2, null }) {
1296 >
1297 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1298 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1299 >        final FailingBiConsumer r = new FailingBiConsumer();
1300 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1301 >
1302 >        f.complete(v1);
1303 >        checkIncomplete(h);
1304 >        g.complete(v2);
1305 >
1306 >        checkCompletedWithWrappedCFException(h);
1307 >        checkCompletedNormally(f, v1);
1308 >        checkCompletedNormally(g, v2);
1309 >        }
1310 >    }
1311 >
1312 >    public void testThenAcceptBoth_actionFailed2() {
1313 >        for (ExecutionMode m : ExecutionMode.values())
1314 >        for (Integer v1 : new Integer[] { 1, null })
1315 >        for (Integer v2 : new Integer[] { 2, null }) {
1316 >
1317 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1318 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1319 >        final FailingBiConsumer r = new FailingBiConsumer();
1320 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1321 >
1322 >        g.complete(v2);
1323 >        checkIncomplete(h);
1324 >        f.complete(v1);
1325 >
1326 >        checkCompletedWithWrappedCFException(h);
1327 >        checkCompletedNormally(f, v1);
1328 >        checkCompletedNormally(g, v2);
1329 >        }
1330      }
1331  
1332      /**
1333       * thenAcceptBoth result completes exceptionally if either source cancelled
1334       */
1335 <    public void testThenAcceptBoth4() {
1336 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1337 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1338 <        AddAction r = new AddAction();
1339 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1340 <        assertTrue(f.cancel(true));
1341 <        f2.complete(two);
1342 <        checkCompletedWithWrappedCancellationException(g);
1343 <        f = new CompletableFuture<Integer>();
1344 <        f2 = new CompletableFuture<Integer>();
1345 <        r = new AddAction();
1346 <        g = f.thenAcceptBoth(f2, r);
1347 <        f.complete(one);
1348 <        assertTrue(f2.cancel(true));
1349 <        checkCompletedWithWrappedCancellationException(g);
1335 >    public void testThenAcceptBoth_sourceCancelled1() {
1336 >        for (ExecutionMode m : ExecutionMode.values())
1337 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1338 >        for (Integer v1 : new Integer[] { 1, null }) {
1339 >
1340 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1341 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1342 >        final SubtractAction r = new SubtractAction();
1343 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1344 >
1345 >        assertTrue(f.cancel(mayInterruptIfRunning));
1346 >        checkIncomplete(h);
1347 >        g.complete(v1);
1348 >
1349 >        checkCompletedWithWrappedCancellationException(h);
1350 >        checkCancelled(f);
1351 >        assertFalse(r.ran());
1352 >        checkCompletedNormally(g, v1);
1353 >        }
1354 >    }
1355 >
1356 >    public void testThenAcceptBoth_sourceCancelled2() {
1357 >        for (ExecutionMode m : ExecutionMode.values())
1358 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1359 >        for (Integer v1 : new Integer[] { 1, null }) {
1360 >
1361 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1362 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1363 >        final SubtractAction r = new SubtractAction();
1364 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1365 >
1366 >        assertTrue(g.cancel(mayInterruptIfRunning));
1367 >        checkIncomplete(h);
1368 >        f.complete(v1);
1369 >
1370 >        checkCompletedWithWrappedCancellationException(h);
1371 >        checkCancelled(g);
1372 >        assertFalse(r.ran());
1373 >        checkCompletedNormally(f, v1);
1374 >        }
1375 >    }
1376 >
1377 >    public void testThenAcceptBoth_sourceCancelled3() {
1378 >        for (ExecutionMode m : ExecutionMode.values())
1379 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1380 >        for (Integer v1 : new Integer[] { 1, null }) {
1381 >
1382 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1383 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1384 >        final SubtractAction r = new SubtractAction();
1385 >
1386 >        assertTrue(g.cancel(mayInterruptIfRunning));
1387 >        f.complete(v1);
1388 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1389 >
1390 >        checkCompletedWithWrappedCancellationException(h);
1391 >        checkCancelled(g);
1392 >        assertFalse(r.ran());
1393 >        checkCompletedNormally(f, v1);
1394 >        }
1395 >    }
1396 >
1397 >    public void testThenAcceptBoth_sourceCancelled4() {
1398 >        for (ExecutionMode m : ExecutionMode.values())
1399 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1400 >        for (Integer v1 : new Integer[] { 1, null }) {
1401 >
1402 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1403 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1404 >        final SubtractAction r = new SubtractAction();
1405 >
1406 >        assertTrue(f.cancel(mayInterruptIfRunning));
1407 >        g.complete(v1);
1408 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1409 >
1410 >        checkCompletedWithWrappedCancellationException(h);
1411 >        checkCancelled(f);
1412 >        assertFalse(r.ran());
1413 >        checkCompletedNormally(g, v1);
1414 >        }
1415      }
1416  
1417      /**
1418       * runAfterBoth result completes normally after normal
1419       * completion of sources
1420       */
1421 <    public void testRunAfterBoth() {
1422 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1423 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1424 <        Noop r = new Noop();
1425 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1426 <        f.complete(one);
1427 <        checkIncomplete(g);
1428 <        f2.complete(two);
1429 <        checkCompletedNormally(g, null);
1421 >    public void testRunAfterBoth_normalCompletion1() {
1422 >        for (ExecutionMode m : ExecutionMode.values())
1423 >        for (Integer v1 : new Integer[] { 1, null })
1424 >        for (Integer v2 : new Integer[] { 2, null }) {
1425 >
1426 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1427 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1428 >        final Noop r = new Noop();
1429 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1430 >
1431 >        f.complete(v1);
1432 >        checkIncomplete(h);
1433 >        assertFalse(r.ran);
1434 >        g.complete(v2);
1435 >
1436 >        checkCompletedNormally(h, null);
1437          assertTrue(r.ran);
1438 +        checkCompletedNormally(f, v1);
1439 +        checkCompletedNormally(g, v2);
1440 +        }
1441 +    }
1442  
1443 <        r = new Noop();
1444 <        f = new CompletableFuture<Integer>();
1445 <        f.complete(one);
1446 <        f2 = new CompletableFuture<Integer>();
1447 <        g = f.runAfterBoth(f2, r);
1448 <        checkIncomplete(g);
1449 <        f2.complete(two);
1450 <        checkCompletedNormally(g, null);
1443 >    public void testRunAfterBoth_normalCompletion2() {
1444 >        for (ExecutionMode m : ExecutionMode.values())
1445 >        for (Integer v1 : new Integer[] { 1, null })
1446 >        for (Integer v2 : new Integer[] { 2, null }) {
1447 >
1448 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1449 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1450 >        final Noop r = new Noop();
1451 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1452 >
1453 >        g.complete(v2);
1454 >        checkIncomplete(h);
1455 >        assertFalse(r.ran);
1456 >        f.complete(v1);
1457 >
1458 >        checkCompletedNormally(h, null);
1459          assertTrue(r.ran);
1460 +        checkCompletedNormally(f, v1);
1461 +        checkCompletedNormally(g, v2);
1462 +        }
1463 +    }
1464 +
1465 +    public void testRunAfterBoth_normalCompletion3() {
1466 +        for (ExecutionMode m : ExecutionMode.values())
1467 +        for (Integer v1 : new Integer[] { 1, null })
1468 +        for (Integer v2 : new Integer[] { 2, null }) {
1469 +
1470 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1471 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1472 +        final Noop r = new Noop();
1473 +
1474 +        g.complete(v2);
1475 +        f.complete(v1);
1476 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1477 +
1478 +        checkCompletedNormally(h, null);
1479 +        assertTrue(r.ran);
1480 +        checkCompletedNormally(f, v1);
1481 +        checkCompletedNormally(g, v2);
1482 +        }
1483 +    }
1484 +
1485 +    public void testRunAfterBoth_normalCompletion4() {
1486 +        for (ExecutionMode m : ExecutionMode.values())
1487 +        for (Integer v1 : new Integer[] { 1, null })
1488 +        for (Integer v2 : new Integer[] { 2, null }) {
1489 +
1490 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1491 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1492 +        final Noop r = new Noop();
1493 +
1494 +        f.complete(v1);
1495 +        g.complete(v2);
1496 +        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1497 +
1498 +        checkCompletedNormally(h, null);
1499 +        assertTrue(r.ran);
1500 +        checkCompletedNormally(f, v1);
1501 +        checkCompletedNormally(g, v2);
1502 +        }
1503      }
1504  
1505      /**
1506       * runAfterBoth result completes exceptionally after exceptional
1507       * completion of either source
1508       */
1509 <    public void testRunAfterBoth2() {
1510 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1511 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1512 <        Noop r = new Noop();
1513 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1514 <        f.completeExceptionally(new CFException());
1515 <        f2.complete(two);
1516 <        checkCompletedWithWrappedCFException(g);
1509 >    public void testRunAfterBoth_exceptionalCompletion1() {
1510 >        for (ExecutionMode m : ExecutionMode.values())
1511 >        for (Integer v1 : new Integer[] { 1, null }) {
1512 >
1513 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1514 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1515 >        final Noop r = new Noop();
1516 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1517 >        final CFException ex = new CFException();
1518  
1519 <        r = new Noop();
1520 <        f = new CompletableFuture<Integer>();
1521 <        f.complete(one);
1522 <        f2 = new CompletableFuture<Integer>();
1523 <        g = f.runAfterBoth(f2, r);
1524 <        f2.completeExceptionally(new CFException());
1525 <        checkCompletedWithWrappedCFException(g);
1519 >        f.completeExceptionally(ex);
1520 >        checkIncomplete(h);
1521 >        g.complete(v1);
1522 >
1523 >        checkCompletedWithWrappedCFException(h, ex);
1524 >        checkCompletedWithWrappedCFException(f, ex);
1525 >        assertFalse(r.ran);
1526 >        checkCompletedNormally(g, v1);
1527 >        }
1528 >    }
1529 >
1530 >    public void testRunAfterBoth_exceptionalCompletion2() {
1531 >        for (ExecutionMode m : ExecutionMode.values())
1532 >        for (Integer v1 : new Integer[] { 1, null }) {
1533 >
1534 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1535 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1536 >        final Noop r = new Noop();
1537 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1538 >        final CFException ex = new CFException();
1539 >
1540 >        g.completeExceptionally(ex);
1541 >        checkIncomplete(h);
1542 >        f.complete(v1);
1543 >
1544 >        checkCompletedWithWrappedCFException(h, ex);
1545 >        checkCompletedWithWrappedCFException(g, ex);
1546 >        assertFalse(r.ran);
1547 >        checkCompletedNormally(f, v1);
1548 >        }
1549 >    }
1550 >
1551 >    public void testRunAfterBoth_exceptionalCompletion3() {
1552 >        for (ExecutionMode m : ExecutionMode.values())
1553 >        for (Integer v1 : new Integer[] { 1, null }) {
1554 >
1555 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1556 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1557 >        final Noop r = new Noop();
1558 >        final CFException ex = new CFException();
1559 >
1560 >        g.completeExceptionally(ex);
1561 >        f.complete(v1);
1562 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1563 >
1564 >        checkCompletedWithWrappedCFException(h, ex);
1565 >        checkCompletedWithWrappedCFException(g, ex);
1566 >        assertFalse(r.ran);
1567 >        checkCompletedNormally(f, v1);
1568 >        }
1569 >    }
1570 >
1571 >    public void testRunAfterBoth_exceptionalCompletion4() {
1572 >        for (ExecutionMode m : ExecutionMode.values())
1573 >        for (Integer v1 : new Integer[] { 1, null }) {
1574 >
1575 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1576 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1577 >        final Noop r = new Noop();
1578 >        final CFException ex = new CFException();
1579 >
1580 >        f.completeExceptionally(ex);
1581 >        g.complete(v1);
1582 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1583 >
1584 >        checkCompletedWithWrappedCFException(h, ex);
1585 >        checkCompletedWithWrappedCFException(f, ex);
1586 >        assertFalse(r.ran);
1587 >        checkCompletedNormally(g, v1);
1588 >        }
1589      }
1590  
1591      /**
1592       * runAfterBoth result completes exceptionally if action does
1593       */
1594 <    public void testRunAfterBoth3() {
1595 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1596 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1597 <        FailingNoop r = new FailingNoop();
1598 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1599 <        f.complete(one);
1600 <        checkIncomplete(g);
1601 <        f2.complete(two);
1602 <        checkCompletedWithWrappedCFException(g);
1594 >    public void testRunAfterBoth_actionFailed1() {
1595 >        for (ExecutionMode m : ExecutionMode.values())
1596 >        for (Integer v1 : new Integer[] { 1, null })
1597 >        for (Integer v2 : new Integer[] { 2, null }) {
1598 >
1599 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1600 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1601 >        final FailingNoop r = new FailingNoop();
1602 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1603 >
1604 >        f.complete(v1);
1605 >        checkIncomplete(h);
1606 >        g.complete(v2);
1607 >
1608 >        checkCompletedWithWrappedCFException(h);
1609 >        checkCompletedNormally(f, v1);
1610 >        checkCompletedNormally(g, v2);
1611 >        }
1612 >    }
1613 >
1614 >    public void testRunAfterBoth_actionFailed2() {
1615 >        for (ExecutionMode m : ExecutionMode.values())
1616 >        for (Integer v1 : new Integer[] { 1, null })
1617 >        for (Integer v2 : new Integer[] { 2, null }) {
1618 >
1619 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1620 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1621 >        final FailingNoop r = new FailingNoop();
1622 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1623 >
1624 >        g.complete(v2);
1625 >        checkIncomplete(h);
1626 >        f.complete(v1);
1627 >
1628 >        checkCompletedWithWrappedCFException(h);
1629 >        checkCompletedNormally(f, v1);
1630 >        checkCompletedNormally(g, v2);
1631 >        }
1632      }
1633  
1634      /**
1635       * runAfterBoth result completes exceptionally if either source cancelled
1636       */
1637 <    public void testRunAfterBoth4() {
1638 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1639 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1640 <        Noop r = new Noop();
1641 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1642 <        assertTrue(f.cancel(true));
1643 <        f2.complete(two);
1644 <        checkCompletedWithWrappedCancellationException(g);
1645 <        f = new CompletableFuture<Integer>();
1646 <        f2 = new CompletableFuture<Integer>();
1647 <        r = new Noop();
1648 <        g = f.runAfterBoth(f2, r);
1649 <        f.complete(one);
1650 <        assertTrue(f2.cancel(true));
1651 <        checkCompletedWithWrappedCancellationException(g);
1637 >    public void testRunAfterBoth_sourceCancelled1() {
1638 >        for (ExecutionMode m : ExecutionMode.values())
1639 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1640 >        for (Integer v1 : new Integer[] { 1, null }) {
1641 >
1642 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1643 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1644 >        final Noop r = new Noop();
1645 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1646 >
1647 >        assertTrue(f.cancel(mayInterruptIfRunning));
1648 >        checkIncomplete(h);
1649 >        g.complete(v1);
1650 >
1651 >        checkCompletedWithWrappedCancellationException(h);
1652 >        checkCancelled(f);
1653 >        assertFalse(r.ran);
1654 >        checkCompletedNormally(g, v1);
1655 >        }
1656 >    }
1657 >
1658 >    public void testRunAfterBoth_sourceCancelled2() {
1659 >        for (ExecutionMode m : ExecutionMode.values())
1660 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1661 >        for (Integer v1 : new Integer[] { 1, null }) {
1662 >
1663 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1664 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1665 >        final Noop r = new Noop();
1666 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1667 >
1668 >        assertTrue(g.cancel(mayInterruptIfRunning));
1669 >        checkIncomplete(h);
1670 >        f.complete(v1);
1671 >
1672 >        checkCompletedWithWrappedCancellationException(h);
1673 >        checkCancelled(g);
1674 >        assertFalse(r.ran);
1675 >        checkCompletedNormally(f, v1);
1676 >        }
1677 >    }
1678 >
1679 >    public void testRunAfterBoth_sourceCancelled3() {
1680 >        for (ExecutionMode m : ExecutionMode.values())
1681 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1682 >        for (Integer v1 : new Integer[] { 1, null }) {
1683 >
1684 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1685 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1686 >        final Noop r = new Noop();
1687 >
1688 >        assertTrue(g.cancel(mayInterruptIfRunning));
1689 >        f.complete(v1);
1690 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1691 >
1692 >        checkCompletedWithWrappedCancellationException(h);
1693 >        checkCancelled(g);
1694 >        assertFalse(r.ran);
1695 >        checkCompletedNormally(f, v1);
1696 >        }
1697 >    }
1698 >
1699 >    public void testRunAfterBoth_sourceCancelled4() {
1700 >        for (ExecutionMode m : ExecutionMode.values())
1701 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1702 >        for (Integer v1 : new Integer[] { 1, null }) {
1703 >
1704 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1705 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 >        final Noop r = new Noop();
1707 >
1708 >        assertTrue(f.cancel(mayInterruptIfRunning));
1709 >        g.complete(v1);
1710 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1711 >
1712 >        checkCompletedWithWrappedCancellationException(h);
1713 >        checkCancelled(f);
1714 >        assertFalse(r.ran);
1715 >        checkCompletedNormally(g, v1);
1716 >        }
1717      }
1718  
1719      /**
# Line 913 | Line 1721 | public class CompletableFutureTest exten
1721       * of either source
1722       */
1723      public void testApplyToEither() {
1724 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1725 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1724 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1725 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1726          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1727          f.complete(one);
1728          checkCompletedNormally(g, two);
1729          f2.complete(one);
1730          checkCompletedNormally(g, two);
1731  
1732 <        f = new CompletableFuture<Integer>();
1732 >        f = new CompletableFuture<>();
1733          f.complete(one);
1734 <        f2 = new CompletableFuture<Integer>();
1734 >        f2 = new CompletableFuture<>();
1735          g = f.applyToEither(f2, inc);
1736          checkCompletedNormally(g, two);
1737      }
# Line 933 | Line 1741 | public class CompletableFutureTest exten
1741       * completion of either source
1742       */
1743      public void testApplyToEither2() {
1744 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1745 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1744 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1745 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1746          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1747          f.completeExceptionally(new CFException());
1748          f2.complete(one);
1749          checkCompletedWithWrappedCFException(g);
1750  
1751 <        f = new CompletableFuture<Integer>();
1752 <        f2 = new CompletableFuture<Integer>();
1751 >        f = new CompletableFuture<>();
1752 >        f2 = new CompletableFuture<>();
1753          f2.completeExceptionally(new CFException());
1754          g = f.applyToEither(f2, inc);
1755          checkCompletedWithWrappedCFException(g);
# Line 951 | Line 1759 | public class CompletableFutureTest exten
1759       * applyToEither result completes exceptionally if action does
1760       */
1761      public void testApplyToEither3() {
1762 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1763 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1762 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1763 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1764          FailingFunction r = new FailingFunction();
1765          CompletableFuture<Integer> g = f.applyToEither(f2, r);
1766          f2.complete(two);
# Line 963 | Line 1771 | public class CompletableFutureTest exten
1771       * applyToEither result completes exceptionally if either source cancelled
1772       */
1773      public void testApplyToEither4() {
1774 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1775 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1774 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1775 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1776          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1777          assertTrue(f.cancel(true));
1778          checkCompletedWithWrappedCancellationException(g);
1779 <        f = new CompletableFuture<Integer>();
1780 <        f2 = new CompletableFuture<Integer>();
1779 >        f = new CompletableFuture<>();
1780 >        f2 = new CompletableFuture<>();
1781          assertTrue(f2.cancel(true));
1782          checkCompletedWithWrappedCancellationException(g);
1783      }
# Line 979 | Line 1787 | public class CompletableFutureTest exten
1787       * of either source
1788       */
1789      public void testAcceptEither() {
1790 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1791 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1790 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1791 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1792          IncAction r = new IncAction();
1793          CompletableFuture<Void> g = f.acceptEither(f2, r);
1794          f.complete(one);
# Line 990 | Line 1798 | public class CompletableFutureTest exten
1798          assertEquals(r.value, 2);
1799  
1800          r = new IncAction();
1801 <        f = new CompletableFuture<Integer>();
1801 >        f = new CompletableFuture<>();
1802          f.complete(one);
1803 <        f2 = new CompletableFuture<Integer>();
1803 >        f2 = new CompletableFuture<>();
1804          g = f.acceptEither(f2, r);
1805          checkCompletedNormally(g, null);
1806          assertEquals(r.value, 2);
# Line 1003 | Line 1811 | public class CompletableFutureTest exten
1811       * completion of either source
1812       */
1813      public void testAcceptEither2() {
1814 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1815 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1814 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1815 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1816          IncAction r = new IncAction();
1817          CompletableFuture<Void> g = f.acceptEither(f2, r);
1818          f.completeExceptionally(new CFException());
# Line 1012 | Line 1820 | public class CompletableFutureTest exten
1820          checkCompletedWithWrappedCFException(g);
1821  
1822          r = new IncAction();
1823 <        f = new CompletableFuture<Integer>();
1824 <        f2 = new CompletableFuture<Integer>();
1823 >        f = new CompletableFuture<>();
1824 >        f2 = new CompletableFuture<>();
1825          f2.completeExceptionally(new CFException());
1826          g = f.acceptEither(f2, r);
1827          checkCompletedWithWrappedCFException(g);
# Line 1023 | Line 1831 | public class CompletableFutureTest exten
1831       * acceptEither result completes exceptionally if action does
1832       */
1833      public void testAcceptEither3() {
1834 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1835 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1834 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1835 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1836          FailingConsumer r = new FailingConsumer();
1837          CompletableFuture<Void> g = f.acceptEither(f2, r);
1838          f2.complete(two);
# Line 1035 | Line 1843 | public class CompletableFutureTest exten
1843       * acceptEither result completes exceptionally if either source cancelled
1844       */
1845      public void testAcceptEither4() {
1846 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1847 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1846 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1847 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1848          IncAction r = new IncAction();
1849          CompletableFuture<Void> g = f.acceptEither(f2, r);
1850          assertTrue(f.cancel(true));
1851          checkCompletedWithWrappedCancellationException(g);
1852 <        f = new CompletableFuture<Integer>();
1853 <        f2 = new CompletableFuture<Integer>();
1852 >        f = new CompletableFuture<>();
1853 >        f2 = new CompletableFuture<>();
1854          assertTrue(f2.cancel(true));
1855          checkCompletedWithWrappedCancellationException(g);
1856      }
1857  
1050
1858      /**
1859       * runAfterEither result completes normally after normal completion
1860       * of either source
1861       */
1862      public void testRunAfterEither() {
1863 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1864 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1863 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1864 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1865          Noop r = new Noop();
1866          CompletableFuture<Void> g = f.runAfterEither(f2, r);
1867          f.complete(one);
# Line 1064 | Line 1871 | public class CompletableFutureTest exten
1871          assertTrue(r.ran);
1872  
1873          r = new Noop();
1874 <        f = new CompletableFuture<Integer>();
1874 >        f = new CompletableFuture<>();
1875          f.complete(one);
1876 <        f2 = new CompletableFuture<Integer>();
1876 >        f2 = new CompletableFuture<>();
1877          g = f.runAfterEither(f2, r);
1878          checkCompletedNormally(g, null);
1879          assertTrue(r.ran);
# Line 1077 | Line 1884 | public class CompletableFutureTest exten
1884       * completion of either source
1885       */
1886      public void testRunAfterEither2() {
1887 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1888 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1887 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1888 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1889          Noop r = new Noop();
1890          CompletableFuture<Void> g = f.runAfterEither(f2, r);
1891          f.completeExceptionally(new CFException());
# Line 1086 | Line 1893 | public class CompletableFutureTest exten
1893          checkCompletedWithWrappedCFException(g);
1894  
1895          r = new Noop();
1896 <        f = new CompletableFuture<Integer>();
1897 <        f2 = new CompletableFuture<Integer>();
1896 >        f = new CompletableFuture<>();
1897 >        f2 = new CompletableFuture<>();
1898          f2.completeExceptionally(new CFException());
1899          g = f.runAfterEither(f2, r);
1900          checkCompletedWithWrappedCFException(g);
# Line 1097 | Line 1904 | public class CompletableFutureTest exten
1904       * runAfterEither result completes exceptionally if action does
1905       */
1906      public void testRunAfterEither3() {
1907 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1908 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1907 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1908 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1909          FailingNoop r = new FailingNoop();
1910          CompletableFuture<Void> g = f.runAfterEither(f2, r);
1911          f2.complete(two);
# Line 1109 | Line 1916 | public class CompletableFutureTest exten
1916       * runAfterEither result completes exceptionally if either source cancelled
1917       */
1918      public void testRunAfterEither4() {
1919 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1920 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1919 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1920 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1921          Noop r = new Noop();
1922          CompletableFuture<Void> g = f.runAfterEither(f2, r);
1923          assertTrue(f.cancel(true));
1924          checkCompletedWithWrappedCancellationException(g);
1925 <        f = new CompletableFuture<Integer>();
1926 <        f2 = new CompletableFuture<Integer>();
1925 >        f = new CompletableFuture<>();
1926 >        f2 = new CompletableFuture<>();
1927          assertTrue(f2.cancel(true));
1928          checkCompletedWithWrappedCancellationException(g);
1929      }
# Line 1125 | Line 1932 | public class CompletableFutureTest exten
1932       * thenCompose result completes normally after normal completion of source
1933       */
1934      public void testThenCompose() {
1935 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1936 <        CompletableFutureInc r = new CompletableFutureInc();
1937 <        CompletableFuture<Integer> g = f.thenCompose(r);
1935 >        CompletableFuture<Integer> f, g;
1936 >        CompletableFutureInc r;
1937 >
1938 >        f = new CompletableFuture<>();
1939 >        g = f.thenCompose(r = new CompletableFutureInc());
1940 >        f.complete(one);
1941 >        checkCompletedNormally(g, two);
1942 >        assertTrue(r.ran);
1943 >
1944 >        f = new CompletableFuture<>();
1945          f.complete(one);
1946 +        g = f.thenCompose(r = new CompletableFutureInc());
1947          checkCompletedNormally(g, two);
1948 +        assertTrue(r.ran);
1949      }
1950  
1951      /**
# Line 1137 | Line 1953 | public class CompletableFutureTest exten
1953       * completion of source
1954       */
1955      public void testThenCompose2() {
1956 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1957 <        CompletableFutureInc r = new CompletableFutureInc();
1958 <        CompletableFuture<Integer> g = f.thenCompose(r);
1956 >        CompletableFuture<Integer> f, g;
1957 >        CompletableFutureInc r;
1958 >
1959 >        f = new CompletableFuture<>();
1960 >        g = f.thenCompose(r = new CompletableFutureInc());
1961          f.completeExceptionally(new CFException());
1962          checkCompletedWithWrappedCFException(g);
1963 +
1964 +        f = new CompletableFuture<>();
1965 +        f.completeExceptionally(new CFException());
1966 +        g = f.thenCompose(r = new CompletableFutureInc());
1967 +        checkCompletedWithWrappedCFException(g);
1968      }
1969  
1970      /**
1971       * thenCompose result completes exceptionally if action does
1972       */
1973      public void testThenCompose3() {
1974 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1975 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1976 <        CompletableFuture<Integer> g = f.thenCompose(r);
1974 >        CompletableFuture<Integer> f, g;
1975 >        FailingCompletableFutureFunction r;
1976 >
1977 >        f = new CompletableFuture<>();
1978 >        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1979          f.complete(one);
1980          checkCompletedWithWrappedCFException(g);
1981 +
1982 +        f = new CompletableFuture<>();
1983 +        f.complete(one);
1984 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
1985 +        checkCompletedWithWrappedCFException(g);
1986      }
1987  
1988      /**
1989       * thenCompose result completes exceptionally if source cancelled
1990       */
1991      public void testThenCompose4() {
1992 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1993 <        CompletableFutureInc r = new CompletableFutureInc();
1994 <        CompletableFuture<Integer> g = f.thenCompose(r);
1992 >        CompletableFuture<Integer> f, g;
1993 >        CompletableFutureInc r;
1994 >
1995 >        f = new CompletableFuture<>();
1996 >        g = f.thenCompose(r = new CompletableFutureInc());
1997          assertTrue(f.cancel(true));
1998          checkCompletedWithWrappedCancellationException(g);
1167    }
1999  
2000 +        f = new CompletableFuture<>();
2001 +        assertTrue(f.cancel(true));
2002 +        g = f.thenCompose(r = new CompletableFutureInc());
2003 +        checkCompletedWithWrappedCancellationException(g);
2004 +    }
2005  
2006      // asyncs
2007  
# Line 1173 | Line 2009 | public class CompletableFutureTest exten
2009       * thenRunAsync result completes normally after normal completion of source
2010       */
2011      public void testThenRunAsync() {
2012 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2012 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2013          Noop r = new Noop();
2014          CompletableFuture<Void> g = f.thenRunAsync(r);
2015          f.complete(null);
2016          checkCompletedNormally(g, null);
2017  
2018          // reordered version
2019 <        f = new CompletableFuture<Integer>();
2019 >        f = new CompletableFuture<>();
2020          f.complete(null);
2021          r = new Noop();
2022          g = f.thenRunAsync(r);
# Line 1192 | Line 2028 | public class CompletableFutureTest exten
2028       * completion of source
2029       */
2030      public void testThenRunAsync2() {
2031 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2031 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2032          Noop r = new Noop();
2033          CompletableFuture<Void> g = f.thenRunAsync(r);
2034          f.completeExceptionally(new CFException());
2035          try {
2036              g.join();
2037              shouldThrow();
2038 <        } catch (Exception ok) {
1203 <        }
2038 >        } catch (CompletionException success) {}
2039          checkCompletedWithWrappedCFException(g);
2040      }
2041  
# Line 1208 | Line 2043 | public class CompletableFutureTest exten
2043       * thenRunAsync result completes exceptionally if action does
2044       */
2045      public void testThenRunAsync3() {
2046 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2046 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2047          FailingNoop r = new FailingNoop();
2048          CompletableFuture<Void> g = f.thenRunAsync(r);
2049          f.complete(null);
# Line 1219 | Line 2054 | public class CompletableFutureTest exten
2054       * thenRunAsync result completes exceptionally if source cancelled
2055       */
2056      public void testThenRunAsync4() {
2057 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2057 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2058          Noop r = new Noop();
2059          CompletableFuture<Void> g = f.thenRunAsync(r);
2060          assertTrue(f.cancel(true));
# Line 1230 | Line 2065 | public class CompletableFutureTest exten
2065       * thenApplyAsync result completes normally after normal completion of source
2066       */
2067      public void testThenApplyAsync() {
2068 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2068 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2069          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2070          f.complete(one);
2071          checkCompletedNormally(g, two);
# Line 1241 | Line 2076 | public class CompletableFutureTest exten
2076       * completion of source
2077       */
2078      public void testThenApplyAsync2() {
2079 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2079 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2080          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2081          f.completeExceptionally(new CFException());
2082          checkCompletedWithWrappedCFException(g);
# Line 1251 | Line 2086 | public class CompletableFutureTest exten
2086       * thenApplyAsync result completes exceptionally if action does
2087       */
2088      public void testThenApplyAsync3() {
2089 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2089 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2090          FailingFunction r = new FailingFunction();
2091          CompletableFuture<Integer> g = f.thenApplyAsync(r);
2092          f.complete(null);
# Line 1262 | Line 2097 | public class CompletableFutureTest exten
2097       * thenApplyAsync result completes exceptionally if source cancelled
2098       */
2099      public void testThenApplyAsync4() {
2100 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2100 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2101          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2102          assertTrue(f.cancel(true));
2103          checkCompletedWithWrappedCancellationException(g);
# Line 1273 | Line 2108 | public class CompletableFutureTest exten
2108       * completion of source
2109       */
2110      public void testThenAcceptAsync() {
2111 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2111 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2112          IncAction r = new IncAction();
2113          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2114          f.complete(one);
# Line 1286 | Line 2121 | public class CompletableFutureTest exten
2121       * completion of source
2122       */
2123      public void testThenAcceptAsync2() {
2124 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2124 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2125          IncAction r = new IncAction();
2126          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2127          f.completeExceptionally(new CFException());
# Line 1297 | Line 2132 | public class CompletableFutureTest exten
2132       * thenAcceptAsync result completes exceptionally if action does
2133       */
2134      public void testThenAcceptAsync3() {
2135 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2135 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2136          FailingConsumer r = new FailingConsumer();
2137          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2138          f.complete(null);
# Line 1308 | Line 2143 | public class CompletableFutureTest exten
2143       * thenAcceptAsync result completes exceptionally if source cancelled
2144       */
2145      public void testThenAcceptAsync4() {
2146 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2146 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2147          IncAction r = new IncAction();
2148          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2149          assertTrue(f.cancel(true));
# Line 1316 | Line 2151 | public class CompletableFutureTest exten
2151      }
2152  
2153      /**
1319     * thenCombineAsync result completes normally after normal
1320     * completion of sources
1321     */
1322    public void testThenCombineAsync() {
1323        CompletableFuture<Integer> f, g, h;
1324
1325        f = new CompletableFuture<Integer>();
1326        g = new CompletableFuture<Integer>();
1327        h = f.thenCombineAsync(g, subtract);
1328        f.complete(3);
1329        checkIncomplete(h);
1330        g.complete(1);
1331        checkCompletedNormally(h, 2);
1332
1333        f = new CompletableFuture<Integer>();
1334        g = new CompletableFuture<Integer>();
1335        h = f.thenCombineAsync(g, subtract);
1336        g.complete(1);
1337        checkIncomplete(h);
1338        f.complete(3);
1339        checkCompletedNormally(h, 2);
1340
1341        f = new CompletableFuture<Integer>();
1342        g = new CompletableFuture<Integer>();
1343        g.complete(1);
1344        f.complete(3);
1345        h = f.thenCombineAsync(g, subtract);
1346        checkCompletedNormally(h, 2);
1347    }
1348
1349    /**
1350     * thenCombineAsync result completes exceptionally after exceptional
1351     * completion of either source
1352     */
1353    public void testThenCombineAsync2() {
1354        CompletableFuture<Integer> f, g, h;
1355
1356        f = new CompletableFuture<Integer>();
1357        g = new CompletableFuture<Integer>();
1358        h = f.thenCombineAsync(g, subtract);
1359        f.completeExceptionally(new CFException());
1360        checkIncomplete(h);
1361        g.complete(1);
1362        checkCompletedWithWrappedCFException(h);
1363
1364        f = new CompletableFuture<Integer>();
1365        g = new CompletableFuture<Integer>();
1366        h = f.thenCombineAsync(g, subtract);
1367        g.completeExceptionally(new CFException());
1368        checkIncomplete(h);
1369        f.complete(3);
1370        checkCompletedWithWrappedCFException(h);
1371
1372        f = new CompletableFuture<Integer>();
1373        g = new CompletableFuture<Integer>();
1374        g.completeExceptionally(new CFException());
1375        f.complete(3);
1376        h = f.thenCombineAsync(g, subtract);
1377        checkCompletedWithWrappedCFException(h);
1378    }
1379
1380    /**
1381     * thenCombineAsync result completes exceptionally if action does
1382     */
1383    public void testThenCombineAsync3() {
1384        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1385        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1386        FailingBiFunction r = new FailingBiFunction();
1387        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1388        f.complete(one);
1389        checkIncomplete(g);
1390        assertFalse(r.ran);
1391        f2.complete(two);
1392        checkCompletedWithWrappedCFException(g);
1393        assertTrue(r.ran);
1394    }
1395
1396    /**
1397     * thenCombineAsync result completes exceptionally if either source cancelled
1398     */
1399    public void testThenCombineAsync4() {
1400        CompletableFuture<Integer> f, g, h;
1401
1402        f = new CompletableFuture<Integer>();
1403        g = new CompletableFuture<Integer>();
1404        h = f.thenCombineAsync(g, subtract);
1405        assertTrue(f.cancel(true));
1406        checkIncomplete(h);
1407        g.complete(1);
1408        checkCompletedWithWrappedCancellationException(h);
1409
1410        f = new CompletableFuture<Integer>();
1411        g = new CompletableFuture<Integer>();
1412        h = f.thenCombineAsync(g, subtract);
1413        assertTrue(g.cancel(true));
1414        checkIncomplete(h);
1415        f.complete(3);
1416        checkCompletedWithWrappedCancellationException(h);
1417
1418        f = new CompletableFuture<Integer>();
1419        g = new CompletableFuture<Integer>();
1420        g.complete(3);
1421        assertTrue(f.cancel(true));
1422        h = f.thenCombineAsync(g, subtract);
1423        checkCompletedWithWrappedCancellationException(h);
1424
1425        f = new CompletableFuture<Integer>();
1426        g = new CompletableFuture<Integer>();
1427        f.complete(3);
1428        assertTrue(g.cancel(true));
1429        h = f.thenCombineAsync(g, subtract);
1430        checkCompletedWithWrappedCancellationException(h);
1431    }
1432
1433    /**
1434     * thenAcceptBothAsync result completes normally after normal
1435     * completion of sources
1436     */
1437    public void testThenAcceptBothAsync() {
1438        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1439        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1440        AddAction r = new AddAction();
1441        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1442        f.complete(one);
1443        checkIncomplete(g);
1444        f2.complete(two);
1445        checkCompletedNormally(g, null);
1446        assertEquals(r.value, 3);
1447    }
1448
1449    /**
1450     * thenAcceptBothAsync result completes exceptionally after exceptional
1451     * completion of source
1452     */
1453    public void testThenAcceptBothAsync2() {
1454        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1455        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1456        AddAction r = new AddAction();
1457        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1458        f.completeExceptionally(new CFException());
1459        f2.complete(two);
1460        checkCompletedWithWrappedCFException(g);
1461
1462        r = new AddAction();
1463        f = new CompletableFuture<Integer>();
1464        f2 = new CompletableFuture<Integer>();
1465        g = f.thenAcceptBothAsync(f2, r);
1466        f.complete(one);
1467        f2.completeExceptionally(new CFException());
1468        checkCompletedWithWrappedCFException(g);
1469    }
1470
1471    /**
1472     * thenAcceptBothAsync result completes exceptionally if action does
1473     */
1474    public void testThenAcceptBothAsync3() {
1475        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1476        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1477        FailingBiConsumer r = new FailingBiConsumer();
1478        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1479        f.complete(one);
1480        checkIncomplete(g);
1481        f2.complete(two);
1482        checkCompletedWithWrappedCFException(g);
1483    }
1484
1485    /**
1486     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1487     */
1488    public void testThenAcceptBothAsync4() {
1489        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1490        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1491        AddAction r = new AddAction();
1492        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1493        assertTrue(f.cancel(true));
1494        f2.complete(two);
1495        checkCompletedWithWrappedCancellationException(g);
1496
1497        r = new AddAction();
1498        f = new CompletableFuture<Integer>();
1499        f2 = new CompletableFuture<Integer>();
1500        g = f.thenAcceptBothAsync(f2, r);
1501        f.complete(one);
1502        assertTrue(f2.cancel(true));
1503        checkCompletedWithWrappedCancellationException(g);
1504    }
1505
1506    /**
1507     * runAfterBothAsync result completes normally after normal
1508     * completion of sources
1509     */
1510    public void testRunAfterBothAsync() {
1511        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1512        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1513        Noop r = new Noop();
1514        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1515        f.complete(one);
1516        checkIncomplete(g);
1517        f2.complete(two);
1518        checkCompletedNormally(g, null);
1519        assertTrue(r.ran);
1520    }
1521
1522    /**
1523     * runAfterBothAsync result completes exceptionally after exceptional
1524     * completion of source
1525     */
1526    public void testRunAfterBothAsync2() {
1527        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1528        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1529        Noop r = new Noop();
1530        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1531        f.completeExceptionally(new CFException());
1532        f2.complete(two);
1533        checkCompletedWithWrappedCFException(g);
1534
1535        r = new Noop();
1536        f = new CompletableFuture<Integer>();
1537        f2 = new CompletableFuture<Integer>();
1538        g = f.runAfterBothAsync(f2, r);
1539        f.complete(one);
1540        f2.completeExceptionally(new CFException());
1541        checkCompletedWithWrappedCFException(g);
1542    }
1543
1544    /**
1545     * runAfterBothAsync result completes exceptionally if action does
1546     */
1547    public void testRunAfterBothAsync3() {
1548        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1549        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1550        FailingNoop r = new FailingNoop();
1551        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1552        f.complete(one);
1553        checkIncomplete(g);
1554        f2.complete(two);
1555        checkCompletedWithWrappedCFException(g);
1556    }
1557
1558    /**
1559     * runAfterBothAsync result completes exceptionally if either source cancelled
1560     */
1561    public void testRunAfterBothAsync4() {
1562        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1563        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1564        Noop r = new Noop();
1565        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1566        assertTrue(f.cancel(true));
1567        f2.complete(two);
1568        checkCompletedWithWrappedCancellationException(g);
1569
1570        r = new Noop();
1571        f = new CompletableFuture<Integer>();
1572        f2 = new CompletableFuture<Integer>();
1573        g = f.runAfterBothAsync(f2, r);
1574        f.complete(one);
1575        assertTrue(f2.cancel(true));
1576        checkCompletedWithWrappedCancellationException(g);
1577    }
1578
1579    /**
2154       * applyToEitherAsync result completes normally after normal
2155       * completion of sources
2156       */
2157      public void testApplyToEitherAsync() {
2158 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2159 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2158 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2159 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2160          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2161          f.complete(one);
2162          checkCompletedNormally(g, two);
2163  
2164 <        f = new CompletableFuture<Integer>();
2164 >        f = new CompletableFuture<>();
2165          f.complete(one);
2166 <        f2 = new CompletableFuture<Integer>();
2166 >        f2 = new CompletableFuture<>();
2167          g = f.applyToEitherAsync(f2, inc);
2168          checkCompletedNormally(g, two);
2169      }
# Line 1599 | Line 2173 | public class CompletableFutureTest exten
2173       * completion of source
2174       */
2175      public void testApplyToEitherAsync2() {
2176 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2177 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2176 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2177 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2178          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2179          f.completeExceptionally(new CFException());
2180          checkCompletedWithWrappedCFException(g);
2181  
2182 <        f = new CompletableFuture<Integer>();
2183 <        f2 = new CompletableFuture<Integer>();
2182 >        f = new CompletableFuture<>();
2183 >        f2 = new CompletableFuture<>();
2184          f2.completeExceptionally(new CFException());
2185          g = f.applyToEitherAsync(f2, inc);
2186          f.complete(one);
# Line 1617 | Line 2191 | public class CompletableFutureTest exten
2191       * applyToEitherAsync result completes exceptionally if action does
2192       */
2193      public void testApplyToEitherAsync3() {
2194 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2195 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2194 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2195 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2196          FailingFunction r = new FailingFunction();
2197          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2198          f.complete(one);
# Line 1629 | Line 2203 | public class CompletableFutureTest exten
2203       * applyToEitherAsync result completes exceptionally if either source cancelled
2204       */
2205      public void testApplyToEitherAsync4() {
2206 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2207 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2206 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2207 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2208          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2209          assertTrue(f.cancel(true));
2210          checkCompletedWithWrappedCancellationException(g);
2211  
2212 <        f = new CompletableFuture<Integer>();
2213 <        f2 = new CompletableFuture<Integer>();
2212 >        f = new CompletableFuture<>();
2213 >        f2 = new CompletableFuture<>();
2214          assertTrue(f2.cancel(true));
2215          g = f.applyToEitherAsync(f2, inc);
2216          checkCompletedWithWrappedCancellationException(g);
# Line 1647 | Line 2221 | public class CompletableFutureTest exten
2221       * completion of sources
2222       */
2223      public void testAcceptEitherAsync() {
2224 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2225 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2224 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2225 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226          IncAction r = new IncAction();
2227          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2228          f.complete(one);
# Line 1656 | Line 2230 | public class CompletableFutureTest exten
2230          assertEquals(r.value, 2);
2231  
2232          r = new IncAction();
2233 <        f = new CompletableFuture<Integer>();
2233 >        f = new CompletableFuture<>();
2234          f.complete(one);
2235 <        f2 = new CompletableFuture<Integer>();
2235 >        f2 = new CompletableFuture<>();
2236          g = f.acceptEitherAsync(f2, r);
2237          checkCompletedNormally(g, null);
2238          assertEquals(r.value, 2);
# Line 1669 | Line 2243 | public class CompletableFutureTest exten
2243       * completion of source
2244       */
2245      public void testAcceptEitherAsync2() {
2246 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2247 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2246 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2247 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2248          IncAction r = new IncAction();
2249          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2250          f.completeExceptionally(new CFException());
2251          checkCompletedWithWrappedCFException(g);
2252  
2253          r = new IncAction();
2254 <        f = new CompletableFuture<Integer>();
2255 <        f2 = new CompletableFuture<Integer>();
2254 >        f = new CompletableFuture<>();
2255 >        f2 = new CompletableFuture<>();
2256          f2.completeExceptionally(new CFException());
2257          g = f.acceptEitherAsync(f2, r);
2258          f.complete(one);
# Line 1689 | Line 2263 | public class CompletableFutureTest exten
2263       * acceptEitherAsync result completes exceptionally if action does
2264       */
2265      public void testAcceptEitherAsync3() {
2266 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2267 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2266 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2267 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2268          FailingConsumer r = new FailingConsumer();
2269          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2270          f.complete(one);
# Line 1702 | Line 2276 | public class CompletableFutureTest exten
2276       * source cancelled
2277       */
2278      public void testAcceptEitherAsync4() {
2279 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2280 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2279 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2280 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2281          IncAction r = new IncAction();
2282          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2283          assertTrue(f.cancel(true));
2284          checkCompletedWithWrappedCancellationException(g);
2285  
2286          r = new IncAction();
2287 <        f = new CompletableFuture<Integer>();
2288 <        f2 = new CompletableFuture<Integer>();
2287 >        f = new CompletableFuture<>();
2288 >        f2 = new CompletableFuture<>();
2289          assertTrue(f2.cancel(true));
2290          g = f.acceptEitherAsync(f2, r);
2291          checkCompletedWithWrappedCancellationException(g);
# Line 1722 | Line 2296 | public class CompletableFutureTest exten
2296       * completion of sources
2297       */
2298      public void testRunAfterEitherAsync() {
2299 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2300 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2299 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2300 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2301          Noop r = new Noop();
2302          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2303          f.complete(one);
# Line 1731 | Line 2305 | public class CompletableFutureTest exten
2305          assertTrue(r.ran);
2306  
2307          r = new Noop();
2308 <        f = new CompletableFuture<Integer>();
2308 >        f = new CompletableFuture<>();
2309          f.complete(one);
2310 <        f2 = new CompletableFuture<Integer>();
2310 >        f2 = new CompletableFuture<>();
2311          g = f.runAfterEitherAsync(f2, r);
2312          checkCompletedNormally(g, null);
2313          assertTrue(r.ran);
# Line 1744 | Line 2318 | public class CompletableFutureTest exten
2318       * completion of source
2319       */
2320      public void testRunAfterEitherAsync2() {
2321 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2322 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2321 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2322 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2323          Noop r = new Noop();
2324          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2325          f.completeExceptionally(new CFException());
2326          checkCompletedWithWrappedCFException(g);
2327  
2328          r = new Noop();
2329 <        f = new CompletableFuture<Integer>();
2330 <        f2 = new CompletableFuture<Integer>();
2329 >        f = new CompletableFuture<>();
2330 >        f2 = new CompletableFuture<>();
2331          f2.completeExceptionally(new CFException());
2332          g = f.runAfterEitherAsync(f2, r);
2333          f.complete(one);
# Line 1764 | Line 2338 | public class CompletableFutureTest exten
2338       * runAfterEitherAsync result completes exceptionally if action does
2339       */
2340      public void testRunAfterEitherAsync3() {
2341 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2342 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2341 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2342 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2343          FailingNoop r = new FailingNoop();
2344          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2345          f.complete(one);
# Line 1777 | Line 2351 | public class CompletableFutureTest exten
2351       * source cancelled
2352       */
2353      public void testRunAfterEitherAsync4() {
2354 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2355 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2354 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2355 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356          Noop r = new Noop();
2357          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2358          assertTrue(f.cancel(true));
2359          checkCompletedWithWrappedCancellationException(g);
2360  
2361          r = new Noop();
2362 <        f = new CompletableFuture<Integer>();
2363 <        f2 = new CompletableFuture<Integer>();
2362 >        f = new CompletableFuture<>();
2363 >        f2 = new CompletableFuture<>();
2364          assertTrue(f2.cancel(true));
2365          g = f.runAfterEitherAsync(f2, r);
2366          checkCompletedWithWrappedCancellationException(g);
# Line 1797 | Line 2371 | public class CompletableFutureTest exten
2371       * completion of source
2372       */
2373      public void testThenComposeAsync() {
2374 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2375 <        CompletableFutureInc r = new CompletableFutureInc();
2376 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2374 >        CompletableFuture<Integer> f, g;
2375 >        CompletableFutureInc r;
2376 >
2377 >        f = new CompletableFuture<>();
2378 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2379          f.complete(one);
2380          checkCompletedNormally(g, two);
2381 +
2382 +        f = new CompletableFuture<>();
2383 +        f.complete(one);
2384 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2385 +        checkCompletedNormally(g, two);
2386      }
2387  
2388      /**
# Line 1809 | Line 2390 | public class CompletableFutureTest exten
2390       * exceptional completion of source
2391       */
2392      public void testThenComposeAsync2() {
2393 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2394 <        CompletableFutureInc r = new CompletableFutureInc();
2395 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2393 >        CompletableFuture<Integer> f, g;
2394 >        CompletableFutureInc r;
2395 >
2396 >        f = new CompletableFuture<>();
2397 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2398 >        f.completeExceptionally(new CFException());
2399 >        checkCompletedWithWrappedCFException(g);
2400 >        assertFalse(r.ran);
2401 >
2402 >        f = new CompletableFuture<>();
2403          f.completeExceptionally(new CFException());
2404 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2405          checkCompletedWithWrappedCFException(g);
2406 +        assertFalse(r.ran);
2407      }
2408  
2409      /**
2410       * thenComposeAsync result completes exceptionally if action does
2411       */
2412      public void testThenComposeAsync3() {
2413 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2414 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2415 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2413 >        CompletableFuture<Integer> f, g;
2414 >        FailingCompletableFutureFunction r;
2415 >
2416 >        f = new CompletableFuture<>();
2417 >        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2418 >        f.complete(one);
2419 >        checkCompletedWithWrappedCFException(g);
2420 >
2421 >        f = new CompletableFuture<>();
2422          f.complete(one);
2423 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2424          checkCompletedWithWrappedCFException(g);
2425      }
2426  
# Line 1831 | Line 2428 | public class CompletableFutureTest exten
2428       * thenComposeAsync result completes exceptionally if source cancelled
2429       */
2430      public void testThenComposeAsync4() {
2431 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2432 <        CompletableFutureInc r = new CompletableFutureInc();
2433 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2431 >        CompletableFuture<Integer> f, g;
2432 >        CompletableFutureInc r;
2433 >
2434 >        f = new CompletableFuture<>();
2435 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2436          assertTrue(f.cancel(true));
2437          checkCompletedWithWrappedCancellationException(g);
1839    }
2438  
2439 +        f = new CompletableFuture<>();
2440 +        assertTrue(f.cancel(true));
2441 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2442 +        checkCompletedWithWrappedCancellationException(g);
2443 +    }
2444  
2445      // async with explicit executors
2446  
# Line 1845 | Line 2448 | public class CompletableFutureTest exten
2448       * thenRunAsync result completes normally after normal completion of source
2449       */
2450      public void testThenRunAsyncE() {
2451 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2451 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2452          Noop r = new Noop();
2453          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2454          f.complete(null);
2455          checkCompletedNormally(g, null);
2456  
2457          // reordered version
2458 <        f = new CompletableFuture<Integer>();
2458 >        f = new CompletableFuture<>();
2459          f.complete(null);
2460          r = new Noop();
2461          g = f.thenRunAsync(r, new ThreadExecutor());
# Line 1864 | Line 2467 | public class CompletableFutureTest exten
2467       * completion of source
2468       */
2469      public void testThenRunAsync2E() {
2470 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2470 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2471          Noop r = new Noop();
2472          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2473          f.completeExceptionally(new CFException());
2474          try {
2475              g.join();
2476              shouldThrow();
2477 <        } catch (Exception ok) {
1875 <        }
2477 >        } catch (CompletionException success) {}
2478          checkCompletedWithWrappedCFException(g);
2479      }
2480  
# Line 1880 | Line 2482 | public class CompletableFutureTest exten
2482       * thenRunAsync result completes exceptionally if action does
2483       */
2484      public void testThenRunAsync3E() {
2485 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2485 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2486          FailingNoop r = new FailingNoop();
2487          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2488          f.complete(null);
# Line 1891 | Line 2493 | public class CompletableFutureTest exten
2493       * thenRunAsync result completes exceptionally if source cancelled
2494       */
2495      public void testThenRunAsync4E() {
2496 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2496 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2497          Noop r = new Noop();
2498          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2499          assertTrue(f.cancel(true));
# Line 1902 | Line 2504 | public class CompletableFutureTest exten
2504       * thenApplyAsync result completes normally after normal completion of source
2505       */
2506      public void testThenApplyAsyncE() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2507 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2508          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2509          f.complete(one);
2510          checkCompletedNormally(g, two);
# Line 1913 | Line 2515 | public class CompletableFutureTest exten
2515       * completion of source
2516       */
2517      public void testThenApplyAsync2E() {
2518 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2518 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2519          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2520          f.completeExceptionally(new CFException());
2521          checkCompletedWithWrappedCFException(g);
# Line 1923 | Line 2525 | public class CompletableFutureTest exten
2525       * thenApplyAsync result completes exceptionally if action does
2526       */
2527      public void testThenApplyAsync3E() {
2528 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2528 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2529          FailingFunction r = new FailingFunction();
2530          CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2531          f.complete(null);
# Line 1934 | Line 2536 | public class CompletableFutureTest exten
2536       * thenApplyAsync result completes exceptionally if source cancelled
2537       */
2538      public void testThenApplyAsync4E() {
2539 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2539 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2540          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2541          assertTrue(f.cancel(true));
2542          checkCompletedWithWrappedCancellationException(g);
# Line 1945 | Line 2547 | public class CompletableFutureTest exten
2547       * completion of source
2548       */
2549      public void testThenAcceptAsyncE() {
2550 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2550 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2551          IncAction r = new IncAction();
2552          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2553          f.complete(one);
# Line 1958 | Line 2560 | public class CompletableFutureTest exten
2560       * completion of source
2561       */
2562      public void testThenAcceptAsync2E() {
2563 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2563 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2564          IncAction r = new IncAction();
2565          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2566          f.completeExceptionally(new CFException());
# Line 1969 | Line 2571 | public class CompletableFutureTest exten
2571       * thenAcceptAsync result completes exceptionally if action does
2572       */
2573      public void testThenAcceptAsync3E() {
2574 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2574 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2575          FailingConsumer r = new FailingConsumer();
2576          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2577          f.complete(null);
# Line 1980 | Line 2582 | public class CompletableFutureTest exten
2582       * thenAcceptAsync result completes exceptionally if source cancelled
2583       */
2584      public void testThenAcceptAsync4E() {
2585 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2585 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2586          IncAction r = new IncAction();
2587          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2588          assertTrue(f.cancel(true));
# Line 1988 | Line 2590 | public class CompletableFutureTest exten
2590      }
2591  
2592      /**
1991     * thenCombineAsync result completes normally after normal
1992     * completion of sources
1993     */
1994    public void testThenCombineAsyncE() {
1995        CompletableFuture<Integer> f, g, h;
1996        ThreadExecutor e = new ThreadExecutor();
1997        int count = 0;
1998
1999        f = new CompletableFuture<Integer>();
2000        g = new CompletableFuture<Integer>();
2001        h = f.thenCombineAsync(g, subtract, e);
2002        f.complete(3);
2003        checkIncomplete(h);
2004        g.complete(1);
2005        checkCompletedNormally(h, 2);
2006        assertEquals(++count, e.count.get());
2007
2008        f = new CompletableFuture<Integer>();
2009        g = new CompletableFuture<Integer>();
2010        h = f.thenCombineAsync(g, subtract, e);
2011        g.complete(1);
2012        checkIncomplete(h);
2013        f.complete(3);
2014        checkCompletedNormally(h, 2);
2015        assertEquals(++count, e.count.get());
2016
2017        f = new CompletableFuture<Integer>();
2018        g = new CompletableFuture<Integer>();
2019        g.complete(1);
2020        f.complete(3);
2021        h = f.thenCombineAsync(g, subtract, e);
2022        checkCompletedNormally(h, 2);
2023        assertEquals(++count, e.count.get());
2024    }
2025
2026    /**
2027     * thenCombineAsync result completes exceptionally after exceptional
2028     * completion of either source
2029     */
2030    public void testThenCombineAsync2E() {
2031        CompletableFuture<Integer> f, g, h;
2032        ThreadExecutor e = new ThreadExecutor();
2033        int count = 0;
2034
2035        f = new CompletableFuture<Integer>();
2036        g = new CompletableFuture<Integer>();
2037        h = f.thenCombineAsync(g, subtract, e);
2038        f.completeExceptionally(new CFException());
2039        checkIncomplete(h);
2040        g.complete(1);
2041        checkCompletedWithWrappedCFException(h);
2042
2043        f = new CompletableFuture<Integer>();
2044        g = new CompletableFuture<Integer>();
2045        h = f.thenCombineAsync(g, subtract, e);
2046        g.completeExceptionally(new CFException());
2047        checkIncomplete(h);
2048        f.complete(3);
2049        checkCompletedWithWrappedCFException(h);
2050
2051        f = new CompletableFuture<Integer>();
2052        g = new CompletableFuture<Integer>();
2053        g.completeExceptionally(new CFException());
2054        h = f.thenCombineAsync(g, subtract, e);
2055        checkIncomplete(h);
2056        f.complete(3);
2057        checkCompletedWithWrappedCFException(h);
2058
2059        assertEquals(0, e.count.get());
2060    }
2061
2062    /**
2063     * thenCombineAsync result completes exceptionally if action does
2064     */
2065    public void testThenCombineAsync3E() {
2066        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2067        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2068        FailingBiFunction r = new FailingBiFunction();
2069        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2070        f.complete(one);
2071        checkIncomplete(g);
2072        assertFalse(r.ran);
2073        f2.complete(two);
2074        checkCompletedWithWrappedCFException(g);
2075        assertTrue(r.ran);
2076    }
2077
2078    /**
2079     * thenCombineAsync result completes exceptionally if either source cancelled
2080     */
2081    public void testThenCombineAsync4E() {
2082        CompletableFuture<Integer> f, g, h;
2083        ThreadExecutor e = new ThreadExecutor();
2084
2085        f = new CompletableFuture<Integer>();
2086        g = new CompletableFuture<Integer>();
2087        h = f.thenCombineAsync(g, subtract, e);
2088        assertTrue(f.cancel(true));
2089        checkIncomplete(h);
2090        g.complete(1);
2091        checkCompletedWithWrappedCancellationException(h);
2092
2093        f = new CompletableFuture<Integer>();
2094        g = new CompletableFuture<Integer>();
2095        h = f.thenCombineAsync(g, subtract, e);
2096        assertTrue(g.cancel(true));
2097        checkIncomplete(h);
2098        f.complete(3);
2099        checkCompletedWithWrappedCancellationException(h);
2100
2101        f = new CompletableFuture<Integer>();
2102        g = new CompletableFuture<Integer>();
2103        assertTrue(g.cancel(true));
2104        h = f.thenCombineAsync(g, subtract, e);
2105        checkIncomplete(h);
2106        f.complete(3);
2107        checkCompletedWithWrappedCancellationException(h);
2108
2109        f = new CompletableFuture<Integer>();
2110        g = new CompletableFuture<Integer>();
2111        assertTrue(f.cancel(true));
2112        assertTrue(g.cancel(true));
2113        h = f.thenCombineAsync(g, subtract, e);
2114        checkCompletedWithWrappedCancellationException(h);
2115
2116        assertEquals(0, e.count.get());
2117    }
2118
2119    /**
2120     * thenAcceptBothAsync result completes normally after normal
2121     * completion of sources
2122     */
2123    public void testThenAcceptBothAsyncE() {
2124        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2125        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2126        AddAction r = new AddAction();
2127        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2128        f.complete(one);
2129        checkIncomplete(g);
2130        f2.complete(two);
2131        checkCompletedNormally(g, null);
2132        assertEquals(r.value, 3);
2133    }
2134
2135    /**
2136     * thenAcceptBothAsync result completes exceptionally after exceptional
2137     * completion of source
2138     */
2139    public void testThenAcceptBothAsync2E() {
2140        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2141        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2142        AddAction r = new AddAction();
2143        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2144        f.completeExceptionally(new CFException());
2145        f2.complete(two);
2146        checkCompletedWithWrappedCFException(g);
2147
2148        r = new AddAction();
2149        f = new CompletableFuture<Integer>();
2150        f2 = new CompletableFuture<Integer>();
2151        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2152        f.complete(one);
2153        f2.completeExceptionally(new CFException());
2154        checkCompletedWithWrappedCFException(g);
2155    }
2156
2157    /**
2158     * thenAcceptBothAsync result completes exceptionally if action does
2159     */
2160    public void testThenAcceptBothAsync3E() {
2161        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2162        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2163        FailingBiConsumer r = new FailingBiConsumer();
2164        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2165        f.complete(one);
2166        checkIncomplete(g);
2167        f2.complete(two);
2168        checkCompletedWithWrappedCFException(g);
2169    }
2170
2171    /**
2172     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2173     */
2174    public void testThenAcceptBothAsync4E() {
2175        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2176        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2177        AddAction r = new AddAction();
2178        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2179        assertTrue(f.cancel(true));
2180        f2.complete(two);
2181        checkCompletedWithWrappedCancellationException(g);
2182
2183        r = new AddAction();
2184        f = new CompletableFuture<Integer>();
2185        f2 = new CompletableFuture<Integer>();
2186        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2187        f.complete(one);
2188        assertTrue(f2.cancel(true));
2189        checkCompletedWithWrappedCancellationException(g);
2190    }
2191
2192    /**
2193     * runAfterBothAsync result completes normally after normal
2194     * completion of sources
2195     */
2196    public void testRunAfterBothAsyncE() {
2197        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2198        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2199        Noop r = new Noop();
2200        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2201        f.complete(one);
2202        checkIncomplete(g);
2203        f2.complete(two);
2204        checkCompletedNormally(g, null);
2205        assertTrue(r.ran);
2206    }
2207
2208    /**
2209     * runAfterBothAsync result completes exceptionally after exceptional
2210     * completion of source
2211     */
2212    public void testRunAfterBothAsync2E() {
2213        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2214        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2215        Noop r = new Noop();
2216        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2217        f.completeExceptionally(new CFException());
2218        f2.complete(two);
2219        checkCompletedWithWrappedCFException(g);
2220
2221        r = new Noop();
2222        f = new CompletableFuture<Integer>();
2223        f2 = new CompletableFuture<Integer>();
2224        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2225        f.complete(one);
2226        f2.completeExceptionally(new CFException());
2227        checkCompletedWithWrappedCFException(g);
2228    }
2229
2230    /**
2231     * runAfterBothAsync result completes exceptionally if action does
2232     */
2233    public void testRunAfterBothAsync3E() {
2234        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2235        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2236        FailingNoop r = new FailingNoop();
2237        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2238        f.complete(one);
2239        checkIncomplete(g);
2240        f2.complete(two);
2241        checkCompletedWithWrappedCFException(g);
2242    }
2243
2244    /**
2245     * runAfterBothAsync result completes exceptionally if either source cancelled
2246     */
2247    public void testRunAfterBothAsync4E() {
2248        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2249        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2250        Noop r = new Noop();
2251        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2252        assertTrue(f.cancel(true));
2253        f2.complete(two);
2254        checkCompletedWithWrappedCancellationException(g);
2255
2256        r = new Noop();
2257        f = new CompletableFuture<Integer>();
2258        f2 = new CompletableFuture<Integer>();
2259        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2260        f.complete(one);
2261        assertTrue(f2.cancel(true));
2262        checkCompletedWithWrappedCancellationException(g);
2263    }
2264
2265    /**
2593       * applyToEitherAsync result completes normally after normal
2594       * completion of sources
2595       */
2596      public void testApplyToEitherAsyncE() {
2597 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2598 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2597 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2598 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2599          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2600          f.complete(one);
2601          checkCompletedNormally(g, two);
2602  
2603 <        f = new CompletableFuture<Integer>();
2603 >        f = new CompletableFuture<>();
2604          f.complete(one);
2605 <        f2 = new CompletableFuture<Integer>();
2605 >        f2 = new CompletableFuture<>();
2606          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2607          checkCompletedNormally(g, two);
2608      }
# Line 2285 | Line 2612 | public class CompletableFutureTest exten
2612       * completion of source
2613       */
2614      public void testApplyToEitherAsync2E() {
2615 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2616 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2615 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2616 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2617          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2618          f.completeExceptionally(new CFException());
2619          checkCompletedWithWrappedCFException(g);
2620  
2621 <        f = new CompletableFuture<Integer>();
2622 <        f2 = new CompletableFuture<Integer>();
2621 >        f = new CompletableFuture<>();
2622 >        f2 = new CompletableFuture<>();
2623          f2.completeExceptionally(new CFException());
2624          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2625          f.complete(one);
# Line 2303 | Line 2630 | public class CompletableFutureTest exten
2630       * applyToEitherAsync result completes exceptionally if action does
2631       */
2632      public void testApplyToEitherAsync3E() {
2633 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2634 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2633 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2634 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635          FailingFunction r = new FailingFunction();
2636          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2637          f.complete(one);
# Line 2315 | Line 2642 | public class CompletableFutureTest exten
2642       * applyToEitherAsync result completes exceptionally if either source cancelled
2643       */
2644      public void testApplyToEitherAsync4E() {
2645 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2646 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2645 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2646 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2647          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2648          assertTrue(f.cancel(true));
2649          checkCompletedWithWrappedCancellationException(g);
2650  
2651 <        f = new CompletableFuture<Integer>();
2652 <        f2 = new CompletableFuture<Integer>();
2651 >        f = new CompletableFuture<>();
2652 >        f2 = new CompletableFuture<>();
2653          assertTrue(f2.cancel(true));
2654          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2655          checkCompletedWithWrappedCancellationException(g);
# Line 2333 | Line 2660 | public class CompletableFutureTest exten
2660       * completion of sources
2661       */
2662      public void testAcceptEitherAsyncE() {
2663 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2664 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2663 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2664 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2665          IncAction r = new IncAction();
2666          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2667          f.complete(one);
# Line 2342 | Line 2669 | public class CompletableFutureTest exten
2669          assertEquals(r.value, 2);
2670  
2671          r = new IncAction();
2672 <        f = new CompletableFuture<Integer>();
2672 >        f = new CompletableFuture<>();
2673          f.complete(one);
2674 <        f2 = new CompletableFuture<Integer>();
2674 >        f2 = new CompletableFuture<>();
2675          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2676          checkCompletedNormally(g, null);
2677          assertEquals(r.value, 2);
# Line 2355 | Line 2682 | public class CompletableFutureTest exten
2682       * completion of source
2683       */
2684      public void testAcceptEitherAsync2E() {
2685 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2686 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2685 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2686 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2687          IncAction r = new IncAction();
2688          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2689          f.completeExceptionally(new CFException());
2690          checkCompletedWithWrappedCFException(g);
2691  
2692          r = new IncAction();
2693 <        f = new CompletableFuture<Integer>();
2694 <        f2 = new CompletableFuture<Integer>();
2693 >        f = new CompletableFuture<>();
2694 >        f2 = new CompletableFuture<>();
2695          f2.completeExceptionally(new CFException());
2696          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2697          f.complete(one);
# Line 2375 | Line 2702 | public class CompletableFutureTest exten
2702       * acceptEitherAsync result completes exceptionally if action does
2703       */
2704      public void testAcceptEitherAsync3E() {
2705 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2706 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2705 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2706 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2707          FailingConsumer r = new FailingConsumer();
2708          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2709          f.complete(one);
# Line 2388 | Line 2715 | public class CompletableFutureTest exten
2715       * source cancelled
2716       */
2717      public void testAcceptEitherAsync4E() {
2718 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2719 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2718 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2719 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2720          IncAction r = new IncAction();
2721          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2722          assertTrue(f.cancel(true));
2723          checkCompletedWithWrappedCancellationException(g);
2724  
2725          r = new IncAction();
2726 <        f = new CompletableFuture<Integer>();
2727 <        f2 = new CompletableFuture<Integer>();
2726 >        f = new CompletableFuture<>();
2727 >        f2 = new CompletableFuture<>();
2728          assertTrue(f2.cancel(true));
2729          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2730          checkCompletedWithWrappedCancellationException(g);
# Line 2408 | Line 2735 | public class CompletableFutureTest exten
2735       * completion of sources
2736       */
2737      public void testRunAfterEitherAsyncE() {
2738 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2739 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2738 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2739 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2740          Noop r = new Noop();
2741          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2742          f.complete(one);
# Line 2417 | Line 2744 | public class CompletableFutureTest exten
2744          assertTrue(r.ran);
2745  
2746          r = new Noop();
2747 <        f = new CompletableFuture<Integer>();
2747 >        f = new CompletableFuture<>();
2748          f.complete(one);
2749 <        f2 = new CompletableFuture<Integer>();
2749 >        f2 = new CompletableFuture<>();
2750          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2751          checkCompletedNormally(g, null);
2752          assertTrue(r.ran);
# Line 2430 | Line 2757 | public class CompletableFutureTest exten
2757       * completion of source
2758       */
2759      public void testRunAfterEitherAsync2E() {
2760 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2761 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2760 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2761 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2762          Noop r = new Noop();
2763          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2764          f.completeExceptionally(new CFException());
2765          checkCompletedWithWrappedCFException(g);
2766  
2767          r = new Noop();
2768 <        f = new CompletableFuture<Integer>();
2769 <        f2 = new CompletableFuture<Integer>();
2768 >        f = new CompletableFuture<>();
2769 >        f2 = new CompletableFuture<>();
2770          f2.completeExceptionally(new CFException());
2771          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2772          f.complete(one);
# Line 2450 | Line 2777 | public class CompletableFutureTest exten
2777       * runAfterEitherAsync result completes exceptionally if action does
2778       */
2779      public void testRunAfterEitherAsync3E() {
2780 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2781 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2780 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2781 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2782          FailingNoop r = new FailingNoop();
2783          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2784          f.complete(one);
# Line 2463 | Line 2790 | public class CompletableFutureTest exten
2790       * source cancelled
2791       */
2792      public void testRunAfterEitherAsync4E() {
2793 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2794 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2793 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2794 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2795          Noop r = new Noop();
2796          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2797          assertTrue(f.cancel(true));
2798          checkCompletedWithWrappedCancellationException(g);
2799  
2800          r = new Noop();
2801 <        f = new CompletableFuture<Integer>();
2802 <        f2 = new CompletableFuture<Integer>();
2801 >        f = new CompletableFuture<>();
2802 >        f2 = new CompletableFuture<>();
2803          assertTrue(f2.cancel(true));
2804          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2805          checkCompletedWithWrappedCancellationException(g);
# Line 2483 | Line 2810 | public class CompletableFutureTest exten
2810       * completion of source
2811       */
2812      public void testThenComposeAsyncE() {
2813 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2813 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2814          CompletableFutureInc r = new CompletableFutureInc();
2815          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2816          f.complete(one);
# Line 2495 | Line 2822 | public class CompletableFutureTest exten
2822       * exceptional completion of source
2823       */
2824      public void testThenComposeAsync2E() {
2825 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2825 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2826          CompletableFutureInc r = new CompletableFutureInc();
2827          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2828          f.completeExceptionally(new CFException());
# Line 2506 | Line 2833 | public class CompletableFutureTest exten
2833       * thenComposeAsync result completes exceptionally if action does
2834       */
2835      public void testThenComposeAsync3E() {
2836 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2836 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2837          FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2838          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2839          f.complete(one);
# Line 2517 | Line 2844 | public class CompletableFutureTest exten
2844       * thenComposeAsync result completes exceptionally if source cancelled
2845       */
2846      public void testThenComposeAsync4E() {
2847 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2847 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2848          CompletableFutureInc r = new CompletableFutureInc();
2849          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2850          assertTrue(f.cancel(true));
# Line 2531 | Line 2858 | public class CompletableFutureTest exten
2858       * with the value null
2859       */
2860      public void testAllOf_empty() throws Exception {
2861 <        CompletableFuture<?> f = CompletableFuture.allOf();
2861 >        CompletableFuture<Void> f = CompletableFuture.allOf();
2862          checkCompletedNormally(f, null);
2863      }
2864  
2865      /**
2866 <     * allOf returns a future completed when all components complete
2866 >     * allOf returns a future completed normally with the value null
2867 >     * when all components complete normally
2868       */
2869 <    public void testAllOf() throws Exception {
2869 >    public void testAllOf_normal() throws Exception {
2870          for (int k = 1; k < 20; ++k) {
2871 <            CompletableFuture[] fs = new CompletableFuture[k];
2871 >            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2872              for (int i = 0; i < k; ++i)
2873 <                fs[i] = new CompletableFuture<Integer>();
2873 >                fs[i] = new CompletableFuture<>();
2874              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2875              for (int i = 0; i < k; ++i) {
2876                  checkIncomplete(f);
2877 +                checkIncomplete(CompletableFuture.allOf(fs));
2878                  fs[i].complete(one);
2879              }
2880              checkCompletedNormally(f, null);
2881 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2882          }
2883      }
2884  
# Line 2556 | Line 2886 | public class CompletableFutureTest exten
2886       * anyOf(no component futures) returns an incomplete future
2887       */
2888      public void testAnyOf_empty() throws Exception {
2889 <        CompletableFuture<?> f = CompletableFuture.anyOf();
2889 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
2890          checkIncomplete(f);
2891      }
2892  
2893      /**
2894 <     * anyOf returns a future completed when any components complete
2894 >     * anyOf returns a future completed normally with a value when
2895 >     * a component future does
2896       */
2897 <    public void testAnyOf() throws Exception {
2898 <        for (int k = 1; k < 20; ++k) {
2897 >    public void testAnyOf_normal() throws Exception {
2898 >        for (int k = 0; k < 10; ++k) {
2899              CompletableFuture[] fs = new CompletableFuture[k];
2900              for (int i = 0; i < k; ++i)
2901 <                fs[i] = new CompletableFuture<Integer>();
2901 >                fs[i] = new CompletableFuture<>();
2902              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2903              checkIncomplete(f);
2904              for (int i = 0; i < k; ++i) {
2905                  fs[i].complete(one);
2906                  checkCompletedNormally(f, one);
2907 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2908 +            }
2909 +        }
2910 +    }
2911 +
2912 +    /**
2913 +     * anyOf result completes exceptionally when any component does.
2914 +     */
2915 +    public void testAnyOf_exceptional() throws Exception {
2916 +        for (int k = 0; k < 10; ++k) {
2917 +            CompletableFuture[] fs = new CompletableFuture[k];
2918 +            for (int i = 0; i < k; ++i)
2919 +                fs[i] = new CompletableFuture<>();
2920 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2921 +            checkIncomplete(f);
2922 +            for (int i = 0; i < k; ++i) {
2923 +                fs[i].completeExceptionally(new CFException());
2924 +                checkCompletedWithWrappedCFException(f);
2925 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
2926              }
2927          }
2928      }
# Line 2581 | Line 2931 | public class CompletableFutureTest exten
2931       * Completion methods throw NullPointerException with null arguments
2932       */
2933      public void testNPE() {
2934 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2935 <        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2934 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2935 >        CompletableFuture<Integer> g = new CompletableFuture<>();
2936          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2937          CompletableFuture<?> h;
2938          ThreadExecutor exec = new ThreadExecutor();
2939  
2940          Runnable[] throwingActions = {
2941 <            () -> { CompletableFuture.supplyAsync(null); },
2942 <            () -> { CompletableFuture.supplyAsync(null, exec); },
2943 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2944 <
2945 <            () -> { CompletableFuture.runAsync(null); },
2946 <            () -> { CompletableFuture.runAsync(null, exec); },
2947 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
2948 <
2949 <            () -> { f.completeExceptionally(null); },
2950 <
2951 <            () -> { f.thenApply(null); },
2952 <            () -> { f.thenApplyAsync(null); },
2953 <            () -> { f.thenApplyAsync((x) -> x, null); },
2954 <            () -> { f.thenApplyAsync(null, exec); },
2955 <
2956 <            () -> { f.thenAccept(null); },
2957 <            () -> { f.thenAcceptAsync(null); },
2958 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2959 <            () -> { f.thenAcceptAsync(null, exec); },
2960 <
2961 <            () -> { f.thenRun(null); },
2962 <            () -> { f.thenRunAsync(null); },
2963 <            () -> { f.thenRunAsync(() -> { ; }, null); },
2964 <            () -> { f.thenRunAsync(null, exec); },
2965 <
2966 <            () -> { f.thenCombine(g, null); },
2967 <            () -> { f.thenCombineAsync(g, null); },
2968 <            () -> { f.thenCombineAsync(g, null, exec); },
2969 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2970 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2971 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2972 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2973 <
2974 <            () -> { f.thenAcceptBoth(g, null); },
2975 <            () -> { f.thenAcceptBothAsync(g, null); },
2976 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
2977 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2978 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2979 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2980 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2981 <
2982 <            () -> { f.runAfterBoth(g, null); },
2983 <            () -> { f.runAfterBothAsync(g, null); },
2984 <            () -> { f.runAfterBothAsync(g, null, exec); },
2985 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2986 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2987 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2988 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2989 <
2990 <            () -> { f.applyToEither(g, null); },
2991 <            () -> { f.applyToEitherAsync(g, null); },
2992 <            () -> { f.applyToEitherAsync(g, null, exec); },
2993 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
2994 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2995 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2996 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2997 <
2998 <            () -> { f.acceptEither(g, null); },
2999 <            () -> { f.acceptEitherAsync(g, null); },
3000 <            () -> { f.acceptEitherAsync(g, null, exec); },
3001 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3002 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3003 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3004 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3005 <
3006 <            () -> { f.runAfterEither(g, null); },
3007 <            () -> { f.runAfterEitherAsync(g, null); },
3008 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3009 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3010 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3011 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3012 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3013 <
3014 <            () -> { f.thenCompose(null); },
3015 <            () -> { f.thenComposeAsync(null); },
3016 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3017 <            () -> { f.thenComposeAsync(null, exec); },
3018 <
3019 <            () -> { f.exceptionally(null); },
3020 <
3021 <            () -> { f.handle(null); },
3022 <
3023 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3024 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3025 <            () -> { CompletableFuture.allOf(f, null); },
3026 <            () -> { CompletableFuture.allOf(null, f); },
3027 <
3028 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3029 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3030 <            () -> { CompletableFuture.anyOf(f, null); },
3031 <            () -> { CompletableFuture.anyOf(null, f); },
2941 >            () -> CompletableFuture.supplyAsync(null),
2942 >            () -> CompletableFuture.supplyAsync(null, exec),
2943 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
2944 >
2945 >            () -> CompletableFuture.runAsync(null),
2946 >            () -> CompletableFuture.runAsync(null, exec),
2947 >            () -> CompletableFuture.runAsync(() -> {}, null),
2948 >
2949 >            () -> f.completeExceptionally(null),
2950 >
2951 >            () -> f.thenApply(null),
2952 >            () -> f.thenApplyAsync(null),
2953 >            () -> f.thenApplyAsync((x) -> x, null),
2954 >            () -> f.thenApplyAsync(null, exec),
2955 >
2956 >            () -> f.thenAccept(null),
2957 >            () -> f.thenAcceptAsync(null),
2958 >            () -> f.thenAcceptAsync((x) -> {} , null),
2959 >            () -> f.thenAcceptAsync(null, exec),
2960 >
2961 >            () -> f.thenRun(null),
2962 >            () -> f.thenRunAsync(null),
2963 >            () -> f.thenRunAsync(() -> {} , null),
2964 >            () -> f.thenRunAsync(null, exec),
2965 >
2966 >            () -> f.thenCombine(g, null),
2967 >            () -> f.thenCombineAsync(g, null),
2968 >            () -> f.thenCombineAsync(g, null, exec),
2969 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
2970 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
2971 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
2972 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
2973 >
2974 >            () -> f.thenAcceptBoth(g, null),
2975 >            () -> f.thenAcceptBothAsync(g, null),
2976 >            () -> f.thenAcceptBothAsync(g, null, exec),
2977 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
2978 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
2979 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
2980 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
2981 >
2982 >            () -> f.runAfterBoth(g, null),
2983 >            () -> f.runAfterBothAsync(g, null),
2984 >            () -> f.runAfterBothAsync(g, null, exec),
2985 >            () -> f.runAfterBoth(nullFuture, () -> {}),
2986 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
2987 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
2988 >            () -> f.runAfterBothAsync(g, () -> {}, null),
2989 >
2990 >            () -> f.applyToEither(g, null),
2991 >            () -> f.applyToEitherAsync(g, null),
2992 >            () -> f.applyToEitherAsync(g, null, exec),
2993 >            () -> f.applyToEither(nullFuture, (x) -> x),
2994 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
2995 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
2996 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
2997 >
2998 >            () -> f.acceptEither(g, null),
2999 >            () -> f.acceptEitherAsync(g, null),
3000 >            () -> f.acceptEitherAsync(g, null, exec),
3001 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3002 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3003 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3004 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3005 >
3006 >            () -> f.runAfterEither(g, null),
3007 >            () -> f.runAfterEitherAsync(g, null),
3008 >            () -> f.runAfterEitherAsync(g, null, exec),
3009 >            () -> f.runAfterEither(nullFuture, () -> {}),
3010 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3011 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3012 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3013 >
3014 >            () -> f.thenCompose(null),
3015 >            () -> f.thenComposeAsync(null),
3016 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3017 >            () -> f.thenComposeAsync(null, exec),
3018 >
3019 >            () -> f.exceptionally(null),
3020 >
3021 >            () -> f.handle(null),
3022 >
3023 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3024 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3025 >            () -> CompletableFuture.allOf(f, null),
3026 >            () -> CompletableFuture.allOf(null, f),
3027 >
3028 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3029 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3030 >            () -> CompletableFuture.anyOf(f, null),
3031 >            () -> CompletableFuture.anyOf(null, f),
3032 >
3033 >            () -> f.obtrudeException(null),
3034          };
3035  
3036          assertThrows(NullPointerException.class, throwingActions);
3037          assertEquals(0, exec.count.get());
3038      }
3039  
3040 +    /**
3041 +     * toCompletableFuture returns this CompletableFuture.
3042 +     */
3043 +    public void testToCompletableFuture() {
3044 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3045 +        assertSame(f, f.toCompletableFuture());
3046 +    }
3047 +
3048 +    /**
3049 +     * whenComplete action executes on normal completion, propagating
3050 +     * source result.
3051 +     */
3052 +    public void testWhenComplete1() {
3053 +        final AtomicInteger a = new AtomicInteger();
3054 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3055 +        CompletableFuture<Integer> g =
3056 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3057 +        f.complete(three);
3058 +        checkCompletedNormally(f, three);
3059 +        checkCompletedNormally(g, three);
3060 +        assertEquals(a.get(), 1);
3061 +    }
3062 +
3063 +    /**
3064 +     * whenComplete action executes on exceptional completion, propagating
3065 +     * source result.
3066 +     */
3067 +    public void testWhenComplete2() {
3068 +        final AtomicInteger a = new AtomicInteger();
3069 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3070 +        CompletableFuture<Integer> g =
3071 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3072 +        f.completeExceptionally(new CFException());
3073 +        assertTrue(f.isCompletedExceptionally());
3074 +        assertTrue(g.isCompletedExceptionally());
3075 +        assertEquals(a.get(), 1);
3076 +    }
3077 +
3078 +    /**
3079 +     * If a whenComplete action throws an exception when triggered by
3080 +     * a normal completion, it completes exceptionally
3081 +     */
3082 +    public void testWhenComplete3() {
3083 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3084 +        CompletableFuture<Integer> g =
3085 +            f.whenComplete((Integer x, Throwable t) ->
3086 +                           { throw new CFException(); } );
3087 +        f.complete(three);
3088 +        checkCompletedNormally(f, three);
3089 +        assertTrue(g.isCompletedExceptionally());
3090 +        checkCompletedWithWrappedCFException(g);
3091 +    }
3092 +
3093 +    /**
3094 +     * whenCompleteAsync action executes on normal completion, propagating
3095 +     * source result.
3096 +     */
3097 +    public void testWhenCompleteAsync1() {
3098 +        final AtomicInteger a = new AtomicInteger();
3099 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3100 +        CompletableFuture<Integer> g =
3101 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3102 +        f.complete(three);
3103 +        checkCompletedNormally(f, three);
3104 +        checkCompletedNormally(g, three);
3105 +        assertEquals(a.get(), 1);
3106 +    }
3107 +
3108 +    /**
3109 +     * whenCompleteAsync action executes on exceptional completion, propagating
3110 +     * source result.
3111 +     */
3112 +    public void testWhenCompleteAsync2() {
3113 +        final AtomicInteger a = new AtomicInteger();
3114 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3115 +        CompletableFuture<Integer> g =
3116 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3117 +        f.completeExceptionally(new CFException());
3118 +        checkCompletedWithWrappedCFException(f);
3119 +        checkCompletedWithWrappedCFException(g);
3120 +    }
3121 +
3122 +    /**
3123 +     * If a whenCompleteAsync action throws an exception when
3124 +     * triggered by a normal completion, it completes exceptionally
3125 +     */
3126 +    public void testWhenCompleteAsync3() {
3127 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3128 +        CompletableFuture<Integer> g =
3129 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3130 +                           { throw new CFException(); } );
3131 +        f.complete(three);
3132 +        checkCompletedNormally(f, three);
3133 +        checkCompletedWithWrappedCFException(g);
3134 +    }
3135 +
3136 +    /**
3137 +     * whenCompleteAsync action executes on normal completion, propagating
3138 +     * source result.
3139 +     */
3140 +    public void testWhenCompleteAsync1e() {
3141 +        final AtomicInteger a = new AtomicInteger();
3142 +        ThreadExecutor exec = new ThreadExecutor();
3143 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3144 +        CompletableFuture<Integer> g =
3145 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3146 +                                exec);
3147 +        f.complete(three);
3148 +        checkCompletedNormally(f, three);
3149 +        checkCompletedNormally(g, three);
3150 +        assertEquals(a.get(), 1);
3151 +    }
3152 +
3153 +    /**
3154 +     * whenCompleteAsync action executes on exceptional completion, propagating
3155 +     * source result.
3156 +     */
3157 +    public void testWhenCompleteAsync2e() {
3158 +        final AtomicInteger a = new AtomicInteger();
3159 +        ThreadExecutor exec = new ThreadExecutor();
3160 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3161 +        CompletableFuture<Integer> g =
3162 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3163 +                                exec);
3164 +        f.completeExceptionally(new CFException());
3165 +        checkCompletedWithWrappedCFException(f);
3166 +        checkCompletedWithWrappedCFException(g);
3167 +    }
3168 +
3169 +    /**
3170 +     * If a whenCompleteAsync action throws an exception when triggered
3171 +     * by a normal completion, it completes exceptionally
3172 +     */
3173 +    public void testWhenCompleteAsync3e() {
3174 +        ThreadExecutor exec = new ThreadExecutor();
3175 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3176 +        CompletableFuture<Integer> g =
3177 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3178 +                                { throw new CFException(); },
3179 +                                exec);
3180 +        f.complete(three);
3181 +        checkCompletedNormally(f, three);
3182 +        checkCompletedWithWrappedCFException(g);
3183 +    }
3184 +
3185 +    /**
3186 +     * handleAsync action completes normally with function value on
3187 +     * either normal or exceptional completion of source
3188 +     */
3189 +    public void testHandleAsync() {
3190 +        CompletableFuture<Integer> f, g;
3191 +        IntegerHandler r;
3192 +
3193 +        f = new CompletableFuture<>();
3194 +        g = f.handleAsync(r = new IntegerHandler());
3195 +        assertFalse(r.ran);
3196 +        f.completeExceptionally(new CFException());
3197 +        checkCompletedWithWrappedCFException(f);
3198 +        checkCompletedNormally(g, three);
3199 +        assertTrue(r.ran);
3200 +
3201 +        f = new CompletableFuture<>();
3202 +        g = f.handleAsync(r = new IntegerHandler());
3203 +        assertFalse(r.ran);
3204 +        f.completeExceptionally(new CFException());
3205 +        checkCompletedWithWrappedCFException(f);
3206 +        checkCompletedNormally(g, three);
3207 +        assertTrue(r.ran);
3208 +
3209 +        f = new CompletableFuture<>();
3210 +        g = f.handleAsync(r = new IntegerHandler());
3211 +        assertFalse(r.ran);
3212 +        f.complete(one);
3213 +        checkCompletedNormally(f, one);
3214 +        checkCompletedNormally(g, two);
3215 +        assertTrue(r.ran);
3216 +
3217 +        f = new CompletableFuture<>();
3218 +        g = f.handleAsync(r = new IntegerHandler());
3219 +        assertFalse(r.ran);
3220 +        f.complete(one);
3221 +        checkCompletedNormally(f, one);
3222 +        checkCompletedNormally(g, two);
3223 +        assertTrue(r.ran);
3224 +    }
3225 +
3226 +    /**
3227 +     * handleAsync action with Executor completes normally with
3228 +     * function value on either normal or exceptional completion of
3229 +     * source
3230 +     */
3231 +    public void testHandleAsync2() {
3232 +        CompletableFuture<Integer> f, g;
3233 +        ThreadExecutor exec = new ThreadExecutor();
3234 +        IntegerHandler r;
3235 +
3236 +        f = new CompletableFuture<>();
3237 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3238 +        assertFalse(r.ran);
3239 +        f.completeExceptionally(new CFException());
3240 +        checkCompletedWithWrappedCFException(f);
3241 +        checkCompletedNormally(g, three);
3242 +        assertTrue(r.ran);
3243 +
3244 +        f = new CompletableFuture<>();
3245 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3246 +        assertFalse(r.ran);
3247 +        f.completeExceptionally(new CFException());
3248 +        checkCompletedWithWrappedCFException(f);
3249 +        checkCompletedNormally(g, three);
3250 +        assertTrue(r.ran);
3251 +
3252 +        f = new CompletableFuture<>();
3253 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3254 +        assertFalse(r.ran);
3255 +        f.complete(one);
3256 +        checkCompletedNormally(f, one);
3257 +        checkCompletedNormally(g, two);
3258 +        assertTrue(r.ran);
3259 +
3260 +        f = new CompletableFuture<>();
3261 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3262 +        assertFalse(r.ran);
3263 +        f.complete(one);
3264 +        checkCompletedNormally(f, one);
3265 +        checkCompletedNormally(g, two);
3266 +        assertTrue(r.ran);
3267 +    }
3268 +
3269   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines