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.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC vs.
Revision 1.122 by jsr166, Sun Sep 6 22:21:07 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines