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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.33 by jsr166, Sun Jun 1 20:40:13 2014 UTC vs.
Revision 1.193 by jsr166, Tue Jan 30 04:07:09 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines