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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines