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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines