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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.42 by jsr166, Mon Jun 2 02:19:23 2014 UTC vs.
Revision 1.77 by jsr166, Sat Jun 7 21:46:50 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines