ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.28 by jsr166, Sun Jul 14 16:34:49 2013 UTC vs.
Revision 1.142 by dl, Fri Apr 1 23:18:00 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines