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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines