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.6 by jsr166, Fri Mar 22 16:10:19 2013 UTC vs.
Revision 1.56 by jsr166, Mon Jun 2 22:20:32 2014 UTC

# Line 16 | Line 16 | import java.util.concurrent.ExecutionExc
16   import java.util.concurrent.Future;
17   import java.util.concurrent.CompletableFuture;
18   import java.util.concurrent.CompletionException;
19 + import java.util.concurrent.CompletionStage;
20 + import java.util.concurrent.ForkJoinPool;
21 + import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.TimeoutException;
23   import java.util.concurrent.atomic.AtomicInteger;
24   import static java.util.concurrent.TimeUnit.MILLISECONDS;
# Line 53 | Line 56 | public class CompletableFutureTest exten
56          catch (Throwable fail) { threadUnexpectedException(fail); }
57      }
58  
59 <    void checkCompletedNormally(CompletableFuture<?> f, Object value) {
59 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
60 >        try {
61 >            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
63          try {
64              assertEquals(value, f.join());
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 63 | Line 69 | public class CompletableFutureTest exten
69          try {
70              assertEquals(value, f.get());
71          } catch (Throwable fail) { threadUnexpectedException(fail); }
66        try {
67            assertEquals(value, f.get(0L, SECONDS));
68        } catch (Throwable fail) { threadUnexpectedException(fail); }
72          assertTrue(f.isDone());
73          assertFalse(f.isCancelled());
74 +        assertFalse(f.isCompletedExceptionally());
75          assertTrue(f.toString().contains("[Completed normally]"));
76      }
77  
78      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
79          try {
80 +            f.get(LONG_DELAY_MS, MILLISECONDS);
81 +            shouldThrow();
82 +        } catch (ExecutionException success) {
83 +            assertTrue(success.getCause() instanceof CFException);
84 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
85 +        try {
86              f.join();
87              shouldThrow();
88 <        } catch (Throwable ex) {
89 <            assertTrue(ex instanceof CompletionException &&
80 <                       ((CompletionException)ex).getCause() instanceof CFException);
88 >        } catch (CompletionException success) {
89 >            assertTrue(success.getCause() instanceof CFException);
90          }
91          try {
92              f.getNow(null);
93              shouldThrow();
94 <        } catch (Throwable ex) {
95 <            assertTrue(ex instanceof CompletionException &&
87 <                       ((CompletionException)ex).getCause() instanceof CFException);
94 >        } catch (CompletionException success) {
95 >            assertTrue(success.getCause() instanceof CFException);
96          }
97          try {
98              f.get();
99              shouldThrow();
100 <        } catch (Throwable ex) {
101 <            assertTrue(ex instanceof ExecutionException &&
102 <                       ((ExecutionException)ex).getCause() instanceof CFException);
100 >        } catch (ExecutionException success) {
101 >            assertTrue(success.getCause() instanceof CFException);
102 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
103 >        assertTrue(f.isDone());
104 >        assertFalse(f.isCancelled());
105 >        assertTrue(f.toString().contains("[Completed exceptionally]"));
106 >    }
107 >
108 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 >                                              CFException ex) {
110 >        try {
111 >            f.get(LONG_DELAY_MS, MILLISECONDS);
112 >            shouldThrow();
113 >        } catch (ExecutionException success) {
114 >            assertSame(ex, success.getCause());
115 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
116 >        try {
117 >            f.join();
118 >            shouldThrow();
119 >        } catch (CompletionException success) {
120 >            assertSame(ex, success.getCause());
121          }
122          try {
123 <            f.get(0L, SECONDS);
123 >            f.getNow(null);
124              shouldThrow();
125 <        } catch (Throwable ex) {
126 <            assertTrue(ex instanceof ExecutionException &&
101 <                       ((ExecutionException)ex).getCause() instanceof CFException);
125 >        } catch (CompletionException success) {
126 >            assertSame(ex, success.getCause());
127          }
128 +        try {
129 +            f.get();
130 +            shouldThrow();
131 +        } catch (ExecutionException success) {
132 +            assertSame(ex, success.getCause());
133 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
134          assertTrue(f.isDone());
135          assertFalse(f.isCancelled());
136 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
137      }
138  
139      void checkCancelled(CompletableFuture<?> f) {
140          try {
141 +            f.get(LONG_DELAY_MS, MILLISECONDS);
142 +            shouldThrow();
143 +        } catch (CancellationException success) {
144 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
145 +        try {
146              f.join();
147              shouldThrow();
148 <        } catch (Throwable ex) {
112 <            assertTrue(ex instanceof CancellationException);
113 <        }
148 >        } catch (CancellationException success) {}
149          try {
150              f.getNow(null);
151              shouldThrow();
152 <        } catch (Throwable ex) {
118 <            assertTrue(ex instanceof CancellationException);
119 <        }
152 >        } catch (CancellationException success) {}
153          try {
154              f.get();
155              shouldThrow();
156 <        } catch (Throwable ex) {
157 <            assertTrue(ex instanceof CancellationException);
125 <        }
126 <        try {
127 <            f.get(0L, SECONDS);
128 <            shouldThrow();
129 <        } catch (Throwable ex) {
130 <            assertTrue(ex instanceof CancellationException);
131 <        }
156 >        } catch (CancellationException success) {
157 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
158          assertTrue(f.isDone());
159 +        assertTrue(f.isCompletedExceptionally());
160          assertTrue(f.isCancelled());
161 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
162      }
163  
164      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
165          try {
166 +            f.get(LONG_DELAY_MS, MILLISECONDS);
167 +            shouldThrow();
168 +        } catch (ExecutionException success) {
169 +            assertTrue(success.getCause() instanceof CancellationException);
170 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
171 +        try {
172              f.join();
173              shouldThrow();
174 <        } catch (Throwable ex) {
175 <            assertTrue(ex instanceof CompletionException &&
142 <                       ((CompletionException)ex).getCause() instanceof CancellationException);
174 >        } catch (CompletionException success) {
175 >            assertTrue(success.getCause() instanceof CancellationException);
176          }
177          try {
178              f.getNow(null);
179              shouldThrow();
180 <        } catch (Throwable ex) {
181 <            assertTrue(ex instanceof CompletionException &&
149 <                       ((CompletionException)ex).getCause() instanceof CancellationException);
180 >        } catch (CompletionException success) {
181 >            assertTrue(success.getCause() instanceof CancellationException);
182          }
183          try {
184              f.get();
185              shouldThrow();
186 <        } catch (Throwable ex) {
187 <            assertTrue(ex instanceof ExecutionException &&
188 <                       ((ExecutionException)ex).getCause() instanceof CancellationException);
157 <        }
158 <        try {
159 <            f.get(0L, SECONDS);
160 <            shouldThrow();
161 <        } catch (Throwable ex) {
162 <            assertTrue(ex instanceof ExecutionException &&
163 <                       ((ExecutionException)ex).getCause() instanceof CancellationException);
164 <        }
186 >        } catch (ExecutionException success) {
187 >            assertTrue(success.getCause() instanceof CancellationException);
188 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
189          assertTrue(f.isDone());
190          assertFalse(f.isCancelled());
191 +        assertTrue(f.isCompletedExceptionally());
192 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
193      }
194  
195      /**
# Line 171 | Line 197 | public class CompletableFutureTest exten
197       * by methods isDone, isCancelled, and getNow
198       */
199      public void testConstructor() {
200 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
200 >        CompletableFuture<Integer> f = new CompletableFuture<>();
201          checkIncomplete(f);
202      }
203  
# Line 180 | Line 206 | public class CompletableFutureTest exten
206       * isCancelled, join, get, and getNow
207       */
208      public void testComplete() {
209 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
209 >        CompletableFuture<Integer> f = new CompletableFuture<>();
210          checkIncomplete(f);
211          f.complete(one);
212          checkCompletedNormally(f, one);
# Line 191 | Line 217 | public class CompletableFutureTest exten
217       * methods isDone, isCancelled, join, get, and getNow
218       */
219      public void testCompleteExceptionally() {
220 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
220 >        CompletableFuture<Integer> f = new CompletableFuture<>();
221          checkIncomplete(f);
222          f.completeExceptionally(new CFException());
223          checkCompletedWithWrappedCFException(f);
# Line 202 | Line 228 | public class CompletableFutureTest exten
228       * methods isDone, isCancelled, join, get, and getNow
229       */
230      public void testCancel() {
231 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
231 >        CompletableFuture<Integer> f = new CompletableFuture<>();
232          checkIncomplete(f);
233          assertTrue(f.cancel(true));
234          checkCancelled(f);
# Line 212 | Line 238 | public class CompletableFutureTest exten
238       * obtrudeValue forces completion with given value
239       */
240      public void testObtrudeValue() {
241 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
241 >        CompletableFuture<Integer> f = new CompletableFuture<>();
242          checkIncomplete(f);
243          f.complete(one);
244          checkCompletedNormally(f, one);
# Line 220 | Line 246 | public class CompletableFutureTest exten
246          checkCompletedNormally(f, three);
247          f.obtrudeValue(two);
248          checkCompletedNormally(f, two);
249 <        f = new CompletableFuture<Integer>();
249 >        f = new CompletableFuture<>();
250          f.obtrudeValue(three);
251          checkCompletedNormally(f, three);
252 <        f = new CompletableFuture<Integer>();
252 >        f.obtrudeValue(null);
253 >        checkCompletedNormally(f, null);
254 >        f = new CompletableFuture<>();
255          f.completeExceptionally(new CFException());
256          f.obtrudeValue(four);
257          checkCompletedNormally(f, four);
# Line 233 | Line 261 | public class CompletableFutureTest exten
261       * obtrudeException forces completion with given exception
262       */
263      public void testObtrudeException() {
264 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
264 >        CompletableFuture<Integer> f = new CompletableFuture<>();
265          checkIncomplete(f);
266          f.complete(one);
267          checkCompletedNormally(f, one);
268          f.obtrudeException(new CFException());
269          checkCompletedWithWrappedCFException(f);
270 <        f = new CompletableFuture<Integer>();
270 >        f = new CompletableFuture<>();
271          f.obtrudeException(new CFException());
272          checkCompletedWithWrappedCFException(f);
273 <        f = new CompletableFuture<Integer>();
273 >        f = new CompletableFuture<>();
274          f.completeExceptionally(new CFException());
275          f.obtrudeValue(four);
276          checkCompletedNormally(f, four);
# Line 254 | Line 282 | public class CompletableFutureTest exten
282       * getNumberOfDependents returns number of dependent tasks
283       */
284      public void testGetNumberOfDependents() {
285 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
286 <        assertEquals(f.getNumberOfDependents(), 0);
287 <        CompletableFuture g = f.thenRun(new Noop());
288 <        assertEquals(f.getNumberOfDependents(), 1);
289 <        assertEquals(g.getNumberOfDependents(), 0);
290 <        CompletableFuture h = f.thenRun(new Noop());
291 <        assertEquals(f.getNumberOfDependents(), 2);
285 >        CompletableFuture<Integer> f = new CompletableFuture<>();
286 >        assertEquals(0, f.getNumberOfDependents());
287 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
288 >        assertEquals(1, f.getNumberOfDependents());
289 >        assertEquals(0, g.getNumberOfDependents());
290 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
291 >        assertEquals(2, f.getNumberOfDependents());
292          f.complete(1);
293          checkCompletedNormally(g, null);
294 <        assertEquals(f.getNumberOfDependents(), 0);
295 <        assertEquals(g.getNumberOfDependents(), 0);
294 >        assertEquals(0, f.getNumberOfDependents());
295 >        assertEquals(0, g.getNumberOfDependents());
296      }
297  
270
298      /**
299       * toString indicates current completion state
300       */
# Line 285 | Line 312 | public class CompletableFutureTest exten
312          assertTrue(f.toString().contains("[Completed exceptionally]"));
313      }
314  
315 +    /**
316 +     * completedFuture returns a completed CompletableFuture with given value
317 +     */
318 +    public void testCompletedFuture() {
319 +        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
320 +        checkCompletedNormally(f, "test");
321 +    }
322 +
323 +    // Choose non-commutative actions for better coverage
324 +
325 +    // A non-commutative function that handles and produces null values as well.
326 +    static Integer subtract(Integer x, Integer y) {
327 +        return (x == null && y == null) ? null :
328 +            ((x == null) ? 42 : x.intValue())
329 +            - ((y == null) ? 99 : y.intValue());
330 +    }
331 +
332 +    // A function that handles and produces null values as well.
333 +    static Integer inc(Integer x) {
334 +        return (x == null) ? null : x + 1;
335 +    }
336 +
337      static final Supplier<Integer> supplyOne =
338          () -> Integer.valueOf(1);
339      static final Function<Integer, Integer> inc =
340          (Integer x) -> Integer.valueOf(x.intValue() + 1);
341 <    static final BiFunction<Integer, Integer, Integer> add =
342 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue());
341 >    static final BiFunction<Integer, Integer, Integer> subtract =
342 >        (Integer x, Integer y) -> subtract(x, y);
343      static final class IncAction implements Consumer<Integer> {
344 <        int value;
345 <        public void accept(Integer x) { value = x.intValue() + 1; }
346 <    }
347 <    static final class AddAction implements BiConsumer<Integer, Integer> {
348 <        int value;
344 >        int invocationCount = 0;
345 >        Integer value;
346 >        public void accept(Integer x) {
347 >            invocationCount++;
348 >            value = inc(x);
349 >        }
350 >    }
351 >    static final class IncFunction implements Function<Integer,Integer> {
352 >        final ExecutionMode m;
353 >        int invocationCount = 0;
354 >        Integer value;
355 >        IncFunction(ExecutionMode m) { this.m = m; }
356 >        public Integer apply(Integer x) {
357 >            m.checkExecutionMode();
358 >            invocationCount++;
359 >            return value = inc(x);
360 >        }
361 >    }
362 >    static final class SubtractAction implements BiConsumer<Integer, Integer> {
363 >        final ExecutionMode m;
364 >        int invocationCount = 0;
365 >        Integer value;
366 >        // Check this action was invoked exactly once when result is computed.
367 >        SubtractAction(ExecutionMode m) { this.m = m; }
368          public void accept(Integer x, Integer y) {
369 <            value = x.intValue() + y.intValue();
369 >            m.checkExecutionMode();
370 >            invocationCount++;
371 >            value = subtract(x, y);
372 >        }
373 >    }
374 >    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
375 >        final ExecutionMode m;
376 >        int invocationCount = 0;
377 >        Integer value;
378 >        // Check this action was invoked exactly once when result is computed.
379 >        SubtractFunction(ExecutionMode m) { this.m = m; }
380 >        public Integer apply(Integer x, Integer y) {
381 >            m.checkExecutionMode();
382 >            invocationCount++;
383 >            return value = subtract(x, y);
384          }
385      }
386      static final class Noop implements Runnable {
387 <        boolean ran;
388 <        public void run() { ran = true; }
387 >        final ExecutionMode m;
388 >        int invocationCount = 0;
389 >        Noop(ExecutionMode m) { this.m = m; }
390 >        public void run() {
391 >            m.checkExecutionMode();
392 >            invocationCount++;
393 >        }
394      }
395  
396      static final class FailingSupplier implements Supplier<Integer> {
397 <        boolean ran;
398 <        public Integer get() { ran = true; throw new CFException(); }
397 >        final ExecutionMode m;
398 >        int invocationCount = 0;
399 >        FailingSupplier(ExecutionMode m) { this.m = m; }
400 >        public Integer get() {
401 >            m.checkExecutionMode();
402 >            invocationCount++;
403 >            throw new CFException();
404 >        }
405      }
406      static final class FailingConsumer implements Consumer<Integer> {
407 <        boolean ran;
408 <        public void accept(Integer x) { ran = true; throw new CFException(); }
407 >        final ExecutionMode m;
408 >        int invocationCount = 0;
409 >        FailingConsumer(ExecutionMode m) { this.m = m; }
410 >        public void accept(Integer x) {
411 >            m.checkExecutionMode();
412 >            invocationCount++;
413 >            throw new CFException();
414 >        }
415      }
416      static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
417 <        boolean ran;
418 <        public void accept(Integer x, Integer y) { ran = true; throw new CFException(); }
417 >        final ExecutionMode m;
418 >        int invocationCount = 0;
419 >        FailingBiConsumer(ExecutionMode m) { this.m = m; }
420 >        public void accept(Integer x, Integer y) {
421 >            m.checkExecutionMode();
422 >            invocationCount++;
423 >            throw new CFException();
424 >        }
425      }
426      static final class FailingFunction implements Function<Integer, Integer> {
427 <        boolean ran;
428 <        public Integer apply(Integer x) { ran = true; throw new CFException(); }
427 >        final ExecutionMode m;
428 >        int invocationCount = 0;
429 >        FailingFunction(ExecutionMode m) { this.m = m; }
430 >        public Integer apply(Integer x) {
431 >            m.checkExecutionMode();
432 >            invocationCount++;
433 >            throw new CFException();
434 >        }
435      }
436      static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
437 <        boolean ran;
438 <        public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); }
439 <    }
440 <    static final class FailingNoop implements Runnable {
441 <        boolean ran;
442 <        public void run() { ran = true; throw new CFException(); }
437 >        final ExecutionMode m;
438 >        int invocationCount = 0;
439 >        FailingBiFunction(ExecutionMode m) { this.m = m; }
440 >        public Integer apply(Integer x, Integer y) {
441 >            m.checkExecutionMode();
442 >            invocationCount++;
443 >            throw new CFException();
444 >        }
445 >    }
446 >    static final class FailingRunnable implements Runnable {
447 >        final ExecutionMode m;
448 >        int invocationCount = 0;
449 >        FailingRunnable(ExecutionMode m) { this.m = m; }
450 >        public void run() {
451 >            m.checkExecutionMode();
452 >            invocationCount++;
453 >            throw new CFException();
454 >        }
455      }
456  
457 <    static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
457 >    static final class CompletableFutureInc
458 >        implements Function<Integer, CompletableFuture<Integer>> {
459 >        final ExecutionMode m;
460 >        int invocationCount = 0;
461 >        CompletableFutureInc(ExecutionMode m) { this.m = m; }
462          public CompletableFuture<Integer> apply(Integer x) {
463 <            CompletableFuture<Integer> f = new CompletableFuture<Integer>();
464 <            f.complete(Integer.valueOf(x.intValue() + 1));
463 >            m.checkExecutionMode();
464 >            invocationCount++;
465 >            CompletableFuture<Integer> f = new CompletableFuture<>();
466 >            f.complete(inc(x));
467              return f;
468          }
469      }
470  
471 <    static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
472 <        boolean ran;
471 >    static final class FailingCompletableFutureFunction
472 >        implements Function<Integer, CompletableFuture<Integer>> {
473 >        final ExecutionMode m;
474 >        int invocationCount = 0;
475 >        FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; }
476          public CompletableFuture<Integer> apply(Integer x) {
477 <            ran = true; throw new CFException();
477 >            m.checkExecutionMode();
478 >            invocationCount++;
479 >            throw new CFException();
480          }
481      }
482  
483      // Used for explicit executor tests
484      static final class ThreadExecutor implements Executor {
485 +        final AtomicInteger count = new AtomicInteger(0);
486 +        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
487 +        static boolean startedCurrentThread() {
488 +            return Thread.currentThread().getThreadGroup() == tg;
489 +        }
490 +
491          public void execute(Runnable r) {
492 <            new Thread(r).start();
492 >            count.getAndIncrement();
493 >            new Thread(tg, r).start();
494          }
495      }
496  
497 <    static final class ExceptionToInteger implements Function<Throwable, Integer> {
498 <        public Integer apply(Throwable x) { return Integer.valueOf(3); }
499 <    }
497 >    /**
498 >     * Permits the testing of parallel code for the 3 different
499 >     * execution modes without repeating all the testing code.
500 >     */
501 >    enum ExecutionMode {
502 >        DEFAULT {
503 >            public void checkExecutionMode() {
504 >                assertNull(ForkJoinTask.getPool());
505 >            }
506 >            public <T> CompletableFuture<Void> thenRun
507 >                (CompletableFuture<T> f, Runnable a) {
508 >                return f.thenRun(a);
509 >            }
510 >            public <T> CompletableFuture<Void> thenAccept
511 >                (CompletableFuture<T> f, Consumer<? super T> a) {
512 >                return f.thenAccept(a);
513 >            }
514 >            public <T,U> CompletableFuture<U> thenApply
515 >                (CompletableFuture<T> f, Function<? super T,U> a) {
516 >                return f.thenApply(a);
517 >            }
518 >            public <T,U> CompletableFuture<U> thenCompose
519 >                (CompletableFuture<T> f,
520 >                 Function<? super T,? extends CompletionStage<U>> a) {
521 >                return f.thenCompose(a);
522 >            }
523 >            public <T,U> CompletableFuture<U> handle
524 >                (CompletableFuture<T> f,
525 >                 BiFunction<? super T,Throwable,? extends U> a) {
526 >                return f.handle(a);
527 >            }
528 >            public <T> CompletableFuture<T> whenComplete
529 >                (CompletableFuture<T> f,
530 >                 BiConsumer<? super T,? super Throwable> a) {
531 >                return f.whenComplete(a);
532 >            }
533 >            public <T,U> CompletableFuture<Void> runAfterBoth
534 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
535 >                return f.runAfterBoth(g, a);
536 >            }
537 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
538 >                (CompletableFuture<T> f,
539 >                 CompletionStage<? extends U> g,
540 >                 BiConsumer<? super T,? super U> a) {
541 >                return f.thenAcceptBoth(g, a);
542 >            }
543 >            public <T,U,V> CompletableFuture<V> thenCombine
544 >                (CompletableFuture<T> f,
545 >                 CompletionStage<? extends U> g,
546 >                 BiFunction<? super T,? super U,? extends V> a) {
547 >                return f.thenCombine(g, a);
548 >            }
549 >            public <T> CompletableFuture<Void> runAfterEither
550 >                (CompletableFuture<T> f,
551 >                 CompletionStage<?> g,
552 >                 java.lang.Runnable a) {
553 >                return f.runAfterEither(g, a);
554 >            }
555 >            public <T> CompletableFuture<Void> acceptEither
556 >                (CompletableFuture<T> f,
557 >                 CompletionStage<? extends T> g,
558 >                 Consumer<? super T> a) {
559 >                return f.acceptEither(g, a);
560 >            }
561 >            public <T,U> CompletableFuture<U> applyToEither
562 >                (CompletableFuture<T> f,
563 >                 CompletionStage<? extends T> g,
564 >                 Function<? super T,U> a) {
565 >                return f.applyToEither(g, a);
566 >            }
567 >        },
568  
569 <    static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
570 <        public Integer apply(Integer x, Throwable t) {
571 <            return (t == null) ? two : three;
572 <        }
573 <    }
569 >        ASYNC {
570 >            public void checkExecutionMode() {
571 >                assertSame(ForkJoinPool.commonPool(),
572 >                           ForkJoinTask.getPool());
573 >            }
574 >            public <T> CompletableFuture<Void> thenRun
575 >                (CompletableFuture<T> f, Runnable a) {
576 >                return f.thenRunAsync(a);
577 >            }
578 >            public <T> CompletableFuture<Void> thenAccept
579 >                (CompletableFuture<T> f, Consumer<? super T> a) {
580 >                return f.thenAcceptAsync(a);
581 >            }
582 >            public <T,U> CompletableFuture<U> thenApply
583 >                (CompletableFuture<T> f, Function<? super T,U> a) {
584 >                return f.thenApplyAsync(a);
585 >            }
586 >            public <T,U> CompletableFuture<U> thenCompose
587 >                (CompletableFuture<T> f,
588 >                 Function<? super T,? extends CompletionStage<U>> a) {
589 >                return f.thenComposeAsync(a);
590 >            }
591 >            public <T,U> CompletableFuture<U> handle
592 >                (CompletableFuture<T> f,
593 >                 BiFunction<? super T,Throwable,? extends U> a) {
594 >                return f.handleAsync(a);
595 >            }
596 >            public <T> CompletableFuture<T> whenComplete
597 >                (CompletableFuture<T> f,
598 >                 BiConsumer<? super T,? super Throwable> a) {
599 >                return f.whenCompleteAsync(a);
600 >            }
601 >            public <T,U> CompletableFuture<Void> runAfterBoth
602 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
603 >                return f.runAfterBothAsync(g, a);
604 >            }
605 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
606 >                (CompletableFuture<T> f,
607 >                 CompletionStage<? extends U> g,
608 >                 BiConsumer<? super T,? super U> a) {
609 >                return f.thenAcceptBothAsync(g, a);
610 >            }
611 >            public <T,U,V> CompletableFuture<V> thenCombine
612 >                (CompletableFuture<T> f,
613 >                 CompletionStage<? extends U> g,
614 >                 BiFunction<? super T,? super U,? extends V> a) {
615 >                return f.thenCombineAsync(g, a);
616 >            }
617 >            public <T> CompletableFuture<Void> runAfterEither
618 >                (CompletableFuture<T> f,
619 >                 CompletionStage<?> g,
620 >                 java.lang.Runnable a) {
621 >                return f.runAfterEitherAsync(g, a);
622 >            }
623 >            public <T> CompletableFuture<Void> acceptEither
624 >                (CompletableFuture<T> f,
625 >                 CompletionStage<? extends T> g,
626 >                 Consumer<? super T> a) {
627 >                return f.acceptEitherAsync(g, a);
628 >            }
629 >            public <T,U> CompletableFuture<U> applyToEither
630 >                (CompletableFuture<T> f,
631 >                 CompletionStage<? extends T> g,
632 >                 Function<? super T,U> a) {
633 >                return f.applyToEitherAsync(g, a);
634 >            }
635 >        },
636 >
637 >        EXECUTOR {
638 >            public void checkExecutionMode() {
639 >                assertTrue(ThreadExecutor.startedCurrentThread());
640 >            }
641 >            public <T> CompletableFuture<Void> thenRun
642 >                (CompletableFuture<T> f, Runnable a) {
643 >                return f.thenRunAsync(a, new ThreadExecutor());
644 >            }
645 >            public <T> CompletableFuture<Void> thenAccept
646 >                (CompletableFuture<T> f, Consumer<? super T> a) {
647 >                return f.thenAcceptAsync(a, new ThreadExecutor());
648 >            }
649 >            public <T,U> CompletableFuture<U> thenApply
650 >                (CompletableFuture<T> f, Function<? super T,U> a) {
651 >                return f.thenApplyAsync(a, new ThreadExecutor());
652 >            }
653 >            public <T,U> CompletableFuture<U> thenCompose
654 >                (CompletableFuture<T> f,
655 >                 Function<? super T,? extends CompletionStage<U>> a) {
656 >                return f.thenComposeAsync(a, new ThreadExecutor());
657 >            }
658 >            public <T,U> CompletableFuture<U> handle
659 >                (CompletableFuture<T> f,
660 >                 BiFunction<? super T,Throwable,? extends U> a) {
661 >                return f.handleAsync(a, new ThreadExecutor());
662 >            }
663 >            public <T> CompletableFuture<T> whenComplete
664 >                (CompletableFuture<T> f,
665 >                 BiConsumer<? super T,? super Throwable> a) {
666 >                return f.whenCompleteAsync(a, new ThreadExecutor());
667 >            }
668 >            public <T,U> CompletableFuture<Void> runAfterBoth
669 >                (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) {
670 >                return f.runAfterBothAsync(g, a, new ThreadExecutor());
671 >            }
672 >            public <T,U> CompletableFuture<Void> thenAcceptBoth
673 >                (CompletableFuture<T> f,
674 >                 CompletionStage<? extends U> g,
675 >                 BiConsumer<? super T,? super U> a) {
676 >                return f.thenAcceptBothAsync(g, a, new ThreadExecutor());
677 >            }
678 >            public <T,U,V> CompletableFuture<V> thenCombine
679 >                (CompletableFuture<T> f,
680 >                 CompletionStage<? extends U> g,
681 >                 BiFunction<? super T,? super U,? extends V> a) {
682 >                return f.thenCombineAsync(g, a, new ThreadExecutor());
683 >            }
684 >            public <T> CompletableFuture<Void> runAfterEither
685 >                (CompletableFuture<T> f,
686 >                 CompletionStage<?> g,
687 >                 java.lang.Runnable a) {
688 >                return f.runAfterEitherAsync(g, a, new ThreadExecutor());
689 >            }
690 >            public <T> CompletableFuture<Void> acceptEither
691 >                (CompletableFuture<T> f,
692 >                 CompletionStage<? extends T> g,
693 >                 Consumer<? super T> a) {
694 >                return f.acceptEitherAsync(g, a, new ThreadExecutor());
695 >            }
696 >            public <T,U> CompletableFuture<U> applyToEither
697 >                (CompletableFuture<T> f,
698 >                 CompletionStage<? extends T> g,
699 >                 Function<? super T,U> a) {
700 >                return f.applyToEitherAsync(g, a, new ThreadExecutor());
701 >            }
702 >        };
703 >
704 >        public abstract void checkExecutionMode();
705 >        public abstract <T> CompletableFuture<Void> thenRun
706 >            (CompletableFuture<T> f, Runnable a);
707 >        public abstract <T> CompletableFuture<Void> thenAccept
708 >            (CompletableFuture<T> f, Consumer<? super T> a);
709 >        public abstract <T,U> CompletableFuture<U> thenApply
710 >            (CompletableFuture<T> f, Function<? super T,U> a);
711 >        public abstract <T,U> CompletableFuture<U> thenCompose
712 >            (CompletableFuture<T> f,
713 >             Function<? super T,? extends CompletionStage<U>> a);
714 >        public abstract <T,U> CompletableFuture<U> handle
715 >            (CompletableFuture<T> f,
716 >             BiFunction<? super T,Throwable,? extends U> a);
717 >        public abstract <T> CompletableFuture<T> whenComplete
718 >            (CompletableFuture<T> f,
719 >             BiConsumer<? super T,? super Throwable> a);
720 >        public abstract <T,U> CompletableFuture<Void> runAfterBoth
721 >            (CompletableFuture<T> f, CompletableFuture<U> g, Runnable a);
722 >        public abstract <T,U> CompletableFuture<Void> thenAcceptBoth
723 >            (CompletableFuture<T> f,
724 >             CompletionStage<? extends U> g,
725 >             BiConsumer<? super T,? super U> a);
726 >        public abstract <T,U,V> CompletableFuture<V> thenCombine
727 >            (CompletableFuture<T> f,
728 >             CompletionStage<? extends U> g,
729 >             BiFunction<? super T,? super U,? extends V> a);
730 >        public abstract <T> CompletableFuture<Void> runAfterEither
731 >            (CompletableFuture<T> f,
732 >             CompletionStage<?> g,
733 >             java.lang.Runnable a);
734 >        public abstract <T> CompletableFuture<Void> acceptEither
735 >            (CompletableFuture<T> f,
736 >             CompletionStage<? extends T> g,
737 >             Consumer<? super T> a);
738 >        public abstract <T,U> CompletableFuture<U> applyToEither
739 >            (CompletableFuture<T> f,
740 >             CompletionStage<? extends T> g,
741 >             Function<? super T,U> a);
742 >    }
743 >
744 >    /**
745 >     * exceptionally action is not invoked when source completes
746 >     * normally, and source result is propagated
747 >     */
748 >    public void testExceptionally_normalCompletion() {
749 >        for (boolean createIncomplete : new boolean[] { true, false })
750 >        for (Integer v1 : new Integer[] { 1, null })
751 >    {
752 >        final AtomicInteger a = new AtomicInteger(0);
753 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
754 >        if (!createIncomplete) f.complete(v1);
755 >        final CompletableFuture<Integer> g = f.exceptionally
756 >            ((Throwable t) -> {
757 >                // Should not be called
758 >                a.getAndIncrement();
759 >                throw new AssertionError();
760 >            });
761 >        if (createIncomplete) f.complete(v1);
762 >
763 >        checkCompletedNormally(g, v1);
764 >        checkCompletedNormally(f, v1);
765 >        assertEquals(0, a.get());
766 >    }}
767  
768  
769      /**
770       * exceptionally action completes with function value on source
771 <     * exception;  otherwise with source value
771 >     * exception
772       */
773 <    public void testExceptionally() {
774 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
775 <        ExceptionToInteger r = new ExceptionToInteger();
776 <        CompletableFuture<Integer> g = f.exceptionally(r);
777 <        f.completeExceptionally(new CFException());
778 <        checkCompletedNormally(g, three);
773 >    public void testExceptionally_exceptionalCompletion() {
774 >        for (boolean createIncomplete : new boolean[] { true, false })
775 >        for (Integer v1 : new Integer[] { 1, null })
776 >    {
777 >        final AtomicInteger a = new AtomicInteger(0);
778 >        final CFException ex = new CFException();
779 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
780 >        if (!createIncomplete) f.completeExceptionally(ex);
781 >        final CompletableFuture<Integer> g = f.exceptionally
782 >            ((Throwable t) -> {
783 >                threadAssertSame(t, ex);
784 >                a.getAndIncrement();
785 >                return v1;
786 >            });
787 >        if (createIncomplete) f.completeExceptionally(ex);
788 >
789 >        checkCompletedNormally(g, v1);
790 >        assertEquals(1, a.get());
791 >    }}
792 >
793 >    public void testExceptionally_exceptionalCompletionActionFailed() {
794 >        for (boolean createIncomplete : new boolean[] { true, false })
795 >        for (Integer v1 : new Integer[] { 1, null })
796 >    {
797 >        final AtomicInteger a = new AtomicInteger(0);
798 >        final CFException ex1 = new CFException();
799 >        final CFException ex2 = new CFException();
800 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
801 >        if (!createIncomplete) f.completeExceptionally(ex1);
802 >        final CompletableFuture<Integer> g = f.exceptionally
803 >            ((Throwable t) -> {
804 >                threadAssertSame(t, ex1);
805 >                a.getAndIncrement();
806 >                throw ex2;
807 >            });
808 >        if (createIncomplete) f.completeExceptionally(ex1);
809 >
810 >        checkCompletedWithWrappedCFException(g, ex2);
811 >        assertEquals(1, a.get());
812 >    }}
813 >
814 >    /**
815 >     * handle action completes normally with function value on normal
816 >     * completion of source
817 >     */
818 >    public void testHandle_normalCompletion() {
819 >        for (ExecutionMode m : ExecutionMode.values())
820 >        for (boolean createIncomplete : new boolean[] { true, false })
821 >        for (Integer v1 : new Integer[] { 1, null })
822 >    {
823 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
824 >        final AtomicInteger a = new AtomicInteger(0);
825 >        if (!createIncomplete) f.complete(v1);
826 >        final CompletableFuture<Integer> g = m.handle
827 >            (f,
828 >             (Integer x, Throwable t) -> {
829 >                threadAssertSame(x, v1);
830 >                threadAssertNull(t);
831 >                a.getAndIncrement();
832 >                return inc(v1);
833 >            });
834 >        if (createIncomplete) f.complete(v1);
835 >
836 >        checkCompletedNormally(g, inc(v1));
837 >        checkCompletedNormally(f, v1);
838 >        assertEquals(1, a.get());
839 >    }}
840 >
841 >    /**
842 >     * handle action completes normally with function value on
843 >     * exceptional completion of source
844 >     */
845 >    public void testHandle_exceptionalCompletion() {
846 >        for (ExecutionMode m : ExecutionMode.values())
847 >        for (boolean createIncomplete : new boolean[] { true, false })
848 >        for (Integer v1 : new Integer[] { 1, null })
849 >    {
850 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
851 >        final AtomicInteger a = new AtomicInteger(0);
852 >        final CFException ex = new CFException();
853 >        if (!createIncomplete) f.completeExceptionally(ex);
854 >        final CompletableFuture<Integer> g = m.handle
855 >            (f,
856 >             (Integer x, Throwable t) -> {
857 >                threadAssertNull(x);
858 >                threadAssertSame(t, ex);
859 >                a.getAndIncrement();
860 >                return v1;
861 >            });
862 >        if (createIncomplete) f.completeExceptionally(ex);
863 >
864 >        checkCompletedNormally(g, v1);
865 >        checkCompletedWithWrappedCFException(f, ex);
866 >        assertEquals(1, a.get());
867 >    }}
868 >
869 >    /**
870 >     * handle action completes normally with function value on
871 >     * cancelled source
872 >     */
873 >    public void testHandle_sourceCancelled() {
874 >        for (ExecutionMode m : ExecutionMode.values())
875 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
876 >        for (boolean createIncomplete : new boolean[] { true, false })
877 >        for (Integer v1 : new Integer[] { 1, null })
878 >    {
879 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
880 >        final AtomicInteger a = new AtomicInteger(0);
881 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
882 >        final CompletableFuture<Integer> g = m.handle
883 >            (f,
884 >             (Integer x, Throwable t) -> {
885 >                threadAssertNull(x);
886 >                threadAssertTrue(t instanceof CancellationException);
887 >                a.getAndIncrement();
888 >                return v1;
889 >            });
890 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
891  
892 <        f = new CompletableFuture<Integer>();
893 <        r = new ExceptionToInteger();
894 <        g = f.exceptionally(r);
895 <        f.complete(one);
382 <        checkCompletedNormally(g, one);
383 <    }
892 >        checkCompletedNormally(g, v1);
893 >        checkCancelled(f);
894 >        assertEquals(1, a.get());
895 >    }}
896  
897      /**
898 <     * handle action completes normally with function value on either
387 <     * normal or exceptional completion of source
898 >     * handle result completes exceptionally if action does
899       */
900 <    public void testHandle() {
901 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
902 <        IntegerHandler r = new IntegerHandler();
903 <        CompletableFuture<Integer> g = f.handle(r);
904 <        f.completeExceptionally(new CFException());
905 <        checkCompletedNormally(g, three);
906 <
907 <        f = new CompletableFuture<Integer>();
908 <        r = new IntegerHandler();
909 <        g = f.handle(r);
910 <        f.complete(one);
911 <        checkCompletedNormally(g, two);
912 <    }
900 >    public void testHandle_sourceFailedActionFailed() {
901 >        for (ExecutionMode m : ExecutionMode.values())
902 >        for (boolean createIncomplete : new boolean[] { true, false })
903 >    {
904 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
905 >        final AtomicInteger a = new AtomicInteger(0);
906 >        final CFException ex1 = new CFException();
907 >        final CFException ex2 = new CFException();
908 >        if (!createIncomplete) f.completeExceptionally(ex1);
909 >        final CompletableFuture<Integer> g = m.handle
910 >            (f,
911 >             (Integer x, Throwable t) -> {
912 >                threadAssertNull(x);
913 >                threadAssertSame(ex1, t);
914 >                a.getAndIncrement();
915 >                throw ex2;
916 >            });
917 >        if (createIncomplete) f.completeExceptionally(ex1);
918 >
919 >        checkCompletedWithWrappedCFException(g, ex2);
920 >        checkCompletedWithWrappedCFException(f, ex1);
921 >        assertEquals(1, a.get());
922 >    }}
923 >
924 >    public void testHandle_sourceCompletedNormallyActionFailed() {
925 >        for (ExecutionMode m : ExecutionMode.values())
926 >        for (boolean createIncomplete : new boolean[] { true, false })
927 >        for (Integer v1 : new Integer[] { 1, null })
928 >    {
929 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
930 >        final AtomicInteger a = new AtomicInteger(0);
931 >        final CFException ex = new CFException();
932 >        if (!createIncomplete) f.complete(v1);
933 >        final CompletableFuture<Integer> g = m.handle
934 >            (f,
935 >             (Integer x, Throwable t) -> {
936 >                threadAssertSame(x, v1);
937 >                threadAssertNull(t);
938 >                a.getAndIncrement();
939 >                throw ex;
940 >            });
941 >        if (createIncomplete) f.complete(v1);
942 >
943 >        checkCompletedWithWrappedCFException(g, ex);
944 >        checkCompletedNormally(f, v1);
945 >        assertEquals(1, a.get());
946 >    }}
947  
948      /**
949       * runAsync completes after running Runnable
950       */
951      public void testRunAsync() {
952 <        Noop r = new Noop();
952 >        Noop r = new Noop(ExecutionMode.ASYNC);
953          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
954          assertNull(f.join());
955 <        assertTrue(r.ran);
955 >        assertEquals(1, r.invocationCount);
956 >        checkCompletedNormally(f, null);
957      }
958  
959      /**
960       * runAsync with executor completes after running Runnable
961       */
962      public void testRunAsync2() {
963 <        Noop r = new Noop();
964 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
963 >        Noop r = new Noop(ExecutionMode.EXECUTOR);
964 >        ThreadExecutor exec = new ThreadExecutor();
965 >        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
966          assertNull(f.join());
967 <        assertTrue(r.ran);
967 >        assertEquals(1, r.invocationCount);
968 >        checkCompletedNormally(f, null);
969 >        assertEquals(1, exec.count.get());
970      }
971  
972      /**
973       * failing runAsync completes exceptionally after running Runnable
974       */
975      public void testRunAsync3() {
976 <        FailingNoop r = new FailingNoop();
976 >        FailingRunnable r = new FailingRunnable(ExecutionMode.ASYNC);
977          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
978          checkCompletedWithWrappedCFException(f);
979 <        assertTrue(r.ran);
979 >        assertEquals(1, r.invocationCount);
980      }
981  
982      /**
983       * supplyAsync completes with result of supplier
984       */
985      public void testSupplyAsync() {
986 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
986 >        CompletableFuture<Integer> f;
987 >        f = CompletableFuture.supplyAsync(supplyOne);
988          assertEquals(f.join(), one);
989 +        checkCompletedNormally(f, one);
990      }
991  
992      /**
993       * supplyAsync with executor completes with result of supplier
994       */
995      public void testSupplyAsync2() {
996 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
996 >        CompletableFuture<Integer> f;
997 >        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
998          assertEquals(f.join(), one);
999 +        checkCompletedNormally(f, one);
1000      }
1001  
1002      /**
1003       * Failing supplyAsync completes exceptionally
1004       */
1005      public void testSupplyAsync3() {
1006 <        FailingSupplier r = new FailingSupplier();
1006 >        FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC);
1007          CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1008          checkCompletedWithWrappedCFException(f);
1009 <        assertTrue(r.ran);
1009 >        assertEquals(1, r.invocationCount);
1010      }
1011  
1012 <    // seq conmpletion methods
1012 >    // seq completion methods
1013  
1014      /**
1015       * thenRun result completes normally after normal completion of source
1016       */
1017 <    public void testThenRun() {
1018 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1019 <        Noop r = new Noop();
1020 <        CompletableFuture<Void> g = f.thenRun(r);
1021 <        f.complete(null);
1022 <        checkCompletedNormally(g, null);
1023 <        // reordered version
1024 <        f = new CompletableFuture<Integer>();
1025 <        f.complete(null);
1026 <        r = new Noop();
1027 <        g = f.thenRun(r);
1017 >    public void testThenRun_normalCompletion() {
1018 >        for (ExecutionMode m : ExecutionMode.values())
1019 >        for (boolean createIncomplete : new boolean[] { true, false })
1020 >        for (Integer v1 : new Integer[] { 1, null })
1021 >    {
1022 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1023 >        final Noop r = new Noop(m);
1024 >        if (!createIncomplete) f.complete(v1);
1025 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1026 >        if (createIncomplete) {
1027 >            checkIncomplete(g);
1028 >            f.complete(v1);
1029 >        }
1030 >
1031          checkCompletedNormally(g, null);
1032 <    }
1032 >        checkCompletedNormally(f, v1);
1033 >        assertEquals(1, r.invocationCount);
1034 >    }}
1035  
1036      /**
1037       * thenRun result completes exceptionally after exceptional
1038       * completion of source
1039       */
1040 <    public void testThenRun2() {
1041 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1042 <        Noop r = new Noop();
1043 <        CompletableFuture<Void> g = f.thenRun(r);
1044 <        f.completeExceptionally(new CFException());
1045 <        checkCompletedWithWrappedCFException(g);
1046 <    }
1040 >    public void testThenRun_exceptionalCompletion() {
1041 >        for (ExecutionMode m : ExecutionMode.values())
1042 >        for (boolean createIncomplete : new boolean[] { true, false })
1043 >    {
1044 >        final CFException ex = new CFException();
1045 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1046 >        final Noop r = new Noop(m);
1047 >        if (!createIncomplete) f.completeExceptionally(ex);
1048 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1049 >        if (createIncomplete) {
1050 >            checkIncomplete(g);
1051 >            f.completeExceptionally(ex);
1052 >        }
1053 >
1054 >        checkCompletedWithWrappedCFException(g, ex);
1055 >        checkCompletedWithWrappedCFException(f, ex);
1056 >        assertEquals(0, r.invocationCount);
1057 >    }}
1058  
1059      /**
1060 <     * thenRun result completes exceptionally if action does
1060 >     * thenRun result completes exceptionally if source cancelled
1061       */
1062 <    public void testThenRun3() {
1063 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1064 <        FailingNoop r = new FailingNoop();
1065 <        CompletableFuture<Void> g = f.thenRun(r);
1066 <        f.complete(null);
1067 <        checkCompletedWithWrappedCFException(g);
1068 <    }
1062 >    public void testThenRun_sourceCancelled() {
1063 >        for (ExecutionMode m : ExecutionMode.values())
1064 >        for (boolean createIncomplete : new boolean[] { true, false })
1065 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1066 >    {
1067 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1068 >        final Noop r = new Noop(m);
1069 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1070 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1071 >        if (createIncomplete) {
1072 >            checkIncomplete(g);
1073 >            assertTrue(f.cancel(mayInterruptIfRunning));
1074 >        }
1075 >
1076 >        checkCompletedWithWrappedCancellationException(g);
1077 >        checkCancelled(f);
1078 >        assertEquals(0, r.invocationCount);
1079 >    }}
1080  
1081      /**
1082 <     * thenRun result completes exceptionally if source cancelled
1082 >     * thenRun result completes exceptionally if action does
1083       */
1084 <    public void testThenRun4() {
1085 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1086 <        Noop r = new Noop();
1087 <        CompletableFuture<Void> g = f.thenRun(r);
1088 <        assertTrue(f.cancel(true));
1089 <        checkCompletedWithWrappedCancellationException(g);
1090 <    }
1084 >    public void testThenRun_actionFailed() {
1085 >        for (ExecutionMode m : ExecutionMode.values())
1086 >        for (boolean createIncomplete : new boolean[] { true, false })
1087 >        for (Integer v1 : new Integer[] { 1, null })
1088 >    {
1089 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1090 >        final FailingRunnable r = new FailingRunnable(m);
1091 >        if (!createIncomplete) f.complete(v1);
1092 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1093 >        if (createIncomplete) {
1094 >            checkIncomplete(g);
1095 >            f.complete(v1);
1096 >        }
1097 >
1098 >        checkCompletedWithWrappedCFException(g);
1099 >        checkCompletedNormally(f, v1);
1100 >    }}
1101  
1102      /**
1103       * thenApply result completes normally after normal completion of source
1104       */
1105 <    public void testThenApply() {
1106 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1107 <        CompletableFuture<Integer> g = f.thenApply(inc);
1108 <        f.complete(one);
1109 <        checkCompletedNormally(g, two);
1110 <    }
1105 >    public void testThenApply_normalCompletion() {
1106 >        for (ExecutionMode m : ExecutionMode.values())
1107 >        for (boolean createIncomplete : new boolean[] { true, false })
1108 >        for (Integer v1 : new Integer[] { 1, null })
1109 >    {
1110 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1111 >        final IncFunction r = new IncFunction(m);
1112 >        if (!createIncomplete) f.complete(v1);
1113 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1114 >        if (createIncomplete) {
1115 >            checkIncomplete(g);
1116 >            f.complete(v1);
1117 >        }
1118 >
1119 >        checkCompletedNormally(g, inc(v1));
1120 >        checkCompletedNormally(f, v1);
1121 >        assertEquals(1, r.invocationCount);
1122 >    }}
1123  
1124      /**
1125       * thenApply result completes exceptionally after exceptional
1126       * completion of source
1127       */
1128 <    public void testThenApply2() {
1129 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1130 <        CompletableFuture<Integer> g = f.thenApply(inc);
1131 <        f.completeExceptionally(new CFException());
1132 <        checkCompletedWithWrappedCFException(g);
1133 <    }
1128 >    public void testThenApply_exceptionalCompletion() {
1129 >        for (ExecutionMode m : ExecutionMode.values())
1130 >        for (boolean createIncomplete : new boolean[] { true, false })
1131 >    {
1132 >        final CFException ex = new CFException();
1133 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1134 >        final IncFunction r = new IncFunction(m);
1135 >        if (!createIncomplete) f.completeExceptionally(ex);
1136 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1137 >        if (createIncomplete) {
1138 >            checkIncomplete(g);
1139 >            f.completeExceptionally(ex);
1140 >        }
1141 >
1142 >        checkCompletedWithWrappedCFException(g, ex);
1143 >        checkCompletedWithWrappedCFException(f, ex);
1144 >        assertEquals(0, r.invocationCount);
1145 >    }}
1146  
1147      /**
1148 <     * thenApply result completes exceptionally if action does
1148 >     * thenApply result completes exceptionally if source cancelled
1149       */
1150 <    public void testThenApply3() {
1151 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1152 <        CompletableFuture<Integer> g = f.thenApply(new FailingFunction());
1153 <        f.complete(one);
1154 <        checkCompletedWithWrappedCFException(g);
1155 <    }
1150 >    public void testThenApply_sourceCancelled() {
1151 >        for (ExecutionMode m : ExecutionMode.values())
1152 >        for (boolean createIncomplete : new boolean[] { true, false })
1153 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1154 >    {
1155 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1156 >        final IncFunction r = new IncFunction(m);
1157 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1158 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1159 >        if (createIncomplete) {
1160 >            checkIncomplete(g);
1161 >            assertTrue(f.cancel(mayInterruptIfRunning));
1162 >        }
1163 >
1164 >        checkCompletedWithWrappedCancellationException(g);
1165 >        checkCancelled(f);
1166 >        assertEquals(0, r.invocationCount);
1167 >    }}
1168  
1169      /**
1170 <     * thenApply result completes exceptionally if source cancelled
1170 >     * thenApply result completes exceptionally if action does
1171       */
1172 <    public void testThenApply4() {
1173 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1174 <        CompletableFuture<Integer> g = f.thenApply(inc);
1175 <        assertTrue(f.cancel(true));
1176 <        checkCompletedWithWrappedCancellationException(g);
1177 <    }
1172 >    public void testThenApply_actionFailed() {
1173 >        for (ExecutionMode m : ExecutionMode.values())
1174 >        for (boolean createIncomplete : new boolean[] { true, false })
1175 >        for (Integer v1 : new Integer[] { 1, null })
1176 >    {
1177 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1178 >        final FailingFunction r = new FailingFunction(m);
1179 >        if (!createIncomplete) f.complete(v1);
1180 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1181 >        if (createIncomplete) {
1182 >            checkIncomplete(g);
1183 >            f.complete(v1);
1184 >        }
1185 >
1186 >        checkCompletedWithWrappedCFException(g);
1187 >        checkCompletedNormally(f, v1);
1188 >    }}
1189  
1190      /**
1191       * thenAccept result completes normally after normal completion of source
1192       */
1193 <    public void testThenAccept() {
1194 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1195 <        IncAction r = new IncAction();
1196 <        CompletableFuture<Void> g = f.thenAccept(r);
1197 <        f.complete(one);
1193 >    public void testThenAccept_normalCompletion() {
1194 >        for (ExecutionMode m : ExecutionMode.values())
1195 >        for (boolean createIncomplete : new boolean[] { true, false })
1196 >        for (Integer v1 : new Integer[] { 1, null })
1197 >    {
1198 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1199 >        final IncAction r = new IncAction();
1200 >        if (!createIncomplete) f.complete(v1);
1201 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1202 >        if (createIncomplete) {
1203 >            checkIncomplete(g);
1204 >            f.complete(v1);
1205 >        }
1206 >
1207          checkCompletedNormally(g, null);
1208 <        assertEquals(r.value, 2);
1209 <    }
1208 >        checkCompletedNormally(f, v1);
1209 >        assertEquals(1, r.invocationCount);
1210 >        assertEquals(inc(v1), r.value);
1211 >    }}
1212  
1213      /**
1214       * thenAccept result completes exceptionally after exceptional
1215       * completion of source
1216       */
1217 <    public void testThenAccept2() {
1218 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1219 <        IncAction r = new IncAction();
1220 <        CompletableFuture<Void> g = f.thenAccept(r);
1221 <        f.completeExceptionally(new CFException());
1222 <        checkCompletedWithWrappedCFException(g);
1223 <    }
1217 >    public void testThenAccept_exceptionalCompletion() {
1218 >        for (ExecutionMode m : ExecutionMode.values())
1219 >        for (boolean createIncomplete : new boolean[] { true, false })
1220 >    {
1221 >        final CFException ex = new CFException();
1222 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1223 >        final IncAction r = new IncAction();
1224 >        if (!createIncomplete) f.completeExceptionally(ex);
1225 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1226 >        if (createIncomplete) {
1227 >            checkIncomplete(g);
1228 >            f.completeExceptionally(ex);
1229 >        }
1230 >
1231 >        checkCompletedWithWrappedCFException(g, ex);
1232 >        checkCompletedWithWrappedCFException(f, ex);
1233 >        assertEquals(0, r.invocationCount);
1234 >    }}
1235  
1236      /**
1237       * thenAccept result completes exceptionally if action does
1238       */
1239 <    public void testThenAccept3() {
1240 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1241 <        FailingConsumer r = new FailingConsumer();
1242 <        CompletableFuture<Void> g = f.thenAccept(r);
1243 <        f.complete(one);
1239 >    public void testThenAccept_actionFailed() {
1240 >        for (ExecutionMode m : ExecutionMode.values())
1241 >        for (boolean createIncomplete : new boolean[] { true, false })
1242 >        for (Integer v1 : new Integer[] { 1, null })
1243 >    {
1244 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1245 >        final FailingConsumer r = new FailingConsumer(m);
1246 >        if (!createIncomplete) f.complete(v1);
1247 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1248 >        if (createIncomplete) {
1249 >            checkIncomplete(g);
1250 >            f.complete(v1);
1251 >        }
1252 >
1253          checkCompletedWithWrappedCFException(g);
1254 <        assertTrue(r.ran);
1255 <    }
1254 >        checkCompletedNormally(f, v1);
1255 >    }}
1256  
1257      /**
1258       * thenAccept result completes exceptionally if source cancelled
1259       */
1260 <    public void testThenAccept4() {
1261 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1262 <        IncAction r = new IncAction();
1263 <        CompletableFuture<Void> g = f.thenAccept(r);
1264 <        assertTrue(f.cancel(true));
1265 <        checkCompletedWithWrappedCancellationException(g);
1266 <    }
1260 >    public void testThenAccept_sourceCancelled() {
1261 >        for (ExecutionMode m : ExecutionMode.values())
1262 >        for (boolean createIncomplete : new boolean[] { true, false })
1263 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1264 >    {
1265 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1266 >        final IncAction r = new IncAction();
1267 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1268 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1269 >        if (createIncomplete) {
1270 >            checkIncomplete(g);
1271 >            assertTrue(f.cancel(mayInterruptIfRunning));
1272 >        }
1273  
1274 +        checkCompletedWithWrappedCancellationException(g);
1275 +        checkCancelled(f);
1276 +        assertEquals(0, r.invocationCount);
1277 +    }}
1278  
1279      /**
1280 <     * thenCombine result completes normally after normal completion of sources
1280 >     * thenCombine result completes normally after normal completion
1281 >     * of sources
1282       */
1283 <    public void testThenCombine() {
1284 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1285 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1286 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1287 <        f.complete(one);
1288 <        checkIncomplete(g);
1289 <        f2.complete(two);
1290 <        checkCompletedNormally(g, three);
1291 <
1292 <        f = new CompletableFuture<Integer>();
1293 <        f.complete(one);
1294 <        f2 = new CompletableFuture<Integer>();
1295 <        g = f.thenCombine(f2, add);
1296 <        checkIncomplete(g);
1297 <        f2.complete(two);
1298 <        checkCompletedNormally(g, three);
1299 <    }
1283 >    public void testThenCombine_normalCompletion() {
1284 >        for (ExecutionMode m : ExecutionMode.values())
1285 >        for (boolean createIncomplete : new boolean[] { true, false })
1286 >        for (boolean fFirst : new boolean[] { true, false })
1287 >        for (Integer v1 : new Integer[] { 1, null })
1288 >        for (Integer v2 : new Integer[] { 2, null })
1289 >    {
1290 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1291 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1292 >        final SubtractFunction r = new SubtractFunction(m);
1293 >
1294 >        if (fFirst) f.complete(v1); else g.complete(v2);
1295 >        if (!createIncomplete)
1296 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1297 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1298 >        if (createIncomplete) {
1299 >            checkIncomplete(h);
1300 >            assertEquals(0, r.invocationCount);
1301 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1302 >        }
1303 >
1304 >        checkCompletedNormally(h, subtract(v1, v2));
1305 >        checkCompletedNormally(f, v1);
1306 >        checkCompletedNormally(g, v2);
1307 >        assertEquals(1, r.invocationCount);
1308 >    }}
1309  
1310      /**
1311       * thenCombine result completes exceptionally after exceptional
1312       * completion of either source
1313       */
1314 <    public void testThenCombine2() {
1315 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1316 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1317 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1318 <        f.completeExceptionally(new CFException());
1319 <        f2.complete(two);
1320 <        checkCompletedWithWrappedCFException(g);
1321 <
1322 <        f = new CompletableFuture<Integer>();
1323 <        f.complete(one);
1324 <        f2 = new CompletableFuture<Integer>();
1325 <        g = f.thenCombine(f2, add);
1326 <        f2.completeExceptionally(new CFException());
1327 <        checkCompletedWithWrappedCFException(g);
1328 <    }
1314 >    public void testThenCombine_exceptionalCompletion() {
1315 >        for (ExecutionMode m : ExecutionMode.values())
1316 >        for (boolean createIncomplete : new boolean[] { true, false })
1317 >        for (boolean fFirst : new boolean[] { true, false })
1318 >        for (Integer v1 : new Integer[] { 1, null })
1319 >    {
1320 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1322 >        final CFException ex = new CFException();
1323 >        final SubtractFunction r = new SubtractFunction(m);
1324 >
1325 >        (fFirst ? f : g).complete(v1);
1326 >        if (!createIncomplete)
1327 >            (!fFirst ? f : g).completeExceptionally(ex);
1328 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1329 >        if (createIncomplete) {
1330 >            checkIncomplete(h);
1331 >            (!fFirst ? f : g).completeExceptionally(ex);
1332 >        }
1333 >
1334 >        checkCompletedWithWrappedCFException(h, ex);
1335 >        assertEquals(0, r.invocationCount);
1336 >        checkCompletedNormally(fFirst ? f : g, v1);
1337 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1338 >    }}
1339  
1340      /**
1341       * thenCombine result completes exceptionally if action does
1342       */
1343 <    public void testThenCombine3() {
1344 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1345 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1346 <        FailingBiFunction r = new FailingBiFunction();
1347 <        CompletableFuture<Integer> g = f.thenCombine(f2, r);
1348 <        f.complete(one);
1349 <        checkIncomplete(g);
1350 <        f2.complete(two);
1351 <        checkCompletedWithWrappedCFException(g);
1352 <    }
1343 >    public void testThenCombine_actionFailed() {
1344 >        for (ExecutionMode m : ExecutionMode.values())
1345 >        for (boolean fFirst : new boolean[] { true, false })
1346 >        for (Integer v1 : new Integer[] { 1, null })
1347 >        for (Integer v2 : new Integer[] { 2, null })
1348 >    {
1349 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1350 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1351 >        final FailingBiFunction r = new FailingBiFunction(m);
1352 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1353 >
1354 >        if (fFirst) {
1355 >            f.complete(v1);
1356 >            g.complete(v2);
1357 >        } else {
1358 >            g.complete(v2);
1359 >            f.complete(v1);
1360 >        }
1361 >
1362 >        checkCompletedWithWrappedCFException(h);
1363 >        checkCompletedNormally(f, v1);
1364 >        checkCompletedNormally(g, v2);
1365 >    }}
1366  
1367      /**
1368       * thenCombine result completes exceptionally if either source cancelled
1369       */
1370 <    public void testThenCombine4() {
1371 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1372 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1373 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
1374 <        assertTrue(f.cancel(true));
1375 <        f2.complete(two);
1376 <        checkCompletedWithWrappedCancellationException(g);
1377 <        f = new CompletableFuture<Integer>();
1378 <        f2 = new CompletableFuture<Integer>();
1379 <        g = f.thenCombine(f2, add);
1380 <        f.complete(one);
1381 <        assertTrue(f2.cancel(true));
1382 <        checkCompletedWithWrappedCancellationException(g);
1383 <    }
1370 >    public void testThenCombine_sourceCancelled() {
1371 >        for (ExecutionMode m : ExecutionMode.values())
1372 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1373 >        for (boolean createIncomplete : new boolean[] { true, false })
1374 >        for (boolean fFirst : new boolean[] { true, false })
1375 >        for (Integer v1 : new Integer[] { 1, null })
1376 >    {
1377 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1378 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1379 >        final SubtractFunction r = new SubtractFunction(m);
1380 >
1381 >        (fFirst ? f : g).complete(v1);
1382 >        if (!createIncomplete)
1383 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1384 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1385 >        if (createIncomplete) {
1386 >            checkIncomplete(h);
1387 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1388 >        }
1389 >
1390 >        checkCompletedWithWrappedCancellationException(h);
1391 >        checkCancelled(!fFirst ? f : g);
1392 >        assertEquals(0, r.invocationCount);
1393 >        checkCompletedNormally(fFirst ? f : g, v1);
1394 >    }}
1395  
1396      /**
1397       * thenAcceptBoth result completes normally after normal
1398       * completion of sources
1399       */
1400 <    public void testThenAcceptBoth() {
1401 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1402 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1403 <        AddAction r = new AddAction();
1404 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1405 <        f.complete(one);
1406 <        checkIncomplete(g);
1407 <        f2.complete(two);
1408 <        checkCompletedNormally(g, null);
1409 <        assertEquals(r.value, 3);
1410 <
1411 <        r = new AddAction();
1412 <        f = new CompletableFuture<Integer>();
1413 <        f.complete(one);
1414 <        f2 = new CompletableFuture<Integer>();
1415 <        g = f.thenAcceptBoth(f2, r);
1416 <        checkIncomplete(g);
1417 <        f2.complete(two);
1418 <        checkCompletedNormally(g, null);
1419 <        assertEquals(r.value, 3);
1420 <    }
1400 >    public void testThenAcceptBoth_normalCompletion() {
1401 >        for (ExecutionMode m : ExecutionMode.values())
1402 >        for (boolean createIncomplete : new boolean[] { true, false })
1403 >        for (boolean fFirst : new boolean[] { true, false })
1404 >        for (Integer v1 : new Integer[] { 1, null })
1405 >        for (Integer v2 : new Integer[] { 2, null })
1406 >    {
1407 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1408 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1409 >        final SubtractAction r = new SubtractAction(m);
1410 >
1411 >        if (fFirst) f.complete(v1); else g.complete(v2);
1412 >        if (!createIncomplete)
1413 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1414 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1415 >        if (createIncomplete) {
1416 >            checkIncomplete(h);
1417 >            assertEquals(0, r.invocationCount);
1418 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1419 >        }
1420 >
1421 >        checkCompletedNormally(h, null);
1422 >        assertEquals(subtract(v1, v2), r.value);
1423 >        checkCompletedNormally(f, v1);
1424 >        checkCompletedNormally(g, v2);
1425 >    }}
1426  
1427      /**
1428       * thenAcceptBoth result completes exceptionally after exceptional
1429       * completion of either source
1430       */
1431 <    public void testThenAcceptBoth2() {
1432 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1433 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1434 <        AddAction r = new AddAction();
1435 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1436 <        f.completeExceptionally(new CFException());
1437 <        f2.complete(two);
1438 <        checkCompletedWithWrappedCFException(g);
1439 <
1440 <        r = new AddAction();
1441 <        f = new CompletableFuture<Integer>();
1442 <        f.complete(one);
1443 <        f2 = new CompletableFuture<Integer>();
1444 <        g = f.thenAcceptBoth(f2, r);
1445 <        f2.completeExceptionally(new CFException());
1446 <        checkCompletedWithWrappedCFException(g);
1447 <    }
1431 >    public void testThenAcceptBoth_exceptionalCompletion() {
1432 >        for (ExecutionMode m : ExecutionMode.values())
1433 >        for (boolean createIncomplete : new boolean[] { true, false })
1434 >        for (boolean fFirst : new boolean[] { true, false })
1435 >        for (Integer v1 : new Integer[] { 1, null })
1436 >    {
1437 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1438 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1439 >        final CFException ex = new CFException();
1440 >        final SubtractAction r = new SubtractAction(m);
1441 >
1442 >        (fFirst ? f : g).complete(v1);
1443 >        if (!createIncomplete)
1444 >            (!fFirst ? f : g).completeExceptionally(ex);
1445 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1446 >        if (createIncomplete) {
1447 >            checkIncomplete(h);
1448 >            (!fFirst ? f : g).completeExceptionally(ex);
1449 >        }
1450 >
1451 >        checkCompletedWithWrappedCFException(h, ex);
1452 >        assertEquals(0, r.invocationCount);
1453 >        checkCompletedNormally(fFirst ? f : g, v1);
1454 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1455 >    }}
1456  
1457      /**
1458       * thenAcceptBoth result completes exceptionally if action does
1459       */
1460 <    public void testThenAcceptBoth3() {
1461 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1462 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1463 <        FailingBiConsumer r = new FailingBiConsumer();
1464 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1465 <        f.complete(one);
1466 <        checkIncomplete(g);
1467 <        f2.complete(two);
1468 <        checkCompletedWithWrappedCFException(g);
1469 <    }
1460 >    public void testThenAcceptBoth_actionFailed() {
1461 >        for (ExecutionMode m : ExecutionMode.values())
1462 >        for (boolean fFirst : new boolean[] { true, false })
1463 >        for (Integer v1 : new Integer[] { 1, null })
1464 >        for (Integer v2 : new Integer[] { 2, null })
1465 >    {
1466 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1467 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1468 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1469 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1470 >
1471 >        if (fFirst) {
1472 >            f.complete(v1);
1473 >            g.complete(v2);
1474 >        } else {
1475 >            g.complete(v2);
1476 >            f.complete(v1);
1477 >        }
1478 >
1479 >        checkCompletedWithWrappedCFException(h);
1480 >        checkCompletedNormally(f, v1);
1481 >        checkCompletedNormally(g, v2);
1482 >    }}
1483  
1484      /**
1485       * thenAcceptBoth result completes exceptionally if either source cancelled
1486       */
1487 <    public void testThenAcceptBoth4() {
1488 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1489 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1490 <        AddAction r = new AddAction();
1491 <        CompletableFuture<Void> g = f.thenAcceptBoth(f2, r);
1492 <        assertTrue(f.cancel(true));
1493 <        f2.complete(two);
1494 <        checkCompletedWithWrappedCancellationException(g);
1495 <        f = new CompletableFuture<Integer>();
1496 <        f2 = new CompletableFuture<Integer>();
1497 <        r = new AddAction();
1498 <        g = f.thenAcceptBoth(f2, r);
1499 <        f.complete(one);
1500 <        assertTrue(f2.cancel(true));
1501 <        checkCompletedWithWrappedCancellationException(g);
1502 <    }
1487 >    public void testThenAcceptBoth_sourceCancelled() {
1488 >        for (ExecutionMode m : ExecutionMode.values())
1489 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1490 >        for (boolean createIncomplete : new boolean[] { true, false })
1491 >        for (boolean fFirst : new boolean[] { true, false })
1492 >        for (Integer v1 : new Integer[] { 1, null })
1493 >    {
1494 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1495 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1496 >        final SubtractAction r = new SubtractAction(m);
1497 >
1498 >        (fFirst ? f : g).complete(v1);
1499 >        if (!createIncomplete)
1500 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1501 >        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1502 >        if (createIncomplete) {
1503 >            checkIncomplete(h);
1504 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1505 >        }
1506 >
1507 >        checkCompletedWithWrappedCancellationException(h);
1508 >        checkCancelled(!fFirst ? f : g);
1509 >        assertEquals(0, r.invocationCount);
1510 >        checkCompletedNormally(fFirst ? f : g, v1);
1511 >    }}
1512  
1513      /**
1514       * runAfterBoth result completes normally after normal
1515       * completion of sources
1516       */
1517 <    public void testRunAfterBoth() {
1518 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1519 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1520 <        Noop r = new Noop();
1521 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1522 <        f.complete(one);
1523 <        checkIncomplete(g);
1524 <        f2.complete(two);
1525 <        checkCompletedNormally(g, null);
1526 <        assertTrue(r.ran);
1527 <
1528 <        r = new Noop();
1529 <        f = new CompletableFuture<Integer>();
1530 <        f.complete(one);
1531 <        f2 = new CompletableFuture<Integer>();
1532 <        g = f.runAfterBoth(f2, r);
1533 <        checkIncomplete(g);
1534 <        f2.complete(two);
1535 <        checkCompletedNormally(g, null);
1536 <        assertTrue(r.ran);
1537 <    }
1517 >    public void testRunAfterBoth_normalCompletion() {
1518 >        for (ExecutionMode m : ExecutionMode.values())
1519 >        for (boolean createIncomplete : new boolean[] { true, false })
1520 >        for (boolean fFirst : new boolean[] { true, false })
1521 >        for (Integer v1 : new Integer[] { 1, null })
1522 >        for (Integer v2 : new Integer[] { 2, null })
1523 >    {
1524 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1525 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1526 >        final Noop r = new Noop(m);
1527 >
1528 >        if (fFirst) f.complete(v1); else g.complete(v2);
1529 >        if (!createIncomplete)
1530 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1531 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1532 >        if (createIncomplete) {
1533 >            checkIncomplete(h);
1534 >            assertEquals(0, r.invocationCount);
1535 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1536 >        }
1537 >
1538 >        checkCompletedNormally(h, null);
1539 >        assertEquals(1, r.invocationCount);
1540 >        checkCompletedNormally(f, v1);
1541 >        checkCompletedNormally(g, v2);
1542 >    }}
1543  
1544      /**
1545       * runAfterBoth result completes exceptionally after exceptional
1546       * completion of either source
1547       */
1548 <    public void testRunAfterBoth2() {
1549 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1550 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1551 <        Noop r = new Noop();
1552 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1553 <        f.completeExceptionally(new CFException());
1554 <        f2.complete(two);
1555 <        checkCompletedWithWrappedCFException(g);
1556 <
1557 <        r = new Noop();
1558 <        f = new CompletableFuture<Integer>();
1559 <        f.complete(one);
1560 <        f2 = new CompletableFuture<Integer>();
1561 <        g = f.runAfterBoth(f2, r);
1562 <        f2.completeExceptionally(new CFException());
1563 <        checkCompletedWithWrappedCFException(g);
1564 <    }
1548 >    public void testRunAfterBoth_exceptionalCompletion() {
1549 >        for (ExecutionMode m : ExecutionMode.values())
1550 >        for (boolean createIncomplete : new boolean[] { true, false })
1551 >        for (boolean fFirst : new boolean[] { true, false })
1552 >        for (Integer v1 : new Integer[] { 1, null })
1553 >    {
1554 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1555 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1556 >        final CFException ex = new CFException();
1557 >        final Noop r = new Noop(m);
1558 >
1559 >        (fFirst ? f : g).complete(v1);
1560 >        if (!createIncomplete)
1561 >            (!fFirst ? f : g).completeExceptionally(ex);
1562 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1563 >        if (createIncomplete) {
1564 >            checkIncomplete(h);
1565 >            (!fFirst ? f : g).completeExceptionally(ex);
1566 >        }
1567 >
1568 >        checkCompletedWithWrappedCFException(h, ex);
1569 >        assertEquals(0, r.invocationCount);
1570 >        checkCompletedNormally(fFirst ? f : g, v1);
1571 >        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1572 >    }}
1573  
1574      /**
1575       * runAfterBoth result completes exceptionally if action does
1576       */
1577 <    public void testRunAfterBoth3() {
1578 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1579 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1580 <        FailingNoop r = new FailingNoop();
1581 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1582 <        f.complete(one);
1583 <        checkIncomplete(g);
1584 <        f2.complete(two);
1585 <        checkCompletedWithWrappedCFException(g);
1586 <    }
1577 >    public void testRunAfterBoth_actionFailed() {
1578 >        for (ExecutionMode m : ExecutionMode.values())
1579 >        for (boolean fFirst : new boolean[] { true, false })
1580 >        for (Integer v1 : new Integer[] { 1, null })
1581 >        for (Integer v2 : new Integer[] { 2, null })
1582 >    {
1583 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1584 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1585 >        final FailingRunnable r = new FailingRunnable(m);
1586 >
1587 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1588 >        if (fFirst) {
1589 >            f.complete(v1);
1590 >            g.complete(v2);
1591 >        } else {
1592 >            g.complete(v2);
1593 >            f.complete(v1);
1594 >        }
1595 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1596 >
1597 >        checkCompletedWithWrappedCFException(h1);
1598 >        checkCompletedWithWrappedCFException(h2);
1599 >        checkCompletedNormally(f, v1);
1600 >        checkCompletedNormally(g, v2);
1601 >    }}
1602  
1603      /**
1604       * runAfterBoth result completes exceptionally if either source cancelled
1605       */
1606 <    public void testRunAfterBoth4() {
1607 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1608 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1609 <        Noop r = new Noop();
1610 <        CompletableFuture<Void> g = f.runAfterBoth(f2, r);
1611 <        assertTrue(f.cancel(true));
1612 <        f2.complete(two);
1613 <        checkCompletedWithWrappedCancellationException(g);
1614 <        f = new CompletableFuture<Integer>();
1615 <        f2 = new CompletableFuture<Integer>();
1616 <        r = new Noop();
1617 <        g = f.runAfterBoth(f2, r);
1618 <        f.complete(one);
1619 <        assertTrue(f2.cancel(true));
1620 <        checkCompletedWithWrappedCancellationException(g);
1621 <    }
1606 >    public void testRunAfterBoth_sourceCancelled() {
1607 >        for (ExecutionMode m : ExecutionMode.values())
1608 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1609 >        for (boolean createIncomplete : new boolean[] { true, false })
1610 >        for (boolean fFirst : new boolean[] { true, false })
1611 >        for (Integer v1 : new Integer[] { 1, null })
1612 >    {
1613 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1614 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1615 >        final Noop r = new Noop(m);
1616 >
1617 >
1618 >        (fFirst ? f : g).complete(v1);
1619 >        if (!createIncomplete)
1620 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1621 >        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1622 >        if (createIncomplete) {
1623 >            checkIncomplete(h);
1624 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1625 >        }
1626 >
1627 >        checkCompletedWithWrappedCancellationException(h);
1628 >        checkCancelled(!fFirst ? f : g);
1629 >        assertEquals(0, r.invocationCount);
1630 >        checkCompletedNormally(fFirst ? f : g, v1);
1631 >    }}
1632  
1633      /**
1634       * applyToEither result completes normally after normal completion
1635       * of either source
1636       */
1637 <    public void testApplyToEither() {
1638 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1639 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1640 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1641 <        f.complete(one);
1642 <        checkCompletedNormally(g, two);
1643 <        f2.complete(one);
1644 <        checkCompletedNormally(g, two);
1645 <
1646 <        f = new CompletableFuture<Integer>();
1647 <        f.complete(one);
1648 <        f2 = new CompletableFuture<Integer>();
1649 <        g = f.applyToEither(f2, inc);
1650 <        checkCompletedNormally(g, two);
1651 <    }
1637 >    public void testApplyToEither_normalCompletion() {
1638 >        for (ExecutionMode m : ExecutionMode.values())
1639 >        for (boolean createIncomplete : new boolean[] { true, false })
1640 >        for (boolean fFirst : new boolean[] { true, false })
1641 >        for (Integer v1 : new Integer[] { 1, null })
1642 >        for (Integer v2 : new Integer[] { 2, null })
1643 >    {
1644 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1645 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 >        final IncFunction r = new IncFunction(m);
1647 >
1648 >        if (!createIncomplete)
1649 >            if (fFirst) f.complete(v1); else g.complete(v2);
1650 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1651 >        if (createIncomplete) {
1652 >            checkIncomplete(h);
1653 >            assertEquals(0, r.invocationCount);
1654 >            if (fFirst) f.complete(v1); else g.complete(v2);
1655 >        }
1656 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1657 >        if (!fFirst) f.complete(v1); else g.complete(v2);
1658 >
1659 >        checkCompletedNormally(f, v1);
1660 >        checkCompletedNormally(g, v2);
1661 >        checkCompletedNormally(h, inc(fFirst ? v1 : v2));
1662 >    }}
1663 >
1664 >    public void testApplyToEither_normalCompletionBothAvailable() {
1665 >        for (ExecutionMode m : ExecutionMode.values())
1666 >        for (boolean fFirst : new boolean[] { true, false })
1667 >        for (Integer v1 : new Integer[] { 1, null })
1668 >        for (Integer v2 : new Integer[] { 2, null })
1669 >    {
1670 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1671 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1672 >        final IncFunction r = new IncFunction(m);
1673 >
1674 >        if (fFirst) {
1675 >            f.complete(v1);
1676 >            g.complete(v2);
1677 >        } else {
1678 >            g.complete(v2);
1679 >            f.complete(v1);
1680 >        }
1681 >
1682 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1683 >
1684 >        checkCompletedNormally(f, v1);
1685 >        checkCompletedNormally(g, v2);
1686 >
1687 >        // unspecified behavior
1688 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1689 >                   Objects.equals(h.join(), inc(v2)));
1690 >        assertEquals(1, r.invocationCount);
1691 >    }}
1692  
1693      /**
1694       * applyToEither result completes exceptionally after exceptional
1695       * completion of either source
1696       */
1697 <    public void testApplyToEither2() {
1698 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1699 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1700 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1701 <        f.completeExceptionally(new CFException());
1702 <        f2.complete(one);
1703 <        checkCompletedWithWrappedCFException(g);
1697 >    public void testApplyToEither_exceptionalCompletion1() {
1698 >        for (ExecutionMode m : ExecutionMode.values())
1699 >        for (boolean createIncomplete : new boolean[] { true, false })
1700 >        for (boolean fFirst : 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 IncFunction r = new IncFunction(m);
1707 >
1708 >        if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex);
1709 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1710 >        if (createIncomplete) {
1711 >            checkIncomplete(h);
1712 >            assertEquals(0, r.invocationCount);
1713 >            (fFirst ? f : g).completeExceptionally(ex);
1714 >        }
1715 >
1716 >        checkCompletedWithWrappedCFException(h, ex);
1717 >        (!fFirst ? f : g).complete(v1);
1718 >
1719 >        assertEquals(0, r.invocationCount);
1720 >        checkCompletedNormally(!fFirst ? f : g, v1);
1721 >        checkCompletedWithWrappedCFException(fFirst ? f : g, ex);
1722 >        checkCompletedWithWrappedCFException(h, ex);
1723 >    }}
1724 >
1725 >    public void testApplyToEither_exceptionalCompletion2() {
1726 >        for (ExecutionMode m : ExecutionMode.values())
1727 >        for (boolean reverseArgs : new boolean[] { true, false })
1728 >        for (boolean fFirst : new boolean[] { true, false })
1729 >        for (Integer v1 : new Integer[] { 1, null })
1730 >    {
1731 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1732 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1733 >        final IncFunction r1 = new IncFunction(m);
1734 >        final IncFunction r2 = new IncFunction(m);
1735 >        final CFException ex = new CFException();
1736 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1737 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1738 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1739 >        if (fFirst) {
1740 >            f.complete(v1);
1741 >            g.completeExceptionally(ex);
1742 >        } else {
1743 >            g.completeExceptionally(ex);
1744 >            f.complete(v1);
1745 >        }
1746 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1747  
1748 <        f = new CompletableFuture<Integer>();
1749 <        f2 = new CompletableFuture<Integer>();
1750 <        f2.completeExceptionally(new CFException());
1751 <        g = f.applyToEither(f2, inc);
1752 <        checkCompletedWithWrappedCFException(g);
1753 <    }
1748 >        // unspecified behavior
1749 >        try {
1750 >            assertEquals(inc(v1), h1.join());
1751 >            assertEquals(1, r1.invocationCount);
1752 >        } catch (CompletionException ok) {
1753 >            checkCompletedWithWrappedCFException(h1, ex);
1754 >            assertEquals(0, r1.invocationCount);
1755 >        }
1756  
1757 <    /**
1758 <     * applyToEither result completes exceptionally if action does
1759 <     */
1760 <    public void testApplyToEither3() {
1761 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1762 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1763 <        FailingFunction r = new FailingFunction();
884 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
885 <        f2.complete(two);
886 <        checkCompletedWithWrappedCFException(g);
887 <    }
1757 >        try {
1758 >            assertEquals(inc(v1), h2.join());
1759 >            assertEquals(1, r2.invocationCount);
1760 >        } catch (CompletionException ok) {
1761 >            checkCompletedWithWrappedCFException(h2, ex);
1762 >            assertEquals(0, r2.invocationCount);
1763 >        }
1764  
1765 <    /**
1766 <     * applyToEither result completes exceptionally if either source cancelled
1767 <     */
892 <    public void testApplyToEither4() {
893 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
894 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
895 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
896 <        assertTrue(f.cancel(true));
897 <        checkCompletedWithWrappedCancellationException(g);
898 <        f = new CompletableFuture<Integer>();
899 <        f2 = new CompletableFuture<Integer>();
900 <        assertTrue(f2.cancel(true));
901 <        checkCompletedWithWrappedCancellationException(g);
902 <    }
1765 >        checkCompletedWithWrappedCFException(g, ex);
1766 >        checkCompletedNormally(f, v1);
1767 >    }}
1768  
1769      /**
1770 <     * acceptEither result completes normally after normal completion
906 <     * of either source
1770 >     * applyToEither result completes exceptionally if action does
1771       */
1772 <    public void testAcceptEither() {
1773 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1774 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1775 <        IncAction r = new IncAction();
1776 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1777 <        f.complete(one);
1778 <        checkCompletedNormally(g, null);
1779 <        f2.complete(one);
1780 <        checkCompletedNormally(g, null);
1781 <        assertEquals(r.value, 2);
1782 <
1783 <        r = new IncAction();
1784 <        f = new CompletableFuture<Integer>();
1785 <        f.complete(one);
1786 <        f2 = new CompletableFuture<Integer>();
1787 <        g = f.acceptEither(f2, r);
1788 <        checkCompletedNormally(g, null);
1789 <        assertEquals(r.value, 2);
1790 <    }
1772 >    public void testApplyToEither_actionFailed1() {
1773 >        for (ExecutionMode m : ExecutionMode.values())
1774 >        for (Integer v1 : new Integer[] { 1, null })
1775 >        for (Integer v2 : new Integer[] { 2, null })
1776 >    {
1777 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1778 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1779 >        final FailingFunction r = new FailingFunction(m);
1780 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1781 >
1782 >        f.complete(v1);
1783 >        checkCompletedWithWrappedCFException(h);
1784 >        g.complete(v2);
1785 >        checkCompletedNormally(f, v1);
1786 >        checkCompletedNormally(g, v2);
1787 >    }}
1788 >
1789 >    public void testApplyToEither_actionFailed2() {
1790 >        for (ExecutionMode m : ExecutionMode.values())
1791 >        for (Integer v1 : new Integer[] { 1, null })
1792 >        for (Integer v2 : new Integer[] { 2, null })
1793 >    {
1794 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1795 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1796 >        final FailingFunction r = new FailingFunction(m);
1797 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1798 >
1799 >        g.complete(v2);
1800 >        checkCompletedWithWrappedCFException(h);
1801 >        f.complete(v1);
1802 >        checkCompletedNormally(f, v1);
1803 >        checkCompletedNormally(g, v2);
1804 >    }}
1805  
1806      /**
1807 <     * acceptEither result completes exceptionally after exceptional
930 <     * completion of either source
1807 >     * applyToEither result completes exceptionally if either source cancelled
1808       */
1809 <    public void testAcceptEither2() {
1810 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1811 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1812 <        IncAction r = new IncAction();
1813 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1814 <        f.completeExceptionally(new CFException());
1815 <        f2.complete(one);
1816 <        checkCompletedWithWrappedCFException(g);
1817 <
1818 <        r = new IncAction();
1819 <        f = new CompletableFuture<Integer>();
1820 <        f2 = new CompletableFuture<Integer>();
1821 <        f2.completeExceptionally(new CFException());
1822 <        g = f.acceptEither(f2, r);
1823 <        checkCompletedWithWrappedCFException(g);
1824 <    }
1809 >    public void testApplyToEither_sourceCancelled1() {
1810 >        for (ExecutionMode m : ExecutionMode.values())
1811 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1812 >        for (boolean createIncomplete : new boolean[] { true, false })
1813 >        for (boolean fFirst : new boolean[] { true, false })
1814 >        for (Integer v1 : new Integer[] { 1, null })
1815 >    {
1816 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1817 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1818 >        final IncFunction r = new IncFunction(m);
1819 >
1820 >        if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1821 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1822 >        if (createIncomplete) {
1823 >            checkIncomplete(h);
1824 >            assertEquals(0, r.invocationCount);
1825 >            assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning));
1826 >        }
1827 >
1828 >        checkCompletedWithWrappedCancellationException(h);
1829 >        (!fFirst ? f : g).complete(v1);
1830 >
1831 >        assertEquals(0, r.invocationCount);
1832 >        checkCompletedNormally(!fFirst ? f : g, v1);
1833 >        checkCancelled(fFirst ? f : g);
1834 >        checkCompletedWithWrappedCancellationException(h);
1835 >    }}
1836 >
1837 >    public void testApplyToEither_sourceCancelled2() {
1838 >        for (ExecutionMode m : ExecutionMode.values())
1839 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1840 >        for (boolean reverseArgs : new boolean[] { true, false })
1841 >        for (boolean fFirst : new boolean[] { true, false })
1842 >        for (Integer v1 : new Integer[] { 1, null })
1843 >    {
1844 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1845 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1846 >        final IncFunction r1 = new IncFunction(m);
1847 >        final IncFunction r2 = new IncFunction(m);
1848 >        final CFException ex = new CFException();
1849 >        final CompletableFuture<Integer> j = (reverseArgs ? g : f);
1850 >        final CompletableFuture<Integer> k = (reverseArgs ? f : g);
1851 >
1852 >        final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1);
1853 >        if (fFirst) {
1854 >            f.complete(v1);
1855 >            assertTrue(g.cancel(mayInterruptIfRunning));
1856 >        } else {
1857 >            assertTrue(g.cancel(mayInterruptIfRunning));
1858 >            f.complete(v1);
1859 >        }
1860 >        final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2);
1861  
1862 <    /**
1863 <     * acceptEither result completes exceptionally if action does
1864 <     */
1865 <    public void testAcceptEither3() {
1866 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1867 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1868 <        FailingConsumer r = new FailingConsumer();
1869 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
957 <        f2.complete(two);
958 <        checkCompletedWithWrappedCFException(g);
959 <    }
1862 >        // unspecified behavior
1863 >        try {
1864 >            assertEquals(inc(v1), h1.join());
1865 >            assertEquals(1, r1.invocationCount);
1866 >        } catch (CompletionException ok) {
1867 >            checkCompletedWithWrappedCancellationException(h1);
1868 >            assertEquals(0, r1.invocationCount);
1869 >        }
1870  
1871 <    /**
1872 <     * acceptEither result completes exceptionally if either source cancelled
1873 <     */
1874 <    public void testAcceptEither4() {
1875 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1876 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1877 <        IncAction r = new IncAction();
968 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
969 <        assertTrue(f.cancel(true));
970 <        checkCompletedWithWrappedCancellationException(g);
971 <        f = new CompletableFuture<Integer>();
972 <        f2 = new CompletableFuture<Integer>();
973 <        assertTrue(f2.cancel(true));
974 <        checkCompletedWithWrappedCancellationException(g);
975 <    }
1871 >        try {
1872 >            assertEquals(inc(v1), h2.join());
1873 >            assertEquals(1, r2.invocationCount);
1874 >        } catch (CompletionException ok) {
1875 >            checkCompletedWithWrappedCancellationException(h2);
1876 >            assertEquals(0, r2.invocationCount);
1877 >        }
1878  
1879 +        checkCancelled(g);
1880 +        checkCompletedNormally(f, v1);
1881 +    }}
1882  
1883      /**
1884 <     * runAfterEither result completes normally after normal completion
1884 >     * acceptEither result completes normally after normal completion
1885       * of either source
1886       */
1887 <    public void testRunAfterEither() {
1888 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1889 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1890 <        Noop r = new Noop();
1891 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1892 <        f.complete(one);
1893 <        checkCompletedNormally(g, null);
1894 <        f2.complete(one);
1895 <        checkCompletedNormally(g, null);
1896 <        assertTrue(r.ran);
1897 <
1898 <        r = new Noop();
1899 <        f = new CompletableFuture<Integer>();
1900 <        f.complete(one);
1901 <        f2 = new CompletableFuture<Integer>();
1902 <        g = f.runAfterEither(f2, r);
1903 <        checkCompletedNormally(g, null);
1904 <        assertTrue(r.ran);
1905 <    }
1887 >    public void testAcceptEither_normalCompletion1() {
1888 >        for (ExecutionMode m : ExecutionMode.values())
1889 >        for (Integer v1 : new Integer[] { 1, null })
1890 >        for (Integer v2 : new Integer[] { 2, null })
1891 >    {
1892 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1893 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1894 >        final IncAction r = new IncAction();
1895 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1896 >
1897 >        f.complete(v1);
1898 >        checkCompletedNormally(h, null);
1899 >        assertEquals(inc(v1), r.value);
1900 >        g.complete(v2);
1901 >
1902 >        checkCompletedNormally(f, v1);
1903 >        checkCompletedNormally(g, v2);
1904 >        checkCompletedNormally(h, null);
1905 >    }}
1906 >
1907 >    public void testAcceptEither_normalCompletion2() {
1908 >        for (ExecutionMode m : ExecutionMode.values())
1909 >        for (Integer v1 : new Integer[] { 1, null })
1910 >        for (Integer v2 : new Integer[] { 2, null })
1911 >    {
1912 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1913 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1914 >        final IncAction r = new IncAction();
1915 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1916 >
1917 >        g.complete(v2);
1918 >        checkCompletedNormally(h, null);
1919 >        assertEquals(inc(v2), r.value);
1920 >        f.complete(v1);
1921 >
1922 >        checkCompletedNormally(f, v1);
1923 >        checkCompletedNormally(g, v2);
1924 >        checkCompletedNormally(h, null);
1925 >    }}
1926 >
1927 >    public void testAcceptEither_normalCompletion3() {
1928 >        for (ExecutionMode m : ExecutionMode.values())
1929 >        for (Integer v1 : new Integer[] { 1, null })
1930 >        for (Integer v2 : new Integer[] { 2, null })
1931 >    {
1932 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1933 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1934 >        final IncAction r = new IncAction();
1935 >
1936 >        f.complete(v1);
1937 >        g.complete(v2);
1938 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1939 >
1940 >        checkCompletedNormally(h, null);
1941 >        checkCompletedNormally(f, v1);
1942 >        checkCompletedNormally(g, v2);
1943 >
1944 >        // unspecified behavior
1945 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
1946 >                   Objects.equals(r.value, inc(v2)));
1947 >    }}
1948  
1949      /**
1950 <     * runAfterEither result completes exceptionally after exceptional
1950 >     * acceptEither result completes exceptionally after exceptional
1951       * completion of either source
1952       */
1953 <    public void testRunAfterEither2() {
1954 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1955 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1956 <        Noop r = new Noop();
1957 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1958 <        f.completeExceptionally(new CFException());
1959 <        f2.complete(one);
1960 <        checkCompletedWithWrappedCFException(g);
1953 >    public void testAcceptEither_exceptionalCompletion1() {
1954 >        for (ExecutionMode m : ExecutionMode.values())
1955 >        for (Integer v1 : new Integer[] { 1, null })
1956 >    {
1957 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1958 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1959 >        final IncAction r = new IncAction();
1960 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1961 >        final CFException ex = new CFException();
1962 >
1963 >        f.completeExceptionally(ex);
1964 >        checkCompletedWithWrappedCFException(h, ex);
1965 >        g.complete(v1);
1966 >
1967 >        assertEquals(0, r.invocationCount);
1968 >        checkCompletedNormally(g, v1);
1969 >        checkCompletedWithWrappedCFException(f, ex);
1970 >        checkCompletedWithWrappedCFException(h, ex);
1971 >    }}
1972 >
1973 >    public void testAcceptEither_exceptionalCompletion2() {
1974 >        for (ExecutionMode m : ExecutionMode.values())
1975 >        for (Integer v1 : new Integer[] { 1, null })
1976 >    {
1977 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1978 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1979 >        final IncAction r = new IncAction();
1980 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1981 >        final CFException ex = new CFException();
1982 >
1983 >        g.completeExceptionally(ex);
1984 >        checkCompletedWithWrappedCFException(h, ex);
1985 >        f.complete(v1);
1986 >
1987 >        assertEquals(0, r.invocationCount);
1988 >        checkCompletedNormally(f, v1);
1989 >        checkCompletedWithWrappedCFException(g, ex);
1990 >        checkCompletedWithWrappedCFException(h, ex);
1991 >    }}
1992 >
1993 >    public void testAcceptEither_exceptionalCompletion3() {
1994 >        for (ExecutionMode m : ExecutionMode.values())
1995 >        for (Integer v1 : new Integer[] { 1, null })
1996 >    {
1997 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1998 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1999 >        final IncAction r = new IncAction();
2000 >        final CFException ex = new CFException();
2001 >
2002 >        g.completeExceptionally(ex);
2003 >        f.complete(v1);
2004 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2005  
2006 <        r = new Noop();
2007 <        f = new CompletableFuture<Integer>();
2008 <        f2 = new CompletableFuture<Integer>();
2009 <        f2.completeExceptionally(new CFException());
2010 <        g = f.runAfterEither(f2, r);
2011 <        checkCompletedWithWrappedCFException(g);
2012 <    }
2013 <
2014 <    /**
2015 <     * runAfterEither result completes exceptionally if action does
2016 <     */
2017 <    public void testRunAfterEither3() {
2018 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2019 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2020 <        FailingNoop r = new FailingNoop();
2021 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2022 <        f2.complete(two);
2023 <        checkCompletedWithWrappedCFException(g);
2024 <    }
2025 <
2026 <    /**
2027 <     * runAfterEither result completes exceptionally if either source cancelled
2028 <     */
2029 <    public void testRunAfterEither4() {
2030 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2031 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2032 <        Noop r = new Noop();
1042 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1043 <        assertTrue(f.cancel(true));
1044 <        checkCompletedWithWrappedCancellationException(g);
1045 <        f = new CompletableFuture<Integer>();
1046 <        f2 = new CompletableFuture<Integer>();
1047 <        assertTrue(f2.cancel(true));
1048 <        checkCompletedWithWrappedCancellationException(g);
1049 <    }
1050 <
1051 <    /**
1052 <     * thenCompose result completes normally after normal completion of source
1053 <     */
1054 <    public void testThenCompose() {
1055 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1056 <        CompletableFutureInc r = new CompletableFutureInc();
1057 <        CompletableFuture<Integer> g = f.thenCompose(r);
1058 <        f.complete(one);
1059 <        checkCompletedNormally(g, two);
1060 <    }
1061 <
1062 <    /**
1063 <     * thenCompose result completes exceptionally after exceptional
1064 <     * completion of source
1065 <     */
1066 <    public void testThenCompose2() {
1067 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1068 <        CompletableFutureInc r = new CompletableFutureInc();
1069 <        CompletableFuture<Integer> g = f.thenCompose(r);
1070 <        f.completeExceptionally(new CFException());
1071 <        checkCompletedWithWrappedCFException(g);
1072 <    }
1073 <
1074 <    /**
1075 <     * thenCompose result completes exceptionally if action does
1076 <     */
1077 <    public void testThenCompose3() {
1078 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1079 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1080 <        CompletableFuture<Integer> g = f.thenCompose(r);
1081 <        f.complete(one);
1082 <        checkCompletedWithWrappedCFException(g);
1083 <    }
1084 <
1085 <    /**
1086 <     * thenCompose result completes exceptionally if source cancelled
1087 <     */
1088 <    public void testThenCompose4() {
1089 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1090 <        CompletableFutureInc r = new CompletableFutureInc();
1091 <        CompletableFuture<Integer> g = f.thenCompose(r);
1092 <        assertTrue(f.cancel(true));
1093 <        checkCompletedWithWrappedCancellationException(g);
1094 <    }
1095 <
1096 <
1097 <    // asyncs
1098 <
1099 <    /**
1100 <     * thenRunAsync result completes normally after normal completion of source
1101 <     */
1102 <    public void testThenRunAsync() {
1103 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1104 <        Noop r = new Noop();
1105 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1106 <        f.complete(null);
1107 <        checkCompletedNormally(g, null);
1108 <
1109 <        // reordered version
1110 <        f = new CompletableFuture<Integer>();
1111 <        f.complete(null);
1112 <        r = new Noop();
1113 <        g = f.thenRunAsync(r);
1114 <        checkCompletedNormally(g, null);
1115 <    }
2006 >        // unspecified behavior
2007 >        Integer v;
2008 >        try {
2009 >            assertNull(h.join());
2010 >            assertEquals(1, r.invocationCount);
2011 >            assertEquals(inc(v1), r.value);
2012 >        } catch (CompletionException ok) {
2013 >            checkCompletedWithWrappedCFException(h, ex);
2014 >            assertEquals(0, r.invocationCount);
2015 >        }
2016 >
2017 >        checkCompletedWithWrappedCFException(g, ex);
2018 >        checkCompletedNormally(f, v1);
2019 >    }}
2020 >
2021 >    public void testAcceptEither_exceptionalCompletion4() {
2022 >        for (ExecutionMode m : ExecutionMode.values())
2023 >        for (Integer v1 : new Integer[] { 1, null })
2024 >    {
2025 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2026 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2027 >        final IncAction r = new IncAction();
2028 >        final CFException ex = new CFException();
2029 >
2030 >        f.completeExceptionally(ex);
2031 >        g.complete(v1);
2032 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2033  
2034 <    /**
2035 <     * thenRunAsync result completes exceptionally after exceptional
1119 <     * completion of source
1120 <     */
1121 <    public void testThenRunAsync2() {
1122 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1123 <        Noop r = new Noop();
1124 <        CompletableFuture<Void> g = f.thenRunAsync(r);
1125 <        f.completeExceptionally(new CFException());
2034 >        // unspecified behavior
2035 >        Integer v;
2036          try {
2037 <            g.join();
2038 <            shouldThrow();
2039 <        } catch (Exception ok) {
2037 >            assertNull(h.join());
2038 >            assertEquals(1, r.invocationCount);
2039 >            assertEquals(inc(v1), r.value);
2040 >        } catch (CompletionException ok) {
2041 >            checkCompletedWithWrappedCFException(h, ex);
2042 >            assertEquals(0, r.invocationCount);
2043          }
1131        checkCompletedWithWrappedCFException(g);
1132    }
1133
1134    /**
1135     * thenRunAsync result completes exceptionally if action does
1136     */
1137    public void testThenRunAsync3() {
1138        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1139        FailingNoop r = new FailingNoop();
1140        CompletableFuture<Void> g = f.thenRunAsync(r);
1141        f.complete(null);
1142        checkCompletedWithWrappedCFException(g);
1143    }
1144
1145    /**
1146     * thenRunAsync result completes exceptionally if source cancelled
1147     */
1148    public void testThenRunAsync4() {
1149        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1150        Noop r = new Noop();
1151        CompletableFuture<Void> g = f.thenRunAsync(r);
1152        assertTrue(f.cancel(true));
1153        checkCompletedWithWrappedCancellationException(g);
1154    }
1155
1156    /**
1157     * thenApplyAsync result completes normally after normal completion of source
1158     */
1159    public void testThenApplyAsync() {
1160        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1161        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1162        f.complete(one);
1163        checkCompletedNormally(g, two);
1164    }
1165
1166    /**
1167     * thenApplyAsync result completes exceptionally after exceptional
1168     * completion of source
1169     */
1170    public void testThenApplyAsync2() {
1171        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1172        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1173        f.completeExceptionally(new CFException());
1174        checkCompletedWithWrappedCFException(g);
1175    }
1176
1177    /**
1178     * thenApplyAsync result completes exceptionally if action does
1179     */
1180    public void testThenApplyAsync3() {
1181        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1182        FailingFunction r = new FailingFunction();
1183        CompletableFuture<Integer> g = f.thenApplyAsync(r);
1184        f.complete(null);
1185        checkCompletedWithWrappedCFException(g);
1186    }
1187
1188    /**
1189     * thenApplyAsync result completes exceptionally if source cancelled
1190     */
1191    public void testThenApplyAsync4() {
1192        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1193        CompletableFuture<Integer> g = f.thenApplyAsync(inc);
1194        assertTrue(f.cancel(true));
1195        checkCompletedWithWrappedCancellationException(g);
1196    }
1197
1198    /**
1199     * thenAcceptAsync result completes normally after normal
1200     * completion of source
1201     */
1202    public void testThenAcceptAsync() {
1203        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1204        IncAction r = new IncAction();
1205        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1206        f.complete(one);
1207        checkCompletedNormally(g, null);
1208        assertEquals(r.value, 2);
1209    }
1210
1211    /**
1212     * thenAcceptAsync result completes exceptionally after exceptional
1213     * completion of source
1214     */
1215    public void testThenAcceptAsync2() {
1216        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1217        IncAction r = new IncAction();
1218        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1219        f.completeExceptionally(new CFException());
1220        checkCompletedWithWrappedCFException(g);
1221    }
1222
1223    /**
1224     * thenAcceptAsync result completes exceptionally if action does
1225     */
1226    public void testThenAcceptAsync3() {
1227        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1228        FailingConsumer r = new FailingConsumer();
1229        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1230        f.complete(null);
1231        checkCompletedWithWrappedCFException(g);
1232    }
1233
1234    /**
1235     * thenAcceptAsync result completes exceptionally if source cancelled
1236     */
1237    public void testThenAcceptAsync4() {
1238        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1239        IncAction r = new IncAction();
1240        CompletableFuture<Void> g = f.thenAcceptAsync(r);
1241        assertTrue(f.cancel(true));
1242        checkCompletedWithWrappedCancellationException(g);
1243    }
1244    /**
1245     * thenCombineAsync result completes normally after normal
1246     * completion of sources
1247     */
1248    public void testThenCombineAsync() {
1249        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1250        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1251        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1252        f.complete(one);
1253        checkIncomplete(g);
1254        f2.complete(two);
1255        checkCompletedNormally(g, three);
1256    }
1257
1258    /**
1259     * thenCombineAsync result completes exceptionally after exceptional
1260     * completion of source
1261     */
1262    public void testThenCombineAsync2() {
1263        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1264        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1265        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1266        f.completeExceptionally(new CFException());
1267        f2.complete(two);
1268        checkCompletedWithWrappedCFException(g);
1269
1270        f = new CompletableFuture<Integer>();
1271        f2 = new CompletableFuture<Integer>();
1272        g = f.thenCombineAsync(f2, add);
1273        f.complete(one);
1274        f2.completeExceptionally(new CFException());
1275        checkCompletedWithWrappedCFException(g);
1276    }
1277
1278    /**
1279     * thenCombineAsync result completes exceptionally if action does
1280     */
1281    public void testThenCombineAsync3() {
1282        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1283        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1284        FailingBiFunction r = new FailingBiFunction();
1285        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1286        f.complete(one);
1287        checkIncomplete(g);
1288        f2.complete(two);
1289        checkCompletedWithWrappedCFException(g);
1290    }
1291
1292    /**
1293     * thenCombineAsync result completes exceptionally if either source cancelled
1294     */
1295    public void testThenCombineAsync4() {
1296        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1297        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1298        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1299        assertTrue(f.cancel(true));
1300        f2.complete(two);
1301        checkCompletedWithWrappedCancellationException(g);
1302
1303        f = new CompletableFuture<Integer>();
1304        f2 = new CompletableFuture<Integer>();
1305        g = f.thenCombineAsync(f2, add);
1306        f.complete(one);
1307        assertTrue(f2.cancel(true));
1308        checkCompletedWithWrappedCancellationException(g);
1309    }
1310
1311    /**
1312     * thenAcceptBothAsync result completes normally after normal
1313     * completion of sources
1314     */
1315    public void testThenAcceptBothAsync() {
1316        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1317        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1318        AddAction r = new AddAction();
1319        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1320        f.complete(one);
1321        checkIncomplete(g);
1322        f2.complete(two);
1323        checkCompletedNormally(g, null);
1324        assertEquals(r.value, 3);
1325    }
1326
1327    /**
1328     * thenAcceptBothAsync result completes exceptionally after exceptional
1329     * completion of source
1330     */
1331    public void testThenAcceptBothAsync2() {
1332        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1333        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1334        AddAction r = new AddAction();
1335        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1336        f.completeExceptionally(new CFException());
1337        f2.complete(two);
1338        checkCompletedWithWrappedCFException(g);
1339
1340        r = new AddAction();
1341        f = new CompletableFuture<Integer>();
1342        f2 = new CompletableFuture<Integer>();
1343        g = f.thenAcceptBothAsync(f2, r);
1344        f.complete(one);
1345        f2.completeExceptionally(new CFException());
1346        checkCompletedWithWrappedCFException(g);
1347    }
1348
1349    /**
1350     * thenAcceptBothAsync result completes exceptionally if action does
1351     */
1352    public void testThenAcceptBothAsync3() {
1353        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1354        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1355        FailingBiConsumer r = new FailingBiConsumer();
1356        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1357        f.complete(one);
1358        checkIncomplete(g);
1359        f2.complete(two);
1360        checkCompletedWithWrappedCFException(g);
1361    }
1362
1363    /**
1364     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1365     */
1366    public void testThenAcceptBothAsync4() {
1367        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1368        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1369        AddAction r = new AddAction();
1370        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r);
1371        assertTrue(f.cancel(true));
1372        f2.complete(two);
1373        checkCompletedWithWrappedCancellationException(g);
1374
1375        r = new AddAction();
1376        f = new CompletableFuture<Integer>();
1377        f2 = new CompletableFuture<Integer>();
1378        g = f.thenAcceptBothAsync(f2, r);
1379        f.complete(one);
1380        assertTrue(f2.cancel(true));
1381        checkCompletedWithWrappedCancellationException(g);
1382    }
1383
1384    /**
1385     * runAfterBothAsync result completes normally after normal
1386     * completion of sources
1387     */
1388    public void testRunAfterBothAsync() {
1389        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1390        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1391        Noop r = new Noop();
1392        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1393        f.complete(one);
1394        checkIncomplete(g);
1395        f2.complete(two);
1396        checkCompletedNormally(g, null);
1397        assertTrue(r.ran);
1398    }
1399
1400    /**
1401     * runAfterBothAsync result completes exceptionally after exceptional
1402     * completion of source
1403     */
1404    public void testRunAfterBothAsync2() {
1405        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1406        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1407        Noop r = new Noop();
1408        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1409        f.completeExceptionally(new CFException());
1410        f2.complete(two);
1411        checkCompletedWithWrappedCFException(g);
1412
1413        r = new Noop();
1414        f = new CompletableFuture<Integer>();
1415        f2 = new CompletableFuture<Integer>();
1416        g = f.runAfterBothAsync(f2, r);
1417        f.complete(one);
1418        f2.completeExceptionally(new CFException());
1419        checkCompletedWithWrappedCFException(g);
1420    }
1421
1422    /**
1423     * runAfterBothAsync result completes exceptionally if action does
1424     */
1425    public void testRunAfterBothAsync3() {
1426        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1427        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1428        FailingNoop r = new FailingNoop();
1429        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1430        f.complete(one);
1431        checkIncomplete(g);
1432        f2.complete(two);
1433        checkCompletedWithWrappedCFException(g);
1434    }
1435
1436    /**
1437     * runAfterBothAsync result completes exceptionally if either source cancelled
1438     */
1439    public void testRunAfterBothAsync4() {
1440        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1441        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1442        Noop r = new Noop();
1443        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r);
1444        assertTrue(f.cancel(true));
1445        f2.complete(two);
1446        checkCompletedWithWrappedCancellationException(g);
1447
1448        r = new Noop();
1449        f = new CompletableFuture<Integer>();
1450        f2 = new CompletableFuture<Integer>();
1451        g = f.runAfterBothAsync(f2, r);
1452        f.complete(one);
1453        assertTrue(f2.cancel(true));
1454        checkCompletedWithWrappedCancellationException(g);
1455    }
1456
1457    /**
1458     * applyToEitherAsync result completes normally after normal
1459     * completion of sources
1460     */
1461    public void testApplyToEitherAsync() {
1462        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1463        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1464        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1465        f.complete(one);
1466        checkCompletedNormally(g, two);
1467
1468        f = new CompletableFuture<Integer>();
1469        f.complete(one);
1470        f2 = new CompletableFuture<Integer>();
1471        g = f.applyToEitherAsync(f2, inc);
1472        checkCompletedNormally(g, two);
1473    }
1474
1475    /**
1476     * applyToEitherAsync result completes exceptionally after exceptional
1477     * completion of source
1478     */
1479    public void testApplyToEitherAsync2() {
1480        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1481        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1482        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1483        f.completeExceptionally(new CFException());
1484        checkCompletedWithWrappedCFException(g);
1485
1486        f = new CompletableFuture<Integer>();
1487        f2 = new CompletableFuture<Integer>();
1488        f2.completeExceptionally(new CFException());
1489        g = f.applyToEitherAsync(f2, inc);
1490        f.complete(one);
1491        checkCompletedWithWrappedCFException(g);
1492    }
1493
1494    /**
1495     * applyToEitherAsync result completes exceptionally if action does
1496     */
1497    public void testApplyToEitherAsync3() {
1498        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1499        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1500        FailingFunction r = new FailingFunction();
1501        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
1502        f.complete(one);
1503        checkCompletedWithWrappedCFException(g);
1504    }
1505
1506    /**
1507     * applyToEitherAsync result completes exceptionally if either source cancelled
1508     */
1509    public void testApplyToEitherAsync4() {
1510        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1511        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1512        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
1513        assertTrue(f.cancel(true));
1514        checkCompletedWithWrappedCancellationException(g);
1515
1516        f = new CompletableFuture<Integer>();
1517        f2 = new CompletableFuture<Integer>();
1518        assertTrue(f2.cancel(true));
1519        g = f.applyToEitherAsync(f2, inc);
1520        checkCompletedWithWrappedCancellationException(g);
1521    }
1522
1523    /**
1524     * acceptEitherAsync result completes normally after normal
1525     * completion of sources
1526     */
1527    public void testAcceptEitherAsync() {
1528        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1529        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1530        IncAction r = new IncAction();
1531        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1532        f.complete(one);
1533        checkCompletedNormally(g, null);
1534        assertEquals(r.value, 2);
1535
1536        r = new IncAction();
1537        f = new CompletableFuture<Integer>();
1538        f.complete(one);
1539        f2 = new CompletableFuture<Integer>();
1540        g = f.acceptEitherAsync(f2, r);
1541        checkCompletedNormally(g, null);
1542        assertEquals(r.value, 2);
1543    }
1544
1545    /**
1546     * acceptEitherAsync result completes exceptionally after exceptional
1547     * completion of source
1548     */
1549    public void testAcceptEitherAsync2() {
1550        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1551        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1552        IncAction r = new IncAction();
1553        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1554        f.completeExceptionally(new CFException());
1555        checkCompletedWithWrappedCFException(g);
1556
1557        r = new IncAction();
1558        f = new CompletableFuture<Integer>();
1559        f2 = new CompletableFuture<Integer>();
1560        f2.completeExceptionally(new CFException());
1561        g = f.acceptEitherAsync(f2, r);
1562        f.complete(one);
1563        checkCompletedWithWrappedCFException(g);
1564    }
1565
1566    /**
1567     * acceptEitherAsync result completes exceptionally if action does
1568     */
1569    public void testAcceptEitherAsync3() {
1570        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1571        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1572        FailingConsumer r = new FailingConsumer();
1573        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1574        f.complete(one);
1575        checkCompletedWithWrappedCFException(g);
1576    }
1577
1578    /**
1579     * acceptEitherAsync result completes exceptionally if either
1580     * source cancelled
1581     */
1582    public void testAcceptEitherAsync4() {
1583        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1584        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1585        IncAction r = new IncAction();
1586        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
1587        assertTrue(f.cancel(true));
1588        checkCompletedWithWrappedCancellationException(g);
1589
1590        r = new IncAction();
1591        f = new CompletableFuture<Integer>();
1592        f2 = new CompletableFuture<Integer>();
1593        assertTrue(f2.cancel(true));
1594        g = f.acceptEitherAsync(f2, r);
1595        checkCompletedWithWrappedCancellationException(g);
1596    }
1597
1598    /**
1599     * runAfterEitherAsync result completes normally after normal
1600     * completion of sources
1601     */
1602    public void testRunAfterEitherAsync() {
1603        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1604        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1605        Noop r = new Noop();
1606        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1607        f.complete(one);
1608        checkCompletedNormally(g, null);
1609        assertTrue(r.ran);
1610
1611        r = new Noop();
1612        f = new CompletableFuture<Integer>();
1613        f.complete(one);
1614        f2 = new CompletableFuture<Integer>();
1615        g = f.runAfterEitherAsync(f2, r);
1616        checkCompletedNormally(g, null);
1617        assertTrue(r.ran);
1618    }
1619
1620    /**
1621     * runAfterEitherAsync result completes exceptionally after exceptional
1622     * completion of source
1623     */
1624    public void testRunAfterEitherAsync2() {
1625        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1626        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1627        Noop r = new Noop();
1628        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1629        f.completeExceptionally(new CFException());
1630        checkCompletedWithWrappedCFException(g);
1631
1632        r = new Noop();
1633        f = new CompletableFuture<Integer>();
1634        f2 = new CompletableFuture<Integer>();
1635        f2.completeExceptionally(new CFException());
1636        g = f.runAfterEitherAsync(f2, r);
1637        f.complete(one);
1638        checkCompletedWithWrappedCFException(g);
1639    }
1640
1641    /**
1642     * runAfterEitherAsync result completes exceptionally if action does
1643     */
1644    public void testRunAfterEitherAsync3() {
1645        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1646        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1647        FailingNoop r = new FailingNoop();
1648        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1649        f.complete(one);
1650        checkCompletedWithWrappedCFException(g);
1651    }
2044  
2045 <    /**
2046 <     * runAfterEitherAsync result completes exceptionally if either
2047 <     * source cancelled
1656 <     */
1657 <    public void testRunAfterEitherAsync4() {
1658 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1659 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1660 <        Noop r = new Noop();
1661 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
1662 <        assertTrue(f.cancel(true));
1663 <        checkCompletedWithWrappedCancellationException(g);
1664 <
1665 <        r = new Noop();
1666 <        f = new CompletableFuture<Integer>();
1667 <        f2 = new CompletableFuture<Integer>();
1668 <        assertTrue(f2.cancel(true));
1669 <        g = f.runAfterEitherAsync(f2, r);
1670 <        checkCompletedWithWrappedCancellationException(g);
1671 <    }
1672 <
1673 <    /**
1674 <     * thenCompse result completes normally after normal completion of source
1675 <     */
1676 <    public void testThenComposeAsync() {
1677 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1678 <        CompletableFutureInc r = new CompletableFutureInc();
1679 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1680 <        f.complete(one);
1681 <        checkCompletedNormally(g, two);
1682 <    }
2045 >        checkCompletedWithWrappedCFException(f, ex);
2046 >        checkCompletedNormally(g, v1);
2047 >    }}
2048  
2049      /**
2050 <     * thenComposeAsync result completes exceptionally after exceptional
1686 <     * completion of source
2050 >     * acceptEither result completes exceptionally if action does
2051       */
2052 <    public void testThenComposeAsync2() {
2053 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2054 <        CompletableFutureInc r = new CompletableFutureInc();
2055 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2056 <        f.completeExceptionally(new CFException());
2057 <        checkCompletedWithWrappedCFException(g);
2058 <    }
2052 >    public void testAcceptEither_actionFailed1() {
2053 >        for (ExecutionMode m : ExecutionMode.values())
2054 >        for (Integer v1 : new Integer[] { 1, null })
2055 >        for (Integer v2 : new Integer[] { 2, null })
2056 >    {
2057 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2058 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2059 >        final FailingConsumer r = new FailingConsumer(m);
2060 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2061 >
2062 >        f.complete(v1);
2063 >        checkCompletedWithWrappedCFException(h);
2064 >        g.complete(v2);
2065 >        checkCompletedNormally(f, v1);
2066 >        checkCompletedNormally(g, v2);
2067 >    }}
2068 >
2069 >    public void testAcceptEither_actionFailed2() {
2070 >        for (ExecutionMode m : ExecutionMode.values())
2071 >        for (Integer v1 : new Integer[] { 1, null })
2072 >        for (Integer v2 : new Integer[] { 2, null })
2073 >    {
2074 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2075 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2076 >        final FailingConsumer r = new FailingConsumer(m);
2077 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2078 >
2079 >        g.complete(v2);
2080 >        checkCompletedWithWrappedCFException(h);
2081 >        f.complete(v1);
2082 >        checkCompletedNormally(f, v1);
2083 >        checkCompletedNormally(g, v2);
2084 >    }}
2085  
2086      /**
2087 <     * thenComposeAsync result completes exceptionally if action does
2087 >     * acceptEither result completes exceptionally if either source cancelled
2088       */
2089 <    public void testThenComposeAsync3() {
2090 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2091 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2092 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2093 <        f.complete(one);
2094 <        checkCompletedWithWrappedCFException(g);
2095 <    }
2089 >    public void testAcceptEither_sourceCancelled1() {
2090 >        for (ExecutionMode m : ExecutionMode.values())
2091 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2092 >        for (Integer v1 : new Integer[] { 1, null })
2093 >    {
2094 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2095 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2096 >        final IncAction r = new IncAction();
2097 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2098 >
2099 >        assertTrue(f.cancel(mayInterruptIfRunning));
2100 >        checkCompletedWithWrappedCancellationException(h);
2101 >        g.complete(v1);
2102  
2103 <    /**
2104 <     * thenComposeAsync result completes exceptionally if source cancelled
2105 <     */
2106 <    public void testThenComposeAsync4() {
2107 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2108 <        CompletableFutureInc r = new CompletableFutureInc();
2109 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
2110 <        assertTrue(f.cancel(true));
2111 <        checkCompletedWithWrappedCancellationException(g);
2112 <    }
2113 <
2114 <
2115 <    // aaync with explicit executors
2116 <
2117 <    /**
2118 <     * thenRunAsync result completes normally after normal completion of source
2119 <     */
2120 <    public void testThenRunAsyncE() {
2121 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2122 <        Noop r = new Noop();
2123 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
2124 <        f.complete(null);
2125 <        checkCompletedNormally(g, null);
2103 >        checkCancelled(f);
2104 >        assertEquals(0, r.invocationCount);
2105 >        checkCompletedNormally(g, v1);
2106 >        checkCompletedWithWrappedCancellationException(h);
2107 >    }}
2108 >
2109 >    public void testAcceptEither_sourceCancelled2() {
2110 >        for (ExecutionMode m : ExecutionMode.values())
2111 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2112 >        for (Integer v1 : new Integer[] { 1, null })
2113 >    {
2114 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2115 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2116 >        final IncAction r = new IncAction();
2117 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2118 >
2119 >        assertTrue(g.cancel(mayInterruptIfRunning));
2120 >        checkCompletedWithWrappedCancellationException(h);
2121 >        f.complete(v1);
2122 >
2123 >        checkCancelled(g);
2124 >        assertEquals(0, r.invocationCount);
2125 >        checkCompletedNormally(f, v1);
2126 >        checkCompletedWithWrappedCancellationException(h);
2127 >    }}
2128 >
2129 >    public void testAcceptEither_sourceCancelled3() {
2130 >        for (ExecutionMode m : ExecutionMode.values())
2131 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2132 >        for (Integer v1 : new Integer[] { 1, null })
2133 >    {
2134 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2135 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2136 >        final IncAction r = new IncAction();
2137 >
2138 >        assertTrue(g.cancel(mayInterruptIfRunning));
2139 >        f.complete(v1);
2140 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2141  
2142 <        // reordered version
2143 <        f = new CompletableFuture<Integer>();
2144 <        f.complete(null);
2145 <        r = new Noop();
2146 <        g = f.thenRunAsync(r, new ThreadExecutor());
2147 <        checkCompletedNormally(g, null);
2148 <    }
2142 >        // unspecified behavior
2143 >        Integer v;
2144 >        try {
2145 >            assertNull(h.join());
2146 >            assertEquals(1, r.invocationCount);
2147 >            assertEquals(inc(v1), r.value);
2148 >        } catch (CompletionException ok) {
2149 >            checkCompletedWithWrappedCancellationException(h);
2150 >            assertEquals(0, r.invocationCount);
2151 >        }
2152 >
2153 >        checkCancelled(g);
2154 >        checkCompletedNormally(f, v1);
2155 >    }}
2156 >
2157 >    public void testAcceptEither_sourceCancelled4() {
2158 >        for (ExecutionMode m : ExecutionMode.values())
2159 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2160 >        for (Integer v1 : new Integer[] { 1, null })
2161 >    {
2162 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2163 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2164 >        final IncAction r = new IncAction();
2165 >
2166 >        assertTrue(f.cancel(mayInterruptIfRunning));
2167 >        g.complete(v1);
2168 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2169  
2170 <    /**
2171 <     * thenRunAsync result completes exceptionally after exceptional
1741 <     * completion of source
1742 <     */
1743 <    public void testThenRunAsync2E() {
1744 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1745 <        Noop r = new Noop();
1746 <        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1747 <        f.completeExceptionally(new CFException());
2170 >        // unspecified behavior
2171 >        Integer v;
2172          try {
2173 <            g.join();
2174 <            shouldThrow();
2175 <        } catch (Exception ok) {
2173 >            assertNull(h.join());
2174 >            assertEquals(1, r.invocationCount);
2175 >            assertEquals(inc(v1), r.value);
2176 >        } catch (CompletionException ok) {
2177 >            checkCompletedWithWrappedCancellationException(h);
2178 >            assertEquals(0, r.invocationCount);
2179          }
1753        checkCompletedWithWrappedCFException(g);
1754    }
1755
1756    /**
1757     * thenRunAsync result completes exceptionally if action does
1758     */
1759    public void testThenRunAsync3E() {
1760        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1761        FailingNoop r = new FailingNoop();
1762        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1763        f.complete(null);
1764        checkCompletedWithWrappedCFException(g);
1765    }
1766
1767    /**
1768     * thenRunAsync result completes exceptionally if source cancelled
1769     */
1770    public void testThenRunAsync4E() {
1771        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1772        Noop r = new Noop();
1773        CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor());
1774        assertTrue(f.cancel(true));
1775        checkCompletedWithWrappedCancellationException(g);
1776    }
1777
1778    /**
1779     * thenApplyAsync result completes normally after normal completion of source
1780     */
1781    public void testThenApplyAsyncE() {
1782        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1783        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1784        f.complete(one);
1785        checkCompletedNormally(g, two);
1786    }
1787
1788    /**
1789     * thenApplyAsync result completes exceptionally after exceptional
1790     * completion of source
1791     */
1792    public void testThenApplyAsync2E() {
1793        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1794        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1795        f.completeExceptionally(new CFException());
1796        checkCompletedWithWrappedCFException(g);
1797    }
1798
1799    /**
1800     * thenApplyAsync result completes exceptionally if action does
1801     */
1802    public void testThenApplyAsync3E() {
1803        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1804        FailingFunction r = new FailingFunction();
1805        CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor());
1806        f.complete(null);
1807        checkCompletedWithWrappedCFException(g);
1808    }
1809
1810    /**
1811     * thenApplyAsync result completes exceptionally if source cancelled
1812     */
1813    public void testThenApplyAsync4E() {
1814        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1815        CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor());
1816        assertTrue(f.cancel(true));
1817        checkCompletedWithWrappedCancellationException(g);
1818    }
1819
1820    /**
1821     * thenAcceptAsync result completes normally after normal
1822     * completion of source
1823     */
1824    public void testThenAcceptAsyncE() {
1825        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1826        IncAction r = new IncAction();
1827        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1828        f.complete(one);
1829        checkCompletedNormally(g, null);
1830        assertEquals(r.value, 2);
1831    }
2180  
2181 <    /**
2182 <     * thenAcceptAsync result completes exceptionally after exceptional
2183 <     * completion of source
1836 <     */
1837 <    public void testThenAcceptAsync2E() {
1838 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1839 <        IncAction r = new IncAction();
1840 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1841 <        f.completeExceptionally(new CFException());
1842 <        checkCompletedWithWrappedCFException(g);
1843 <    }
1844 <
1845 <    /**
1846 <     * thenAcceptAsync result completes exceptionally if action does
1847 <     */
1848 <    public void testThenAcceptAsync3E() {
1849 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1850 <        FailingConsumer r = new FailingConsumer();
1851 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1852 <        f.complete(null);
1853 <        checkCompletedWithWrappedCFException(g);
1854 <    }
1855 <
1856 <    /**
1857 <     * thenAcceptAsync result completes exceptionally if source cancelled
1858 <     */
1859 <    public void testThenAcceptAsync4E() {
1860 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1861 <        IncAction r = new IncAction();
1862 <        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
1863 <        assertTrue(f.cancel(true));
1864 <        checkCompletedWithWrappedCancellationException(g);
1865 <    }
1866 <    /**
1867 <     * thenCombineAsync result completes normally after normal
1868 <     * completion of sources
1869 <     */
1870 <    public void testThenCombineAsyncE() {
1871 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1872 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1873 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1874 <        f.complete(one);
1875 <        checkIncomplete(g);
1876 <        f2.complete(two);
1877 <        checkCompletedNormally(g, three);
1878 <    }
1879 <
1880 <    /**
1881 <     * thenCombineAsync result completes exceptionally after exceptional
1882 <     * completion of source
1883 <     */
1884 <    public void testThenCombineAsync2E() {
1885 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1886 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1887 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1888 <        f.completeExceptionally(new CFException());
1889 <        f2.complete(two);
1890 <        checkCompletedWithWrappedCFException(g);
1891 <
1892 <        f = new CompletableFuture<Integer>();
1893 <        f2 = new CompletableFuture<Integer>();
1894 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1895 <        f.complete(one);
1896 <        f2.completeExceptionally(new CFException());
1897 <        checkCompletedWithWrappedCFException(g);
1898 <    }
1899 <
1900 <    /**
1901 <     * thenCombineAsync result completes exceptionally if action does
1902 <     */
1903 <    public void testThenCombineAsync3E() {
1904 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1905 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1906 <        FailingBiFunction r = new FailingBiFunction();
1907 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
1908 <        f.complete(one);
1909 <        checkIncomplete(g);
1910 <        f2.complete(two);
1911 <        checkCompletedWithWrappedCFException(g);
1912 <    }
1913 <
1914 <    /**
1915 <     * thenCombineAsync result completes exceptionally if either source cancelled
1916 <     */
1917 <    public void testThenCombineAsync4E() {
1918 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1919 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1920 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1921 <        assertTrue(f.cancel(true));
1922 <        f2.complete(two);
1923 <        checkCompletedWithWrappedCancellationException(g);
1924 <
1925 <        f = new CompletableFuture<Integer>();
1926 <        f2 = new CompletableFuture<Integer>();
1927 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1928 <        f.complete(one);
1929 <        assertTrue(f2.cancel(true));
1930 <        checkCompletedWithWrappedCancellationException(g);
1931 <    }
1932 <
1933 <    /**
1934 <     * thenAcceptBothAsync result completes normally after normal
1935 <     * completion of sources
1936 <     */
1937 <    public void testThenAcceptBothAsyncE() {
1938 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1939 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1940 <        AddAction r = new AddAction();
1941 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1942 <        f.complete(one);
1943 <        checkIncomplete(g);
1944 <        f2.complete(two);
1945 <        checkCompletedNormally(g, null);
1946 <        assertEquals(r.value, 3);
1947 <    }
1948 <
1949 <    /**
1950 <     * thenAcceptBothAsync result completes exceptionally after exceptional
1951 <     * completion of source
1952 <     */
1953 <    public void testThenAcceptBothAsync2E() {
1954 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1955 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1956 <        AddAction r = new AddAction();
1957 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1958 <        f.completeExceptionally(new CFException());
1959 <        f2.complete(two);
1960 <        checkCompletedWithWrappedCFException(g);
1961 <
1962 <        r = new AddAction();
1963 <        f = new CompletableFuture<Integer>();
1964 <        f2 = new CompletableFuture<Integer>();
1965 <        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1966 <        f.complete(one);
1967 <        f2.completeExceptionally(new CFException());
1968 <        checkCompletedWithWrappedCFException(g);
1969 <    }
1970 <
1971 <    /**
1972 <     * thenAcceptBothAsync result completes exceptionally if action does
1973 <     */
1974 <    public void testThenAcceptBothAsync3E() {
1975 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1976 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1977 <        FailingBiConsumer r = new FailingBiConsumer();
1978 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1979 <        f.complete(one);
1980 <        checkIncomplete(g);
1981 <        f2.complete(two);
1982 <        checkCompletedWithWrappedCFException(g);
1983 <    }
1984 <
1985 <    /**
1986 <     * thenAcceptBothAsync result completes exceptionally if either source cancelled
1987 <     */
1988 <    public void testThenAcceptBothAsync4E() {
1989 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1990 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1991 <        AddAction r = new AddAction();
1992 <        CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
1993 <        assertTrue(f.cancel(true));
1994 <        f2.complete(two);
1995 <        checkCompletedWithWrappedCancellationException(g);
1996 <
1997 <        r = new AddAction();
1998 <        f = new CompletableFuture<Integer>();
1999 <        f2 = new CompletableFuture<Integer>();
2000 <        g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor());
2001 <        f.complete(one);
2002 <        assertTrue(f2.cancel(true));
2003 <        checkCompletedWithWrappedCancellationException(g);
2004 <    }
2005 <
2006 <    /**
2007 <     * runAfterBothAsync result completes normally after normal
2008 <     * completion of sources
2009 <     */
2010 <    public void testRunAfterBothAsyncE() {
2011 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2012 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2013 <        Noop r = new Noop();
2014 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2015 <        f.complete(one);
2016 <        checkIncomplete(g);
2017 <        f2.complete(two);
2018 <        checkCompletedNormally(g, null);
2019 <        assertTrue(r.ran);
2020 <    }
2021 <
2022 <    /**
2023 <     * runAfterBothAsync result completes exceptionally after exceptional
2024 <     * completion of source
2025 <     */
2026 <    public void testRunAfterBothAsync2E() {
2027 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2028 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2029 <        Noop r = new Noop();
2030 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2031 <        f.completeExceptionally(new CFException());
2032 <        f2.complete(two);
2033 <        checkCompletedWithWrappedCFException(g);
2034 <
2035 <        r = new Noop();
2036 <        f = new CompletableFuture<Integer>();
2037 <        f2 = new CompletableFuture<Integer>();
2038 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2039 <        f.complete(one);
2040 <        f2.completeExceptionally(new CFException());
2041 <        checkCompletedWithWrappedCFException(g);
2042 <    }
2043 <
2044 <    /**
2045 <     * runAfterBothAsync result completes exceptionally if action does
2046 <     */
2047 <    public void testRunAfterBothAsync3E() {
2048 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2049 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2050 <        FailingNoop r = new FailingNoop();
2051 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2052 <        f.complete(one);
2053 <        checkIncomplete(g);
2054 <        f2.complete(two);
2055 <        checkCompletedWithWrappedCFException(g);
2056 <    }
2057 <
2058 <    /**
2059 <     * runAfterBothAsync result completes exceptionally if either source cancelled
2060 <     */
2061 <    public void testRunAfterBothAsync4E() {
2062 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2063 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2064 <        Noop r = new Noop();
2065 <        CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2066 <        assertTrue(f.cancel(true));
2067 <        f2.complete(two);
2068 <        checkCompletedWithWrappedCancellationException(g);
2069 <
2070 <        r = new Noop();
2071 <        f = new CompletableFuture<Integer>();
2072 <        f2 = new CompletableFuture<Integer>();
2073 <        g = f.runAfterBothAsync(f2, r, new ThreadExecutor());
2074 <        f.complete(one);
2075 <        assertTrue(f2.cancel(true));
2076 <        checkCompletedWithWrappedCancellationException(g);
2077 <    }
2181 >        checkCancelled(f);
2182 >        checkCompletedNormally(g, v1);
2183 >    }}
2184  
2185      /**
2186 <     * applyToEitherAsync result completes normally after normal
2187 <     * completion of sources
2186 >     * runAfterEither result completes normally after normal completion
2187 >     * of either source
2188       */
2189 <    public void testApplyToEitherAsyncE() {
2190 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2191 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2192 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2193 <        f.complete(one);
2194 <        checkCompletedNormally(g, two);
2195 <
2196 <        f = new CompletableFuture<Integer>();
2197 <        f.complete(one);
2198 <        f2 = new CompletableFuture<Integer>();
2199 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2200 <        checkCompletedNormally(g, two);
2201 <    }
2189 >    public void testRunAfterEither_normalCompletion1() {
2190 >        for (ExecutionMode m : ExecutionMode.values())
2191 >        for (Integer v1 : new Integer[] { 1, null })
2192 >        for (Integer v2 : new Integer[] { 2, null })
2193 >    {
2194 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2195 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 >        final Noop r = new Noop(m);
2197 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2198 >
2199 >        f.complete(v1);
2200 >        checkCompletedNormally(h, null);
2201 >        assertEquals(1, r.invocationCount);
2202 >        g.complete(v2);
2203 >
2204 >        checkCompletedNormally(f, v1);
2205 >        checkCompletedNormally(g, v2);
2206 >        checkCompletedNormally(h, null);
2207 >        assertEquals(1, r.invocationCount);
2208 >    }}
2209 >
2210 >    public void testRunAfterEither_normalCompletion2() {
2211 >        for (ExecutionMode m : ExecutionMode.values())
2212 >        for (Integer v1 : new Integer[] { 1, null })
2213 >        for (Integer v2 : new Integer[] { 2, null })
2214 >    {
2215 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2216 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2217 >        final Noop r = new Noop(m);
2218 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2219 >
2220 >        g.complete(v2);
2221 >        checkCompletedNormally(h, null);
2222 >        assertEquals(1, r.invocationCount);
2223 >        f.complete(v1);
2224 >
2225 >        checkCompletedNormally(f, v1);
2226 >        checkCompletedNormally(g, v2);
2227 >        checkCompletedNormally(h, null);
2228 >        assertEquals(1, r.invocationCount);
2229 >        }}
2230 >
2231 >    public void testRunAfterEither_normalCompletion3() {
2232 >        for (ExecutionMode m : ExecutionMode.values())
2233 >        for (Integer v1 : new Integer[] { 1, null })
2234 >        for (Integer v2 : new Integer[] { 2, null })
2235 >    {
2236 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2237 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2238 >        final Noop r = new Noop(m);
2239 >
2240 >        f.complete(v1);
2241 >        g.complete(v2);
2242 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2243 >
2244 >        checkCompletedNormally(h, null);
2245 >        checkCompletedNormally(f, v1);
2246 >        checkCompletedNormally(g, v2);
2247 >        assertEquals(1, r.invocationCount);
2248 >    }}
2249  
2250      /**
2251 <     * applyToEitherAsync result completes exceptionally after exceptional
2252 <     * completion of source
2251 >     * runAfterEither result completes exceptionally after exceptional
2252 >     * completion of either source
2253       */
2254 <    public void testApplyToEitherAsync2E() {
2255 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2256 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2257 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2258 <        f.completeExceptionally(new CFException());
2259 <        checkCompletedWithWrappedCFException(g);
2254 >    public void testRunAfterEither_exceptionalCompletion1() {
2255 >        for (ExecutionMode m : ExecutionMode.values())
2256 >        for (Integer v1 : new Integer[] { 1, null })
2257 >    {
2258 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2259 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2260 >        final Noop r = new Noop(m);
2261 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2262 >        final CFException ex = new CFException();
2263 >
2264 >        f.completeExceptionally(ex);
2265 >        checkCompletedWithWrappedCFException(h, ex);
2266 >        g.complete(v1);
2267 >
2268 >        assertEquals(0, r.invocationCount);
2269 >        checkCompletedNormally(g, v1);
2270 >        checkCompletedWithWrappedCFException(f, ex);
2271 >        checkCompletedWithWrappedCFException(h, ex);
2272 >    }}
2273 >
2274 >    public void testRunAfterEither_exceptionalCompletion2() {
2275 >        for (ExecutionMode m : ExecutionMode.values())
2276 >        for (Integer v1 : new Integer[] { 1, null })
2277 >    {
2278 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2279 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2280 >        final Noop r = new Noop(m);
2281 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2282 >        final CFException ex = new CFException();
2283 >
2284 >        g.completeExceptionally(ex);
2285 >        checkCompletedWithWrappedCFException(h, ex);
2286 >        f.complete(v1);
2287 >
2288 >        assertEquals(0, r.invocationCount);
2289 >        checkCompletedNormally(f, v1);
2290 >        checkCompletedWithWrappedCFException(g, ex);
2291 >        checkCompletedWithWrappedCFException(h, ex);
2292 >    }}
2293 >
2294 >    public void testRunAfterEither_exceptionalCompletion3() {
2295 >        for (ExecutionMode m : ExecutionMode.values())
2296 >        for (Integer v1 : new Integer[] { 1, null })
2297 >    {
2298 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2299 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2300 >        final Noop r = new Noop(m);
2301 >        final CFException ex = new CFException();
2302 >
2303 >        g.completeExceptionally(ex);
2304 >        f.complete(v1);
2305 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2306  
2307 <        f = new CompletableFuture<Integer>();
2308 <        f2 = new CompletableFuture<Integer>();
2309 <        f2.completeExceptionally(new CFException());
2310 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2311 <        f.complete(one);
2312 <        checkCompletedWithWrappedCFException(g);
2313 <    }
2314 <
2315 <    /**
2316 <     * applyToEitherAsync result completes exceptionally if action does
2317 <     */
2318 <    public void testApplyToEitherAsync3E() {
2319 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2320 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2321 <        FailingFunction r = new FailingFunction();
2322 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2323 <        f.complete(one);
2324 <        checkCompletedWithWrappedCFException(g);
2325 <    }
2307 >        // unspecified behavior
2308 >        Integer v;
2309 >        try {
2310 >            assertNull(h.join());
2311 >            assertEquals(1, r.invocationCount);
2312 >        } catch (CompletionException ok) {
2313 >            checkCompletedWithWrappedCFException(h, ex);
2314 >            assertEquals(0, r.invocationCount);
2315 >        }
2316 >
2317 >        checkCompletedWithWrappedCFException(g, ex);
2318 >        checkCompletedNormally(f, v1);
2319 >    }}
2320 >
2321 >    public void testRunAfterEither_exceptionalCompletion4() {
2322 >        for (ExecutionMode m : ExecutionMode.values())
2323 >        for (Integer v1 : new Integer[] { 1, null })
2324 >    {
2325 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2326 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2327 >        final Noop r = new Noop(m);
2328 >        final CFException ex = new CFException();
2329 >
2330 >        f.completeExceptionally(ex);
2331 >        g.complete(v1);
2332 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2333  
2334 <    /**
2335 <     * applyToEitherAsync result completes exceptionally if either source cancelled
2336 <     */
2337 <    public void testApplyToEitherAsync4E() {
2338 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2339 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2340 <        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2341 <        assertTrue(f.cancel(true));
2342 <        checkCompletedWithWrappedCancellationException(g);
2334 >        // unspecified behavior
2335 >        Integer v;
2336 >        try {
2337 >            assertNull(h.join());
2338 >            assertEquals(1, r.invocationCount);
2339 >        } catch (CompletionException ok) {
2340 >            checkCompletedWithWrappedCFException(h, ex);
2341 >            assertEquals(0, r.invocationCount);
2342 >        }
2343  
2344 <        f = new CompletableFuture<Integer>();
2345 <        f2 = new CompletableFuture<Integer>();
2346 <        assertTrue(f2.cancel(true));
2141 <        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2142 <        checkCompletedWithWrappedCancellationException(g);
2143 <    }
2344 >        checkCompletedWithWrappedCFException(f, ex);
2345 >        checkCompletedNormally(g, v1);
2346 >    }}
2347  
2348      /**
2349 <     * acceptEitherAsync result completes normally after normal
2147 <     * completion of sources
2349 >     * runAfterEither result completes exceptionally if action does
2350       */
2351 <    public void testAcceptEitherAsyncE() {
2352 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2353 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2354 <        IncAction r = new IncAction();
2355 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2356 <        f.complete(one);
2357 <        checkCompletedNormally(g, null);
2358 <        assertEquals(r.value, 2);
2359 <
2360 <        r = new IncAction();
2361 <        f = new CompletableFuture<Integer>();
2362 <        f.complete(one);
2363 <        f2 = new CompletableFuture<Integer>();
2364 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2365 <        checkCompletedNormally(g, null);
2366 <        assertEquals(r.value, 2);
2367 <    }
2351 >    public void testRunAfterEither_actionFailed1() {
2352 >        for (ExecutionMode m : ExecutionMode.values())
2353 >        for (Integer v1 : new Integer[] { 1, null })
2354 >        for (Integer v2 : new Integer[] { 2, null })
2355 >    {
2356 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2357 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2358 >        final FailingRunnable r = new FailingRunnable(m);
2359 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2360 >
2361 >        f.complete(v1);
2362 >        checkCompletedWithWrappedCFException(h);
2363 >        g.complete(v2);
2364 >        checkCompletedNormally(f, v1);
2365 >        checkCompletedNormally(g, v2);
2366 >    }}
2367 >
2368 >    public void testRunAfterEither_actionFailed2() {
2369 >        for (ExecutionMode m : ExecutionMode.values())
2370 >        for (Integer v1 : new Integer[] { 1, null })
2371 >        for (Integer v2 : new Integer[] { 2, null })
2372 >    {
2373 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2374 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2375 >        final FailingRunnable r = new FailingRunnable(m);
2376 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2377 >
2378 >        g.complete(v2);
2379 >        checkCompletedWithWrappedCFException(h);
2380 >        f.complete(v1);
2381 >        checkCompletedNormally(f, v1);
2382 >        checkCompletedNormally(g, v2);
2383 >    }}
2384  
2385      /**
2386 <     * acceptEitherAsync result completes exceptionally after exceptional
2169 <     * completion of source
2386 >     * runAfterEither result completes exceptionally if either source cancelled
2387       */
2388 <    public void testAcceptEitherAsync2E() {
2389 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2390 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2391 <        IncAction r = new IncAction();
2392 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2393 <        f.completeExceptionally(new CFException());
2394 <        checkCompletedWithWrappedCFException(g);
2388 >    public void testRunAfterEither_sourceCancelled1() {
2389 >        for (ExecutionMode m : ExecutionMode.values())
2390 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2391 >        for (Integer v1 : new Integer[] { 1, null })
2392 >    {
2393 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2394 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2395 >        final Noop r = new Noop(m);
2396 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2397 >
2398 >        assertTrue(f.cancel(mayInterruptIfRunning));
2399 >        checkCompletedWithWrappedCancellationException(h);
2400 >        g.complete(v1);
2401  
2402 <        r = new IncAction();
2403 <        f = new CompletableFuture<Integer>();
2404 <        f2 = new CompletableFuture<Integer>();
2405 <        f2.completeExceptionally(new CFException());
2406 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2407 <        f.complete(one);
2408 <        checkCompletedWithWrappedCFException(g);
2409 <    }
2402 >        checkCancelled(f);
2403 >        assertEquals(0, r.invocationCount);
2404 >        checkCompletedNormally(g, v1);
2405 >        checkCompletedWithWrappedCancellationException(h);
2406 >    }}
2407 >
2408 >    public void testRunAfterEither_sourceCancelled2() {
2409 >        for (ExecutionMode m : ExecutionMode.values())
2410 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2411 >        for (Integer v1 : new Integer[] { 1, null })
2412 >    {
2413 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2414 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2415 >        final Noop r = new Noop(m);
2416 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2417 >
2418 >        assertTrue(g.cancel(mayInterruptIfRunning));
2419 >        checkCompletedWithWrappedCancellationException(h);
2420 >        f.complete(v1);
2421 >
2422 >        checkCancelled(g);
2423 >        assertEquals(0, r.invocationCount);
2424 >        checkCompletedNormally(f, v1);
2425 >        checkCompletedWithWrappedCancellationException(h);
2426 >    }}
2427 >
2428 >    public void testRunAfterEither_sourceCancelled3() {
2429 >        for (ExecutionMode m : ExecutionMode.values())
2430 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2431 >        for (Integer v1 : new Integer[] { 1, null })
2432 >    {
2433 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2434 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2435 >        final Noop r = new Noop(m);
2436 >
2437 >        assertTrue(g.cancel(mayInterruptIfRunning));
2438 >        f.complete(v1);
2439 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2440  
2441 <    /**
2442 <     * acceptEitherAsync result completes exceptionally if action does
2443 <     */
2444 <    public void testAcceptEitherAsync3E() {
2445 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2446 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2447 <        FailingConsumer r = new FailingConsumer();
2448 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2449 <        f.complete(one);
2450 <        checkCompletedWithWrappedCFException(g);
2451 <    }
2441 >        // unspecified behavior
2442 >        Integer v;
2443 >        try {
2444 >            assertNull(h.join());
2445 >            assertEquals(1, r.invocationCount);
2446 >        } catch (CompletionException ok) {
2447 >            checkCompletedWithWrappedCancellationException(h);
2448 >            assertEquals(0, r.invocationCount);
2449 >        }
2450 >
2451 >        checkCancelled(g);
2452 >        checkCompletedNormally(f, v1);
2453 >    }}
2454 >
2455 >    public void testRunAfterEither_sourceCancelled4() {
2456 >        for (ExecutionMode m : ExecutionMode.values())
2457 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2458 >        for (Integer v1 : new Integer[] { 1, null })
2459 >    {
2460 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2461 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2462 >        final Noop r = new Noop(m);
2463 >
2464 >        assertTrue(f.cancel(mayInterruptIfRunning));
2465 >        g.complete(v1);
2466 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2467  
2468 <    /**
2469 <     * acceptEitherAsync result completes exceptionally if either
2470 <     * source cancelled
2471 <     */
2472 <    public void testAcceptEitherAsync4E() {
2473 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2474 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2475 <        IncAction r = new IncAction();
2476 <        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2209 <        assertTrue(f.cancel(true));
2210 <        checkCompletedWithWrappedCancellationException(g);
2468 >        // unspecified behavior
2469 >        Integer v;
2470 >        try {
2471 >            assertNull(h.join());
2472 >            assertEquals(1, r.invocationCount);
2473 >        } catch (CompletionException ok) {
2474 >            checkCompletedWithWrappedCancellationException(h);
2475 >            assertEquals(0, r.invocationCount);
2476 >        }
2477  
2478 <        r = new IncAction();
2479 <        f = new CompletableFuture<Integer>();
2480 <        f2 = new CompletableFuture<Integer>();
2215 <        assertTrue(f2.cancel(true));
2216 <        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2217 <        checkCompletedWithWrappedCancellationException(g);
2218 <    }
2478 >        checkCancelled(f);
2479 >        checkCompletedNormally(g, v1);
2480 >    }}
2481  
2482      /**
2483 <     * runAfterEitherAsync result completes normally after normal
2222 <     * completion of sources
2483 >     * thenCompose result completes normally after normal completion of source
2484       */
2485 <    public void testRunAfterEitherAsyncE() {
2486 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2487 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2488 <        Noop r = new Noop();
2489 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2490 <        f.complete(one);
2491 <        checkCompletedNormally(g, null);
2492 <        assertTrue(r.ran);
2493 <
2494 <        r = new Noop();
2495 <        f = new CompletableFuture<Integer>();
2496 <        f.complete(one);
2497 <        f2 = new CompletableFuture<Integer>();
2498 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2499 <        checkCompletedNormally(g, null);
2239 <        assertTrue(r.ran);
2240 <    }
2485 >    public void testThenCompose_normalCompletion() {
2486 >        for (ExecutionMode m : ExecutionMode.values())
2487 >        for (boolean createIncomplete : new boolean[] { true, false })
2488 >        for (Integer v1 : new Integer[] { 1, null })
2489 >    {
2490 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2491 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2492 >        if (!createIncomplete) f.complete(v1);
2493 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2494 >        if (createIncomplete) f.complete(v1);
2495 >
2496 >        checkCompletedNormally(g, inc(v1));
2497 >        checkCompletedNormally(f, v1);
2498 >        assertEquals(1, r.invocationCount);
2499 >    }}
2500  
2501      /**
2502 <     * runAfterEitherAsync result completes exceptionally after exceptional
2502 >     * thenCompose result completes exceptionally after exceptional
2503       * completion of source
2504       */
2505 <    public void testRunAfterEitherAsync2E() {
2506 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2507 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2508 <        Noop r = new Noop();
2509 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2510 <        f.completeExceptionally(new CFException());
2511 <        checkCompletedWithWrappedCFException(g);
2512 <
2513 <        r = new Noop();
2514 <        f = new CompletableFuture<Integer>();
2515 <        f2 = new CompletableFuture<Integer>();
2516 <        f2.completeExceptionally(new CFException());
2517 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2518 <        f.complete(one);
2519 <        checkCompletedWithWrappedCFException(g);
2261 <    }
2262 <
2263 <    /**
2264 <     * runAfterEitherAsync result completes exceptionally if action does
2265 <     */
2266 <    public void testRunAfterEitherAsync3E() {
2267 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2268 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2269 <        FailingNoop r = new FailingNoop();
2270 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2271 <        f.complete(one);
2272 <        checkCompletedWithWrappedCFException(g);
2273 <    }
2274 <
2275 <    /**
2276 <     * runAfterEitherAsync result completes exceptionally if either
2277 <     * source cancelled
2278 <     */
2279 <    public void testRunAfterEitherAsync4E() {
2280 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2281 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2282 <        Noop r = new Noop();
2283 <        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2284 <        assertTrue(f.cancel(true));
2285 <        checkCompletedWithWrappedCancellationException(g);
2286 <
2287 <        r = new Noop();
2288 <        f = new CompletableFuture<Integer>();
2289 <        f2 = new CompletableFuture<Integer>();
2290 <        assertTrue(f2.cancel(true));
2291 <        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2292 <        checkCompletedWithWrappedCancellationException(g);
2293 <    }
2505 >    public void testThenCompose_exceptionalCompletion() {
2506 >        for (ExecutionMode m : ExecutionMode.values())
2507 >        for (boolean createIncomplete : new boolean[] { true, false })
2508 >    {
2509 >        final CFException ex = new CFException();
2510 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2511 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2512 >        if (!createIncomplete) f.completeExceptionally(ex);
2513 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2514 >        if (createIncomplete) f.completeExceptionally(ex);
2515 >
2516 >        checkCompletedWithWrappedCFException(g, ex);
2517 >        checkCompletedWithWrappedCFException(f, ex);
2518 >        assertEquals(0, r.invocationCount);
2519 >    }}
2520  
2521      /**
2522 <     * thenCompse result completes normally after normal completion of source
2522 >     * thenCompose result completes exceptionally if action does
2523       */
2524 <    public void testThenComposeAsyncE() {
2525 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2526 <        CompletableFutureInc r = new CompletableFutureInc();
2527 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2528 <        f.complete(one);
2529 <        checkCompletedNormally(g, two);
2530 <    }
2524 >    public void testThenCompose_actionFailed() {
2525 >        for (ExecutionMode m : ExecutionMode.values())
2526 >        for (boolean createIncomplete : new boolean[] { true, false })
2527 >        for (Integer v1 : new Integer[] { 1, null })
2528 >    {
2529 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2530 >        final FailingCompletableFutureFunction r
2531 >            = new FailingCompletableFutureFunction(m);
2532 >        if (!createIncomplete) f.complete(v1);
2533 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2534 >        if (createIncomplete) f.complete(v1);
2535  
2306    /**
2307     * thenComposeAsync result completes exceptionally after exceptional
2308     * completion of source
2309     */
2310    public void testThenComposeAsync2E() {
2311        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2312        CompletableFutureInc r = new CompletableFutureInc();
2313        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2314        f.completeExceptionally(new CFException());
2536          checkCompletedWithWrappedCFException(g);
2537 <    }
2537 >        checkCompletedNormally(f, v1);
2538 >    }}
2539  
2540      /**
2541 <     * thenComposeAsync result completes exceptionally if action does
2541 >     * thenCompose result completes exceptionally if source cancelled
2542       */
2543 <    public void testThenComposeAsync3E() {
2544 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2545 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
2546 <        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2547 <        f.complete(one);
2548 <        checkCompletedWithWrappedCFException(g);
2549 <    }
2543 >    public void testThenCompose_sourceCancelled() {
2544 >        for (ExecutionMode m : ExecutionMode.values())
2545 >        for (boolean createIncomplete : new boolean[] { true, false })
2546 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2547 >    {
2548 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2549 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2550 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2551 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2552 >        if (createIncomplete) {
2553 >            checkIncomplete(g);
2554 >            assertTrue(f.cancel(mayInterruptIfRunning));
2555 >        }
2556  
2329    /**
2330     * thenComposeAsync result completes exceptionally if source cancelled
2331     */
2332    public void testThenComposeAsync4E() {
2333        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2334        CompletableFutureInc r = new CompletableFutureInc();
2335        CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor());
2336        assertTrue(f.cancel(true));
2557          checkCompletedWithWrappedCancellationException(g);
2558 <    }
2558 >        checkCancelled(f);
2559 >    }}
2560  
2561      // other static methods
2562  
# Line 2344 | Line 2565 | public class CompletableFutureTest exten
2565       * with the value null
2566       */
2567      public void testAllOf_empty() throws Exception {
2568 <        CompletableFuture<?> f = CompletableFuture.allOf();
2568 >        CompletableFuture<Void> f = CompletableFuture.allOf();
2569          checkCompletedNormally(f, null);
2570      }
2571  
2572      /**
2573 <     * allOf returns a future completed when all components complete
2573 >     * allOf returns a future completed normally with the value null
2574 >     * when all components complete normally
2575       */
2576 <    public void testAllOf() throws Exception {
2576 >    public void testAllOf_normal() throws Exception {
2577          for (int k = 1; k < 20; ++k) {
2578 <            CompletableFuture[] fs = new CompletableFuture[k];
2578 >            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2579              for (int i = 0; i < k; ++i)
2580 <                fs[i] = new CompletableFuture<Integer>();
2581 <            CompletableFuture<?> f = CompletableFuture.allOf(fs);
2580 >                fs[i] = new CompletableFuture<>();
2581 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2582              for (int i = 0; i < k; ++i) {
2583                  checkIncomplete(f);
2584 +                checkIncomplete(CompletableFuture.allOf(fs));
2585                  fs[i].complete(one);
2586              }
2587 <            assertTrue(f.isDone());
2588 <            assertFalse(f.isCancelled());
2587 >            checkCompletedNormally(f, null);
2588 >            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2589          }
2590      }
2591  
# Line 2370 | Line 2593 | public class CompletableFutureTest exten
2593       * anyOf(no component futures) returns an incomplete future
2594       */
2595      public void testAnyOf_empty() throws Exception {
2596 <        CompletableFuture<?> f = CompletableFuture.anyOf();
2596 >        CompletableFuture<Object> f = CompletableFuture.anyOf();
2597          checkIncomplete(f);
2598      }
2599  
2600      /**
2601 <     * allOf returns a future completed when any components complete
2601 >     * anyOf returns a future completed normally with a value when
2602 >     * a component future does
2603       */
2604 <    public void testAnyOf() throws Exception {
2605 <        for (int k = 1; k < 20; ++k) {
2604 >    public void testAnyOf_normal() throws Exception {
2605 >        for (int k = 0; k < 10; ++k) {
2606              CompletableFuture[] fs = new CompletableFuture[k];
2607              for (int i = 0; i < k; ++i)
2608 <                fs[i] = new CompletableFuture<Integer>();
2609 <            CompletableFuture<?> f = CompletableFuture.anyOf(fs);
2608 >                fs[i] = new CompletableFuture<>();
2609 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2610              checkIncomplete(f);
2611              for (int i = 0; i < k; ++i) {
2612                  fs[i].complete(one);
2613 <                assertTrue(f.isDone());
2613 >                checkCompletedNormally(f, one);
2614 >                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
2615 >            }
2616 >        }
2617 >    }
2618 >
2619 >    /**
2620 >     * anyOf result completes exceptionally when any component does.
2621 >     */
2622 >    public void testAnyOf_exceptional() throws Exception {
2623 >        for (int k = 0; k < 10; ++k) {
2624 >            CompletableFuture[] fs = new CompletableFuture[k];
2625 >            for (int i = 0; i < k; ++i)
2626 >                fs[i] = new CompletableFuture<>();
2627 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2628 >            checkIncomplete(f);
2629 >            for (int i = 0; i < k; ++i) {
2630 >                fs[i].completeExceptionally(new CFException());
2631 >                checkCompletedWithWrappedCFException(f);
2632 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
2633              }
2634          }
2635      }
# Line 2395 | Line 2638 | public class CompletableFutureTest exten
2638       * Completion methods throw NullPointerException with null arguments
2639       */
2640      public void testNPE() {
2641 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2642 <        CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2643 <        CompletableFuture h;
2644 <        try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2645 <        try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2646 <        try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2647 <        try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2648 <        try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2649 <        try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2650 <        try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2651 <        try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2652 <        try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2653 <        try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2654 <        try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2655 <        try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2656 <        try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2657 <        try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2658 <        try { h = f.handle(null); } catch (NullPointerException ok) {}
2659 <        try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2660 <
2661 <        try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2662 <        try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2663 <        try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2664 <        try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2665 <        try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2666 <        try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2667 <        try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2668 <        try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2669 <        try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2670 <        try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2671 <        try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2672 <        try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2673 <        try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2641 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2642 >        CompletableFuture<Integer> g = new CompletableFuture<>();
2643 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2644 >        CompletableFuture<?> h;
2645 >        ThreadExecutor exec = new ThreadExecutor();
2646 >
2647 >        Runnable[] throwingActions = {
2648 >            () -> CompletableFuture.supplyAsync(null),
2649 >            () -> CompletableFuture.supplyAsync(null, exec),
2650 >            () -> CompletableFuture.supplyAsync(supplyOne, null),
2651 >
2652 >            () -> CompletableFuture.runAsync(null),
2653 >            () -> CompletableFuture.runAsync(null, exec),
2654 >            () -> CompletableFuture.runAsync(() -> {}, null),
2655 >
2656 >            () -> f.completeExceptionally(null),
2657 >
2658 >            () -> f.thenApply(null),
2659 >            () -> f.thenApplyAsync(null),
2660 >            () -> f.thenApplyAsync((x) -> x, null),
2661 >            () -> f.thenApplyAsync(null, exec),
2662 >
2663 >            () -> f.thenAccept(null),
2664 >            () -> f.thenAcceptAsync(null),
2665 >            () -> f.thenAcceptAsync((x) -> {} , null),
2666 >            () -> f.thenAcceptAsync(null, exec),
2667 >
2668 >            () -> f.thenRun(null),
2669 >            () -> f.thenRunAsync(null),
2670 >            () -> f.thenRunAsync(() -> {} , null),
2671 >            () -> f.thenRunAsync(null, exec),
2672 >
2673 >            () -> f.thenCombine(g, null),
2674 >            () -> f.thenCombineAsync(g, null),
2675 >            () -> f.thenCombineAsync(g, null, exec),
2676 >            () -> f.thenCombine(nullFuture, (x, y) -> x),
2677 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x),
2678 >            () -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec),
2679 >            () -> f.thenCombineAsync(g, (x, y) -> x, null),
2680 >
2681 >            () -> f.thenAcceptBoth(g, null),
2682 >            () -> f.thenAcceptBothAsync(g, null),
2683 >            () -> f.thenAcceptBothAsync(g, null, exec),
2684 >            () -> f.thenAcceptBoth(nullFuture, (x, y) -> {}),
2685 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}),
2686 >            () -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec),
2687 >            () -> f.thenAcceptBothAsync(g, (x, y) -> {}, null),
2688 >
2689 >            () -> f.runAfterBoth(g, null),
2690 >            () -> f.runAfterBothAsync(g, null),
2691 >            () -> f.runAfterBothAsync(g, null, exec),
2692 >            () -> f.runAfterBoth(nullFuture, () -> {}),
2693 >            () -> f.runAfterBothAsync(nullFuture, () -> {}),
2694 >            () -> f.runAfterBothAsync(nullFuture, () -> {}, exec),
2695 >            () -> f.runAfterBothAsync(g, () -> {}, null),
2696 >
2697 >            () -> f.applyToEither(g, null),
2698 >            () -> f.applyToEitherAsync(g, null),
2699 >            () -> f.applyToEitherAsync(g, null, exec),
2700 >            () -> f.applyToEither(nullFuture, (x) -> x),
2701 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
2702 >            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
2703 >            () -> f.applyToEitherAsync(g, (x) -> x, null),
2704 >
2705 >            () -> f.acceptEither(g, null),
2706 >            () -> f.acceptEitherAsync(g, null),
2707 >            () -> f.acceptEitherAsync(g, null, exec),
2708 >            () -> f.acceptEither(nullFuture, (x) -> {}),
2709 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
2710 >            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
2711 >            () -> f.acceptEitherAsync(g, (x) -> {}, null),
2712 >
2713 >            () -> f.runAfterEither(g, null),
2714 >            () -> f.runAfterEitherAsync(g, null),
2715 >            () -> f.runAfterEitherAsync(g, null, exec),
2716 >            () -> f.runAfterEither(nullFuture, () -> {}),
2717 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}),
2718 >            () -> f.runAfterEitherAsync(nullFuture, () -> {}, exec),
2719 >            () -> f.runAfterEitherAsync(g, () -> {}, null),
2720 >
2721 >            () -> f.thenCompose(null),
2722 >            () -> f.thenComposeAsync(null),
2723 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2724 >            () -> f.thenComposeAsync(null, exec),
2725 >
2726 >            () -> f.exceptionally(null),
2727 >
2728 >            () -> f.handle(null),
2729 >
2730 >            () -> CompletableFuture.allOf((CompletableFuture<?>)null),
2731 >            () -> CompletableFuture.allOf((CompletableFuture<?>[])null),
2732 >            () -> CompletableFuture.allOf(f, null),
2733 >            () -> CompletableFuture.allOf(null, f),
2734 >
2735 >            () -> CompletableFuture.anyOf((CompletableFuture<?>)null),
2736 >            () -> CompletableFuture.anyOf((CompletableFuture<?>[])null),
2737 >            () -> CompletableFuture.anyOf(f, null),
2738 >            () -> CompletableFuture.anyOf(null, f),
2739 >
2740 >            () -> f.obtrudeException(null),
2741 >        };
2742 >
2743 >        assertThrows(NullPointerException.class, throwingActions);
2744 >        assertEquals(0, exec.count.get());
2745 >    }
2746 >
2747 >    /**
2748 >     * toCompletableFuture returns this CompletableFuture.
2749 >     */
2750 >    public void testToCompletableFuture() {
2751 >        CompletableFuture<Integer> f = new CompletableFuture<>();
2752 >        assertSame(f, f.toCompletableFuture());
2753 >    }
2754 >
2755 >    /**
2756 >     * whenComplete action executes on normal completion, propagating
2757 >     * source result.
2758 >     */
2759 >    public void testWhenComplete_normalCompletion1() {
2760 >        for (ExecutionMode m : ExecutionMode.values())
2761 >        for (boolean createIncomplete : new boolean[] { true, false })
2762 >        for (Integer v1 : new Integer[] { 1, null })
2763 >    {
2764 >        final AtomicInteger a = new AtomicInteger(0);
2765 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2766 >        if (!createIncomplete) f.complete(v1);
2767 >        final CompletableFuture<Integer> g = m.whenComplete
2768 >            (f,
2769 >             (Integer x, Throwable t) -> {
2770 >                threadAssertSame(x, v1);
2771 >                threadAssertNull(t);
2772 >                a.getAndIncrement();
2773 >            });
2774 >        if (createIncomplete) f.complete(v1);
2775 >
2776 >        checkCompletedNormally(g, v1);
2777 >        checkCompletedNormally(f, v1);
2778 >        assertEquals(1, a.get());
2779 >    }}
2780 >
2781 >    /**
2782 >     * whenComplete action executes on exceptional completion, propagating
2783 >     * source result.
2784 >     */
2785 >    public void testWhenComplete_exceptionalCompletion() {
2786 >        for (ExecutionMode m : ExecutionMode.values())
2787 >        for (boolean createIncomplete : new boolean[] { true, false })
2788 >        for (Integer v1 : new Integer[] { 1, null })
2789 >    {
2790 >        final AtomicInteger a = new AtomicInteger(0);
2791 >        final CFException ex = new CFException();
2792 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2793 >        if (!createIncomplete) f.completeExceptionally(ex);
2794 >        final CompletableFuture<Integer> g = m.whenComplete
2795 >            (f,
2796 >             (Integer x, Throwable t) -> {
2797 >                threadAssertNull(x);
2798 >                threadAssertSame(t, ex);
2799 >                a.getAndIncrement();
2800 >            });
2801 >        if (createIncomplete) f.completeExceptionally(ex);
2802 >        checkCompletedWithWrappedCFException(f, ex);
2803 >        checkCompletedWithWrappedCFException(g, ex);
2804 >        assertEquals(1, a.get());
2805 >    }}
2806 >
2807 >    /**
2808 >     * whenComplete action executes on cancelled source, propagating
2809 >     * CancellationException.
2810 >     */
2811 >    public void testWhenComplete_sourceCancelled() {
2812 >        for (ExecutionMode m : ExecutionMode.values())
2813 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2814 >        for (boolean createIncomplete : new boolean[] { true, false })
2815 >    {
2816 >        final AtomicInteger a = new AtomicInteger(0);
2817 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2818 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2819 >        final CompletableFuture<Integer> g = m.whenComplete
2820 >            (f,
2821 >             (Integer x, Throwable t) -> {
2822 >                threadAssertNull(x);
2823 >                threadAssertTrue(t instanceof CancellationException);
2824 >                a.getAndIncrement();
2825 >            });
2826 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2827  
2828 <    }
2828 >        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2829 >        checkCompletedWithWrappedCancellationException(g);
2830 >        checkCancelled(f);
2831 >        assertEquals(1, a.get());
2832 >    }}
2833  
2834 +    /**
2835 +     * If a whenComplete action throws an exception when triggered by
2836 +     * a normal completion, it completes exceptionally
2837 +     */
2838 +    public void testWhenComplete_actionFailed() {
2839 +        for (boolean createIncomplete : new boolean[] { true, false })
2840 +        for (ExecutionMode m : ExecutionMode.values())
2841 +        for (Integer v1 : new Integer[] { 1, null })
2842 +    {
2843 +        final AtomicInteger a = new AtomicInteger(0);
2844 +        final CFException ex = new CFException();
2845 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2846 +        if (!createIncomplete) f.complete(v1);
2847 +        final CompletableFuture<Integer> g = m.whenComplete
2848 +            (f,
2849 +             (Integer x, Throwable t) -> {
2850 +                threadAssertSame(x, v1);
2851 +                threadAssertNull(t);
2852 +                a.getAndIncrement();
2853 +                throw ex;
2854 +            });
2855 +        if (createIncomplete) f.complete(v1);
2856 +        checkCompletedNormally(f, v1);
2857 +        checkCompletedWithWrappedCFException(g, ex);
2858 +        assertEquals(1, a.get());
2859 +    }}
2860 +
2861 +    /**
2862 +     * If a whenComplete action throws an exception when triggered by
2863 +     * a source completion that also throws an exception, the source
2864 +     * exception takes precedence.
2865 +     */
2866 +    public void testWhenComplete_actionFailedSourceFailed() {
2867 +        for (boolean createIncomplete : new boolean[] { true, false })
2868 +        for (ExecutionMode m : ExecutionMode.values())
2869 +        for (Integer v1 : new Integer[] { 1, null })
2870 +    {
2871 +        final AtomicInteger a = new AtomicInteger(0);
2872 +        final CFException ex1 = new CFException();
2873 +        final CFException ex2 = new CFException();
2874 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2875 +
2876 +        if (!createIncomplete) f.completeExceptionally(ex1);
2877 +        final CompletableFuture<Integer> g = m.whenComplete
2878 +            (f,
2879 +             (Integer x, Throwable t) -> {
2880 +                threadAssertSame(t, ex1);
2881 +                threadAssertNull(x);
2882 +                a.getAndIncrement();
2883 +                throw ex2;
2884 +            });
2885 +        if (createIncomplete) f.completeExceptionally(ex1);
2886 +
2887 +        checkCompletedWithWrappedCFException(f, ex1);
2888 +        checkCompletedWithWrappedCFException(g, ex1);
2889 +        assertEquals(1, a.get());
2890 +    }}
2891  
2892   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines