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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines