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.38 by jsr166, Sun Jun 1 23:51:44 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 and produces null values as well.
322 +    static Integer subtract(Integer x, Integer y) {
323 +        return (x == null && y == null) ? null :
324 +            ((x == null) ? 42 : x.intValue())
325 +            - ((y == null) ? 99 : y.intValue());
326 +    }
327 +
328 +    // A function that handles and produces null values as well.
329 +    static Integer inc(Integer x) {
330 +        return (x == null) ? null : x + 1;
331 +    }
332 +
333      static final Supplier<Integer> supplyOne =
334          () -> Integer.valueOf(1);
335      static final Function<Integer, Integer> inc =
336          (Integer x) -> Integer.valueOf(x.intValue() + 1);
337      static final BiFunction<Integer, Integer, Integer> subtract =
338 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
338 >        (Integer x, Integer y) -> subtract(x, y);
339      static final class IncAction implements Consumer<Integer> {
340 <        int value;
341 <        public void accept(Integer x) { value = x.intValue() + 1; }
340 >        int invocationCount = 0;
341 >        Integer value;
342 >        public boolean ran() { return invocationCount == 1; }
343 >        public void accept(Integer x) {
344 >            invocationCount++;
345 >            value = inc(x);
346 >        }
347      }
348 <    static final class AddAction implements BiConsumer<Integer, Integer> {
349 <        int value;
348 >    static final class IncFunction implements Function<Integer,Integer> {
349 >        int invocationCount = 0;
350 >        Integer value;
351 >        public boolean ran() { return invocationCount == 1; }
352 >        public Integer apply(Integer x) {
353 >            invocationCount++;
354 >            return value = inc(x);
355 >        }
356 >    }
357 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
358 >        int invocationCount = 0;
359 >        Integer value;
360 >        // Check this action was invoked exactly once when result is computed.
361 >        public boolean ran() { return invocationCount == 1; }
362          public void accept(Integer x, Integer y) {
363 <            value = x.intValue() + y.intValue();
363 >            invocationCount++;
364 >            value = subtract(x, y);
365 >        }
366 >    }
367 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
368 >        int invocationCount = 0;
369 >        Integer value;
370 >        // Check this action was invoked exactly once when result is computed.
371 >        public boolean ran() { return invocationCount == 1; }
372 >        public Integer apply(Integer x, Integer y) {
373 >            invocationCount++;
374 >            return value = subtract(x, y);
375          }
376      }
377      static final class Noop implements Runnable {
# Line 332 | Line 406 | public class CompletableFutureTest exten
406  
407      static final class CompletableFutureInc
408          implements Function<Integer, CompletableFuture<Integer>> {
409 +        boolean ran;
410          public CompletableFuture<Integer> apply(Integer x) {
411 <            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
411 >            ran = true;
412 >            CompletableFuture<Integer> f = new CompletableFuture<>();
413              f.complete(Integer.valueOf(x.intValue() + 1));
414              return f;
415          }
# Line 369 | Line 445 | public class CompletableFutureTest exten
445          }
446      }
447  
448 +    /**
449 +     * Permits the testing of parallel code for the 3 different
450 +     * execution modes without repeating all the testing code.
451 +     */
452 +    enum ExecutionMode {
453 +        DEFAULT {
454 +            public <T,U> CompletableFuture<Void> runAfterBoth
455 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
456 +                return f.runAfterBoth(g, 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 f.thenAcceptBoth(g, a);
463 +            }
464 +            public <T,U,V> CompletableFuture<V> thenCombine
465 +                (CompletableFuture<T> f,
466 +                 CompletionStage<? extends U> g,
467 +                 BiFunction<? super T,? super U,? extends V> a) {
468 +                return f.thenCombine(g, a);
469 +            }
470 +            public <T,U> CompletableFuture<U> applyToEither
471 +                (CompletableFuture<T> f,
472 +                 CompletionStage<? extends T> g,
473 +                 Function<? super T,U> a) {
474 +                return f.applyToEither(g, a);
475 +            }
476 +            public <T> CompletableFuture<Void> acceptEither
477 +                (CompletableFuture<T> f,
478 +                 CompletionStage<? extends T> g,
479 +                 Consumer<? super T> a) {
480 +                return f.acceptEither(g, a);
481 +            }
482 +            public <T> CompletableFuture<Void> runAfterEither
483 +                (CompletableFuture<T> f,
484 +                 CompletionStage<?> g,
485 +                 java.lang.Runnable a) {
486 +                return f.runAfterEither(g, a);
487 +            }
488 +            public <T,U> CompletableFuture<U> thenCompose
489 +                (CompletableFuture<T> f,
490 +                 Function<? super T,? extends CompletionStage<U>> a) {
491 +                return f.thenCompose(a);
492 +            }
493 +            public <T> CompletableFuture<T> whenComplete
494 +                (CompletableFuture<T> f,
495 +                 BiConsumer<? super T,? super Throwable> a) {
496 +                return f.whenComplete(a);
497 +            }
498 +        },
499 +
500 + //             /** Experimental way to do more testing */
501 + //         REVERSE_DEFAULT {
502 + //             public <T,U> CompletableFuture<Void> runAfterBoth
503 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
504 + //                 return g.runAfterBoth(f, a);
505 + //             }
506 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
507 + //                 (CompletableFuture<T> f,
508 + //                  CompletionStage<? extends U> g,
509 + //                  BiConsumer<? super T,? super U> a) {
510 + //                 return DEFAULT.thenAcceptBoth(f, g, a);
511 + //             }
512 + //         },
513 +
514 +        DEFAULT_ASYNC {
515 +            public <T,U> CompletableFuture<Void> runAfterBoth
516 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
517 +                return f.runAfterBothAsync(g, a);
518 +            }
519 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
520 +                (CompletableFuture<T> f,
521 +                 CompletionStage<? extends U> g,
522 +                 BiConsumer<? super T,? super U> a) {
523 +                return f.thenAcceptBothAsync(g, a);
524 +            }
525 +            public <T,U,V> CompletableFuture<V> thenCombine
526 +                (CompletableFuture<T> f,
527 +                 CompletionStage<? extends U> g,
528 +                 BiFunction<? super T,? super U,? extends V> a) {
529 +                return f.thenCombineAsync(g, a);
530 +            }
531 +            public <T,U> CompletableFuture<U> applyToEither
532 +                (CompletableFuture<T> f,
533 +                 CompletionStage<? extends T> g,
534 +                 Function<? super T,U> a) {
535 +                return f.applyToEitherAsync(g, a);
536 +            }
537 +            public <T> CompletableFuture<Void> acceptEither
538 +                (CompletableFuture<T> f,
539 +                 CompletionStage<? extends T> g,
540 +                 Consumer<? super T> a) {
541 +                return f.acceptEitherAsync(g, a);
542 +            }
543 +            public <T> CompletableFuture<Void> runAfterEither
544 +                (CompletableFuture<T> f,
545 +                 CompletionStage<?> g,
546 +                 java.lang.Runnable a) {
547 +                return f.runAfterEitherAsync(g, a);
548 +            }
549 +            public <T,U> CompletableFuture<U> thenCompose
550 +                (CompletableFuture<T> f,
551 +                 Function<? super T,? extends CompletionStage<U>> a) {
552 +                return f.thenComposeAsync(a);
553 +            }
554 +            public <T> CompletableFuture<T> whenComplete
555 +                (CompletableFuture<T> f,
556 +                 BiConsumer<? super T,? super Throwable> a) {
557 +                return f.whenCompleteAsync(a);
558 +            }
559 +        },
560 +
561 + //         REVERSE_DEFAULT_ASYNC {
562 + //             public <T,U> CompletableFuture<Void> runAfterBoth
563 + //                 (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
564 + //                 return f.runAfterBothAsync(g, a);
565 + //             }
566 + //             public <T,U> CompletableFuture<Void> thenAcceptBoth
567 + //                 (CompletableFuture<T> f,
568 + //                  CompletionStage<? extends U> g,
569 + //                  BiConsumer<? super T,? super U> a) {
570 + //                 return DEFAULT_ASYNC.thenAcceptBoth(f, g, a);
571 + //             }
572 + //         },
573 +
574 +        EXECUTOR {
575 +            public <T,U> CompletableFuture<Void> runAfterBoth
576 +                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
577 +                return f.runAfterBothAsync(g, a, new ThreadExecutor());
578 +            }
579 +            public <T,U> CompletableFuture<Void> thenAcceptBoth
580 +                (CompletableFuture<T> f,
581 +                 CompletionStage<? extends U> g,
582 +                 BiConsumer<? super T,? super U> a) {
583 +                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
584 +            }
585 +            public <T,U,V> CompletableFuture<V> thenCombine
586 +                (CompletableFuture<T> f,
587 +                 CompletionStage<? extends U> g,
588 +                 BiFunction<? super T,? super U,? extends V> a) {
589 +                return f.thenCombineAsync(g, a, new ThreadExecutor());
590 +            }
591 +            public <T,U> CompletableFuture<U> applyToEither
592 +                (CompletableFuture<T> f,
593 +                 CompletionStage<? extends T> g,
594 +                 Function<? super T,U> a) {
595 +                return f.applyToEitherAsync(g, a, new ThreadExecutor());
596 +            }
597 +            public <T> CompletableFuture<Void> acceptEither
598 +                (CompletableFuture<T> f,
599 +                 CompletionStage<? extends T> g,
600 +                 Consumer<? super T> a) {
601 +                return f.acceptEitherAsync(g, a, new ThreadExecutor());
602 +            }
603 +            public <T> CompletableFuture<Void> runAfterEither
604 +                (CompletableFuture<T> f,
605 +                 CompletionStage<?> g,
606 +                 java.lang.Runnable a) {
607 +                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
608 +            }
609 +            public <T,U> CompletableFuture<U> thenCompose
610 +                (CompletableFuture<T> f,
611 +                 Function<? super T,? extends CompletionStage<U>> a) {
612 +                return f.thenComposeAsync(a, new ThreadExecutor());
613 +            }
614 +            public <T> CompletableFuture<T> whenComplete
615 +                (CompletableFuture<T> f,
616 +                 BiConsumer<? super T,? super Throwable> a) {
617 +                return f.whenCompleteAsync(a, new ThreadExecutor());
618 +            }
619 +        };
620 +
621 +        public abstract <T,U> CompletableFuture<Void> runAfterBoth
622 +            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
623 +        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
624 +            (CompletableFuture<T> f,
625 +             CompletionStage<? extends U> g,
626 +             BiConsumer<? super T,? super U> a);
627 +        public abstract <T,U,V> CompletableFuture<V> thenCombine
628 +            (CompletableFuture<T> f,
629 +             CompletionStage<? extends U> g,
630 +             BiFunction<? super T,? super U,? extends V> a);
631 +        public abstract <T,U> CompletableFuture<U> applyToEither
632 +            (CompletableFuture<T> f,
633 +             CompletionStage<? extends T> g,
634 +             Function<? super T,U> a);
635 +        public abstract <T> CompletableFuture<Void> acceptEither
636 +            (CompletableFuture<T> f,
637 +             CompletionStage<? extends T> g,
638 +             Consumer<? super T> a);
639 +        public abstract <T> CompletableFuture<Void> runAfterEither
640 +            (CompletableFuture<T> f,
641 +             CompletionStage<?> g,
642 +             java.lang.Runnable a);
643 +        public abstract <T,U> CompletableFuture<U> thenCompose
644 +            (CompletableFuture<T> f,
645 +             Function<? super T,? extends CompletionStage<U>> a);
646 +        public abstract <T> CompletableFuture<T> whenComplete
647 +            (CompletableFuture<T> f,
648 +             BiConsumer<? super T,? super Throwable> a);
649 +
650 +
651 +    }
652  
653      /**
654       * exceptionally action completes with function value on source
655 <     * exception;  otherwise with source value
655 >     * exception; otherwise with source value
656       */
657      public void testExceptionally() {
658 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
658 >        CompletableFuture<Integer> f = new CompletableFuture<>();
659          ExceptionToInteger r = new ExceptionToInteger();
660          CompletableFuture<Integer> g = f.exceptionally(r);
661          f.completeExceptionally(new CFException());
662          checkCompletedNormally(g, three);
663  
664 <        f = new CompletableFuture<Integer>();
664 >        f = new CompletableFuture<>();
665          r = new ExceptionToInteger();
666          g = f.exceptionally(r);
667          f.complete(one);
# Line 396 | Line 676 | public class CompletableFutureTest exten
676          CompletableFuture<Integer> f, g;
677          IntegerHandler r;
678  
679 <        f = new CompletableFuture<Integer>();
679 >        f = new CompletableFuture<>();
680          f.completeExceptionally(new CFException());
681          g = f.handle(r = new IntegerHandler());
682          assertTrue(r.ran);
683          checkCompletedNormally(g, three);
684  
685 <        f = new CompletableFuture<Integer>();
685 >        f = new CompletableFuture<>();
686          g = f.handle(r = new IntegerHandler());
687          assertFalse(r.ran);
688          f.completeExceptionally(new CFException());
689          checkCompletedNormally(g, three);
690          assertTrue(r.ran);
691  
692 <        f = new CompletableFuture<Integer>();
692 >        f = new CompletableFuture<>();
693          f.complete(one);
694          g = f.handle(r = new IntegerHandler());
695          assertTrue(r.ran);
696          checkCompletedNormally(g, two);
697  
698 <        f = new CompletableFuture<Integer>();
698 >        f = new CompletableFuture<>();
699          g = f.handle(r = new IntegerHandler());
700          assertFalse(r.ran);
701          f.complete(one);
# Line 493 | Line 773 | public class CompletableFutureTest exten
773       * thenRun result completes normally after normal completion of source
774       */
775      public void testThenRun() {
776 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
777 <        Noop r = new Noop();
778 <        CompletableFuture<Void> g = f.thenRun(r);
776 >        CompletableFuture<Integer> f;
777 >        CompletableFuture<Void> g;
778 >        Noop r;
779 >
780 >        f = new CompletableFuture<>();
781 >        g = f.thenRun(r = new Noop());
782          f.complete(null);
783          checkCompletedNormally(g, null);
784 <        // reordered version
785 <        f = new CompletableFuture<Integer>();
784 >        assertTrue(r.ran);
785 >
786 >        f = new CompletableFuture<>();
787          f.complete(null);
788 <        r = new Noop();
505 <        g = f.thenRun(r);
788 >        g = f.thenRun(r = new Noop());
789          checkCompletedNormally(g, null);
790 +        assertTrue(r.ran);
791      }
792  
793      /**
# Line 511 | Line 795 | public class CompletableFutureTest exten
795       * completion of source
796       */
797      public void testThenRun2() {
798 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
799 <        Noop r = new Noop();
800 <        CompletableFuture<Void> g = f.thenRun(r);
798 >        CompletableFuture<Integer> f;
799 >        CompletableFuture<Void> g;
800 >        Noop r;
801 >
802 >        f = new CompletableFuture<>();
803 >        g = f.thenRun(r = new Noop());
804          f.completeExceptionally(new CFException());
805          checkCompletedWithWrappedCFException(g);
806 +        assertFalse(r.ran);
807 +
808 +        f = new CompletableFuture<>();
809 +        f.completeExceptionally(new CFException());
810 +        g = f.thenRun(r = new Noop());
811 +        checkCompletedWithWrappedCFException(g);
812 +        assertFalse(r.ran);
813      }
814  
815      /**
816       * thenRun result completes exceptionally if action does
817       */
818      public void testThenRun3() {
819 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
820 <        FailingNoop r = new FailingNoop();
821 <        CompletableFuture<Void> g = f.thenRun(r);
819 >        CompletableFuture<Integer> f;
820 >        CompletableFuture<Void> g;
821 >        FailingNoop r;
822 >
823 >        f = new CompletableFuture<>();
824 >        g = f.thenRun(r = new FailingNoop());
825          f.complete(null);
826          checkCompletedWithWrappedCFException(g);
827 +
828 +        f = new CompletableFuture<>();
829 +        f.complete(null);
830 +        g = f.thenRun(r = new FailingNoop());
831 +        checkCompletedWithWrappedCFException(g);
832      }
833  
834      /**
835       * thenRun result completes exceptionally if source cancelled
836       */
837      public void testThenRun4() {
838 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
839 <        Noop r = new Noop();
840 <        CompletableFuture<Void> g = f.thenRun(r);
838 >        CompletableFuture<Integer> f;
839 >        CompletableFuture<Void> g;
840 >        Noop r;
841 >
842 >        f = new CompletableFuture<>();
843 >        g = f.thenRun(r = new Noop());
844 >        assertTrue(f.cancel(true));
845 >        checkCompletedWithWrappedCancellationException(g);
846 >
847 >        f = new CompletableFuture<>();
848          assertTrue(f.cancel(true));
849 +        g = f.thenRun(r = new Noop());
850          checkCompletedWithWrappedCancellationException(g);
851      }
852  
# Line 544 | Line 854 | public class CompletableFutureTest exten
854       * thenApply result completes normally after normal completion of source
855       */
856      public void testThenApply() {
857 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
857 >        CompletableFuture<Integer> f = new CompletableFuture<>();
858          CompletableFuture<Integer> g = f.thenApply(inc);
859          f.complete(one);
860          checkCompletedNormally(g, two);
# Line 555 | Line 865 | public class CompletableFutureTest exten
865       * completion of source
866       */
867      public void testThenApply2() {
868 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
868 >        CompletableFuture<Integer> f = new CompletableFuture<>();
869          CompletableFuture<Integer> g = f.thenApply(inc);
870          f.completeExceptionally(new CFException());
871          checkCompletedWithWrappedCFException(g);
# Line 565 | Line 875 | public class CompletableFutureTest exten
875       * thenApply result completes exceptionally if action does
876       */
877      public void testThenApply3() {
878 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
878 >        CompletableFuture<Integer> f = new CompletableFuture<>();
879          CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
880          f.complete(one);
881          checkCompletedWithWrappedCFException(g);
# Line 575 | Line 885 | public class CompletableFutureTest exten
885       * thenApply result completes exceptionally if source cancelled
886       */
887      public void testThenApply4() {
888 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
888 >        CompletableFuture<Integer> f = new CompletableFuture<>();
889          CompletableFuture<Integer> g = f.thenApply(inc);
890          assertTrue(f.cancel(true));
891          checkCompletedWithWrappedCancellationException(g);
# Line 585 | Line 895 | public class CompletableFutureTest exten
895       * thenAccept result completes normally after normal completion of source
896       */
897      public void testThenAccept() {
898 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
898 >        CompletableFuture<Integer> f = new CompletableFuture<>();
899          IncAction r = new IncAction();
900          CompletableFuture<Void> g = f.thenAccept(r);
901          f.complete(one);
902          checkCompletedNormally(g, null);
903 <        assertEquals(r.value, 2);
903 >        assertEquals(r.value, (Integer) 2);
904      }
905  
906      /**
# Line 598 | Line 908 | public class CompletableFutureTest exten
908       * completion of source
909       */
910      public void testThenAccept2() {
911 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
911 >        CompletableFuture<Integer> f = new CompletableFuture<>();
912          IncAction r = new IncAction();
913          CompletableFuture<Void> g = f.thenAccept(r);
914          f.completeExceptionally(new CFException());
# Line 609 | Line 919 | public class CompletableFutureTest exten
919       * thenAccept result completes exceptionally if action does
920       */
921      public void testThenAccept3() {
922 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
922 >        CompletableFuture<Integer> f = new CompletableFuture<>();
923          FailingConsumer r = new FailingConsumer();
924          CompletableFuture<Void> g = f.thenAccept(r);
925          f.complete(one);
# Line 621 | Line 931 | public class CompletableFutureTest exten
931       * thenAccept result completes exceptionally if source cancelled
932       */
933      public void testThenAccept4() {
934 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
934 >        CompletableFuture<Integer> f = new CompletableFuture<>();
935          IncAction r = new IncAction();
936          CompletableFuture<Void> g = f.thenAccept(r);
937          assertTrue(f.cancel(true));
938          checkCompletedWithWrappedCancellationException(g);
939      }
940  
631
941      /**
942       * thenCombine result completes normally after normal completion
943       * of sources
944       */
945 <    public void testThenCombine() {
946 <        CompletableFuture<Integer> f, g, h;
945 >    public void testThenCombine_normalCompletion1() {
946 >        for (ExecutionMode m : ExecutionMode.values())
947 >        for (Integer v1 : new Integer[] { 1, null })
948 >        for (Integer v2 : new Integer[] { 2, null }) {
949 >
950 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
951 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
952 >        final SubtractFunction r = new SubtractFunction();
953 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
954 >
955 >        f.complete(v1);
956 >        checkIncomplete(h);
957 >        assertFalse(r.ran());
958 >        g.complete(v2);
959  
960 <        f = new CompletableFuture<Integer>();
961 <        g = new CompletableFuture<Integer>();
962 <        h = f.thenCombine(g, subtract);
963 <        f.complete(3);
964 <        checkIncomplete(h);
965 <        g.complete(1);
966 <        checkCompletedNormally(h, 2);
967 <
968 <        f = new CompletableFuture<Integer>();
969 <        g = new CompletableFuture<Integer>();
970 <        h = f.thenCombine(g, subtract);
971 <        g.complete(1);
972 <        checkIncomplete(h);
973 <        f.complete(3);
974 <        checkCompletedNormally(h, 2);
975 <
976 <        f = new CompletableFuture<Integer>();
977 <        g = new CompletableFuture<Integer>();
978 <        g.complete(1);
979 <        f.complete(3);
980 <        h = f.thenCombine(g, subtract);
981 <        checkCompletedNormally(h, 2);
960 >        checkCompletedNormally(h, subtract(v1, v2));
961 >        checkCompletedNormally(f, v1);
962 >        checkCompletedNormally(g, v2);
963 >        }
964 >    }
965 >
966 >    public void testThenCombine_normalCompletion2() {
967 >        for (ExecutionMode m : ExecutionMode.values())
968 >        for (Integer v1 : new Integer[] { 1, null })
969 >        for (Integer v2 : new Integer[] { 2, null }) {
970 >
971 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
973 >        final SubtractFunction r = new SubtractFunction();
974 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
975 >
976 >        g.complete(v2);
977 >        checkIncomplete(h);
978 >        assertFalse(r.ran());
979 >        f.complete(v1);
980 >
981 >        checkCompletedNormally(h, subtract(v1, v2));
982 >        checkCompletedNormally(f, v1);
983 >        checkCompletedNormally(g, v2);
984 >        }
985 >    }
986 >
987 >    public void testThenCombine_normalCompletion3() {
988 >        for (ExecutionMode m : ExecutionMode.values())
989 >        for (Integer v1 : new Integer[] { 1, null })
990 >        for (Integer v2 : new Integer[] { 2, null }) {
991 >
992 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
993 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
994 >        final SubtractFunction r = new SubtractFunction();
995 >
996 >        g.complete(v2);
997 >        f.complete(v1);
998 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
999 >
1000 >        checkCompletedNormally(h, subtract(v1, v2));
1001 >        checkCompletedNormally(f, v1);
1002 >        checkCompletedNormally(g, v2);
1003 >        }
1004 >    }
1005 >
1006 >    public void testThenCombine_normalCompletion4() {
1007 >        for (ExecutionMode m : ExecutionMode.values())
1008 >        for (Integer v1 : new Integer[] { 1, null })
1009 >        for (Integer v2 : new Integer[] { 2, null }) {
1010 >
1011 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1012 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1013 >        final SubtractFunction r = new SubtractFunction();
1014 >
1015 >        f.complete(v1);
1016 >        g.complete(v2);
1017 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1018 >
1019 >        checkCompletedNormally(h, subtract(v1, v2));
1020 >        checkCompletedNormally(f, v1);
1021 >        checkCompletedNormally(g, v2);
1022 >        }
1023      }
1024  
1025      /**
1026       * thenCombine result completes exceptionally after exceptional
1027       * completion of either source
1028       */
1029 <    public void testThenCombine2() {
1030 <        CompletableFuture<Integer> f, g, h;
1029 >    public void testThenCombine_exceptionalCompletion1() {
1030 >        for (ExecutionMode m : ExecutionMode.values())
1031 >        for (Integer v1 : new Integer[] { 1, null }) {
1032 >
1033 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1034 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1035 >        final SubtractFunction r = new SubtractFunction();
1036 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1037 >        final CFException ex = new CFException();
1038  
1039 <        f = new CompletableFuture<Integer>();
671 <        g = new CompletableFuture<Integer>();
672 <        h = f.thenCombine(g, subtract);
673 <        f.completeExceptionally(new CFException());
1039 >        f.completeExceptionally(ex);
1040          checkIncomplete(h);
1041 <        g.complete(1);
1042 <        checkCompletedWithWrappedCFException(h);
1041 >        g.complete(v1);
1042 >
1043 >        checkCompletedWithWrappedCFException(h, ex);
1044 >        checkCompletedWithWrappedCFException(f, ex);
1045 >        assertFalse(r.ran());
1046 >        checkCompletedNormally(g, v1);
1047 >        }
1048 >    }
1049 >
1050 >    public void testThenCombine_exceptionalCompletion2() {
1051 >        for (ExecutionMode m : ExecutionMode.values())
1052 >        for (Integer v1 : new Integer[] { 1, null }) {
1053 >
1054 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1055 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1056 >        final SubtractFunction r = new SubtractFunction();
1057 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1058 >        final CFException ex = new CFException();
1059  
1060 <        f = new CompletableFuture<Integer>();
679 <        g = new CompletableFuture<Integer>();
680 <        h = f.thenCombine(g, subtract);
681 <        g.completeExceptionally(new CFException());
1060 >        g.completeExceptionally(ex);
1061          checkIncomplete(h);
1062 <        f.complete(3);
684 <        checkCompletedWithWrappedCFException(h);
1062 >        f.complete(v1);
1063  
1064 <        f = new CompletableFuture<Integer>();
1065 <        g = new CompletableFuture<Integer>();
1066 <        f.complete(3);
1067 <        g.completeExceptionally(new CFException());
1068 <        h = f.thenCombine(g, subtract);
1069 <        checkCompletedWithWrappedCFException(h);
1064 >        checkCompletedWithWrappedCFException(h, ex);
1065 >        checkCompletedWithWrappedCFException(g, ex);
1066 >        assertFalse(r.ran());
1067 >        checkCompletedNormally(f, v1);
1068 >        }
1069 >    }
1070  
1071 <        f = new CompletableFuture<Integer>();
1072 <        g = new CompletableFuture<Integer>();
1073 <        f.completeExceptionally(new CFException());
1074 <        g.complete(3);
1075 <        h = f.thenCombine(g, subtract);
1076 <        checkCompletedWithWrappedCFException(h);
1071 >    public void testThenCombine_exceptionalCompletion3() {
1072 >        for (ExecutionMode m : ExecutionMode.values())
1073 >        for (Integer v1 : new Integer[] { 1, null }) {
1074 >
1075 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1076 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1077 >        final SubtractFunction r = new SubtractFunction();
1078 >        final CFException ex = new CFException();
1079 >
1080 >        g.completeExceptionally(ex);
1081 >        f.complete(v1);
1082 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1083 >
1084 >        checkCompletedWithWrappedCFException(h, ex);
1085 >        checkCompletedWithWrappedCFException(g, ex);
1086 >        assertFalse(r.ran());
1087 >        checkCompletedNormally(f, v1);
1088 >        }
1089 >    }
1090 >
1091 >    public void testThenCombine_exceptionalCompletion4() {
1092 >        for (ExecutionMode m : ExecutionMode.values())
1093 >        for (Integer v1 : new Integer[] { 1, null }) {
1094 >
1095 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1096 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1097 >        final SubtractFunction r = new SubtractFunction();
1098 >        final CFException ex = new CFException();
1099 >
1100 >        f.completeExceptionally(ex);
1101 >        g.complete(v1);
1102 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1103 >
1104 >        checkCompletedWithWrappedCFException(h, ex);
1105 >        checkCompletedWithWrappedCFException(f, ex);
1106 >        assertFalse(r.ran());
1107 >        checkCompletedNormally(g, v1);
1108 >        }
1109      }
1110  
1111      /**
1112       * thenCombine result completes exceptionally if action does
1113       */
1114 <    public void testThenCombine3() {
1115 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1116 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1117 <        FailingBiFunction r = new FailingBiFunction();
1118 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1119 <        f.complete(one);
1120 <        checkIncomplete(g);
1121 <        assertFalse(r.ran);
1122 <        f2.complete(two);
1123 <        checkCompletedWithWrappedCFException(g);
1124 <        assertTrue(r.ran);
1114 >    public void testThenCombine_actionFailed1() {
1115 >        for (ExecutionMode m : ExecutionMode.values())
1116 >        for (Integer v1 : new Integer[] { 1, null })
1117 >        for (Integer v2 : new Integer[] { 2, null }) {
1118 >
1119 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1120 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1121 >        final FailingBiFunction r = new FailingBiFunction();
1122 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1123 >
1124 >        f.complete(v1);
1125 >        checkIncomplete(h);
1126 >        g.complete(v2);
1127 >
1128 >        checkCompletedWithWrappedCFException(h);
1129 >        checkCompletedNormally(f, v1);
1130 >        checkCompletedNormally(g, v2);
1131 >        }
1132 >    }
1133 >
1134 >    public void testThenCombine_actionFailed2() {
1135 >        for (ExecutionMode m : ExecutionMode.values())
1136 >        for (Integer v1 : new Integer[] { 1, null })
1137 >        for (Integer v2 : new Integer[] { 2, null }) {
1138 >
1139 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1140 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1141 >        final FailingBiFunction r = new FailingBiFunction();
1142 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1143 >
1144 >        g.complete(v2);
1145 >        checkIncomplete(h);
1146 >        f.complete(v1);
1147 >
1148 >        checkCompletedWithWrappedCFException(h);
1149 >        checkCompletedNormally(f, v1);
1150 >        checkCompletedNormally(g, v2);
1151 >        }
1152      }
1153  
1154      /**
1155       * thenCombine result completes exceptionally if either source cancelled
1156       */
1157 <    public void testThenCombine4() {
1158 <        CompletableFuture<Integer> f, g, h;
1157 >    public void testThenCombine_sourceCancelled1() {
1158 >        for (ExecutionMode m : ExecutionMode.values())
1159 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1160 >        for (Integer v1 : new Integer[] { 1, null }) {
1161 >
1162 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1163 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1164 >        final SubtractFunction r = new SubtractFunction();
1165 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1166  
1167 <        f = new CompletableFuture<Integer>();
724 <        g = new CompletableFuture<Integer>();
725 <        h = f.thenCombine(g, subtract);
726 <        assertTrue(f.cancel(true));
1167 >        assertTrue(f.cancel(mayInterruptIfRunning));
1168          checkIncomplete(h);
1169 <        g.complete(1);
1169 >        g.complete(v1);
1170 >
1171          checkCompletedWithWrappedCancellationException(h);
1172 +        checkCancelled(f);
1173 +        assertFalse(r.ran());
1174 +        checkCompletedNormally(g, v1);
1175 +        }
1176 +    }
1177 +
1178 +    public void testThenCombine_sourceCancelled2() {
1179 +        for (ExecutionMode m : ExecutionMode.values())
1180 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1181 +        for (Integer v1 : new Integer[] { 1, null }) {
1182 +
1183 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1184 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1185 +        final SubtractFunction r = new SubtractFunction();
1186 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1187  
1188 <        f = new CompletableFuture<Integer>();
732 <        g = new CompletableFuture<Integer>();
733 <        h = f.thenCombine(g, subtract);
734 <        assertTrue(g.cancel(true));
1188 >        assertTrue(g.cancel(mayInterruptIfRunning));
1189          checkIncomplete(h);
1190 <        f.complete(3);
1190 >        f.complete(v1);
1191 >
1192          checkCompletedWithWrappedCancellationException(h);
1193 +        checkCancelled(g);
1194 +        assertFalse(r.ran());
1195 +        checkCompletedNormally(f, v1);
1196 +        }
1197 +    }
1198 +
1199 +    public void testThenCombine_sourceCancelled3() {
1200 +        for (ExecutionMode m : ExecutionMode.values())
1201 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1202 +        for (Integer v1 : new Integer[] { 1, null }) {
1203 +
1204 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1205 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1206 +        final SubtractFunction r = new SubtractFunction();
1207 +
1208 +        assertTrue(g.cancel(mayInterruptIfRunning));
1209 +        f.complete(v1);
1210 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1211  
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);
1212          checkCompletedWithWrappedCancellationException(h);
1213 +        checkCancelled(g);
1214 +        assertFalse(r.ran());
1215 +        checkCompletedNormally(f, v1);
1216 +        }
1217 +    }
1218 +
1219 +    public void testThenCombine_sourceCancelled4() {
1220 +        for (ExecutionMode m : ExecutionMode.values())
1221 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1222 +        for (Integer v1 : new Integer[] { 1, null }) {
1223 +
1224 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1225 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1226 +        final SubtractFunction r = new SubtractFunction();
1227 +
1228 +        assertTrue(f.cancel(mayInterruptIfRunning));
1229 +        g.complete(v1);
1230 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1231 +
1232 +        checkCompletedWithWrappedCancellationException(h);
1233 +        checkCancelled(f);
1234 +        assertFalse(r.ran());
1235 +        checkCompletedNormally(g, v1);
1236 +        }
1237      }
1238  
1239      /**
1240       * thenAcceptBoth result completes normally after normal
1241       * completion of sources
1242       */
1243 <    public void testThenAcceptBoth() {
1244 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1245 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1246 <        AddAction r = new AddAction();
1247 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1248 <        f.complete(one);
1249 <        checkIncomplete(g);
1250 <        f2.complete(two);
1251 <        checkCompletedNormally(g, null);
760 <        assertEquals(r.value, 3);
1243 >    public void testThenAcceptBoth_normalCompletion1() {
1244 >        for (ExecutionMode m : ExecutionMode.values())
1245 >        for (Integer v1 : new Integer[] { 1, null })
1246 >        for (Integer v2 : new Integer[] { 2, null }) {
1247 >
1248 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1249 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1250 >        final SubtractAction r = new SubtractAction();
1251 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1252  
1253 <        r = new AddAction();
1254 <        f = new CompletableFuture<Integer>();
1255 <        f.complete(one);
1256 <        f2 = new CompletableFuture<Integer>();
1257 <        g = f.thenAcceptBoth(f2, r);
1258 <        checkIncomplete(g);
1259 <        f2.complete(two);
1260 <        checkCompletedNormally(g, null);
1261 <        assertEquals(r.value, 3);
1253 >        f.complete(v1);
1254 >        checkIncomplete(h);
1255 >        assertFalse(r.ran());
1256 >        g.complete(v2);
1257 >
1258 >        checkCompletedNormally(h, null);
1259 >        assertEquals(r.value, subtract(v1, v2));
1260 >        checkCompletedNormally(f, v1);
1261 >        checkCompletedNormally(g, v2);
1262 >        }
1263 >    }
1264 >
1265 >    public void testThenAcceptBoth_normalCompletion2() {
1266 >        for (ExecutionMode m : ExecutionMode.values())
1267 >        for (Integer v1 : new Integer[] { 1, null })
1268 >        for (Integer v2 : new Integer[] { 2, null }) {
1269 >
1270 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1271 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1272 >        final SubtractAction r = new SubtractAction();
1273 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1274 >
1275 >        g.complete(v2);
1276 >        checkIncomplete(h);
1277 >        assertFalse(r.ran());
1278 >        f.complete(v1);
1279 >
1280 >        checkCompletedNormally(h, null);
1281 >        assertEquals(r.value, subtract(v1, v2));
1282 >        checkCompletedNormally(f, v1);
1283 >        checkCompletedNormally(g, v2);
1284 >        }
1285 >    }
1286 >
1287 >    public void testThenAcceptBoth_normalCompletion3() {
1288 >        for (ExecutionMode m : ExecutionMode.values())
1289 >        for (Integer v1 : new Integer[] { 1, null })
1290 >        for (Integer v2 : new Integer[] { 2, null }) {
1291 >
1292 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1293 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1294 >        final SubtractAction r = new SubtractAction();
1295 >
1296 >        g.complete(v2);
1297 >        f.complete(v1);
1298 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1299 >
1300 >        checkCompletedNormally(h, null);
1301 >        assertEquals(r.value, subtract(v1, v2));
1302 >        checkCompletedNormally(f, v1);
1303 >        checkCompletedNormally(g, v2);
1304 >        }
1305 >    }
1306 >
1307 >    public void testThenAcceptBoth_normalCompletion4() {
1308 >        for (ExecutionMode m : ExecutionMode.values())
1309 >        for (Integer v1 : new Integer[] { 1, null })
1310 >        for (Integer v2 : new Integer[] { 2, null }) {
1311 >
1312 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1313 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1314 >        final SubtractAction r = new SubtractAction();
1315 >
1316 >        f.complete(v1);
1317 >        g.complete(v2);
1318 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1319 >
1320 >        checkCompletedNormally(h, null);
1321 >        assertEquals(r.value, subtract(v1, v2));
1322 >        checkCompletedNormally(f, v1);
1323 >        checkCompletedNormally(g, v2);
1324 >        }
1325      }
1326  
1327      /**
1328       * thenAcceptBoth result completes exceptionally after exceptional
1329       * completion of either source
1330       */
1331 <    public void testThenAcceptBoth2() {
1332 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1333 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1334 <        AddAction r = new AddAction();
1335 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1336 <        f.completeExceptionally(new CFException());
1337 <        f2.complete(two);
1338 <        checkCompletedWithWrappedCFException(g);
1331 >    public void testThenAcceptBoth_exceptionalCompletion1() {
1332 >        for (ExecutionMode m : ExecutionMode.values())
1333 >        for (Integer v1 : new Integer[] { 1, null }) {
1334 >
1335 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1336 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1337 >        final SubtractAction r = new SubtractAction();
1338 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1339 >        final CFException ex = new CFException();
1340  
1341 <        r = new AddAction();
1342 <        f = new CompletableFuture<Integer>();
1343 <        f.complete(one);
1344 <        f2 = new CompletableFuture<Integer>();
1345 <        g = f.thenAcceptBoth(f2, r);
1346 <        f2.completeExceptionally(new CFException());
1347 <        checkCompletedWithWrappedCFException(g);
1341 >        f.completeExceptionally(ex);
1342 >        checkIncomplete(h);
1343 >        g.complete(v1);
1344 >
1345 >        checkCompletedWithWrappedCFException(h, ex);
1346 >        checkCompletedWithWrappedCFException(f, ex);
1347 >        assertFalse(r.ran());
1348 >        checkCompletedNormally(g, v1);
1349 >        }
1350 >    }
1351 >
1352 >    public void testThenAcceptBoth_exceptionalCompletion2() {
1353 >        for (ExecutionMode m : ExecutionMode.values())
1354 >        for (Integer v1 : new Integer[] { 1, null }) {
1355 >
1356 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1357 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1358 >        final SubtractAction r = new SubtractAction();
1359 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1360 >        final CFException ex = new CFException();
1361 >
1362 >        g.completeExceptionally(ex);
1363 >        checkIncomplete(h);
1364 >        f.complete(v1);
1365 >
1366 >        checkCompletedWithWrappedCFException(h, ex);
1367 >        checkCompletedWithWrappedCFException(g, ex);
1368 >        assertFalse(r.ran());
1369 >        checkCompletedNormally(f, v1);
1370 >        }
1371 >    }
1372 >
1373 >    public void testThenAcceptBoth_exceptionalCompletion3() {
1374 >        for (ExecutionMode m : ExecutionMode.values())
1375 >        for (Integer v1 : new Integer[] { 1, null }) {
1376 >
1377 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1378 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 >        final SubtractAction r = new SubtractAction();
1380 >        final CFException ex = new CFException();
1381 >
1382 >        g.completeExceptionally(ex);
1383 >        f.complete(v1);
1384 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1385 >
1386 >        checkCompletedWithWrappedCFException(h, ex);
1387 >        checkCompletedWithWrappedCFException(g, ex);
1388 >        assertFalse(r.ran());
1389 >        checkCompletedNormally(f, v1);
1390 >        }
1391 >    }
1392 >
1393 >    public void testThenAcceptBoth_exceptionalCompletion4() {
1394 >        for (ExecutionMode m : ExecutionMode.values())
1395 >        for (Integer v1 : new Integer[] { 1, null }) {
1396 >
1397 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1398 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1399 >        final SubtractAction r = new SubtractAction();
1400 >        final CFException ex = new CFException();
1401 >
1402 >        f.completeExceptionally(ex);
1403 >        g.complete(v1);
1404 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1405 >
1406 >        checkCompletedWithWrappedCFException(h, ex);
1407 >        checkCompletedWithWrappedCFException(f, ex);
1408 >        assertFalse(r.ran());
1409 >        checkCompletedNormally(g, v1);
1410 >        }
1411      }
1412  
1413      /**
1414       * thenAcceptBoth result completes exceptionally if action does
1415       */
1416 <    public void testThenAcceptBoth3() {
1417 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1418 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1419 <        FailingBiConsumer r = new FailingBiConsumer();
1420 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1421 <        f.complete(one);
1422 <        checkIncomplete(g);
1423 <        f2.complete(two);
1424 <        checkCompletedWithWrappedCFException(g);
1416 >    public void testThenAcceptBoth_actionFailed1() {
1417 >        for (ExecutionMode m : ExecutionMode.values())
1418 >        for (Integer v1 : new Integer[] { 1, null })
1419 >        for (Integer v2 : new Integer[] { 2, null }) {
1420 >
1421 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1422 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1423 >        final FailingBiConsumer r = new FailingBiConsumer();
1424 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1425 >
1426 >        f.complete(v1);
1427 >        checkIncomplete(h);
1428 >        g.complete(v2);
1429 >
1430 >        checkCompletedWithWrappedCFException(h);
1431 >        checkCompletedNormally(f, v1);
1432 >        checkCompletedNormally(g, v2);
1433 >        }
1434 >    }
1435 >
1436 >    public void testThenAcceptBoth_actionFailed2() {
1437 >        for (ExecutionMode m : ExecutionMode.values())
1438 >        for (Integer v1 : new Integer[] { 1, null })
1439 >        for (Integer v2 : new Integer[] { 2, null }) {
1440 >
1441 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1442 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1443 >        final FailingBiConsumer r = new FailingBiConsumer();
1444 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1445 >
1446 >        g.complete(v2);
1447 >        checkIncomplete(h);
1448 >        f.complete(v1);
1449 >
1450 >        checkCompletedWithWrappedCFException(h);
1451 >        checkCompletedNormally(f, v1);
1452 >        checkCompletedNormally(g, v2);
1453 >        }
1454      }
1455  
1456      /**
1457       * thenAcceptBoth result completes exceptionally if either source cancelled
1458       */
1459 <    public void testThenAcceptBoth4() {
1460 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1461 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1462 <        AddAction r = new AddAction();
1463 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1464 <        assertTrue(f.cancel(true));
1465 <        f2.complete(two);
1466 <        checkCompletedWithWrappedCancellationException(g);
1467 <        f = new CompletableFuture<Integer>();
1468 <        f2 = new CompletableFuture<Integer>();
1469 <        r = new AddAction();
1470 <        g = f.thenAcceptBoth(f2, r);
1471 <        f.complete(one);
1472 <        assertTrue(f2.cancel(true));
1473 <        checkCompletedWithWrappedCancellationException(g);
1459 >    public void testThenAcceptBoth_sourceCancelled1() {
1460 >        for (ExecutionMode m : ExecutionMode.values())
1461 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1462 >        for (Integer v1 : new Integer[] { 1, null }) {
1463 >
1464 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1465 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1466 >        final SubtractAction r = new SubtractAction();
1467 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1468 >
1469 >        assertTrue(f.cancel(mayInterruptIfRunning));
1470 >        checkIncomplete(h);
1471 >        g.complete(v1);
1472 >
1473 >        checkCompletedWithWrappedCancellationException(h);
1474 >        checkCancelled(f);
1475 >        assertFalse(r.ran());
1476 >        checkCompletedNormally(g, v1);
1477 >        }
1478 >    }
1479 >
1480 >    public void testThenAcceptBoth_sourceCancelled2() {
1481 >        for (ExecutionMode m : ExecutionMode.values())
1482 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1483 >        for (Integer v1 : new Integer[] { 1, null }) {
1484 >
1485 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1486 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1487 >        final SubtractAction r = new SubtractAction();
1488 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1489 >
1490 >        assertTrue(g.cancel(mayInterruptIfRunning));
1491 >        checkIncomplete(h);
1492 >        f.complete(v1);
1493 >
1494 >        checkCompletedWithWrappedCancellationException(h);
1495 >        checkCancelled(g);
1496 >        assertFalse(r.ran());
1497 >        checkCompletedNormally(f, v1);
1498 >        }
1499 >    }
1500 >
1501 >    public void testThenAcceptBoth_sourceCancelled3() {
1502 >        for (ExecutionMode m : ExecutionMode.values())
1503 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1504 >        for (Integer v1 : new Integer[] { 1, null }) {
1505 >
1506 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1507 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1508 >        final SubtractAction r = new SubtractAction();
1509 >
1510 >        assertTrue(g.cancel(mayInterruptIfRunning));
1511 >        f.complete(v1);
1512 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1513 >
1514 >        checkCompletedWithWrappedCancellationException(h);
1515 >        checkCancelled(g);
1516 >        assertFalse(r.ran());
1517 >        checkCompletedNormally(f, v1);
1518 >        }
1519 >    }
1520 >
1521 >    public void testThenAcceptBoth_sourceCancelled4() {
1522 >        for (ExecutionMode m : ExecutionMode.values())
1523 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1524 >        for (Integer v1 : new Integer[] { 1, null }) {
1525 >
1526 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1527 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1528 >        final SubtractAction r = new SubtractAction();
1529 >
1530 >        assertTrue(f.cancel(mayInterruptIfRunning));
1531 >        g.complete(v1);
1532 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1533 >
1534 >        checkCompletedWithWrappedCancellationException(h);
1535 >        checkCancelled(f);
1536 >        assertFalse(r.ran());
1537 >        checkCompletedNormally(g, v1);
1538 >        }
1539      }
1540  
1541      /**
1542       * runAfterBoth result completes normally after normal
1543       * completion of sources
1544       */
1545 <    public void testRunAfterBoth() {
1546 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1547 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1548 <        Noop r = new Noop();
1549 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1550 <        f.complete(one);
1551 <        checkIncomplete(g);
1552 <        f2.complete(two);
1553 <        checkCompletedNormally(g, null);
1545 >    public void testRunAfterBoth_normalCompletion1() {
1546 >        for (ExecutionMode m : ExecutionMode.values())
1547 >        for (Integer v1 : new Integer[] { 1, null })
1548 >        for (Integer v2 : new Integer[] { 2, null }) {
1549 >
1550 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1551 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1552 >        final Noop r = new Noop();
1553 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1554 >
1555 >        f.complete(v1);
1556 >        checkIncomplete(h);
1557 >        assertFalse(r.ran);
1558 >        g.complete(v2);
1559 >
1560 >        checkCompletedNormally(h, null);
1561          assertTrue(r.ran);
1562 +        checkCompletedNormally(f, v1);
1563 +        checkCompletedNormally(g, v2);
1564 +        }
1565 +    }
1566  
1567 <        r = new Noop();
1568 <        f = new CompletableFuture<Integer>();
1569 <        f.complete(one);
1570 <        f2 = new CompletableFuture<Integer>();
1571 <        g = f.runAfterBoth(f2, r);
1572 <        checkIncomplete(g);
1573 <        f2.complete(two);
1574 <        checkCompletedNormally(g, null);
1567 >    public void testRunAfterBoth_normalCompletion2() {
1568 >        for (ExecutionMode m : ExecutionMode.values())
1569 >        for (Integer v1 : new Integer[] { 1, null })
1570 >        for (Integer v2 : new Integer[] { 2, null }) {
1571 >
1572 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1573 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1574 >        final Noop r = new Noop();
1575 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1576 >
1577 >        g.complete(v2);
1578 >        checkIncomplete(h);
1579 >        assertFalse(r.ran);
1580 >        f.complete(v1);
1581 >
1582 >        checkCompletedNormally(h, null);
1583 >        assertTrue(r.ran);
1584 >        checkCompletedNormally(f, v1);
1585 >        checkCompletedNormally(g, v2);
1586 >        }
1587 >    }
1588 >
1589 >    public void testRunAfterBoth_normalCompletion3() {
1590 >        for (ExecutionMode m : ExecutionMode.values())
1591 >        for (Integer v1 : new Integer[] { 1, null })
1592 >        for (Integer v2 : new Integer[] { 2, null }) {
1593 >
1594 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1595 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1596 >        final Noop r = new Noop();
1597 >
1598 >        g.complete(v2);
1599 >        f.complete(v1);
1600 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1601 >
1602 >        checkCompletedNormally(h, null);
1603 >        assertTrue(r.ran);
1604 >        checkCompletedNormally(f, v1);
1605 >        checkCompletedNormally(g, v2);
1606 >        }
1607 >    }
1608 >
1609 >    public void testRunAfterBoth_normalCompletion4() {
1610 >        for (ExecutionMode m : ExecutionMode.values())
1611 >        for (Integer v1 : new Integer[] { 1, null })
1612 >        for (Integer v2 : new Integer[] { 2, null }) {
1613 >
1614 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1615 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1616 >        final Noop r = new Noop();
1617 >
1618 >        f.complete(v1);
1619 >        g.complete(v2);
1620 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1621 >
1622 >        checkCompletedNormally(h, null);
1623          assertTrue(r.ran);
1624 +        checkCompletedNormally(f, v1);
1625 +        checkCompletedNormally(g, v2);
1626 +        }
1627      }
1628  
1629      /**
1630       * runAfterBoth result completes exceptionally after exceptional
1631       * completion of either source
1632       */
1633 <    public void testRunAfterBoth2() {
1634 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1635 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1636 <        Noop r = new Noop();
1637 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1638 <        f.completeExceptionally(new CFException());
1639 <        f2.complete(two);
1640 <        checkCompletedWithWrappedCFException(g);
1633 >    public void testRunAfterBoth_exceptionalCompletion1() {
1634 >        for (ExecutionMode m : ExecutionMode.values())
1635 >        for (Integer v1 : new Integer[] { 1, null }) {
1636 >
1637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1639 >        final Noop r = new Noop();
1640 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1641 >        final CFException ex = new CFException();
1642  
1643 <        r = new Noop();
1644 <        f = new CompletableFuture<Integer>();
1645 <        f.complete(one);
1646 <        f2 = new CompletableFuture<Integer>();
1647 <        g = f.runAfterBoth(f2, r);
1648 <        f2.completeExceptionally(new CFException());
1649 <        checkCompletedWithWrappedCFException(g);
1643 >        f.completeExceptionally(ex);
1644 >        checkIncomplete(h);
1645 >        g.complete(v1);
1646 >
1647 >        checkCompletedWithWrappedCFException(h, ex);
1648 >        checkCompletedWithWrappedCFException(f, ex);
1649 >        assertFalse(r.ran);
1650 >        checkCompletedNormally(g, v1);
1651 >        }
1652 >    }
1653 >
1654 >    public void testRunAfterBoth_exceptionalCompletion2() {
1655 >        for (ExecutionMode m : ExecutionMode.values())
1656 >        for (Integer v1 : new Integer[] { 1, null }) {
1657 >
1658 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1659 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1660 >        final Noop r = new Noop();
1661 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1662 >        final CFException ex = new CFException();
1663 >
1664 >        g.completeExceptionally(ex);
1665 >        checkIncomplete(h);
1666 >        f.complete(v1);
1667 >
1668 >        checkCompletedWithWrappedCFException(h, ex);
1669 >        checkCompletedWithWrappedCFException(g, ex);
1670 >        assertFalse(r.ran);
1671 >        checkCompletedNormally(f, v1);
1672 >        }
1673 >    }
1674 >
1675 >    public void testRunAfterBoth_exceptionalCompletion3() {
1676 >        for (ExecutionMode m : ExecutionMode.values())
1677 >        for (Integer v1 : new Integer[] { 1, null }) {
1678 >
1679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1681 >        final Noop r = new Noop();
1682 >        final CFException ex = new CFException();
1683 >
1684 >        g.completeExceptionally(ex);
1685 >        f.complete(v1);
1686 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1687 >
1688 >        checkCompletedWithWrappedCFException(h, ex);
1689 >        checkCompletedWithWrappedCFException(g, ex);
1690 >        assertFalse(r.ran);
1691 >        checkCompletedNormally(f, v1);
1692 >        }
1693 >    }
1694 >
1695 >    public void testRunAfterBoth_exceptionalCompletion4() {
1696 >        for (ExecutionMode m : ExecutionMode.values())
1697 >        for (Integer v1 : new Integer[] { 1, null }) {
1698 >
1699 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1700 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1701 >        final Noop r = new Noop();
1702 >        final CFException ex = new CFException();
1703 >
1704 >        f.completeExceptionally(ex);
1705 >        g.complete(v1);
1706 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1707 >
1708 >        checkCompletedWithWrappedCFException(h, ex);
1709 >        checkCompletedWithWrappedCFException(f, ex);
1710 >        assertFalse(r.ran);
1711 >        checkCompletedNormally(g, v1);
1712 >        }
1713      }
1714  
1715      /**
1716       * runAfterBoth result completes exceptionally if action does
1717       */
1718 <    public void testRunAfterBoth3() {
1719 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1720 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1721 <        FailingNoop r = new FailingNoop();
1722 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1723 <        f.complete(one);
1724 <        checkIncomplete(g);
1725 <        f2.complete(two);
1726 <        checkCompletedWithWrappedCFException(g);
1718 >    public void testRunAfterBoth_actionFailed1() {
1719 >        for (ExecutionMode m : ExecutionMode.values())
1720 >        for (Integer v1 : new Integer[] { 1, null })
1721 >        for (Integer v2 : new Integer[] { 2, null }) {
1722 >
1723 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1724 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1725 >        final FailingNoop r = new FailingNoop();
1726 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1727 >
1728 >        f.complete(v1);
1729 >        checkIncomplete(h);
1730 >        g.complete(v2);
1731 >
1732 >        checkCompletedWithWrappedCFException(h);
1733 >        checkCompletedNormally(f, v1);
1734 >        checkCompletedNormally(g, v2);
1735 >        }
1736 >    }
1737 >
1738 >    public void testRunAfterBoth_actionFailed2() {
1739 >        for (ExecutionMode m : ExecutionMode.values())
1740 >        for (Integer v1 : new Integer[] { 1, null })
1741 >        for (Integer v2 : new Integer[] { 2, null }) {
1742 >
1743 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1744 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1745 >        final FailingNoop r = new FailingNoop();
1746 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1747 >
1748 >        g.complete(v2);
1749 >        checkIncomplete(h);
1750 >        f.complete(v1);
1751 >
1752 >        checkCompletedWithWrappedCFException(h);
1753 >        checkCompletedNormally(f, v1);
1754 >        checkCompletedNormally(g, v2);
1755 >        }
1756      }
1757  
1758      /**
1759       * runAfterBoth result completes exceptionally if either source cancelled
1760       */
1761 <    public void testRunAfterBoth4() {
1762 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1763 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1764 <        Noop r = new Noop();
1765 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1766 <        assertTrue(f.cancel(true));
1767 <        f2.complete(two);
1768 <        checkCompletedWithWrappedCancellationException(g);
1769 <        f = new CompletableFuture<Integer>();
1770 <        f2 = new CompletableFuture<Integer>();
1771 <        r = new Noop();
1772 <        g = f.runAfterBoth(f2, r);
1773 <        f.complete(one);
1774 <        assertTrue(f2.cancel(true));
1775 <        checkCompletedWithWrappedCancellationException(g);
1761 >    public void testRunAfterBoth_sourceCancelled1() {
1762 >        for (ExecutionMode m : ExecutionMode.values())
1763 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1764 >        for (Integer v1 : new Integer[] { 1, null }) {
1765 >
1766 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1767 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1768 >        final Noop r = new Noop();
1769 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1770 >
1771 >        assertTrue(f.cancel(mayInterruptIfRunning));
1772 >        checkIncomplete(h);
1773 >        g.complete(v1);
1774 >
1775 >        checkCompletedWithWrappedCancellationException(h);
1776 >        checkCancelled(f);
1777 >        assertFalse(r.ran);
1778 >        checkCompletedNormally(g, v1);
1779 >        }
1780 >    }
1781 >
1782 >    public void testRunAfterBoth_sourceCancelled2() {
1783 >        for (ExecutionMode m : ExecutionMode.values())
1784 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1785 >        for (Integer v1 : new Integer[] { 1, null }) {
1786 >
1787 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1788 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1789 >        final Noop r = new Noop();
1790 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1791 >
1792 >        assertTrue(g.cancel(mayInterruptIfRunning));
1793 >        checkIncomplete(h);
1794 >        f.complete(v1);
1795 >
1796 >        checkCompletedWithWrappedCancellationException(h);
1797 >        checkCancelled(g);
1798 >        assertFalse(r.ran);
1799 >        checkCompletedNormally(f, v1);
1800 >        }
1801 >    }
1802 >
1803 >    public void testRunAfterBoth_sourceCancelled3() {
1804 >        for (ExecutionMode m : ExecutionMode.values())
1805 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1806 >        for (Integer v1 : new Integer[] { 1, null }) {
1807 >
1808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 >        final Noop r = new Noop();
1811 >
1812 >        assertTrue(g.cancel(mayInterruptIfRunning));
1813 >        f.complete(v1);
1814 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1815 >
1816 >        checkCompletedWithWrappedCancellationException(h);
1817 >        checkCancelled(g);
1818 >        assertFalse(r.ran);
1819 >        checkCompletedNormally(f, v1);
1820 >        }
1821 >    }
1822 >
1823 >    public void testRunAfterBoth_sourceCancelled4() {
1824 >        for (ExecutionMode m : ExecutionMode.values())
1825 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1826 >        for (Integer v1 : new Integer[] { 1, null }) {
1827 >
1828 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1829 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1830 >        final Noop r = new Noop();
1831 >
1832 >        assertTrue(f.cancel(mayInterruptIfRunning));
1833 >        g.complete(v1);
1834 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1835 >
1836 >        checkCompletedWithWrappedCancellationException(h);
1837 >        checkCancelled(f);
1838 >        assertFalse(r.ran);
1839 >        checkCompletedNormally(g, v1);
1840 >        }
1841      }
1842  
1843      /**
# Line 913 | Line 1845 | public class CompletableFutureTest exten
1845       * of either source
1846       */
1847      public void testApplyToEither() {
1848 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1849 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1848 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1849 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1850          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1851          f.complete(one);
1852          checkCompletedNormally(g, two);
1853          f2.complete(one);
1854          checkCompletedNormally(g, two);
1855  
1856 <        f = new CompletableFuture<Integer>();
1856 >        f = new CompletableFuture<>();
1857          f.complete(one);
1858 <        f2 = new CompletableFuture<Integer>();
1858 >        f2 = new CompletableFuture<>();
1859          g = f.applyToEither(f2, inc);
1860          checkCompletedNormally(g, two);
1861      }
# Line 933 | Line 1865 | public class CompletableFutureTest exten
1865       * completion of either source
1866       */
1867      public void testApplyToEither2() {
1868 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1869 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1868 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1869 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1870          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1871          f.completeExceptionally(new CFException());
1872          f2.complete(one);
1873          checkCompletedWithWrappedCFException(g);
1874  
1875 <        f = new CompletableFuture<Integer>();
1876 <        f2 = new CompletableFuture<Integer>();
1875 >        f = new CompletableFuture<>();
1876 >        f2 = new CompletableFuture<>();
1877          f2.completeExceptionally(new CFException());
1878          g = f.applyToEither(f2, inc);
1879          checkCompletedWithWrappedCFException(g);
# Line 951 | Line 1883 | public class CompletableFutureTest exten
1883       * applyToEither result completes exceptionally if action does
1884       */
1885      public void testApplyToEither3() {
1886 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1887 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1886 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1887 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1888          FailingFunction r = new FailingFunction();
1889          CompletableFuture<Integer> g = f.applyToEither(f2, r);
1890          f2.complete(two);
# Line 963 | Line 1895 | public class CompletableFutureTest exten
1895       * applyToEither result completes exceptionally if either source cancelled
1896       */
1897      public void testApplyToEither4() {
1898 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1899 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1898 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1899 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1900          CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1901          assertTrue(f.cancel(true));
1902          checkCompletedWithWrappedCancellationException(g);
1903 <        f = new CompletableFuture<Integer>();
1904 <        f2 = new CompletableFuture<Integer>();
1903 >        f = new CompletableFuture<>();
1904 >        f2 = new CompletableFuture<>();
1905          assertTrue(f2.cancel(true));
1906          checkCompletedWithWrappedCancellationException(g);
1907      }
# Line 979 | Line 1911 | public class CompletableFutureTest exten
1911       * of either source
1912       */
1913      public void testAcceptEither() {
1914 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1915 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1914 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1915 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1916          IncAction r = new IncAction();
1917          CompletableFuture<Void> g = f.acceptEither(f2, r);
1918          f.complete(one);
1919          checkCompletedNormally(g, null);
1920          f2.complete(one);
1921          checkCompletedNormally(g, null);
1922 <        assertEquals(r.value, 2);
1922 >        assertEquals(r.value, (Integer) 2);
1923  
1924          r = new IncAction();
1925 <        f = new CompletableFuture<Integer>();
1925 >        f = new CompletableFuture<>();
1926          f.complete(one);
1927 <        f2 = new CompletableFuture<Integer>();
1927 >        f2 = new CompletableFuture<>();
1928          g = f.acceptEither(f2, r);
1929          checkCompletedNormally(g, null);
1930 <        assertEquals(r.value, 2);
1930 >        assertEquals(r.value, (Integer) 2);
1931      }
1932  
1933      /**
# Line 1003 | Line 1935 | public class CompletableFutureTest exten
1935       * completion of either source
1936       */
1937      public void testAcceptEither2() {
1938 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1939 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1938 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1939 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1940          IncAction r = new IncAction();
1941          CompletableFuture<Void> g = f.acceptEither(f2, r);
1942          f.completeExceptionally(new CFException());
# Line 1012 | Line 1944 | public class CompletableFutureTest exten
1944          checkCompletedWithWrappedCFException(g);
1945  
1946          r = new IncAction();
1947 <        f = new CompletableFuture<Integer>();
1948 <        f2 = new CompletableFuture<Integer>();
1947 >        f = new CompletableFuture<>();
1948 >        f2 = new CompletableFuture<>();
1949          f2.completeExceptionally(new CFException());
1950          g = f.acceptEither(f2, r);
1951          checkCompletedWithWrappedCFException(g);
# Line 1023 | Line 1955 | public class CompletableFutureTest exten
1955       * acceptEither result completes exceptionally if action does
1956       */
1957      public void testAcceptEither3() {
1958 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1959 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1958 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1959 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1960          FailingConsumer r = new FailingConsumer();
1961          CompletableFuture<Void> g = f.acceptEither(f2, r);
1962          f2.complete(two);
# Line 1035 | Line 1967 | public class CompletableFutureTest exten
1967       * acceptEither result completes exceptionally if either source cancelled
1968       */
1969      public void testAcceptEither4() {
1970 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1971 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1970 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1971 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1972          IncAction r = new IncAction();
1973          CompletableFuture<Void> g = f.acceptEither(f2, r);
1974          assertTrue(f.cancel(true));
1975          checkCompletedWithWrappedCancellationException(g);
1976 <        f = new CompletableFuture<Integer>();
1977 <        f2 = new CompletableFuture<Integer>();
1976 >        f = new CompletableFuture<>();
1977 >        f2 = new CompletableFuture<>();
1978          assertTrue(f2.cancel(true));
1979          checkCompletedWithWrappedCancellationException(g);
1980      }
1981  
1050
1982      /**
1983       * runAfterEither result completes normally after normal completion
1984       * of either source
1985       */
1986      public void testRunAfterEither() {
1987 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1988 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1987 >        CompletableFuture<Integer> f = new CompletableFuture<>();
1988 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1989          Noop r = new Noop();
1990          CompletableFuture<Void> g = f.runAfterEither(f2, r);
1991          f.complete(one);
# Line 1064 | Line 1995 | public class CompletableFutureTest exten
1995          assertTrue(r.ran);
1996  
1997          r = new Noop();
1998 <        f = new CompletableFuture<Integer>();
1998 >        f = new CompletableFuture<>();
1999          f.complete(one);
2000 <        f2 = new CompletableFuture<Integer>();
2000 >        f2 = new CompletableFuture<>();
2001          g = f.runAfterEither(f2, r);
2002          checkCompletedNormally(g, null);
2003          assertTrue(r.ran);
# Line 1077 | Line 2008 | public class CompletableFutureTest exten
2008       * completion of either source
2009       */
2010      public void testRunAfterEither2() {
2011 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2012 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2011 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2012 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2013          Noop r = new Noop();
2014          CompletableFuture<Void> g = f.runAfterEither(f2, r);
2015          f.completeExceptionally(new CFException());
# Line 1086 | Line 2017 | public class CompletableFutureTest exten
2017          checkCompletedWithWrappedCFException(g);
2018  
2019          r = new Noop();
2020 <        f = new CompletableFuture<Integer>();
2021 <        f2 = new CompletableFuture<Integer>();
2020 >        f = new CompletableFuture<>();
2021 >        f2 = new CompletableFuture<>();
2022          f2.completeExceptionally(new CFException());
2023          g = f.runAfterEither(f2, r);
2024          checkCompletedWithWrappedCFException(g);
# Line 1097 | Line 2028 | public class CompletableFutureTest exten
2028       * runAfterEither result completes exceptionally if action does
2029       */
2030      public void testRunAfterEither3() {
2031 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2032 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2031 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2032 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2033          FailingNoop r = new FailingNoop();
2034          CompletableFuture<Void> g = f.runAfterEither(f2, r);
2035          f2.complete(two);
# Line 1109 | Line 2040 | public class CompletableFutureTest exten
2040       * runAfterEither result completes exceptionally if either source cancelled
2041       */
2042      public void testRunAfterEither4() {
2043 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2044 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2043 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2044 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2045          Noop r = new Noop();
2046          CompletableFuture<Void> g = f.runAfterEither(f2, r);
2047          assertTrue(f.cancel(true));
2048          checkCompletedWithWrappedCancellationException(g);
2049 <        f = new CompletableFuture<Integer>();
2050 <        f2 = new CompletableFuture<Integer>();
2049 >        f = new CompletableFuture<>();
2050 >        f2 = new CompletableFuture<>();
2051          assertTrue(f2.cancel(true));
2052          checkCompletedWithWrappedCancellationException(g);
2053      }
# Line 1125 | Line 2056 | public class CompletableFutureTest exten
2056       * thenCompose result completes normally after normal completion of source
2057       */
2058      public void testThenCompose() {
2059 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2060 <        CompletableFutureInc r = new CompletableFutureInc();
2061 <        CompletableFuture<Integer> g = f.thenCompose(r);
2059 >        CompletableFuture<Integer> f, g;
2060 >        CompletableFutureInc r;
2061 >
2062 >        f = new CompletableFuture<>();
2063 >        g = f.thenCompose(r = new CompletableFutureInc());
2064 >        f.complete(one);
2065 >        checkCompletedNormally(g, two);
2066 >        assertTrue(r.ran);
2067 >
2068 >        f = new CompletableFuture<>();
2069          f.complete(one);
2070 +        g = f.thenCompose(r = new CompletableFutureInc());
2071          checkCompletedNormally(g, two);
2072 +        assertTrue(r.ran);
2073      }
2074  
2075      /**
# Line 1137 | Line 2077 | public class CompletableFutureTest exten
2077       * completion of source
2078       */
2079      public void testThenCompose2() {
2080 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2081 <        CompletableFutureInc r = new CompletableFutureInc();
2082 <        CompletableFuture<Integer> g = f.thenCompose(r);
2080 >        CompletableFuture<Integer> f, g;
2081 >        CompletableFutureInc r;
2082 >
2083 >        f = new CompletableFuture<>();
2084 >        g = f.thenCompose(r = new CompletableFutureInc());
2085          f.completeExceptionally(new CFException());
2086          checkCompletedWithWrappedCFException(g);
2087 +
2088 +        f = new CompletableFuture<>();
2089 +        f.completeExceptionally(new CFException());
2090 +        g = f.thenCompose(r = new CompletableFutureInc());
2091 +        checkCompletedWithWrappedCFException(g);
2092      }
2093  
2094      /**
2095       * thenCompose result completes exceptionally if action does
2096       */
2097      public void testThenCompose3() {
2098 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2099 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2100 <        CompletableFuture<Integer> g = f.thenCompose(r);
2098 >        CompletableFuture<Integer> f, g;
2099 >        FailingCompletableFutureFunction r;
2100 >
2101 >        f = new CompletableFuture<>();
2102 >        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2103 >        f.complete(one);
2104 >        checkCompletedWithWrappedCFException(g);
2105 >
2106 >        f = new CompletableFuture<>();
2107          f.complete(one);
2108 +        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2109          checkCompletedWithWrappedCFException(g);
2110      }
2111  
# Line 1159 | Line 2113 | public class CompletableFutureTest exten
2113       * thenCompose result completes exceptionally if source cancelled
2114       */
2115      public void testThenCompose4() {
2116 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2117 <        CompletableFutureInc r = new CompletableFutureInc();
2118 <        CompletableFuture<Integer> g = f.thenCompose(r);
2116 >        CompletableFuture<Integer> f, g;
2117 >        CompletableFutureInc r;
2118 >
2119 >        f = new CompletableFuture<>();
2120 >        g = f.thenCompose(r = new CompletableFutureInc());
2121          assertTrue(f.cancel(true));
2122          checkCompletedWithWrappedCancellationException(g);
1167    }
2123  
2124 +        f = new CompletableFuture<>();
2125 +        assertTrue(f.cancel(true));
2126 +        g = f.thenCompose(r = new CompletableFutureInc());
2127 +        checkCompletedWithWrappedCancellationException(g);
2128 +    }
2129  
2130      // asyncs
2131  
# Line 1173 | Line 2133 | public class CompletableFutureTest exten
2133       * thenRunAsync result completes normally after normal completion of source
2134       */
2135      public void testThenRunAsync() {
2136 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2136 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2137          Noop r = new Noop();
2138          CompletableFuture<Void> g = f.thenRunAsync(r);
2139          f.complete(null);
2140          checkCompletedNormally(g, null);
2141  
2142          // reordered version
2143 <        f = new CompletableFuture<Integer>();
2143 >        f = new CompletableFuture<>();
2144          f.complete(null);
2145          r = new Noop();
2146          g = f.thenRunAsync(r);
# Line 1192 | Line 2152 | public class CompletableFutureTest exten
2152       * completion of source
2153       */
2154      public void testThenRunAsync2() {
2155 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2155 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2156          Noop r = new Noop();
2157          CompletableFuture<Void> g = f.thenRunAsync(r);
2158          f.completeExceptionally(new CFException());
2159          try {
2160              g.join();
2161              shouldThrow();
2162 <        } catch (Exception ok) {
1203 <        }
2162 >        } catch (CompletionException success) {}
2163          checkCompletedWithWrappedCFException(g);
2164      }
2165  
# Line 1208 | Line 2167 | public class CompletableFutureTest exten
2167       * thenRunAsync result completes exceptionally if action does
2168       */
2169      public void testThenRunAsync3() {
2170 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2170 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2171          FailingNoop r = new FailingNoop();
2172          CompletableFuture<Void> g = f.thenRunAsync(r);
2173          f.complete(null);
# Line 1219 | Line 2178 | public class CompletableFutureTest exten
2178       * thenRunAsync result completes exceptionally if source cancelled
2179       */
2180      public void testThenRunAsync4() {
2181 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2181 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2182          Noop r = new Noop();
2183          CompletableFuture<Void> g = f.thenRunAsync(r);
2184          assertTrue(f.cancel(true));
# Line 1230 | Line 2189 | public class CompletableFutureTest exten
2189       * thenApplyAsync result completes normally after normal completion of source
2190       */
2191      public void testThenApplyAsync() {
2192 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2192 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2193          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2194          f.complete(one);
2195          checkCompletedNormally(g, two);
# Line 1241 | Line 2200 | public class CompletableFutureTest exten
2200       * completion of source
2201       */
2202      public void testThenApplyAsync2() {
2203 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2203 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2204          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2205          f.completeExceptionally(new CFException());
2206          checkCompletedWithWrappedCFException(g);
# Line 1251 | Line 2210 | public class CompletableFutureTest exten
2210       * thenApplyAsync result completes exceptionally if action does
2211       */
2212      public void testThenApplyAsync3() {
2213 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2213 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2214          FailingFunction r = new FailingFunction();
2215          CompletableFuture<Integer> g = f.thenApplyAsync(r);
2216          f.complete(null);
# Line 1262 | Line 2221 | public class CompletableFutureTest exten
2221       * thenApplyAsync result completes exceptionally if source cancelled
2222       */
2223      public void testThenApplyAsync4() {
2224 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2224 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2225          CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2226          assertTrue(f.cancel(true));
2227          checkCompletedWithWrappedCancellationException(g);
# Line 1273 | Line 2232 | public class CompletableFutureTest exten
2232       * completion of source
2233       */
2234      public void testThenAcceptAsync() {
2235 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2235 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2236          IncAction r = new IncAction();
2237          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2238          f.complete(one);
2239          checkCompletedNormally(g, null);
2240 <        assertEquals(r.value, 2);
2240 >        assertEquals(r.value, (Integer) 2);
2241      }
2242  
2243      /**
# Line 1286 | Line 2245 | public class CompletableFutureTest exten
2245       * completion of source
2246       */
2247      public void testThenAcceptAsync2() {
2248 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2248 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2249          IncAction r = new IncAction();
2250          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2251          f.completeExceptionally(new CFException());
# Line 1297 | Line 2256 | public class CompletableFutureTest exten
2256       * thenAcceptAsync result completes exceptionally if action does
2257       */
2258      public void testThenAcceptAsync3() {
2259 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2259 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2260          FailingConsumer r = new FailingConsumer();
2261          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2262          f.complete(null);
# Line 1308 | Line 2267 | public class CompletableFutureTest exten
2267       * thenAcceptAsync result completes exceptionally if source cancelled
2268       */
2269      public void testThenAcceptAsync4() {
2270 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2270 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2271          IncAction r = new IncAction();
2272          CompletableFuture<Void> g = f.thenAcceptAsync(r);
2273          assertTrue(f.cancel(true));
# Line 1316 | Line 2275 | public class CompletableFutureTest exten
2275      }
2276  
2277      /**
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    /**
2278       * applyToEitherAsync result completes normally after normal
2279       * completion of sources
2280       */
2281      public void testApplyToEitherAsync() {
2282 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2283 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2282 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2283 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2284          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2285          f.complete(one);
2286          checkCompletedNormally(g, two);
2287  
2288 <        f = new CompletableFuture<Integer>();
2288 >        f = new CompletableFuture<>();
2289          f.complete(one);
2290 <        f2 = new CompletableFuture<Integer>();
2290 >        f2 = new CompletableFuture<>();
2291          g = f.applyToEitherAsync(f2, inc);
2292          checkCompletedNormally(g, two);
2293      }
# Line 1599 | Line 2297 | public class CompletableFutureTest exten
2297       * completion of source
2298       */
2299      public void testApplyToEitherAsync2() {
2300 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2301 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2300 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2301 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2302          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2303          f.completeExceptionally(new CFException());
2304          checkCompletedWithWrappedCFException(g);
2305  
2306 <        f = new CompletableFuture<Integer>();
2307 <        f2 = new CompletableFuture<Integer>();
2306 >        f = new CompletableFuture<>();
2307 >        f2 = new CompletableFuture<>();
2308          f2.completeExceptionally(new CFException());
2309          g = f.applyToEitherAsync(f2, inc);
2310          f.complete(one);
# Line 1617 | Line 2315 | public class CompletableFutureTest exten
2315       * applyToEitherAsync result completes exceptionally if action does
2316       */
2317      public void testApplyToEitherAsync3() {
2318 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2319 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2318 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2319 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2320          FailingFunction r = new FailingFunction();
2321          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2322          f.complete(one);
# Line 1629 | Line 2327 | public class CompletableFutureTest exten
2327       * applyToEitherAsync result completes exceptionally if either source cancelled
2328       */
2329      public void testApplyToEitherAsync4() {
2330 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2331 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2330 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2331 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2332          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2333          assertTrue(f.cancel(true));
2334          checkCompletedWithWrappedCancellationException(g);
2335  
2336 <        f = new CompletableFuture<Integer>();
2337 <        f2 = new CompletableFuture<Integer>();
2336 >        f = new CompletableFuture<>();
2337 >        f2 = new CompletableFuture<>();
2338          assertTrue(f2.cancel(true));
2339          g = f.applyToEitherAsync(f2, inc);
2340          checkCompletedWithWrappedCancellationException(g);
# Line 1647 | Line 2345 | public class CompletableFutureTest exten
2345       * completion of sources
2346       */
2347      public void testAcceptEitherAsync() {
2348 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2349 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2348 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2349 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2350          IncAction r = new IncAction();
2351          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2352          f.complete(one);
2353          checkCompletedNormally(g, null);
2354 <        assertEquals(r.value, 2);
2354 >        assertEquals(r.value, (Integer) 2);
2355  
2356          r = new IncAction();
2357 <        f = new CompletableFuture<Integer>();
2357 >        f = new CompletableFuture<>();
2358          f.complete(one);
2359 <        f2 = new CompletableFuture<Integer>();
2359 >        f2 = new CompletableFuture<>();
2360          g = f.acceptEitherAsync(f2, r);
2361          checkCompletedNormally(g, null);
2362 <        assertEquals(r.value, 2);
2362 >        assertEquals(r.value, (Integer) 2);
2363      }
2364  
2365      /**
# Line 1669 | Line 2367 | public class CompletableFutureTest exten
2367       * completion of source
2368       */
2369      public void testAcceptEitherAsync2() {
2370 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2371 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2370 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2371 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2372          IncAction r = new IncAction();
2373          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2374          f.completeExceptionally(new CFException());
2375          checkCompletedWithWrappedCFException(g);
2376  
2377          r = new IncAction();
2378 <        f = new CompletableFuture<Integer>();
2379 <        f2 = new CompletableFuture<Integer>();
2378 >        f = new CompletableFuture<>();
2379 >        f2 = new CompletableFuture<>();
2380          f2.completeExceptionally(new CFException());
2381          g = f.acceptEitherAsync(f2, r);
2382          f.complete(one);
# Line 1689 | Line 2387 | public class CompletableFutureTest exten
2387       * acceptEitherAsync result completes exceptionally if action does
2388       */
2389      public void testAcceptEitherAsync3() {
2390 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2391 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2390 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2391 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2392          FailingConsumer r = new FailingConsumer();
2393          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2394          f.complete(one);
# Line 1702 | Line 2400 | public class CompletableFutureTest exten
2400       * source cancelled
2401       */
2402      public void testAcceptEitherAsync4() {
2403 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2404 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2403 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2404 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2405          IncAction r = new IncAction();
2406          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2407          assertTrue(f.cancel(true));
2408          checkCompletedWithWrappedCancellationException(g);
2409  
2410          r = new IncAction();
2411 <        f = new CompletableFuture<Integer>();
2412 <        f2 = new CompletableFuture<Integer>();
2411 >        f = new CompletableFuture<>();
2412 >        f2 = new CompletableFuture<>();
2413          assertTrue(f2.cancel(true));
2414          g = f.acceptEitherAsync(f2, r);
2415          checkCompletedWithWrappedCancellationException(g);
# Line 1722 | Line 2420 | public class CompletableFutureTest exten
2420       * completion of sources
2421       */
2422      public void testRunAfterEitherAsync() {
2423 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2424 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2423 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2424 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2425          Noop r = new Noop();
2426          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2427          f.complete(one);
# Line 1731 | Line 2429 | public class CompletableFutureTest exten
2429          assertTrue(r.ran);
2430  
2431          r = new Noop();
2432 <        f = new CompletableFuture<Integer>();
2432 >        f = new CompletableFuture<>();
2433          f.complete(one);
2434 <        f2 = new CompletableFuture<Integer>();
2434 >        f2 = new CompletableFuture<>();
2435          g = f.runAfterEitherAsync(f2, r);
2436          checkCompletedNormally(g, null);
2437          assertTrue(r.ran);
# Line 1744 | Line 2442 | public class CompletableFutureTest exten
2442       * completion of source
2443       */
2444      public void testRunAfterEitherAsync2() {
2445 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2446 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2445 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2446 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2447          Noop r = new Noop();
2448          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2449          f.completeExceptionally(new CFException());
2450          checkCompletedWithWrappedCFException(g);
2451  
2452          r = new Noop();
2453 <        f = new CompletableFuture<Integer>();
2454 <        f2 = new CompletableFuture<Integer>();
2453 >        f = new CompletableFuture<>();
2454 >        f2 = new CompletableFuture<>();
2455          f2.completeExceptionally(new CFException());
2456          g = f.runAfterEitherAsync(f2, r);
2457          f.complete(one);
# Line 1764 | Line 2462 | public class CompletableFutureTest exten
2462       * runAfterEitherAsync result completes exceptionally if action does
2463       */
2464      public void testRunAfterEitherAsync3() {
2465 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2466 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2465 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2466 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2467          FailingNoop r = new FailingNoop();
2468          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2469          f.complete(one);
# Line 1777 | Line 2475 | public class CompletableFutureTest exten
2475       * source cancelled
2476       */
2477      public void testRunAfterEitherAsync4() {
2478 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2479 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2478 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2479 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2480          Noop r = new Noop();
2481          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2482          assertTrue(f.cancel(true));
2483          checkCompletedWithWrappedCancellationException(g);
2484  
2485          r = new Noop();
2486 <        f = new CompletableFuture<Integer>();
2487 <        f2 = new CompletableFuture<Integer>();
2486 >        f = new CompletableFuture<>();
2487 >        f2 = new CompletableFuture<>();
2488          assertTrue(f2.cancel(true));
2489          g = f.runAfterEitherAsync(f2, r);
2490          checkCompletedWithWrappedCancellationException(g);
# Line 1797 | Line 2495 | public class CompletableFutureTest exten
2495       * completion of source
2496       */
2497      public void testThenComposeAsync() {
2498 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2499 <        CompletableFutureInc r = new CompletableFutureInc();
2500 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2498 >        CompletableFuture<Integer> f, g;
2499 >        CompletableFutureInc r;
2500 >
2501 >        f = new CompletableFuture<>();
2502 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2503 >        f.complete(one);
2504 >        checkCompletedNormally(g, two);
2505 >
2506 >        f = new CompletableFuture<>();
2507          f.complete(one);
2508 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2509          checkCompletedNormally(g, two);
2510      }
2511  
# Line 1809 | Line 2514 | public class CompletableFutureTest exten
2514       * exceptional completion of source
2515       */
2516      public void testThenComposeAsync2() {
2517 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2518 <        CompletableFutureInc r = new CompletableFutureInc();
2519 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2517 >        CompletableFuture<Integer> f, g;
2518 >        CompletableFutureInc r;
2519 >
2520 >        f = new CompletableFuture<>();
2521 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2522          f.completeExceptionally(new CFException());
2523          checkCompletedWithWrappedCFException(g);
2524 +        assertFalse(r.ran);
2525 +
2526 +        f = new CompletableFuture<>();
2527 +        f.completeExceptionally(new CFException());
2528 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2529 +        checkCompletedWithWrappedCFException(g);
2530 +        assertFalse(r.ran);
2531      }
2532  
2533      /**
2534       * thenComposeAsync result completes exceptionally if action does
2535       */
2536      public void testThenComposeAsync3() {
2537 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2538 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2539 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2537 >        CompletableFuture<Integer> f, g;
2538 >        FailingCompletableFutureFunction r;
2539 >
2540 >        f = new CompletableFuture<>();
2541 >        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2542 >        f.complete(one);
2543 >        checkCompletedWithWrappedCFException(g);
2544 >
2545 >        f = new CompletableFuture<>();
2546          f.complete(one);
2547 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2548          checkCompletedWithWrappedCFException(g);
2549      }
2550  
# Line 1831 | Line 2552 | public class CompletableFutureTest exten
2552       * thenComposeAsync result completes exceptionally if source cancelled
2553       */
2554      public void testThenComposeAsync4() {
2555 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2556 <        CompletableFutureInc r = new CompletableFutureInc();
2557 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2555 >        CompletableFuture<Integer> f, g;
2556 >        CompletableFutureInc r;
2557 >
2558 >        f = new CompletableFuture<>();
2559 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
2560          assertTrue(f.cancel(true));
2561          checkCompletedWithWrappedCancellationException(g);
1839    }
2562  
2563 +        f = new CompletableFuture<>();
2564 +        assertTrue(f.cancel(true));
2565 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
2566 +        checkCompletedWithWrappedCancellationException(g);
2567 +    }
2568  
2569      // async with explicit executors
2570  
# Line 1845 | Line 2572 | public class CompletableFutureTest exten
2572       * thenRunAsync result completes normally after normal completion of source
2573       */
2574      public void testThenRunAsyncE() {
2575 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2575 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2576          Noop r = new Noop();
2577          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2578          f.complete(null);
2579          checkCompletedNormally(g, null);
2580  
2581          // reordered version
2582 <        f = new CompletableFuture<Integer>();
2582 >        f = new CompletableFuture<>();
2583          f.complete(null);
2584          r = new Noop();
2585          g = f.thenRunAsync(r, new ThreadExecutor());
# Line 1864 | Line 2591 | public class CompletableFutureTest exten
2591       * completion of source
2592       */
2593      public void testThenRunAsync2E() {
2594 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2594 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2595          Noop r = new Noop();
2596          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2597          f.completeExceptionally(new CFException());
2598          try {
2599              g.join();
2600              shouldThrow();
2601 <        } catch (Exception ok) {
1875 <        }
2601 >        } catch (CompletionException success) {}
2602          checkCompletedWithWrappedCFException(g);
2603      }
2604  
# Line 1880 | Line 2606 | public class CompletableFutureTest exten
2606       * thenRunAsync result completes exceptionally if action does
2607       */
2608      public void testThenRunAsync3E() {
2609 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2609 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2610          FailingNoop r = new FailingNoop();
2611          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2612          f.complete(null);
# Line 1891 | Line 2617 | public class CompletableFutureTest exten
2617       * thenRunAsync result completes exceptionally if source cancelled
2618       */
2619      public void testThenRunAsync4E() {
2620 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2620 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2621          Noop r = new Noop();
2622          CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2623          assertTrue(f.cancel(true));
# Line 1902 | Line 2628 | public class CompletableFutureTest exten
2628       * thenApplyAsync result completes normally after normal completion of source
2629       */
2630      public void testThenApplyAsyncE() {
2631 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2631 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2632          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2633          f.complete(one);
2634          checkCompletedNormally(g, two);
# Line 1913 | Line 2639 | public class CompletableFutureTest exten
2639       * completion of source
2640       */
2641      public void testThenApplyAsync2E() {
2642 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2642 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2643          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2644          f.completeExceptionally(new CFException());
2645          checkCompletedWithWrappedCFException(g);
# Line 1923 | Line 2649 | public class CompletableFutureTest exten
2649       * thenApplyAsync result completes exceptionally if action does
2650       */
2651      public void testThenApplyAsync3E() {
2652 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2652 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2653          FailingFunction r = new FailingFunction();
2654          CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
2655          f.complete(null);
# Line 1934 | Line 2660 | public class CompletableFutureTest exten
2660       * thenApplyAsync result completes exceptionally if source cancelled
2661       */
2662      public void testThenApplyAsync4E() {
2663 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2663 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2664          CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2665          assertTrue(f.cancel(true));
2666          checkCompletedWithWrappedCancellationException(g);
# Line 1945 | Line 2671 | public class CompletableFutureTest exten
2671       * completion of source
2672       */
2673      public void testThenAcceptAsyncE() {
2674 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2674 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2675          IncAction r = new IncAction();
2676          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2677          f.complete(one);
2678          checkCompletedNormally(g, null);
2679 <        assertEquals(r.value, 2);
2679 >        assertEquals(r.value, (Integer) 2);
2680      }
2681  
2682      /**
# Line 1958 | Line 2684 | public class CompletableFutureTest exten
2684       * completion of source
2685       */
2686      public void testThenAcceptAsync2E() {
2687 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2687 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2688          IncAction r = new IncAction();
2689          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2690          f.completeExceptionally(new CFException());
# Line 1969 | Line 2695 | public class CompletableFutureTest exten
2695       * thenAcceptAsync result completes exceptionally if action does
2696       */
2697      public void testThenAcceptAsync3E() {
2698 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2698 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2699          FailingConsumer r = new FailingConsumer();
2700          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2701          f.complete(null);
# Line 1980 | Line 2706 | public class CompletableFutureTest exten
2706       * thenAcceptAsync result completes exceptionally if source cancelled
2707       */
2708      public void testThenAcceptAsync4E() {
2709 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2709 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2710          IncAction r = new IncAction();
2711          CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2712          assertTrue(f.cancel(true));
# Line 1988 | Line 2714 | public class CompletableFutureTest exten
2714      }
2715  
2716      /**
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    /**
2717       * applyToEitherAsync result completes normally after normal
2718       * completion of sources
2719       */
2720      public void testApplyToEitherAsyncE() {
2721 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2722 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2721 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2722 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2723          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2724          f.complete(one);
2725          checkCompletedNormally(g, two);
2726  
2727 <        f = new CompletableFuture<Integer>();
2727 >        f = new CompletableFuture<>();
2728          f.complete(one);
2729 <        f2 = new CompletableFuture<Integer>();
2729 >        f2 = new CompletableFuture<>();
2730          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2731          checkCompletedNormally(g, two);
2732      }
# Line 2285 | Line 2736 | public class CompletableFutureTest exten
2736       * completion of source
2737       */
2738      public void testApplyToEitherAsync2E() {
2739 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2740 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2739 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2740 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2741          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2742          f.completeExceptionally(new CFException());
2743          checkCompletedWithWrappedCFException(g);
2744  
2745 <        f = new CompletableFuture<Integer>();
2746 <        f2 = new CompletableFuture<Integer>();
2745 >        f = new CompletableFuture<>();
2746 >        f2 = new CompletableFuture<>();
2747          f2.completeExceptionally(new CFException());
2748          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2749          f.complete(one);
# Line 2303 | Line 2754 | public class CompletableFutureTest exten
2754       * applyToEitherAsync result completes exceptionally if action does
2755       */
2756      public void testApplyToEitherAsync3E() {
2757 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2758 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2757 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2758 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2759          FailingFunction r = new FailingFunction();
2760          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2761          f.complete(one);
# Line 2315 | Line 2766 | public class CompletableFutureTest exten
2766       * applyToEitherAsync result completes exceptionally if either source cancelled
2767       */
2768      public void testApplyToEitherAsync4E() {
2769 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2770 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2769 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2770 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2771          CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2772          assertTrue(f.cancel(true));
2773          checkCompletedWithWrappedCancellationException(g);
2774  
2775 <        f = new CompletableFuture<Integer>();
2776 <        f2 = new CompletableFuture<Integer>();
2775 >        f = new CompletableFuture<>();
2776 >        f2 = new CompletableFuture<>();
2777          assertTrue(f2.cancel(true));
2778          g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2779          checkCompletedWithWrappedCancellationException(g);
# Line 2333 | Line 2784 | public class CompletableFutureTest exten
2784       * completion of sources
2785       */
2786      public void testAcceptEitherAsyncE() {
2787 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2788 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2787 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2788 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2789          IncAction r = new IncAction();
2790          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2791          f.complete(one);
2792          checkCompletedNormally(g, null);
2793 <        assertEquals(r.value, 2);
2793 >        assertEquals(r.value, (Integer) 2);
2794  
2795          r = new IncAction();
2796 <        f = new CompletableFuture<Integer>();
2796 >        f = new CompletableFuture<>();
2797          f.complete(one);
2798 <        f2 = new CompletableFuture<Integer>();
2798 >        f2 = new CompletableFuture<>();
2799          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2800          checkCompletedNormally(g, null);
2801 <        assertEquals(r.value, 2);
2801 >        assertEquals(r.value, (Integer) 2);
2802      }
2803  
2804      /**
# Line 2355 | Line 2806 | public class CompletableFutureTest exten
2806       * completion of source
2807       */
2808      public void testAcceptEitherAsync2E() {
2809 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2810 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2809 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2810 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2811          IncAction r = new IncAction();
2812          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2813          f.completeExceptionally(new CFException());
2814          checkCompletedWithWrappedCFException(g);
2815  
2816          r = new IncAction();
2817 <        f = new CompletableFuture<Integer>();
2818 <        f2 = new CompletableFuture<Integer>();
2817 >        f = new CompletableFuture<>();
2818 >        f2 = new CompletableFuture<>();
2819          f2.completeExceptionally(new CFException());
2820          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2821          f.complete(one);
# Line 2375 | Line 2826 | public class CompletableFutureTest exten
2826       * acceptEitherAsync result completes exceptionally if action does
2827       */
2828      public void testAcceptEitherAsync3E() {
2829 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2830 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2829 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2830 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2831          FailingConsumer r = new FailingConsumer();
2832          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2833          f.complete(one);
# Line 2388 | Line 2839 | public class CompletableFutureTest exten
2839       * source cancelled
2840       */
2841      public void testAcceptEitherAsync4E() {
2842 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2843 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2842 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2843 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2844          IncAction r = new IncAction();
2845          CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2846          assertTrue(f.cancel(true));
2847          checkCompletedWithWrappedCancellationException(g);
2848  
2849          r = new IncAction();
2850 <        f = new CompletableFuture<Integer>();
2851 <        f2 = new CompletableFuture<Integer>();
2850 >        f = new CompletableFuture<>();
2851 >        f2 = new CompletableFuture<>();
2852          assertTrue(f2.cancel(true));
2853          g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2854          checkCompletedWithWrappedCancellationException(g);
# Line 2408 | Line 2859 | public class CompletableFutureTest exten
2859       * completion of sources
2860       */
2861      public void testRunAfterEitherAsyncE() {
2862 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2863 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2862 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2863 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2864          Noop r = new Noop();
2865          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2866          f.complete(one);
# Line 2417 | Line 2868 | public class CompletableFutureTest exten
2868          assertTrue(r.ran);
2869  
2870          r = new Noop();
2871 <        f = new CompletableFuture<Integer>();
2871 >        f = new CompletableFuture<>();
2872          f.complete(one);
2873 <        f2 = new CompletableFuture<Integer>();
2873 >        f2 = new CompletableFuture<>();
2874          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2875          checkCompletedNormally(g, null);
2876          assertTrue(r.ran);
# Line 2430 | Line 2881 | public class CompletableFutureTest exten
2881       * completion of source
2882       */
2883      public void testRunAfterEitherAsync2E() {
2884 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2885 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2884 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2885 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2886          Noop r = new Noop();
2887          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2888          f.completeExceptionally(new CFException());
2889          checkCompletedWithWrappedCFException(g);
2890  
2891          r = new Noop();
2892 <        f = new CompletableFuture<Integer>();
2893 <        f2 = new CompletableFuture<Integer>();
2892 >        f = new CompletableFuture<>();
2893 >        f2 = new CompletableFuture<>();
2894          f2.completeExceptionally(new CFException());
2895          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2896          f.complete(one);
# Line 2450 | Line 2901 | public class CompletableFutureTest exten
2901       * runAfterEitherAsync result completes exceptionally if action does
2902       */
2903      public void testRunAfterEitherAsync3E() {
2904 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2905 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2904 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2905 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2906          FailingNoop r = new FailingNoop();
2907          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2908          f.complete(one);
# Line 2463 | Line 2914 | public class CompletableFutureTest exten
2914       * source cancelled
2915       */
2916      public void testRunAfterEitherAsync4E() {
2917 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2918 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2917 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2918 >        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2919          Noop r = new Noop();
2920          CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2921          assertTrue(f.cancel(true));
2922          checkCompletedWithWrappedCancellationException(g);
2923  
2924          r = new Noop();
2925 <        f = new CompletableFuture<Integer>();
2926 <        f2 = new CompletableFuture<Integer>();
2925 >        f = new CompletableFuture<>();
2926 >        f2 = new CompletableFuture<>();
2927          assertTrue(f2.cancel(true));
2928          g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2929          checkCompletedWithWrappedCancellationException(g);
# Line 2483 | Line 2934 | public class CompletableFutureTest exten
2934       * completion of source
2935       */
2936      public void testThenComposeAsyncE() {
2937 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2937 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2938          CompletableFutureInc r = new CompletableFutureInc();
2939          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2940          f.complete(one);
# Line 2495 | Line 2946 | public class CompletableFutureTest exten
2946       * exceptional completion of source
2947       */
2948      public void testThenComposeAsync2E() {
2949 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2949 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2950          CompletableFutureInc r = new CompletableFutureInc();
2951          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2952          f.completeExceptionally(new CFException());
# Line 2506 | Line 2957 | public class CompletableFutureTest exten
2957       * thenComposeAsync result completes exceptionally if action does
2958       */
2959      public void testThenComposeAsync3E() {
2960 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2960 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2961          FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2962          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2963          f.complete(one);
# Line 2517 | Line 2968 | public class CompletableFutureTest exten
2968       * thenComposeAsync result completes exceptionally if source cancelled
2969       */
2970      public void testThenComposeAsync4E() {
2971 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2971 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2972          CompletableFutureInc r = new CompletableFutureInc();
2973          CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2974          assertTrue(f.cancel(true));
# Line 2531 | Line 2982 | public class CompletableFutureTest exten
2982       * with the value null
2983       */
2984      public void testAllOf_empty() throws Exception {
2985 <        CompletableFuture<?> f = CompletableFuture.allOf();
2985 >        CompletableFuture<Void> f = CompletableFuture.allOf();
2986          checkCompletedNormally(f, null);
2987      }
2988  
2989      /**
2990 <     * allOf returns a future completed when all components complete
2990 >     * allOf returns a future completed normally with the value null
2991 >     * when all components complete normally
2992       */
2993 <    public void testAllOf() throws Exception {
2993 >    public void testAllOf_normal() throws Exception {
2994          for (int k = 1; k < 20; ++k) {
2995 <            CompletableFuture[] fs = new CompletableFuture[k];
2995 >            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2996              for (int i = 0; i < k; ++i)
2997 <                fs[i] = new CompletableFuture<Integer>();
2997 >                fs[i] = new CompletableFuture<>();
2998              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2999              for (int i = 0; i < k; ++i) {
3000                  checkIncomplete(f);
3001 +                checkIncomplete(CompletableFuture.allOf(fs));
3002                  fs[i].complete(one);
3003              }
3004              checkCompletedNormally(f, null);
3005 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3006          }
3007      }
3008  
# Line 2556 | Line 3010 | public class CompletableFutureTest exten
3010       * anyOf(no component futures) returns an incomplete future
3011       */
3012      public void testAnyOf_empty() throws Exception {
3013 <        CompletableFuture<?> f = CompletableFuture.anyOf();
3013 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3014          checkIncomplete(f);
3015      }
3016  
3017      /**
3018 <     * anyOf returns a future completed when any components complete
3018 >     * anyOf returns a future completed normally with a value when
3019 >     * a component future does
3020       */
3021 <    public void testAnyOf() throws Exception {
3022 <        for (int k = 1; k < 20; ++k) {
3021 >    public void testAnyOf_normal() throws Exception {
3022 >        for (int k = 0; k < 10; ++k) {
3023              CompletableFuture[] fs = new CompletableFuture[k];
3024              for (int i = 0; i < k; ++i)
3025 <                fs[i] = new CompletableFuture<Integer>();
3025 >                fs[i] = new CompletableFuture<>();
3026              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3027              checkIncomplete(f);
3028              for (int i = 0; i < k; ++i) {
3029                  fs[i].complete(one);
3030                  checkCompletedNormally(f, one);
3031 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3032 +            }
3033 +        }
3034 +    }
3035 +
3036 +    /**
3037 +     * anyOf result completes exceptionally when any component does.
3038 +     */
3039 +    public void testAnyOf_exceptional() throws Exception {
3040 +        for (int k = 0; k < 10; ++k) {
3041 +            CompletableFuture[] fs = new CompletableFuture[k];
3042 +            for (int i = 0; i < k; ++i)
3043 +                fs[i] = new CompletableFuture<>();
3044 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3045 +            checkIncomplete(f);
3046 +            for (int i = 0; i < k; ++i) {
3047 +                fs[i].completeExceptionally(new CFException());
3048 +                checkCompletedWithWrappedCFException(f);
3049 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3050              }
3051          }
3052      }
# Line 2581 | Line 3055 | public class CompletableFutureTest exten
3055       * Completion methods throw NullPointerException with null arguments
3056       */
3057      public void testNPE() {
3058 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
3059 <        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
3058 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3059 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3060          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3061          CompletableFuture<?> h;
3062          ThreadExecutor exec = new ThreadExecutor();
3063  
3064          Runnable[] throwingActions = {
3065 <            () -> { CompletableFuture.supplyAsync(null); },
3066 <            () -> { CompletableFuture.supplyAsync(null, exec); },
3067 <            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
3068 <
3069 <            () -> { CompletableFuture.runAsync(null); },
3070 <            () -> { CompletableFuture.runAsync(null, exec); },
3071 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
3072 <
3073 <            () -> { f.completeExceptionally(null); },
3074 <
3075 <            () -> { f.thenApply(null); },
3076 <            () -> { f.thenApplyAsync(null); },
3077 <            () -> { f.thenApplyAsync((x) -> x, null); },
3078 <            () -> { f.thenApplyAsync(null, exec); },
3079 <
3080 <            () -> { f.thenAccept(null); },
3081 <            () -> { f.thenAcceptAsync(null); },
3082 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
3083 <            () -> { f.thenAcceptAsync(null, exec); },
3084 <
3085 <            () -> { f.thenRun(null); },
3086 <            () -> { f.thenRunAsync(null); },
3087 <            () -> { f.thenRunAsync(() -> { ; }, null); },
3088 <            () -> { f.thenRunAsync(null, exec); },
3089 <
3090 <            () -> { f.thenCombine(g, null); },
3091 <            () -> { f.thenCombineAsync(g, null); },
3092 <            () -> { f.thenCombineAsync(g, null, exec); },
3093 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
3094 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
3095 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
3096 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
3097 <
3098 <            () -> { f.thenAcceptBoth(g, null); },
3099 <            () -> { f.thenAcceptBothAsync(g, null); },
3100 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
3101 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
3102 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
3103 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
3104 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
3105 <
3106 <            () -> { f.runAfterBoth(g, null); },
3107 <            () -> { f.runAfterBothAsync(g, null); },
3108 <            () -> { f.runAfterBothAsync(g, null, exec); },
3109 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
3110 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
3111 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
3112 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
3113 <
3114 <            () -> { f.applyToEither(g, null); },
3115 <            () -> { f.applyToEitherAsync(g, null); },
3116 <            () -> { f.applyToEitherAsync(g, null, exec); },
3117 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
3118 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
3119 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
3120 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
3121 <
3122 <            () -> { f.acceptEither(g, null); },
3123 <            () -> { f.acceptEitherAsync(g, null); },
3124 <            () -> { f.acceptEitherAsync(g, null, exec); },
3125 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
3126 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
3127 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
3128 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3129 <
3130 <            () -> { f.runAfterEither(g, null); },
3131 <            () -> { f.runAfterEitherAsync(g, null); },
3132 <            () -> { f.runAfterEitherAsync(g, null, exec); },
3133 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
3134 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3135 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3136 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3137 <
3138 <            () -> { f.thenCompose(null); },
3139 <            () -> { f.thenComposeAsync(null); },
3140 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3141 <            () -> { f.thenComposeAsync(null, exec); },
3142 <
3143 <            () -> { f.exceptionally(null); },
3144 <
3145 <            () -> { f.handle(null); },
3146 <
3147 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3148 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3149 <            () -> { CompletableFuture.allOf(f, null); },
3150 <            () -> { CompletableFuture.allOf(null, f); },
3151 <
3152 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3153 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3154 <            () -> { CompletableFuture.anyOf(f, null); },
3155 <            () -> { CompletableFuture.anyOf(null, f); },
3065 >            () -> CompletableFuture.supplyAsync(null),
3066 >            () -> CompletableFuture.supplyAsync(null, exec),
3067 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
3068 >
3069 >            () -> CompletableFuture.runAsync(null),
3070 >            () -> CompletableFuture.runAsync(null, exec),
3071 >            () -> CompletableFuture.runAsync(() -> {}, null),
3072 >
3073 >            () -> f.completeExceptionally(null),
3074 >
3075 >            () -> f.thenApply(null),
3076 >            () -> f.thenApplyAsync(null),
3077 >            () -> f.thenApplyAsync((x) -> x, null),
3078 >            () -> f.thenApplyAsync(null, exec),
3079 >
3080 >            () -> f.thenAccept(null),
3081 >            () -> f.thenAcceptAsync(null),
3082 >            () -> f.thenAcceptAsync((x) -> {} , null),
3083 >            () -> f.thenAcceptAsync(null, exec),
3084 >
3085 >            () -> f.thenRun(null),
3086 >            () -> f.thenRunAsync(null),
3087 >            () -> f.thenRunAsync(() -> {} , null),
3088 >            () -> f.thenRunAsync(null, exec),
3089 >
3090 >            () -> f.thenCombine(g, null),
3091 >            () -> f.thenCombineAsync(g, null),
3092 >            () -> f.thenCombineAsync(g, null, exec),
3093 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3094 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3095 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3096 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3097 >
3098 >            () -> f.thenAcceptBoth(g, null),
3099 >            () -> f.thenAcceptBothAsync(g, null),
3100 >            () -> f.thenAcceptBothAsync(g, null, exec),
3101 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3102 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3103 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3104 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3105 >
3106 >            () -> f.runAfterBoth(g, null),
3107 >            () -> f.runAfterBothAsync(g, null),
3108 >            () -> f.runAfterBothAsync(g, null, exec),
3109 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3110 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3111 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3112 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3113 >
3114 >            () -> f.applyToEither(g, null),
3115 >            () -> f.applyToEitherAsync(g, null),
3116 >            () -> f.applyToEitherAsync(g, null, exec),
3117 >            () -> f.applyToEither(nullFuture, (x) -> x),
3118 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3119 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3120 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3121 >
3122 >            () -> f.acceptEither(g, null),
3123 >            () -> f.acceptEitherAsync(g, null),
3124 >            () -> f.acceptEitherAsync(g, null, exec),
3125 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3126 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3127 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3128 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3129 >
3130 >            () -> f.runAfterEither(g, null),
3131 >            () -> f.runAfterEitherAsync(g, null),
3132 >            () -> f.runAfterEitherAsync(g, null, exec),
3133 >            () -> f.runAfterEither(nullFuture, () -> {}),
3134 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3135 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3136 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3137 >
3138 >            () -> f.thenCompose(null),
3139 >            () -> f.thenComposeAsync(null),
3140 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
3141 >            () -> f.thenComposeAsync(null, exec),
3142 >
3143 >            () -> f.exceptionally(null),
3144 >
3145 >            () -> f.handle(null),
3146 >
3147 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3148 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3149 >            () -> CompletableFuture.allOf(f, null),
3150 >            () -> CompletableFuture.allOf(null, f),
3151 >
3152 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3153 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3154 >            () -> CompletableFuture.anyOf(f, null),
3155 >            () -> CompletableFuture.anyOf(null, f),
3156 >
3157 >            () -> f.obtrudeException(null),
3158          };
3159  
3160          assertThrows(NullPointerException.class, throwingActions);
3161          assertEquals(0, exec.count.get());
3162      }
3163  
3164 +    /**
3165 +     * toCompletableFuture returns this CompletableFuture.
3166 +     */
3167 +    public void testToCompletableFuture() {
3168 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3169 +        assertSame(f, f.toCompletableFuture());
3170 +    }
3171 +
3172 +    /**
3173 +     * whenComplete action executes on normal completion, propagating
3174 +     * source result.
3175 +     */
3176 +    public void testWhenComplete1() {
3177 +        final AtomicInteger a = new AtomicInteger();
3178 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3179 +        CompletableFuture<Integer> g =
3180 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3181 +        f.complete(three);
3182 +        checkCompletedNormally(f, three);
3183 +        checkCompletedNormally(g, three);
3184 +        assertEquals(a.get(), 1);
3185 +    }
3186 +
3187 +    /**
3188 +     * whenComplete action executes on exceptional completion, propagating
3189 +     * source result.
3190 +     */
3191 +    public void testWhenComplete2() {
3192 +        final AtomicInteger a = new AtomicInteger();
3193 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3194 +        CompletableFuture<Integer> g =
3195 +            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3196 +        f.completeExceptionally(new CFException());
3197 +        assertTrue(f.isCompletedExceptionally());
3198 +        assertTrue(g.isCompletedExceptionally());
3199 +        assertEquals(a.get(), 1);
3200 +    }
3201 +
3202 +    /**
3203 +     * If a whenComplete action throws an exception when triggered by
3204 +     * a normal completion, it completes exceptionally
3205 +     */
3206 +    public void testWhenComplete3() {
3207 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3208 +        CompletableFuture<Integer> g =
3209 +            f.whenComplete((Integer x, Throwable t) ->
3210 +                           { throw new CFException(); } );
3211 +        f.complete(three);
3212 +        checkCompletedNormally(f, three);
3213 +        assertTrue(g.isCompletedExceptionally());
3214 +        checkCompletedWithWrappedCFException(g);
3215 +    }
3216 +
3217 +    /**
3218 +     * whenCompleteAsync action executes on normal completion, propagating
3219 +     * source result.
3220 +     */
3221 +    public void testWhenCompleteAsync1() {
3222 +        final AtomicInteger a = new AtomicInteger();
3223 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 +        CompletableFuture<Integer> g =
3225 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3226 +        f.complete(three);
3227 +        checkCompletedNormally(f, three);
3228 +        checkCompletedNormally(g, three);
3229 +        assertEquals(a.get(), 1);
3230 +    }
3231 +
3232 +    /**
3233 +     * whenCompleteAsync action executes on exceptional completion, propagating
3234 +     * source result.
3235 +     */
3236 +    public void testWhenCompleteAsync2() {
3237 +        final AtomicInteger a = new AtomicInteger();
3238 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3239 +        CompletableFuture<Integer> g =
3240 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3241 +        f.completeExceptionally(new CFException());
3242 +        checkCompletedWithWrappedCFException(f);
3243 +        checkCompletedWithWrappedCFException(g);
3244 +    }
3245 +
3246 +    /**
3247 +     * If a whenCompleteAsync action throws an exception when
3248 +     * triggered by a normal completion, it completes exceptionally
3249 +     */
3250 +    public void testWhenCompleteAsync3() {
3251 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3252 +        CompletableFuture<Integer> g =
3253 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3254 +                           { throw new CFException(); } );
3255 +        f.complete(three);
3256 +        checkCompletedNormally(f, three);
3257 +        checkCompletedWithWrappedCFException(g);
3258 +    }
3259 +
3260 +    /**
3261 +     * whenCompleteAsync action executes on normal completion, propagating
3262 +     * source result.
3263 +     */
3264 +    public void testWhenCompleteAsync1e() {
3265 +        final AtomicInteger a = new AtomicInteger();
3266 +        ThreadExecutor exec = new ThreadExecutor();
3267 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3268 +        CompletableFuture<Integer> g =
3269 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3270 +                                exec);
3271 +        f.complete(three);
3272 +        checkCompletedNormally(f, three);
3273 +        checkCompletedNormally(g, three);
3274 +        assertEquals(a.get(), 1);
3275 +    }
3276 +
3277 +    /**
3278 +     * whenCompleteAsync action executes on exceptional completion, propagating
3279 +     * source result.
3280 +     */
3281 +    public void testWhenCompleteAsync2e() {
3282 +        final AtomicInteger a = new AtomicInteger();
3283 +        ThreadExecutor exec = new ThreadExecutor();
3284 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3285 +        CompletableFuture<Integer> g =
3286 +            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3287 +                                exec);
3288 +        f.completeExceptionally(new CFException());
3289 +        checkCompletedWithWrappedCFException(f);
3290 +        checkCompletedWithWrappedCFException(g);
3291 +    }
3292 +
3293 +    /**
3294 +     * If a whenCompleteAsync action throws an exception when triggered
3295 +     * by a normal completion, it completes exceptionally
3296 +     */
3297 +    public void testWhenCompleteAsync3e() {
3298 +        ThreadExecutor exec = new ThreadExecutor();
3299 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3300 +        CompletableFuture<Integer> g =
3301 +            f.whenCompleteAsync((Integer x, Throwable t) ->
3302 +                                { throw new CFException(); },
3303 +                                exec);
3304 +        f.complete(three);
3305 +        checkCompletedNormally(f, three);
3306 +        checkCompletedWithWrappedCFException(g);
3307 +    }
3308 +
3309 +    /**
3310 +     * handleAsync action completes normally with function value on
3311 +     * either normal or exceptional completion of source
3312 +     */
3313 +    public void testHandleAsync() {
3314 +        CompletableFuture<Integer> f, g;
3315 +        IntegerHandler r;
3316 +
3317 +        f = new CompletableFuture<>();
3318 +        g = f.handleAsync(r = new IntegerHandler());
3319 +        assertFalse(r.ran);
3320 +        f.completeExceptionally(new CFException());
3321 +        checkCompletedWithWrappedCFException(f);
3322 +        checkCompletedNormally(g, three);
3323 +        assertTrue(r.ran);
3324 +
3325 +        f = new CompletableFuture<>();
3326 +        g = f.handleAsync(r = new IntegerHandler());
3327 +        assertFalse(r.ran);
3328 +        f.completeExceptionally(new CFException());
3329 +        checkCompletedWithWrappedCFException(f);
3330 +        checkCompletedNormally(g, three);
3331 +        assertTrue(r.ran);
3332 +
3333 +        f = new CompletableFuture<>();
3334 +        g = f.handleAsync(r = new IntegerHandler());
3335 +        assertFalse(r.ran);
3336 +        f.complete(one);
3337 +        checkCompletedNormally(f, one);
3338 +        checkCompletedNormally(g, two);
3339 +        assertTrue(r.ran);
3340 +
3341 +        f = new CompletableFuture<>();
3342 +        g = f.handleAsync(r = new IntegerHandler());
3343 +        assertFalse(r.ran);
3344 +        f.complete(one);
3345 +        checkCompletedNormally(f, one);
3346 +        checkCompletedNormally(g, two);
3347 +        assertTrue(r.ran);
3348 +    }
3349 +
3350 +    /**
3351 +     * handleAsync action with Executor completes normally with
3352 +     * function value on either normal or exceptional completion of
3353 +     * source
3354 +     */
3355 +    public void testHandleAsync2() {
3356 +        CompletableFuture<Integer> f, g;
3357 +        ThreadExecutor exec = new ThreadExecutor();
3358 +        IntegerHandler r;
3359 +
3360 +        f = new CompletableFuture<>();
3361 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3362 +        assertFalse(r.ran);
3363 +        f.completeExceptionally(new CFException());
3364 +        checkCompletedWithWrappedCFException(f);
3365 +        checkCompletedNormally(g, three);
3366 +        assertTrue(r.ran);
3367 +
3368 +        f = new CompletableFuture<>();
3369 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3370 +        assertFalse(r.ran);
3371 +        f.completeExceptionally(new CFException());
3372 +        checkCompletedWithWrappedCFException(f);
3373 +        checkCompletedNormally(g, three);
3374 +        assertTrue(r.ran);
3375 +
3376 +        f = new CompletableFuture<>();
3377 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3378 +        assertFalse(r.ran);
3379 +        f.complete(one);
3380 +        checkCompletedNormally(f, one);
3381 +        checkCompletedNormally(g, two);
3382 +        assertTrue(r.ran);
3383 +
3384 +        f = new CompletableFuture<>();
3385 +        g = f.handleAsync(r = new IntegerHandler(), exec);
3386 +        assertFalse(r.ran);
3387 +        f.complete(one);
3388 +        checkCompletedNormally(f, one);
3389 +        checkCompletedNormally(g, two);
3390 +        assertTrue(r.ran);
3391 +    }
3392 +
3393   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines