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.15 by jsr166, Wed Apr 3 16:59:57 2013 UTC vs.
Revision 1.55 by jsr166, Mon Jun 2 21:41:37 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);
287 <        CompletableFuture g = f.thenRun(new Noop());
288 <        assertEquals(f.getNumberOfDependents(), 1);
289 <        assertEquals(g.getNumberOfDependents(), 0);
290 <        CompletableFuture h = f.thenRun(new Noop());
291 <        assertEquals(f.getNumberOfDependents(), 2);
285 >        CompletableFuture<Integer> f = new CompletableFuture<>();
286 >        assertEquals(0, f.getNumberOfDependents());
287 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
288 >        assertEquals(1, f.getNumberOfDependents());
289 >        assertEquals(0, g.getNumberOfDependents());
290 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
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 >        final ExecutionMode m;
379 >        int invocationCount = 0;
380 >        Noop(ExecutionMode m) { this.m = m; }
381 >        public void run() {
382 >            m.checkExecutionMode();
383 >            invocationCount++;
384 >        }
385      }
386  
387      static final class FailingSupplier implements Supplier<Integer> {
388 <        boolean ran;
389 <        public Integer get() { ran = true; throw new CFException(); }
388 >        int invocationCount = 0;
389 >        public Integer get() {
390 >            invocationCount++;
391 >            throw new CFException();
392 >        }
393      }
394      static final class FailingConsumer implements Consumer<Integer> {
395 <        boolean ran;
396 <        public void accept(Integer x) { ran = true; throw new CFException(); }
395 >        int invocationCount = 0;
396 >        public void accept(Integer x) {
397 >            invocationCount++;
398 >            throw new CFException();
399 >        }
400      }
401      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
402 <        boolean ran;
403 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
402 >        int invocationCount = 0;
403 >        public void accept(Integer x, Integer y) {
404 >            invocationCount++;
405 >            throw new CFException();
406 >        }
407      }
408      static final class FailingFunction implements Function<Integer, Integer> {
409 <        boolean ran;
410 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
409 >        int invocationCount = 0;
410 >        public Integer apply(Integer x) {
411 >            invocationCount++;
412 >            throw new CFException();
413 >        }
414      }
415      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
416 <        boolean ran;
417 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
416 >        int invocationCount = 0;
417 >        public Integer apply(Integer x, Integer y) {
418 >            invocationCount++;
419 >            throw new CFException();
420 >        }
421      }
422 <    static final class FailingNoop implements Runnable {
423 <        boolean ran;
424 <        public void run() { ran = true; throw new CFException(); }
422 >    static final class FailingRunnable implements Runnable {
423 >        int invocationCount = 0;
424 >        public void run() {
425 >            invocationCount++;
426 >            throw new CFException();
427 >        }
428      }
429  
430 <    static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
430 >    static final class CompletableFutureInc
431 >        implements Function<Integer, CompletableFuture<Integer>> {
432 >        int invocationCount = 0;
433          public CompletableFuture<Integer> apply(Integer x) {
434 <            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
435 <            f.complete(Integer.valueOf(x.intValue() + 1));
434 >            invocationCount++;
435 >            CompletableFuture<Integer> f = new CompletableFuture<>();
436 >            f.complete(inc(x));
437              return f;
438          }
439      }
440  
441 <    static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
442 <        boolean ran;
441 >    static final class FailingCompletableFutureFunction
442 >        implements Function<Integer, CompletableFuture<Integer>> {
443 >        int invocationCount = 0;
444          public CompletableFuture<Integer> apply(Integer x) {
445 <            ran = true; throw new CFException();
445 >            invocationCount++;
446 >            throw new CFException();
447          }
448      }
449  
450      // Used for explicit executor tests
451      static final class ThreadExecutor implements Executor {
452 +        AtomicInteger count = new AtomicInteger(0);
453 +
454          public void execute(Runnable r) {
455 +            count.getAndIncrement();
456              new Thread(r).start();
457          }
458      }
459  
460 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
461 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
462 <    }
460 >    /**
461 >     * Permits the testing of parallel code for the 3 different
462 >     * execution modes without repeating all the testing code.
463 >     */
464 >    enum ExecutionMode {
465 >        DEFAULT {
466 >            public void checkExecutionMode() {
467 >                assertNull(ForkJoinTask.getPool());
468 >            }
469 >            public <T> CompletableFuture<Void> thenRun
470 >                (CompletableFuture<T> f, Runnable a) {
471 >                return f.thenRun(a);
472 >            }
473 >            public <T> CompletableFuture<Void> thenAccept
474 >                (CompletableFuture<T> f, Consumer<? super T> a) {
475 >                return f.thenAccept(a);
476 >            }
477 >            public <T,U> CompletableFuture<U> thenApply
478 >                (CompletableFuture<T> f, Function<? super T,U> a) {
479 >                return f.thenApply(a);
480 >            }
481 >            public <T,U> CompletableFuture<U> thenCompose
482 >                (CompletableFuture<T> f,
483 >                 Function<? super T,? extends CompletionStage<U>> a) {
484 >                return f.thenCompose(a);
485 >            }
486 >            public <T,U> CompletableFuture<U> handle
487 >                (CompletableFuture<T> f,
488 >                 BiFunction<? super T,Throwable,? extends U> a) {
489 >                return f.handle(a);
490 >            }
491 >            public <T> CompletableFuture<T> whenComplete
492 >                (CompletableFuture<T> f,
493 >                 BiConsumer<? super T,? super Throwable> a) {
494 >                return f.whenComplete(a);
495 >            }
496 >            public <T,U> CompletableFuture<Void> runAfterBoth
497 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
498 >                return f.runAfterBoth(g, a);
499 >            }
500 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
501 >                (CompletableFuture<T> f,
502 >                 CompletionStage<? extends U> g,
503 >                 BiConsumer<? super T,? super U> a) {
504 >                return f.thenAcceptBoth(g, a);
505 >            }
506 >            public <T,U,V> CompletableFuture<V> thenCombine
507 >                (CompletableFuture<T> f,
508 >                 CompletionStage<? extends U> g,
509 >                 BiFunction<? super T,? super U,? extends V> a) {
510 >                return f.thenCombine(g, a);
511 >            }
512 >            public <T> CompletableFuture<Void> runAfterEither
513 >                (CompletableFuture<T> f,
514 >                 CompletionStage<?> g,
515 >                 java.lang.Runnable a) {
516 >                return f.runAfterEither(g, a);
517 >            }
518 >            public <T> CompletableFuture<Void> acceptEither
519 >                (CompletableFuture<T> f,
520 >                 CompletionStage<? extends T> g,
521 >                 Consumer<? super T> a) {
522 >                return f.acceptEither(g, a);
523 >            }
524 >            public <T,U> CompletableFuture<U> applyToEither
525 >                (CompletableFuture<T> f,
526 >                 CompletionStage<? extends T> g,
527 >                 Function<? super T,U> a) {
528 >                return f.applyToEither(g, a);
529 >            }
530 >        },
531  
532 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
533 <        boolean ran;
534 <        public Integer apply(Integer x, Throwable t) {
535 <            ran = true;
536 <            return (t == null) ? two : three;
537 <        }
532 >        ASYNC {
533 >            public void checkExecutionMode() {
534 >                assertSame(ForkJoinPool.commonPool(),
535 >                           ForkJoinTask.getPool());
536 >            }
537 >            public <T> CompletableFuture<Void> thenRun
538 >                (CompletableFuture<T> f, Runnable a) {
539 >                return f.thenRunAsync(a);
540 >            }
541 >            public <T> CompletableFuture<Void> thenAccept
542 >                (CompletableFuture<T> f, Consumer<? super T> a) {
543 >                return f.thenAcceptAsync(a);
544 >            }
545 >            public <T,U> CompletableFuture<U> thenApply
546 >                (CompletableFuture<T> f, Function<? super T,U> a) {
547 >                return f.thenApplyAsync(a);
548 >            }
549 >            public <T,U> CompletableFuture<U> thenCompose
550 >                (CompletableFuture<T> f,
551 >                 Function<? super T,? extends CompletionStage<U>> a) {
552 >                return f.thenComposeAsync(a);
553 >            }
554 >            public <T,U> CompletableFuture<U> handle
555 >                (CompletableFuture<T> f,
556 >                 BiFunction<? super T,Throwable,? extends U> a) {
557 >                return f.handleAsync(a);
558 >            }
559 >            public <T> CompletableFuture<T> whenComplete
560 >                (CompletableFuture<T> f,
561 >                 BiConsumer<? super T,? super Throwable> a) {
562 >                return f.whenCompleteAsync(a);
563 >            }
564 >            public <T,U> CompletableFuture<Void> runAfterBoth
565 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
566 >                return f.runAfterBothAsync(g, a);
567 >            }
568 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
569 >                (CompletableFuture<T> f,
570 >                 CompletionStage<? extends U> g,
571 >                 BiConsumer<? super T,? super U> a) {
572 >                return f.thenAcceptBothAsync(g, a);
573 >            }
574 >            public <T,U,V> CompletableFuture<V> thenCombine
575 >                (CompletableFuture<T> f,
576 >                 CompletionStage<? extends U> g,
577 >                 BiFunction<? super T,? super U,? extends V> a) {
578 >                return f.thenCombineAsync(g, a);
579 >            }
580 >            public <T> CompletableFuture<Void> runAfterEither
581 >                (CompletableFuture<T> f,
582 >                 CompletionStage<?> g,
583 >                 java.lang.Runnable a) {
584 >                return f.runAfterEitherAsync(g, a);
585 >            }
586 >            public <T> CompletableFuture<Void> acceptEither
587 >                (CompletableFuture<T> f,
588 >                 CompletionStage<? extends T> g,
589 >                 Consumer<? super T> a) {
590 >                return f.acceptEitherAsync(g, a);
591 >            }
592 >            public <T,U> CompletableFuture<U> applyToEither
593 >                (CompletableFuture<T> f,
594 >                 CompletionStage<? extends T> g,
595 >                 Function<? super T,U> a) {
596 >                return f.applyToEitherAsync(g, a);
597 >            }
598 >        },
599 >
600 >        EXECUTOR {
601 >            public void checkExecutionMode() {
602 >                //TODO
603 >            }
604 >            public <T> CompletableFuture<Void> thenRun
605 >                (CompletableFuture<T> f, Runnable a) {
606 >                return f.thenRunAsync(a, new ThreadExecutor());
607 >            }
608 >            public <T> CompletableFuture<Void> thenAccept
609 >                (CompletableFuture<T> f, Consumer<? super T> a) {
610 >                return f.thenAcceptAsync(a, new ThreadExecutor());
611 >            }
612 >            public <T,U> CompletableFuture<U> thenApply
613 >                (CompletableFuture<T> f, Function<? super T,U> a) {
614 >                return f.thenApplyAsync(a, new ThreadExecutor());
615 >            }
616 >            public <T,U> CompletableFuture<U> thenCompose
617 >                (CompletableFuture<T> f,
618 >                 Function<? super T,? extends CompletionStage<U>> a) {
619 >                return f.thenComposeAsync(a, new ThreadExecutor());
620 >            }
621 >            public <T,U> CompletableFuture<U> handle
622 >                (CompletableFuture<T> f,
623 >                 BiFunction<? super T,Throwable,? extends U> a) {
624 >                return f.handleAsync(a, new ThreadExecutor());
625 >            }
626 >            public <T> CompletableFuture<T> whenComplete
627 >                (CompletableFuture<T> f,
628 >                 BiConsumer<? super T,? super Throwable> a) {
629 >                return f.whenCompleteAsync(a, new ThreadExecutor());
630 >            }
631 >            public <T,U> CompletableFuture<Void> runAfterBoth
632 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
633 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
634 >            }
635 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
636 >                (CompletableFuture<T> f,
637 >                 CompletionStage<? extends U> g,
638 >                 BiConsumer<? super T,? super U> a) {
639 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
640 >            }
641 >            public <T,U,V> CompletableFuture<V> thenCombine
642 >                (CompletableFuture<T> f,
643 >                 CompletionStage<? extends U> g,
644 >                 BiFunction<? super T,? super U,? extends V> a) {
645 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
646 >            }
647 >            public <T> CompletableFuture<Void> runAfterEither
648 >                (CompletableFuture<T> f,
649 >                 CompletionStage<?> g,
650 >                 java.lang.Runnable a) {
651 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
652 >            }
653 >            public <T> CompletableFuture<Void> acceptEither
654 >                (CompletableFuture<T> f,
655 >                 CompletionStage<? extends T> g,
656 >                 Consumer<? super T> a) {
657 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
658 >            }
659 >            public <T,U> CompletableFuture<U> applyToEither
660 >                (CompletableFuture<T> f,
661 >                 CompletionStage<? extends T> g,
662 >                 Function<? super T,U> a) {
663 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
664 >            }
665 >        };
666 >
667 >        public abstract void checkExecutionMode();
668 >        public abstract <T> CompletableFuture<Void> thenRun
669 >            (CompletableFuture<T> f, Runnable a);
670 >        public abstract <T> CompletableFuture<Void> thenAccept
671 >            (CompletableFuture<T> f, Consumer<? super T> a);
672 >        public abstract <T,U> CompletableFuture<U> thenApply
673 >            (CompletableFuture<T> f, Function<? super T,U> a);
674 >        public abstract <T,U> CompletableFuture<U> thenCompose
675 >            (CompletableFuture<T> f,
676 >             Function<? super T,? extends CompletionStage<U>> a);
677 >        public abstract <T,U> CompletableFuture<U> handle
678 >            (CompletableFuture<T> f,
679 >             BiFunction<? super T,Throwable,? extends U> a);
680 >        public abstract <T> CompletableFuture<T> whenComplete
681 >            (CompletableFuture<T> f,
682 >             BiConsumer<? super T,? super Throwable> a);
683 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
684 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
685 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
686 >            (CompletableFuture<T> f,
687 >             CompletionStage<? extends U> g,
688 >             BiConsumer<? super T,? super U> a);
689 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
690 >            (CompletableFuture<T> f,
691 >             CompletionStage<? extends U> g,
692 >             BiFunction<? super T,? super U,? extends V> a);
693 >        public abstract <T> CompletableFuture<Void> runAfterEither
694 >            (CompletableFuture<T> f,
695 >             CompletionStage<?> g,
696 >             java.lang.Runnable a);
697 >        public abstract <T> CompletableFuture<Void> acceptEither
698 >            (CompletableFuture<T> f,
699 >             CompletionStage<? extends T> g,
700 >             Consumer<? super T> a);
701 >        public abstract <T,U> CompletableFuture<U> applyToEither
702 >            (CompletableFuture<T> f,
703 >             CompletionStage<? extends T> g,
704 >             Function<? super T,U> a);
705      }
706  
707 +    /**
708 +     * exceptionally action is not invoked when source completes
709 +     * normally, and source result is propagated
710 +     */
711 +    public void testExceptionally_normalCompletion() {
712 +        for (boolean createIncomplete : new boolean[] { true, false })
713 +        for (Integer v1 : new Integer[] { 1, null })
714 +    {
715 +        final AtomicInteger a = new AtomicInteger(0);
716 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
717 +        if (!createIncomplete) f.complete(v1);
718 +        final CompletableFuture<Integer> g = f.exceptionally
719 +            ((Throwable t) -> {
720 +                // Should not be called
721 +                a.getAndIncrement();
722 +                throw new AssertionError();
723 +            });
724 +        if (createIncomplete) f.complete(v1);
725 +
726 +        checkCompletedNormally(g, v1);
727 +        checkCompletedNormally(f, v1);
728 +        assertEquals(0, a.get());
729 +    }}
730 +
731  
732      /**
733       * exceptionally action completes with function value on source
734 <     * exception;  otherwise with source value
734 >     * exception
735       */
736 <    public void testExceptionally() {
737 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
738 <        ExceptionToInteger r = new ExceptionToInteger();
739 <        CompletableFuture<Integer> g = f.exceptionally(r);
740 <        f.completeExceptionally(new CFException());
741 <        checkCompletedNormally(g, three);
742 <
743 <        f = new CompletableFuture<Integer>();
744 <        r = new ExceptionToInteger();
745 <        g = f.exceptionally(r);
746 <        f.complete(one);
747 <        checkCompletedNormally(g, one);
748 <    }
736 >    public void testExceptionally_exceptionalCompletion() {
737 >        for (boolean createIncomplete : new boolean[] { true, false })
738 >        for (Integer v1 : new Integer[] { 1, null })
739 >    {
740 >        final AtomicInteger a = new AtomicInteger(0);
741 >        final CFException ex = new CFException();
742 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
743 >        if (!createIncomplete) f.completeExceptionally(ex);
744 >        final CompletableFuture<Integer> g = f.exceptionally
745 >            ((Throwable t) -> {
746 >                threadAssertSame(t, ex);
747 >                a.getAndIncrement();
748 >                return v1;
749 >            });
750 >        if (createIncomplete) f.completeExceptionally(ex);
751 >
752 >        checkCompletedNormally(g, v1);
753 >        assertEquals(1, a.get());
754 >    }}
755 >
756 >    public void testExceptionally_exceptionalCompletionActionFailed() {
757 >        for (boolean createIncomplete : new boolean[] { true, false })
758 >        for (Integer v1 : new Integer[] { 1, null })
759 >    {
760 >        final AtomicInteger a = new AtomicInteger(0);
761 >        final CFException ex1 = new CFException();
762 >        final CFException ex2 = new CFException();
763 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
764 >        if (!createIncomplete) f.completeExceptionally(ex1);
765 >        final CompletableFuture<Integer> g = f.exceptionally
766 >            ((Throwable t) -> {
767 >                threadAssertSame(t, ex1);
768 >                a.getAndIncrement();
769 >                throw ex2;
770 >            });
771 >        if (createIncomplete) f.completeExceptionally(ex1);
772 >
773 >        checkCompletedWithWrappedCFException(g, ex2);
774 >        assertEquals(1, a.get());
775 >    }}
776 >
777 >    /**
778 >     * handle action completes normally with function value on normal
779 >     * completion of source
780 >     */
781 >    public void testHandle_normalCompletion() {
782 >        for (ExecutionMode m : ExecutionMode.values())
783 >        for (boolean createIncomplete : new boolean[] { true, false })
784 >        for (Integer v1 : new Integer[] { 1, null })
785 >    {
786 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
787 >        final AtomicInteger a = new AtomicInteger(0);
788 >        if (!createIncomplete) f.complete(v1);
789 >        final CompletableFuture<Integer> g = m.handle
790 >            (f,
791 >             (Integer x, Throwable t) -> {
792 >                threadAssertSame(x, v1);
793 >                threadAssertNull(t);
794 >                a.getAndIncrement();
795 >                return inc(v1);
796 >            });
797 >        if (createIncomplete) f.complete(v1);
798 >
799 >        checkCompletedNormally(g, inc(v1));
800 >        checkCompletedNormally(f, v1);
801 >        assertEquals(1, a.get());
802 >    }}
803  
804      /**
805 <     * handle action completes normally with function value on either
806 <     * normal or exceptional completion of source
805 >     * handle action completes normally with function value on
806 >     * exceptional completion of source
807       */
808 <    public void testHandle() {
809 <        CompletableFuture<Integer> f, g;
810 <        IntegerHandler r;
808 >    public void testHandle_exceptionalCompletion() {
809 >        for (ExecutionMode m : ExecutionMode.values())
810 >        for (boolean createIncomplete : new boolean[] { true, false })
811 >        for (Integer v1 : new Integer[] { 1, null })
812 >    {
813 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
814 >        final AtomicInteger a = new AtomicInteger(0);
815 >        final CFException ex = new CFException();
816 >        if (!createIncomplete) f.completeExceptionally(ex);
817 >        final CompletableFuture<Integer> g = m.handle
818 >            (f,
819 >             (Integer x, Throwable t) -> {
820 >                threadAssertNull(x);
821 >                threadAssertSame(t, ex);
822 >                a.getAndIncrement();
823 >                return v1;
824 >            });
825 >        if (createIncomplete) f.completeExceptionally(ex);
826 >
827 >        checkCompletedNormally(g, v1);
828 >        checkCompletedWithWrappedCFException(f, ex);
829 >        assertEquals(1, a.get());
830 >    }}
831 >
832 >    /**
833 >     * handle action completes normally with function value on
834 >     * cancelled source
835 >     */
836 >    public void testHandle_sourceCancelled() {
837 >        for (ExecutionMode m : ExecutionMode.values())
838 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
839 >        for (boolean createIncomplete : new boolean[] { true, false })
840 >        for (Integer v1 : new Integer[] { 1, null })
841 >    {
842 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
843 >        final AtomicInteger a = new AtomicInteger(0);
844 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
845 >        final CompletableFuture<Integer> g = m.handle
846 >            (f,
847 >             (Integer x, Throwable t) -> {
848 >                threadAssertNull(x);
849 >                threadAssertTrue(t instanceof CancellationException);
850 >                a.getAndIncrement();
851 >                return v1;
852 >            });
853 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
854  
855 <        f = new CompletableFuture<Integer>();
856 <        f.completeExceptionally(new CFException());
857 <        g = f.handle(r = new IntegerHandler());
858 <        assertTrue(r.ran);
396 <        checkCompletedNormally(g, three);
397 <
398 <        f = new CompletableFuture<Integer>();
399 <        g = f.handle(r = new IntegerHandler());
400 <        assertFalse(r.ran);
401 <        f.completeExceptionally(new CFException());
402 <        checkCompletedNormally(g, three);
403 <        assertTrue(r.ran);
855 >        checkCompletedNormally(g, v1);
856 >        checkCancelled(f);
857 >        assertEquals(1, a.get());
858 >    }}
859  
860 <        f = new CompletableFuture<Integer>();
861 <        f.complete(one);
862 <        g = f.handle(r = new IntegerHandler());
863 <        assertTrue(r.ran);
864 <        checkCompletedNormally(g, two);
865 <
866 <        f = new CompletableFuture<Integer>();
867 <        g = f.handle(r = new IntegerHandler());
868 <        assertFalse(r.ran);
869 <        f.complete(one);
870 <        assertTrue(r.ran);
871 <        checkCompletedNormally(g, two);
872 <    }
860 >    /**
861 >     * handle result completes exceptionally if action does
862 >     */
863 >    public void testHandle_sourceFailedActionFailed() {
864 >        for (ExecutionMode m : ExecutionMode.values())
865 >        for (boolean createIncomplete : new boolean[] { true, false })
866 >    {
867 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
868 >        final AtomicInteger a = new AtomicInteger(0);
869 >        final CFException ex1 = new CFException();
870 >        final CFException ex2 = new CFException();
871 >        if (!createIncomplete) f.completeExceptionally(ex1);
872 >        final CompletableFuture<Integer> g = m.handle
873 >            (f,
874 >             (Integer x, Throwable t) -> {
875 >                threadAssertNull(x);
876 >                threadAssertSame(ex1, t);
877 >                a.getAndIncrement();
878 >                throw ex2;
879 >            });
880 >        if (createIncomplete) f.completeExceptionally(ex1);
881 >
882 >        checkCompletedWithWrappedCFException(g, ex2);
883 >        checkCompletedWithWrappedCFException(f, ex1);
884 >        assertEquals(1, a.get());
885 >    }}
886 >
887 >    public void testHandle_sourceCompletedNormallyActionFailed() {
888 >        for (ExecutionMode m : ExecutionMode.values())
889 >        for (boolean createIncomplete : new boolean[] { true, false })
890 >        for (Integer v1 : new Integer[] { 1, null })
891 >    {
892 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
893 >        final AtomicInteger a = new AtomicInteger(0);
894 >        final CFException ex = new CFException();
895 >        if (!createIncomplete) f.complete(v1);
896 >        final CompletableFuture<Integer> g = m.handle
897 >            (f,
898 >             (Integer x, Throwable t) -> {
899 >                threadAssertSame(x, v1);
900 >                threadAssertNull(t);
901 >                a.getAndIncrement();
902 >                throw ex;
903 >            });
904 >        if (createIncomplete) f.complete(v1);
905 >
906 >        checkCompletedWithWrappedCFException(g, ex);
907 >        checkCompletedNormally(f, v1);
908 >        assertEquals(1, a.get());
909 >    }}
910  
911      /**
912       * runAsync completes after running Runnable
913       */
914      public void testRunAsync() {
915 <        Noop r = new Noop();
915 >        Noop r = new Noop(ExecutionMode.ASYNC);
916          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
917          assertNull(f.join());
918 <        assertTrue(r.ran);
918 >        assertEquals(1, r.invocationCount);
919          checkCompletedNormally(f, null);
920      }
921  
# Line 431 | Line 923 | public class CompletableFutureTest exten
923       * runAsync with executor completes after running Runnable
924       */
925      public void testRunAsync2() {
926 <        Noop r = new Noop();
927 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
926 >        Noop r = new Noop(ExecutionMode.EXECUTOR);
927 >        ThreadExecutor exec = new ThreadExecutor();
928 >        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
929          assertNull(f.join());
930 <        assertTrue(r.ran);
930 >        assertEquals(1, r.invocationCount);
931          checkCompletedNormally(f, null);
932 +        assertEquals(1, exec.count.get());
933      }
934  
935      /**
936       * failing runAsync completes exceptionally after running Runnable
937       */
938      public void testRunAsync3() {
939 <        FailingNoop r = new FailingNoop();
939 >        FailingRunnable r = new FailingRunnable();
940          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
941          checkCompletedWithWrappedCFException(f);
942 <        assertTrue(r.ran);
942 >        assertEquals(1, r.invocationCount);
943      }
944  
945      /**
946       * supplyAsync completes with result of supplier
947       */
948      public void testSupplyAsync() {
949 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
949 >        CompletableFuture<Integer> f;
950 >        f = CompletableFuture.supplyAsync(supplyOne);
951          assertEquals(f.join(), one);
952 +        checkCompletedNormally(f, one);
953      }
954  
955      /**
956       * supplyAsync with executor completes with result of supplier
957       */
958      public void testSupplyAsync2() {
959 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
959 >        CompletableFuture<Integer> f;
960 >        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
961          assertEquals(f.join(), one);
962 +        checkCompletedNormally(f, one);
963      }
964  
965      /**
# Line 471 | Line 969 | public class CompletableFutureTest exten
969          FailingSupplier r = new FailingSupplier();
970          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
971          checkCompletedWithWrappedCFException(f);
972 <        assertTrue(r.ran);
972 >        assertEquals(1, r.invocationCount);
973      }
974  
975      // seq completion methods
# Line 479 | Line 977 | public class CompletableFutureTest exten
977      /**
978       * thenRun result completes normally after normal completion of source
979       */
980 <    public void testThenRun() {
981 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
982 <        Noop r = new Noop();
983 <        CompletableFuture<Void> g = f.thenRun(r);
984 <        f.complete(null);
985 <        checkCompletedNormally(g, null);
986 <        // reordered version
987 <        f = new CompletableFuture<Integer>();
988 <        f.complete(null);
989 <        r = new Noop();
990 <        g = f.thenRun(r);
980 >    public void testThenRun_normalCompletion() {
981 >        for (ExecutionMode m : ExecutionMode.values())
982 >        for (boolean createIncomplete : new boolean[] { true, false })
983 >        for (Integer v1 : new Integer[] { 1, null })
984 >    {
985 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
986 >        final Noop r = new Noop(m);
987 >        if (!createIncomplete) f.complete(v1);
988 >        final CompletableFuture<Void> g = m.thenRun(f, r);
989 >        if (createIncomplete) {
990 >            checkIncomplete(g);
991 >            f.complete(v1);
992 >        }
993 >
994          checkCompletedNormally(g, null);
995 <    }
995 >        checkCompletedNormally(f, v1);
996 >        assertEquals(1, r.invocationCount);
997 >    }}
998  
999      /**
1000       * thenRun result completes exceptionally after exceptional
1001       * completion of source
1002       */
1003 <    public void testThenRun2() {
1004 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1005 <        Noop r = new Noop();
1006 <        CompletableFuture<Void> g = f.thenRun(r);
1007 <        f.completeExceptionally(new CFException());
1008 <        checkCompletedWithWrappedCFException(g);
1009 <    }
1003 >    public void testThenRun_exceptionalCompletion() {
1004 >        for (ExecutionMode m : ExecutionMode.values())
1005 >        for (boolean createIncomplete : new boolean[] { true, false })
1006 >    {
1007 >        final CFException ex = new CFException();
1008 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1009 >        final Noop r = new Noop(m);
1010 >        if (!createIncomplete) f.completeExceptionally(ex);
1011 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1012 >        if (createIncomplete) {
1013 >            checkIncomplete(g);
1014 >            f.completeExceptionally(ex);
1015 >        }
1016  
1017 <    /**
1018 <     * thenRun result completes exceptionally if action does
1019 <     */
1020 <    public void testThenRun3() {
512 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
513 <        FailingNoop r = new FailingNoop();
514 <        CompletableFuture<Void> g = f.thenRun(r);
515 <        f.complete(null);
516 <        checkCompletedWithWrappedCFException(g);
517 <    }
1017 >        checkCompletedWithWrappedCFException(g, ex);
1018 >        checkCompletedWithWrappedCFException(f, ex);
1019 >        assertEquals(0, r.invocationCount);
1020 >    }}
1021  
1022      /**
1023       * thenRun result completes exceptionally if source cancelled
1024       */
1025 <    public void testThenRun4() {
1026 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1027 <        Noop r = new Noop();
1028 <        CompletableFuture<Void> g = f.thenRun(r);
1029 <        assertTrue(f.cancel(true));
1025 >    public void testThenRun_sourceCancelled() {
1026 >        for (ExecutionMode m : ExecutionMode.values())
1027 >        for (boolean createIncomplete : new boolean[] { true, false })
1028 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1029 >    {
1030 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1031 >        final Noop r = new Noop(m);
1032 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1033 >        final CompletableFuture<Void> g = f.thenRun(r);
1034 >        if (createIncomplete) {
1035 >            checkIncomplete(g);
1036 >            assertTrue(f.cancel(mayInterruptIfRunning));
1037 >        }
1038 >
1039          checkCompletedWithWrappedCancellationException(g);
1040 <    }
1040 >        checkCancelled(f);
1041 >        assertEquals(0, r.invocationCount);
1042 >    }}
1043 >
1044 >    /**
1045 >     * thenRun result completes exceptionally if action does
1046 >     */
1047 >    public void testThenRun_actionFailed() {
1048 >        for (ExecutionMode m : ExecutionMode.values())
1049 >        for (boolean createIncomplete : new boolean[] { true, false })
1050 >        for (Integer v1 : new Integer[] { 1, null })
1051 >    {
1052 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1053 >        final FailingRunnable r = new FailingRunnable();
1054 >        if (!createIncomplete) f.complete(v1);
1055 >        final CompletableFuture<Void> g = f.thenRun(r);
1056 >        if (createIncomplete) {
1057 >            checkIncomplete(g);
1058 >            f.complete(v1);
1059 >        }
1060 >
1061 >        checkCompletedWithWrappedCFException(g);
1062 >        checkCompletedNormally(f, v1);
1063 >    }}
1064  
1065      /**
1066       * thenApply result completes normally after normal completion of source
1067       */
1068 <    public void testThenApply() {
1069 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1070 <        CompletableFuture<Integer> g = f.thenApply(inc);
1071 <        f.complete(one);
1072 <        checkCompletedNormally(g, two);
1073 <    }
1068 >    public void testThenApply_normalCompletion() {
1069 >        for (ExecutionMode m : ExecutionMode.values())
1070 >        for (boolean createIncomplete : new boolean[] { true, false })
1071 >        for (Integer v1 : new Integer[] { 1, null })
1072 >    {
1073 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1074 >        final IncFunction r = new IncFunction();
1075 >        if (!createIncomplete) f.complete(v1);
1076 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1077 >        if (createIncomplete) {
1078 >            checkIncomplete(g);
1079 >            f.complete(v1);
1080 >        }
1081 >
1082 >        checkCompletedNormally(g, inc(v1));
1083 >        checkCompletedNormally(f, v1);
1084 >        assertEquals(1, r.invocationCount);
1085 >    }}
1086  
1087      /**
1088       * thenApply result completes exceptionally after exceptional
1089       * completion of source
1090       */
1091 <    public void testThenApply2() {
1092 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1093 <        CompletableFuture<Integer> g = f.thenApply(inc);
1094 <        f.completeExceptionally(new CFException());
1095 <        checkCompletedWithWrappedCFException(g);
1096 <    }
1091 >    public void testThenApply_exceptionalCompletion() {
1092 >        for (ExecutionMode m : ExecutionMode.values())
1093 >        for (boolean createIncomplete : new boolean[] { true, false })
1094 >    {
1095 >        final CFException ex = new CFException();
1096 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1097 >        final IncFunction r = new IncFunction();
1098 >        if (!createIncomplete) f.completeExceptionally(ex);
1099 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1100 >        if (createIncomplete) {
1101 >            checkIncomplete(g);
1102 >            f.completeExceptionally(ex);
1103 >        }
1104  
1105 <    /**
1106 <     * thenApply result completes exceptionally if action does
1107 <     */
1108 <    public void testThenApply3() {
555 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
556 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
557 <        f.complete(one);
558 <        checkCompletedWithWrappedCFException(g);
559 <    }
1105 >        checkCompletedWithWrappedCFException(g, ex);
1106 >        checkCompletedWithWrappedCFException(f, ex);
1107 >        assertEquals(0, r.invocationCount);
1108 >    }}
1109  
1110      /**
1111       * thenApply result completes exceptionally if source cancelled
1112       */
1113 <    public void testThenApply4() {
1114 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1115 <        CompletableFuture<Integer> g = f.thenApply(inc);
1116 <        assertTrue(f.cancel(true));
1113 >    public void testThenApply_sourceCancelled() {
1114 >        for (ExecutionMode m : ExecutionMode.values())
1115 >        for (boolean createIncomplete : new boolean[] { true, false })
1116 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1117 >    {
1118 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1119 >        final IncFunction r = new IncFunction();
1120 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1121 >        final CompletableFuture<Integer> g = f.thenApply(r);
1122 >        if (createIncomplete) {
1123 >            checkIncomplete(g);
1124 >            assertTrue(f.cancel(mayInterruptIfRunning));
1125 >        }
1126 >
1127          checkCompletedWithWrappedCancellationException(g);
1128 <    }
1128 >        checkCancelled(f);
1129 >        assertEquals(0, r.invocationCount);
1130 >    }}
1131 >
1132 >    /**
1133 >     * thenApply result completes exceptionally if action does
1134 >     */
1135 >    public void testThenApply_actionFailed() {
1136 >        for (ExecutionMode m : ExecutionMode.values())
1137 >        for (boolean createIncomplete : new boolean[] { true, false })
1138 >        for (Integer v1 : new Integer[] { 1, null })
1139 >    {
1140 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1141 >        final FailingFunction r = new FailingFunction();
1142 >        if (!createIncomplete) f.complete(v1);
1143 >        final CompletableFuture<Integer> g = f.thenApply(r);
1144 >        if (createIncomplete) {
1145 >            checkIncomplete(g);
1146 >            f.complete(v1);
1147 >        }
1148 >
1149 >        checkCompletedWithWrappedCFException(g);
1150 >        checkCompletedNormally(f, v1);
1151 >    }}
1152  
1153      /**
1154       * thenAccept result completes normally after normal completion of source
1155       */
1156 <    public void testThenAccept() {
1157 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1158 <        IncAction r = new IncAction();
1159 <        CompletableFuture<Void> g = f.thenAccept(r);
1160 <        f.complete(one);
1156 >    public void testThenAccept_normalCompletion() {
1157 >        for (ExecutionMode m : ExecutionMode.values())
1158 >        for (boolean createIncomplete : new boolean[] { true, false })
1159 >        for (Integer v1 : new Integer[] { 1, null })
1160 >    {
1161 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1162 >        final IncAction r = new IncAction();
1163 >        if (!createIncomplete) f.complete(v1);
1164 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1165 >        if (createIncomplete) {
1166 >            checkIncomplete(g);
1167 >            f.complete(v1);
1168 >        }
1169 >
1170          checkCompletedNormally(g, null);
1171 <        assertEquals(r.value, 2);
1172 <    }
1171 >        checkCompletedNormally(f, v1);
1172 >        assertEquals(1, r.invocationCount);
1173 >        assertEquals(inc(v1), r.value);
1174 >    }}
1175  
1176      /**
1177       * thenAccept result completes exceptionally after exceptional
1178       * completion of source
1179       */
1180 <    public void testThenAccept2() {
1181 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1182 <        IncAction r = new IncAction();
1183 <        CompletableFuture<Void> g = f.thenAccept(r);
1184 <        f.completeExceptionally(new CFException());
1185 <        checkCompletedWithWrappedCFException(g);
1186 <    }
1180 >    public void testThenAccept_exceptionalCompletion() {
1181 >        for (ExecutionMode m : ExecutionMode.values())
1182 >        for (boolean createIncomplete : new boolean[] { true, false })
1183 >    {
1184 >        final CFException ex = new CFException();
1185 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1186 >        final IncAction r = new IncAction();
1187 >        if (!createIncomplete) f.completeExceptionally(ex);
1188 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1189 >        if (createIncomplete) {
1190 >            checkIncomplete(g);
1191 >            f.completeExceptionally(ex);
1192 >        }
1193 >
1194 >        checkCompletedWithWrappedCFException(g, ex);
1195 >        checkCompletedWithWrappedCFException(f, ex);
1196 >        assertEquals(0, r.invocationCount);
1197 >    }}
1198  
1199      /**
1200       * thenAccept result completes exceptionally if action does
1201       */
1202 <    public void testThenAccept3() {
1203 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1204 <        FailingConsumer r = new FailingConsumer();
1205 <        CompletableFuture<Void> g = f.thenAccept(r);
1206 <        f.complete(one);
1202 >    public void testThenAccept_actionFailed() {
1203 >        for (ExecutionMode m : ExecutionMode.values())
1204 >        for (boolean createIncomplete : new boolean[] { true, false })
1205 >        for (Integer v1 : new Integer[] { 1, null })
1206 >    {
1207 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1208 >        final FailingConsumer r = new FailingConsumer();
1209 >        if (!createIncomplete) f.complete(v1);
1210 >        final CompletableFuture<Void> g = f.thenAccept(r);
1211 >        if (createIncomplete) {
1212 >            checkIncomplete(g);
1213 >            f.complete(v1);
1214 >        }
1215 >
1216          checkCompletedWithWrappedCFException(g);
1217 <        assertTrue(r.ran);
1218 <    }
1217 >        checkCompletedNormally(f, v1);
1218 >    }}
1219  
1220      /**
1221       * thenAccept result completes exceptionally if source cancelled
1222       */
1223 <    public void testThenAccept4() {
1224 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1225 <        IncAction r = new IncAction();
1226 <        CompletableFuture<Void> g = f.thenAccept(r);
1227 <        assertTrue(f.cancel(true));
1228 <        checkCompletedWithWrappedCancellationException(g);
1229 <    }
1223 >    public void testThenAccept_sourceCancelled() {
1224 >        for (ExecutionMode m : ExecutionMode.values())
1225 >        for (boolean createIncomplete : new boolean[] { true, false })
1226 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1227 >    {
1228 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1229 >        final IncAction r = new IncAction();
1230 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1231 >        final CompletableFuture<Void> g = f.thenAccept(r);
1232 >        if (createIncomplete) {
1233 >            checkIncomplete(g);
1234 >            assertTrue(f.cancel(mayInterruptIfRunning));
1235 >        }
1236  
1237 +        checkCompletedWithWrappedCancellationException(g);
1238 +        checkCancelled(f);
1239 +        assertEquals(0, r.invocationCount);
1240 +    }}
1241  
1242      /**
1243 <     * thenCombine result completes normally after normal completion of sources
1243 >     * thenCombine result completes normally after normal completion
1244 >     * of sources
1245       */
1246 <    public void testThenCombine() {
1247 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1248 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1249 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1250 <        f.complete(one);
1251 <        checkIncomplete(g);
1252 <        f2.complete(two);
1253 <        checkCompletedNormally(g, three);
1246 >    public void testThenCombine_normalCompletion() {
1247 >        for (ExecutionMode m : ExecutionMode.values())
1248 >        for (boolean createIncomplete : new boolean[] { true, false })
1249 >        for (boolean fFirst : new boolean[] { true, false })
1250 >        for (Integer v1 : new Integer[] { 1, null })
1251 >        for (Integer v2 : new Integer[] { 2, null })
1252 >    {
1253 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1254 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1255 >        final SubtractFunction r = new SubtractFunction();
1256 >
1257 >        if (fFirst) f.complete(v1); else g.complete(v2);
1258 >        if (!createIncomplete)
1259 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1260 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1261 >        if (createIncomplete) {
1262 >            checkIncomplete(h);
1263 >            assertEquals(0, r.invocationCount);
1264 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1265 >        }
1266  
1267 <        f = new CompletableFuture<Integer>();
1268 <        f.complete(one);
1269 <        f2 = new CompletableFuture<Integer>();
1270 <        g = f.thenCombine(f2, add);
1271 <        checkIncomplete(g);
636 <        f2.complete(two);
637 <        checkCompletedNormally(g, three);
638 <    }
1267 >        checkCompletedNormally(h, subtract(v1, v2));
1268 >        checkCompletedNormally(f, v1);
1269 >        checkCompletedNormally(g, v2);
1270 >        assertEquals(1, r.invocationCount);
1271 >    }}
1272  
1273      /**
1274       * thenCombine result completes exceptionally after exceptional
1275       * completion of either source
1276       */
1277 <    public void testThenCombine2() {
1278 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1279 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1280 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1281 <        f.completeExceptionally(new CFException());
1282 <        f2.complete(two);
1283 <        checkCompletedWithWrappedCFException(g);
1277 >    public void testThenCombine_exceptionalCompletion() {
1278 >        for (ExecutionMode m : ExecutionMode.values())
1279 >        for (boolean createIncomplete : new boolean[] { true, false })
1280 >        for (boolean fFirst : new boolean[] { true, false })
1281 >        for (Integer v1 : new Integer[] { 1, null })
1282 >    {
1283 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1285 >        final CFException ex = new CFException();
1286 >        final SubtractFunction r = new SubtractFunction();
1287 >
1288 >        (fFirst ? f : g).complete(v1);
1289 >        if (!createIncomplete)
1290 >            (!fFirst ? f : g).completeExceptionally(ex);
1291 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1292 >        if (createIncomplete) {
1293 >            checkIncomplete(h);
1294 >            (!fFirst ? f : g).completeExceptionally(ex);
1295 >        }
1296  
1297 <        f = new CompletableFuture<Integer>();
1298 <        f.complete(one);
1299 <        f2 = new CompletableFuture<Integer>();
1300 <        g = f.thenCombine(f2, add);
1301 <        f2.completeExceptionally(new CFException());
657 <        checkCompletedWithWrappedCFException(g);
658 <    }
1297 >        checkCompletedWithWrappedCFException(h, ex);
1298 >        assertEquals(0, r.invocationCount);
1299 >        checkCompletedNormally(fFirst ? f : g, v1);
1300 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1301 >    }}
1302  
1303      /**
1304       * thenCombine result completes exceptionally if action does
1305       */
1306 <    public void testThenCombine3() {
1307 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1308 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1309 <        FailingBiFunction r = new FailingBiFunction();
1310 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1311 <        f.complete(one);
1312 <        checkIncomplete(g);
1313 <        f2.complete(two);
1314 <        checkCompletedWithWrappedCFException(g);
1315 <    }
1306 >    public void testThenCombine_actionFailed() {
1307 >        for (ExecutionMode m : ExecutionMode.values())
1308 >        for (boolean fFirst : new boolean[] { true, false })
1309 >        for (Integer v1 : new Integer[] { 1, null })
1310 >        for (Integer v2 : new Integer[] { 2, null })
1311 >    {
1312 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1313 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1314 >        final FailingBiFunction r = new FailingBiFunction();
1315 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1316 >
1317 >        if (fFirst) {
1318 >            f.complete(v1);
1319 >            g.complete(v2);
1320 >        } else {
1321 >            g.complete(v2);
1322 >            f.complete(v1);
1323 >        }
1324 >
1325 >        checkCompletedWithWrappedCFException(h);
1326 >        checkCompletedNormally(f, v1);
1327 >        checkCompletedNormally(g, v2);
1328 >    }}
1329  
1330      /**
1331       * thenCombine result completes exceptionally if either source cancelled
1332       */
1333 <    public void testThenCombine4() {
1334 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1335 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1336 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1337 <        assertTrue(f.cancel(true));
1338 <        f2.complete(two);
1339 <        checkCompletedWithWrappedCancellationException(g);
1340 <        f = new CompletableFuture<Integer>();
1341 <        f2 = new CompletableFuture<Integer>();
1342 <        g = f.thenCombine(f2, add);
1343 <        f.complete(one);
1344 <        assertTrue(f2.cancel(true));
1345 <        checkCompletedWithWrappedCancellationException(g);
1346 <    }
1333 >    public void testThenCombine_sourceCancelled() {
1334 >        for (ExecutionMode m : ExecutionMode.values())
1335 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1336 >        for (boolean createIncomplete : new boolean[] { true, false })
1337 >        for (boolean fFirst : new boolean[] { true, false })
1338 >        for (Integer v1 : new Integer[] { 1, null })
1339 >    {
1340 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1341 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1342 >        final SubtractFunction r = new SubtractFunction();
1343 >
1344 >        (fFirst ? f : g).complete(v1);
1345 >        if (!createIncomplete)
1346 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1347 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1348 >        if (createIncomplete) {
1349 >            checkIncomplete(h);
1350 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1351 >        }
1352 >
1353 >        checkCompletedWithWrappedCancellationException(h);
1354 >        checkCancelled(!fFirst ? f : g);
1355 >        assertEquals(0, r.invocationCount);
1356 >        checkCompletedNormally(fFirst ? f : g, v1);
1357 >    }}
1358  
1359      /**
1360       * thenAcceptBoth result completes normally after normal
1361       * completion of sources
1362       */
1363 <    public void testThenAcceptBoth() {
1364 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1365 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1366 <        AddAction r = new AddAction();
1367 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1368 <        f.complete(one);
1369 <        checkIncomplete(g);
1370 <        f2.complete(two);
1371 <        checkCompletedNormally(g, null);
1372 <        assertEquals(r.value, 3);
1363 >    public void testThenAcceptBoth_normalCompletion() {
1364 >        for (ExecutionMode m : ExecutionMode.values())
1365 >        for (boolean createIncomplete : new boolean[] { true, false })
1366 >        for (boolean fFirst : new boolean[] { true, false })
1367 >        for (Integer v1 : new Integer[] { 1, null })
1368 >        for (Integer v2 : new Integer[] { 2, null })
1369 >    {
1370 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1371 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1372 >        final SubtractAction r = new SubtractAction();
1373 >
1374 >        if (fFirst) f.complete(v1); else g.complete(v2);
1375 >        if (!createIncomplete)
1376 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1377 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1378 >        if (createIncomplete) {
1379 >            checkIncomplete(h);
1380 >            assertEquals(0, r.invocationCount);
1381 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1382 >        }
1383  
1384 <        r = new AddAction();
1385 <        f = new CompletableFuture<Integer>();
1386 <        f.complete(one);
1387 <        f2 = new CompletableFuture<Integer>();
1388 <        g = f.thenAcceptBoth(f2, r);
712 <        checkIncomplete(g);
713 <        f2.complete(two);
714 <        checkCompletedNormally(g, null);
715 <        assertEquals(r.value, 3);
716 <    }
1384 >        checkCompletedNormally(h, null);
1385 >        assertEquals(subtract(v1, v2), r.value);
1386 >        checkCompletedNormally(f, v1);
1387 >        checkCompletedNormally(g, v2);
1388 >    }}
1389  
1390      /**
1391       * thenAcceptBoth result completes exceptionally after exceptional
1392       * completion of either source
1393       */
1394 <    public void testThenAcceptBoth2() {
1395 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1396 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1397 <        AddAction r = new AddAction();
1398 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1399 <        f.completeExceptionally(new CFException());
1400 <        f2.complete(two);
1401 <        checkCompletedWithWrappedCFException(g);
1394 >    public void testThenAcceptBoth_exceptionalCompletion() {
1395 >        for (ExecutionMode m : ExecutionMode.values())
1396 >        for (boolean createIncomplete : new boolean[] { true, false })
1397 >        for (boolean fFirst : new boolean[] { true, false })
1398 >        for (Integer v1 : new Integer[] { 1, null })
1399 >    {
1400 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1401 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1402 >        final CFException ex = new CFException();
1403 >        final SubtractAction r = new SubtractAction();
1404 >
1405 >        (fFirst ? f : g).complete(v1);
1406 >        if (!createIncomplete)
1407 >            (!fFirst ? f : g).completeExceptionally(ex);
1408 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1409 >        if (createIncomplete) {
1410 >            checkIncomplete(h);
1411 >            (!fFirst ? f : g).completeExceptionally(ex);
1412 >        }
1413  
1414 <        r = new AddAction();
1415 <        f = new CompletableFuture<Integer>();
1416 <        f.complete(one);
1417 <        f2 = new CompletableFuture<Integer>();
1418 <        g = f.thenAcceptBoth(f2, r);
736 <        f2.completeExceptionally(new CFException());
737 <        checkCompletedWithWrappedCFException(g);
738 <    }
1414 >        checkCompletedWithWrappedCFException(h, ex);
1415 >        assertEquals(0, r.invocationCount);
1416 >        checkCompletedNormally(fFirst ? f : g, v1);
1417 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1418 >    }}
1419  
1420      /**
1421       * thenAcceptBoth result completes exceptionally if action does
1422       */
1423 <    public void testThenAcceptBoth3() {
1424 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1425 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1426 <        FailingBiConsumer r = new FailingBiConsumer();
1427 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1428 <        f.complete(one);
1429 <        checkIncomplete(g);
1430 <        f2.complete(two);
1431 <        checkCompletedWithWrappedCFException(g);
1432 <    }
1423 >    public void testThenAcceptBoth_actionFailed() {
1424 >        for (ExecutionMode m : ExecutionMode.values())
1425 >        for (boolean fFirst : new boolean[] { true, false })
1426 >        for (Integer v1 : new Integer[] { 1, null })
1427 >        for (Integer v2 : new Integer[] { 2, null })
1428 >    {
1429 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1430 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1431 >        final FailingBiConsumer r = new FailingBiConsumer();
1432 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1433 >
1434 >        if (fFirst) {
1435 >            f.complete(v1);
1436 >            g.complete(v2);
1437 >        } else {
1438 >            g.complete(v2);
1439 >            f.complete(v1);
1440 >        }
1441 >
1442 >        checkCompletedWithWrappedCFException(h);
1443 >        checkCompletedNormally(f, v1);
1444 >        checkCompletedNormally(g, v2);
1445 >    }}
1446  
1447      /**
1448       * thenAcceptBoth result completes exceptionally if either source cancelled
1449       */
1450 <    public void testThenAcceptBoth4() {
1451 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1452 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1453 <        AddAction r = new AddAction();
1454 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1455 <        assertTrue(f.cancel(true));
1456 <        f2.complete(two);
1457 <        checkCompletedWithWrappedCancellationException(g);
1458 <        f = new CompletableFuture<Integer>();
1459 <        f2 = new CompletableFuture<Integer>();
1460 <        r = new AddAction();
1461 <        g = f.thenAcceptBoth(f2, r);
1462 <        f.complete(one);
1463 <        assertTrue(f2.cancel(true));
1464 <        checkCompletedWithWrappedCancellationException(g);
1465 <    }
1450 >    public void testThenAcceptBoth_sourceCancelled() {
1451 >        for (ExecutionMode m : ExecutionMode.values())
1452 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1453 >        for (boolean createIncomplete : new boolean[] { true, false })
1454 >        for (boolean fFirst : new boolean[] { true, false })
1455 >        for (Integer v1 : new Integer[] { 1, null })
1456 >    {
1457 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1458 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1459 >        final SubtractAction r = new SubtractAction();
1460 >
1461 >        (fFirst ? f : g).complete(v1);
1462 >        if (!createIncomplete)
1463 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1464 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1465 >        if (createIncomplete) {
1466 >            checkIncomplete(h);
1467 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1468 >        }
1469 >
1470 >        checkCompletedWithWrappedCancellationException(h);
1471 >        checkCancelled(!fFirst ? f : g);
1472 >        assertEquals(0, r.invocationCount);
1473 >        checkCompletedNormally(fFirst ? f : g, v1);
1474 >    }}
1475  
1476      /**
1477       * runAfterBoth result completes normally after normal
1478       * completion of sources
1479       */
1480 <    public void testRunAfterBoth() {
1481 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1482 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1483 <        Noop r = new Noop();
1484 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1485 <        f.complete(one);
1486 <        checkIncomplete(g);
1487 <        f2.complete(two);
1488 <        checkCompletedNormally(g, null);
1489 <        assertTrue(r.ran);
1480 >    public void testRunAfterBoth_normalCompletion() {
1481 >        for (ExecutionMode m : ExecutionMode.values())
1482 >        for (boolean createIncomplete : new boolean[] { true, false })
1483 >        for (boolean fFirst : new boolean[] { true, false })
1484 >        for (Integer v1 : new Integer[] { 1, null })
1485 >        for (Integer v2 : new Integer[] { 2, null })
1486 >    {
1487 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1488 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1489 >        final Noop r = new Noop(m);
1490 >
1491 >        if (fFirst) f.complete(v1); else g.complete(v2);
1492 >        if (!createIncomplete)
1493 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1494 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1495 >        if (createIncomplete) {
1496 >            checkIncomplete(h);
1497 >            assertEquals(0, r.invocationCount);
1498 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1499 >        }
1500  
1501 <        r = new Noop();
1502 <        f = new CompletableFuture<Integer>();
1503 <        f.complete(one);
1504 <        f2 = new CompletableFuture<Integer>();
1505 <        g = f.runAfterBoth(f2, r);
794 <        checkIncomplete(g);
795 <        f2.complete(two);
796 <        checkCompletedNormally(g, null);
797 <        assertTrue(r.ran);
798 <    }
1501 >        checkCompletedNormally(h, null);
1502 >        assertEquals(1, r.invocationCount);
1503 >        checkCompletedNormally(f, v1);
1504 >        checkCompletedNormally(g, v2);
1505 >    }}
1506  
1507      /**
1508       * runAfterBoth result completes exceptionally after exceptional
1509       * completion of either source
1510       */
1511 <    public void testRunAfterBoth2() {
1512 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1513 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1514 <        Noop r = new Noop();
1515 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1516 <        f.completeExceptionally(new CFException());
1517 <        f2.complete(two);
1518 <        checkCompletedWithWrappedCFException(g);
1511 >    public void testRunAfterBoth_exceptionalCompletion() {
1512 >        for (ExecutionMode m : ExecutionMode.values())
1513 >        for (boolean createIncomplete : new boolean[] { true, false })
1514 >        for (boolean fFirst : new boolean[] { true, false })
1515 >        for (Integer v1 : new Integer[] { 1, null })
1516 >    {
1517 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1518 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1519 >        final CFException ex = new CFException();
1520 >        final Noop r = new Noop(m);
1521 >
1522 >        (fFirst ? f : g).complete(v1);
1523 >        if (!createIncomplete)
1524 >            (!fFirst ? f : g).completeExceptionally(ex);
1525 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1526 >        if (createIncomplete) {
1527 >            checkIncomplete(h);
1528 >            (!fFirst ? f : g).completeExceptionally(ex);
1529 >        }
1530  
1531 <        r = new Noop();
1532 <        f = new CompletableFuture<Integer>();
1533 <        f.complete(one);
1534 <        f2 = new CompletableFuture<Integer>();
1535 <        g = f.runAfterBoth(f2, r);
818 <        f2.completeExceptionally(new CFException());
819 <        checkCompletedWithWrappedCFException(g);
820 <    }
1531 >        checkCompletedWithWrappedCFException(h, ex);
1532 >        assertEquals(0, r.invocationCount);
1533 >        checkCompletedNormally(fFirst ? f : g, v1);
1534 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1535 >    }}
1536  
1537      /**
1538       * runAfterBoth result completes exceptionally if action does
1539       */
1540 <    public void testRunAfterBoth3() {
1541 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1542 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1543 <        FailingNoop r = new FailingNoop();
1544 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1545 <        f.complete(one);
1546 <        checkIncomplete(g);
1547 <        f2.complete(two);
1548 <        checkCompletedWithWrappedCFException(g);
1549 <    }
1550 <
1551 <    /**
1552 <     * runAfterBoth result completes exceptionally if either source cancelled
1553 <     */
1554 <    public void testRunAfterBoth4() {
1555 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1556 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1557 <        Noop r = new Noop();
1558 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
844 <        assertTrue(f.cancel(true));
845 <        f2.complete(two);
846 <        checkCompletedWithWrappedCancellationException(g);
847 <        f = new CompletableFuture<Integer>();
848 <        f2 = new CompletableFuture<Integer>();
849 <        r = new Noop();
850 <        g = f.runAfterBoth(f2, r);
851 <        f.complete(one);
852 <        assertTrue(f2.cancel(true));
853 <        checkCompletedWithWrappedCancellationException(g);
854 <    }
855 <
856 <    /**
857 <     * applyToEither result completes normally after normal completion
858 <     * of either source
859 <     */
860 <    public void testApplyToEither() {
861 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
862 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
863 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
864 <        f.complete(one);
865 <        checkCompletedNormally(g, two);
866 <        f2.complete(one);
867 <        checkCompletedNormally(g, two);
868 <
869 <        f = new CompletableFuture<Integer>();
870 <        f.complete(one);
871 <        f2 = new CompletableFuture<Integer>();
872 <        g = f.applyToEither(f2, inc);
873 <        checkCompletedNormally(g, two);
874 <    }
875 <
876 <    /**
877 <     * applyToEither result completes exceptionally after exceptional
878 <     * completion of either source
879 <     */
880 <    public void testApplyToEither2() {
881 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
882 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
883 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
884 <        f.completeExceptionally(new CFException());
885 <        f2.complete(one);
886 <        checkCompletedWithWrappedCFException(g);
1540 >    public void testRunAfterBoth_actionFailed() {
1541 >        for (ExecutionMode m : ExecutionMode.values())
1542 >        for (boolean fFirst : new boolean[] { true, false })
1543 >        for (Integer v1 : new Integer[] { 1, null })
1544 >        for (Integer v2 : new Integer[] { 2, null })
1545 >    {
1546 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1547 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1548 >        final FailingRunnable r = new FailingRunnable();
1549 >
1550 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1551 >        if (fFirst) {
1552 >            f.complete(v1);
1553 >            g.complete(v2);
1554 >        } else {
1555 >            g.complete(v2);
1556 >            f.complete(v1);
1557 >        }
1558 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1559  
1560 <        f = new CompletableFuture<Integer>();
1561 <        f2 = new CompletableFuture<Integer>();
1562 <        f2.completeExceptionally(new CFException());
1563 <        g = f.applyToEither(f2, inc);
1564 <        checkCompletedWithWrappedCFException(g);
893 <    }
1560 >        checkCompletedWithWrappedCFException(h1);
1561 >        checkCompletedWithWrappedCFException(h2);
1562 >        checkCompletedNormally(f, v1);
1563 >        checkCompletedNormally(g, v2);
1564 >    }}
1565  
1566      /**
1567 <     * applyToEither result completes exceptionally if action does
1567 >     * runAfterBoth result completes exceptionally if either source cancelled
1568       */
1569 <    public void testApplyToEither3() {
1570 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1571 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1572 <        FailingFunction r = new FailingFunction();
1573 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1574 <        f2.complete(two);
1575 <        checkCompletedWithWrappedCFException(g);
1576 <    }
1569 >    public void testRunAfterBoth_sourceCancelled() {
1570 >        for (ExecutionMode m : ExecutionMode.values())
1571 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1572 >        for (boolean createIncomplete : new boolean[] { true, false })
1573 >        for (boolean fFirst : new boolean[] { true, false })
1574 >        for (Integer v1 : new Integer[] { 1, null })
1575 >    {
1576 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1577 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1578 >        final Noop r = new Noop(m);
1579 >
1580 >
1581 >        (fFirst ? f : g).complete(v1);
1582 >        if (!createIncomplete)
1583 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1584 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1585 >        if (createIncomplete) {
1586 >            checkIncomplete(h);
1587 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1588 >        }
1589  
1590 <    /**
1591 <     * applyToEither result completes exceptionally if either source cancelled
1592 <     */
1593 <    public void testApplyToEither4() {
1594 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
912 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
913 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
914 <        assertTrue(f.cancel(true));
915 <        checkCompletedWithWrappedCancellationException(g);
916 <        f = new CompletableFuture<Integer>();
917 <        f2 = new CompletableFuture<Integer>();
918 <        assertTrue(f2.cancel(true));
919 <        checkCompletedWithWrappedCancellationException(g);
920 <    }
1590 >        checkCompletedWithWrappedCancellationException(h);
1591 >        checkCancelled(!fFirst ? f : g);
1592 >        assertEquals(0, r.invocationCount);
1593 >        checkCompletedNormally(fFirst ? f : g, v1);
1594 >    }}
1595  
1596      /**
1597 <     * acceptEither result completes normally after normal completion
1597 >     * applyToEither result completes normally after normal completion
1598       * of either source
1599       */
1600 <    public void testAcceptEither() {
1601 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1602 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1603 <        IncAction r = new IncAction();
1604 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1605 <        f.complete(one);
1606 <        checkCompletedNormally(g, null);
1607 <        f2.complete(one);
1608 <        checkCompletedNormally(g, null);
1609 <        assertEquals(r.value, 2);
1610 <
1611 <        r = new IncAction();
1612 <        f = new CompletableFuture<Integer>();
1613 <        f.complete(one);
1614 <        f2 = new CompletableFuture<Integer>();
1615 <        g = f.acceptEither(f2, r);
1616 <        checkCompletedNormally(g, null);
1617 <        assertEquals(r.value, 2);
1618 <    }
1619 <
1620 <    /**
947 <     * acceptEither result completes exceptionally after exceptional
948 <     * completion of either source
949 <     */
950 <    public void testAcceptEither2() {
951 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
952 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
953 <        IncAction r = new IncAction();
954 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
955 <        f.completeExceptionally(new CFException());
956 <        f2.complete(one);
957 <        checkCompletedWithWrappedCFException(g);
1600 >    public void testApplyToEither_normalCompletion() {
1601 >        for (ExecutionMode m : ExecutionMode.values())
1602 >        for (boolean createIncomplete : new boolean[] { true, false })
1603 >        for (boolean fFirst : new boolean[] { true, false })
1604 >        for (Integer v1 : new Integer[] { 1, null })
1605 >        for (Integer v2 : new Integer[] { 2, null })
1606 >    {
1607 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1608 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1609 >        final IncFunction r = new IncFunction();
1610 >
1611 >        if (!createIncomplete)
1612 >            if (fFirst) f.complete(v1); else g.complete(v2);
1613 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1614 >        if (createIncomplete) {
1615 >            checkIncomplete(h);
1616 >            assertEquals(0, r.invocationCount);
1617 >            if (fFirst) f.complete(v1); else g.complete(v2);
1618 >        }
1619 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1620 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1621  
1622 <        r = new IncAction();
1623 <        f = new CompletableFuture<Integer>();
1624 <        f2 = new CompletableFuture<Integer>();
1625 <        f2.completeExceptionally(new CFException());
1626 <        g = f.acceptEither(f2, r);
1627 <        checkCompletedWithWrappedCFException(g);
1628 <    }
1622 >        checkCompletedNormally(f, v1);
1623 >        checkCompletedNormally(g, v2);
1624 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1625 >    }}
1626 >
1627 >    public void testApplyToEither_normalCompletionBothAvailable() {
1628 >        for (ExecutionMode m : ExecutionMode.values())
1629 >        for (boolean fFirst : new boolean[] { true, false })
1630 >        for (Integer v1 : new Integer[] { 1, null })
1631 >        for (Integer v2 : new Integer[] { 2, null })
1632 >    {
1633 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1634 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1635 >        final IncFunction r = new IncFunction();
1636 >
1637 >        if (fFirst) {
1638 >            f.complete(v1);
1639 >            g.complete(v2);
1640 >        } else {
1641 >            g.complete(v2);
1642 >            f.complete(v1);
1643 >        }
1644  
1645 <    /**
968 <     * acceptEither result completes exceptionally if action does
969 <     */
970 <    public void testAcceptEither3() {
971 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
972 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
973 <        FailingConsumer r = new FailingConsumer();
974 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
975 <        f2.complete(two);
976 <        checkCompletedWithWrappedCFException(g);
977 <    }
1645 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1646  
1647 <    /**
1648 <     * acceptEither result completes exceptionally if either source cancelled
981 <     */
982 <    public void testAcceptEither4() {
983 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
984 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
985 <        IncAction r = new IncAction();
986 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
987 <        assertTrue(f.cancel(true));
988 <        checkCompletedWithWrappedCancellationException(g);
989 <        f = new CompletableFuture<Integer>();
990 <        f2 = new CompletableFuture<Integer>();
991 <        assertTrue(f2.cancel(true));
992 <        checkCompletedWithWrappedCancellationException(g);
993 <    }
1647 >        checkCompletedNormally(f, v1);
1648 >        checkCompletedNormally(g, v2);
1649  
1650 +        // unspecified behavior
1651 +        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1652 +                   Objects.equals(h.join(), inc(v2)));
1653 +        assertEquals(1, r.invocationCount);
1654 +    }}
1655  
1656      /**
1657 <     * runAfterEither result completes normally after normal completion
998 <     * of either source
999 <     */
1000 <    public void testRunAfterEither() {
1001 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1002 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1003 <        Noop r = new Noop();
1004 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1005 <        f.complete(one);
1006 <        checkCompletedNormally(g, null);
1007 <        f2.complete(one);
1008 <        checkCompletedNormally(g, null);
1009 <        assertTrue(r.ran);
1010 <
1011 <        r = new Noop();
1012 <        f = new CompletableFuture<Integer>();
1013 <        f.complete(one);
1014 <        f2 = new CompletableFuture<Integer>();
1015 <        g = f.runAfterEither(f2, r);
1016 <        checkCompletedNormally(g, null);
1017 <        assertTrue(r.ran);
1018 <    }
1019 <
1020 <    /**
1021 <     * runAfterEither result completes exceptionally after exceptional
1657 >     * applyToEither result completes exceptionally after exceptional
1658       * completion of either source
1659       */
1660 <    public void testRunAfterEither2() {
1661 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1662 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1663 <        Noop r = new Noop();
1664 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1665 <        f.completeExceptionally(new CFException());
1666 <        f2.complete(one);
1667 <        checkCompletedWithWrappedCFException(g);
1668 <
1669 <        r = new Noop();
1670 <        f = new CompletableFuture<Integer>();
1671 <        f2 = new CompletableFuture<Integer>();
1672 <        f2.completeExceptionally(new CFException());
1673 <        g = f.runAfterEither(f2, r);
1674 <        checkCompletedWithWrappedCFException(g);
1675 <    }
1676 <
1677 <    /**
1042 <     * runAfterEither result completes exceptionally if action does
1043 <     */
1044 <    public void testRunAfterEither3() {
1045 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1046 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1047 <        FailingNoop r = new FailingNoop();
1048 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1049 <        f2.complete(two);
1050 <        checkCompletedWithWrappedCFException(g);
1051 <    }
1052 <
1053 <    /**
1054 <     * runAfterEither result completes exceptionally if either source cancelled
1055 <     */
1056 <    public void testRunAfterEither4() {
1057 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1058 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1059 <        Noop r = new Noop();
1060 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1061 <        assertTrue(f.cancel(true));
1062 <        checkCompletedWithWrappedCancellationException(g);
1063 <        f = new CompletableFuture<Integer>();
1064 <        f2 = new CompletableFuture<Integer>();
1065 <        assertTrue(f2.cancel(true));
1066 <        checkCompletedWithWrappedCancellationException(g);
1067 <    }
1068 <
1069 <    /**
1070 <     * thenCompose result completes normally after normal completion of source
1071 <     */
1072 <    public void testThenCompose() {
1073 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1074 <        CompletableFutureInc r = new CompletableFutureInc();
1075 <        CompletableFuture<Integer> g = f.thenCompose(r);
1076 <        f.complete(one);
1077 <        checkCompletedNormally(g, two);
1078 <    }
1079 <
1080 <    /**
1081 <     * thenCompose result completes exceptionally after exceptional
1082 <     * completion of source
1083 <     */
1084 <    public void testThenCompose2() {
1085 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1086 <        CompletableFutureInc r = new CompletableFutureInc();
1087 <        CompletableFuture<Integer> g = f.thenCompose(r);
1088 <        f.completeExceptionally(new CFException());
1089 <        checkCompletedWithWrappedCFException(g);
1090 <    }
1091 <
1092 <    /**
1093 <     * thenCompose result completes exceptionally if action does
1094 <     */
1095 <    public void testThenCompose3() {
1096 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1097 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1098 <        CompletableFuture<Integer> g = f.thenCompose(r);
1099 <        f.complete(one);
1100 <        checkCompletedWithWrappedCFException(g);
1101 <    }
1102 <
1103 <    /**
1104 <     * thenCompose result completes exceptionally if source cancelled
1105 <     */
1106 <    public void testThenCompose4() {
1107 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1108 <        CompletableFutureInc r = new CompletableFutureInc();
1109 <        CompletableFuture<Integer> g = f.thenCompose(r);
1110 <        assertTrue(f.cancel(true));
1111 <        checkCompletedWithWrappedCancellationException(g);
1112 <    }
1113 <
1114 <
1115 <    // asyncs
1660 >    public void testApplyToEither_exceptionalCompletion1() {
1661 >        for (ExecutionMode m : ExecutionMode.values())
1662 >        for (boolean createIncomplete : new boolean[] { true, false })
1663 >        for (boolean fFirst : new boolean[] { true, false })
1664 >        for (Integer v1 : new Integer[] { 1, null })
1665 >    {
1666 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1667 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1668 >        final CFException ex = new CFException();
1669 >        final IncFunction r = new IncFunction();
1670 >
1671 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1672 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1673 >        if (createIncomplete) {
1674 >            checkIncomplete(h);
1675 >            assertEquals(0, r.invocationCount);
1676 >            (fFirst ? f : g).completeExceptionally(ex);
1677 >        }
1678  
1679 <    /**
1680 <     * thenRunAsync result completes normally after normal completion of source
1119 <     */
1120 <    public void testThenRunAsync() {
1121 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1122 <        Noop r = new Noop();
1123 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1124 <        f.complete(null);
1125 <        checkCompletedNormally(g, null);
1679 >        checkCompletedWithWrappedCFException(h, ex);
1680 >        (!fFirst ? f : g).complete(v1);
1681  
1682 <        // reordered version
1683 <        f = new CompletableFuture<Integer>();
1684 <        f.complete(null);
1685 <        r = new Noop();
1686 <        g = f.thenRunAsync(r);
1687 <        checkCompletedNormally(g, null);
1688 <    }
1682 >        assertEquals(0, r.invocationCount);
1683 >        checkCompletedNormally(!fFirst ? f : g, v1);
1684 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1685 >        checkCompletedWithWrappedCFException(h, ex);
1686 >    }}
1687 >
1688 >    public void testApplyToEither_exceptionalCompletion2() {
1689 >        for (ExecutionMode m : ExecutionMode.values())
1690 >        for (boolean reverseArgs : new boolean[] { true, false })
1691 >        for (boolean fFirst : new boolean[] { true, false })
1692 >        for (Integer v1 : new Integer[] { 1, null })
1693 >    {
1694 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1695 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1696 >        final IncFunction r1 = new IncFunction();
1697 >        final IncFunction r2 = new IncFunction();
1698 >        final CFException ex = new CFException();
1699 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1700 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1701 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1702 >        if (fFirst) {
1703 >            f.complete(v1);
1704 >            g.completeExceptionally(ex);
1705 >        } else {
1706 >            g.completeExceptionally(ex);
1707 >            f.complete(v1);
1708 >        }
1709 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1710  
1711 <    /**
1136 <     * thenRunAsync result completes exceptionally after exceptional
1137 <     * completion of source
1138 <     */
1139 <    public void testThenRunAsync2() {
1140 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1141 <        Noop r = new Noop();
1142 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1143 <        f.completeExceptionally(new CFException());
1711 >        // unspecified behavior
1712          try {
1713 <            g.join();
1714 <            shouldThrow();
1715 <        } catch (Exception ok) {
1713 >            assertEquals(inc(v1), h1.join());
1714 >            assertEquals(1, r1.invocationCount);
1715 >        } catch (CompletionException ok) {
1716 >            checkCompletedWithWrappedCFException(h1, ex);
1717 >            assertEquals(0, r1.invocationCount);
1718          }
1149        checkCompletedWithWrappedCFException(g);
1150    }
1719  
1152    /**
1153     * thenRunAsync result completes exceptionally if action does
1154     */
1155    public void testThenRunAsync3() {
1156        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1157        FailingNoop r = new FailingNoop();
1158        CompletableFuture<Void> g = f.thenRunAsync(r);
1159        f.complete(null);
1160        checkCompletedWithWrappedCFException(g);
1161    }
1162
1163    /**
1164     * thenRunAsync result completes exceptionally if source cancelled
1165     */
1166    public void testThenRunAsync4() {
1167        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1168        Noop r = new Noop();
1169        CompletableFuture<Void> g = f.thenRunAsync(r);
1170        assertTrue(f.cancel(true));
1171        checkCompletedWithWrappedCancellationException(g);
1172    }
1173
1174    /**
1175     * thenApplyAsync result completes normally after normal completion of source
1176     */
1177    public void testThenApplyAsync() {
1178        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1179        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1180        f.complete(one);
1181        checkCompletedNormally(g, two);
1182    }
1183
1184    /**
1185     * thenApplyAsync result completes exceptionally after exceptional
1186     * completion of source
1187     */
1188    public void testThenApplyAsync2() {
1189        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1190        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1191        f.completeExceptionally(new CFException());
1192        checkCompletedWithWrappedCFException(g);
1193    }
1194
1195    /**
1196     * thenApplyAsync result completes exceptionally if action does
1197     */
1198    public void testThenApplyAsync3() {
1199        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1200        FailingFunction r = new FailingFunction();
1201        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1202        f.complete(null);
1203        checkCompletedWithWrappedCFException(g);
1204    }
1205
1206    /**
1207     * thenApplyAsync result completes exceptionally if source cancelled
1208     */
1209    public void testThenApplyAsync4() {
1210        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1211        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1212        assertTrue(f.cancel(true));
1213        checkCompletedWithWrappedCancellationException(g);
1214    }
1215
1216    /**
1217     * thenAcceptAsync result completes normally after normal
1218     * completion of source
1219     */
1220    public void testThenAcceptAsync() {
1221        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1222        IncAction r = new IncAction();
1223        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1224        f.complete(one);
1225        checkCompletedNormally(g, null);
1226        assertEquals(r.value, 2);
1227    }
1228
1229    /**
1230     * thenAcceptAsync result completes exceptionally after exceptional
1231     * completion of source
1232     */
1233    public void testThenAcceptAsync2() {
1234        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1235        IncAction r = new IncAction();
1236        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1237        f.completeExceptionally(new CFException());
1238        checkCompletedWithWrappedCFException(g);
1239    }
1240
1241    /**
1242     * thenAcceptAsync result completes exceptionally if action does
1243     */
1244    public void testThenAcceptAsync3() {
1245        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1246        FailingConsumer r = new FailingConsumer();
1247        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1248        f.complete(null);
1249        checkCompletedWithWrappedCFException(g);
1250    }
1251
1252    /**
1253     * thenAcceptAsync result completes exceptionally if source cancelled
1254     */
1255    public void testThenAcceptAsync4() {
1256        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1257        IncAction r = new IncAction();
1258        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1259        assertTrue(f.cancel(true));
1260        checkCompletedWithWrappedCancellationException(g);
1261    }
1262    /**
1263     * thenCombineAsync result completes normally after normal
1264     * completion of sources
1265     */
1266    public void testThenCombineAsync() {
1267        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1268        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1269        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1270        f.complete(one);
1271        checkIncomplete(g);
1272        f2.complete(two);
1273        checkCompletedNormally(g, three);
1274    }
1275
1276    /**
1277     * thenCombineAsync result completes exceptionally after exceptional
1278     * completion of source
1279     */
1280    public void testThenCombineAsync2() {
1281        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1282        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1283        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1284        f.completeExceptionally(new CFException());
1285        f2.complete(two);
1286        checkCompletedWithWrappedCFException(g);
1287
1288        f = new CompletableFuture<Integer>();
1289        f2 = new CompletableFuture<Integer>();
1290        g = f.thenCombineAsync(f2, add);
1291        f.complete(one);
1292        f2.completeExceptionally(new CFException());
1293        checkCompletedWithWrappedCFException(g);
1294    }
1295
1296    /**
1297     * thenCombineAsync result completes exceptionally if action does
1298     */
1299    public void testThenCombineAsync3() {
1300        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1301        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1302        FailingBiFunction r = new FailingBiFunction();
1303        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1304        f.complete(one);
1305        checkIncomplete(g);
1306        f2.complete(two);
1307        checkCompletedWithWrappedCFException(g);
1308    }
1309
1310    /**
1311     * thenCombineAsync result completes exceptionally if either source cancelled
1312     */
1313    public void testThenCombineAsync4() {
1314        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1315        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1316        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1317        assertTrue(f.cancel(true));
1318        f2.complete(two);
1319        checkCompletedWithWrappedCancellationException(g);
1320
1321        f = new CompletableFuture<Integer>();
1322        f2 = new CompletableFuture<Integer>();
1323        g = f.thenCombineAsync(f2, add);
1324        f.complete(one);
1325        assertTrue(f2.cancel(true));
1326        checkCompletedWithWrappedCancellationException(g);
1327    }
1328
1329    /**
1330     * thenAcceptBothAsync result completes normally after normal
1331     * completion of sources
1332     */
1333    public void testThenAcceptBothAsync() {
1334        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1335        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1336        AddAction r = new AddAction();
1337        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1338        f.complete(one);
1339        checkIncomplete(g);
1340        f2.complete(two);
1341        checkCompletedNormally(g, null);
1342        assertEquals(r.value, 3);
1343    }
1344
1345    /**
1346     * thenAcceptBothAsync result completes exceptionally after exceptional
1347     * completion of source
1348     */
1349    public void testThenAcceptBothAsync2() {
1350        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1351        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1352        AddAction r = new AddAction();
1353        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1354        f.completeExceptionally(new CFException());
1355        f2.complete(two);
1356        checkCompletedWithWrappedCFException(g);
1357
1358        r = new AddAction();
1359        f = new CompletableFuture<Integer>();
1360        f2 = new CompletableFuture<Integer>();
1361        g = f.thenAcceptBothAsync(f2, r);
1362        f.complete(one);
1363        f2.completeExceptionally(new CFException());
1364        checkCompletedWithWrappedCFException(g);
1365    }
1366
1367    /**
1368     * thenAcceptBothAsync result completes exceptionally if action does
1369     */
1370    public void testThenAcceptBothAsync3() {
1371        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1372        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1373        FailingBiConsumer r = new FailingBiConsumer();
1374        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1375        f.complete(one);
1376        checkIncomplete(g);
1377        f2.complete(two);
1378        checkCompletedWithWrappedCFException(g);
1379    }
1380
1381    /**
1382     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1383     */
1384    public void testThenAcceptBothAsync4() {
1385        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1386        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1387        AddAction r = new AddAction();
1388        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1389        assertTrue(f.cancel(true));
1390        f2.complete(two);
1391        checkCompletedWithWrappedCancellationException(g);
1392
1393        r = new AddAction();
1394        f = new CompletableFuture<Integer>();
1395        f2 = new CompletableFuture<Integer>();
1396        g = f.thenAcceptBothAsync(f2, r);
1397        f.complete(one);
1398        assertTrue(f2.cancel(true));
1399        checkCompletedWithWrappedCancellationException(g);
1400    }
1401
1402    /**
1403     * runAfterBothAsync result completes normally after normal
1404     * completion of sources
1405     */
1406    public void testRunAfterBothAsync() {
1407        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1408        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1409        Noop r = new Noop();
1410        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1411        f.complete(one);
1412        checkIncomplete(g);
1413        f2.complete(two);
1414        checkCompletedNormally(g, null);
1415        assertTrue(r.ran);
1416    }
1417
1418    /**
1419     * runAfterBothAsync result completes exceptionally after exceptional
1420     * completion of source
1421     */
1422    public void testRunAfterBothAsync2() {
1423        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1424        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1425        Noop r = new Noop();
1426        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1427        f.completeExceptionally(new CFException());
1428        f2.complete(two);
1429        checkCompletedWithWrappedCFException(g);
1430
1431        r = new Noop();
1432        f = new CompletableFuture<Integer>();
1433        f2 = new CompletableFuture<Integer>();
1434        g = f.runAfterBothAsync(f2, r);
1435        f.complete(one);
1436        f2.completeExceptionally(new CFException());
1437        checkCompletedWithWrappedCFException(g);
1438    }
1439
1440    /**
1441     * runAfterBothAsync result completes exceptionally if action does
1442     */
1443    public void testRunAfterBothAsync3() {
1444        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1445        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1446        FailingNoop r = new FailingNoop();
1447        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1448        f.complete(one);
1449        checkIncomplete(g);
1450        f2.complete(two);
1451        checkCompletedWithWrappedCFException(g);
1452    }
1453
1454    /**
1455     * runAfterBothAsync result completes exceptionally if either source cancelled
1456     */
1457    public void testRunAfterBothAsync4() {
1458        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1459        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1460        Noop r = new Noop();
1461        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1462        assertTrue(f.cancel(true));
1463        f2.complete(two);
1464        checkCompletedWithWrappedCancellationException(g);
1465
1466        r = new Noop();
1467        f = new CompletableFuture<Integer>();
1468        f2 = new CompletableFuture<Integer>();
1469        g = f.runAfterBothAsync(f2, r);
1470        f.complete(one);
1471        assertTrue(f2.cancel(true));
1472        checkCompletedWithWrappedCancellationException(g);
1473    }
1474
1475    /**
1476     * applyToEitherAsync result completes normally after normal
1477     * completion of sources
1478     */
1479    public void testApplyToEitherAsync() {
1480        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1481        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1482        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1483        f.complete(one);
1484        checkCompletedNormally(g, two);
1485
1486        f = new CompletableFuture<Integer>();
1487        f.complete(one);
1488        f2 = new CompletableFuture<Integer>();
1489        g = f.applyToEitherAsync(f2, inc);
1490        checkCompletedNormally(g, two);
1491    }
1492
1493    /**
1494     * applyToEitherAsync result completes exceptionally after exceptional
1495     * completion of source
1496     */
1497    public void testApplyToEitherAsync2() {
1498        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1499        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1500        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1501        f.completeExceptionally(new CFException());
1502        checkCompletedWithWrappedCFException(g);
1503
1504        f = new CompletableFuture<Integer>();
1505        f2 = new CompletableFuture<Integer>();
1506        f2.completeExceptionally(new CFException());
1507        g = f.applyToEitherAsync(f2, inc);
1508        f.complete(one);
1509        checkCompletedWithWrappedCFException(g);
1510    }
1511
1512    /**
1513     * applyToEitherAsync result completes exceptionally if action does
1514     */
1515    public void testApplyToEitherAsync3() {
1516        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1517        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1518        FailingFunction r = new FailingFunction();
1519        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1520        f.complete(one);
1521        checkCompletedWithWrappedCFException(g);
1522    }
1523
1524    /**
1525     * applyToEitherAsync result completes exceptionally if either source cancelled
1526     */
1527    public void testApplyToEitherAsync4() {
1528        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1529        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1530        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1531        assertTrue(f.cancel(true));
1532        checkCompletedWithWrappedCancellationException(g);
1533
1534        f = new CompletableFuture<Integer>();
1535        f2 = new CompletableFuture<Integer>();
1536        assertTrue(f2.cancel(true));
1537        g = f.applyToEitherAsync(f2, inc);
1538        checkCompletedWithWrappedCancellationException(g);
1539    }
1540
1541    /**
1542     * acceptEitherAsync result completes normally after normal
1543     * completion of sources
1544     */
1545    public void testAcceptEitherAsync() {
1546        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1547        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1548        IncAction r = new IncAction();
1549        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1550        f.complete(one);
1551        checkCompletedNormally(g, null);
1552        assertEquals(r.value, 2);
1553
1554        r = new IncAction();
1555        f = new CompletableFuture<Integer>();
1556        f.complete(one);
1557        f2 = new CompletableFuture<Integer>();
1558        g = f.acceptEitherAsync(f2, r);
1559        checkCompletedNormally(g, null);
1560        assertEquals(r.value, 2);
1561    }
1562
1563    /**
1564     * acceptEitherAsync result completes exceptionally after exceptional
1565     * completion of source
1566     */
1567    public void testAcceptEitherAsync2() {
1568        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1569        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1570        IncAction r = new IncAction();
1571        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1572        f.completeExceptionally(new CFException());
1573        checkCompletedWithWrappedCFException(g);
1574
1575        r = new IncAction();
1576        f = new CompletableFuture<Integer>();
1577        f2 = new CompletableFuture<Integer>();
1578        f2.completeExceptionally(new CFException());
1579        g = f.acceptEitherAsync(f2, r);
1580        f.complete(one);
1581        checkCompletedWithWrappedCFException(g);
1582    }
1583
1584    /**
1585     * acceptEitherAsync result completes exceptionally if action does
1586     */
1587    public void testAcceptEitherAsync3() {
1588        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1589        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1590        FailingConsumer r = new FailingConsumer();
1591        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1592        f.complete(one);
1593        checkCompletedWithWrappedCFException(g);
1594    }
1595
1596    /**
1597     * acceptEitherAsync result completes exceptionally if either
1598     * source cancelled
1599     */
1600    public void testAcceptEitherAsync4() {
1601        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1602        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1603        IncAction r = new IncAction();
1604        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1605        assertTrue(f.cancel(true));
1606        checkCompletedWithWrappedCancellationException(g);
1607
1608        r = new IncAction();
1609        f = new CompletableFuture<Integer>();
1610        f2 = new CompletableFuture<Integer>();
1611        assertTrue(f2.cancel(true));
1612        g = f.acceptEitherAsync(f2, r);
1613        checkCompletedWithWrappedCancellationException(g);
1614    }
1615
1616    /**
1617     * runAfterEitherAsync result completes normally after normal
1618     * completion of sources
1619     */
1620    public void testRunAfterEitherAsync() {
1621        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1622        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1623        Noop r = new Noop();
1624        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1625        f.complete(one);
1626        checkCompletedNormally(g, null);
1627        assertTrue(r.ran);
1628
1629        r = new Noop();
1630        f = new CompletableFuture<Integer>();
1631        f.complete(one);
1632        f2 = new CompletableFuture<Integer>();
1633        g = f.runAfterEitherAsync(f2, r);
1634        checkCompletedNormally(g, null);
1635        assertTrue(r.ran);
1636    }
1637
1638    /**
1639     * runAfterEitherAsync result completes exceptionally after exceptional
1640     * completion of source
1641     */
1642    public void testRunAfterEitherAsync2() {
1643        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1644        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1645        Noop r = new Noop();
1646        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1647        f.completeExceptionally(new CFException());
1648        checkCompletedWithWrappedCFException(g);
1649
1650        r = new Noop();
1651        f = new CompletableFuture<Integer>();
1652        f2 = new CompletableFuture<Integer>();
1653        f2.completeExceptionally(new CFException());
1654        g = f.runAfterEitherAsync(f2, r);
1655        f.complete(one);
1656        checkCompletedWithWrappedCFException(g);
1657    }
1658
1659    /**
1660     * runAfterEitherAsync result completes exceptionally if action does
1661     */
1662    public void testRunAfterEitherAsync3() {
1663        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1664        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1665        FailingNoop r = new FailingNoop();
1666        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1667        f.complete(one);
1668        checkCompletedWithWrappedCFException(g);
1669    }
1670
1671    /**
1672     * runAfterEitherAsync result completes exceptionally if either
1673     * source cancelled
1674     */
1675    public void testRunAfterEitherAsync4() {
1676        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1677        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1678        Noop r = new Noop();
1679        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1680        assertTrue(f.cancel(true));
1681        checkCompletedWithWrappedCancellationException(g);
1682
1683        r = new Noop();
1684        f = new CompletableFuture<Integer>();
1685        f2 = new CompletableFuture<Integer>();
1686        assertTrue(f2.cancel(true));
1687        g = f.runAfterEitherAsync(f2, r);
1688        checkCompletedWithWrappedCancellationException(g);
1689    }
1690
1691    /**
1692     * thenComposeAsync result completes normally after normal
1693     * completion of source
1694     */
1695    public void testThenComposeAsync() {
1696        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1697        CompletableFutureInc r = new CompletableFutureInc();
1698        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1699        f.complete(one);
1700        checkCompletedNormally(g, two);
1701    }
1702
1703    /**
1704     * thenComposeAsync result completes exceptionally after
1705     * exceptional completion of source
1706     */
1707    public void testThenComposeAsync2() {
1708        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1709        CompletableFutureInc r = new CompletableFutureInc();
1710        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1711        f.completeExceptionally(new CFException());
1712        checkCompletedWithWrappedCFException(g);
1713    }
1714
1715    /**
1716     * thenComposeAsync result completes exceptionally if action does
1717     */
1718    public void testThenComposeAsync3() {
1719        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1720        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1721        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1722        f.complete(one);
1723        checkCompletedWithWrappedCFException(g);
1724    }
1725
1726    /**
1727     * thenComposeAsync result completes exceptionally if source cancelled
1728     */
1729    public void testThenComposeAsync4() {
1730        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1731        CompletableFutureInc r = new CompletableFutureInc();
1732        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1733        assertTrue(f.cancel(true));
1734        checkCompletedWithWrappedCancellationException(g);
1735    }
1736
1737
1738    // async with explicit executors
1739
1740    /**
1741     * thenRunAsync result completes normally after normal completion of source
1742     */
1743    public void testThenRunAsyncE() {
1744        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1745        Noop r = new Noop();
1746        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1747        f.complete(null);
1748        checkCompletedNormally(g, null);
1749
1750        // reordered version
1751        f = new CompletableFuture<Integer>();
1752        f.complete(null);
1753        r = new Noop();
1754        g = f.thenRunAsync(r, new ThreadExecutor());
1755        checkCompletedNormally(g, null);
1756    }
1757
1758    /**
1759     * thenRunAsync result completes exceptionally after exceptional
1760     * completion of source
1761     */
1762    public void testThenRunAsync2E() {
1763        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1764        Noop r = new Noop();
1765        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1766        f.completeExceptionally(new CFException());
1720          try {
1721 <            g.join();
1722 <            shouldThrow();
1723 <        } catch (Exception ok) {
1721 >            assertEquals(inc(v1), h2.join());
1722 >            assertEquals(1, r2.invocationCount);
1723 >        } catch (CompletionException ok) {
1724 >            checkCompletedWithWrappedCFException(h2, ex);
1725 >            assertEquals(0, r2.invocationCount);
1726          }
1772        checkCompletedWithWrappedCFException(g);
1773    }
1727  
1728 <    /**
1729 <     * thenRunAsync result completes exceptionally if action does
1730 <     */
1778 <    public void testThenRunAsync3E() {
1779 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1780 <        FailingNoop r = new FailingNoop();
1781 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1782 <        f.complete(null);
1783 <        checkCompletedWithWrappedCFException(g);
1784 <    }
1785 <
1786 <    /**
1787 <     * thenRunAsync result completes exceptionally if source cancelled
1788 <     */
1789 <    public void testThenRunAsync4E() {
1790 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1791 <        Noop r = new Noop();
1792 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1793 <        assertTrue(f.cancel(true));
1794 <        checkCompletedWithWrappedCancellationException(g);
1795 <    }
1796 <
1797 <    /**
1798 <     * thenApplyAsync result completes normally after normal completion of source
1799 <     */
1800 <    public void testThenApplyAsyncE() {
1801 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1802 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1803 <        f.complete(one);
1804 <        checkCompletedNormally(g, two);
1805 <    }
1728 >        checkCompletedWithWrappedCFException(g, ex);
1729 >        checkCompletedNormally(f, v1);
1730 >    }}
1731  
1732      /**
1733 <     * thenApplyAsync result completes exceptionally after exceptional
1809 <     * completion of source
1810 <     */
1811 <    public void testThenApplyAsync2E() {
1812 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1813 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1814 <        f.completeExceptionally(new CFException());
1815 <        checkCompletedWithWrappedCFException(g);
1816 <    }
1817 <
1818 <    /**
1819 <     * thenApplyAsync result completes exceptionally if action does
1820 <     */
1821 <    public void testThenApplyAsync3E() {
1822 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1823 <        FailingFunction r = new FailingFunction();
1824 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
1825 <        f.complete(null);
1826 <        checkCompletedWithWrappedCFException(g);
1827 <    }
1828 <
1829 <    /**
1830 <     * thenApplyAsync result completes exceptionally if source cancelled
1733 >     * applyToEither result completes exceptionally if action does
1734       */
1735 <    public void testThenApplyAsync4E() {
1736 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1737 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1738 <        assertTrue(f.cancel(true));
1739 <        checkCompletedWithWrappedCancellationException(g);
1740 <    }
1735 >    public void testApplyToEither_actionFailed1() {
1736 >        for (ExecutionMode m : ExecutionMode.values())
1737 >        for (Integer v1 : new Integer[] { 1, null })
1738 >        for (Integer v2 : new Integer[] { 2, null })
1739 >    {
1740 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1741 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1742 >        final FailingFunction r = new FailingFunction();
1743 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1744 >
1745 >        f.complete(v1);
1746 >        checkCompletedWithWrappedCFException(h);
1747 >        g.complete(v2);
1748 >        checkCompletedNormally(f, v1);
1749 >        checkCompletedNormally(g, v2);
1750 >    }}
1751 >
1752 >    public void testApplyToEither_actionFailed2() {
1753 >        for (ExecutionMode m : ExecutionMode.values())
1754 >        for (Integer v1 : new Integer[] { 1, null })
1755 >        for (Integer v2 : new Integer[] { 2, null })
1756 >    {
1757 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1758 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1759 >        final FailingFunction r = new FailingFunction();
1760 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1761 >
1762 >        g.complete(v2);
1763 >        checkCompletedWithWrappedCFException(h);
1764 >        f.complete(v1);
1765 >        checkCompletedNormally(f, v1);
1766 >        checkCompletedNormally(g, v2);
1767 >    }}
1768  
1769      /**
1770 <     * thenAcceptAsync result completes normally after normal
1841 <     * completion of source
1770 >     * applyToEither result completes exceptionally if either source cancelled
1771       */
1772 <    public void testThenAcceptAsyncE() {
1773 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1774 <        IncAction r = new IncAction();
1775 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1776 <        f.complete(one);
1777 <        checkCompletedNormally(g, null);
1778 <        assertEquals(r.value, 2);
1779 <    }
1772 >    public void testApplyToEither_sourceCancelled1() {
1773 >        for (ExecutionMode m : ExecutionMode.values())
1774 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1775 >        for (boolean createIncomplete : new boolean[] { true, false })
1776 >        for (boolean fFirst : new boolean[] { true, false })
1777 >        for (Integer v1 : new Integer[] { 1, null })
1778 >    {
1779 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1780 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1781 >        final IncFunction r = new IncFunction();
1782 >
1783 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1784 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1785 >        if (createIncomplete) {
1786 >            checkIncomplete(h);
1787 >            assertEquals(0, r.invocationCount);
1788 >            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1789 >        }
1790  
1791 <    /**
1792 <     * thenAcceptAsync result completes exceptionally after exceptional
1854 <     * completion of source
1855 <     */
1856 <    public void testThenAcceptAsync2E() {
1857 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1858 <        IncAction r = new IncAction();
1859 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1860 <        f.completeExceptionally(new CFException());
1861 <        checkCompletedWithWrappedCFException(g);
1862 <    }
1791 >        checkCompletedWithWrappedCancellationException(h);
1792 >        (!fFirst ? f : g).complete(v1);
1793  
1794 <    /**
1795 <     * thenAcceptAsync result completes exceptionally if action does
1796 <     */
1797 <    public void testThenAcceptAsync3E() {
1798 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1799 <        FailingConsumer r = new FailingConsumer();
1800 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1801 <        f.complete(null);
1802 <        checkCompletedWithWrappedCFException(g);
1803 <    }
1794 >        assertEquals(0, r.invocationCount);
1795 >        checkCompletedNormally(!fFirst ? f : g, v1);
1796 >        checkCancelled(fFirst ? f : g);
1797 >        checkCompletedWithWrappedCancellationException(h);
1798 >    }}
1799 >
1800 >    public void testApplyToEither_sourceCancelled2() {
1801 >        for (ExecutionMode m : ExecutionMode.values())
1802 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1803 >        for (boolean reverseArgs : new boolean[] { true, false })
1804 >        for (boolean fFirst : new boolean[] { true, false })
1805 >        for (Integer v1 : new Integer[] { 1, null })
1806 >    {
1807 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1808 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1809 >        final IncFunction r1 = new IncFunction();
1810 >        final IncFunction r2 = new IncFunction();
1811 >        final CFException ex = new CFException();
1812 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1813 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1814 >
1815 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1816 >        if (fFirst) {
1817 >            f.complete(v1);
1818 >            assertTrue(g.cancel(mayInterruptIfRunning));
1819 >        } else {
1820 >            assertTrue(g.cancel(mayInterruptIfRunning));
1821 >            f.complete(v1);
1822 >        }
1823 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1824  
1825 <    /**
1826 <     * thenAcceptAsync result completes exceptionally if source cancelled
1827 <     */
1828 <    public void testThenAcceptAsync4E() {
1829 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1830 <        IncAction r = new IncAction();
1831 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1832 <        assertTrue(f.cancel(true));
1883 <        checkCompletedWithWrappedCancellationException(g);
1884 <    }
1885 <    /**
1886 <     * thenCombineAsync result completes normally after normal
1887 <     * completion of sources
1888 <     */
1889 <    public void testThenCombineAsyncE() {
1890 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1891 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1892 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1893 <        f.complete(one);
1894 <        checkIncomplete(g);
1895 <        f2.complete(two);
1896 <        checkCompletedNormally(g, three);
1897 <    }
1825 >        // unspecified behavior
1826 >        try {
1827 >            assertEquals(inc(v1), h1.join());
1828 >            assertEquals(1, r1.invocationCount);
1829 >        } catch (CompletionException ok) {
1830 >            checkCompletedWithWrappedCancellationException(h1);
1831 >            assertEquals(0, r1.invocationCount);
1832 >        }
1833  
1834 <    /**
1835 <     * thenCombineAsync result completes exceptionally after exceptional
1836 <     * completion of source
1837 <     */
1838 <    public void testThenCombineAsync2E() {
1839 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1840 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1906 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1907 <        f.completeExceptionally(new CFException());
1908 <        f2.complete(two);
1909 <        checkCompletedWithWrappedCFException(g);
1834 >        try {
1835 >            assertEquals(inc(v1), h2.join());
1836 >            assertEquals(1, r2.invocationCount);
1837 >        } catch (CompletionException ok) {
1838 >            checkCompletedWithWrappedCancellationException(h2);
1839 >            assertEquals(0, r2.invocationCount);
1840 >        }
1841  
1842 <        f = new CompletableFuture<Integer>();
1843 <        f2 = new CompletableFuture<Integer>();
1844 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1914 <        f.complete(one);
1915 <        f2.completeExceptionally(new CFException());
1916 <        checkCompletedWithWrappedCFException(g);
1917 <    }
1842 >        checkCancelled(g);
1843 >        checkCompletedNormally(f, v1);
1844 >    }}
1845  
1846      /**
1847 <     * thenCombineAsync result completes exceptionally if action does
1847 >     * acceptEither result completes normally after normal completion
1848 >     * of either source
1849       */
1850 <    public void testThenCombineAsync3E() {
1851 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1852 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1853 <        FailingBiFunction r = new FailingBiFunction();
1854 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
1855 <        f.complete(one);
1856 <        checkIncomplete(g);
1857 <        f2.complete(two);
1858 <        checkCompletedWithWrappedCFException(g);
1859 <    }
1850 >    public void testAcceptEither_normalCompletion1() {
1851 >        for (ExecutionMode m : ExecutionMode.values())
1852 >        for (Integer v1 : new Integer[] { 1, null })
1853 >        for (Integer v2 : new Integer[] { 2, null })
1854 >    {
1855 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1856 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1857 >        final IncAction r = new IncAction();
1858 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1859 >
1860 >        f.complete(v1);
1861 >        checkCompletedNormally(h, null);
1862 >        assertEquals(inc(v1), r.value);
1863 >        g.complete(v2);
1864 >
1865 >        checkCompletedNormally(f, v1);
1866 >        checkCompletedNormally(g, v2);
1867 >        checkCompletedNormally(h, null);
1868 >    }}
1869 >
1870 >    public void testAcceptEither_normalCompletion2() {
1871 >        for (ExecutionMode m : ExecutionMode.values())
1872 >        for (Integer v1 : new Integer[] { 1, null })
1873 >        for (Integer v2 : new Integer[] { 2, null })
1874 >    {
1875 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1876 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1877 >        final IncAction r = new IncAction();
1878 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1879 >
1880 >        g.complete(v2);
1881 >        checkCompletedNormally(h, null);
1882 >        assertEquals(inc(v2), r.value);
1883 >        f.complete(v1);
1884 >
1885 >        checkCompletedNormally(f, v1);
1886 >        checkCompletedNormally(g, v2);
1887 >        checkCompletedNormally(h, null);
1888 >    }}
1889 >
1890 >    public void testAcceptEither_normalCompletion3() {
1891 >        for (ExecutionMode m : ExecutionMode.values())
1892 >        for (Integer v1 : new Integer[] { 1, null })
1893 >        for (Integer v2 : new Integer[] { 2, null })
1894 >    {
1895 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1896 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1897 >        final IncAction r = new IncAction();
1898 >
1899 >        f.complete(v1);
1900 >        g.complete(v2);
1901 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1902 >
1903 >        checkCompletedNormally(h, null);
1904 >        checkCompletedNormally(f, v1);
1905 >        checkCompletedNormally(g, v2);
1906 >
1907 >        // unspecified behavior
1908 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
1909 >                   Objects.equals(r.value, inc(v2)));
1910 >    }}
1911  
1912      /**
1913 <     * thenCombineAsync result completes exceptionally if either source cancelled
1913 >     * acceptEither result completes exceptionally after exceptional
1914 >     * completion of either source
1915       */
1916 <    public void testThenCombineAsync4E() {
1917 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1918 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1919 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1920 <        assertTrue(f.cancel(true));
1921 <        f2.complete(two);
1922 <        checkCompletedWithWrappedCancellationException(g);
1916 >    public void testAcceptEither_exceptionalCompletion1() {
1917 >        for (ExecutionMode m : ExecutionMode.values())
1918 >        for (Integer v1 : new Integer[] { 1, null })
1919 >    {
1920 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1921 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1922 >        final IncAction r = new IncAction();
1923 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1924 >        final CFException ex = new CFException();
1925 >
1926 >        f.completeExceptionally(ex);
1927 >        checkCompletedWithWrappedCFException(h, ex);
1928 >        g.complete(v1);
1929 >
1930 >        assertEquals(0, r.invocationCount);
1931 >        checkCompletedNormally(g, v1);
1932 >        checkCompletedWithWrappedCFException(f, ex);
1933 >        checkCompletedWithWrappedCFException(h, ex);
1934 >    }}
1935 >
1936 >    public void testAcceptEither_exceptionalCompletion2() {
1937 >        for (ExecutionMode m : ExecutionMode.values())
1938 >        for (Integer v1 : new Integer[] { 1, null })
1939 >    {
1940 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1941 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1942 >        final IncAction r = new IncAction();
1943 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1944 >        final CFException ex = new CFException();
1945 >
1946 >        g.completeExceptionally(ex);
1947 >        checkCompletedWithWrappedCFException(h, ex);
1948 >        f.complete(v1);
1949 >
1950 >        assertEquals(0, r.invocationCount);
1951 >        checkCompletedNormally(f, v1);
1952 >        checkCompletedWithWrappedCFException(g, ex);
1953 >        checkCompletedWithWrappedCFException(h, ex);
1954 >    }}
1955 >
1956 >    public void testAcceptEither_exceptionalCompletion3() {
1957 >        for (ExecutionMode m : ExecutionMode.values())
1958 >        for (Integer v1 : new Integer[] { 1, null })
1959 >    {
1960 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1961 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1962 >        final IncAction r = new IncAction();
1963 >        final CFException ex = new CFException();
1964 >
1965 >        g.completeExceptionally(ex);
1966 >        f.complete(v1);
1967 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1968  
1969 <        f = new CompletableFuture<Integer>();
1970 <        f2 = new CompletableFuture<Integer>();
1971 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1972 <        f.complete(one);
1973 <        assertTrue(f2.cancel(true));
1974 <        checkCompletedWithWrappedCancellationException(g);
1975 <    }
1969 >        // unspecified behavior
1970 >        Integer v;
1971 >        try {
1972 >            assertNull(h.join());
1973 >            assertEquals(1, r.invocationCount);
1974 >            assertEquals(inc(v1), r.value);
1975 >        } catch (CompletionException ok) {
1976 >            checkCompletedWithWrappedCFException(h, ex);
1977 >            assertEquals(0, r.invocationCount);
1978 >        }
1979  
1980 <    /**
1981 <     * thenAcceptBothAsync result completes normally after normal
1982 <     * completion of sources
1983 <     */
1984 <    public void testThenAcceptBothAsyncE() {
1985 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1986 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1987 <        AddAction r = new AddAction();
1988 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1989 <        f.complete(one);
1990 <        checkIncomplete(g);
1991 <        f2.complete(two);
1992 <        checkCompletedNormally(g, null);
1993 <        assertEquals(r.value, 3);
1994 <    }
1980 >        checkCompletedWithWrappedCFException(g, ex);
1981 >        checkCompletedNormally(f, v1);
1982 >    }}
1983 >
1984 >    public void testAcceptEither_exceptionalCompletion4() {
1985 >        for (ExecutionMode m : ExecutionMode.values())
1986 >        for (Integer v1 : new Integer[] { 1, null })
1987 >    {
1988 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1989 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1990 >        final IncAction r = new IncAction();
1991 >        final CFException ex = new CFException();
1992 >
1993 >        f.completeExceptionally(ex);
1994 >        g.complete(v1);
1995 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1996  
1997 <    /**
1998 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1999 <     * completion of source
2000 <     */
2001 <    public void testThenAcceptBothAsync2E() {
2002 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2003 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2004 <        AddAction r = new AddAction();
2005 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2006 <        f.completeExceptionally(new CFException());
1978 <        f2.complete(two);
1979 <        checkCompletedWithWrappedCFException(g);
1997 >        // unspecified behavior
1998 >        Integer v;
1999 >        try {
2000 >            assertNull(h.join());
2001 >            assertEquals(1, r.invocationCount);
2002 >            assertEquals(inc(v1), r.value);
2003 >        } catch (CompletionException ok) {
2004 >            checkCompletedWithWrappedCFException(h, ex);
2005 >            assertEquals(0, r.invocationCount);
2006 >        }
2007  
2008 <        r = new AddAction();
2009 <        f = new CompletableFuture<Integer>();
2010 <        f2 = new CompletableFuture<Integer>();
1984 <        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1985 <        f.complete(one);
1986 <        f2.completeExceptionally(new CFException());
1987 <        checkCompletedWithWrappedCFException(g);
1988 <    }
2008 >        checkCompletedWithWrappedCFException(f, ex);
2009 >        checkCompletedNormally(g, v1);
2010 >    }}
2011  
2012      /**
2013 <     * thenAcceptBothAsync result completes exceptionally if action does
2013 >     * acceptEither result completes exceptionally if action does
2014       */
2015 <    public void testThenAcceptBothAsync3E() {
2016 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2017 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2018 <        FailingBiConsumer r = new FailingBiConsumer();
2019 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2020 <        f.complete(one);
2021 <        checkIncomplete(g);
2022 <        f2.complete(two);
2023 <        checkCompletedWithWrappedCFException(g);
2024 <    }
2015 >    public void testAcceptEither_actionFailed1() {
2016 >        for (ExecutionMode m : ExecutionMode.values())
2017 >        for (Integer v1 : new Integer[] { 1, null })
2018 >        for (Integer v2 : new Integer[] { 2, null })
2019 >    {
2020 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2021 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2022 >        final FailingConsumer r = new FailingConsumer();
2023 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2024 >
2025 >        f.complete(v1);
2026 >        checkCompletedWithWrappedCFException(h);
2027 >        g.complete(v2);
2028 >        checkCompletedNormally(f, v1);
2029 >        checkCompletedNormally(g, v2);
2030 >    }}
2031 >
2032 >    public void testAcceptEither_actionFailed2() {
2033 >        for (ExecutionMode m : ExecutionMode.values())
2034 >        for (Integer v1 : new Integer[] { 1, null })
2035 >        for (Integer v2 : new Integer[] { 2, null })
2036 >    {
2037 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2038 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 >        final FailingConsumer r = new FailingConsumer();
2040 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2041 >
2042 >        g.complete(v2);
2043 >        checkCompletedWithWrappedCFException(h);
2044 >        f.complete(v1);
2045 >        checkCompletedNormally(f, v1);
2046 >        checkCompletedNormally(g, v2);
2047 >    }}
2048  
2049      /**
2050 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2050 >     * acceptEither result completes exceptionally if either source cancelled
2051       */
2052 <    public void testThenAcceptBothAsync4E() {
2053 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2054 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2055 <        AddAction r = new AddAction();
2056 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2057 <        assertTrue(f.cancel(true));
2058 <        f2.complete(two);
2059 <        checkCompletedWithWrappedCancellationException(g);
2052 >    public void testAcceptEither_sourceCancelled1() {
2053 >        for (ExecutionMode m : ExecutionMode.values())
2054 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2055 >        for (Integer v1 : new Integer[] { 1, null })
2056 >    {
2057 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 >        final IncAction r = new IncAction();
2060 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2061 >
2062 >        assertTrue(f.cancel(mayInterruptIfRunning));
2063 >        checkCompletedWithWrappedCancellationException(h);
2064 >        g.complete(v1);
2065  
2066 <        r = new AddAction();
2067 <        f = new CompletableFuture<Integer>();
2068 <        f2 = new CompletableFuture<Integer>();
2069 <        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2070 <        f.complete(one);
2071 <        assertTrue(f2.cancel(true));
2072 <        checkCompletedWithWrappedCancellationException(g);
2073 <    }
2066 >        checkCancelled(f);
2067 >        assertEquals(0, r.invocationCount);
2068 >        checkCompletedNormally(g, v1);
2069 >        checkCompletedWithWrappedCancellationException(h);
2070 >    }}
2071 >
2072 >    public void testAcceptEither_sourceCancelled2() {
2073 >        for (ExecutionMode m : ExecutionMode.values())
2074 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2075 >        for (Integer v1 : new Integer[] { 1, null })
2076 >    {
2077 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2078 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2079 >        final IncAction r = new IncAction();
2080 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2081 >
2082 >        assertTrue(g.cancel(mayInterruptIfRunning));
2083 >        checkCompletedWithWrappedCancellationException(h);
2084 >        f.complete(v1);
2085 >
2086 >        checkCancelled(g);
2087 >        assertEquals(0, r.invocationCount);
2088 >        checkCompletedNormally(f, v1);
2089 >        checkCompletedWithWrappedCancellationException(h);
2090 >    }}
2091 >
2092 >    public void testAcceptEither_sourceCancelled3() {
2093 >        for (ExecutionMode m : ExecutionMode.values())
2094 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2095 >        for (Integer v1 : new Integer[] { 1, null })
2096 >    {
2097 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2098 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2099 >        final IncAction r = new IncAction();
2100 >
2101 >        assertTrue(g.cancel(mayInterruptIfRunning));
2102 >        f.complete(v1);
2103 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2104  
2105 <    /**
2106 <     * runAfterBothAsync result completes normally after normal
2107 <     * completion of sources
2108 <     */
2109 <    public void testRunAfterBothAsyncE() {
2110 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2111 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2112 <        Noop r = new Noop();
2113 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2114 <        f.complete(one);
2035 <        checkIncomplete(g);
2036 <        f2.complete(two);
2037 <        checkCompletedNormally(g, null);
2038 <        assertTrue(r.ran);
2039 <    }
2105 >        // unspecified behavior
2106 >        Integer v;
2107 >        try {
2108 >            assertNull(h.join());
2109 >            assertEquals(1, r.invocationCount);
2110 >            assertEquals(inc(v1), r.value);
2111 >        } catch (CompletionException ok) {
2112 >            checkCompletedWithWrappedCancellationException(h);
2113 >            assertEquals(0, r.invocationCount);
2114 >        }
2115  
2116 <    /**
2117 <     * runAfterBothAsync result completes exceptionally after exceptional
2118 <     * completion of source
2119 <     */
2120 <    public void testRunAfterBothAsync2E() {
2121 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2122 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2123 <        Noop r = new Noop();
2124 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2125 <        f.completeExceptionally(new CFException());
2126 <        f2.complete(two);
2127 <        checkCompletedWithWrappedCFException(g);
2116 >        checkCancelled(g);
2117 >        checkCompletedNormally(f, v1);
2118 >    }}
2119 >
2120 >    public void testAcceptEither_sourceCancelled4() {
2121 >        for (ExecutionMode m : ExecutionMode.values())
2122 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2123 >        for (Integer v1 : new Integer[] { 1, null })
2124 >    {
2125 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2126 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2127 >        final IncAction r = new IncAction();
2128 >
2129 >        assertTrue(f.cancel(mayInterruptIfRunning));
2130 >        g.complete(v1);
2131 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2132  
2133 <        r = new Noop();
2134 <        f = new CompletableFuture<Integer>();
2135 <        f2 = new CompletableFuture<Integer>();
2136 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2137 <        f.complete(one);
2138 <        f2.completeExceptionally(new CFException());
2139 <        checkCompletedWithWrappedCFException(g);
2140 <    }
2133 >        // unspecified behavior
2134 >        Integer v;
2135 >        try {
2136 >            assertNull(h.join());
2137 >            assertEquals(1, r.invocationCount);
2138 >            assertEquals(inc(v1), r.value);
2139 >        } catch (CompletionException ok) {
2140 >            checkCompletedWithWrappedCancellationException(h);
2141 >            assertEquals(0, r.invocationCount);
2142 >        }
2143  
2144 <    /**
2145 <     * runAfterBothAsync result completes exceptionally if action does
2146 <     */
2066 <    public void testRunAfterBothAsync3E() {
2067 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2068 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2069 <        FailingNoop r = new FailingNoop();
2070 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2071 <        f.complete(one);
2072 <        checkIncomplete(g);
2073 <        f2.complete(two);
2074 <        checkCompletedWithWrappedCFException(g);
2075 <    }
2144 >        checkCancelled(f);
2145 >        checkCompletedNormally(g, v1);
2146 >    }}
2147  
2148      /**
2149 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2149 >     * runAfterEither result completes normally after normal completion
2150 >     * of either source
2151       */
2152 <    public void testRunAfterBothAsync4E() {
2153 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2154 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2155 <        Noop r = new Noop();
2156 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2157 <        assertTrue(f.cancel(true));
2158 <        f2.complete(two);
2159 <        checkCompletedWithWrappedCancellationException(g);
2160 <
2161 <        r = new Noop();
2162 <        f = new CompletableFuture<Integer>();
2163 <        f2 = new CompletableFuture<Integer>();
2164 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2165 <        f.complete(one);
2166 <        assertTrue(f2.cancel(true));
2167 <        checkCompletedWithWrappedCancellationException(g);
2168 <    }
2152 >    public void testRunAfterEither_normalCompletion1() {
2153 >        for (ExecutionMode m : ExecutionMode.values())
2154 >        for (Integer v1 : new Integer[] { 1, null })
2155 >        for (Integer v2 : new Integer[] { 2, null })
2156 >    {
2157 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2158 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 >        final Noop r = new Noop(m);
2160 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2161 >
2162 >        f.complete(v1);
2163 >        checkCompletedNormally(h, null);
2164 >        assertEquals(1, r.invocationCount);
2165 >        g.complete(v2);
2166 >
2167 >        checkCompletedNormally(f, v1);
2168 >        checkCompletedNormally(g, v2);
2169 >        checkCompletedNormally(h, null);
2170 >        assertEquals(1, r.invocationCount);
2171 >    }}
2172 >
2173 >    public void testRunAfterEither_normalCompletion2() {
2174 >        for (ExecutionMode m : ExecutionMode.values())
2175 >        for (Integer v1 : new Integer[] { 1, null })
2176 >        for (Integer v2 : new Integer[] { 2, null })
2177 >    {
2178 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180 >        final Noop r = new Noop(m);
2181 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2182 >
2183 >        g.complete(v2);
2184 >        checkCompletedNormally(h, null);
2185 >        assertEquals(1, r.invocationCount);
2186 >        f.complete(v1);
2187 >
2188 >        checkCompletedNormally(f, v1);
2189 >        checkCompletedNormally(g, v2);
2190 >        checkCompletedNormally(h, null);
2191 >        assertEquals(1, r.invocationCount);
2192 >        }}
2193 >
2194 >    public void testRunAfterEither_normalCompletion3() {
2195 >        for (ExecutionMode m : ExecutionMode.values())
2196 >        for (Integer v1 : new Integer[] { 1, null })
2197 >        for (Integer v2 : new Integer[] { 2, null })
2198 >    {
2199 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2200 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2201 >        final Noop r = new Noop(m);
2202 >
2203 >        f.complete(v1);
2204 >        g.complete(v2);
2205 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2206 >
2207 >        checkCompletedNormally(h, null);
2208 >        checkCompletedNormally(f, v1);
2209 >        checkCompletedNormally(g, v2);
2210 >        assertEquals(1, r.invocationCount);
2211 >    }}
2212  
2213      /**
2214 <     * applyToEitherAsync result completes normally after normal
2215 <     * completion of sources
2214 >     * runAfterEither result completes exceptionally after exceptional
2215 >     * completion of either source
2216       */
2217 <    public void testApplyToEitherAsyncE() {
2218 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2219 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2220 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2221 <        f.complete(one);
2222 <        checkCompletedNormally(g, two);
2217 >    public void testRunAfterEither_exceptionalCompletion1() {
2218 >        for (ExecutionMode m : ExecutionMode.values())
2219 >        for (Integer v1 : new Integer[] { 1, null })
2220 >    {
2221 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2222 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2223 >        final Noop r = new Noop(m);
2224 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2225 >        final CFException ex = new CFException();
2226 >
2227 >        f.completeExceptionally(ex);
2228 >        checkCompletedWithWrappedCFException(h, ex);
2229 >        g.complete(v1);
2230 >
2231 >        assertEquals(0, r.invocationCount);
2232 >        checkCompletedNormally(g, v1);
2233 >        checkCompletedWithWrappedCFException(f, ex);
2234 >        checkCompletedWithWrappedCFException(h, ex);
2235 >    }}
2236 >
2237 >    public void testRunAfterEither_exceptionalCompletion2() {
2238 >        for (ExecutionMode m : ExecutionMode.values())
2239 >        for (Integer v1 : new Integer[] { 1, null })
2240 >    {
2241 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2242 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2243 >        final Noop r = new Noop(m);
2244 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2245 >        final CFException ex = new CFException();
2246 >
2247 >        g.completeExceptionally(ex);
2248 >        checkCompletedWithWrappedCFException(h, ex);
2249 >        f.complete(v1);
2250 >
2251 >        assertEquals(0, r.invocationCount);
2252 >        checkCompletedNormally(f, v1);
2253 >        checkCompletedWithWrappedCFException(g, ex);
2254 >        checkCompletedWithWrappedCFException(h, ex);
2255 >    }}
2256 >
2257 >    public void testRunAfterEither_exceptionalCompletion3() {
2258 >        for (ExecutionMode m : ExecutionMode.values())
2259 >        for (Integer v1 : new Integer[] { 1, null })
2260 >    {
2261 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2262 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2263 >        final Noop r = new Noop(m);
2264 >        final CFException ex = new CFException();
2265 >
2266 >        g.completeExceptionally(ex);
2267 >        f.complete(v1);
2268 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2269  
2270 <        f = new CompletableFuture<Integer>();
2271 <        f.complete(one);
2272 <        f2 = new CompletableFuture<Integer>();
2273 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2274 <        checkCompletedNormally(g, two);
2275 <    }
2270 >        // unspecified behavior
2271 >        Integer v;
2272 >        try {
2273 >            assertNull(h.join());
2274 >            assertEquals(1, r.invocationCount);
2275 >        } catch (CompletionException ok) {
2276 >            checkCompletedWithWrappedCFException(h, ex);
2277 >            assertEquals(0, r.invocationCount);
2278 >        }
2279  
2280 <    /**
2281 <     * applyToEitherAsync result completes exceptionally after exceptional
2282 <     * completion of source
2283 <     */
2284 <    public void testApplyToEitherAsync2E() {
2285 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2286 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2287 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2288 <        f.completeExceptionally(new CFException());
2289 <        checkCompletedWithWrappedCFException(g);
2280 >        checkCompletedWithWrappedCFException(g, ex);
2281 >        checkCompletedNormally(f, v1);
2282 >    }}
2283 >
2284 >    public void testRunAfterEither_exceptionalCompletion4() {
2285 >        for (ExecutionMode m : ExecutionMode.values())
2286 >        for (Integer v1 : new Integer[] { 1, null })
2287 >    {
2288 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2289 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2290 >        final Noop r = new Noop(m);
2291 >        final CFException ex = new CFException();
2292 >
2293 >        f.completeExceptionally(ex);
2294 >        g.complete(v1);
2295 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2296  
2297 <        f = new CompletableFuture<Integer>();
2298 <        f2 = new CompletableFuture<Integer>();
2299 <        f2.completeExceptionally(new CFException());
2300 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2301 <        f.complete(one);
2302 <        checkCompletedWithWrappedCFException(g);
2303 <    }
2297 >        // unspecified behavior
2298 >        Integer v;
2299 >        try {
2300 >            assertNull(h.join());
2301 >            assertEquals(1, r.invocationCount);
2302 >        } catch (CompletionException ok) {
2303 >            checkCompletedWithWrappedCFException(h, ex);
2304 >            assertEquals(0, r.invocationCount);
2305 >        }
2306  
2307 <    /**
2308 <     * applyToEitherAsync result completes exceptionally if action does
2309 <     */
2138 <    public void testApplyToEitherAsync3E() {
2139 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2140 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2141 <        FailingFunction r = new FailingFunction();
2142 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2143 <        f.complete(one);
2144 <        checkCompletedWithWrappedCFException(g);
2145 <    }
2307 >        checkCompletedWithWrappedCFException(f, ex);
2308 >        checkCompletedNormally(g, v1);
2309 >    }}
2310  
2311      /**
2312 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2312 >     * runAfterEither result completes exceptionally if action does
2313       */
2314 <    public void testApplyToEitherAsync4E() {
2315 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2316 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2317 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2318 <        assertTrue(f.cancel(true));
2319 <        checkCompletedWithWrappedCancellationException(g);
2320 <
2321 <        f = new CompletableFuture<Integer>();
2322 <        f2 = new CompletableFuture<Integer>();
2323 <        assertTrue(f2.cancel(true));
2324 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2325 <        checkCompletedWithWrappedCancellationException(g);
2326 <    }
2314 >    public void testRunAfterEither_actionFailed1() {
2315 >        for (ExecutionMode m : ExecutionMode.values())
2316 >        for (Integer v1 : new Integer[] { 1, null })
2317 >        for (Integer v2 : new Integer[] { 2, null })
2318 >    {
2319 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2320 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 >        final FailingRunnable r = new FailingRunnable();
2322 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2323 >
2324 >        f.complete(v1);
2325 >        checkCompletedWithWrappedCFException(h);
2326 >        g.complete(v2);
2327 >        checkCompletedNormally(f, v1);
2328 >        checkCompletedNormally(g, v2);
2329 >    }}
2330 >
2331 >    public void testRunAfterEither_actionFailed2() {
2332 >        for (ExecutionMode m : ExecutionMode.values())
2333 >        for (Integer v1 : new Integer[] { 1, null })
2334 >        for (Integer v2 : new Integer[] { 2, null })
2335 >    {
2336 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2337 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2338 >        final FailingRunnable r = new FailingRunnable();
2339 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2340 >
2341 >        g.complete(v2);
2342 >        checkCompletedWithWrappedCFException(h);
2343 >        f.complete(v1);
2344 >        checkCompletedNormally(f, v1);
2345 >        checkCompletedNormally(g, v2);
2346 >    }}
2347  
2348      /**
2349 <     * acceptEitherAsync result completes normally after normal
2166 <     * completion of sources
2349 >     * runAfterEither result completes exceptionally if either source cancelled
2350       */
2351 <    public void testAcceptEitherAsyncE() {
2352 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2353 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2354 <        IncAction r = new IncAction();
2355 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2356 <        f.complete(one);
2357 <        checkCompletedNormally(g, null);
2358 <        assertEquals(r.value, 2);
2351 >    public void testRunAfterEither_sourceCancelled1() {
2352 >        for (ExecutionMode m : ExecutionMode.values())
2353 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2354 >        for (Integer v1 : new Integer[] { 1, null })
2355 >    {
2356 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2357 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2358 >        final Noop r = new Noop(m);
2359 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2360 >
2361 >        assertTrue(f.cancel(mayInterruptIfRunning));
2362 >        checkCompletedWithWrappedCancellationException(h);
2363 >        g.complete(v1);
2364  
2365 <        r = new IncAction();
2366 <        f = new CompletableFuture<Integer>();
2367 <        f.complete(one);
2368 <        f2 = new CompletableFuture<Integer>();
2369 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2370 <        checkCompletedNormally(g, null);
2371 <        assertEquals(r.value, 2);
2372 <    }
2373 <
2374 <    /**
2375 <     * acceptEitherAsync result completes exceptionally after exceptional
2376 <     * completion of source
2377 <     */
2378 <    public void testAcceptEitherAsync2E() {
2379 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2380 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2381 <        IncAction r = new IncAction();
2382 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2383 <        f.completeExceptionally(new CFException());
2384 <        checkCompletedWithWrappedCFException(g);
2365 >        checkCancelled(f);
2366 >        assertEquals(0, r.invocationCount);
2367 >        checkCompletedNormally(g, v1);
2368 >        checkCompletedWithWrappedCancellationException(h);
2369 >    }}
2370 >
2371 >    public void testRunAfterEither_sourceCancelled2() {
2372 >        for (ExecutionMode m : ExecutionMode.values())
2373 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2374 >        for (Integer v1 : new Integer[] { 1, null })
2375 >    {
2376 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2377 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2378 >        final Noop r = new Noop(m);
2379 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2380 >
2381 >        assertTrue(g.cancel(mayInterruptIfRunning));
2382 >        checkCompletedWithWrappedCancellationException(h);
2383 >        f.complete(v1);
2384 >
2385 >        checkCancelled(g);
2386 >        assertEquals(0, r.invocationCount);
2387 >        checkCompletedNormally(f, v1);
2388 >        checkCompletedWithWrappedCancellationException(h);
2389 >    }}
2390 >
2391 >    public void testRunAfterEither_sourceCancelled3() {
2392 >        for (ExecutionMode m : ExecutionMode.values())
2393 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2394 >        for (Integer v1 : new Integer[] { 1, null })
2395 >    {
2396 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2397 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2398 >        final Noop r = new Noop(m);
2399 >
2400 >        assertTrue(g.cancel(mayInterruptIfRunning));
2401 >        f.complete(v1);
2402 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2403  
2404 <        r = new IncAction();
2405 <        f = new CompletableFuture<Integer>();
2406 <        f2 = new CompletableFuture<Integer>();
2407 <        f2.completeExceptionally(new CFException());
2408 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2409 <        f.complete(one);
2410 <        checkCompletedWithWrappedCFException(g);
2411 <    }
2404 >        // unspecified behavior
2405 >        Integer v;
2406 >        try {
2407 >            assertNull(h.join());
2408 >            assertEquals(1, r.invocationCount);
2409 >        } catch (CompletionException ok) {
2410 >            checkCompletedWithWrappedCancellationException(h);
2411 >            assertEquals(0, r.invocationCount);
2412 >        }
2413  
2414 <    /**
2415 <     * acceptEitherAsync result completes exceptionally if action does
2416 <     */
2417 <    public void testAcceptEitherAsync3E() {
2418 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2419 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2420 <        FailingConsumer r = new FailingConsumer();
2421 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2422 <        f.complete(one);
2423 <        checkCompletedWithWrappedCFException(g);
2424 <    }
2414 >        checkCancelled(g);
2415 >        checkCompletedNormally(f, v1);
2416 >    }}
2417 >
2418 >    public void testRunAfterEither_sourceCancelled4() {
2419 >        for (ExecutionMode m : ExecutionMode.values())
2420 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2421 >        for (Integer v1 : new Integer[] { 1, null })
2422 >    {
2423 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2424 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2425 >        final Noop r = new Noop(m);
2426 >
2427 >        assertTrue(f.cancel(mayInterruptIfRunning));
2428 >        g.complete(v1);
2429 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2430  
2431 <    /**
2432 <     * acceptEitherAsync result completes exceptionally if either
2433 <     * source cancelled
2434 <     */
2435 <    public void testAcceptEitherAsync4E() {
2436 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2437 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2438 <        IncAction r = new IncAction();
2439 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2228 <        assertTrue(f.cancel(true));
2229 <        checkCompletedWithWrappedCancellationException(g);
2431 >        // unspecified behavior
2432 >        Integer v;
2433 >        try {
2434 >            assertNull(h.join());
2435 >            assertEquals(1, r.invocationCount);
2436 >        } catch (CompletionException ok) {
2437 >            checkCompletedWithWrappedCancellationException(h);
2438 >            assertEquals(0, r.invocationCount);
2439 >        }
2440  
2441 <        r = new IncAction();
2442 <        f = new CompletableFuture<Integer>();
2443 <        f2 = new CompletableFuture<Integer>();
2234 <        assertTrue(f2.cancel(true));
2235 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2236 <        checkCompletedWithWrappedCancellationException(g);
2237 <    }
2441 >        checkCancelled(f);
2442 >        checkCompletedNormally(g, v1);
2443 >    }}
2444  
2445      /**
2446 <     * runAfterEitherAsync result completes normally after normal
2241 <     * completion of sources
2446 >     * thenCompose result completes normally after normal completion of source
2447       */
2448 <    public void testRunAfterEitherAsyncE() {
2449 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2450 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2451 <        Noop r = new Noop();
2452 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2453 <        f.complete(one);
2454 <        checkCompletedNormally(g, null);
2455 <        assertTrue(r.ran);
2456 <
2457 <        r = new Noop();
2458 <        f = new CompletableFuture<Integer>();
2459 <        f.complete(one);
2460 <        f2 = new CompletableFuture<Integer>();
2461 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2462 <        checkCompletedNormally(g, null);
2258 <        assertTrue(r.ran);
2259 <    }
2448 >    public void testThenCompose_normalCompletion() {
2449 >        for (ExecutionMode m : ExecutionMode.values())
2450 >        for (boolean createIncomplete : new boolean[] { true, false })
2451 >        for (Integer v1 : new Integer[] { 1, null })
2452 >    {
2453 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2454 >        final CompletableFutureInc r = new CompletableFutureInc();
2455 >        if (!createIncomplete) f.complete(v1);
2456 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2457 >        if (createIncomplete) f.complete(v1);
2458 >
2459 >        checkCompletedNormally(g, inc(v1));
2460 >        checkCompletedNormally(f, v1);
2461 >        assertEquals(1, r.invocationCount);
2462 >    }}
2463  
2464      /**
2465 <     * runAfterEitherAsync result completes exceptionally after exceptional
2465 >     * thenCompose result completes exceptionally after exceptional
2466       * completion of source
2467       */
2468 <    public void testRunAfterEitherAsync2E() {
2469 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2470 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2471 <        Noop r = new Noop();
2472 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2473 <        f.completeExceptionally(new CFException());
2474 <        checkCompletedWithWrappedCFException(g);
2475 <
2476 <        r = new Noop();
2477 <        f = new CompletableFuture<Integer>();
2478 <        f2 = new CompletableFuture<Integer>();
2479 <        f2.completeExceptionally(new CFException());
2480 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2481 <        f.complete(one);
2482 <        checkCompletedWithWrappedCFException(g);
2280 <    }
2281 <
2282 <    /**
2283 <     * runAfterEitherAsync result completes exceptionally if action does
2284 <     */
2285 <    public void testRunAfterEitherAsync3E() {
2286 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2287 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2288 <        FailingNoop r = new FailingNoop();
2289 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2290 <        f.complete(one);
2291 <        checkCompletedWithWrappedCFException(g);
2292 <    }
2468 >    public void testThenCompose_exceptionalCompletion() {
2469 >        for (ExecutionMode m : ExecutionMode.values())
2470 >        for (boolean createIncomplete : new boolean[] { true, false })
2471 >    {
2472 >        final CFException ex = new CFException();
2473 >        final CompletableFutureInc r = new CompletableFutureInc();
2474 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2475 >        if (!createIncomplete) f.completeExceptionally(ex);
2476 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2477 >        if (createIncomplete) f.completeExceptionally(ex);
2478 >
2479 >        checkCompletedWithWrappedCFException(g, ex);
2480 >        checkCompletedWithWrappedCFException(f, ex);
2481 >        assertEquals(0, r.invocationCount);
2482 >    }}
2483  
2484      /**
2485 <     * runAfterEitherAsync result completes exceptionally if either
2296 <     * source cancelled
2297 <     */
2298 <    public void testRunAfterEitherAsync4E() {
2299 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2300 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2301 <        Noop r = new Noop();
2302 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2303 <        assertTrue(f.cancel(true));
2304 <        checkCompletedWithWrappedCancellationException(g);
2305 <
2306 <        r = new Noop();
2307 <        f = new CompletableFuture<Integer>();
2308 <        f2 = new CompletableFuture<Integer>();
2309 <        assertTrue(f2.cancel(true));
2310 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2311 <        checkCompletedWithWrappedCancellationException(g);
2312 <    }
2313 <
2314 <    /**
2315 <     * thenComposeAsync result completes normally after normal
2316 <     * completion of source
2485 >     * thenCompose result completes exceptionally if action does
2486       */
2487 <    public void testThenComposeAsyncE() {
2488 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2489 <        CompletableFutureInc r = new CompletableFutureInc();
2490 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2491 <        f.complete(one);
2492 <        checkCompletedNormally(g, two);
2493 <    }
2487 >    public void testThenCompose_actionFailed() {
2488 >        for (ExecutionMode m : ExecutionMode.values())
2489 >        for (boolean createIncomplete : new boolean[] { true, false })
2490 >        for (Integer v1 : new Integer[] { 1, null })
2491 >    {
2492 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2493 >        final FailingCompletableFutureFunction r
2494 >            = new FailingCompletableFutureFunction();
2495 >        if (!createIncomplete) f.complete(v1);
2496 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2497 >        if (createIncomplete) f.complete(v1);
2498  
2326    /**
2327     * thenComposeAsync result completes exceptionally after
2328     * exceptional completion of source
2329     */
2330    public void testThenComposeAsync2E() {
2331        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2332        CompletableFutureInc r = new CompletableFutureInc();
2333        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2334        f.completeExceptionally(new CFException());
2499          checkCompletedWithWrappedCFException(g);
2500 <    }
2500 >        checkCompletedNormally(f, v1);
2501 >    }}
2502  
2503      /**
2504 <     * thenComposeAsync result completes exceptionally if action does
2504 >     * thenCompose result completes exceptionally if source cancelled
2505       */
2506 <    public void testThenComposeAsync3E() {
2507 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2508 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2509 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2510 <        f.complete(one);
2511 <        checkCompletedWithWrappedCFException(g);
2512 <    }
2506 >    public void testThenCompose_sourceCancelled() {
2507 >        for (ExecutionMode m : ExecutionMode.values())
2508 >        for (boolean createIncomplete : new boolean[] { true, false })
2509 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2510 >    {
2511 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 >        final CompletableFutureInc r = new CompletableFutureInc();
2513 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2514 >        final CompletableFuture<Integer> g = f.thenCompose(r);
2515 >        if (createIncomplete) {
2516 >            checkIncomplete(g);
2517 >            assertTrue(f.cancel(mayInterruptIfRunning));
2518 >        }
2519  
2349    /**
2350     * thenComposeAsync result completes exceptionally if source cancelled
2351     */
2352    public void testThenComposeAsync4E() {
2353        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2354        CompletableFutureInc r = new CompletableFutureInc();
2355        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2356        assertTrue(f.cancel(true));
2520          checkCompletedWithWrappedCancellationException(g);
2521 <    }
2521 >        checkCancelled(f);
2522 >    }}
2523  
2524      // other static methods
2525  
# Line 2364 | Line 2528 | public class CompletableFutureTest exten
2528       * with the value null
2529       */
2530      public void testAllOf_empty() throws Exception {
2531 <        CompletableFuture<?> f = CompletableFuture.allOf();
2531 >        CompletableFuture<Void> f = CompletableFuture.allOf();
2532          checkCompletedNormally(f, null);
2533      }
2534  
2535      /**
2536 <     * allOf returns a future completed when all components complete
2536 >     * allOf returns a future completed normally with the value null
2537 >     * when all components complete normally
2538       */
2539 <    public void testAllOf() throws Exception {
2539 >    public void testAllOf_normal() throws Exception {
2540          for (int k = 1; k < 20; ++k) {
2541 <            CompletableFuture[] fs = new CompletableFuture[k];
2541 >            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2542              for (int i = 0; i < k; ++i)
2543 <                fs[i] = new CompletableFuture<Integer>();
2543 >                fs[i] = new CompletableFuture<>();
2544              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2545              for (int i = 0; i < k; ++i) {
2546                  checkIncomplete(f);
2547 +                checkIncomplete(CompletableFuture.allOf(fs));
2548                  fs[i].complete(one);
2549              }
2550              checkCompletedNormally(f, null);
2551 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2552          }
2553      }
2554  
# Line 2389 | Line 2556 | public class CompletableFutureTest exten
2556       * anyOf(no component futures) returns an incomplete future
2557       */
2558      public void testAnyOf_empty() throws Exception {
2559 <        CompletableFuture<?> f = CompletableFuture.anyOf();
2559 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
2560          checkIncomplete(f);
2561      }
2562  
2563      /**
2564 <     * anyOf returns a future completed when any components complete
2564 >     * anyOf returns a future completed normally with a value when
2565 >     * a component future does
2566       */
2567 <    public void testAnyOf() throws Exception {
2568 <        for (int k = 1; k < 20; ++k) {
2567 >    public void testAnyOf_normal() throws Exception {
2568 >        for (int k = 0; k < 10; ++k) {
2569              CompletableFuture[] fs = new CompletableFuture[k];
2570              for (int i = 0; i < k; ++i)
2571 <                fs[i] = new CompletableFuture<Integer>();
2571 >                fs[i] = new CompletableFuture<>();
2572              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2573              checkIncomplete(f);
2574              for (int i = 0; i < k; ++i) {
2575                  fs[i].complete(one);
2576                  checkCompletedNormally(f, one);
2577 +                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2578 +            }
2579 +        }
2580 +    }
2581 +
2582 +    /**
2583 +     * anyOf result completes exceptionally when any component does.
2584 +     */
2585 +    public void testAnyOf_exceptional() throws Exception {
2586 +        for (int k = 0; k < 10; ++k) {
2587 +            CompletableFuture[] fs = new CompletableFuture[k];
2588 +            for (int i = 0; i < k; ++i)
2589 +                fs[i] = new CompletableFuture<>();
2590 +            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2591 +            checkIncomplete(f);
2592 +            for (int i = 0; i < k; ++i) {
2593 +                fs[i].completeExceptionally(new CFException());
2594 +                checkCompletedWithWrappedCFException(f);
2595 +                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
2596              }
2597          }
2598      }
# Line 2414 | Line 2601 | public class CompletableFutureTest exten
2601       * Completion methods throw NullPointerException with null arguments
2602       */
2603      public void testNPE() {
2604 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2605 <        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2604 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2605 >        CompletableFuture<Integer> g = new CompletableFuture<>();
2606          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2607          CompletableFuture<?> h;
2608 <        Executor exec = new ThreadExecutor();
2608 >        ThreadExecutor exec = new ThreadExecutor();
2609  
2610          Runnable[] throwingActions = {
2611 <            () -> { CompletableFuture.supplyAsync(null); },
2612 <            () -> { CompletableFuture.supplyAsync(null, exec); },
2613 <            () -> { CompletableFuture.supplyAsync(() -> one, null); },
2614 <
2615 <            () -> { CompletableFuture.runAsync(null); },
2616 <            () -> { CompletableFuture.runAsync(null, exec); },
2617 <            () -> { CompletableFuture.runAsync(() -> {}, null); },
2618 <
2619 <            () -> { f.completeExceptionally(null); },
2620 <
2621 <            () -> { f.thenApply(null); },
2622 <            () -> { f.thenApplyAsync(null); },
2623 <            () -> { f.thenApplyAsync((x) -> x, null); },
2624 <            () -> { f.thenApplyAsync(null, exec); },
2625 <
2626 <            () -> { f.thenAccept(null); },
2627 <            () -> { f.thenAcceptAsync(null); },
2628 <            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2629 <            () -> { f.thenAcceptAsync(null, exec); },
2630 <
2631 <            () -> { f.thenRun(null); },
2632 <            () -> { f.thenRunAsync(null); },
2633 <            () -> { f.thenRunAsync(() -> { ; }, null); },
2634 <            () -> { f.thenRunAsync(null, exec); },
2635 <
2636 <            () -> { f.thenCombine(g, null); },
2637 <            () -> { f.thenCombineAsync(g, null); },
2638 <            () -> { f.thenCombineAsync(g, null, exec); },
2639 <            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2640 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2641 <            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2642 <            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2643 <
2644 <            () -> { f.thenAcceptBoth(g, null); },
2645 <            () -> { f.thenAcceptBothAsync(g, null); },
2646 <            () -> { f.thenAcceptBothAsync(g, null, exec); },
2647 <            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2648 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2649 <            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2650 <            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2651 <
2652 <            () -> { f.runAfterBoth(g, null); },
2653 <            () -> { f.runAfterBothAsync(g, null); },
2654 <            () -> { f.runAfterBothAsync(g, null, exec); },
2655 <            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2656 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2657 <            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2658 <            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2659 <
2660 <            () -> { f.applyToEither(g, null); },
2661 <            () -> { f.applyToEitherAsync(g, null); },
2662 <            () -> { f.applyToEitherAsync(g, null, exec); },
2663 <            () -> { f.applyToEither(nullFuture, (x) -> x); },
2664 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2665 <            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2666 <            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2667 <
2668 <            () -> { f.acceptEither(g, null); },
2669 <            () -> { f.acceptEitherAsync(g, null); },
2670 <            () -> { f.acceptEitherAsync(g, null, exec); },
2671 <            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2672 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2673 <            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2674 <            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2675 <
2676 <            () -> { f.runAfterEither(g, null); },
2677 <            () -> { f.runAfterEitherAsync(g, null); },
2678 <            () -> { f.runAfterEitherAsync(g, null, exec); },
2679 <            () -> { f.runAfterEither(nullFuture, () -> {}); },
2680 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2681 <            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2682 <            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2683 <
2684 <            () -> { f.thenCompose(null); },
2685 <            () -> { f.thenComposeAsync(null); },
2686 <            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2687 <            () -> { f.thenComposeAsync(null, exec); },
2688 <
2689 <            () -> { f.exceptionally(null); },
2690 <
2691 <            () -> { f.handle(null); },
2692 <
2693 <            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2694 <            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2695 <            () -> { CompletableFuture.allOf(f, null); },
2696 <            () -> { CompletableFuture.allOf(null, f); },
2697 <
2698 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2699 <            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2700 <            () -> { CompletableFuture.anyOf(f, null); },
2701 <            () -> { CompletableFuture.anyOf(null, f); },
2702 <
2703 <            // TODO: Crashes javac with lambda-8-2013-03-31...
2517 <            //() -> { CompletableFuture<?> x = f.thenAccept(null); },
2518 <            //() -> { CompletableFuture<Void> x = f.thenRun(null); },
2519 <            //() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); },
2611 >            () -> CompletableFuture.supplyAsync(null),
2612 >            () -> CompletableFuture.supplyAsync(null, exec),
2613 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
2614 >
2615 >            () -> CompletableFuture.runAsync(null),
2616 >            () -> CompletableFuture.runAsync(null, exec),
2617 >            () -> CompletableFuture.runAsync(() -> {}, null),
2618 >
2619 >            () -> f.completeExceptionally(null),
2620 >
2621 >            () -> f.thenApply(null),
2622 >            () -> f.thenApplyAsync(null),
2623 >            () -> f.thenApplyAsync((x) -> x, null),
2624 >            () -> f.thenApplyAsync(null, exec),
2625 >
2626 >            () -> f.thenAccept(null),
2627 >            () -> f.thenAcceptAsync(null),
2628 >            () -> f.thenAcceptAsync((x) -> {} , null),
2629 >            () -> f.thenAcceptAsync(null, exec),
2630 >
2631 >            () -> f.thenRun(null),
2632 >            () -> f.thenRunAsync(null),
2633 >            () -> f.thenRunAsync(() -> {} , null),
2634 >            () -> f.thenRunAsync(null, exec),
2635 >
2636 >            () -> f.thenCombine(g, null),
2637 >            () -> f.thenCombineAsync(g, null),
2638 >            () -> f.thenCombineAsync(g, null, exec),
2639 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
2640 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
2641 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
2642 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
2643 >
2644 >            () -> f.thenAcceptBoth(g, null),
2645 >            () -> f.thenAcceptBothAsync(g, null),
2646 >            () -> f.thenAcceptBothAsync(g, null, exec),
2647 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
2648 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
2649 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
2650 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
2651 >
2652 >            () -> f.runAfterBoth(g, null),
2653 >            () -> f.runAfterBothAsync(g, null),
2654 >            () -> f.runAfterBothAsync(g, null, exec),
2655 >            () -> f.runAfterBoth(nullFuture, () -> {}),
2656 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
2657 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
2658 >            () -> f.runAfterBothAsync(g, () -> {}, null),
2659 >
2660 >            () -> f.applyToEither(g, null),
2661 >            () -> f.applyToEitherAsync(g, null),
2662 >            () -> f.applyToEitherAsync(g, null, exec),
2663 >            () -> f.applyToEither(nullFuture, (x) -> x),
2664 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
2665 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
2666 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
2667 >
2668 >            () -> f.acceptEither(g, null),
2669 >            () -> f.acceptEitherAsync(g, null),
2670 >            () -> f.acceptEitherAsync(g, null, exec),
2671 >            () -> f.acceptEither(nullFuture, (x) -> {}),
2672 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
2673 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
2674 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
2675 >
2676 >            () -> f.runAfterEither(g, null),
2677 >            () -> f.runAfterEitherAsync(g, null),
2678 >            () -> f.runAfterEitherAsync(g, null, exec),
2679 >            () -> f.runAfterEither(nullFuture, () -> {}),
2680 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
2681 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
2682 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
2683 >
2684 >            () -> f.thenCompose(null),
2685 >            () -> f.thenComposeAsync(null),
2686 >            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2687 >            () -> f.thenComposeAsync(null, exec),
2688 >
2689 >            () -> f.exceptionally(null),
2690 >
2691 >            () -> f.handle(null),
2692 >
2693 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
2694 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
2695 >            () -> CompletableFuture.allOf(f, null),
2696 >            () -> CompletableFuture.allOf(null, f),
2697 >
2698 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
2699 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
2700 >            () -> CompletableFuture.anyOf(f, null),
2701 >            () -> CompletableFuture.anyOf(null, f),
2702 >
2703 >            () -> f.obtrudeException(null),
2704          };
2705  
2706          assertThrows(NullPointerException.class, throwingActions);
2707 +        assertEquals(0, exec.count.get());
2708      }
2709  
2710 +    /**
2711 +     * toCompletableFuture returns this CompletableFuture.
2712 +     */
2713 +    public void testToCompletableFuture() {
2714 +        CompletableFuture<Integer> f = new CompletableFuture<>();
2715 +        assertSame(f, f.toCompletableFuture());
2716 +    }
2717 +
2718 +    /**
2719 +     * whenComplete action executes on normal completion, propagating
2720 +     * source result.
2721 +     */
2722 +    public void testWhenComplete_normalCompletion1() {
2723 +        for (ExecutionMode m : ExecutionMode.values())
2724 +        for (boolean createIncomplete : new boolean[] { true, false })
2725 +        for (Integer v1 : new Integer[] { 1, null })
2726 +    {
2727 +        final AtomicInteger a = new AtomicInteger(0);
2728 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2729 +        if (!createIncomplete) f.complete(v1);
2730 +        final CompletableFuture<Integer> g = m.whenComplete
2731 +            (f,
2732 +             (Integer x, Throwable t) -> {
2733 +                threadAssertSame(x, v1);
2734 +                threadAssertNull(t);
2735 +                a.getAndIncrement();
2736 +            });
2737 +        if (createIncomplete) f.complete(v1);
2738 +
2739 +        checkCompletedNormally(g, v1);
2740 +        checkCompletedNormally(f, v1);
2741 +        assertEquals(1, a.get());
2742 +    }}
2743 +
2744 +    /**
2745 +     * whenComplete action executes on exceptional completion, propagating
2746 +     * source result.
2747 +     */
2748 +    public void testWhenComplete_exceptionalCompletion() {
2749 +        for (ExecutionMode m : ExecutionMode.values())
2750 +        for (boolean createIncomplete : new boolean[] { true, false })
2751 +        for (Integer v1 : new Integer[] { 1, null })
2752 +    {
2753 +        final AtomicInteger a = new AtomicInteger(0);
2754 +        final CFException ex = new CFException();
2755 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2756 +        if (!createIncomplete) f.completeExceptionally(ex);
2757 +        final CompletableFuture<Integer> g = m.whenComplete
2758 +            (f,
2759 +             (Integer x, Throwable t) -> {
2760 +                threadAssertNull(x);
2761 +                threadAssertSame(t, ex);
2762 +                a.getAndIncrement();
2763 +            });
2764 +        if (createIncomplete) f.completeExceptionally(ex);
2765 +        checkCompletedWithWrappedCFException(f, ex);
2766 +        checkCompletedWithWrappedCFException(g, ex);
2767 +        assertEquals(1, a.get());
2768 +    }}
2769 +
2770 +    /**
2771 +     * whenComplete action executes on cancelled source, propagating
2772 +     * CancellationException.
2773 +     */
2774 +    public void testWhenComplete_sourceCancelled() {
2775 +        for (ExecutionMode m : ExecutionMode.values())
2776 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2777 +        for (boolean createIncomplete : new boolean[] { true, false })
2778 +    {
2779 +        final AtomicInteger a = new AtomicInteger(0);
2780 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2781 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2782 +        final CompletableFuture<Integer> g = m.whenComplete
2783 +            (f,
2784 +             (Integer x, Throwable t) -> {
2785 +                threadAssertNull(x);
2786 +                threadAssertTrue(t instanceof CancellationException);
2787 +                a.getAndIncrement();
2788 +            });
2789 +        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2790 +
2791 +        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2792 +        checkCompletedWithWrappedCancellationException(g);
2793 +        checkCancelled(f);
2794 +        assertEquals(1, a.get());
2795 +    }}
2796 +
2797 +    /**
2798 +     * If a whenComplete action throws an exception when triggered by
2799 +     * a normal completion, it completes exceptionally
2800 +     */
2801 +    public void testWhenComplete_actionFailed() {
2802 +        for (boolean createIncomplete : new boolean[] { true, false })
2803 +        for (ExecutionMode m : ExecutionMode.values())
2804 +        for (Integer v1 : new Integer[] { 1, null })
2805 +    {
2806 +        final AtomicInteger a = new AtomicInteger(0);
2807 +        final CFException ex = new CFException();
2808 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2809 +        if (!createIncomplete) f.complete(v1);
2810 +        final CompletableFuture<Integer> g = m.whenComplete
2811 +            (f,
2812 +             (Integer x, Throwable t) -> {
2813 +                threadAssertSame(x, v1);
2814 +                threadAssertNull(t);
2815 +                a.getAndIncrement();
2816 +                throw ex;
2817 +            });
2818 +        if (createIncomplete) f.complete(v1);
2819 +        checkCompletedNormally(f, v1);
2820 +        checkCompletedWithWrappedCFException(g, ex);
2821 +        assertEquals(1, a.get());
2822 +    }}
2823 +
2824 +    /**
2825 +     * If a whenComplete action throws an exception when triggered by
2826 +     * a source completion that also throws an exception, the source
2827 +     * exception takes precedence.
2828 +     */
2829 +    public void testWhenComplete_actionFailedSourceFailed() {
2830 +        for (boolean createIncomplete : new boolean[] { true, false })
2831 +        for (ExecutionMode m : ExecutionMode.values())
2832 +        for (Integer v1 : new Integer[] { 1, null })
2833 +    {
2834 +        final AtomicInteger a = new AtomicInteger(0);
2835 +        final CFException ex1 = new CFException();
2836 +        final CFException ex2 = new CFException();
2837 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2838 +
2839 +        if (!createIncomplete) f.completeExceptionally(ex1);
2840 +        final CompletableFuture<Integer> g = m.whenComplete
2841 +            (f,
2842 +             (Integer x, Throwable t) -> {
2843 +                threadAssertSame(t, ex1);
2844 +                threadAssertNull(x);
2845 +                a.getAndIncrement();
2846 +                throw ex2;
2847 +            });
2848 +        if (createIncomplete) f.completeExceptionally(ex1);
2849 +
2850 +        checkCompletedWithWrappedCFException(f, ex1);
2851 +        checkCompletedWithWrappedCFException(g, ex1);
2852 +        assertEquals(1, a.get());
2853 +    }}
2854 +
2855   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines