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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines