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.28 by jsr166, Sun Jul 14 16:34:49 2013 UTC vs.
Revision 1.137 by jsr166, Sun Nov 15 21:41:22 2015 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 > import static java.util.concurrent.CompletableFuture.completedFuture;
11 > import static java.util.concurrent.CompletableFuture.failedFuture;
12 >
13 > import java.lang.reflect.Method;
14 > import java.lang.reflect.Modifier;
15 >
16 > import java.util.stream.Collectors;
17 > import java.util.stream.Stream;
18 >
19 > import java.util.ArrayList;
20 > import java.util.Arrays;
21 > import java.util.List;
22 > import java.util.Objects;
23 > import java.util.Set;
24   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
25   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
26   import java.util.concurrent.CompletableFuture;
27   import java.util.concurrent.CompletionException;
28 + import java.util.concurrent.CompletionStage;
29 + import java.util.concurrent.ExecutionException;
30 + import java.util.concurrent.Executor;
31 + import java.util.concurrent.ForkJoinPool;
32 + import java.util.concurrent.ForkJoinTask;
33   import java.util.concurrent.TimeoutException;
34 + import java.util.concurrent.TimeUnit;
35   import java.util.concurrent.atomic.AtomicInteger;
36 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 < import static java.util.concurrent.TimeUnit.SECONDS;
23 < import java.util.*;
24 < import java.util.function.Supplier;
25 < import java.util.function.Consumer;
36 > import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
27 import java.util.function.Function;
38   import java.util.function.BiFunction;
39 + import java.util.function.Consumer;
40 + import java.util.function.Function;
41 + import java.util.function.Predicate;
42 + import java.util.function.Supplier;
43 +
44 + import junit.framework.AssertionFailedError;
45 + import junit.framework.Test;
46 + import junit.framework.TestSuite;
47  
48   public class CompletableFutureTest extends JSR166TestCase {
49  
50      public static void main(String[] args) {
51 <        junit.textui.TestRunner.run(suite());
51 >        main(suite(), args);
52      }
53      public static Test suite() {
54          return new TestSuite(CompletableFutureTest.class);
# Line 41 | Line 59 | public class CompletableFutureTest exten
59      void checkIncomplete(CompletableFuture<?> f) {
60          assertFalse(f.isDone());
61          assertFalse(f.isCancelled());
62 <        assertTrue(f.toString().contains("[Not completed]"));
62 >        assertTrue(f.toString().contains("Not completed"));
63          try {
64              assertNull(f.getNow(null));
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 54 | Line 72 | public class CompletableFutureTest exten
72      }
73  
74      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
75 <        try {
76 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
59 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
75 >        checkTimedGet(f, value);
76 >
77          try {
78              assertEquals(value, f.join());
79          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 72 | Line 89 | public class CompletableFutureTest exten
89          assertTrue(f.toString().contains("[Completed normally]"));
90      }
91  
92 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
92 >    /**
93 >     * Returns the "raw" internal exceptional completion of f,
94 >     * without any additional wrapping with CompletionException.
95 >     */
96 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
97 >        // handle (and whenComplete) can distinguish between "direct"
98 >        // and "wrapped" exceptional completion
99 >        return f.handle((U u, Throwable t) -> t).join();
100 >    }
101 >
102 >    void checkCompletedExceptionally(CompletableFuture<?> f,
103 >                                     boolean wrapped,
104 >                                     Consumer<Throwable> checker) {
105 >        Throwable cause = exceptionalCompletion(f);
106 >        if (wrapped) {
107 >            assertTrue(cause instanceof CompletionException);
108 >            cause = cause.getCause();
109 >        }
110 >        checker.accept(cause);
111 >
112 >        long startTime = System.nanoTime();
113          try {
114              f.get(LONG_DELAY_MS, MILLISECONDS);
115              shouldThrow();
116          } catch (ExecutionException success) {
117 <            assertTrue(success.getCause() instanceof CFException);
117 >            assertSame(cause, success.getCause());
118          } catch (Throwable fail) { threadUnexpectedException(fail); }
119 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
120 +
121          try {
122              f.join();
123              shouldThrow();
124          } catch (CompletionException success) {
125 <            assertTrue(success.getCause() instanceof CFException);
126 <        }
125 >            assertSame(cause, success.getCause());
126 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
127 >
128          try {
129              f.getNow(null);
130              shouldThrow();
131          } catch (CompletionException success) {
132 <            assertTrue(success.getCause() instanceof CFException);
133 <        }
132 >            assertSame(cause, success.getCause());
133 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 >
135          try {
136              f.get();
137              shouldThrow();
138          } catch (ExecutionException success) {
139 <            assertTrue(success.getCause() instanceof CFException);
139 >            assertSame(cause, success.getCause());
140          } catch (Throwable fail) { threadUnexpectedException(fail); }
141 <        assertTrue(f.isDone());
141 >
142          assertFalse(f.isCancelled());
143 +        assertTrue(f.isDone());
144 +        assertTrue(f.isCompletedExceptionally());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 +    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
149 +        checkCompletedExceptionally(f, true,
150 +            (t) -> assertTrue(t instanceof CFException));
151 +    }
152 +
153 +    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
154 +        checkCompletedExceptionally(f, true,
155 +            (t) -> assertTrue(t instanceof CancellationException));
156 +    }
157 +
158 +    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
159 +        checkCompletedExceptionally(f, false,
160 +            (t) -> assertTrue(t instanceof TimeoutException));
161 +    }
162 +
163 +    void checkCompletedWithWrappedException(CompletableFuture<?> f,
164 +                                            Throwable ex) {
165 +        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
166 +    }
167 +
168 +    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
169 +        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
170 +    }
171 +
172      void checkCancelled(CompletableFuture<?> f) {
173 +        long startTime = System.nanoTime();
174          try {
175              f.get(LONG_DELAY_MS, MILLISECONDS);
176              shouldThrow();
177          } catch (CancellationException success) {
178          } catch (Throwable fail) { threadUnexpectedException(fail); }
179 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
180 +
181          try {
182              f.join();
183              shouldThrow();
# Line 121 | Line 191 | public class CompletableFutureTest exten
191              shouldThrow();
192          } catch (CancellationException success) {
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
124        assertTrue(f.isDone());
125        assertTrue(f.isCompletedExceptionally());
126        assertTrue(f.isCancelled());
127        assertTrue(f.toString().contains("[Completed exceptionally]"));
128    }
194  
195 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
196 <        try {
132 <            f.get(LONG_DELAY_MS, MILLISECONDS);
133 <            shouldThrow();
134 <        } catch (ExecutionException success) {
135 <            assertTrue(success.getCause() instanceof CancellationException);
136 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
137 <        try {
138 <            f.join();
139 <            shouldThrow();
140 <        } catch (CompletionException success) {
141 <            assertTrue(success.getCause() instanceof CancellationException);
142 <        }
143 <        try {
144 <            f.getNow(null);
145 <            shouldThrow();
146 <        } catch (CompletionException success) {
147 <            assertTrue(success.getCause() instanceof CancellationException);
148 <        }
149 <        try {
150 <            f.get();
151 <            shouldThrow();
152 <        } catch (ExecutionException success) {
153 <            assertTrue(success.getCause() instanceof CancellationException);
154 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
195 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
196 >
197          assertTrue(f.isDone());
156        assertFalse(f.isCancelled());
198          assertTrue(f.isCompletedExceptionally());
199 +        assertTrue(f.isCancelled());
200          assertTrue(f.toString().contains("[Completed exceptionally]"));
201      }
202  
# Line 172 | Line 214 | public class CompletableFutureTest exten
214       * isCancelled, join, get, and getNow
215       */
216      public void testComplete() {
217 +        for (Integer v1 : new Integer[] { 1, null })
218 +    {
219          CompletableFuture<Integer> f = new CompletableFuture<>();
220          checkIncomplete(f);
221 <        f.complete(one);
222 <        checkCompletedNormally(f, one);
223 <    }
221 >        assertTrue(f.complete(v1));
222 >        assertFalse(f.complete(v1));
223 >        checkCompletedNormally(f, v1);
224 >    }}
225  
226      /**
227       * completeExceptionally completes exceptionally, as indicated by
# Line 184 | Line 229 | public class CompletableFutureTest exten
229       */
230      public void testCompleteExceptionally() {
231          CompletableFuture<Integer> f = new CompletableFuture<>();
232 +        CFException ex = new CFException();
233          checkIncomplete(f);
234 <        f.completeExceptionally(new CFException());
235 <        checkCompletedWithWrappedCFException(f);
234 >        f.completeExceptionally(ex);
235 >        checkCompletedExceptionally(f, ex);
236      }
237  
238      /**
# Line 194 | Line 240 | public class CompletableFutureTest exten
240       * methods isDone, isCancelled, join, get, and getNow
241       */
242      public void testCancel() {
243 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
244 +    {
245          CompletableFuture<Integer> f = new CompletableFuture<>();
246          checkIncomplete(f);
247 <        assertTrue(f.cancel(true));
247 >        assertTrue(f.cancel(mayInterruptIfRunning));
248 >        assertTrue(f.cancel(mayInterruptIfRunning));
249 >        assertTrue(f.cancel(!mayInterruptIfRunning));
250          checkCancelled(f);
251 <    }
251 >    }}
252  
253      /**
254       * obtrudeValue forces completion with given value
# Line 206 | Line 256 | public class CompletableFutureTest exten
256      public void testObtrudeValue() {
257          CompletableFuture<Integer> f = new CompletableFuture<>();
258          checkIncomplete(f);
259 <        f.complete(one);
259 >        assertTrue(f.complete(one));
260          checkCompletedNormally(f, one);
261          f.obtrudeValue(three);
262          checkCompletedNormally(f, three);
# Line 215 | Line 265 | public class CompletableFutureTest exten
265          f = new CompletableFuture<>();
266          f.obtrudeValue(three);
267          checkCompletedNormally(f, three);
268 +        f.obtrudeValue(null);
269 +        checkCompletedNormally(f, null);
270          f = new CompletableFuture<>();
271          f.completeExceptionally(new CFException());
272          f.obtrudeValue(four);
# Line 225 | Line 277 | public class CompletableFutureTest exten
277       * obtrudeException forces completion with given exception
278       */
279      public void testObtrudeException() {
280 <        CompletableFuture<Integer> f = new CompletableFuture<>();
281 <        checkIncomplete(f);
282 <        f.complete(one);
283 <        checkCompletedNormally(f, one);
284 <        f.obtrudeException(new CFException());
233 <        checkCompletedWithWrappedCFException(f);
280 >        for (Integer v1 : new Integer[] { 1, null })
281 >    {
282 >        CFException ex;
283 >        CompletableFuture<Integer> f;
284 >
285          f = new CompletableFuture<>();
286 <        f.obtrudeException(new CFException());
287 <        checkCompletedWithWrappedCFException(f);
286 >        assertTrue(f.complete(v1));
287 >        for (int i = 0; i < 2; i++) {
288 >            f.obtrudeException(ex = new CFException());
289 >            checkCompletedExceptionally(f, ex);
290 >        }
291 >
292          f = new CompletableFuture<>();
293 <        f.completeExceptionally(new CFException());
294 <        f.obtrudeValue(four);
295 <        checkCompletedNormally(f, four);
296 <        f.obtrudeException(new CFException());
297 <        checkCompletedWithWrappedCFException(f);
298 <    }
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 >        f.completeExceptionally(ex = new CFException());
300 >        f.obtrudeValue(v1);
301 >        checkCompletedNormally(f, v1);
302 >        f.obtrudeException(ex = new CFException());
303 >        checkCompletedExceptionally(f, ex);
304 >        f.completeExceptionally(new CFException());
305 >        checkCompletedExceptionally(f, ex);
306 >        assertFalse(f.complete(v1));
307 >        checkCompletedExceptionally(f, ex);
308 >    }}
309  
310      /**
311       * getNumberOfDependents returns number of dependent tasks
312       */
313      public void testGetNumberOfDependents() {
314 <        CompletableFuture<Integer> f = new CompletableFuture<>();
315 <        assertEquals(f.getNumberOfDependents(), 0);
316 <        CompletableFuture g = f.thenRun(new Noop());
317 <        assertEquals(f.getNumberOfDependents(), 1);
318 <        assertEquals(g.getNumberOfDependents(), 0);
319 <        CompletableFuture h = f.thenRun(new Noop());
320 <        assertEquals(f.getNumberOfDependents(), 2);
321 <        f.complete(1);
314 >        for (ExecutionMode m : ExecutionMode.values())
315 >        for (Integer v1 : new Integer[] { 1, null })
316 >    {
317 >        CompletableFuture<Integer> f = new CompletableFuture<>();
318 >        assertEquals(0, f.getNumberOfDependents());
319 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
320 >        assertEquals(1, f.getNumberOfDependents());
321 >        assertEquals(0, g.getNumberOfDependents());
322 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
323 >        assertEquals(2, f.getNumberOfDependents());
324 >        assertEquals(0, h.getNumberOfDependents());
325 >        assertTrue(f.complete(v1));
326          checkCompletedNormally(g, null);
327 <        assertEquals(f.getNumberOfDependents(), 0);
328 <        assertEquals(g.getNumberOfDependents(), 0);
329 <    }
327 >        checkCompletedNormally(h, null);
328 >        assertEquals(0, f.getNumberOfDependents());
329 >        assertEquals(0, g.getNumberOfDependents());
330 >        assertEquals(0, h.getNumberOfDependents());
331 >    }}
332  
333      /**
334       * toString indicates current completion state
# Line 268 | Line 339 | public class CompletableFutureTest exten
339          f = new CompletableFuture<String>();
340          assertTrue(f.toString().contains("[Not completed]"));
341  
342 <        f.complete("foo");
342 >        assertTrue(f.complete("foo"));
343          assertTrue(f.toString().contains("[Completed normally]"));
344  
345          f = new CompletableFuture<String>();
346 <        f.completeExceptionally(new IndexOutOfBoundsException());
346 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348 +
349 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
350 +            f = new CompletableFuture<String>();
351 +            assertTrue(f.cancel(mayInterruptIfRunning));
352 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
353 +        }
354      }
355  
356      /**
# Line 284 | Line 361 | public class CompletableFutureTest exten
361          checkCompletedNormally(f, "test");
362      }
363  
364 <    // Choose non-commutative actions for better coverage
364 >    abstract class CheckedAction {
365 >        int invocationCount = 0;
366 >        final ExecutionMode m;
367 >        CheckedAction(ExecutionMode m) { this.m = m; }
368 >        void invoked() {
369 >            m.checkExecutionMode();
370 >            assertEquals(0, invocationCount++);
371 >        }
372 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
373 >        void assertInvoked() { assertEquals(1, invocationCount); }
374 >    }
375 >
376 >    abstract class CheckedIntegerAction extends CheckedAction {
377 >        Integer value;
378 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
379 >        void assertValue(Integer expected) {
380 >            assertInvoked();
381 >            assertEquals(expected, value);
382 >        }
383 >    }
384 >
385 >    class IntegerSupplier extends CheckedAction
386 >        implements Supplier<Integer>
387 >    {
388 >        final Integer value;
389 >        IntegerSupplier(ExecutionMode m, Integer value) {
390 >            super(m);
391 >            this.value = value;
392 >        }
393 >        public Integer get() {
394 >            invoked();
395 >            return value;
396 >        }
397 >    }
398 >
399 >    // A function that handles and produces null values as well.
400 >    static Integer inc(Integer x) {
401 >        return (x == null) ? null : x + 1;
402 >    }
403 >
404 >    class NoopConsumer extends CheckedIntegerAction
405 >        implements Consumer<Integer>
406 >    {
407 >        NoopConsumer(ExecutionMode m) { super(m); }
408 >        public void accept(Integer x) {
409 >            invoked();
410 >            value = x;
411 >        }
412 >    }
413 >
414 >    class IncFunction extends CheckedIntegerAction
415 >        implements Function<Integer,Integer>
416 >    {
417 >        IncFunction(ExecutionMode m) { super(m); }
418 >        public Integer apply(Integer x) {
419 >            invoked();
420 >            return value = inc(x);
421 >        }
422 >    }
423  
424 <    static final Supplier<Integer> supplyOne =
425 <        () -> Integer.valueOf(1);
426 <    static final Function<Integer, Integer> inc =
427 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
428 <    static final BiFunction<Integer, Integer, Integer> subtract =
429 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
295 <    static final class IncAction implements Consumer<Integer> {
296 <        int value;
297 <        public void accept(Integer x) { value = x.intValue() + 1; }
424 >    // Choose non-commutative actions for better coverage
425 >    // A non-commutative function that handles and produces null values as well.
426 >    static Integer subtract(Integer x, Integer y) {
427 >        return (x == null && y == null) ? null :
428 >            ((x == null) ? 42 : x.intValue())
429 >            - ((y == null) ? 99 : y.intValue());
430      }
431 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
432 <        int value;
431 >
432 >    class SubtractAction extends CheckedIntegerAction
433 >        implements BiConsumer<Integer, Integer>
434 >    {
435 >        SubtractAction(ExecutionMode m) { super(m); }
436          public void accept(Integer x, Integer y) {
437 <            value = x.intValue() - y.intValue();
437 >            invoked();
438 >            value = subtract(x, y);
439          }
440      }
441 <    static final class Noop implements Runnable {
442 <        boolean ran;
443 <        public void run() { ran = true; }
441 >
442 >    class SubtractFunction extends CheckedIntegerAction
443 >        implements BiFunction<Integer, Integer, Integer>
444 >    {
445 >        SubtractFunction(ExecutionMode m) { super(m); }
446 >        public Integer apply(Integer x, Integer y) {
447 >            invoked();
448 >            return value = subtract(x, y);
449 >        }
450      }
451  
452 <    static final class FailingSupplier implements Supplier<Integer> {
453 <        boolean ran;
454 <        public Integer get() { ran = true; throw new CFException(); }
452 >    class Noop extends CheckedAction implements Runnable {
453 >        Noop(ExecutionMode m) { super(m); }
454 >        public void run() {
455 >            invoked();
456 >        }
457      }
458 <    static final class FailingConsumer implements Consumer<Integer> {
459 <        boolean ran;
460 <        public void accept(Integer x) { ran = true; throw new CFException(); }
458 >
459 >    class FailingSupplier extends CheckedAction
460 >        implements Supplier<Integer>
461 >    {
462 >        FailingSupplier(ExecutionMode m) { super(m); }
463 >        public Integer get() {
464 >            invoked();
465 >            throw new CFException();
466 >        }
467      }
468 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
469 <        boolean ran;
470 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
468 >
469 >    class FailingConsumer extends CheckedIntegerAction
470 >        implements Consumer<Integer>
471 >    {
472 >        FailingConsumer(ExecutionMode m) { super(m); }
473 >        public void accept(Integer x) {
474 >            invoked();
475 >            value = x;
476 >            throw new CFException();
477 >        }
478 >    }
479 >
480 >    class FailingBiConsumer extends CheckedIntegerAction
481 >        implements BiConsumer<Integer, Integer>
482 >    {
483 >        FailingBiConsumer(ExecutionMode m) { super(m); }
484 >        public void accept(Integer x, Integer y) {
485 >            invoked();
486 >            value = subtract(x, y);
487 >            throw new CFException();
488 >        }
489      }
490 <    static final class FailingFunction implements Function<Integer, Integer> {
491 <        boolean ran;
492 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
490 >
491 >    class FailingFunction extends CheckedIntegerAction
492 >        implements Function<Integer, Integer>
493 >    {
494 >        FailingFunction(ExecutionMode m) { super(m); }
495 >        public Integer apply(Integer x) {
496 >            invoked();
497 >            value = x;
498 >            throw new CFException();
499 >        }
500      }
501 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
502 <        boolean ran;
503 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
501 >
502 >    class FailingBiFunction extends CheckedIntegerAction
503 >        implements BiFunction<Integer, Integer, Integer>
504 >    {
505 >        FailingBiFunction(ExecutionMode m) { super(m); }
506 >        public Integer apply(Integer x, Integer y) {
507 >            invoked();
508 >            value = subtract(x, y);
509 >            throw new CFException();
510 >        }
511      }
512 <    static final class FailingNoop implements Runnable {
513 <        boolean ran;
514 <        public void run() { ran = true; throw new CFException(); }
512 >
513 >    class FailingRunnable extends CheckedAction implements Runnable {
514 >        FailingRunnable(ExecutionMode m) { super(m); }
515 >        public void run() {
516 >            invoked();
517 >            throw new CFException();
518 >        }
519      }
520  
521 <    static final class CompletableFutureInc
522 <        implements Function<Integer, CompletableFuture<Integer>> {
523 <        boolean ran;
521 >    class CompletableFutureInc extends CheckedIntegerAction
522 >        implements Function<Integer, CompletableFuture<Integer>>
523 >    {
524 >        CompletableFutureInc(ExecutionMode m) { super(m); }
525          public CompletableFuture<Integer> apply(Integer x) {
526 <            ran = true;
526 >            invoked();
527 >            value = x;
528              CompletableFuture<Integer> f = new CompletableFuture<>();
529 <            f.complete(Integer.valueOf(x.intValue() + 1));
529 >            assertTrue(f.complete(inc(x)));
530              return f;
531          }
532      }
533  
534 <    static final class FailingCompletableFutureFunction
535 <        implements Function<Integer, CompletableFuture<Integer>> {
536 <        boolean ran;
534 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
535 >        implements Function<Integer, CompletableFuture<Integer>>
536 >    {
537 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
538          public CompletableFuture<Integer> apply(Integer x) {
539 <            ran = true; throw new CFException();
539 >            invoked();
540 >            value = x;
541 >            throw new CFException();
542          }
543      }
544  
545      // Used for explicit executor tests
546      static final class ThreadExecutor implements Executor {
547 <        AtomicInteger count = new AtomicInteger(0);
547 >        final AtomicInteger count = new AtomicInteger(0);
548 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
549 >        static boolean startedCurrentThread() {
550 >            return Thread.currentThread().getThreadGroup() == tg;
551 >        }
552  
553          public void execute(Runnable r) {
554              count.getAndIncrement();
555 <            new Thread(r).start();
555 >            new Thread(tg, r).start();
556          }
557      }
558  
559 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
560 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
366 <    }
559 >    static final boolean defaultExecutorIsCommonPool
560 >        = ForkJoinPool.getCommonPoolParallelism() > 1;
561  
562 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
563 <        boolean ran;
564 <        public Integer apply(Integer x, Throwable t) {
565 <            ran = true;
566 <            return (t == null) ? two : three;
567 <        }
568 <    }
562 >    /**
563 >     * Permits the testing of parallel code for the 3 different
564 >     * execution modes without copy/pasting all the test methods.
565 >     */
566 >    enum ExecutionMode {
567 >        SYNC {
568 >            public void checkExecutionMode() {
569 >                assertFalse(ThreadExecutor.startedCurrentThread());
570 >                assertNull(ForkJoinTask.getPool());
571 >            }
572 >            public CompletableFuture<Void> runAsync(Runnable a) {
573 >                throw new UnsupportedOperationException();
574 >            }
575 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576 >                throw new UnsupportedOperationException();
577 >            }
578 >            public <T> CompletableFuture<Void> thenRun
579 >                (CompletableFuture<T> f, Runnable a) {
580 >                return f.thenRun(a);
581 >            }
582 >            public <T> CompletableFuture<Void> thenAccept
583 >                (CompletableFuture<T> f, Consumer<? super T> a) {
584 >                return f.thenAccept(a);
585 >            }
586 >            public <T,U> CompletableFuture<U> thenApply
587 >                (CompletableFuture<T> f, Function<? super T,U> a) {
588 >                return f.thenApply(a);
589 >            }
590 >            public <T,U> CompletableFuture<U> thenCompose
591 >                (CompletableFuture<T> f,
592 >                 Function<? super T,? extends CompletionStage<U>> a) {
593 >                return f.thenCompose(a);
594 >            }
595 >            public <T,U> CompletableFuture<U> handle
596 >                (CompletableFuture<T> f,
597 >                 BiFunction<? super T,Throwable,? extends U> a) {
598 >                return f.handle(a);
599 >            }
600 >            public <T> CompletableFuture<T> whenComplete
601 >                (CompletableFuture<T> f,
602 >                 BiConsumer<? super T,? super Throwable> a) {
603 >                return f.whenComplete(a);
604 >            }
605 >            public <T,U> CompletableFuture<Void> runAfterBoth
606 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
607 >                return f.runAfterBoth(g, a);
608 >            }
609 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
610 >                (CompletableFuture<T> f,
611 >                 CompletionStage<? extends U> g,
612 >                 BiConsumer<? super T,? super U> a) {
613 >                return f.thenAcceptBoth(g, a);
614 >            }
615 >            public <T,U,V> CompletableFuture<V> thenCombine
616 >                (CompletableFuture<T> f,
617 >                 CompletionStage<? extends U> g,
618 >                 BiFunction<? super T,? super U,? extends V> a) {
619 >                return f.thenCombine(g, a);
620 >            }
621 >            public <T> CompletableFuture<Void> runAfterEither
622 >                (CompletableFuture<T> f,
623 >                 CompletionStage<?> g,
624 >                 java.lang.Runnable a) {
625 >                return f.runAfterEither(g, a);
626 >            }
627 >            public <T> CompletableFuture<Void> acceptEither
628 >                (CompletableFuture<T> f,
629 >                 CompletionStage<? extends T> g,
630 >                 Consumer<? super T> a) {
631 >                return f.acceptEither(g, a);
632 >            }
633 >            public <T,U> CompletableFuture<U> applyToEither
634 >                (CompletableFuture<T> f,
635 >                 CompletionStage<? extends T> g,
636 >                 Function<? super T,U> a) {
637 >                return f.applyToEither(g, a);
638 >            }
639 >        },
640 >
641 >        ASYNC {
642 >            public void checkExecutionMode() {
643 >                assertEquals(defaultExecutorIsCommonPool,
644 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
645 >            }
646 >            public CompletableFuture<Void> runAsync(Runnable a) {
647 >                return CompletableFuture.runAsync(a);
648 >            }
649 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
650 >                return CompletableFuture.supplyAsync(a);
651 >            }
652 >            public <T> CompletableFuture<Void> thenRun
653 >                (CompletableFuture<T> f, Runnable a) {
654 >                return f.thenRunAsync(a);
655 >            }
656 >            public <T> CompletableFuture<Void> thenAccept
657 >                (CompletableFuture<T> f, Consumer<? super T> a) {
658 >                return f.thenAcceptAsync(a);
659 >            }
660 >            public <T,U> CompletableFuture<U> thenApply
661 >                (CompletableFuture<T> f, Function<? super T,U> a) {
662 >                return f.thenApplyAsync(a);
663 >            }
664 >            public <T,U> CompletableFuture<U> thenCompose
665 >                (CompletableFuture<T> f,
666 >                 Function<? super T,? extends CompletionStage<U>> a) {
667 >                return f.thenComposeAsync(a);
668 >            }
669 >            public <T,U> CompletableFuture<U> handle
670 >                (CompletableFuture<T> f,
671 >                 BiFunction<? super T,Throwable,? extends U> a) {
672 >                return f.handleAsync(a);
673 >            }
674 >            public <T> CompletableFuture<T> whenComplete
675 >                (CompletableFuture<T> f,
676 >                 BiConsumer<? super T,? super Throwable> a) {
677 >                return f.whenCompleteAsync(a);
678 >            }
679 >            public <T,U> CompletableFuture<Void> runAfterBoth
680 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
681 >                return f.runAfterBothAsync(g, a);
682 >            }
683 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
684 >                (CompletableFuture<T> f,
685 >                 CompletionStage<? extends U> g,
686 >                 BiConsumer<? super T,? super U> a) {
687 >                return f.thenAcceptBothAsync(g, a);
688 >            }
689 >            public <T,U,V> CompletableFuture<V> thenCombine
690 >                (CompletableFuture<T> f,
691 >                 CompletionStage<? extends U> g,
692 >                 BiFunction<? super T,? super U,? extends V> a) {
693 >                return f.thenCombineAsync(g, a);
694 >            }
695 >            public <T> CompletableFuture<Void> runAfterEither
696 >                (CompletableFuture<T> f,
697 >                 CompletionStage<?> g,
698 >                 java.lang.Runnable a) {
699 >                return f.runAfterEitherAsync(g, a);
700 >            }
701 >            public <T> CompletableFuture<Void> acceptEither
702 >                (CompletableFuture<T> f,
703 >                 CompletionStage<? extends T> g,
704 >                 Consumer<? super T> a) {
705 >                return f.acceptEitherAsync(g, a);
706 >            }
707 >            public <T,U> CompletableFuture<U> applyToEither
708 >                (CompletableFuture<T> f,
709 >                 CompletionStage<? extends T> g,
710 >                 Function<? super T,U> a) {
711 >                return f.applyToEitherAsync(g, a);
712 >            }
713 >        },
714 >
715 >        EXECUTOR {
716 >            public void checkExecutionMode() {
717 >                assertTrue(ThreadExecutor.startedCurrentThread());
718 >            }
719 >            public CompletableFuture<Void> runAsync(Runnable a) {
720 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
721 >            }
722 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724 >            }
725 >            public <T> CompletableFuture<Void> thenRun
726 >                (CompletableFuture<T> f, Runnable a) {
727 >                return f.thenRunAsync(a, new ThreadExecutor());
728 >            }
729 >            public <T> CompletableFuture<Void> thenAccept
730 >                (CompletableFuture<T> f, Consumer<? super T> a) {
731 >                return f.thenAcceptAsync(a, new ThreadExecutor());
732 >            }
733 >            public <T,U> CompletableFuture<U> thenApply
734 >                (CompletableFuture<T> f, Function<? super T,U> a) {
735 >                return f.thenApplyAsync(a, new ThreadExecutor());
736 >            }
737 >            public <T,U> CompletableFuture<U> thenCompose
738 >                (CompletableFuture<T> f,
739 >                 Function<? super T,? extends CompletionStage<U>> a) {
740 >                return f.thenComposeAsync(a, new ThreadExecutor());
741 >            }
742 >            public <T,U> CompletableFuture<U> handle
743 >                (CompletableFuture<T> f,
744 >                 BiFunction<? super T,Throwable,? extends U> a) {
745 >                return f.handleAsync(a, new ThreadExecutor());
746 >            }
747 >            public <T> CompletableFuture<T> whenComplete
748 >                (CompletableFuture<T> f,
749 >                 BiConsumer<? super T,? super Throwable> a) {
750 >                return f.whenCompleteAsync(a, new ThreadExecutor());
751 >            }
752 >            public <T,U> CompletableFuture<Void> runAfterBoth
753 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
754 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
755 >            }
756 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
757 >                (CompletableFuture<T> f,
758 >                 CompletionStage<? extends U> g,
759 >                 BiConsumer<? super T,? super U> a) {
760 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
761 >            }
762 >            public <T,U,V> CompletableFuture<V> thenCombine
763 >                (CompletableFuture<T> f,
764 >                 CompletionStage<? extends U> g,
765 >                 BiFunction<? super T,? super U,? extends V> a) {
766 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
767 >            }
768 >            public <T> CompletableFuture<Void> runAfterEither
769 >                (CompletableFuture<T> f,
770 >                 CompletionStage<?> g,
771 >                 java.lang.Runnable a) {
772 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
773 >            }
774 >            public <T> CompletableFuture<Void> acceptEither
775 >                (CompletableFuture<T> f,
776 >                 CompletionStage<? extends T> g,
777 >                 Consumer<? super T> a) {
778 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
779 >            }
780 >            public <T,U> CompletableFuture<U> applyToEither
781 >                (CompletableFuture<T> f,
782 >                 CompletionStage<? extends T> g,
783 >                 Function<? super T,U> a) {
784 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
785 >            }
786 >        };
787 >
788 >        public abstract void checkExecutionMode();
789 >        public abstract CompletableFuture<Void> runAsync(Runnable a);
790 >        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791 >        public abstract <T> CompletableFuture<Void> thenRun
792 >            (CompletableFuture<T> f, Runnable a);
793 >        public abstract <T> CompletableFuture<Void> thenAccept
794 >            (CompletableFuture<T> f, Consumer<? super T> a);
795 >        public abstract <T,U> CompletableFuture<U> thenApply
796 >            (CompletableFuture<T> f, Function<? super T,U> a);
797 >        public abstract <T,U> CompletableFuture<U> thenCompose
798 >            (CompletableFuture<T> f,
799 >             Function<? super T,? extends CompletionStage<U>> a);
800 >        public abstract <T,U> CompletableFuture<U> handle
801 >            (CompletableFuture<T> f,
802 >             BiFunction<? super T,Throwable,? extends U> a);
803 >        public abstract <T> CompletableFuture<T> whenComplete
804 >            (CompletableFuture<T> f,
805 >             BiConsumer<? super T,? super Throwable> a);
806 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
807 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
808 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
809 >            (CompletableFuture<T> f,
810 >             CompletionStage<? extends U> g,
811 >             BiConsumer<? super T,? super U> a);
812 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
813 >            (CompletableFuture<T> f,
814 >             CompletionStage<? extends U> g,
815 >             BiFunction<? super T,? super U,? extends V> a);
816 >        public abstract <T> CompletableFuture<Void> runAfterEither
817 >            (CompletableFuture<T> f,
818 >             CompletionStage<?> g,
819 >             java.lang.Runnable a);
820 >        public abstract <T> CompletableFuture<Void> acceptEither
821 >            (CompletableFuture<T> f,
822 >             CompletionStage<? extends T> g,
823 >             Consumer<? super T> a);
824 >        public abstract <T,U> CompletableFuture<U> applyToEither
825 >            (CompletableFuture<T> f,
826 >             CompletionStage<? extends T> g,
827 >             Function<? super T,U> a);
828 >    }
829 >
830 >    /**
831 >     * exceptionally action is not invoked when source completes
832 >     * normally, and source result is propagated
833 >     */
834 >    public void testExceptionally_normalCompletion() {
835 >        for (boolean createIncomplete : new boolean[] { true, false })
836 >        for (Integer v1 : new Integer[] { 1, null })
837 >    {
838 >        final AtomicInteger a = new AtomicInteger(0);
839 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
840 >        if (!createIncomplete) assertTrue(f.complete(v1));
841 >        final CompletableFuture<Integer> g = f.exceptionally
842 >            ((Throwable t) -> {
843 >                a.getAndIncrement();
844 >                threadFail("should not be called");
845 >                return null;            // unreached
846 >            });
847 >        if (createIncomplete) assertTrue(f.complete(v1));
848 >
849 >        checkCompletedNormally(g, v1);
850 >        checkCompletedNormally(f, v1);
851 >        assertEquals(0, a.get());
852 >    }}
853  
854      /**
855       * exceptionally action completes with function value on source
856 <     * exception;  otherwise with source value
856 >     * exception
857       */
858 <    public void testExceptionally() {
859 <        CompletableFuture<Integer> f = new CompletableFuture<>();
860 <        ExceptionToInteger r = new ExceptionToInteger();
861 <        CompletableFuture<Integer> g = f.exceptionally(r);
862 <        f.completeExceptionally(new CFException());
863 <        checkCompletedNormally(g, three);
858 >    public void testExceptionally_exceptionalCompletion() {
859 >        for (boolean createIncomplete : new boolean[] { true, false })
860 >        for (Integer v1 : new Integer[] { 1, null })
861 >    {
862 >        final AtomicInteger a = new AtomicInteger(0);
863 >        final CFException ex = new CFException();
864 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
865 >        if (!createIncomplete) f.completeExceptionally(ex);
866 >        final CompletableFuture<Integer> g = f.exceptionally
867 >            ((Throwable t) -> {
868 >                ExecutionMode.SYNC.checkExecutionMode();
869 >                threadAssertSame(t, ex);
870 >                a.getAndIncrement();
871 >                return v1;
872 >            });
873 >        if (createIncomplete) f.completeExceptionally(ex);
874 >
875 >        checkCompletedNormally(g, v1);
876 >        assertEquals(1, a.get());
877 >    }}
878 >
879 >    /**
880 >     * If an "exceptionally action" throws an exception, it completes
881 >     * exceptionally with that exception
882 >     */
883 >    public void testExceptionally_exceptionalCompletionActionFailed() {
884 >        for (boolean createIncomplete : new boolean[] { true, false })
885 >    {
886 >        final AtomicInteger a = new AtomicInteger(0);
887 >        final CFException ex1 = new CFException();
888 >        final CFException ex2 = new CFException();
889 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
890 >        if (!createIncomplete) f.completeExceptionally(ex1);
891 >        final CompletableFuture<Integer> g = f.exceptionally
892 >            ((Throwable t) -> {
893 >                ExecutionMode.SYNC.checkExecutionMode();
894 >                threadAssertSame(t, ex1);
895 >                a.getAndIncrement();
896 >                throw ex2;
897 >            });
898 >        if (createIncomplete) f.completeExceptionally(ex1);
899 >
900 >        checkCompletedWithWrappedException(g, ex2);
901 >        checkCompletedExceptionally(f, ex1);
902 >        assertEquals(1, a.get());
903 >    }}
904  
905 <        f = new CompletableFuture<>();
906 <        r = new ExceptionToInteger();
907 <        g = f.exceptionally(r);
908 <        f.complete(one);
909 <        checkCompletedNormally(g, one);
910 <    }
905 >    /**
906 >     * whenComplete action executes on normal completion, propagating
907 >     * source result.
908 >     */
909 >    public void testWhenComplete_normalCompletion() {
910 >        for (ExecutionMode m : ExecutionMode.values())
911 >        for (boolean createIncomplete : new boolean[] { true, false })
912 >        for (Integer v1 : new Integer[] { 1, null })
913 >    {
914 >        final AtomicInteger a = new AtomicInteger(0);
915 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
916 >        if (!createIncomplete) assertTrue(f.complete(v1));
917 >        final CompletableFuture<Integer> g = m.whenComplete
918 >            (f,
919 >             (Integer result, Throwable t) -> {
920 >                m.checkExecutionMode();
921 >                threadAssertSame(result, v1);
922 >                threadAssertNull(t);
923 >                a.getAndIncrement();
924 >            });
925 >        if (createIncomplete) assertTrue(f.complete(v1));
926 >
927 >        checkCompletedNormally(g, v1);
928 >        checkCompletedNormally(f, v1);
929 >        assertEquals(1, a.get());
930 >    }}
931  
932      /**
933 <     * handle action completes normally with function value on either
934 <     * normal or exceptional completion of source
933 >     * whenComplete action executes on exceptional completion, propagating
934 >     * source result.
935       */
936 <    public void testHandle() {
937 <        CompletableFuture<Integer> f, g;
938 <        IntegerHandler r;
936 >    public void testWhenComplete_exceptionalCompletion() {
937 >        for (ExecutionMode m : ExecutionMode.values())
938 >        for (boolean createIncomplete : new boolean[] { true, false })
939 >    {
940 >        final AtomicInteger a = new AtomicInteger(0);
941 >        final CFException ex = new CFException();
942 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
943 >        if (!createIncomplete) f.completeExceptionally(ex);
944 >        final CompletableFuture<Integer> g = m.whenComplete
945 >            (f,
946 >             (Integer result, Throwable t) -> {
947 >                m.checkExecutionMode();
948 >                threadAssertNull(result);
949 >                threadAssertSame(t, ex);
950 >                a.getAndIncrement();
951 >            });
952 >        if (createIncomplete) f.completeExceptionally(ex);
953 >
954 >        checkCompletedWithWrappedException(g, ex);
955 >        checkCompletedExceptionally(f, ex);
956 >        assertEquals(1, a.get());
957 >    }}
958 >
959 >    /**
960 >     * whenComplete action executes on cancelled source, propagating
961 >     * CancellationException.
962 >     */
963 >    public void testWhenComplete_sourceCancelled() {
964 >        for (ExecutionMode m : ExecutionMode.values())
965 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
966 >        for (boolean createIncomplete : new boolean[] { true, false })
967 >    {
968 >        final AtomicInteger a = new AtomicInteger(0);
969 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
970 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971 >        final CompletableFuture<Integer> g = m.whenComplete
972 >            (f,
973 >             (Integer result, Throwable t) -> {
974 >                m.checkExecutionMode();
975 >                threadAssertNull(result);
976 >                threadAssertTrue(t instanceof CancellationException);
977 >                a.getAndIncrement();
978 >            });
979 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
980  
981 <        f = new CompletableFuture<>();
982 <        f.completeExceptionally(new CFException());
983 <        g = f.handle(r = new IntegerHandler());
984 <        assertTrue(r.ran);
406 <        checkCompletedNormally(g, three);
981 >        checkCompletedWithWrappedCancellationException(g);
982 >        checkCancelled(f);
983 >        assertEquals(1, a.get());
984 >    }}
985  
986 <        f = new CompletableFuture<>();
987 <        g = f.handle(r = new IntegerHandler());
988 <        assertFalse(r.ran);
989 <        f.completeExceptionally(new CFException());
990 <        checkCompletedNormally(g, three);
991 <        assertTrue(r.ran);
986 >    /**
987 >     * If a whenComplete action throws an exception when triggered by
988 >     * a normal completion, it completes exceptionally
989 >     */
990 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
991 >        for (boolean createIncomplete : new boolean[] { true, false })
992 >        for (ExecutionMode m : ExecutionMode.values())
993 >        for (Integer v1 : new Integer[] { 1, null })
994 >    {
995 >        final AtomicInteger a = new AtomicInteger(0);
996 >        final CFException ex = new CFException();
997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
998 >        if (!createIncomplete) assertTrue(f.complete(v1));
999 >        final CompletableFuture<Integer> g = m.whenComplete
1000 >            (f,
1001 >             (Integer result, Throwable t) -> {
1002 >                m.checkExecutionMode();
1003 >                threadAssertSame(result, v1);
1004 >                threadAssertNull(t);
1005 >                a.getAndIncrement();
1006 >                throw ex;
1007 >            });
1008 >        if (createIncomplete) assertTrue(f.complete(v1));
1009 >
1010 >        checkCompletedWithWrappedException(g, ex);
1011 >        checkCompletedNormally(f, v1);
1012 >        assertEquals(1, a.get());
1013 >    }}
1014  
1015 <        f = new CompletableFuture<>();
1016 <        f.complete(one);
1017 <        g = f.handle(r = new IntegerHandler());
1018 <        assertTrue(r.ran);
1019 <        checkCompletedNormally(g, two);
1015 >    /**
1016 >     * If a whenComplete action throws an exception when triggered by
1017 >     * a source completion that also throws an exception, the source
1018 >     * exception takes precedence (unlike handle)
1019 >     */
1020 >    public void testWhenComplete_sourceFailedActionFailed() {
1021 >        for (boolean createIncomplete : new boolean[] { true, false })
1022 >        for (ExecutionMode m : ExecutionMode.values())
1023 >    {
1024 >        final AtomicInteger a = new AtomicInteger(0);
1025 >        final CFException ex1 = new CFException();
1026 >        final CFException ex2 = new CFException();
1027 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1028 >
1029 >        if (!createIncomplete) f.completeExceptionally(ex1);
1030 >        final CompletableFuture<Integer> g = m.whenComplete
1031 >            (f,
1032 >             (Integer result, Throwable t) -> {
1033 >                m.checkExecutionMode();
1034 >                threadAssertSame(t, ex1);
1035 >                threadAssertNull(result);
1036 >                a.getAndIncrement();
1037 >                throw ex2;
1038 >            });
1039 >        if (createIncomplete) f.completeExceptionally(ex1);
1040 >
1041 >        checkCompletedWithWrappedException(g, ex1);
1042 >        checkCompletedExceptionally(f, ex1);
1043 >        assertEquals(1, a.get());
1044 >    }}
1045 >
1046 >    /**
1047 >     * handle action completes normally with function value on normal
1048 >     * completion of source
1049 >     */
1050 >    public void testHandle_normalCompletion() {
1051 >        for (ExecutionMode m : ExecutionMode.values())
1052 >        for (boolean createIncomplete : new boolean[] { true, false })
1053 >        for (Integer v1 : new Integer[] { 1, null })
1054 >    {
1055 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1056 >        final AtomicInteger a = new AtomicInteger(0);
1057 >        if (!createIncomplete) assertTrue(f.complete(v1));
1058 >        final CompletableFuture<Integer> g = m.handle
1059 >            (f,
1060 >             (Integer result, Throwable t) -> {
1061 >                m.checkExecutionMode();
1062 >                threadAssertSame(result, v1);
1063 >                threadAssertNull(t);
1064 >                a.getAndIncrement();
1065 >                return inc(v1);
1066 >            });
1067 >        if (createIncomplete) assertTrue(f.complete(v1));
1068 >
1069 >        checkCompletedNormally(g, inc(v1));
1070 >        checkCompletedNormally(f, v1);
1071 >        assertEquals(1, a.get());
1072 >    }}
1073  
1074 <        f = new CompletableFuture<>();
1075 <        g = f.handle(r = new IntegerHandler());
1076 <        assertFalse(r.ran);
1077 <        f.complete(one);
1078 <        assertTrue(r.ran);
1079 <        checkCompletedNormally(g, two);
1080 <    }
1074 >    /**
1075 >     * handle action completes normally with function value on
1076 >     * exceptional completion of source
1077 >     */
1078 >    public void testHandle_exceptionalCompletion() {
1079 >        for (ExecutionMode m : ExecutionMode.values())
1080 >        for (boolean createIncomplete : new boolean[] { true, false })
1081 >        for (Integer v1 : new Integer[] { 1, null })
1082 >    {
1083 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1084 >        final AtomicInteger a = new AtomicInteger(0);
1085 >        final CFException ex = new CFException();
1086 >        if (!createIncomplete) f.completeExceptionally(ex);
1087 >        final CompletableFuture<Integer> g = m.handle
1088 >            (f,
1089 >             (Integer result, Throwable t) -> {
1090 >                m.checkExecutionMode();
1091 >                threadAssertNull(result);
1092 >                threadAssertSame(t, ex);
1093 >                a.getAndIncrement();
1094 >                return v1;
1095 >            });
1096 >        if (createIncomplete) f.completeExceptionally(ex);
1097 >
1098 >        checkCompletedNormally(g, v1);
1099 >        checkCompletedExceptionally(f, ex);
1100 >        assertEquals(1, a.get());
1101 >    }}
1102 >
1103 >    /**
1104 >     * handle action completes normally with function value on
1105 >     * cancelled source
1106 >     */
1107 >    public void testHandle_sourceCancelled() {
1108 >        for (ExecutionMode m : ExecutionMode.values())
1109 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1110 >        for (boolean createIncomplete : new boolean[] { true, false })
1111 >        for (Integer v1 : new Integer[] { 1, null })
1112 >    {
1113 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1114 >        final AtomicInteger a = new AtomicInteger(0);
1115 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1116 >        final CompletableFuture<Integer> g = m.handle
1117 >            (f,
1118 >             (Integer result, Throwable t) -> {
1119 >                m.checkExecutionMode();
1120 >                threadAssertNull(result);
1121 >                threadAssertTrue(t instanceof CancellationException);
1122 >                a.getAndIncrement();
1123 >                return v1;
1124 >            });
1125 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1126 >
1127 >        checkCompletedNormally(g, v1);
1128 >        checkCancelled(f);
1129 >        assertEquals(1, a.get());
1130 >    }}
1131  
1132      /**
1133 <     * runAsync completes after running Runnable
1133 >     * If a "handle action" throws an exception when triggered by
1134 >     * a normal completion, it completes exceptionally
1135       */
1136 <    public void testRunAsync() {
1137 <        Noop r = new Noop();
1138 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1139 <        assertNull(f.join());
1140 <        assertTrue(r.ran);
1141 <        checkCompletedNormally(f, null);
1142 <    }
1136 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1137 >        for (ExecutionMode m : ExecutionMode.values())
1138 >        for (boolean createIncomplete : new boolean[] { true, false })
1139 >        for (Integer v1 : new Integer[] { 1, null })
1140 >    {
1141 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1142 >        final AtomicInteger a = new AtomicInteger(0);
1143 >        final CFException ex = new CFException();
1144 >        if (!createIncomplete) assertTrue(f.complete(v1));
1145 >        final CompletableFuture<Integer> g = m.handle
1146 >            (f,
1147 >             (Integer result, Throwable t) -> {
1148 >                m.checkExecutionMode();
1149 >                threadAssertSame(result, v1);
1150 >                threadAssertNull(t);
1151 >                a.getAndIncrement();
1152 >                throw ex;
1153 >            });
1154 >        if (createIncomplete) assertTrue(f.complete(v1));
1155 >
1156 >        checkCompletedWithWrappedException(g, ex);
1157 >        checkCompletedNormally(f, v1);
1158 >        assertEquals(1, a.get());
1159 >    }}
1160 >
1161 >    /**
1162 >     * If a "handle action" throws an exception when triggered by
1163 >     * a source completion that also throws an exception, the action
1164 >     * exception takes precedence (unlike whenComplete)
1165 >     */
1166 >    public void testHandle_sourceFailedActionFailed() {
1167 >        for (boolean createIncomplete : new boolean[] { true, false })
1168 >        for (ExecutionMode m : ExecutionMode.values())
1169 >    {
1170 >        final AtomicInteger a = new AtomicInteger(0);
1171 >        final CFException ex1 = new CFException();
1172 >        final CFException ex2 = new CFException();
1173 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174 >
1175 >        if (!createIncomplete) f.completeExceptionally(ex1);
1176 >        final CompletableFuture<Integer> g = m.handle
1177 >            (f,
1178 >             (Integer result, Throwable t) -> {
1179 >                m.checkExecutionMode();
1180 >                threadAssertNull(result);
1181 >                threadAssertSame(ex1, t);
1182 >                a.getAndIncrement();
1183 >                throw ex2;
1184 >            });
1185 >        if (createIncomplete) f.completeExceptionally(ex1);
1186 >
1187 >        checkCompletedWithWrappedException(g, ex2);
1188 >        checkCompletedExceptionally(f, ex1);
1189 >        assertEquals(1, a.get());
1190 >    }}
1191  
1192      /**
1193 <     * runAsync with executor completes after running Runnable
1193 >     * runAsync completes after running Runnable
1194       */
1195 <    public void testRunAsync2() {
1196 <        Noop r = new Noop();
1197 <        ThreadExecutor exec = new ThreadExecutor();
1198 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
1195 >    public void testRunAsync_normalCompletion() {
1196 >        ExecutionMode[] executionModes = {
1197 >            ExecutionMode.ASYNC,
1198 >            ExecutionMode.EXECUTOR,
1199 >        };
1200 >        for (ExecutionMode m : executionModes)
1201 >    {
1202 >        final Noop r = new Noop(m);
1203 >        final CompletableFuture<Void> f = m.runAsync(r);
1204          assertNull(f.join());
448        assertTrue(r.ran);
1205          checkCompletedNormally(f, null);
1206 <        assertEquals(1, exec.count.get());
1207 <    }
1206 >        r.assertInvoked();
1207 >    }}
1208  
1209      /**
1210       * failing runAsync completes exceptionally after running Runnable
1211       */
1212 <    public void testRunAsync3() {
1213 <        FailingNoop r = new FailingNoop();
1214 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1212 >    public void testRunAsync_exceptionalCompletion() {
1213 >        ExecutionMode[] executionModes = {
1214 >            ExecutionMode.ASYNC,
1215 >            ExecutionMode.EXECUTOR,
1216 >        };
1217 >        for (ExecutionMode m : executionModes)
1218 >    {
1219 >        final FailingRunnable r = new FailingRunnable(m);
1220 >        final CompletableFuture<Void> f = m.runAsync(r);
1221          checkCompletedWithWrappedCFException(f);
1222 <        assertTrue(r.ran);
1223 <    }
1222 >        r.assertInvoked();
1223 >    }}
1224  
1225      /**
1226       * supplyAsync completes with result of supplier
1227       */
1228 <    public void testSupplyAsync() {
1229 <        CompletableFuture<Integer> f;
1230 <        f = CompletableFuture.supplyAsync(supplyOne);
1231 <        assertEquals(f.join(), one);
1232 <        checkCompletedNormally(f, one);
1233 <    }
1234 <
1235 <    /**
1236 <     * supplyAsync with executor completes with result of supplier
1237 <     */
1238 <    public void testSupplyAsync2() {
1239 <        CompletableFuture<Integer> f;
1240 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1241 <        assertEquals(f.join(), one);
480 <        checkCompletedNormally(f, one);
481 <    }
1228 >    public void testSupplyAsync_normalCompletion() {
1229 >        ExecutionMode[] executionModes = {
1230 >            ExecutionMode.ASYNC,
1231 >            ExecutionMode.EXECUTOR,
1232 >        };
1233 >        for (ExecutionMode m : executionModes)
1234 >        for (Integer v1 : new Integer[] { 1, null })
1235 >    {
1236 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1237 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1238 >        assertSame(v1, f.join());
1239 >        checkCompletedNormally(f, v1);
1240 >        r.assertInvoked();
1241 >    }}
1242  
1243      /**
1244       * Failing supplyAsync completes exceptionally
1245       */
1246 <    public void testSupplyAsync3() {
1247 <        FailingSupplier r = new FailingSupplier();
1248 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1246 >    public void testSupplyAsync_exceptionalCompletion() {
1247 >        ExecutionMode[] executionModes = {
1248 >            ExecutionMode.ASYNC,
1249 >            ExecutionMode.EXECUTOR,
1250 >        };
1251 >        for (ExecutionMode m : executionModes)
1252 >    {
1253 >        FailingSupplier r = new FailingSupplier(m);
1254 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1255          checkCompletedWithWrappedCFException(f);
1256 <        assertTrue(r.ran);
1257 <    }
1256 >        r.assertInvoked();
1257 >    }}
1258  
1259      // seq completion methods
1260  
1261      /**
1262       * thenRun result completes normally after normal completion of source
1263       */
1264 <    public void testThenRun() {
1265 <        CompletableFuture<Integer> f;
1266 <        CompletableFuture<Void> g;
1267 <        Noop r;
1268 <
1269 <        f = new CompletableFuture<>();
1270 <        g = f.thenRun(r = new Noop());
1271 <        f.complete(null);
1272 <        checkCompletedNormally(g, null);
1273 <        assertTrue(r.ran);
1274 <
1275 <        f = new CompletableFuture<>();
1276 <        f.complete(null);
1277 <        g = f.thenRun(r = new Noop());
1278 <        checkCompletedNormally(g, null);
1279 <        assertTrue(r.ran);
1280 <    }
1264 >    public void testThenRun_normalCompletion() {
1265 >        for (ExecutionMode m : ExecutionMode.values())
1266 >        for (Integer v1 : new Integer[] { 1, null })
1267 >    {
1268 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1269 >        final Noop[] rs = new Noop[6];
1270 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1271 >
1272 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1273 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1274 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1275 >        checkIncomplete(h0);
1276 >        checkIncomplete(h1);
1277 >        checkIncomplete(h2);
1278 >        assertTrue(f.complete(v1));
1279 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1280 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1281 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1282 >
1283 >        checkCompletedNormally(h0, null);
1284 >        checkCompletedNormally(h1, null);
1285 >        checkCompletedNormally(h2, null);
1286 >        checkCompletedNormally(h3, null);
1287 >        checkCompletedNormally(h4, null);
1288 >        checkCompletedNormally(h5, null);
1289 >        checkCompletedNormally(f, v1);
1290 >        for (Noop r : rs) r.assertInvoked();
1291 >    }}
1292  
1293      /**
1294       * thenRun result completes exceptionally after exceptional
1295       * completion of source
1296       */
1297 <    public void testThenRun2() {
1298 <        CompletableFuture<Integer> f;
1299 <        CompletableFuture<Void> g;
1300 <        Noop r;
1301 <
1302 <        f = new CompletableFuture<>();
1303 <        g = f.thenRun(r = new Noop());
1304 <        f.completeExceptionally(new CFException());
1305 <        checkCompletedWithWrappedCFException(g);
1306 <        assertFalse(r.ran);
1307 <
1308 <        f = new CompletableFuture<>();
1309 <        f.completeExceptionally(new CFException());
1310 <        g = f.thenRun(r = new Noop());
1311 <        checkCompletedWithWrappedCFException(g);
1312 <        assertFalse(r.ran);
1313 <    }
1297 >    public void testThenRun_exceptionalCompletion() {
1298 >        for (ExecutionMode m : ExecutionMode.values())
1299 >    {
1300 >        final CFException ex = new CFException();
1301 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1302 >        final Noop[] rs = new Noop[6];
1303 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1304 >
1305 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1306 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1307 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1308 >        checkIncomplete(h0);
1309 >        checkIncomplete(h1);
1310 >        checkIncomplete(h2);
1311 >        assertTrue(f.completeExceptionally(ex));
1312 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1313 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1314 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1315 >
1316 >        checkCompletedWithWrappedException(h0, ex);
1317 >        checkCompletedWithWrappedException(h1, ex);
1318 >        checkCompletedWithWrappedException(h2, ex);
1319 >        checkCompletedWithWrappedException(h3, ex);
1320 >        checkCompletedWithWrappedException(h4, ex);
1321 >        checkCompletedWithWrappedException(h5, ex);
1322 >        checkCompletedExceptionally(f, ex);
1323 >        for (Noop r : rs) r.assertNotInvoked();
1324 >    }}
1325  
1326      /**
1327 <     * thenRun result completes exceptionally if action does
1327 >     * thenRun result completes exceptionally if source cancelled
1328       */
1329 <    public void testThenRun3() {
1330 <        CompletableFuture<Integer> f;
1331 <        CompletableFuture<Void> g;
1332 <        FailingNoop r;
1333 <
1334 <        f = new CompletableFuture<>();
1335 <        g = f.thenRun(r = new FailingNoop());
1336 <        f.complete(null);
1337 <        checkCompletedWithWrappedCFException(g);
1338 <
1339 <        f = new CompletableFuture<>();
1340 <        f.complete(null);
1341 <        g = f.thenRun(r = new FailingNoop());
1342 <        checkCompletedWithWrappedCFException(g);
1343 <    }
1329 >    public void testThenRun_sourceCancelled() {
1330 >        for (ExecutionMode m : ExecutionMode.values())
1331 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1332 >    {
1333 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1334 >        final Noop[] rs = new Noop[6];
1335 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1336 >
1337 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1338 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1339 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1340 >        checkIncomplete(h0);
1341 >        checkIncomplete(h1);
1342 >        checkIncomplete(h2);
1343 >        assertTrue(f.cancel(mayInterruptIfRunning));
1344 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1345 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1346 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1347 >
1348 >        checkCompletedWithWrappedCancellationException(h0);
1349 >        checkCompletedWithWrappedCancellationException(h1);
1350 >        checkCompletedWithWrappedCancellationException(h2);
1351 >        checkCompletedWithWrappedCancellationException(h3);
1352 >        checkCompletedWithWrappedCancellationException(h4);
1353 >        checkCompletedWithWrappedCancellationException(h5);
1354 >        checkCancelled(f);
1355 >        for (Noop r : rs) r.assertNotInvoked();
1356 >    }}
1357  
1358      /**
1359 <     * thenRun result completes exceptionally if source cancelled
1359 >     * thenRun result completes exceptionally if action does
1360       */
1361 <    public void testThenRun4() {
1362 <        CompletableFuture<Integer> f;
1363 <        CompletableFuture<Void> g;
1364 <        Noop r;
1365 <
1366 <        f = new CompletableFuture<>();
1367 <        g = f.thenRun(r = new Noop());
1368 <        assertTrue(f.cancel(true));
1369 <        checkCompletedWithWrappedCancellationException(g);
1370 <
1371 <        f = new CompletableFuture<>();
1372 <        assertTrue(f.cancel(true));
1373 <        g = f.thenRun(r = new Noop());
1374 <        checkCompletedWithWrappedCancellationException(g);
1375 <    }
1361 >    public void testThenRun_actionFailed() {
1362 >        for (ExecutionMode m : ExecutionMode.values())
1363 >        for (Integer v1 : new Integer[] { 1, null })
1364 >    {
1365 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1366 >        final FailingRunnable[] rs = new FailingRunnable[6];
1367 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1368 >
1369 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1370 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1371 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1372 >        assertTrue(f.complete(v1));
1373 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1374 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1375 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1376 >
1377 >        checkCompletedWithWrappedCFException(h0);
1378 >        checkCompletedWithWrappedCFException(h1);
1379 >        checkCompletedWithWrappedCFException(h2);
1380 >        checkCompletedWithWrappedCFException(h3);
1381 >        checkCompletedWithWrappedCFException(h4);
1382 >        checkCompletedWithWrappedCFException(h5);
1383 >        checkCompletedNormally(f, v1);
1384 >    }}
1385  
1386      /**
1387       * thenApply result completes normally after normal completion of source
1388       */
1389 <    public void testThenApply() {
1390 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1391 <        CompletableFuture<Integer> g = f.thenApply(inc);
1392 <        f.complete(one);
1393 <        checkCompletedNormally(g, two);
1394 <    }
1389 >    public void testThenApply_normalCompletion() {
1390 >        for (ExecutionMode m : ExecutionMode.values())
1391 >        for (Integer v1 : new Integer[] { 1, null })
1392 >    {
1393 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1394 >        final IncFunction[] rs = new IncFunction[4];
1395 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1396 >
1397 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1398 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1399 >        checkIncomplete(h0);
1400 >        checkIncomplete(h1);
1401 >        assertTrue(f.complete(v1));
1402 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1403 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1404 >
1405 >        checkCompletedNormally(h0, inc(v1));
1406 >        checkCompletedNormally(h1, inc(v1));
1407 >        checkCompletedNormally(h2, inc(v1));
1408 >        checkCompletedNormally(h3, inc(v1));
1409 >        checkCompletedNormally(f, v1);
1410 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1411 >    }}
1412  
1413      /**
1414       * thenApply result completes exceptionally after exceptional
1415       * completion of source
1416       */
1417 <    public void testThenApply2() {
1418 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1419 <        CompletableFuture<Integer> g = f.thenApply(inc);
1420 <        f.completeExceptionally(new CFException());
1421 <        checkCompletedWithWrappedCFException(g);
1422 <    }
1417 >    public void testThenApply_exceptionalCompletion() {
1418 >        for (ExecutionMode m : ExecutionMode.values())
1419 >    {
1420 >        final CFException ex = new CFException();
1421 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1422 >        final IncFunction[] rs = new IncFunction[4];
1423 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1424 >
1425 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1426 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1427 >        assertTrue(f.completeExceptionally(ex));
1428 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1429 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1430 >
1431 >        checkCompletedWithWrappedException(h0, ex);
1432 >        checkCompletedWithWrappedException(h1, ex);
1433 >        checkCompletedWithWrappedException(h2, ex);
1434 >        checkCompletedWithWrappedException(h3, ex);
1435 >        checkCompletedExceptionally(f, ex);
1436 >        for (IncFunction r : rs) r.assertNotInvoked();
1437 >    }}
1438  
1439      /**
1440 <     * thenApply result completes exceptionally if action does
1440 >     * thenApply result completes exceptionally if source cancelled
1441       */
1442 <    public void testThenApply3() {
1443 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1444 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1445 <        f.complete(one);
1446 <        checkCompletedWithWrappedCFException(g);
1447 <    }
1442 >    public void testThenApply_sourceCancelled() {
1443 >        for (ExecutionMode m : ExecutionMode.values())
1444 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1445 >    {
1446 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1447 >        final IncFunction[] rs = new IncFunction[4];
1448 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1449 >
1450 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1451 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1452 >        assertTrue(f.cancel(mayInterruptIfRunning));
1453 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1454 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1455 >
1456 >        checkCompletedWithWrappedCancellationException(h0);
1457 >        checkCompletedWithWrappedCancellationException(h1);
1458 >        checkCompletedWithWrappedCancellationException(h2);
1459 >        checkCompletedWithWrappedCancellationException(h3);
1460 >        checkCancelled(f);
1461 >        for (IncFunction r : rs) r.assertNotInvoked();
1462 >    }}
1463  
1464      /**
1465 <     * thenApply result completes exceptionally if source cancelled
1465 >     * thenApply result completes exceptionally if action does
1466       */
1467 <    public void testThenApply4() {
1468 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1469 <        CompletableFuture<Integer> g = f.thenApply(inc);
1470 <        assertTrue(f.cancel(true));
1471 <        checkCompletedWithWrappedCancellationException(g);
1472 <    }
1467 >    public void testThenApply_actionFailed() {
1468 >        for (ExecutionMode m : ExecutionMode.values())
1469 >        for (Integer v1 : new Integer[] { 1, null })
1470 >    {
1471 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1472 >        final FailingFunction[] rs = new FailingFunction[4];
1473 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1474 >
1475 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1476 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1477 >        assertTrue(f.complete(v1));
1478 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1479 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1480 >
1481 >        checkCompletedWithWrappedCFException(h0);
1482 >        checkCompletedWithWrappedCFException(h1);
1483 >        checkCompletedWithWrappedCFException(h2);
1484 >        checkCompletedWithWrappedCFException(h3);
1485 >        checkCompletedNormally(f, v1);
1486 >    }}
1487  
1488      /**
1489       * thenAccept result completes normally after normal completion of source
1490       */
1491 <    public void testThenAccept() {
1492 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1493 <        IncAction r = new IncAction();
1494 <        CompletableFuture<Void> g = f.thenAccept(r);
1495 <        f.complete(one);
1496 <        checkCompletedNormally(g, null);
1497 <        assertEquals(r.value, 2);
1498 <    }
1491 >    public void testThenAccept_normalCompletion() {
1492 >        for (ExecutionMode m : ExecutionMode.values())
1493 >        for (Integer v1 : new Integer[] { 1, null })
1494 >    {
1495 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1496 >        final NoopConsumer[] rs = new NoopConsumer[4];
1497 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1498 >
1499 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1500 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1501 >        checkIncomplete(h0);
1502 >        checkIncomplete(h1);
1503 >        assertTrue(f.complete(v1));
1504 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1505 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1506 >
1507 >        checkCompletedNormally(h0, null);
1508 >        checkCompletedNormally(h1, null);
1509 >        checkCompletedNormally(h2, null);
1510 >        checkCompletedNormally(h3, null);
1511 >        checkCompletedNormally(f, v1);
1512 >        for (NoopConsumer r : rs) r.assertValue(v1);
1513 >    }}
1514  
1515      /**
1516       * thenAccept result completes exceptionally after exceptional
1517       * completion of source
1518       */
1519 <    public void testThenAccept2() {
1520 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1521 <        IncAction r = new IncAction();
1522 <        CompletableFuture<Void> g = f.thenAccept(r);
1523 <        f.completeExceptionally(new CFException());
1524 <        checkCompletedWithWrappedCFException(g);
1525 <    }
1519 >    public void testThenAccept_exceptionalCompletion() {
1520 >        for (ExecutionMode m : ExecutionMode.values())
1521 >    {
1522 >        final CFException ex = new CFException();
1523 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1524 >        final NoopConsumer[] rs = new NoopConsumer[4];
1525 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1526 >
1527 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1528 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1529 >        assertTrue(f.completeExceptionally(ex));
1530 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1531 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1532 >
1533 >        checkCompletedWithWrappedException(h0, ex);
1534 >        checkCompletedWithWrappedException(h1, ex);
1535 >        checkCompletedWithWrappedException(h2, ex);
1536 >        checkCompletedWithWrappedException(h3, ex);
1537 >        checkCompletedExceptionally(f, ex);
1538 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1539 >    }}
1540  
1541      /**
1542 <     * thenAccept result completes exceptionally if action does
1542 >     * thenAccept result completes exceptionally if source cancelled
1543       */
1544 <    public void testThenAccept3() {
1545 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1546 <        FailingConsumer r = new FailingConsumer();
1547 <        CompletableFuture<Void> g = f.thenAccept(r);
1548 <        f.complete(one);
1549 <        checkCompletedWithWrappedCFException(g);
1550 <        assertTrue(r.ran);
1551 <    }
1544 >    public void testThenAccept_sourceCancelled() {
1545 >        for (ExecutionMode m : ExecutionMode.values())
1546 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1547 >    {
1548 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1549 >        final NoopConsumer[] rs = new NoopConsumer[4];
1550 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1551 >
1552 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1553 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1554 >        assertTrue(f.cancel(mayInterruptIfRunning));
1555 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1556 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1557 >
1558 >        checkCompletedWithWrappedCancellationException(h0);
1559 >        checkCompletedWithWrappedCancellationException(h1);
1560 >        checkCompletedWithWrappedCancellationException(h2);
1561 >        checkCompletedWithWrappedCancellationException(h3);
1562 >        checkCancelled(f);
1563 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1564 >    }}
1565  
1566      /**
1567 <     * thenAccept result completes exceptionally if source cancelled
1567 >     * thenAccept result completes exceptionally if action does
1568       */
1569 <    public void testThenAccept4() {
1570 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1571 <        IncAction r = new IncAction();
1572 <        CompletableFuture<Void> g = f.thenAccept(r);
1573 <        assertTrue(f.cancel(true));
1574 <        checkCompletedWithWrappedCancellationException(g);
1575 <    }
1569 >    public void testThenAccept_actionFailed() {
1570 >        for (ExecutionMode m : ExecutionMode.values())
1571 >        for (Integer v1 : new Integer[] { 1, null })
1572 >    {
1573 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1574 >        final FailingConsumer[] rs = new FailingConsumer[4];
1575 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1576 >
1577 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1578 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1579 >        assertTrue(f.complete(v1));
1580 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1581 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1582 >
1583 >        checkCompletedWithWrappedCFException(h0);
1584 >        checkCompletedWithWrappedCFException(h1);
1585 >        checkCompletedWithWrappedCFException(h2);
1586 >        checkCompletedWithWrappedCFException(h3);
1587 >        checkCompletedNormally(f, v1);
1588 >    }}
1589  
1590      /**
1591       * thenCombine result completes normally after normal completion
1592       * of sources
1593       */
1594 <    public void testThenCombine() {
1595 <        CompletableFuture<Integer> f, g, h;
1596 <
1597 <        f = new CompletableFuture<>();
1598 <        g = new CompletableFuture<>();
1599 <        h = f.thenCombine(g, subtract);
1600 <        f.complete(3);
1601 <        checkIncomplete(h);
1602 <        g.complete(1);
1603 <        checkCompletedNormally(h, 2);
1604 <
1605 <        f = new CompletableFuture<>();
1606 <        g = new CompletableFuture<>();
1607 <        h = f.thenCombine(g, subtract);
1608 <        g.complete(1);
1609 <        checkIncomplete(h);
1610 <        f.complete(3);
1611 <        checkCompletedNormally(h, 2);
1612 <
1613 <        f = new CompletableFuture<>();
1614 <        g = new CompletableFuture<>();
1615 <        g.complete(1);
1616 <        f.complete(3);
1617 <        h = f.thenCombine(g, subtract);
1618 <        checkCompletedNormally(h, 2);
1619 <    }
1594 >    public void testThenCombine_normalCompletion() {
1595 >        for (ExecutionMode m : ExecutionMode.values())
1596 >        for (boolean fFirst : new boolean[] { true, false })
1597 >        for (Integer v1 : new Integer[] { 1, null })
1598 >        for (Integer v2 : new Integer[] { 2, null })
1599 >    {
1600 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1601 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1602 >        final SubtractFunction[] rs = new SubtractFunction[6];
1603 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1604 >
1605 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1606 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1607 >        final Integer w1 =  fFirst ? v1 : v2;
1608 >        final Integer w2 = !fFirst ? v1 : v2;
1609 >
1610 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1611 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1612 >        assertTrue(fst.complete(w1));
1613 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1614 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1615 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1616 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1617 >        checkCompletedNormally(h1, subtract(w1, w1));
1618 >        checkCompletedNormally(h3, subtract(w1, w1));
1619 >        rs[1].assertValue(subtract(w1, w1));
1620 >        rs[3].assertValue(subtract(w1, w1));
1621 >        assertTrue(snd.complete(w2));
1622 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1623 >
1624 >        checkCompletedNormally(h0, subtract(v1, v2));
1625 >        checkCompletedNormally(h2, subtract(v1, v2));
1626 >        checkCompletedNormally(h4, subtract(v1, v2));
1627 >        rs[0].assertValue(subtract(v1, v2));
1628 >        rs[2].assertValue(subtract(v1, v2));
1629 >        rs[4].assertValue(subtract(v1, v2));
1630 >
1631 >        checkCompletedNormally(f, v1);
1632 >        checkCompletedNormally(g, v2);
1633 >    }}
1634  
1635      /**
1636       * thenCombine result completes exceptionally after exceptional
1637       * completion of either source
1638       */
1639 <    public void testThenCombine2() {
1640 <        CompletableFuture<Integer> f, g, h;
1641 <
1642 <        f = new CompletableFuture<>();
1643 <        g = new CompletableFuture<>();
1644 <        h = f.thenCombine(g, subtract);
1645 <        f.completeExceptionally(new CFException());
1646 <        checkIncomplete(h);
1647 <        g.complete(1);
1648 <        checkCompletedWithWrappedCFException(h);
1649 <
1650 <        f = new CompletableFuture<>();
1651 <        g = new CompletableFuture<>();
1652 <        h = f.thenCombine(g, subtract);
1653 <        g.completeExceptionally(new CFException());
1654 <        checkIncomplete(h);
1655 <        f.complete(3);
1656 <        checkCompletedWithWrappedCFException(h);
1657 <
1658 <        f = new CompletableFuture<>();
1659 <        g = new CompletableFuture<>();
1660 <        f.complete(3);
1661 <        g.completeExceptionally(new CFException());
1662 <        h = f.thenCombine(g, subtract);
1663 <        checkCompletedWithWrappedCFException(h);
1664 <
1665 <        f = new CompletableFuture<>();
1666 <        g = new CompletableFuture<>();
1667 <        f.completeExceptionally(new CFException());
1668 <        g.complete(3);
1669 <        h = f.thenCombine(g, subtract);
1670 <        checkCompletedWithWrappedCFException(h);
1671 <    }
1639 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1640 >        for (ExecutionMode m : ExecutionMode.values())
1641 >        for (boolean fFirst : new boolean[] { true, false })
1642 >        for (boolean failFirst : new boolean[] { true, false })
1643 >        for (Integer v1 : new Integer[] { 1, null })
1644 >    {
1645 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1646 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1647 >        final CFException ex = new CFException();
1648 >        final SubtractFunction r1 = new SubtractFunction(m);
1649 >        final SubtractFunction r2 = new SubtractFunction(m);
1650 >        final SubtractFunction r3 = new SubtractFunction(m);
1651 >
1652 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1653 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1654 >        final Callable<Boolean> complete1 = failFirst ?
1655 >            () -> fst.completeExceptionally(ex) :
1656 >            () -> fst.complete(v1);
1657 >        final Callable<Boolean> complete2 = failFirst ?
1658 >            () -> snd.complete(v1) :
1659 >            () -> snd.completeExceptionally(ex);
1660 >
1661 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1662 >        assertTrue(complete1.call());
1663 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1664 >        checkIncomplete(h1);
1665 >        checkIncomplete(h2);
1666 >        assertTrue(complete2.call());
1667 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1668 >
1669 >        checkCompletedWithWrappedException(h1, ex);
1670 >        checkCompletedWithWrappedException(h2, ex);
1671 >        checkCompletedWithWrappedException(h3, ex);
1672 >        r1.assertNotInvoked();
1673 >        r2.assertNotInvoked();
1674 >        r3.assertNotInvoked();
1675 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1676 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1677 >    }}
1678  
1679      /**
1680 <     * thenCombine result completes exceptionally if action does
1680 >     * thenCombine result completes exceptionally if either source cancelled
1681       */
1682 <    public void testThenCombine3() {
1683 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1684 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1685 <        FailingBiFunction r = new FailingBiFunction();
1686 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1687 <        f.complete(one);
1688 <        checkIncomplete(g);
1689 <        assertFalse(r.ran);
1690 <        f2.complete(two);
1691 <        checkCompletedWithWrappedCFException(g);
1692 <        assertTrue(r.ran);
1693 <    }
1682 >    public void testThenCombine_sourceCancelled() throws Throwable {
1683 >        for (ExecutionMode m : ExecutionMode.values())
1684 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1685 >        for (boolean fFirst : new boolean[] { true, false })
1686 >        for (boolean failFirst : new boolean[] { true, false })
1687 >        for (Integer v1 : new Integer[] { 1, null })
1688 >    {
1689 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1690 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1691 >        final SubtractFunction r1 = new SubtractFunction(m);
1692 >        final SubtractFunction r2 = new SubtractFunction(m);
1693 >        final SubtractFunction r3 = new SubtractFunction(m);
1694 >
1695 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1696 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1697 >        final Callable<Boolean> complete1 = failFirst ?
1698 >            () -> fst.cancel(mayInterruptIfRunning) :
1699 >            () -> fst.complete(v1);
1700 >        final Callable<Boolean> complete2 = failFirst ?
1701 >            () -> snd.complete(v1) :
1702 >            () -> snd.cancel(mayInterruptIfRunning);
1703 >
1704 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1705 >        assertTrue(complete1.call());
1706 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1707 >        checkIncomplete(h1);
1708 >        checkIncomplete(h2);
1709 >        assertTrue(complete2.call());
1710 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1711 >
1712 >        checkCompletedWithWrappedCancellationException(h1);
1713 >        checkCompletedWithWrappedCancellationException(h2);
1714 >        checkCompletedWithWrappedCancellationException(h3);
1715 >        r1.assertNotInvoked();
1716 >        r2.assertNotInvoked();
1717 >        r3.assertNotInvoked();
1718 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1719 >        checkCancelled(failFirst ? fst : snd);
1720 >    }}
1721  
1722      /**
1723 <     * thenCombine result completes exceptionally if either source cancelled
1723 >     * thenCombine result completes exceptionally if action does
1724       */
1725 <    public void testThenCombine4() {
1726 <        CompletableFuture<Integer> f, g, h;
1727 <
1728 <        f = new CompletableFuture<>();
1729 <        g = new CompletableFuture<>();
1730 <        h = f.thenCombine(g, subtract);
1731 <        assertTrue(f.cancel(true));
1732 <        checkIncomplete(h);
1733 <        g.complete(1);
1734 <        checkCompletedWithWrappedCancellationException(h);
1735 <
1736 <        f = new CompletableFuture<>();
1737 <        g = new CompletableFuture<>();
1738 <        h = f.thenCombine(g, subtract);
1739 <        assertTrue(g.cancel(true));
1740 <        checkIncomplete(h);
1741 <        f.complete(3);
1742 <        checkCompletedWithWrappedCancellationException(h);
1743 <
1744 <        f = new CompletableFuture<>();
1745 <        g = new CompletableFuture<>();
1746 <        assertTrue(f.cancel(true));
1747 <        assertTrue(g.cancel(true));
1748 <        h = f.thenCombine(g, subtract);
1749 <        checkCompletedWithWrappedCancellationException(h);
1750 <    }
1725 >    public void testThenCombine_actionFailed() {
1726 >        for (ExecutionMode m : ExecutionMode.values())
1727 >        for (boolean fFirst : new boolean[] { true, false })
1728 >        for (Integer v1 : new Integer[] { 1, null })
1729 >        for (Integer v2 : new Integer[] { 2, null })
1730 >    {
1731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1733 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1734 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1735 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1736 >
1737 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1738 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1739 >        final Integer w1 =  fFirst ? v1 : v2;
1740 >        final Integer w2 = !fFirst ? v1 : v2;
1741 >
1742 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1743 >        assertTrue(fst.complete(w1));
1744 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1745 >        assertTrue(snd.complete(w2));
1746 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1747 >
1748 >        checkCompletedWithWrappedCFException(h1);
1749 >        checkCompletedWithWrappedCFException(h2);
1750 >        checkCompletedWithWrappedCFException(h3);
1751 >        r1.assertInvoked();
1752 >        r2.assertInvoked();
1753 >        r3.assertInvoked();
1754 >        checkCompletedNormally(f, v1);
1755 >        checkCompletedNormally(g, v2);
1756 >    }}
1757  
1758      /**
1759       * thenAcceptBoth result completes normally after normal
1760       * completion of sources
1761       */
1762 <    public void testThenAcceptBoth() {
1763 <        CompletableFuture<Integer> f, g;
1764 <        CompletableFuture<Void> h;
1765 <        SubtractAction r;
1766 <
1767 <        f = new CompletableFuture<>();
1768 <        g = new CompletableFuture<>();
1769 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1770 <        f.complete(3);
1771 <        checkIncomplete(h);
1772 <        g.complete(1);
1773 <        checkCompletedNormally(h, null);
1774 <        assertEquals(r.value, 2);
1775 <
1776 <        f = new CompletableFuture<>();
1777 <        g = new CompletableFuture<>();
1778 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1779 <        g.complete(1);
1780 <        checkIncomplete(h);
1781 <        f.complete(3);
1782 <        checkCompletedNormally(h, null);
1783 <        assertEquals(r.value, 2);
1784 <
1785 <        f = new CompletableFuture<>();
1786 <        g = new CompletableFuture<>();
1787 <        g.complete(1);
1788 <        f.complete(3);
1789 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1790 <        checkCompletedNormally(h, null);
1791 <        assertEquals(r.value, 2);
1792 <    }
1762 >    public void testThenAcceptBoth_normalCompletion() {
1763 >        for (ExecutionMode m : ExecutionMode.values())
1764 >        for (boolean fFirst : new boolean[] { true, false })
1765 >        for (Integer v1 : new Integer[] { 1, null })
1766 >        for (Integer v2 : new Integer[] { 2, null })
1767 >    {
1768 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1769 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1770 >        final SubtractAction r1 = new SubtractAction(m);
1771 >        final SubtractAction r2 = new SubtractAction(m);
1772 >        final SubtractAction r3 = new SubtractAction(m);
1773 >
1774 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1775 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1776 >        final Integer w1 =  fFirst ? v1 : v2;
1777 >        final Integer w2 = !fFirst ? v1 : v2;
1778 >
1779 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1780 >        assertTrue(fst.complete(w1));
1781 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1782 >        checkIncomplete(h1);
1783 >        checkIncomplete(h2);
1784 >        r1.assertNotInvoked();
1785 >        r2.assertNotInvoked();
1786 >        assertTrue(snd.complete(w2));
1787 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1788 >
1789 >        checkCompletedNormally(h1, null);
1790 >        checkCompletedNormally(h2, null);
1791 >        checkCompletedNormally(h3, null);
1792 >        r1.assertValue(subtract(v1, v2));
1793 >        r2.assertValue(subtract(v1, v2));
1794 >        r3.assertValue(subtract(v1, v2));
1795 >        checkCompletedNormally(f, v1);
1796 >        checkCompletedNormally(g, v2);
1797 >    }}
1798  
1799      /**
1800       * thenAcceptBoth result completes exceptionally after exceptional
1801       * completion of either source
1802       */
1803 <    public void testThenAcceptBoth2() {
1804 <        CompletableFuture<Integer> f, g;
1805 <        CompletableFuture<Void> h;
1806 <        SubtractAction r;
1807 <
1808 <        f = new CompletableFuture<>();
1809 <        g = new CompletableFuture<>();
1810 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1811 <        f.completeExceptionally(new CFException());
1812 <        checkIncomplete(h);
1813 <        g.complete(1);
1814 <        checkCompletedWithWrappedCFException(h);
1815 <
1816 <        f = new CompletableFuture<>();
1817 <        g = new CompletableFuture<>();
1818 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1819 <        g.completeExceptionally(new CFException());
1820 <        checkIncomplete(h);
1821 <        f.complete(3);
1822 <        checkCompletedWithWrappedCFException(h);
1823 <
1824 <        f = new CompletableFuture<>();
1825 <        g = new CompletableFuture<>();
1826 <        f.complete(3);
1827 <        g.completeExceptionally(new CFException());
1828 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1829 <        checkCompletedWithWrappedCFException(h);
1830 <
1831 <        f = new CompletableFuture<>();
1832 <        g = new CompletableFuture<>();
1833 <        f.completeExceptionally(new CFException());
1834 <        g.complete(3);
1835 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1836 <        checkCompletedWithWrappedCFException(h);
1837 <    }
1803 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1804 >        for (ExecutionMode m : ExecutionMode.values())
1805 >        for (boolean fFirst : new boolean[] { true, false })
1806 >        for (boolean failFirst : new boolean[] { true, false })
1807 >        for (Integer v1 : new Integer[] { 1, null })
1808 >    {
1809 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1810 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1811 >        final CFException ex = new CFException();
1812 >        final SubtractAction r1 = new SubtractAction(m);
1813 >        final SubtractAction r2 = new SubtractAction(m);
1814 >        final SubtractAction r3 = new SubtractAction(m);
1815 >
1816 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1817 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1818 >        final Callable<Boolean> complete1 = failFirst ?
1819 >            () -> fst.completeExceptionally(ex) :
1820 >            () -> fst.complete(v1);
1821 >        final Callable<Boolean> complete2 = failFirst ?
1822 >            () -> snd.complete(v1) :
1823 >            () -> snd.completeExceptionally(ex);
1824 >
1825 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1826 >        assertTrue(complete1.call());
1827 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1828 >        checkIncomplete(h1);
1829 >        checkIncomplete(h2);
1830 >        assertTrue(complete2.call());
1831 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1832 >
1833 >        checkCompletedWithWrappedException(h1, ex);
1834 >        checkCompletedWithWrappedException(h2, ex);
1835 >        checkCompletedWithWrappedException(h3, ex);
1836 >        r1.assertNotInvoked();
1837 >        r2.assertNotInvoked();
1838 >        r3.assertNotInvoked();
1839 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1840 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1841 >    }}
1842  
1843      /**
1844 <     * thenAcceptBoth result completes exceptionally if action does
1844 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1845       */
1846 <    public void testThenAcceptBoth3() {
1847 <        CompletableFuture<Integer> f, g;
1848 <        CompletableFuture<Void> h;
1849 <        FailingBiConsumer r;
1850 <
1851 <        f = new CompletableFuture<>();
1852 <        g = new CompletableFuture<>();
1853 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1854 <        f.complete(3);
1855 <        checkIncomplete(h);
1856 <        g.complete(1);
1857 <        checkCompletedWithWrappedCFException(h);
1858 <
1859 <        f = new CompletableFuture<>();
1860 <        g = new CompletableFuture<>();
1861 <        f.complete(3);
1862 <        g.complete(1);
1863 <        h = f.thenAcceptBoth(g, r = new FailingBiConsumer());
1864 <        checkCompletedWithWrappedCFException(h);
1865 <    }
1846 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1847 >        for (ExecutionMode m : ExecutionMode.values())
1848 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1849 >        for (boolean fFirst : new boolean[] { true, false })
1850 >        for (boolean failFirst : new boolean[] { true, false })
1851 >        for (Integer v1 : new Integer[] { 1, null })
1852 >    {
1853 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1854 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1855 >        final SubtractAction r1 = new SubtractAction(m);
1856 >        final SubtractAction r2 = new SubtractAction(m);
1857 >        final SubtractAction r3 = new SubtractAction(m);
1858 >
1859 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1860 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1861 >        final Callable<Boolean> complete1 = failFirst ?
1862 >            () -> fst.cancel(mayInterruptIfRunning) :
1863 >            () -> fst.complete(v1);
1864 >        final Callable<Boolean> complete2 = failFirst ?
1865 >            () -> snd.complete(v1) :
1866 >            () -> snd.cancel(mayInterruptIfRunning);
1867 >
1868 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1869 >        assertTrue(complete1.call());
1870 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1871 >        checkIncomplete(h1);
1872 >        checkIncomplete(h2);
1873 >        assertTrue(complete2.call());
1874 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1875 >
1876 >        checkCompletedWithWrappedCancellationException(h1);
1877 >        checkCompletedWithWrappedCancellationException(h2);
1878 >        checkCompletedWithWrappedCancellationException(h3);
1879 >        r1.assertNotInvoked();
1880 >        r2.assertNotInvoked();
1881 >        r3.assertNotInvoked();
1882 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1883 >        checkCancelled(failFirst ? fst : snd);
1884 >    }}
1885  
1886      /**
1887 <     * thenAcceptBoth result completes exceptionally if either source cancelled
1887 >     * thenAcceptBoth result completes exceptionally if action does
1888       */
1889 <    public void testThenAcceptBoth4() {
1890 <        CompletableFuture<Integer> f, g;
1891 <        CompletableFuture<Void> h;
1892 <        SubtractAction r;
1893 <
1894 <        f = new CompletableFuture<>();
1895 <        g = new CompletableFuture<>();
1896 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1897 <        assertTrue(f.cancel(true));
1898 <        checkIncomplete(h);
1899 <        g.complete(1);
1900 <        checkCompletedWithWrappedCancellationException(h);
1901 <
1902 <        f = new CompletableFuture<>();
1903 <        g = new CompletableFuture<>();
1904 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1905 <        assertTrue(g.cancel(true));
1906 <        checkIncomplete(h);
1907 <        f.complete(3);
1908 <        checkCompletedWithWrappedCancellationException(h);
1909 <
1910 <        f = new CompletableFuture<>();
1911 <        g = new CompletableFuture<>();
1912 <        f.complete(3);
1913 <        assertTrue(g.cancel(true));
1914 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
1915 <        checkCompletedWithWrappedCancellationException(h);
1916 <
1917 <        f = new CompletableFuture<>();
1918 <        g = new CompletableFuture<>();
1919 <        assertTrue(f.cancel(true));
1920 <        g.complete(3);
914 <        h = f.thenAcceptBoth(g, r = new SubtractAction());
915 <        checkCompletedWithWrappedCancellationException(h);
916 <    }
1889 >    public void testThenAcceptBoth_actionFailed() {
1890 >        for (ExecutionMode m : ExecutionMode.values())
1891 >        for (boolean fFirst : new boolean[] { true, false })
1892 >        for (Integer v1 : new Integer[] { 1, null })
1893 >        for (Integer v2 : new Integer[] { 2, null })
1894 >    {
1895 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1896 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1897 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1898 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1899 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1900 >
1901 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1902 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1903 >        final Integer w1 =  fFirst ? v1 : v2;
1904 >        final Integer w2 = !fFirst ? v1 : v2;
1905 >
1906 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1907 >        assertTrue(fst.complete(w1));
1908 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1909 >        assertTrue(snd.complete(w2));
1910 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1911 >
1912 >        checkCompletedWithWrappedCFException(h1);
1913 >        checkCompletedWithWrappedCFException(h2);
1914 >        checkCompletedWithWrappedCFException(h3);
1915 >        r1.assertInvoked();
1916 >        r2.assertInvoked();
1917 >        r3.assertInvoked();
1918 >        checkCompletedNormally(f, v1);
1919 >        checkCompletedNormally(g, v2);
1920 >    }}
1921  
1922      /**
1923       * runAfterBoth result completes normally after normal
1924       * completion of sources
1925       */
1926 <    public void testRunAfterBoth() {
1927 <        CompletableFuture<Integer> f, g;
1928 <        CompletableFuture<Void> h;
1929 <        Noop r;
1930 <
1931 <        f = new CompletableFuture<>();
1932 <        g = new CompletableFuture<>();
1933 <        h = f.runAfterBoth(g, r = new Noop());
1934 <        f.complete(3);
1935 <        checkIncomplete(h);
1936 <        g.complete(1);
1937 <        checkCompletedNormally(h, null);
1938 <        assertTrue(r.ran);
1939 <
1940 <        f = new CompletableFuture<>();
1941 <        g = new CompletableFuture<>();
1942 <        h = f.runAfterBoth(g, r = new Noop());
1943 <        g.complete(1);
1944 <        checkIncomplete(h);
1945 <        f.complete(3);
1946 <        checkCompletedNormally(h, null);
1947 <        assertTrue(r.ran);
1948 <
1949 <        f = new CompletableFuture<>();
1950 <        g = new CompletableFuture<>();
1951 <        g.complete(1);
1952 <        f.complete(3);
1953 <        h = f.runAfterBoth(g, r = new Noop());
1954 <        checkCompletedNormally(h, null);
1955 <        assertTrue(r.ran);
1956 <    }
1926 >    public void testRunAfterBoth_normalCompletion() {
1927 >        for (ExecutionMode m : ExecutionMode.values())
1928 >        for (boolean fFirst : new boolean[] { true, false })
1929 >        for (Integer v1 : new Integer[] { 1, null })
1930 >        for (Integer v2 : new Integer[] { 2, null })
1931 >    {
1932 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1933 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1934 >        final Noop r1 = new Noop(m);
1935 >        final Noop r2 = new Noop(m);
1936 >        final Noop r3 = new Noop(m);
1937 >
1938 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1939 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1940 >        final Integer w1 =  fFirst ? v1 : v2;
1941 >        final Integer w2 = !fFirst ? v1 : v2;
1942 >
1943 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1944 >        assertTrue(fst.complete(w1));
1945 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1946 >        checkIncomplete(h1);
1947 >        checkIncomplete(h2);
1948 >        r1.assertNotInvoked();
1949 >        r2.assertNotInvoked();
1950 >        assertTrue(snd.complete(w2));
1951 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1952 >
1953 >        checkCompletedNormally(h1, null);
1954 >        checkCompletedNormally(h2, null);
1955 >        checkCompletedNormally(h3, null);
1956 >        r1.assertInvoked();
1957 >        r2.assertInvoked();
1958 >        r3.assertInvoked();
1959 >        checkCompletedNormally(f, v1);
1960 >        checkCompletedNormally(g, v2);
1961 >    }}
1962  
1963      /**
1964       * runAfterBoth result completes exceptionally after exceptional
1965       * completion of either source
1966       */
1967 <    public void testRunAfterBoth2() {
1968 <        CompletableFuture<Integer> f, g;
1969 <        CompletableFuture<Void> h;
1970 <        Noop r;
1971 <
1972 <        f = new CompletableFuture<>();
1973 <        g = new CompletableFuture<>();
1974 <        h = f.runAfterBoth(g, r = new Noop());
1975 <        f.completeExceptionally(new CFException());
1976 <        checkIncomplete(h);
1977 <        g.complete(1);
1978 <        checkCompletedWithWrappedCFException(h);
1979 <        assertFalse(r.ran);
1980 <
1981 <        f = new CompletableFuture<>();
1982 <        g = new CompletableFuture<>();
1983 <        h = f.runAfterBoth(g, r = new Noop());
1984 <        g.completeExceptionally(new CFException());
1985 <        checkIncomplete(h);
1986 <        f.complete(3);
1987 <        checkCompletedWithWrappedCFException(h);
1988 <        assertFalse(r.ran);
1989 <
1990 <        f = new CompletableFuture<>();
1991 <        g = new CompletableFuture<>();
1992 <        g.completeExceptionally(new CFException());
1993 <        f.complete(3);
1994 <        h = f.runAfterBoth(g, r = new Noop());
1995 <        checkCompletedWithWrappedCFException(h);
1996 <        assertFalse(r.ran);
1997 <
1998 <        f = new CompletableFuture<>();
1999 <        g = new CompletableFuture<>();
2000 <        f.completeExceptionally(new CFException());
2001 <        g.complete(1);
2002 <        h = f.runAfterBoth(g, r = new Noop());
2003 <        checkCompletedWithWrappedCFException(h);
2004 <        assertFalse(r.ran);
2005 <    }
1967 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1968 >        for (ExecutionMode m : ExecutionMode.values())
1969 >        for (boolean fFirst : new boolean[] { true, false })
1970 >        for (boolean failFirst : new boolean[] { true, false })
1971 >        for (Integer v1 : new Integer[] { 1, null })
1972 >    {
1973 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1974 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1975 >        final CFException ex = new CFException();
1976 >        final Noop r1 = new Noop(m);
1977 >        final Noop r2 = new Noop(m);
1978 >        final Noop r3 = new Noop(m);
1979 >
1980 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1981 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1982 >        final Callable<Boolean> complete1 = failFirst ?
1983 >            () -> fst.completeExceptionally(ex) :
1984 >            () -> fst.complete(v1);
1985 >        final Callable<Boolean> complete2 = failFirst ?
1986 >            () -> snd.complete(v1) :
1987 >            () -> snd.completeExceptionally(ex);
1988 >
1989 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1990 >        assertTrue(complete1.call());
1991 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1992 >        checkIncomplete(h1);
1993 >        checkIncomplete(h2);
1994 >        assertTrue(complete2.call());
1995 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1996 >
1997 >        checkCompletedWithWrappedException(h1, ex);
1998 >        checkCompletedWithWrappedException(h2, ex);
1999 >        checkCompletedWithWrappedException(h3, ex);
2000 >        r1.assertNotInvoked();
2001 >        r2.assertNotInvoked();
2002 >        r3.assertNotInvoked();
2003 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2004 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2005 >    }}
2006  
2007      /**
2008 <     * runAfterBoth result completes exceptionally if action does
2008 >     * runAfterBoth result completes exceptionally if either source cancelled
2009       */
2010 <    public void testRunAfterBoth3() {
2011 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2012 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2013 <        FailingNoop r = new FailingNoop();
2014 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
2015 <        f.complete(one);
2016 <        checkIncomplete(g);
2017 <        f2.complete(two);
2018 <        checkCompletedWithWrappedCFException(g);
2019 <    }
2010 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2011 >        for (ExecutionMode m : ExecutionMode.values())
2012 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2013 >        for (boolean fFirst : new boolean[] { true, false })
2014 >        for (boolean failFirst : new boolean[] { true, false })
2015 >        for (Integer v1 : new Integer[] { 1, null })
2016 >    {
2017 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2018 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2019 >        final Noop r1 = new Noop(m);
2020 >        final Noop r2 = new Noop(m);
2021 >        final Noop r3 = new Noop(m);
2022 >
2023 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2024 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2025 >        final Callable<Boolean> complete1 = failFirst ?
2026 >            () -> fst.cancel(mayInterruptIfRunning) :
2027 >            () -> fst.complete(v1);
2028 >        final Callable<Boolean> complete2 = failFirst ?
2029 >            () -> snd.complete(v1) :
2030 >            () -> snd.cancel(mayInterruptIfRunning);
2031 >
2032 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2033 >        assertTrue(complete1.call());
2034 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2035 >        checkIncomplete(h1);
2036 >        checkIncomplete(h2);
2037 >        assertTrue(complete2.call());
2038 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2039 >
2040 >        checkCompletedWithWrappedCancellationException(h1);
2041 >        checkCompletedWithWrappedCancellationException(h2);
2042 >        checkCompletedWithWrappedCancellationException(h3);
2043 >        r1.assertNotInvoked();
2044 >        r2.assertNotInvoked();
2045 >        r3.assertNotInvoked();
2046 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2047 >        checkCancelled(failFirst ? fst : snd);
2048 >    }}
2049  
2050      /**
2051 <     * runAfterBoth result completes exceptionally if either source cancelled
2051 >     * runAfterBoth result completes exceptionally if action does
2052       */
2053 <    public void testRunAfterBoth4() {
2054 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2055 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2056 <        Noop r = new Noop();
2057 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
2058 <        assertTrue(f.cancel(true));
2059 <        f2.complete(two);
2060 <        checkCompletedWithWrappedCancellationException(g);
2061 <        f = new CompletableFuture<>();
2062 <        f2 = new CompletableFuture<>();
2063 <        r = new Noop();
2064 <        g = f.runAfterBoth(f2, r);
2065 <        f.complete(one);
2066 <        assertTrue(f2.cancel(true));
2067 <        checkCompletedWithWrappedCancellationException(g);
2068 <    }
2053 >    public void testRunAfterBoth_actionFailed() {
2054 >        for (ExecutionMode m : ExecutionMode.values())
2055 >        for (boolean fFirst : new boolean[] { true, false })
2056 >        for (Integer v1 : new Integer[] { 1, null })
2057 >        for (Integer v2 : new Integer[] { 2, null })
2058 >    {
2059 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2060 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2061 >        final FailingRunnable r1 = new FailingRunnable(m);
2062 >        final FailingRunnable r2 = new FailingRunnable(m);
2063 >        final FailingRunnable r3 = new FailingRunnable(m);
2064 >
2065 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2066 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2067 >        final Integer w1 =  fFirst ? v1 : v2;
2068 >        final Integer w2 = !fFirst ? v1 : v2;
2069 >
2070 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2071 >        assertTrue(fst.complete(w1));
2072 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2073 >        assertTrue(snd.complete(w2));
2074 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2075 >
2076 >        checkCompletedWithWrappedCFException(h1);
2077 >        checkCompletedWithWrappedCFException(h2);
2078 >        checkCompletedWithWrappedCFException(h3);
2079 >        r1.assertInvoked();
2080 >        r2.assertInvoked();
2081 >        r3.assertInvoked();
2082 >        checkCompletedNormally(f, v1);
2083 >        checkCompletedNormally(g, v2);
2084 >    }}
2085  
2086      /**
2087       * applyToEither result completes normally after normal completion
2088       * of either source
2089       */
2090 <    public void testApplyToEither() {
2091 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2092 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2093 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2094 <        f.complete(one);
2095 <        checkCompletedNormally(g, two);
2096 <        f2.complete(one);
2097 <        checkCompletedNormally(g, two);
2098 <
2099 <        f = new CompletableFuture<>();
2100 <        f.complete(one);
2101 <        f2 = new CompletableFuture<>();
2102 <        g = f.applyToEither(f2, inc);
2103 <        checkCompletedNormally(g, two);
2104 <    }
2090 >    public void testApplyToEither_normalCompletion() {
2091 >        for (ExecutionMode m : ExecutionMode.values())
2092 >        for (Integer v1 : new Integer[] { 1, null })
2093 >        for (Integer v2 : new Integer[] { 2, null })
2094 >    {
2095 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2096 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2097 >        final IncFunction[] rs = new IncFunction[6];
2098 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2099 >
2100 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2101 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2102 >        checkIncomplete(h0);
2103 >        checkIncomplete(h1);
2104 >        rs[0].assertNotInvoked();
2105 >        rs[1].assertNotInvoked();
2106 >        f.complete(v1);
2107 >        checkCompletedNormally(h0, inc(v1));
2108 >        checkCompletedNormally(h1, inc(v1));
2109 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2110 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2111 >        checkCompletedNormally(h2, inc(v1));
2112 >        checkCompletedNormally(h3, inc(v1));
2113 >        g.complete(v2);
2114 >
2115 >        // unspecified behavior - both source completions available
2116 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2117 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2118 >        rs[4].assertValue(h4.join());
2119 >        rs[5].assertValue(h5.join());
2120 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
2121 >                   Objects.equals(inc(v2), h4.join()));
2122 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
2123 >                   Objects.equals(inc(v2), h5.join()));
2124 >
2125 >        checkCompletedNormally(f, v1);
2126 >        checkCompletedNormally(g, v2);
2127 >        checkCompletedNormally(h0, inc(v1));
2128 >        checkCompletedNormally(h1, inc(v1));
2129 >        checkCompletedNormally(h2, inc(v1));
2130 >        checkCompletedNormally(h3, inc(v1));
2131 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2132 >    }}
2133  
2134      /**
2135       * applyToEither result completes exceptionally after exceptional
2136       * completion of either source
2137       */
2138 <    public void testApplyToEither2() {
2139 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2140 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2141 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2142 <        f.completeExceptionally(new CFException());
2143 <        f2.complete(one);
2144 <        checkCompletedWithWrappedCFException(g);
2138 >    public void testApplyToEither_exceptionalCompletion() {
2139 >        for (ExecutionMode m : ExecutionMode.values())
2140 >        for (Integer v1 : new Integer[] { 1, null })
2141 >    {
2142 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2143 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2144 >        final CFException ex = new CFException();
2145 >        final IncFunction[] rs = new IncFunction[6];
2146 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2147 >
2148 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2149 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2150 >        checkIncomplete(h0);
2151 >        checkIncomplete(h1);
2152 >        rs[0].assertNotInvoked();
2153 >        rs[1].assertNotInvoked();
2154 >        f.completeExceptionally(ex);
2155 >        checkCompletedWithWrappedException(h0, ex);
2156 >        checkCompletedWithWrappedException(h1, ex);
2157 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2158 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2159 >        checkCompletedWithWrappedException(h2, ex);
2160 >        checkCompletedWithWrappedException(h3, ex);
2161 >        g.complete(v1);
2162 >
2163 >        // unspecified behavior - both source completions available
2164 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2165 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2166 >        try {
2167 >            assertEquals(inc(v1), h4.join());
2168 >            rs[4].assertValue(inc(v1));
2169 >        } catch (CompletionException ok) {
2170 >            checkCompletedWithWrappedException(h4, ex);
2171 >            rs[4].assertNotInvoked();
2172 >        }
2173 >        try {
2174 >            assertEquals(inc(v1), h5.join());
2175 >            rs[5].assertValue(inc(v1));
2176 >        } catch (CompletionException ok) {
2177 >            checkCompletedWithWrappedException(h5, ex);
2178 >            rs[5].assertNotInvoked();
2179 >        }
2180  
2181 <        f = new CompletableFuture<>();
2182 <        f2 = new CompletableFuture<>();
2183 <        f2.completeExceptionally(new CFException());
2184 <        g = f.applyToEither(f2, inc);
2185 <        checkCompletedWithWrappedCFException(g);
2186 <    }
2181 >        checkCompletedExceptionally(f, ex);
2182 >        checkCompletedNormally(g, v1);
2183 >        checkCompletedWithWrappedException(h0, ex);
2184 >        checkCompletedWithWrappedException(h1, ex);
2185 >        checkCompletedWithWrappedException(h2, ex);
2186 >        checkCompletedWithWrappedException(h3, ex);
2187 >        checkCompletedWithWrappedException(h4, ex);
2188 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2189 >    }}
2190 >
2191 >    public void testApplyToEither_exceptionalCompletion2() {
2192 >        for (ExecutionMode m : ExecutionMode.values())
2193 >        for (boolean fFirst : new boolean[] { true, false })
2194 >        for (Integer v1 : new Integer[] { 1, null })
2195 >    {
2196 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2197 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2198 >        final CFException ex = new CFException();
2199 >        final IncFunction[] rs = new IncFunction[6];
2200 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2201 >
2202 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2203 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2204 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2205 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2206 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2207 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2208  
2209 <    /**
2210 <     * applyToEither result completes exceptionally if action does
2211 <     */
2212 <    public void testApplyToEither3() {
2213 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2214 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2215 <        FailingFunction r = new FailingFunction();
2216 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2217 <        f2.complete(two);
2218 <        checkCompletedWithWrappedCFException(g);
2219 <    }
2209 >        // unspecified behavior - both source completions available
2210 >        try {
2211 >            assertEquals(inc(v1), h0.join());
2212 >            rs[0].assertValue(inc(v1));
2213 >        } catch (CompletionException ok) {
2214 >            checkCompletedWithWrappedException(h0, ex);
2215 >            rs[0].assertNotInvoked();
2216 >        }
2217 >        try {
2218 >            assertEquals(inc(v1), h1.join());
2219 >            rs[1].assertValue(inc(v1));
2220 >        } catch (CompletionException ok) {
2221 >            checkCompletedWithWrappedException(h1, ex);
2222 >            rs[1].assertNotInvoked();
2223 >        }
2224 >        try {
2225 >            assertEquals(inc(v1), h2.join());
2226 >            rs[2].assertValue(inc(v1));
2227 >        } catch (CompletionException ok) {
2228 >            checkCompletedWithWrappedException(h2, ex);
2229 >            rs[2].assertNotInvoked();
2230 >        }
2231 >        try {
2232 >            assertEquals(inc(v1), h3.join());
2233 >            rs[3].assertValue(inc(v1));
2234 >        } catch (CompletionException ok) {
2235 >            checkCompletedWithWrappedException(h3, ex);
2236 >            rs[3].assertNotInvoked();
2237 >        }
2238  
2239 <    /**
2240 <     * applyToEither result completes exceptionally if either source cancelled
2241 <     */
1086 <    public void testApplyToEither4() {
1087 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1088 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1089 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1090 <        assertTrue(f.cancel(true));
1091 <        checkCompletedWithWrappedCancellationException(g);
1092 <        f = new CompletableFuture<>();
1093 <        f2 = new CompletableFuture<>();
1094 <        assertTrue(f2.cancel(true));
1095 <        checkCompletedWithWrappedCancellationException(g);
1096 <    }
2239 >        checkCompletedNormally(f, v1);
2240 >        checkCompletedExceptionally(g, ex);
2241 >    }}
2242  
2243      /**
2244 <     * acceptEither result completes normally after normal completion
1100 <     * of either source
2244 >     * applyToEither result completes exceptionally if either source cancelled
2245       */
2246 <    public void testAcceptEither() {
2247 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2248 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2249 <        IncAction r = new IncAction();
2250 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2251 <        f.complete(one);
2252 <        checkCompletedNormally(g, null);
2253 <        f2.complete(one);
2254 <        checkCompletedNormally(g, null);
2255 <        assertEquals(r.value, 2);
2256 <
2257 <        r = new IncAction();
2258 <        f = new CompletableFuture<>();
2259 <        f.complete(one);
2260 <        f2 = new CompletableFuture<>();
2261 <        g = f.acceptEither(f2, r);
2262 <        checkCompletedNormally(g, null);
2263 <        assertEquals(r.value, 2);
2264 <    }
2246 >    public void testApplyToEither_sourceCancelled() {
2247 >        for (ExecutionMode m : ExecutionMode.values())
2248 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2249 >        for (Integer v1 : new Integer[] { 1, null })
2250 >    {
2251 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2252 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2253 >        final IncFunction[] rs = new IncFunction[6];
2254 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2255 >
2256 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2257 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2258 >        checkIncomplete(h0);
2259 >        checkIncomplete(h1);
2260 >        rs[0].assertNotInvoked();
2261 >        rs[1].assertNotInvoked();
2262 >        f.cancel(mayInterruptIfRunning);
2263 >        checkCompletedWithWrappedCancellationException(h0);
2264 >        checkCompletedWithWrappedCancellationException(h1);
2265 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2266 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2267 >        checkCompletedWithWrappedCancellationException(h2);
2268 >        checkCompletedWithWrappedCancellationException(h3);
2269 >        g.complete(v1);
2270 >
2271 >        // unspecified behavior - both source completions available
2272 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2273 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2274 >        try {
2275 >            assertEquals(inc(v1), h4.join());
2276 >            rs[4].assertValue(inc(v1));
2277 >        } catch (CompletionException ok) {
2278 >            checkCompletedWithWrappedCancellationException(h4);
2279 >            rs[4].assertNotInvoked();
2280 >        }
2281 >        try {
2282 >            assertEquals(inc(v1), h5.join());
2283 >            rs[5].assertValue(inc(v1));
2284 >        } catch (CompletionException ok) {
2285 >            checkCompletedWithWrappedCancellationException(h5);
2286 >            rs[5].assertNotInvoked();
2287 >        }
2288  
2289 <    /**
2290 <     * acceptEither result completes exceptionally after exceptional
2291 <     * completion of either source
2292 <     */
2293 <    public void testAcceptEither2() {
2294 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2295 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2296 <        IncAction r = new IncAction();
2297 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2298 <        f.completeExceptionally(new CFException());
2299 <        f2.complete(one);
2300 <        checkCompletedWithWrappedCFException(g);
2289 >        checkCancelled(f);
2290 >        checkCompletedNormally(g, v1);
2291 >        checkCompletedWithWrappedCancellationException(h0);
2292 >        checkCompletedWithWrappedCancellationException(h1);
2293 >        checkCompletedWithWrappedCancellationException(h2);
2294 >        checkCompletedWithWrappedCancellationException(h3);
2295 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2296 >    }}
2297 >
2298 >    public void testApplyToEither_sourceCancelled2() {
2299 >        for (ExecutionMode m : ExecutionMode.values())
2300 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2301 >        for (boolean fFirst : new boolean[] { true, false })
2302 >        for (Integer v1 : new Integer[] { 1, null })
2303 >    {
2304 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2305 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2306 >        final IncFunction[] rs = new IncFunction[6];
2307 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2308 >
2309 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2310 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2311 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2312 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2313 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2314 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2315  
2316 <        r = new IncAction();
2317 <        f = new CompletableFuture<>();
2318 <        f2 = new CompletableFuture<>();
2319 <        f2.completeExceptionally(new CFException());
2320 <        g = f.acceptEither(f2, r);
2321 <        checkCompletedWithWrappedCFException(g);
2322 <    }
2316 >        // unspecified behavior - both source completions available
2317 >        try {
2318 >            assertEquals(inc(v1), h0.join());
2319 >            rs[0].assertValue(inc(v1));
2320 >        } catch (CompletionException ok) {
2321 >            checkCompletedWithWrappedCancellationException(h0);
2322 >            rs[0].assertNotInvoked();
2323 >        }
2324 >        try {
2325 >            assertEquals(inc(v1), h1.join());
2326 >            rs[1].assertValue(inc(v1));
2327 >        } catch (CompletionException ok) {
2328 >            checkCompletedWithWrappedCancellationException(h1);
2329 >            rs[1].assertNotInvoked();
2330 >        }
2331 >        try {
2332 >            assertEquals(inc(v1), h2.join());
2333 >            rs[2].assertValue(inc(v1));
2334 >        } catch (CompletionException ok) {
2335 >            checkCompletedWithWrappedCancellationException(h2);
2336 >            rs[2].assertNotInvoked();
2337 >        }
2338 >        try {
2339 >            assertEquals(inc(v1), h3.join());
2340 >            rs[3].assertValue(inc(v1));
2341 >        } catch (CompletionException ok) {
2342 >            checkCompletedWithWrappedCancellationException(h3);
2343 >            rs[3].assertNotInvoked();
2344 >        }
2345  
2346 <    /**
2347 <     * acceptEither result completes exceptionally if action does
2348 <     */
1146 <    public void testAcceptEither3() {
1147 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1148 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1149 <        FailingConsumer r = new FailingConsumer();
1150 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1151 <        f2.complete(two);
1152 <        checkCompletedWithWrappedCFException(g);
1153 <    }
2346 >        checkCompletedNormally(f, v1);
2347 >        checkCancelled(g);
2348 >    }}
2349  
2350      /**
2351 <     * acceptEither result completes exceptionally if either source cancelled
2351 >     * applyToEither result completes exceptionally if action does
2352       */
2353 <    public void testAcceptEither4() {
2354 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2355 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2356 <        IncAction r = new IncAction();
2357 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2358 <        assertTrue(f.cancel(true));
2359 <        checkCompletedWithWrappedCancellationException(g);
2360 <        f = new CompletableFuture<>();
2361 <        f2 = new CompletableFuture<>();
2362 <        assertTrue(f2.cancel(true));
2363 <        checkCompletedWithWrappedCancellationException(g);
2364 <    }
2353 >    public void testApplyToEither_actionFailed() {
2354 >        for (ExecutionMode m : ExecutionMode.values())
2355 >        for (Integer v1 : new Integer[] { 1, null })
2356 >        for (Integer v2 : new Integer[] { 2, null })
2357 >    {
2358 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2359 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2360 >        final FailingFunction[] rs = new FailingFunction[6];
2361 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2362 >
2363 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2364 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2365 >        f.complete(v1);
2366 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2367 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2368 >        checkCompletedWithWrappedCFException(h0);
2369 >        checkCompletedWithWrappedCFException(h1);
2370 >        checkCompletedWithWrappedCFException(h2);
2371 >        checkCompletedWithWrappedCFException(h3);
2372 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2373 >
2374 >        g.complete(v2);
2375 >
2376 >        // unspecified behavior - both source completions available
2377 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2378 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2379 >
2380 >        checkCompletedWithWrappedCFException(h4);
2381 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2382 >                   Objects.equals(v2, rs[4].value));
2383 >        checkCompletedWithWrappedCFException(h5);
2384 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2385 >                   Objects.equals(v2, rs[5].value));
2386 >
2387 >        checkCompletedNormally(f, v1);
2388 >        checkCompletedNormally(g, v2);
2389 >    }}
2390  
2391      /**
2392 <     * runAfterEither result completes normally after normal completion
2392 >     * acceptEither result completes normally after normal completion
2393       * of either source
2394       */
2395 <    public void testRunAfterEither() {
2396 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2397 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2398 <        Noop r = new Noop();
2399 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2400 <        f.complete(one);
2401 <        checkCompletedNormally(g, null);
2402 <        f2.complete(one);
2403 <        checkCompletedNormally(g, null);
2404 <        assertTrue(r.ran);
2405 <
2406 <        r = new Noop();
2407 <        f = new CompletableFuture<>();
2408 <        f.complete(one);
2409 <        f2 = new CompletableFuture<>();
2410 <        g = f.runAfterEither(f2, r);
2411 <        checkCompletedNormally(g, null);
2412 <        assertTrue(r.ran);
2413 <    }
2395 >    public void testAcceptEither_normalCompletion() {
2396 >        for (ExecutionMode m : ExecutionMode.values())
2397 >        for (Integer v1 : new Integer[] { 1, null })
2398 >        for (Integer v2 : new Integer[] { 2, null })
2399 >    {
2400 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2401 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2402 >        final NoopConsumer[] rs = new NoopConsumer[6];
2403 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2404 >
2405 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2406 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2407 >        checkIncomplete(h0);
2408 >        checkIncomplete(h1);
2409 >        rs[0].assertNotInvoked();
2410 >        rs[1].assertNotInvoked();
2411 >        f.complete(v1);
2412 >        checkCompletedNormally(h0, null);
2413 >        checkCompletedNormally(h1, null);
2414 >        rs[0].assertValue(v1);
2415 >        rs[1].assertValue(v1);
2416 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2417 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2418 >        checkCompletedNormally(h2, null);
2419 >        checkCompletedNormally(h3, null);
2420 >        rs[2].assertValue(v1);
2421 >        rs[3].assertValue(v1);
2422 >        g.complete(v2);
2423 >
2424 >        // unspecified behavior - both source completions available
2425 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2426 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2427 >        checkCompletedNormally(h4, null);
2428 >        checkCompletedNormally(h5, null);
2429 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2430 >                   Objects.equals(v2, rs[4].value));
2431 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2432 >                   Objects.equals(v2, rs[5].value));
2433 >
2434 >        checkCompletedNormally(f, v1);
2435 >        checkCompletedNormally(g, v2);
2436 >        checkCompletedNormally(h0, null);
2437 >        checkCompletedNormally(h1, null);
2438 >        checkCompletedNormally(h2, null);
2439 >        checkCompletedNormally(h3, null);
2440 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2441 >    }}
2442  
2443      /**
2444 <     * runAfterEither result completes exceptionally after exceptional
2444 >     * acceptEither result completes exceptionally after exceptional
2445       * completion of either source
2446       */
2447 <    public void testRunAfterEither2() {
2448 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2449 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2450 <        Noop r = new Noop();
2451 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2452 <        f.completeExceptionally(new CFException());
2453 <        f2.complete(one);
2454 <        checkCompletedWithWrappedCFException(g);
2447 >    public void testAcceptEither_exceptionalCompletion() {
2448 >        for (ExecutionMode m : ExecutionMode.values())
2449 >        for (Integer v1 : new Integer[] { 1, null })
2450 >    {
2451 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2452 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2453 >        final CFException ex = new CFException();
2454 >        final NoopConsumer[] rs = new NoopConsumer[6];
2455 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2456 >
2457 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2458 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2459 >        checkIncomplete(h0);
2460 >        checkIncomplete(h1);
2461 >        rs[0].assertNotInvoked();
2462 >        rs[1].assertNotInvoked();
2463 >        f.completeExceptionally(ex);
2464 >        checkCompletedWithWrappedException(h0, ex);
2465 >        checkCompletedWithWrappedException(h1, ex);
2466 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2467 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2468 >        checkCompletedWithWrappedException(h2, ex);
2469 >        checkCompletedWithWrappedException(h3, ex);
2470 >
2471 >        g.complete(v1);
2472 >
2473 >        // unspecified behavior - both source completions available
2474 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2475 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2476 >        try {
2477 >            assertNull(h4.join());
2478 >            rs[4].assertValue(v1);
2479 >        } catch (CompletionException ok) {
2480 >            checkCompletedWithWrappedException(h4, ex);
2481 >            rs[4].assertNotInvoked();
2482 >        }
2483 >        try {
2484 >            assertNull(h5.join());
2485 >            rs[5].assertValue(v1);
2486 >        } catch (CompletionException ok) {
2487 >            checkCompletedWithWrappedException(h5, ex);
2488 >            rs[5].assertNotInvoked();
2489 >        }
2490  
2491 <        r = new Noop();
2492 <        f = new CompletableFuture<>();
2493 <        f2 = new CompletableFuture<>();
2494 <        f2.completeExceptionally(new CFException());
2495 <        g = f.runAfterEither(f2, r);
2496 <        checkCompletedWithWrappedCFException(g);
2497 <    }
2491 >        checkCompletedExceptionally(f, ex);
2492 >        checkCompletedNormally(g, v1);
2493 >        checkCompletedWithWrappedException(h0, ex);
2494 >        checkCompletedWithWrappedException(h1, ex);
2495 >        checkCompletedWithWrappedException(h2, ex);
2496 >        checkCompletedWithWrappedException(h3, ex);
2497 >        checkCompletedWithWrappedException(h4, ex);
2498 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2499 >    }}
2500 >
2501 >    public void testAcceptEither_exceptionalCompletion2() {
2502 >        for (ExecutionMode m : ExecutionMode.values())
2503 >        for (boolean fFirst : new boolean[] { true, false })
2504 >        for (Integer v1 : new Integer[] { 1, null })
2505 >    {
2506 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2507 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2508 >        final CFException ex = new CFException();
2509 >        final NoopConsumer[] rs = new NoopConsumer[6];
2510 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2511 >
2512 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2513 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2514 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2515 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2516 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2517 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2518  
2519 <    /**
2520 <     * runAfterEither result completes exceptionally if action does
2521 <     */
2522 <    public void testRunAfterEither3() {
2523 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2524 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2525 <        FailingNoop r = new FailingNoop();
2526 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2527 <        f2.complete(two);
2528 <        checkCompletedWithWrappedCFException(g);
2529 <    }
2519 >        // unspecified behavior - both source completions available
2520 >        try {
2521 >            assertEquals(null, h0.join());
2522 >            rs[0].assertValue(v1);
2523 >        } catch (CompletionException ok) {
2524 >            checkCompletedWithWrappedException(h0, ex);
2525 >            rs[0].assertNotInvoked();
2526 >        }
2527 >        try {
2528 >            assertEquals(null, h1.join());
2529 >            rs[1].assertValue(v1);
2530 >        } catch (CompletionException ok) {
2531 >            checkCompletedWithWrappedException(h1, ex);
2532 >            rs[1].assertNotInvoked();
2533 >        }
2534 >        try {
2535 >            assertEquals(null, h2.join());
2536 >            rs[2].assertValue(v1);
2537 >        } catch (CompletionException ok) {
2538 >            checkCompletedWithWrappedException(h2, ex);
2539 >            rs[2].assertNotInvoked();
2540 >        }
2541 >        try {
2542 >            assertEquals(null, h3.join());
2543 >            rs[3].assertValue(v1);
2544 >        } catch (CompletionException ok) {
2545 >            checkCompletedWithWrappedException(h3, ex);
2546 >            rs[3].assertNotInvoked();
2547 >        }
2548  
2549 <    /**
2550 <     * runAfterEither result completes exceptionally if either source cancelled
2551 <     */
1231 <    public void testRunAfterEither4() {
1232 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1233 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1234 <        Noop r = new Noop();
1235 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1236 <        assertTrue(f.cancel(true));
1237 <        checkCompletedWithWrappedCancellationException(g);
1238 <        f = new CompletableFuture<>();
1239 <        f2 = new CompletableFuture<>();
1240 <        assertTrue(f2.cancel(true));
1241 <        checkCompletedWithWrappedCancellationException(g);
1242 <    }
2549 >        checkCompletedNormally(f, v1);
2550 >        checkCompletedExceptionally(g, ex);
2551 >    }}
2552  
2553      /**
2554 <     * thenCompose result completes normally after normal completion of source
2554 >     * acceptEither result completes exceptionally if either source cancelled
2555       */
2556 <    public void testThenCompose() {
2557 <        CompletableFuture<Integer> f, g;
2558 <        CompletableFutureInc r;
2559 <
2560 <        f = new CompletableFuture<>();
2561 <        g = f.thenCompose(r = new CompletableFutureInc());
2562 <        f.complete(one);
2563 <        checkCompletedNormally(g, two);
2564 <        assertTrue(r.ran);
2556 >    public void testAcceptEither_sourceCancelled() {
2557 >        for (ExecutionMode m : ExecutionMode.values())
2558 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2559 >        for (Integer v1 : new Integer[] { 1, null })
2560 >    {
2561 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2562 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2563 >        final NoopConsumer[] rs = new NoopConsumer[6];
2564 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2565 >
2566 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2567 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2568 >        checkIncomplete(h0);
2569 >        checkIncomplete(h1);
2570 >        rs[0].assertNotInvoked();
2571 >        rs[1].assertNotInvoked();
2572 >        f.cancel(mayInterruptIfRunning);
2573 >        checkCompletedWithWrappedCancellationException(h0);
2574 >        checkCompletedWithWrappedCancellationException(h1);
2575 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2576 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2577 >        checkCompletedWithWrappedCancellationException(h2);
2578 >        checkCompletedWithWrappedCancellationException(h3);
2579 >
2580 >        g.complete(v1);
2581 >
2582 >        // unspecified behavior - both source completions available
2583 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2584 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2585 >        try {
2586 >            assertNull(h4.join());
2587 >            rs[4].assertValue(v1);
2588 >        } catch (CompletionException ok) {
2589 >            checkCompletedWithWrappedCancellationException(h4);
2590 >            rs[4].assertNotInvoked();
2591 >        }
2592 >        try {
2593 >            assertNull(h5.join());
2594 >            rs[5].assertValue(v1);
2595 >        } catch (CompletionException ok) {
2596 >            checkCompletedWithWrappedCancellationException(h5);
2597 >            rs[5].assertNotInvoked();
2598 >        }
2599  
2600 <        f = new CompletableFuture<>();
2601 <        f.complete(one);
2602 <        g = f.thenCompose(r = new CompletableFutureInc());
2603 <        checkCompletedNormally(g, two);
2604 <        assertTrue(r.ran);
2605 <    }
2600 >        checkCancelled(f);
2601 >        checkCompletedNormally(g, v1);
2602 >        checkCompletedWithWrappedCancellationException(h0);
2603 >        checkCompletedWithWrappedCancellationException(h1);
2604 >        checkCompletedWithWrappedCancellationException(h2);
2605 >        checkCompletedWithWrappedCancellationException(h3);
2606 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2607 >    }}
2608  
2609      /**
2610 <     * thenCompose result completes exceptionally after exceptional
1266 <     * completion of source
2610 >     * acceptEither result completes exceptionally if action does
2611       */
2612 <    public void testThenCompose2() {
2613 <        CompletableFuture<Integer> f, g;
2614 <        CompletableFutureInc r;
2615 <
2616 <        f = new CompletableFuture<>();
2617 <        g = f.thenCompose(r = new CompletableFutureInc());
2618 <        f.completeExceptionally(new CFException());
2619 <        checkCompletedWithWrappedCFException(g);
2620 <
2621 <        f = new CompletableFuture<>();
2622 <        f.completeExceptionally(new CFException());
2623 <        g = f.thenCompose(r = new CompletableFutureInc());
2624 <        checkCompletedWithWrappedCFException(g);
2625 <    }
2612 >    public void testAcceptEither_actionFailed() {
2613 >        for (ExecutionMode m : ExecutionMode.values())
2614 >        for (Integer v1 : new Integer[] { 1, null })
2615 >        for (Integer v2 : new Integer[] { 2, null })
2616 >    {
2617 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2618 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2619 >        final FailingConsumer[] rs = new FailingConsumer[6];
2620 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2621 >
2622 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2623 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2624 >        f.complete(v1);
2625 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2626 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2627 >        checkCompletedWithWrappedCFException(h0);
2628 >        checkCompletedWithWrappedCFException(h1);
2629 >        checkCompletedWithWrappedCFException(h2);
2630 >        checkCompletedWithWrappedCFException(h3);
2631 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2632 >
2633 >        g.complete(v2);
2634 >
2635 >        // unspecified behavior - both source completions available
2636 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2637 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2638 >
2639 >        checkCompletedWithWrappedCFException(h4);
2640 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2641 >                   Objects.equals(v2, rs[4].value));
2642 >        checkCompletedWithWrappedCFException(h5);
2643 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2644 >                   Objects.equals(v2, rs[5].value));
2645 >
2646 >        checkCompletedNormally(f, v1);
2647 >        checkCompletedNormally(g, v2);
2648 >    }}
2649  
2650      /**
2651 <     * thenCompose result completes exceptionally if action does
2651 >     * runAfterEither result completes normally after normal completion
2652 >     * of either source
2653       */
2654 <    public void testThenCompose3() {
2655 <        CompletableFuture<Integer> f, g;
2656 <        FailingCompletableFutureFunction r;
2657 <
2658 <        f = new CompletableFuture<>();
2659 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2660 <        f.complete(one);
2661 <        checkCompletedWithWrappedCFException(g);
2662 <
2663 <        f = new CompletableFuture<>();
2664 <        f.complete(one);
2665 <        g = f.thenCompose(r = new FailingCompletableFutureFunction());
2666 <        checkCompletedWithWrappedCFException(g);
2667 <    }
2654 >    public void testRunAfterEither_normalCompletion() {
2655 >        for (ExecutionMode m : ExecutionMode.values())
2656 >        for (Integer v1 : new Integer[] { 1, null })
2657 >        for (Integer v2 : new Integer[] { 2, null })
2658 >    {
2659 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2660 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2661 >        final Noop[] rs = new Noop[6];
2662 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2663 >
2664 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2665 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2666 >        checkIncomplete(h0);
2667 >        checkIncomplete(h1);
2668 >        rs[0].assertNotInvoked();
2669 >        rs[1].assertNotInvoked();
2670 >        f.complete(v1);
2671 >        checkCompletedNormally(h0, null);
2672 >        checkCompletedNormally(h1, null);
2673 >        rs[0].assertInvoked();
2674 >        rs[1].assertInvoked();
2675 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2676 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2677 >        checkCompletedNormally(h2, null);
2678 >        checkCompletedNormally(h3, null);
2679 >        rs[2].assertInvoked();
2680 >        rs[3].assertInvoked();
2681 >
2682 >        g.complete(v2);
2683 >
2684 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2685 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2686 >
2687 >        checkCompletedNormally(f, v1);
2688 >        checkCompletedNormally(g, v2);
2689 >        checkCompletedNormally(h0, null);
2690 >        checkCompletedNormally(h1, null);
2691 >        checkCompletedNormally(h2, null);
2692 >        checkCompletedNormally(h3, null);
2693 >        checkCompletedNormally(h4, null);
2694 >        checkCompletedNormally(h5, null);
2695 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2696 >    }}
2697  
2698      /**
2699 <     * thenCompose result completes exceptionally if source cancelled
2699 >     * runAfterEither result completes exceptionally after exceptional
2700 >     * completion of either source
2701       */
2702 <    public void testThenCompose4() {
2703 <        CompletableFuture<Integer> f, g;
2704 <        CompletableFutureInc r;
2705 <
2706 <        f = new CompletableFuture<>();
2707 <        g = f.thenCompose(r = new CompletableFutureInc());
2708 <        assertTrue(f.cancel(true));
2709 <        checkCompletedWithWrappedCancellationException(g);
2710 <
2711 <        f = new CompletableFuture<>();
2712 <        assertTrue(f.cancel(true));
2713 <        g = f.thenCompose(r = new CompletableFutureInc());
2714 <        checkCompletedWithWrappedCancellationException(g);
2715 <    }
2702 >    public void testRunAfterEither_exceptionalCompletion() {
2703 >        for (ExecutionMode m : ExecutionMode.values())
2704 >        for (Integer v1 : new Integer[] { 1, null })
2705 >    {
2706 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2707 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2708 >        final CFException ex = new CFException();
2709 >        final Noop[] rs = new Noop[6];
2710 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2711 >
2712 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2713 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2714 >        checkIncomplete(h0);
2715 >        checkIncomplete(h1);
2716 >        rs[0].assertNotInvoked();
2717 >        rs[1].assertNotInvoked();
2718 >        assertTrue(f.completeExceptionally(ex));
2719 >        checkCompletedWithWrappedException(h0, ex);
2720 >        checkCompletedWithWrappedException(h1, ex);
2721 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2722 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2723 >        checkCompletedWithWrappedException(h2, ex);
2724 >        checkCompletedWithWrappedException(h3, ex);
2725 >
2726 >        assertTrue(g.complete(v1));
2727 >
2728 >        // unspecified behavior - both source completions available
2729 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2730 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2731 >        try {
2732 >            assertNull(h4.join());
2733 >            rs[4].assertInvoked();
2734 >        } catch (CompletionException ok) {
2735 >            checkCompletedWithWrappedException(h4, ex);
2736 >            rs[4].assertNotInvoked();
2737 >        }
2738 >        try {
2739 >            assertNull(h5.join());
2740 >            rs[5].assertInvoked();
2741 >        } catch (CompletionException ok) {
2742 >            checkCompletedWithWrappedException(h5, ex);
2743 >            rs[5].assertNotInvoked();
2744 >        }
2745  
2746 <    // asyncs
2746 >        checkCompletedExceptionally(f, ex);
2747 >        checkCompletedNormally(g, v1);
2748 >        checkCompletedWithWrappedException(h0, ex);
2749 >        checkCompletedWithWrappedException(h1, ex);
2750 >        checkCompletedWithWrappedException(h2, ex);
2751 >        checkCompletedWithWrappedException(h3, ex);
2752 >        checkCompletedWithWrappedException(h4, ex);
2753 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2754 >    }}
2755 >
2756 >    public void testRunAfterEither_exceptionalCompletion2() {
2757 >        for (ExecutionMode m : ExecutionMode.values())
2758 >        for (boolean fFirst : new boolean[] { true, false })
2759 >        for (Integer v1 : new Integer[] { 1, null })
2760 >    {
2761 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2762 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2763 >        final CFException ex = new CFException();
2764 >        final Noop[] rs = new Noop[6];
2765 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2766 >
2767 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2768 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2769 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2770 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2771 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2772 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2773  
2774 <    /**
2775 <     * thenRunAsync result completes normally after normal completion of source
2776 <     */
2777 <    public void testThenRunAsync() {
2778 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2779 <        Noop r = new Noop();
2780 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2781 <        f.complete(null);
2782 <        checkCompletedNormally(g, null);
2774 >        // unspecified behavior - both source completions available
2775 >        try {
2776 >            assertEquals(null, h0.join());
2777 >            rs[0].assertInvoked();
2778 >        } catch (CompletionException ok) {
2779 >            checkCompletedWithWrappedException(h0, ex);
2780 >            rs[0].assertNotInvoked();
2781 >        }
2782 >        try {
2783 >            assertEquals(null, h1.join());
2784 >            rs[1].assertInvoked();
2785 >        } catch (CompletionException ok) {
2786 >            checkCompletedWithWrappedException(h1, ex);
2787 >            rs[1].assertNotInvoked();
2788 >        }
2789 >        try {
2790 >            assertEquals(null, h2.join());
2791 >            rs[2].assertInvoked();
2792 >        } catch (CompletionException ok) {
2793 >            checkCompletedWithWrappedException(h2, ex);
2794 >            rs[2].assertNotInvoked();
2795 >        }
2796 >        try {
2797 >            assertEquals(null, h3.join());
2798 >            rs[3].assertInvoked();
2799 >        } catch (CompletionException ok) {
2800 >            checkCompletedWithWrappedException(h3, ex);
2801 >            rs[3].assertNotInvoked();
2802 >        }
2803  
2804 <        // reordered version
2805 <        f = new CompletableFuture<>();
2806 <        f.complete(null);
1334 <        r = new Noop();
1335 <        g = f.thenRunAsync(r);
1336 <        checkCompletedNormally(g, null);
1337 <    }
2804 >        checkCompletedNormally(f, v1);
2805 >        checkCompletedExceptionally(g, ex);
2806 >    }}
2807  
2808      /**
2809 <     * thenRunAsync result completes exceptionally after exceptional
1341 <     * completion of source
2809 >     * runAfterEither result completes exceptionally if either source cancelled
2810       */
2811 <    public void testThenRunAsync2() {
2812 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2813 <        Noop r = new Noop();
2814 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2815 <        f.completeExceptionally(new CFException());
2811 >    public void testRunAfterEither_sourceCancelled() {
2812 >        for (ExecutionMode m : ExecutionMode.values())
2813 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2814 >        for (Integer v1 : new Integer[] { 1, null })
2815 >    {
2816 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2817 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2818 >        final Noop[] rs = new Noop[6];
2819 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2820 >
2821 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2822 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2823 >        checkIncomplete(h0);
2824 >        checkIncomplete(h1);
2825 >        rs[0].assertNotInvoked();
2826 >        rs[1].assertNotInvoked();
2827 >        f.cancel(mayInterruptIfRunning);
2828 >        checkCompletedWithWrappedCancellationException(h0);
2829 >        checkCompletedWithWrappedCancellationException(h1);
2830 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2831 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2832 >        checkCompletedWithWrappedCancellationException(h2);
2833 >        checkCompletedWithWrappedCancellationException(h3);
2834 >
2835 >        assertTrue(g.complete(v1));
2836 >
2837 >        // unspecified behavior - both source completions available
2838 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2839 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2840          try {
2841 <            g.join();
2842 <            shouldThrow();
2843 <        } catch (Exception ok) {
2841 >            assertNull(h4.join());
2842 >            rs[4].assertInvoked();
2843 >        } catch (CompletionException ok) {
2844 >            checkCompletedWithWrappedCancellationException(h4);
2845 >            rs[4].assertNotInvoked();
2846 >        }
2847 >        try {
2848 >            assertNull(h5.join());
2849 >            rs[5].assertInvoked();
2850 >        } catch (CompletionException ok) {
2851 >            checkCompletedWithWrappedCancellationException(h5);
2852 >            rs[5].assertNotInvoked();
2853          }
1353        checkCompletedWithWrappedCFException(g);
1354    }
2854  
2855 <    /**
2856 <     * thenRunAsync result completes exceptionally if action does
2857 <     */
2858 <    public void testThenRunAsync3() {
2859 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2860 <        FailingNoop r = new FailingNoop();
2861 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2862 <        f.complete(null);
1364 <        checkCompletedWithWrappedCFException(g);
1365 <    }
2855 >        checkCancelled(f);
2856 >        checkCompletedNormally(g, v1);
2857 >        checkCompletedWithWrappedCancellationException(h0);
2858 >        checkCompletedWithWrappedCancellationException(h1);
2859 >        checkCompletedWithWrappedCancellationException(h2);
2860 >        checkCompletedWithWrappedCancellationException(h3);
2861 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2862 >    }}
2863  
2864      /**
2865 <     * thenRunAsync result completes exceptionally if source cancelled
2865 >     * runAfterEither result completes exceptionally if action does
2866       */
2867 <    public void testThenRunAsync4() {
2868 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2869 <        Noop r = new Noop();
2870 <        CompletableFuture<Void> g = f.thenRunAsync(r);
2871 <        assertTrue(f.cancel(true));
2872 <        checkCompletedWithWrappedCancellationException(g);
2873 <    }
2867 >    public void testRunAfterEither_actionFailed() {
2868 >        for (ExecutionMode m : ExecutionMode.values())
2869 >        for (Integer v1 : new Integer[] { 1, null })
2870 >        for (Integer v2 : new Integer[] { 2, null })
2871 >    {
2872 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2873 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2874 >        final FailingRunnable[] rs = new FailingRunnable[6];
2875 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2876 >
2877 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2878 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2879 >        assertTrue(f.complete(v1));
2880 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2881 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2882 >        checkCompletedWithWrappedCFException(h0);
2883 >        checkCompletedWithWrappedCFException(h1);
2884 >        checkCompletedWithWrappedCFException(h2);
2885 >        checkCompletedWithWrappedCFException(h3);
2886 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2887 >        assertTrue(g.complete(v2));
2888 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2889 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2890 >        checkCompletedWithWrappedCFException(h4);
2891 >        checkCompletedWithWrappedCFException(h5);
2892 >
2893 >        checkCompletedNormally(f, v1);
2894 >        checkCompletedNormally(g, v2);
2895 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2896 >    }}
2897  
2898      /**
2899 <     * thenApplyAsync result completes normally after normal completion of source
2899 >     * thenCompose result completes normally after normal completion of source
2900       */
2901 <    public void testThenApplyAsync() {
2902 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2903 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2904 <        f.complete(one);
2905 <        checkCompletedNormally(g, two);
2906 <    }
2901 >    public void testThenCompose_normalCompletion() {
2902 >        for (ExecutionMode m : ExecutionMode.values())
2903 >        for (boolean createIncomplete : new boolean[] { true, false })
2904 >        for (Integer v1 : new Integer[] { 1, null })
2905 >    {
2906 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2907 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2908 >        if (!createIncomplete) assertTrue(f.complete(v1));
2909 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2910 >        if (createIncomplete) assertTrue(f.complete(v1));
2911 >
2912 >        checkCompletedNormally(g, inc(v1));
2913 >        checkCompletedNormally(f, v1);
2914 >        r.assertValue(v1);
2915 >    }}
2916  
2917      /**
2918 <     * thenApplyAsync result completes exceptionally after exceptional
2918 >     * thenCompose result completes exceptionally after exceptional
2919       * completion of source
2920       */
2921 <    public void testThenApplyAsync2() {
2922 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2923 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
2924 <        f.completeExceptionally(new CFException());
2925 <        checkCompletedWithWrappedCFException(g);
2926 <    }
2921 >    public void testThenCompose_exceptionalCompletion() {
2922 >        for (ExecutionMode m : ExecutionMode.values())
2923 >        for (boolean createIncomplete : new boolean[] { true, false })
2924 >    {
2925 >        final CFException ex = new CFException();
2926 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2927 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2928 >        if (!createIncomplete) f.completeExceptionally(ex);
2929 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2930 >        if (createIncomplete) f.completeExceptionally(ex);
2931 >
2932 >        checkCompletedWithWrappedException(g, ex);
2933 >        checkCompletedExceptionally(f, ex);
2934 >        r.assertNotInvoked();
2935 >    }}
2936  
2937      /**
2938 <     * thenApplyAsync result completes exceptionally if action does
1401 <     */
1402 <    public void testThenApplyAsync3() {
1403 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1404 <        FailingFunction r = new FailingFunction();
1405 <        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1406 <        f.complete(null);
1407 <        checkCompletedWithWrappedCFException(g);
1408 <    }
1409 <
1410 <    /**
1411 <     * thenApplyAsync result completes exceptionally if source cancelled
1412 <     */
1413 <    public void testThenApplyAsync4() {
1414 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1415 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1416 <        assertTrue(f.cancel(true));
1417 <        checkCompletedWithWrappedCancellationException(g);
1418 <    }
1419 <
1420 <    /**
1421 <     * thenAcceptAsync result completes normally after normal
1422 <     * completion of source
2938 >     * thenCompose result completes exceptionally if action does
2939       */
2940 <    public void testThenAcceptAsync() {
2941 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2942 <        IncAction r = new IncAction();
2943 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2944 <        f.complete(one);
2945 <        checkCompletedNormally(g, null);
2946 <        assertEquals(r.value, 2);
2947 <    }
2940 >    public void testThenCompose_actionFailed() {
2941 >        for (ExecutionMode m : ExecutionMode.values())
2942 >        for (boolean createIncomplete : new boolean[] { true, false })
2943 >        for (Integer v1 : new Integer[] { 1, null })
2944 >    {
2945 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2946 >        final FailingCompletableFutureFunction r
2947 >            = new FailingCompletableFutureFunction(m);
2948 >        if (!createIncomplete) assertTrue(f.complete(v1));
2949 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2950 >        if (createIncomplete) assertTrue(f.complete(v1));
2951  
1433    /**
1434     * thenAcceptAsync result completes exceptionally after exceptional
1435     * completion of source
1436     */
1437    public void testThenAcceptAsync2() {
1438        CompletableFuture<Integer> f = new CompletableFuture<>();
1439        IncAction r = new IncAction();
1440        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1441        f.completeExceptionally(new CFException());
2952          checkCompletedWithWrappedCFException(g);
2953 <    }
2953 >        checkCompletedNormally(f, v1);
2954 >    }}
2955  
2956      /**
2957 <     * thenAcceptAsync result completes exceptionally if action does
2957 >     * thenCompose result completes exceptionally if source cancelled
2958       */
2959 <    public void testThenAcceptAsync3() {
2960 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2961 <        FailingConsumer r = new FailingConsumer();
2962 <        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2963 <        f.complete(null);
2964 <        checkCompletedWithWrappedCFException(g);
2965 <    }
2959 >    public void testThenCompose_sourceCancelled() {
2960 >        for (ExecutionMode m : ExecutionMode.values())
2961 >        for (boolean createIncomplete : new boolean[] { true, false })
2962 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2963 >    {
2964 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2965 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2966 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2967 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2968 >        if (createIncomplete) {
2969 >            checkIncomplete(g);
2970 >            assertTrue(f.cancel(mayInterruptIfRunning));
2971 >        }
2972  
1456    /**
1457     * thenAcceptAsync result completes exceptionally if source cancelled
1458     */
1459    public void testThenAcceptAsync4() {
1460        CompletableFuture<Integer> f = new CompletableFuture<>();
1461        IncAction r = new IncAction();
1462        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1463        assertTrue(f.cancel(true));
2973          checkCompletedWithWrappedCancellationException(g);
2974 <    }
2974 >        checkCancelled(f);
2975 >    }}
2976  
2977      /**
2978 <     * thenCombineAsync result completes normally after normal
1469 <     * completion of sources
2978 >     * thenCompose result completes exceptionally if the result of the action does
2979       */
2980 <    public void testThenCombineAsync() {
2981 <        CompletableFuture<Integer> f, g, h;
2980 >    public void testThenCompose_actionReturnsFailingFuture() {
2981 >        for (ExecutionMode m : ExecutionMode.values())
2982 >        for (int order = 0; order < 6; order++)
2983 >        for (Integer v1 : new Integer[] { 1, null })
2984 >    {
2985 >        final CFException ex = new CFException();
2986 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2987 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2988 >        final CompletableFuture<Integer> h;
2989 >        // Test all permutations of orders
2990 >        switch (order) {
2991 >        case 0:
2992 >            assertTrue(f.complete(v1));
2993 >            assertTrue(g.completeExceptionally(ex));
2994 >            h = m.thenCompose(f, (x -> g));
2995 >            break;
2996 >        case 1:
2997 >            assertTrue(f.complete(v1));
2998 >            h = m.thenCompose(f, (x -> g));
2999 >            assertTrue(g.completeExceptionally(ex));
3000 >            break;
3001 >        case 2:
3002 >            assertTrue(g.completeExceptionally(ex));
3003 >            assertTrue(f.complete(v1));
3004 >            h = m.thenCompose(f, (x -> g));
3005 >            break;
3006 >        case 3:
3007 >            assertTrue(g.completeExceptionally(ex));
3008 >            h = m.thenCompose(f, (x -> g));
3009 >            assertTrue(f.complete(v1));
3010 >            break;
3011 >        case 4:
3012 >            h = m.thenCompose(f, (x -> g));
3013 >            assertTrue(f.complete(v1));
3014 >            assertTrue(g.completeExceptionally(ex));
3015 >            break;
3016 >        case 5:
3017 >            h = m.thenCompose(f, (x -> g));
3018 >            assertTrue(f.complete(v1));
3019 >            assertTrue(g.completeExceptionally(ex));
3020 >            break;
3021 >        default: throw new AssertionError();
3022 >        }
3023  
3024 <        f = new CompletableFuture<>();
3025 <        g = new CompletableFuture<>();
3026 <        h = f.thenCombineAsync(g, subtract);
3027 <        f.complete(3);
1478 <        checkIncomplete(h);
1479 <        g.complete(1);
1480 <        checkCompletedNormally(h, 2);
3024 >        checkCompletedExceptionally(g, ex);
3025 >        checkCompletedWithWrappedException(h, ex);
3026 >        checkCompletedNormally(f, v1);
3027 >    }}
3028  
3029 <        f = new CompletableFuture<>();
1483 <        g = new CompletableFuture<>();
1484 <        h = f.thenCombineAsync(g, subtract);
1485 <        g.complete(1);
1486 <        checkIncomplete(h);
1487 <        f.complete(3);
1488 <        checkCompletedNormally(h, 2);
1489 <
1490 <        f = new CompletableFuture<>();
1491 <        g = new CompletableFuture<>();
1492 <        g.complete(1);
1493 <        f.complete(3);
1494 <        h = f.thenCombineAsync(g, subtract);
1495 <        checkCompletedNormally(h, 2);
1496 <    }
3029 >    // other static methods
3030  
3031      /**
3032 <     * thenCombineAsync result completes exceptionally after exceptional
3033 <     * completion of either source
3032 >     * allOf(no component futures) returns a future completed normally
3033 >     * with the value null
3034       */
3035 <    public void testThenCombineAsync2() {
3036 <        CompletableFuture<Integer> f, g, h;
3037 <
1505 <        f = new CompletableFuture<>();
1506 <        g = new CompletableFuture<>();
1507 <        h = f.thenCombineAsync(g, subtract);
1508 <        f.completeExceptionally(new CFException());
1509 <        checkIncomplete(h);
1510 <        g.complete(1);
1511 <        checkCompletedWithWrappedCFException(h);
1512 <
1513 <        f = new CompletableFuture<>();
1514 <        g = new CompletableFuture<>();
1515 <        h = f.thenCombineAsync(g, subtract);
1516 <        g.completeExceptionally(new CFException());
1517 <        checkIncomplete(h);
1518 <        f.complete(3);
1519 <        checkCompletedWithWrappedCFException(h);
1520 <
1521 <        f = new CompletableFuture<>();
1522 <        g = new CompletableFuture<>();
1523 <        g.completeExceptionally(new CFException());
1524 <        f.complete(3);
1525 <        h = f.thenCombineAsync(g, subtract);
1526 <        checkCompletedWithWrappedCFException(h);
3035 >    public void testAllOf_empty() throws Exception {
3036 >        CompletableFuture<Void> f = CompletableFuture.allOf();
3037 >        checkCompletedNormally(f, null);
3038      }
3039  
3040      /**
3041 <     * thenCombineAsync result completes exceptionally if action does
3041 >     * allOf returns a future completed normally with the value null
3042 >     * when all components complete normally
3043       */
3044 <    public void testThenCombineAsync3() {
3045 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3046 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3047 <        FailingBiFunction r = new FailingBiFunction();
3048 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
3049 <        f.complete(one);
3050 <        checkIncomplete(g);
3051 <        assertFalse(r.ran);
3052 <        f2.complete(two);
3053 <        checkCompletedWithWrappedCFException(g);
3054 <        assertTrue(r.ran);
3044 >    public void testAllOf_normal() throws Exception {
3045 >        for (int k = 1; k < 10; k++) {
3046 >            CompletableFuture<Integer>[] fs
3047 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3048 >            for (int i = 0; i < k; i++)
3049 >                fs[i] = new CompletableFuture<>();
3050 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3051 >            for (int i = 0; i < k; i++) {
3052 >                checkIncomplete(f);
3053 >                checkIncomplete(CompletableFuture.allOf(fs));
3054 >                fs[i].complete(one);
3055 >            }
3056 >            checkCompletedNormally(f, null);
3057 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3058 >        }
3059      }
3060  
3061 <    /**
3062 <     * thenCombineAsync result completes exceptionally if either source cancelled
3063 <     */
3064 <    public void testThenCombineAsync4() {
3065 <        CompletableFuture<Integer> f, g, h;
3066 <
3067 <        f = new CompletableFuture<>();
3068 <        g = new CompletableFuture<>();
3069 <        h = f.thenCombineAsync(g, subtract);
3070 <        assertTrue(f.cancel(true));
3071 <        checkIncomplete(h);
3072 <        g.complete(1);
3073 <        checkCompletedWithWrappedCancellationException(h);
3074 <
3075 <        f = new CompletableFuture<>();
1560 <        g = new CompletableFuture<>();
1561 <        h = f.thenCombineAsync(g, subtract);
1562 <        assertTrue(g.cancel(true));
1563 <        checkIncomplete(h);
1564 <        f.complete(3);
1565 <        checkCompletedWithWrappedCancellationException(h);
1566 <
1567 <        f = new CompletableFuture<>();
1568 <        g = new CompletableFuture<>();
1569 <        g.complete(3);
1570 <        assertTrue(f.cancel(true));
1571 <        h = f.thenCombineAsync(g, subtract);
1572 <        checkCompletedWithWrappedCancellationException(h);
1573 <
1574 <        f = new CompletableFuture<>();
1575 <        g = new CompletableFuture<>();
1576 <        f.complete(3);
1577 <        assertTrue(g.cancel(true));
1578 <        h = f.thenCombineAsync(g, subtract);
1579 <        checkCompletedWithWrappedCancellationException(h);
3061 >    public void testAllOf_backwards() throws Exception {
3062 >        for (int k = 1; k < 10; k++) {
3063 >            CompletableFuture<Integer>[] fs
3064 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3065 >            for (int i = 0; i < k; i++)
3066 >                fs[i] = new CompletableFuture<>();
3067 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3068 >            for (int i = k - 1; i >= 0; i--) {
3069 >                checkIncomplete(f);
3070 >                checkIncomplete(CompletableFuture.allOf(fs));
3071 >                fs[i].complete(one);
3072 >            }
3073 >            checkCompletedNormally(f, null);
3074 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3075 >        }
3076      }
3077  
3078 <    /**
3079 <     * thenAcceptBothAsync result completes normally after normal
3080 <     * completion of sources
3081 <     */
3082 <    public void testThenAcceptBothAsync() {
3083 <        CompletableFuture<Integer> f, g;
3084 <        CompletableFuture<Void> h;
3085 <        SubtractAction r;
3086 <
3087 <        f = new CompletableFuture<>();
3088 <        g = new CompletableFuture<>();
3089 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
3090 <        f.complete(3);
3091 <        checkIncomplete(h);
3092 <        g.complete(1);
3093 <        checkCompletedNormally(h, null);
3094 <        assertEquals(r.value, 2);
3095 <
3096 <        f = new CompletableFuture<>();
3097 <        g = new CompletableFuture<>();
3098 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
3099 <        g.complete(1);
1604 <        checkIncomplete(h);
1605 <        f.complete(3);
1606 <        checkCompletedNormally(h, null);
1607 <        assertEquals(r.value, 2);
1608 <
1609 <        f = new CompletableFuture<>();
1610 <        g = new CompletableFuture<>();
1611 <        g.complete(1);
1612 <        f.complete(3);
1613 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1614 <        checkCompletedNormally(h, null);
1615 <        assertEquals(r.value, 2);
3078 >    public void testAllOf_exceptional() throws Exception {
3079 >        for (int k = 1; k < 10; k++) {
3080 >            CompletableFuture<Integer>[] fs
3081 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3082 >            CFException ex = new CFException();
3083 >            for (int i = 0; i < k; i++)
3084 >                fs[i] = new CompletableFuture<>();
3085 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3086 >            for (int i = 0; i < k; i++) {
3087 >                checkIncomplete(f);
3088 >                checkIncomplete(CompletableFuture.allOf(fs));
3089 >                if (i != k / 2) {
3090 >                    fs[i].complete(i);
3091 >                    checkCompletedNormally(fs[i], i);
3092 >                } else {
3093 >                    fs[i].completeExceptionally(ex);
3094 >                    checkCompletedExceptionally(fs[i], ex);
3095 >                }
3096 >            }
3097 >            checkCompletedWithWrappedException(f, ex);
3098 >            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3099 >        }
3100      }
3101  
3102      /**
3103 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1620 <     * completion of source
3103 >     * anyOf(no component futures) returns an incomplete future
3104       */
3105 <    public void testThenAcceptBothAsync2() {
3106 <        CompletableFuture<Integer> f, g;
3107 <        CompletableFuture<Void> h;
3108 <        SubtractAction r;
3109 <
1627 <        f = new CompletableFuture<>();
1628 <        g = new CompletableFuture<>();
1629 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1630 <        f.completeExceptionally(new CFException());
1631 <        checkIncomplete(h);
1632 <        g.complete(1);
1633 <        checkCompletedWithWrappedCFException(h);
1634 <
1635 <        f = new CompletableFuture<>();
1636 <        g = new CompletableFuture<>();
1637 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1638 <        g.completeExceptionally(new CFException());
1639 <        checkIncomplete(h);
1640 <        f.complete(3);
1641 <        checkCompletedWithWrappedCFException(h);
3105 >    public void testAnyOf_empty() throws Exception {
3106 >        for (Integer v1 : new Integer[] { 1, null })
3107 >    {
3108 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
3109 >        checkIncomplete(f);
3110  
3111 <        f = new CompletableFuture<>();
3112 <        g = new CompletableFuture<>();
3113 <        f.complete(3);
1646 <        g.completeExceptionally(new CFException());
1647 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1648 <        checkCompletedWithWrappedCFException(h);
1649 <
1650 <        f = new CompletableFuture<>();
1651 <        g = new CompletableFuture<>();
1652 <        f.completeExceptionally(new CFException());
1653 <        g.complete(3);
1654 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1655 <        checkCompletedWithWrappedCFException(h);
1656 <    }
3111 >        f.complete(v1);
3112 >        checkCompletedNormally(f, v1);
3113 >    }}
3114  
3115      /**
3116 <     * thenAcceptBothAsync result completes exceptionally if action does
3116 >     * anyOf returns a future completed normally with a value when
3117 >     * a component future does
3118       */
3119 <    public void testThenAcceptBothAsync3() {
3120 <        CompletableFuture<Integer> f, g;
3121 <        CompletableFuture<Void> h;
3122 <        FailingBiConsumer r;
3123 <
3124 <        f = new CompletableFuture<>();
3125 <        g = new CompletableFuture<>();
3126 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
3127 <        f.complete(3);
3128 <        checkIncomplete(h);
3129 <        g.complete(1);
3130 <        checkCompletedWithWrappedCFException(h);
3131 <
3132 <        f = new CompletableFuture<>();
1675 <        g = new CompletableFuture<>();
1676 <        f.complete(3);
1677 <        g.complete(1);
1678 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer());
1679 <        checkCompletedWithWrappedCFException(h);
3119 >    public void testAnyOf_normal() throws Exception {
3120 >        for (int k = 0; k < 10; k++) {
3121 >            CompletableFuture[] fs = new CompletableFuture[k];
3122 >            for (int i = 0; i < k; i++)
3123 >                fs[i] = new CompletableFuture<>();
3124 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3125 >            checkIncomplete(f);
3126 >            for (int i = 0; i < k; i++) {
3127 >                fs[i].complete(i);
3128 >                checkCompletedNormally(f, 0);
3129 >                int x = (int) CompletableFuture.anyOf(fs).join();
3130 >                assertTrue(0 <= x && x <= i);
3131 >            }
3132 >        }
3133      }
3134 <
3135 <    /**
3136 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
3137 <     */
3138 <    public void testThenAcceptBothAsync4() {
3139 <        CompletableFuture<Integer> f, g;
3140 <        CompletableFuture<Void> h;
3141 <        SubtractAction r;
3142 <
3143 <        f = new CompletableFuture<>();
3144 <        g = new CompletableFuture<>();
3145 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
3146 <        assertTrue(f.cancel(true));
3147 <        checkIncomplete(h);
1695 <        g.complete(1);
1696 <        checkCompletedWithWrappedCancellationException(h);
1697 <
1698 <        f = new CompletableFuture<>();
1699 <        g = new CompletableFuture<>();
1700 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1701 <        assertTrue(g.cancel(true));
1702 <        checkIncomplete(h);
1703 <        f.complete(3);
1704 <        checkCompletedWithWrappedCancellationException(h);
1705 <
1706 <        f = new CompletableFuture<>();
1707 <        g = new CompletableFuture<>();
1708 <        f.complete(3);
1709 <        assertTrue(g.cancel(true));
1710 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1711 <        checkCompletedWithWrappedCancellationException(h);
1712 <
1713 <        f = new CompletableFuture<>();
1714 <        g = new CompletableFuture<>();
1715 <        assertTrue(f.cancel(true));
1716 <        g.complete(3);
1717 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction());
1718 <        checkCompletedWithWrappedCancellationException(h);
3134 >    public void testAnyOf_normal_backwards() throws Exception {
3135 >        for (int k = 0; k < 10; k++) {
3136 >            CompletableFuture[] fs = new CompletableFuture[k];
3137 >            for (int i = 0; i < k; i++)
3138 >                fs[i] = new CompletableFuture<>();
3139 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3140 >            checkIncomplete(f);
3141 >            for (int i = k - 1; i >= 0; i--) {
3142 >                fs[i].complete(i);
3143 >                checkCompletedNormally(f, k - 1);
3144 >                int x = (int) CompletableFuture.anyOf(fs).join();
3145 >                assertTrue(i <= x && x <= k - 1);
3146 >            }
3147 >        }
3148      }
3149  
3150      /**
3151 <     * runAfterBothAsync result completes normally after normal
1723 <     * completion of sources
3151 >     * anyOf result completes exceptionally when any component does.
3152       */
3153 <    public void testRunAfterBothAsync() {
3154 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3155 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3156 <        Noop r = new Noop();
3157 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
3158 <        f.complete(one);
3159 <        checkIncomplete(g);
3160 <        f2.complete(two);
3161 <        checkCompletedNormally(g, null);
3162 <        assertTrue(r.ran);
3153 >    public void testAnyOf_exceptional() throws Exception {
3154 >        for (int k = 0; k < 10; k++) {
3155 >            CompletableFuture[] fs = new CompletableFuture[k];
3156 >            CFException[] exs = new CFException[k];
3157 >            for (int i = 0; i < k; i++) {
3158 >                fs[i] = new CompletableFuture<>();
3159 >                exs[i] = new CFException();
3160 >            }
3161 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3162 >            checkIncomplete(f);
3163 >            for (int i = 0; i < k; i++) {
3164 >                fs[i].completeExceptionally(exs[i]);
3165 >                checkCompletedWithWrappedException(f, exs[0]);
3166 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3167 >            }
3168 >        }
3169      }
3170  
3171 <    /**
3172 <     * runAfterBothAsync result completes exceptionally after exceptional
3173 <     * completion of source
3174 <     */
3175 <    public void testRunAfterBothAsync2() {
3176 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3177 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3178 <        Noop r = new Noop();
3179 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
3180 <        f.completeExceptionally(new CFException());
3181 <        f2.complete(two);
3182 <        checkCompletedWithWrappedCFException(g);
3183 <
3184 <        r = new Noop();
3185 <        f = new CompletableFuture<>();
3186 <        f2 = new CompletableFuture<>();
1753 <        g = f.runAfterBothAsync(f2, r);
1754 <        f.complete(one);
1755 <        f2.completeExceptionally(new CFException());
1756 <        checkCompletedWithWrappedCFException(g);
3171 >    public void testAnyOf_exceptional_backwards() throws Exception {
3172 >        for (int k = 0; k < 10; k++) {
3173 >            CompletableFuture[] fs = new CompletableFuture[k];
3174 >            CFException[] exs = new CFException[k];
3175 >            for (int i = 0; i < k; i++) {
3176 >                fs[i] = new CompletableFuture<>();
3177 >                exs[i] = new CFException();
3178 >            }
3179 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3180 >            checkIncomplete(f);
3181 >            for (int i = k - 1; i >= 0; i--) {
3182 >                fs[i].completeExceptionally(exs[i]);
3183 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3184 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3185 >            }
3186 >        }
3187      }
3188  
3189      /**
3190 <     * runAfterBothAsync result completes exceptionally if action does
3190 >     * Completion methods throw NullPointerException with null arguments
3191       */
3192 <    public void testRunAfterBothAsync3() {
3192 >    public void testNPE() {
3193          CompletableFuture<Integer> f = new CompletableFuture<>();
3194 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3195 <        FailingNoop r = new FailingNoop();
3196 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1767 <        f.complete(one);
1768 <        checkIncomplete(g);
1769 <        f2.complete(two);
1770 <        checkCompletedWithWrappedCFException(g);
1771 <    }
3194 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3195 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3196 >        ThreadExecutor exec = new ThreadExecutor();
3197  
3198 <    /**
3199 <     * runAfterBothAsync result completes exceptionally if either source cancelled
3200 <     */
3201 <    public void testRunAfterBothAsync4() {
3202 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3203 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3204 <        Noop r = new Noop();
3205 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
3206 <        assertTrue(f.cancel(true));
3207 <        f2.complete(two);
3208 <        checkCompletedWithWrappedCancellationException(g);
3198 >        Runnable[] throwingActions = {
3199 >            () -> CompletableFuture.supplyAsync(null),
3200 >            () -> CompletableFuture.supplyAsync(null, exec),
3201 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3202 >
3203 >            () -> CompletableFuture.runAsync(null),
3204 >            () -> CompletableFuture.runAsync(null, exec),
3205 >            () -> CompletableFuture.runAsync(() -> {}, null),
3206 >
3207 >            () -> f.completeExceptionally(null),
3208 >
3209 >            () -> f.thenApply(null),
3210 >            () -> f.thenApplyAsync(null),
3211 >            () -> f.thenApplyAsync((x) -> x, null),
3212 >            () -> f.thenApplyAsync(null, exec),
3213 >
3214 >            () -> f.thenAccept(null),
3215 >            () -> f.thenAcceptAsync(null),
3216 >            () -> f.thenAcceptAsync((x) -> {} , null),
3217 >            () -> f.thenAcceptAsync(null, exec),
3218 >
3219 >            () -> f.thenRun(null),
3220 >            () -> f.thenRunAsync(null),
3221 >            () -> f.thenRunAsync(() -> {} , null),
3222 >            () -> f.thenRunAsync(null, exec),
3223 >
3224 >            () -> f.thenCombine(g, null),
3225 >            () -> f.thenCombineAsync(g, null),
3226 >            () -> f.thenCombineAsync(g, null, exec),
3227 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
3228 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
3229 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
3230 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
3231 >
3232 >            () -> f.thenAcceptBoth(g, null),
3233 >            () -> f.thenAcceptBothAsync(g, null),
3234 >            () -> f.thenAcceptBothAsync(g, null, exec),
3235 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
3236 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
3237 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
3238 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
3239 >
3240 >            () -> f.runAfterBoth(g, null),
3241 >            () -> f.runAfterBothAsync(g, null),
3242 >            () -> f.runAfterBothAsync(g, null, exec),
3243 >            () -> f.runAfterBoth(nullFuture, () -> {}),
3244 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
3245 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
3246 >            () -> f.runAfterBothAsync(g, () -> {}, null),
3247 >
3248 >            () -> f.applyToEither(g, null),
3249 >            () -> f.applyToEitherAsync(g, null),
3250 >            () -> f.applyToEitherAsync(g, null, exec),
3251 >            () -> f.applyToEither(nullFuture, (x) -> x),
3252 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3253 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3254 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
3255 >
3256 >            () -> f.acceptEither(g, null),
3257 >            () -> f.acceptEitherAsync(g, null),
3258 >            () -> f.acceptEitherAsync(g, null, exec),
3259 >            () -> f.acceptEither(nullFuture, (x) -> {}),
3260 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3261 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3262 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3263 >
3264 >            () -> f.runAfterEither(g, null),
3265 >            () -> f.runAfterEitherAsync(g, null),
3266 >            () -> f.runAfterEitherAsync(g, null, exec),
3267 >            () -> f.runAfterEither(nullFuture, () -> {}),
3268 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
3269 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
3270 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
3271 >
3272 >            () -> f.thenCompose(null),
3273 >            () -> f.thenComposeAsync(null),
3274 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
3275 >            () -> f.thenComposeAsync(null, exec),
3276 >
3277 >            () -> f.exceptionally(null),
3278 >
3279 >            () -> f.handle(null),
3280 >
3281 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
3282 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
3283 >            () -> CompletableFuture.allOf(f, null),
3284 >            () -> CompletableFuture.allOf(null, f),
3285 >
3286 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
3287 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
3288 >            () -> CompletableFuture.anyOf(f, null),
3289 >            () -> CompletableFuture.anyOf(null, f),
3290 >
3291 >            () -> f.obtrudeException(null),
3292 >
3293 >            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3294 >            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3295 >            () -> CompletableFuture.delayedExecutor(1L, null),
3296  
3297 <        r = new Noop();
3298 <        f = new CompletableFuture<>();
1787 <        f2 = new CompletableFuture<>();
1788 <        g = f.runAfterBothAsync(f2, r);
1789 <        f.complete(one);
1790 <        assertTrue(f2.cancel(true));
1791 <        checkCompletedWithWrappedCancellationException(g);
1792 <    }
3297 >            () -> f.orTimeout(1L, null),
3298 >            () -> f.completeOnTimeout(42, 1L, null),
3299  
3300 <    /**
3301 <     * applyToEitherAsync result completes normally after normal
3302 <     * completion of sources
1797 <     */
1798 <    public void testApplyToEitherAsync() {
1799 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1800 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1801 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1802 <        f.complete(one);
1803 <        checkCompletedNormally(g, two);
3300 >            () -> CompletableFuture.failedFuture(null),
3301 >            () -> CompletableFuture.failedStage(null),
3302 >        };
3303  
3304 <        f = new CompletableFuture<>();
3305 <        f.complete(one);
1807 <        f2 = new CompletableFuture<>();
1808 <        g = f.applyToEitherAsync(f2, inc);
1809 <        checkCompletedNormally(g, two);
3304 >        assertThrows(NullPointerException.class, throwingActions);
3305 >        assertEquals(0, exec.count.get());
3306      }
3307  
3308      /**
3309 <     * applyToEitherAsync result completes exceptionally after exceptional
1814 <     * completion of source
3309 >     * toCompletableFuture returns this CompletableFuture.
3310       */
3311 <    public void testApplyToEitherAsync2() {
3311 >    public void testToCompletableFuture() {
3312          CompletableFuture<Integer> f = new CompletableFuture<>();
3313 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1819 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1820 <        f.completeExceptionally(new CFException());
1821 <        checkCompletedWithWrappedCFException(g);
1822 <
1823 <        f = new CompletableFuture<>();
1824 <        f2 = new CompletableFuture<>();
1825 <        f2.completeExceptionally(new CFException());
1826 <        g = f.applyToEitherAsync(f2, inc);
1827 <        f.complete(one);
1828 <        checkCompletedWithWrappedCFException(g);
3313 >        assertSame(f, f.toCompletableFuture());
3314      }
3315  
3316 <    /**
1832 <     * applyToEitherAsync result completes exceptionally if action does
1833 <     */
1834 <    public void testApplyToEitherAsync3() {
1835 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1836 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1837 <        FailingFunction r = new FailingFunction();
1838 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1839 <        f.complete(one);
1840 <        checkCompletedWithWrappedCFException(g);
1841 <    }
3316 >    // jdk9
3317  
3318      /**
3319 <     * applyToEitherAsync result completes exceptionally if either source cancelled
3319 >     * newIncompleteFuture returns an incomplete CompletableFuture
3320       */
3321 <    public void testApplyToEitherAsync4() {
3321 >    public void testNewIncompleteFuture() {
3322 >        for (Integer v1 : new Integer[] { 1, null })
3323 >    {
3324          CompletableFuture<Integer> f = new CompletableFuture<>();
3325 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3326 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
3327 <        assertTrue(f.cancel(true));
3328 <        checkCompletedWithWrappedCancellationException(g);
3329 <
3330 <        f = new CompletableFuture<>();
3331 <        f2 = new CompletableFuture<>();
3332 <        assertTrue(f2.cancel(true));
3333 <        g = f.applyToEitherAsync(f2, inc);
3334 <        checkCompletedWithWrappedCancellationException(g);
1858 <    }
3325 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3326 >        checkIncomplete(f);
3327 >        checkIncomplete(g);
3328 >        f.complete(v1);
3329 >        checkCompletedNormally(f, v1);
3330 >        checkIncomplete(g);
3331 >        g.complete(v1);
3332 >        checkCompletedNormally(g, v1);
3333 >        assertSame(g.getClass(), CompletableFuture.class);
3334 >    }}
3335  
3336      /**
3337 <     * acceptEitherAsync result completes normally after normal
1862 <     * completion of sources
3337 >     * completedStage returns a completed CompletionStage
3338       */
3339 <    public void testAcceptEitherAsync() {
3340 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3341 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3342 <        IncAction r = new IncAction();
3343 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
3344 <        f.complete(one);
3345 <        checkCompletedNormally(g, null);
1871 <        assertEquals(r.value, 2);
1872 <
1873 <        r = new IncAction();
1874 <        f = new CompletableFuture<>();
1875 <        f.complete(one);
1876 <        f2 = new CompletableFuture<>();
1877 <        g = f.acceptEitherAsync(f2, r);
1878 <        checkCompletedNormally(g, null);
1879 <        assertEquals(r.value, 2);
3339 >    public void testCompletedStage() {
3340 >        AtomicInteger x = new AtomicInteger(0);
3341 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3342 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3343 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3344 >        assertEquals(x.get(), 1);
3345 >        assertNull(r.get());
3346      }
3347  
3348      /**
3349 <     * acceptEitherAsync result completes exceptionally after exceptional
3350 <     * completion of source
3349 >     * defaultExecutor by default returns the commonPool if
3350 >     * it supports more than one thread.
3351       */
3352 <    public void testAcceptEitherAsync2() {
3352 >    public void testDefaultExecutor() {
3353          CompletableFuture<Integer> f = new CompletableFuture<>();
3354 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3355 <        IncAction r = new IncAction();
3356 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
3357 <        f.completeExceptionally(new CFException());
3358 <        checkCompletedWithWrappedCFException(g);
3359 <
1894 <        r = new IncAction();
1895 <        f = new CompletableFuture<>();
1896 <        f2 = new CompletableFuture<>();
1897 <        f2.completeExceptionally(new CFException());
1898 <        g = f.acceptEitherAsync(f2, r);
1899 <        f.complete(one);
1900 <        checkCompletedWithWrappedCFException(g);
3354 >        Executor e = f.defaultExecutor();
3355 >        Executor c = ForkJoinPool.commonPool();
3356 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3357 >            assertSame(e, c);
3358 >        else
3359 >            assertNotSame(e, c);
3360      }
3361  
3362      /**
3363 <     * acceptEitherAsync result completes exceptionally if action does
3363 >     * failedFuture returns a CompletableFuture completed
3364 >     * exceptionally with the given Exception
3365       */
3366 <    public void testAcceptEitherAsync3() {
3367 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3368 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3369 <        FailingConsumer r = new FailingConsumer();
1910 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1911 <        f.complete(one);
1912 <        checkCompletedWithWrappedCFException(g);
3366 >    public void testFailedFuture() {
3367 >        CFException ex = new CFException();
3368 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3369 >        checkCompletedExceptionally(f, ex);
3370      }
3371  
3372      /**
3373 <     * acceptEitherAsync result completes exceptionally if either
1917 <     * source cancelled
3373 >     * failedFuture(null) throws NPE
3374       */
3375 <    public void testAcceptEitherAsync4() {
3376 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3377 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3378 <        IncAction r = new IncAction();
3379 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1924 <        assertTrue(f.cancel(true));
1925 <        checkCompletedWithWrappedCancellationException(g);
1926 <
1927 <        r = new IncAction();
1928 <        f = new CompletableFuture<>();
1929 <        f2 = new CompletableFuture<>();
1930 <        assertTrue(f2.cancel(true));
1931 <        g = f.acceptEitherAsync(f2, r);
1932 <        checkCompletedWithWrappedCancellationException(g);
3375 >    public void testFailedFuture_null() {
3376 >        try {
3377 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3378 >            shouldThrow();
3379 >        } catch (NullPointerException success) {}
3380      }
3381  
3382      /**
3383 <     * runAfterEitherAsync result completes normally after normal
3384 <     * completion of sources
3383 >     * copy returns a CompletableFuture that is completed normally,
3384 >     * with the same value, when source is.
3385       */
3386 <    public void testRunAfterEitherAsync() {
3386 >    public void testCopy() {
3387          CompletableFuture<Integer> f = new CompletableFuture<>();
3388 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3389 <        Noop r = new Noop();
3390 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
3391 <        f.complete(one);
3392 <        checkCompletedNormally(g, null);
3393 <        assertTrue(r.ran);
1947 <
1948 <        r = new Noop();
1949 <        f = new CompletableFuture<>();
1950 <        f.complete(one);
1951 <        f2 = new CompletableFuture<>();
1952 <        g = f.runAfterEitherAsync(f2, r);
1953 <        checkCompletedNormally(g, null);
1954 <        assertTrue(r.ran);
3388 >        CompletableFuture<Integer> g = f.copy();
3389 >        checkIncomplete(f);
3390 >        checkIncomplete(g);
3391 >        f.complete(1);
3392 >        checkCompletedNormally(f, 1);
3393 >        checkCompletedNormally(g, 1);
3394      }
3395  
3396      /**
3397 <     * runAfterEitherAsync result completes exceptionally after exceptional
3398 <     * completion of source
3397 >     * copy returns a CompletableFuture that is completed exceptionally
3398 >     * when source is.
3399       */
3400 <    public void testRunAfterEitherAsync2() {
3400 >    public void testCopy2() {
3401          CompletableFuture<Integer> f = new CompletableFuture<>();
3402 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3403 <        Noop r = new Noop();
3404 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
3405 <        f.completeExceptionally(new CFException());
3406 <        checkCompletedWithWrappedCFException(g);
3407 <
3408 <        r = new Noop();
1970 <        f = new CompletableFuture<>();
1971 <        f2 = new CompletableFuture<>();
1972 <        f2.completeExceptionally(new CFException());
1973 <        g = f.runAfterEitherAsync(f2, r);
1974 <        f.complete(one);
1975 <        checkCompletedWithWrappedCFException(g);
3402 >        CompletableFuture<Integer> g = f.copy();
3403 >        checkIncomplete(f);
3404 >        checkIncomplete(g);
3405 >        CFException ex = new CFException();
3406 >        f.completeExceptionally(ex);
3407 >        checkCompletedExceptionally(f, ex);
3408 >        checkCompletedWithWrappedException(g, ex);
3409      }
3410  
3411      /**
3412 <     * runAfterEitherAsync result completes exceptionally if action does
3412 >     * minimalCompletionStage returns a CompletableFuture that is
3413 >     * completed normally, with the same value, when source is.
3414       */
3415 <    public void testRunAfterEitherAsync3() {
3415 >    public void testMinimalCompletionStage() {
3416          CompletableFuture<Integer> f = new CompletableFuture<>();
3417 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3418 <        FailingNoop r = new FailingNoop();
3419 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
3420 <        f.complete(one);
3421 <        checkCompletedWithWrappedCFException(g);
3417 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3418 >        AtomicInteger x = new AtomicInteger(0);
3419 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3420 >        checkIncomplete(f);
3421 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3422 >        f.complete(1);
3423 >        checkCompletedNormally(f, 1);
3424 >        assertEquals(x.get(), 1);
3425 >        assertNull(r.get());
3426      }
3427  
3428      /**
3429 <     * runAfterEitherAsync result completes exceptionally if either
3430 <     * source cancelled
3429 >     * minimalCompletionStage returns a CompletableFuture that is
3430 >     * completed exceptionally when source is.
3431       */
3432 <    public void testRunAfterEitherAsync4() {
3432 >    public void testMinimalCompletionStage2() {
3433          CompletableFuture<Integer> f = new CompletableFuture<>();
3434 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3435 <        Noop r = new Noop();
3436 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
3437 <        assertTrue(f.cancel(true));
3438 <        checkCompletedWithWrappedCancellationException(g);
3439 <
3440 <        r = new Noop();
3441 <        f = new CompletableFuture<>();
3442 <        f2 = new CompletableFuture<>();
3443 <        assertTrue(f2.cancel(true));
2006 <        g = f.runAfterEitherAsync(f2, r);
2007 <        checkCompletedWithWrappedCancellationException(g);
2008 <    }
2009 <
2010 <    /**
2011 <     * thenComposeAsync result completes normally after normal
2012 <     * completion of source
2013 <     */
2014 <    public void testThenComposeAsync() {
2015 <        CompletableFuture<Integer> f, g;
2016 <        CompletableFutureInc r;
2017 <
2018 <        f = new CompletableFuture<>();
2019 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2020 <        f.complete(one);
2021 <        checkCompletedNormally(g, two);
2022 <
2023 <        f = new CompletableFuture<>();
2024 <        f.complete(one);
2025 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2026 <        checkCompletedNormally(g, two);
2027 <    }
2028 <
2029 <    /**
2030 <     * thenComposeAsync result completes exceptionally after
2031 <     * exceptional completion of source
2032 <     */
2033 <    public void testThenComposeAsync2() {
2034 <        CompletableFuture<Integer> f, g;
2035 <        CompletableFutureInc r;
2036 <
2037 <        f = new CompletableFuture<>();
2038 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2039 <        f.completeExceptionally(new CFException());
2040 <        checkCompletedWithWrappedCFException(g);
2041 <        assertFalse(r.ran);
2042 <
2043 <        f = new CompletableFuture<>();
2044 <        f.completeExceptionally(new CFException());
2045 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2046 <        checkCompletedWithWrappedCFException(g);
2047 <        assertFalse(r.ran);
2048 <    }
2049 <
2050 <    /**
2051 <     * thenComposeAsync result completes exceptionally if action does
2052 <     */
2053 <    public void testThenComposeAsync3() {
2054 <        CompletableFuture<Integer> f, g;
2055 <        FailingCompletableFutureFunction r;
2056 <
2057 <        f = new CompletableFuture<>();
2058 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2059 <        f.complete(one);
2060 <        checkCompletedWithWrappedCFException(g);
2061 <
2062 <        f = new CompletableFuture<>();
2063 <        f.complete(one);
2064 <        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
2065 <        checkCompletedWithWrappedCFException(g);
3434 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3435 >        AtomicInteger x = new AtomicInteger(0);
3436 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3437 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3438 >        checkIncomplete(f);
3439 >        CFException ex = new CFException();
3440 >        f.completeExceptionally(ex);
3441 >        checkCompletedExceptionally(f, ex);
3442 >        assertEquals(x.get(), 0);
3443 >        assertEquals(r.get().getCause(), ex);
3444      }
3445  
3446      /**
3447 <     * thenComposeAsync result completes exceptionally if source cancelled
3447 >     * failedStage returns a CompletionStage completed
3448 >     * exceptionally with the given Exception
3449       */
3450 <    public void testThenComposeAsync4() {
3451 <        CompletableFuture<Integer> f, g;
3452 <        CompletableFutureInc r;
3453 <
3454 <        f = new CompletableFuture<>();
3455 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
3456 <        assertTrue(f.cancel(true));
3457 <        checkCompletedWithWrappedCancellationException(g);
2079 <
2080 <        f = new CompletableFuture<>();
2081 <        assertTrue(f.cancel(true));
2082 <        g = f.thenComposeAsync(r = new CompletableFutureInc());
2083 <        checkCompletedWithWrappedCancellationException(g);
3450 >    public void testFailedStage() {
3451 >        CFException ex = new CFException();
3452 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3453 >        AtomicInteger x = new AtomicInteger(0);
3454 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3455 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3456 >        assertEquals(x.get(), 0);
3457 >        assertEquals(r.get(), ex);
3458      }
3459  
2086    // async with explicit executors
2087
3460      /**
3461 <     * thenRunAsync result completes normally after normal completion of source
3461 >     * completeAsync completes with value of given supplier
3462       */
3463 <    public void testThenRunAsyncE() {
3463 >    public void testCompleteAsync() {
3464 >        for (Integer v1 : new Integer[] { 1, null })
3465 >    {
3466          CompletableFuture<Integer> f = new CompletableFuture<>();
3467 <        Noop r = new Noop();
3468 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
3469 <        f.complete(null);
3470 <        checkCompletedNormally(g, null);
2097 <
2098 <        // reordered version
2099 <        f = new CompletableFuture<>();
2100 <        f.complete(null);
2101 <        r = new Noop();
2102 <        g = f.thenRunAsync(r, new ThreadExecutor());
2103 <        checkCompletedNormally(g, null);
2104 <    }
3467 >        f.completeAsync(() -> v1);
3468 >        f.join();
3469 >        checkCompletedNormally(f, v1);
3470 >    }}
3471  
3472      /**
3473 <     * thenRunAsync result completes exceptionally after exceptional
2108 <     * completion of source
3473 >     * completeAsync completes exceptionally if given supplier throws
3474       */
3475 <    public void testThenRunAsync2E() {
3475 >    public void testCompleteAsync2() {
3476          CompletableFuture<Integer> f = new CompletableFuture<>();
3477 <        Noop r = new Noop();
3478 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2114 <        f.completeExceptionally(new CFException());
3477 >        CFException ex = new CFException();
3478 >        f.completeAsync(() -> {if (true) throw ex; return 1;});
3479          try {
3480 <            g.join();
3480 >            f.join();
3481              shouldThrow();
3482 <        } catch (Exception ok) {
3483 <        }
2120 <        checkCompletedWithWrappedCFException(g);
2121 <    }
2122 <
2123 <    /**
2124 <     * thenRunAsync result completes exceptionally if action does
2125 <     */
2126 <    public void testThenRunAsync3E() {
2127 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2128 <        FailingNoop r = new FailingNoop();
2129 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2130 <        f.complete(null);
2131 <        checkCompletedWithWrappedCFException(g);
2132 <    }
2133 <
2134 <    /**
2135 <     * thenRunAsync result completes exceptionally if source cancelled
2136 <     */
2137 <    public void testThenRunAsync4E() {
2138 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2139 <        Noop r = new Noop();
2140 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2141 <        assertTrue(f.cancel(true));
2142 <        checkCompletedWithWrappedCancellationException(g);
2143 <    }
2144 <
2145 <    /**
2146 <     * thenApplyAsync result completes normally after normal completion of source
2147 <     */
2148 <    public void testThenApplyAsyncE() {
2149 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2150 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
2151 <        f.complete(one);
2152 <        checkCompletedNormally(g, two);
3482 >        } catch (CompletionException success) {}
3483 >        checkCompletedWithWrappedException(f, ex);
3484      }
3485  
3486      /**
3487 <     * thenApplyAsync result completes exceptionally after exceptional
2157 <     * completion of source
3487 >     * completeAsync with given executor completes with value of given supplier
3488       */
3489 <    public void testThenApplyAsync2E() {
3489 >    public void testCompleteAsync3() {
3490 >        for (Integer v1 : new Integer[] { 1, null })
3491 >    {
3492          CompletableFuture<Integer> f = new CompletableFuture<>();
3493 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3494 <        f.completeExceptionally(new CFException());
3495 <        checkCompletedWithWrappedCFException(g);
3496 <    }
3493 >        ThreadExecutor executor = new ThreadExecutor();
3494 >        f.completeAsync(() -> v1, executor);
3495 >        assertSame(v1, f.join());
3496 >        checkCompletedNormally(f, v1);
3497 >        assertEquals(1, executor.count.get());
3498 >    }}
3499  
3500      /**
3501 <     * thenApplyAsync result completes exceptionally if action does
3501 >     * completeAsync with given executor completes exceptionally if
3502 >     * given supplier throws
3503       */
3504 <    public void testThenApplyAsync3E() {
3504 >    public void testCompleteAsync4() {
3505          CompletableFuture<Integer> f = new CompletableFuture<>();
3506 <        FailingFunction r = new FailingFunction();
3507 <        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
3508 <        f.complete(null);
3509 <        checkCompletedWithWrappedCFException(g);
3506 >        CFException ex = new CFException();
3507 >        ThreadExecutor executor = new ThreadExecutor();
3508 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3509 >        try {
3510 >            f.join();
3511 >            shouldThrow();
3512 >        } catch (CompletionException success) {}
3513 >        checkCompletedWithWrappedException(f, ex);
3514 >        assertEquals(1, executor.count.get());
3515      }
3516  
3517      /**
3518 <     * thenApplyAsync result completes exceptionally if source cancelled
3518 >     * orTimeout completes with TimeoutException if not complete
3519       */
3520 <    public void testThenApplyAsync4E() {
3520 >    public void testOrTimeout_timesOut() {
3521 >        long timeoutMillis = timeoutMillis();
3522          CompletableFuture<Integer> f = new CompletableFuture<>();
3523 <        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
3524 <        assertTrue(f.cancel(true));
3525 <        checkCompletedWithWrappedCancellationException(g);
3523 >        long startTime = System.nanoTime();
3524 >        f.orTimeout(timeoutMillis, MILLISECONDS);
3525 >        checkCompletedWithTimeoutException(f);
3526 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3527      }
3528  
3529      /**
3530 <     * thenAcceptAsync result completes normally after normal
2189 <     * completion of source
3530 >     * orTimeout completes normally if completed before timeout
3531       */
3532 <    public void testThenAcceptAsyncE() {
3532 >    public void testOrTimeout_completed() {
3533 >        for (Integer v1 : new Integer[] { 1, null })
3534 >    {
3535          CompletableFuture<Integer> f = new CompletableFuture<>();
3536 <        IncAction r = new IncAction();
3537 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3538 <        f.complete(one);
3539 <        checkCompletedNormally(g, null);
3540 <        assertEquals(r.value, 2);
3541 <    }
3536 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3537 >        long startTime = System.nanoTime();
3538 >        f.complete(v1);
3539 >        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3540 >        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3541 >        g.complete(v1);
3542 >        checkCompletedNormally(f, v1);
3543 >        checkCompletedNormally(g, v1);
3544 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3545 >    }}
3546  
3547      /**
3548 <     * thenAcceptAsync result completes exceptionally after exceptional
2202 <     * completion of source
3548 >     * completeOnTimeout completes with given value if not complete
3549       */
3550 <    public void testThenAcceptAsync2E() {
3551 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3552 <        IncAction r = new IncAction();
2207 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
2208 <        f.completeExceptionally(new CFException());
2209 <        checkCompletedWithWrappedCFException(g);
3550 >    public void testCompleteOnTimeout_timesOut() {
3551 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3552 >                       () -> testCompleteOnTimeout_timesOut(null));
3553      }
3554  
3555 <    /**
3556 <     * thenAcceptAsync result completes exceptionally if action does
2214 <     */
2215 <    public void testThenAcceptAsync3E() {
3555 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3556 >        long timeoutMillis = timeoutMillis();
3557          CompletableFuture<Integer> f = new CompletableFuture<>();
3558 <        FailingConsumer r = new FailingConsumer();
3559 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3560 <        f.complete(null);
3561 <        checkCompletedWithWrappedCFException(g);
3558 >        long startTime = System.nanoTime();
3559 >        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3560 >        assertSame(v, f.join());
3561 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3562 >        f.complete(99);         // should have no effect
3563 >        checkCompletedNormally(f, v);
3564      }
3565  
3566      /**
3567 <     * thenAcceptAsync result completes exceptionally if source cancelled
3567 >     * completeOnTimeout has no effect if completed within timeout
3568       */
3569 <    public void testThenAcceptAsync4E() {
3569 >    public void testCompleteOnTimeout_completed() {
3570 >        for (Integer v1 : new Integer[] { 1, null })
3571 >    {
3572          CompletableFuture<Integer> f = new CompletableFuture<>();
3573 <        IncAction r = new IncAction();
3574 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3575 <        assertTrue(f.cancel(true));
3576 <        checkCompletedWithWrappedCancellationException(g);
3577 <    }
3578 <
3579 <    /**
3580 <     * thenCombineAsync result completes normally after normal
3581 <     * completion of sources
3582 <     */
3583 <    public void testThenCombineAsyncE() {
3584 <        CompletableFuture<Integer> f, g, h;
3585 <        ThreadExecutor e = new ThreadExecutor();
3586 <        int count = 0;
3587 <
3588 <        f = new CompletableFuture<>();
3589 <        g = new CompletableFuture<>();
3590 <        h = f.thenCombineAsync(g, subtract, e);
3591 <        f.complete(3);
3592 <        checkIncomplete(h);
3593 <        g.complete(1);
3594 <        checkCompletedNormally(h, 2);
3595 <        assertEquals(++count, e.count.get());
3596 <
3597 <        f = new CompletableFuture<>();
3598 <        g = new CompletableFuture<>();
3599 <        h = f.thenCombineAsync(g, subtract, e);
3600 <        g.complete(1);
3601 <        checkIncomplete(h);
3602 <        f.complete(3);
3603 <        checkCompletedNormally(h, 2);
3604 <        assertEquals(++count, e.count.get());
3605 <
3606 <        f = new CompletableFuture<>();
3607 <        g = new CompletableFuture<>();
3608 <        g.complete(1);
3609 <        f.complete(3);
3610 <        h = f.thenCombineAsync(g, subtract, e);
2266 <        checkCompletedNormally(h, 2);
2267 <        assertEquals(++count, e.count.get());
2268 <    }
2269 <
2270 <    /**
2271 <     * thenCombineAsync result completes exceptionally after exceptional
2272 <     * completion of either source
2273 <     */
2274 <    public void testThenCombineAsync2E() {
2275 <        CompletableFuture<Integer> f, g, h;
2276 <        ThreadExecutor e = new ThreadExecutor();
2277 <        int count = 0;
2278 <
2279 <        f = new CompletableFuture<>();
2280 <        g = new CompletableFuture<>();
2281 <        h = f.thenCombineAsync(g, subtract, e);
2282 <        f.completeExceptionally(new CFException());
2283 <        checkIncomplete(h);
2284 <        g.complete(1);
2285 <        checkCompletedWithWrappedCFException(h);
3573 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3574 >        long startTime = System.nanoTime();
3575 >        f.complete(v1);
3576 >        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3577 >        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3578 >        g.complete(v1);
3579 >        checkCompletedNormally(f, v1);
3580 >        checkCompletedNormally(g, v1);
3581 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3582 >    }}
3583 >
3584 >    /**
3585 >     * delayedExecutor returns an executor that delays submission
3586 >     */
3587 >    public void testDelayedExecutor() {
3588 >        testInParallel(() -> testDelayedExecutor(null, null),
3589 >                       () -> testDelayedExecutor(null, 1),
3590 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3591 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3592 >    }
3593 >
3594 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3595 >        long timeoutMillis = timeoutMillis();
3596 >        // Use an "unreasonably long" long timeout to catch lingering threads
3597 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3598 >        final Executor delayer, longDelayer;
3599 >        if (executor == null) {
3600 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3601 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3602 >        } else {
3603 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3604 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3605 >        }
3606 >        long startTime = System.nanoTime();
3607 >        CompletableFuture<Integer> f =
3608 >            CompletableFuture.supplyAsync(() -> v, delayer);
3609 >        CompletableFuture<Integer> g =
3610 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3611  
3612 <        f = new CompletableFuture<>();
2288 <        g = new CompletableFuture<>();
2289 <        h = f.thenCombineAsync(g, subtract, e);
2290 <        g.completeExceptionally(new CFException());
2291 <        checkIncomplete(h);
2292 <        f.complete(3);
2293 <        checkCompletedWithWrappedCFException(h);
3612 >        assertNull(g.getNow(null));
3613  
3614 <        f = new CompletableFuture<>();
3615 <        g = new CompletableFuture<>();
3616 <        g.completeExceptionally(new CFException());
3617 <        h = f.thenCombineAsync(g, subtract, e);
2299 <        checkIncomplete(h);
2300 <        f.complete(3);
2301 <        checkCompletedWithWrappedCFException(h);
3614 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3615 >        long millisElapsed = millisElapsedSince(startTime);
3616 >        assertTrue(millisElapsed >= timeoutMillis);
3617 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3618  
3619 <        assertEquals(0, e.count.get());
2304 <    }
3619 >        checkCompletedNormally(f, v);
3620  
2306    /**
2307     * thenCombineAsync result completes exceptionally if action does
2308     */
2309    public void testThenCombineAsync3E() {
2310        CompletableFuture<Integer> f = new CompletableFuture<>();
2311        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2312        FailingBiFunction r = new FailingBiFunction();
2313        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2314        f.complete(one);
3621          checkIncomplete(g);
2316        assertFalse(r.ran);
2317        f2.complete(two);
2318        checkCompletedWithWrappedCFException(g);
2319        assertTrue(r.ran);
2320    }
2321
2322    /**
2323     * thenCombineAsync result completes exceptionally if either source cancelled
2324     */
2325    public void testThenCombineAsync4E() {
2326        CompletableFuture<Integer> f, g, h;
2327        ThreadExecutor e = new ThreadExecutor();
2328
2329        f = new CompletableFuture<>();
2330        g = new CompletableFuture<>();
2331        h = f.thenCombineAsync(g, subtract, e);
2332        assertTrue(f.cancel(true));
2333        checkIncomplete(h);
2334        g.complete(1);
2335        checkCompletedWithWrappedCancellationException(h);
2336
2337        f = new CompletableFuture<>();
2338        g = new CompletableFuture<>();
2339        h = f.thenCombineAsync(g, subtract, e);
3622          assertTrue(g.cancel(true));
2341        checkIncomplete(h);
2342        f.complete(3);
2343        checkCompletedWithWrappedCancellationException(h);
2344
2345        f = new CompletableFuture<>();
2346        g = new CompletableFuture<>();
2347        assertTrue(g.cancel(true));
2348        h = f.thenCombineAsync(g, subtract, e);
2349        checkIncomplete(h);
2350        f.complete(3);
2351        checkCompletedWithWrappedCancellationException(h);
2352
2353        f = new CompletableFuture<>();
2354        g = new CompletableFuture<>();
2355        assertTrue(f.cancel(true));
2356        assertTrue(g.cancel(true));
2357        h = f.thenCombineAsync(g, subtract, e);
2358        checkCompletedWithWrappedCancellationException(h);
2359
2360        assertEquals(0, e.count.get());
3623      }
3624  
3625 <    /**
2364 <     * thenAcceptBothAsync result completes normally after normal
2365 <     * completion of sources
2366 <     */
2367 <    public void testThenAcceptBothAsyncE() {
2368 <        CompletableFuture<Integer> f, g;
2369 <        CompletableFuture<Void> h;
2370 <        SubtractAction r;
2371 <        ThreadExecutor e = new ThreadExecutor();
2372 <
2373 <        f = new CompletableFuture<>();
2374 <        g = new CompletableFuture<>();
2375 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2376 <        f.complete(3);
2377 <        checkIncomplete(h);
2378 <        g.complete(1);
2379 <        checkCompletedNormally(h, null);
2380 <        assertEquals(r.value, 2);
2381 <
2382 <        f = new CompletableFuture<>();
2383 <        g = new CompletableFuture<>();
2384 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2385 <        g.complete(1);
2386 <        checkIncomplete(h);
2387 <        f.complete(3);
2388 <        checkCompletedNormally(h, null);
2389 <        assertEquals(r.value, 2);
2390 <
2391 <        f = new CompletableFuture<>();
2392 <        g = new CompletableFuture<>();
2393 <        g.complete(1);
2394 <        f.complete(3);
2395 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2396 <        checkCompletedNormally(h, null);
2397 <        assertEquals(r.value, 2);
2398 <
2399 <        assertEquals(3, e.count.get());
2400 <    }
2401 <
2402 <    /**
2403 <     * thenAcceptBothAsync result completes exceptionally after exceptional
2404 <     * completion of source
2405 <     */
2406 <    public void testThenAcceptBothAsync2E() {
2407 <        CompletableFuture<Integer> f, g;
2408 <        CompletableFuture<Void> h;
2409 <        SubtractAction r;
2410 <        ThreadExecutor e = new ThreadExecutor();
2411 <
2412 <        f = new CompletableFuture<>();
2413 <        g = new CompletableFuture<>();
2414 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2415 <        f.completeExceptionally(new CFException());
2416 <        checkIncomplete(h);
2417 <        g.complete(1);
2418 <        checkCompletedWithWrappedCFException(h);
2419 <
2420 <        f = new CompletableFuture<>();
2421 <        g = new CompletableFuture<>();
2422 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2423 <        g.completeExceptionally(new CFException());
2424 <        checkIncomplete(h);
2425 <        f.complete(3);
2426 <        checkCompletedWithWrappedCFException(h);
3625 >    //--- tests of implementation details; not part of official tck ---
3626  
3627 <        f = new CompletableFuture<>();
3628 <        g = new CompletableFuture<>();
3629 <        f.complete(3);
3630 <        g.completeExceptionally(new CFException());
3631 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
3632 <        checkCompletedWithWrappedCFException(h);
3633 <
2435 <        f = new CompletableFuture<>();
2436 <        g = new CompletableFuture<>();
2437 <        f.completeExceptionally(new CFException());
2438 <        g.complete(3);
2439 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2440 <        checkCompletedWithWrappedCFException(h);
2441 <
2442 <        assertEquals(0, e.count.get());
2443 <    }
2444 <
2445 <    /**
2446 <     * thenAcceptBothAsync result completes exceptionally if action does
2447 <     */
2448 <    public void testThenAcceptBothAsync3E() {
2449 <        CompletableFuture<Integer> f, g;
2450 <        CompletableFuture<Void> h;
2451 <        FailingBiConsumer r;
2452 <        ThreadExecutor e = new ThreadExecutor();
2453 <
2454 <        f = new CompletableFuture<>();
2455 <        g = new CompletableFuture<>();
2456 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2457 <        f.complete(3);
2458 <        checkIncomplete(h);
2459 <        g.complete(1);
2460 <        checkCompletedWithWrappedCFException(h);
2461 <
2462 <        f = new CompletableFuture<>();
2463 <        g = new CompletableFuture<>();
2464 <        f.complete(3);
2465 <        g.complete(1);
2466 <        h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e);
2467 <        checkCompletedWithWrappedCFException(h);
2468 <
2469 <        assertEquals(2, e.count.get());
2470 <    }
2471 <
2472 <    /**
2473 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
2474 <     */
2475 <    public void testThenAcceptBothAsync4E() {
2476 <        CompletableFuture<Integer> f, g;
2477 <        CompletableFuture<Void> h;
2478 <        SubtractAction r;
2479 <        ThreadExecutor e = new ThreadExecutor();
2480 <
2481 <        f = new CompletableFuture<>();
2482 <        g = new CompletableFuture<>();
2483 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2484 <        assertTrue(f.cancel(true));
2485 <        checkIncomplete(h);
2486 <        g.complete(1);
2487 <        checkCompletedWithWrappedCancellationException(h);
2488 <
2489 <        f = new CompletableFuture<>();
2490 <        g = new CompletableFuture<>();
2491 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2492 <        assertTrue(g.cancel(true));
2493 <        checkIncomplete(h);
2494 <        f.complete(3);
2495 <        checkCompletedWithWrappedCancellationException(h);
2496 <
2497 <        f = new CompletableFuture<>();
2498 <        g = new CompletableFuture<>();
2499 <        f.complete(3);
2500 <        assertTrue(g.cancel(true));
2501 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2502 <        checkCompletedWithWrappedCancellationException(h);
2503 <
2504 <        f = new CompletableFuture<>();
2505 <        g = new CompletableFuture<>();
2506 <        assertTrue(f.cancel(true));
2507 <        g.complete(3);
2508 <        h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e);
2509 <        checkCompletedWithWrappedCancellationException(h);
2510 <
2511 <        assertEquals(0, e.count.get());
2512 <    }
2513 <
2514 <    /**
2515 <     * runAfterBothAsync result completes normally after normal
2516 <     * completion of sources
2517 <     */
2518 <    public void testRunAfterBothAsyncE() {
2519 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2520 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2521 <        Noop r = new Noop();
2522 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2523 <        f.complete(one);
2524 <        checkIncomplete(g);
2525 <        f2.complete(two);
2526 <        checkCompletedNormally(g, null);
2527 <        assertTrue(r.ran);
2528 <    }
2529 <
2530 <    /**
2531 <     * runAfterBothAsync result completes exceptionally after exceptional
2532 <     * completion of source
2533 <     */
2534 <    public void testRunAfterBothAsync2E() {
2535 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2536 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2537 <        Noop r = new Noop();
2538 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2539 <        f.completeExceptionally(new CFException());
2540 <        f2.complete(two);
2541 <        checkCompletedWithWrappedCFException(g);
2542 <
2543 <        r = new Noop();
2544 <        f = new CompletableFuture<>();
2545 <        f2 = new CompletableFuture<>();
2546 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2547 <        f.complete(one);
2548 <        f2.completeExceptionally(new CFException());
2549 <        checkCompletedWithWrappedCFException(g);
2550 <    }
2551 <
2552 <    /**
2553 <     * runAfterBothAsync result completes exceptionally if action does
2554 <     */
2555 <    public void testRunAfterBothAsync3E() {
2556 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2557 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2558 <        FailingNoop r = new FailingNoop();
2559 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2560 <        f.complete(one);
2561 <        checkIncomplete(g);
2562 <        f2.complete(two);
2563 <        checkCompletedWithWrappedCFException(g);
2564 <    }
2565 <
2566 <    /**
2567 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2568 <     */
2569 <    public void testRunAfterBothAsync4E() {
2570 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2571 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2572 <        Noop r = new Noop();
2573 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2574 <        assertTrue(f.cancel(true));
2575 <        f2.complete(two);
2576 <        checkCompletedWithWrappedCancellationException(g);
2577 <
2578 <        r = new Noop();
2579 <        f = new CompletableFuture<>();
2580 <        f2 = new CompletableFuture<>();
2581 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2582 <        f.complete(one);
2583 <        assertTrue(f2.cancel(true));
2584 <        checkCompletedWithWrappedCancellationException(g);
2585 <    }
2586 <
2587 <    /**
2588 <     * applyToEitherAsync result completes normally after normal
2589 <     * completion of sources
2590 <     */
2591 <    public void testApplyToEitherAsyncE() {
2592 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2593 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2594 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2595 <        f.complete(one);
2596 <        checkCompletedNormally(g, two);
2597 <
2598 <        f = new CompletableFuture<>();
2599 <        f.complete(one);
2600 <        f2 = new CompletableFuture<>();
2601 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2602 <        checkCompletedNormally(g, two);
2603 <    }
2604 <
2605 <    /**
2606 <     * applyToEitherAsync result completes exceptionally after exceptional
2607 <     * completion of source
2608 <     */
2609 <    public void testApplyToEitherAsync2E() {
2610 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2611 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2612 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2613 <        f.completeExceptionally(new CFException());
2614 <        checkCompletedWithWrappedCFException(g);
2615 <
2616 <        f = new CompletableFuture<>();
2617 <        f2 = new CompletableFuture<>();
2618 <        f2.completeExceptionally(new CFException());
2619 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2620 <        f.complete(one);
2621 <        checkCompletedWithWrappedCFException(g);
2622 <    }
2623 <
2624 <    /**
2625 <     * applyToEitherAsync result completes exceptionally if action does
2626 <     */
2627 <    public void testApplyToEitherAsync3E() {
2628 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2629 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2630 <        FailingFunction r = new FailingFunction();
2631 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2632 <        f.complete(one);
2633 <        checkCompletedWithWrappedCFException(g);
2634 <    }
2635 <
2636 <    /**
2637 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2638 <     */
2639 <    public void testApplyToEitherAsync4E() {
2640 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2641 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2642 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2643 <        assertTrue(f.cancel(true));
2644 <        checkCompletedWithWrappedCancellationException(g);
2645 <
2646 <        f = new CompletableFuture<>();
2647 <        f2 = new CompletableFuture<>();
2648 <        assertTrue(f2.cancel(true));
2649 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2650 <        checkCompletedWithWrappedCancellationException(g);
2651 <    }
2652 <
2653 <    /**
2654 <     * acceptEitherAsync result completes normally after normal
2655 <     * completion of sources
2656 <     */
2657 <    public void testAcceptEitherAsyncE() {
2658 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2659 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2660 <        IncAction r = new IncAction();
2661 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2662 <        f.complete(one);
2663 <        checkCompletedNormally(g, null);
2664 <        assertEquals(r.value, 2);
2665 <
2666 <        r = new IncAction();
2667 <        f = new CompletableFuture<>();
2668 <        f.complete(one);
2669 <        f2 = new CompletableFuture<>();
2670 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2671 <        checkCompletedNormally(g, null);
2672 <        assertEquals(r.value, 2);
2673 <    }
2674 <
2675 <    /**
2676 <     * acceptEitherAsync result completes exceptionally after exceptional
2677 <     * completion of source
2678 <     */
2679 <    public void testAcceptEitherAsync2E() {
2680 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2681 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2682 <        IncAction r = new IncAction();
2683 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2684 <        f.completeExceptionally(new CFException());
2685 <        checkCompletedWithWrappedCFException(g);
2686 <
2687 <        r = new IncAction();
2688 <        f = new CompletableFuture<>();
2689 <        f2 = new CompletableFuture<>();
2690 <        f2.completeExceptionally(new CFException());
2691 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2692 <        f.complete(one);
2693 <        checkCompletedWithWrappedCFException(g);
2694 <    }
2695 <
2696 <    /**
2697 <     * acceptEitherAsync result completes exceptionally if action does
2698 <     */
2699 <    public void testAcceptEitherAsync3E() {
2700 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2701 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2702 <        FailingConsumer r = new FailingConsumer();
2703 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2704 <        f.complete(one);
2705 <        checkCompletedWithWrappedCFException(g);
2706 <    }
2707 <
2708 <    /**
2709 <     * acceptEitherAsync result completes exceptionally if either
2710 <     * source cancelled
2711 <     */
2712 <    public void testAcceptEitherAsync4E() {
2713 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2714 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2715 <        IncAction r = new IncAction();
2716 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2717 <        assertTrue(f.cancel(true));
2718 <        checkCompletedWithWrappedCancellationException(g);
2719 <
2720 <        r = new IncAction();
2721 <        f = new CompletableFuture<>();
2722 <        f2 = new CompletableFuture<>();
2723 <        assertTrue(f2.cancel(true));
2724 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2725 <        checkCompletedWithWrappedCancellationException(g);
3627 >    Object resultOf(CompletableFuture<?> f) {
3628 >        try {
3629 >            java.lang.reflect.Field resultField
3630 >                = CompletableFuture.class.getDeclaredField("result");
3631 >            resultField.setAccessible(true);
3632 >            return resultField.get(f);
3633 >        } catch (Throwable t) { throw new AssertionError(t); }
3634      }
3635  
3636 <    /**
3637 <     * runAfterEitherAsync result completes normally after normal
3638 <     * completion of sources
3639 <     */
3640 <    public void testRunAfterEitherAsyncE() {
3641 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3642 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2735 <        Noop r = new Noop();
2736 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2737 <        f.complete(one);
2738 <        checkCompletedNormally(g, null);
2739 <        assertTrue(r.ran);
2740 <
2741 <        r = new Noop();
2742 <        f = new CompletableFuture<>();
2743 <        f.complete(one);
2744 <        f2 = new CompletableFuture<>();
2745 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2746 <        checkCompletedNormally(g, null);
2747 <        assertTrue(r.ran);
2748 <    }
3636 >    public void testExceptionPropagationReusesResultObject() {
3637 >        if (!testImplementationDetails) return;
3638 >        for (ExecutionMode m : ExecutionMode.values())
3639 >    {
3640 >        final CFException ex = new CFException();
3641 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3642 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3643  
3644 <    /**
3645 <     * runAfterEitherAsync result completes exceptionally after exceptional
2752 <     * completion of source
2753 <     */
2754 <    public void testRunAfterEitherAsync2E() {
2755 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2756 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2757 <        Noop r = new Noop();
2758 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2759 <        f.completeExceptionally(new CFException());
2760 <        checkCompletedWithWrappedCFException(g);
3644 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3645 >            = new ArrayList<>();
3646  
3647 <        r = new Noop();
3648 <        f = new CompletableFuture<>();
3649 <        f2 = new CompletableFuture<>();
2765 <        f2.completeExceptionally(new CFException());
2766 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2767 <        f.complete(one);
2768 <        checkCompletedWithWrappedCFException(g);
2769 <    }
3647 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3648 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3649 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3650  
3651 <    /**
3652 <     * runAfterEitherAsync result completes exceptionally if action does
3653 <     */
2774 <    public void testRunAfterEitherAsync3E() {
2775 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2776 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2777 <        FailingNoop r = new FailingNoop();
2778 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2779 <        f.complete(one);
2780 <        checkCompletedWithWrappedCFException(g);
2781 <    }
3651 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3652 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3653 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3654  
3655 <    /**
3656 <     * runAfterEitherAsync result completes exceptionally if either
3657 <     * source cancelled
2786 <     */
2787 <    public void testRunAfterEitherAsync4E() {
2788 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2789 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2790 <        Noop r = new Noop();
2791 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2792 <        assertTrue(f.cancel(true));
2793 <        checkCompletedWithWrappedCancellationException(g);
3655 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3656 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3657 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3658  
3659 <        r = new Noop();
2796 <        f = new CompletableFuture<>();
2797 <        f2 = new CompletableFuture<>();
2798 <        assertTrue(f2.cancel(true));
2799 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2800 <        checkCompletedWithWrappedCancellationException(g);
2801 <    }
3659 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3660  
3661 <    /**
2804 <     * thenComposeAsync result completes normally after normal
2805 <     * completion of source
2806 <     */
2807 <    public void testThenComposeAsyncE() {
2808 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2809 <        CompletableFutureInc r = new CompletableFutureInc();
2810 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2811 <        f.complete(one);
2812 <        checkCompletedNormally(g, two);
2813 <    }
3661 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3662  
3663 <    /**
3664 <     * thenComposeAsync result completes exceptionally after
2817 <     * exceptional completion of source
2818 <     */
2819 <    public void testThenComposeAsync2E() {
2820 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2821 <        CompletableFutureInc r = new CompletableFutureInc();
2822 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2823 <        f.completeExceptionally(new CFException());
2824 <        checkCompletedWithWrappedCFException(g);
2825 <    }
3663 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3664 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3665  
3666 <    /**
3667 <     * thenComposeAsync result completes exceptionally if action does
3668 <     */
3669 <    public void testThenComposeAsync3E() {
3670 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3671 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
3672 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3673 <        f.complete(one);
3674 <        checkCompletedWithWrappedCFException(g);
3675 <    }
3666 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3667 >                 fun : funs) {
3668 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3669 >            f.completeExceptionally(ex);
3670 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3671 >            checkCompletedWithWrappedException(src, ex);
3672 >            CompletableFuture<?> dep = fun.apply(src);
3673 >            checkCompletedWithWrappedException(dep, ex);
3674 >            assertSame(resultOf(src), resultOf(dep));
3675 >        }
3676  
3677 <    /**
3678 <     * thenComposeAsync result completes exceptionally if source cancelled
3679 <     */
3680 <    public void testThenComposeAsync4E() {
3681 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3682 <        CompletableFutureInc r = new CompletableFutureInc();
3683 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
3684 <        assertTrue(f.cancel(true));
3685 <        checkCompletedWithWrappedCancellationException(g);
3686 <    }
3677 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3678 >                 fun : funs) {
3679 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3680 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3681 >            CompletableFuture<?> dep = fun.apply(src);
3682 >            f.completeExceptionally(ex);
3683 >            checkCompletedWithWrappedException(src, ex);
3684 >            checkCompletedWithWrappedException(dep, ex);
3685 >            assertSame(resultOf(src), resultOf(dep));
3686 >        }
3687  
3688 <    // other static methods
3688 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3689 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3690 >                 fun : funs) {
3691 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3692 >            f.cancel(mayInterruptIfRunning);
3693 >            checkCancelled(f);
3694 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3695 >            checkCompletedWithWrappedCancellationException(src);
3696 >            CompletableFuture<?> dep = fun.apply(src);
3697 >            checkCompletedWithWrappedCancellationException(dep);
3698 >            assertSame(resultOf(src), resultOf(dep));
3699 >        }
3700  
3701 <    /**
3702 <     * allOf(no component futures) returns a future completed normally
3703 <     * with the value null
3704 <     */
3705 <    public void testAllOf_empty() throws Exception {
3706 <        CompletableFuture<Void> f = CompletableFuture.allOf();
3707 <        checkCompletedNormally(f, null);
3708 <    }
3701 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3702 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3703 >                 fun : funs) {
3704 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3705 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3706 >            CompletableFuture<?> dep = fun.apply(src);
3707 >            f.cancel(mayInterruptIfRunning);
3708 >            checkCancelled(f);
3709 >            checkCompletedWithWrappedCancellationException(src);
3710 >            checkCompletedWithWrappedCancellationException(dep);
3711 >            assertSame(resultOf(src), resultOf(dep));
3712 >        }
3713 >    }}
3714  
3715      /**
3716 <     * allOf returns a future completed normally with the value null
2862 <     * when all components complete normally
3716 >     * Minimal completion stages throw UOE for all non-CompletionStage methods
3717       */
3718 <    public void testAllOf_normal() throws Exception {
3719 <        for (int k = 1; k < 20; ++k) {
3720 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3721 <            for (int i = 0; i < k; ++i)
3722 <                fs[i] = new CompletableFuture<>();
3723 <            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3724 <            for (int i = 0; i < k; ++i) {
3725 <                checkIncomplete(f);
3726 <                checkIncomplete(CompletableFuture.allOf(fs));
3727 <                fs[i].complete(one);
3718 >    public void testMinimalCompletionStage_minimality() {
3719 >        if (!testImplementationDetails) return;
3720 >        Function<Method, String> toSignature =
3721 >            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3722 >        Predicate<Method> isNotStatic =
3723 >            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3724 >        List<Method> minimalMethods =
3725 >            Stream.of(Object.class, CompletionStage.class)
3726 >            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3727 >            .filter(isNotStatic)
3728 >            .collect(Collectors.toList());
3729 >        // Methods from CompletableFuture permitted NOT to throw UOE
3730 >        String[] signatureWhitelist = {
3731 >            "newIncompleteFuture[]",
3732 >            "defaultExecutor[]",
3733 >            "minimalCompletionStage[]",
3734 >            "copy[]",
3735 >        };
3736 >        Set<String> permittedMethodSignatures =
3737 >            Stream.concat(minimalMethods.stream().map(toSignature),
3738 >                          Stream.of(signatureWhitelist))
3739 >            .collect(Collectors.toSet());
3740 >        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3741 >            .filter(isNotStatic)
3742 >            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3743 >            .collect(Collectors.toList());
3744 >
3745 >        CompletionStage<Integer> minimalStage =
3746 >            new CompletableFuture<Integer>().minimalCompletionStage();
3747 >
3748 >        List<Method> bugs = new ArrayList<>();
3749 >        for (Method method : allMethods) {
3750 >            Class<?>[] parameterTypes = method.getParameterTypes();
3751 >            Object[] args = new Object[parameterTypes.length];
3752 >            // Manufacture boxed primitives for primitive params
3753 >            for (int i = 0; i < args.length; i++) {
3754 >                Class<?> type = parameterTypes[i];
3755 >                if (parameterTypes[i] == boolean.class)
3756 >                    args[i] = false;
3757 >                else if (parameterTypes[i] == int.class)
3758 >                    args[i] = 0;
3759 >                else if (parameterTypes[i] == long.class)
3760 >                    args[i] = 0L;
3761              }
3762 <            checkCompletedNormally(f, null);
3763 <            checkCompletedNormally(CompletableFuture.allOf(fs), null);
3762 >            try {
3763 >                method.invoke(minimalStage, args);
3764 >                bugs.add(method);
3765 >            }
3766 >            catch (java.lang.reflect.InvocationTargetException expected) {
3767 >                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3768 >                    bugs.add(method);
3769 >                    // expected.getCause().printStackTrace();
3770 >                }
3771 >            }
3772 >            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3773          }
3774 +        if (!bugs.isEmpty())
3775 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3776      }
3777  
3778 <    /**
3779 <     * anyOf(no component futures) returns an incomplete future
3780 <     */
3781 <    public void testAnyOf_empty() throws Exception {
3782 <        CompletableFuture<Object> f = CompletableFuture.anyOf();
3783 <        checkIncomplete(f);
3784 <    }
3785 <
3786 <    /**
3787 <     * anyOf returns a future completed normally with a value when
3788 <     * a component future does
3789 <     */
3790 <    public void testAnyOf_normal() throws Exception {
3791 <        for (int k = 0; k < 10; ++k) {
3792 <            CompletableFuture[] fs = new CompletableFuture[k];
3793 <            for (int i = 0; i < k; ++i)
3794 <                fs[i] = new CompletableFuture<>();
2897 <            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2898 <            checkIncomplete(f);
2899 <            for (int i = 0; i < k; ++i) {
2900 <                fs[i].complete(one);
2901 <                checkCompletedNormally(f, one);
2902 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2903 <            }
3778 >    static class Monad {
3779 >        static class ZeroException extends RuntimeException {
3780 >            public ZeroException() { super("monadic zero"); }
3781 >        }
3782 >        // "return", "unit"
3783 >        static <T> CompletableFuture<T> unit(T value) {
3784 >            return completedFuture(value);
3785 >        }
3786 >        // monadic zero ?
3787 >        static <T> CompletableFuture<T> zero() {
3788 >            return failedFuture(new ZeroException());
3789 >        }
3790 >        // >=>
3791 >        static <T,U,V> Function<T, CompletableFuture<V>> compose
3792 >            (Function<T, CompletableFuture<U>> f,
3793 >             Function<U, CompletableFuture<V>> g) {
3794 >            return (x) -> f.apply(x).thenCompose(g);
3795          }
2905    }
3796  
3797 <    /**
3798 <     * anyOf result completes exceptionally when any component does.
3799 <     */
3800 <    public void testAnyOf_exceptional() throws Exception {
3801 <        for (int k = 0; k < 10; ++k) {
3802 <            CompletableFuture[] fs = new CompletableFuture[k];
2913 <            for (int i = 0; i < k; ++i)
2914 <                fs[i] = new CompletableFuture<>();
2915 <            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2916 <            checkIncomplete(f);
2917 <            for (int i = 0; i < k; ++i) {
2918 <                fs[i].completeExceptionally(new CFException());
2919 <                checkCompletedWithWrappedCFException(f);
2920 <                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3797 >        static void assertZero(CompletableFuture<?> f) {
3798 >            try {
3799 >                f.getNow(null);
3800 >                throw new AssertionFailedError("should throw");
3801 >            } catch (CompletionException success) {
3802 >                assertTrue(success.getCause() instanceof ZeroException);
3803              }
3804          }
2923    }
2924
2925    /**
2926     * Completion methods throw NullPointerException with null arguments
2927     */
2928    public void testNPE() {
2929        CompletableFuture<Integer> f = new CompletableFuture<>();
2930        CompletableFuture<Integer> g = new CompletableFuture<>();
2931        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2932        CompletableFuture<?> h;
2933        ThreadExecutor exec = new ThreadExecutor();
2934
2935        Runnable[] throwingActions = {
2936            () -> { CompletableFuture.supplyAsync(null); },
2937            () -> { CompletableFuture.supplyAsync(null, exec); },
2938            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2939
2940            () -> { CompletableFuture.runAsync(null); },
2941            () -> { CompletableFuture.runAsync(null, exec); },
2942            () -> { CompletableFuture.runAsync(() -> {}, null); },
2943
2944            () -> { f.completeExceptionally(null); },
2945
2946            () -> { f.thenApply(null); },
2947            () -> { f.thenApplyAsync(null); },
2948            () -> { f.thenApplyAsync((x) -> x, null); },
2949            () -> { f.thenApplyAsync(null, exec); },
2950
2951            () -> { f.thenAccept(null); },
2952            () -> { f.thenAcceptAsync(null); },
2953            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2954            () -> { f.thenAcceptAsync(null, exec); },
2955
2956            () -> { f.thenRun(null); },
2957            () -> { f.thenRunAsync(null); },
2958            () -> { f.thenRunAsync(() -> { ; }, null); },
2959            () -> { f.thenRunAsync(null, exec); },
2960
2961            () -> { f.thenCombine(g, null); },
2962            () -> { f.thenCombineAsync(g, null); },
2963            () -> { f.thenCombineAsync(g, null, exec); },
2964            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2965            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2966            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2967            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2968
2969            () -> { f.thenAcceptBoth(g, null); },
2970            () -> { f.thenAcceptBothAsync(g, null); },
2971            () -> { f.thenAcceptBothAsync(g, null, exec); },
2972            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2973            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2974            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2975            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2976
2977            () -> { f.runAfterBoth(g, null); },
2978            () -> { f.runAfterBothAsync(g, null); },
2979            () -> { f.runAfterBothAsync(g, null, exec); },
2980            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2981            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2982            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2983            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2984
2985            () -> { f.applyToEither(g, null); },
2986            () -> { f.applyToEitherAsync(g, null); },
2987            () -> { f.applyToEitherAsync(g, null, exec); },
2988            () -> { f.applyToEither(nullFuture, (x) -> x); },
2989            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2990            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2991            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2992
2993            () -> { f.acceptEither(g, null); },
2994            () -> { f.acceptEitherAsync(g, null); },
2995            () -> { f.acceptEitherAsync(g, null, exec); },
2996            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2997            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2998            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2999            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
3000
3001            () -> { f.runAfterEither(g, null); },
3002            () -> { f.runAfterEitherAsync(g, null); },
3003            () -> { f.runAfterEitherAsync(g, null, exec); },
3004            () -> { f.runAfterEither(nullFuture, () -> {}); },
3005            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
3006            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
3007            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
3008
3009            () -> { f.thenCompose(null); },
3010            () -> { f.thenComposeAsync(null); },
3011            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
3012            () -> { f.thenComposeAsync(null, exec); },
3013
3014            () -> { f.exceptionally(null); },
3015
3016            () -> { f.handle(null); },
3017
3018            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
3019            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
3020            () -> { CompletableFuture.allOf(f, null); },
3021            () -> { CompletableFuture.allOf(null, f); },
3022
3023            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
3024            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
3025            () -> { CompletableFuture.anyOf(f, null); },
3026            () -> { CompletableFuture.anyOf(null, f); },
3027        };
3028
3029        assertThrows(NullPointerException.class, throwingActions);
3030        assertEquals(0, exec.count.get());
3031    }
3032
3033    /**
3034     * toCompletableFuture returns this CompletableFuture.
3035     */
3036    public void testToCompletableFuture() {
3037        CompletableFuture<Integer> f = new CompletableFuture<>();
3038        assertSame(f, f.toCompletableFuture());
3039    }
3040
3041    /**
3042     * whenComplete action executes on normal completion, propagating
3043     * source result.
3044     */
3045    public void testWhenComplete1() {
3046        final AtomicInteger a = new AtomicInteger();
3047        CompletableFuture<Integer> f = new CompletableFuture<>();
3048        CompletableFuture<Integer> g =
3049            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3050        f.complete(three);
3051        checkCompletedNormally(f, three);
3052        checkCompletedNormally(g, three);
3053        assertEquals(a.get(), 1);
3054    }
3055
3056    /**
3057     * whenComplete action executes on exceptional completion, propagating
3058     * source result.
3059     */
3060    public void testWhenComplete2() {
3061        final AtomicInteger a = new AtomicInteger();
3062        CompletableFuture<Integer> f = new CompletableFuture<>();
3063        CompletableFuture<Integer> g =
3064            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3065        f.completeExceptionally(new CFException());
3066        assertTrue(f.isCompletedExceptionally());
3067        assertTrue(g.isCompletedExceptionally());
3068        assertEquals(a.get(), 1);
3069    }
3070
3071    /**
3072     * If a whenComplete action throws an exception when triggered by
3073     * a normal completion, it completes exceptionally
3074     */
3075    public void testWhenComplete3() {
3076        CompletableFuture<Integer> f = new CompletableFuture<>();
3077        CompletableFuture<Integer> g =
3078            f.whenComplete((Integer x, Throwable t) ->
3079                           { throw new CFException(); } );
3080        f.complete(three);
3081        checkCompletedNormally(f, three);
3082        assertTrue(g.isCompletedExceptionally());
3083        checkCompletedWithWrappedCFException(g);
3084    }
3085
3086    /**
3087     * whenCompleteAsync action executes on normal completion, propagating
3088     * source result.
3089     */
3090    public void testWhenCompleteAsync1() {
3091        final AtomicInteger a = new AtomicInteger();
3092        CompletableFuture<Integer> f = new CompletableFuture<>();
3093        CompletableFuture<Integer> g =
3094            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3095        f.complete(three);
3096        checkCompletedNormally(f, three);
3097        checkCompletedNormally(g, three);
3098        assertEquals(a.get(), 1);
3099    }
3100
3101    /**
3102     * whenCompleteAsync action executes on exceptional completion, propagating
3103     * source result.
3104     */
3105    public void testWhenCompleteAsync2() {
3106        final AtomicInteger a = new AtomicInteger();
3107        CompletableFuture<Integer> f = new CompletableFuture<>();
3108        CompletableFuture<Integer> g =
3109            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3110        f.completeExceptionally(new CFException());
3111        checkCompletedWithWrappedCFException(f);
3112        checkCompletedWithWrappedCFException(g);
3113    }
3114
3115    /**
3116     * If a whenCompleteAsync action throws an exception when
3117     * triggered by a normal completion, it completes exceptionally
3118     */
3119    public void testWhenCompleteAsync3() {
3120        CompletableFuture<Integer> f = new CompletableFuture<>();
3121        CompletableFuture<Integer> g =
3122            f.whenCompleteAsync((Integer x, Throwable t) ->
3123                           { throw new CFException(); } );
3124        f.complete(three);
3125        checkCompletedNormally(f, three);
3126        checkCompletedWithWrappedCFException(g);
3127    }
3128
3129    /**
3130     * whenCompleteAsync action executes on normal completion, propagating
3131     * source result.
3132     */
3133    public void testWhenCompleteAsync1e() {
3134        final AtomicInteger a = new AtomicInteger();
3135        ThreadExecutor exec = new ThreadExecutor();
3136        CompletableFuture<Integer> f = new CompletableFuture<>();
3137        CompletableFuture<Integer> g =
3138            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3139                                exec);
3140        f.complete(three);
3141        checkCompletedNormally(f, three);
3142        checkCompletedNormally(g, three);
3143        assertEquals(a.get(), 1);
3144    }
3145
3146    /**
3147     * whenCompleteAsync action executes on exceptional completion, propagating
3148     * source result.
3149     */
3150    public void testWhenCompleteAsync2e() {
3151        final AtomicInteger a = new AtomicInteger();
3152        ThreadExecutor exec = new ThreadExecutor();
3153        CompletableFuture<Integer> f = new CompletableFuture<>();
3154        CompletableFuture<Integer> g =
3155            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3156                                exec);
3157        f.completeExceptionally(new CFException());
3158        checkCompletedWithWrappedCFException(f);
3159        checkCompletedWithWrappedCFException(g);
3160    }
3805  
3806 <    /**
3807 <     * If a whenCompleteAsync action throws an exception when triggered
3808 <     * by a normal completion, it completes exceptionally
3809 <     */
3810 <    public void testWhenCompleteAsync3e() {
3811 <        ThreadExecutor exec = new ThreadExecutor();
3812 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3813 <        CompletableFuture<Integer> g =
3814 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3815 <                                { throw new CFException(); },
3816 <                                exec);
3817 <        f.complete(three);
3818 <        checkCompletedNormally(f, three);
3819 <        checkCompletedWithWrappedCFException(g);
3820 <    }
3821 <
3822 <    /**
3823 <     * handleAsync action completes normally with function value on
3180 <     * either normal or exceptional completion of source
3181 <     */
3182 <    public void testHandleAsync() {
3183 <        CompletableFuture<Integer> f, g;
3184 <        IntegerHandler r;
3185 <
3186 <        f = new CompletableFuture<>();
3187 <        g = f.handleAsync(r = new IntegerHandler());
3188 <        assertFalse(r.ran);
3189 <        f.completeExceptionally(new CFException());
3190 <        checkCompletedWithWrappedCFException(f);
3191 <        checkCompletedNormally(g, three);
3192 <        assertTrue(r.ran);
3193 <
3194 <        f = new CompletableFuture<>();
3195 <        g = f.handleAsync(r = new IntegerHandler());
3196 <        assertFalse(r.ran);
3197 <        f.completeExceptionally(new CFException());
3198 <        checkCompletedWithWrappedCFException(f);
3199 <        checkCompletedNormally(g, three);
3200 <        assertTrue(r.ran);
3806 >        static <T> void assertFutureEquals(CompletableFuture<T> f,
3807 >                                           CompletableFuture<T> g) {
3808 >            T fval = null, gval = null;
3809 >            Throwable fex = null, gex = null;
3810 >
3811 >            try { fval = f.get(); }
3812 >            catch (ExecutionException ex) { fex = ex.getCause(); }
3813 >            catch (Throwable ex) { fex = ex; }
3814 >
3815 >            try { gval = g.get(); }
3816 >            catch (ExecutionException ex) { gex = ex.getCause(); }
3817 >            catch (Throwable ex) { gex = ex; }
3818 >
3819 >            if (fex != null || gex != null)
3820 >                assertSame(fex.getClass(), gex.getClass());
3821 >            else
3822 >                assertEquals(fval, gval);
3823 >        }
3824  
3825 <        f = new CompletableFuture<>();
3826 <        g = f.handleAsync(r = new IntegerHandler());
3827 <        assertFalse(r.ran);
3205 <        f.complete(one);
3206 <        checkCompletedNormally(f, one);
3207 <        checkCompletedNormally(g, two);
3208 <        assertTrue(r.ran);
3825 >        static class PlusFuture<T> extends CompletableFuture<T> {
3826 >            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3827 >        }
3828  
3829 <        f = new CompletableFuture<>();
3830 <        g = f.handleAsync(r = new IntegerHandler());
3831 <        assertFalse(r.ran);
3832 <        f.complete(one);
3833 <        checkCompletedNormally(f, one);
3834 <        checkCompletedNormally(g, two);
3835 <        assertTrue(r.ran);
3829 >        /** Implements "monadic plus". */
3830 >        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3831 >                                             CompletableFuture<? extends T> g) {
3832 >            PlusFuture<T> plus = new PlusFuture<T>();
3833 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3834 >                try {
3835 >                    if (ex == null) {
3836 >                        if (plus.complete(result))
3837 >                            if (plus.firstFailure.get() != null)
3838 >                                plus.firstFailure.set(null);
3839 >                    }
3840 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3841 >                        if (plus.isDone())
3842 >                            plus.firstFailure.set(null);
3843 >                    }
3844 >                    else {
3845 >                        // first failure has precedence
3846 >                        Throwable first = plus.firstFailure.getAndSet(null);
3847 >
3848 >                        // may fail with "Self-suppression not permitted"
3849 >                        try { first.addSuppressed(ex); }
3850 >                        catch (Exception ignored) {}
3851 >
3852 >                        plus.completeExceptionally(first);
3853 >                    }
3854 >                } catch (Throwable unexpected) {
3855 >                    plus.completeExceptionally(unexpected);
3856 >                }
3857 >            };
3858 >            f.whenComplete(action);
3859 >            g.whenComplete(action);
3860 >            return plus;
3861 >        }
3862      }
3863  
3864      /**
3865 <     * handleAsync action with Executor completes normally with
3866 <     * function value on either normal or exceptional completion of
3222 <     * source
3865 >     * CompletableFuture is an additive monad - sort of.
3866 >     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3867       */
3868 <    public void testHandleAsync2() {
3869 <        CompletableFuture<Integer> f, g;
3870 <        ThreadExecutor exec = new ThreadExecutor();
3871 <        IntegerHandler r;
3872 <
3873 <        f = new CompletableFuture<>();
3874 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3875 <        assertFalse(r.ran);
3876 <        f.completeExceptionally(new CFException());
3877 <        checkCompletedWithWrappedCFException(f);
3878 <        checkCompletedNormally(g, three);
3879 <        assertTrue(r.ran);
3880 <
3881 <        f = new CompletableFuture<>();
3882 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3883 <        assertFalse(r.ran);
3884 <        f.completeExceptionally(new CFException());
3885 <        checkCompletedWithWrappedCFException(f);
3886 <        checkCompletedNormally(g, three);
3887 <        assertTrue(r.ran);
3888 <
3889 <        f = new CompletableFuture<>();
3890 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3891 <        assertFalse(r.ran);
3892 <        f.complete(one);
3893 <        checkCompletedNormally(f, one);
3894 <        checkCompletedNormally(g, two);
3895 <        assertTrue(r.ran);
3868 >    public void testAdditiveMonad() throws Throwable {
3869 >        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3870 >        CompletableFuture<Long> zero = Monad.zero();
3871 >
3872 >        // Some mutually non-commutative functions
3873 >        Function<Long, CompletableFuture<Long>> triple
3874 >            = (x) -> Monad.unit(3 * x);
3875 >        Function<Long, CompletableFuture<Long>> inc
3876 >            = (x) -> Monad.unit(x + 1);
3877 >
3878 >        // unit is a right identity: m >>= unit === m
3879 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3880 >                                 inc.apply(5L));
3881 >        // unit is a left identity: (unit x) >>= f === f x
3882 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3883 >                                 inc.apply(5L));
3884 >
3885 >        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3886 >        Monad.assertFutureEquals(
3887 >            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3888 >            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3889 >
3890 >        // The case for CompletableFuture as an additive monad is weaker...
3891 >
3892 >        // zero is a monadic zero
3893 >        Monad.assertZero(zero);
3894 >
3895 >        // left zero: zero >>= f === zero
3896 >        Monad.assertZero(zero.thenCompose(inc));
3897 >        // right zero: f >>= (\x -> zero) === zero
3898 >        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3899 >
3900 >        // f plus zero === f
3901 >        Monad.assertFutureEquals(Monad.unit(5L),
3902 >                                 Monad.plus(Monad.unit(5L), zero));
3903 >        // zero plus f === f
3904 >        Monad.assertFutureEquals(Monad.unit(5L),
3905 >                                 Monad.plus(zero, Monad.unit(5L)));
3906 >        // zero plus zero === zero
3907 >        Monad.assertZero(Monad.plus(zero, zero));
3908 >        {
3909 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3910 >                                                   Monad.unit(8L));
3911 >            // non-determinism
3912 >            assertTrue(f.get() == 5L || f.get() == 8L);
3913 >        }
3914  
3915 <        f = new CompletableFuture<>();
3916 <        g = f.handleAsync(r = new IntegerHandler(), exec);
3917 <        assertFalse(r.ran);
3918 <        f.complete(one);
3919 <        checkCompletedNormally(f, one);
3920 <        checkCompletedNormally(g, two);
3921 <        assertTrue(r.ran);
3922 <    }
3915 >        CompletableFuture<Long> godot = new CompletableFuture<>();
3916 >        // f plus godot === f (doesn't wait for godot)
3917 >        Monad.assertFutureEquals(Monad.unit(5L),
3918 >                                 Monad.plus(Monad.unit(5L), godot));
3919 >        // godot plus f === f (doesn't wait for godot)
3920 >        Monad.assertFutureEquals(Monad.unit(5L),
3921 >                                 Monad.plus(godot, Monad.unit(5L)));
3922 >    }
3923 >
3924 > //     static <U> U join(CompletionStage<U> stage) {
3925 > //         CompletableFuture<U> f = new CompletableFuture<>();
3926 > //         stage.whenComplete((v, ex) -> {
3927 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3928 > //         });
3929 > //         return f.join();
3930 > //     }
3931 >
3932 > //     static <U> boolean isDone(CompletionStage<U> stage) {
3933 > //         CompletableFuture<U> f = new CompletableFuture<>();
3934 > //         stage.whenComplete((v, ex) -> {
3935 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3936 > //         });
3937 > //         return f.isDone();
3938 > //     }
3939 >
3940 > //     static <U> U join2(CompletionStage<U> stage) {
3941 > //         return stage.toCompletableFuture().copy().join();
3942 > //     }
3943 >
3944 > //     static <U> boolean isDone2(CompletionStage<U> stage) {
3945 > //         return stage.toCompletableFuture().copy().isDone();
3946 > //     }
3947  
3948   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines