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.49 by jsr166, Mon Jun 2 19:20:51 2014 UTC vs.
Revision 1.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC

# Line 105 | 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 131 | 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 218 | 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 261 | 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());
269 <        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);
279 <    }
311 >        checkCompletedExceptionally(f, ex);
312 >        f.complete(v1);
313 >        checkCompletedExceptionally(f, ex);
314 >    }}
315  
316      /**
317       * getNumberOfDependents returns number of dependent tasks
# Line 284 | Line 319 | public class CompletableFutureTest exten
319      public void testGetNumberOfDependents() {
320          CompletableFuture<Integer> f = new CompletableFuture<>();
321          assertEquals(0, f.getNumberOfDependents());
322 <        CompletableFuture g = f.thenRun(new Noop());
322 >        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
323          assertEquals(1, f.getNumberOfDependents());
324          assertEquals(0, g.getNumberOfDependents());
325 <        CompletableFuture h = f.thenRun(new Noop());
325 >        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
326          assertEquals(2, f.getNumberOfDependents());
327          f.complete(1);
328          checkCompletedNormally(g, null);
# Line 310 | Line 345 | public class CompletableFutureTest exten
345          f = new CompletableFuture<String>();
346          f.completeExceptionally(new IndexOutOfBoundsException());
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348 +
349 +        f = new CompletableFuture<String>();
350 +        f.cancel(true);
351 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
352 +
353 +        f = new CompletableFuture<String>();
354 +        f.cancel(false);
355 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
356      }
357  
358      /**
# Line 320 | Line 363 | public class CompletableFutureTest exten
363          checkCompletedNormally(f, "test");
364      }
365  
366 <    // Choose non-commutative actions for better coverage
366 >    abstract class CheckedAction {
367 >        int invocationCount = 0;
368 >        final ExecutionMode m;
369 >        CheckedAction(ExecutionMode m) { this.m = m; }
370 >        void invoked() {
371 >            m.checkExecutionMode();
372 >            assertEquals(0, invocationCount++);
373 >        }
374 >        void assertNotInvoked() { assertEquals(0, invocationCount); }
375 >        void assertInvoked() { assertEquals(1, invocationCount); }
376 >    }
377  
378 <    // A non-commutative function that handles and produces null values as well.
379 <    static Integer subtract(Integer x, Integer y) {
380 <        return (x == null && y == null) ? null :
381 <            ((x == null) ? 42 : x.intValue())
382 <            - ((y == null) ? 99 : y.intValue());
378 >    abstract class CheckedIntegerAction extends CheckedAction {
379 >        Integer value;
380 >        CheckedIntegerAction(ExecutionMode m) { super(m); }
381 >        void assertValue(Integer expected) {
382 >            assertInvoked();
383 >            assertEquals(expected, value);
384 >        }
385 >    }
386 >
387 >    class IntegerSupplier extends CheckedAction
388 >        implements Supplier<Integer>
389 >    {
390 >        final Integer value;
391 >        IntegerSupplier(ExecutionMode m, Integer value) {
392 >            super(m);
393 >            this.value = value;
394 >        }
395 >        public Integer get() {
396 >            invoked();
397 >            return value;
398 >        }
399      }
400  
401      // A function that handles and produces null values as well.
# Line 334 | Line 403 | public class CompletableFutureTest exten
403          return (x == null) ? null : x + 1;
404      }
405  
406 <    static final Supplier<Integer> supplyOne =
407 <        () -> Integer.valueOf(1);
408 <    static final Function<Integer, Integer> inc =
409 <        (Integer x) -> Integer.valueOf(x.intValue() + 1);
341 <    static final BiFunction<Integer, Integer, Integer> subtract =
342 <        (Integer x, Integer y) -> subtract(x, y);
343 <    static final class IncAction implements Consumer<Integer> {
344 <        int invocationCount = 0;
345 <        Integer value;
406 >    class NoopConsumer extends CheckedIntegerAction
407 >        implements Consumer<Integer>
408 >    {
409 >        NoopConsumer(ExecutionMode m) { super(m); }
410          public void accept(Integer x) {
411 <            invocationCount++;
412 <            value = inc(x);
411 >            invoked();
412 >            value = x;
413          }
414      }
415 <    static final class IncFunction implements Function<Integer,Integer> {
416 <        int invocationCount = 0;
417 <        Integer value;
415 >
416 >    class IncFunction extends CheckedIntegerAction
417 >        implements Function<Integer,Integer>
418 >    {
419 >        IncFunction(ExecutionMode m) { super(m); }
420          public Integer apply(Integer x) {
421 <            invocationCount++;
421 >            invoked();
422              return value = inc(x);
423          }
424      }
425 <    static final class SubtractAction implements BiConsumer<Integer, Integer> {
426 <        int invocationCount = 0;
427 <        Integer value;
428 <        // Check this action was invoked exactly once when result is computed.
425 >
426 >    // Choose non-commutative actions for better coverage
427 >    // A non-commutative function that handles and produces null values as well.
428 >    static Integer subtract(Integer x, Integer y) {
429 >        return (x == null && y == null) ? null :
430 >            ((x == null) ? 42 : x.intValue())
431 >            - ((y == null) ? 99 : y.intValue());
432 >    }
433 >
434 >    class SubtractAction extends CheckedIntegerAction
435 >        implements BiConsumer<Integer, Integer>
436 >    {
437 >        SubtractAction(ExecutionMode m) { super(m); }
438          public void accept(Integer x, Integer y) {
439 <            invocationCount++;
439 >            invoked();
440              value = subtract(x, y);
441          }
442      }
443 <    static final class SubtractFunction implements BiFunction<Integer, Integer, Integer> {
444 <        int invocationCount = 0;
445 <        Integer value;
446 <        // Check this action was invoked exactly once when result is computed.
443 >
444 >    class SubtractFunction extends CheckedIntegerAction
445 >        implements BiFunction<Integer, Integer, Integer>
446 >    {
447 >        SubtractFunction(ExecutionMode m) { super(m); }
448          public Integer apply(Integer x, Integer y) {
449 <            invocationCount++;
449 >            invoked();
450              return value = subtract(x, y);
451          }
452      }
453 <    static final class Noop implements Runnable {
454 <        int invocationCount = 0;
453 >
454 >    class Noop extends CheckedAction implements Runnable {
455 >        Noop(ExecutionMode m) { super(m); }
456          public void run() {
457 <            invocationCount++;
457 >            invoked();
458          }
459      }
460  
461 <    static final class FailingSupplier implements Supplier<Integer> {
462 <        int invocationCount = 0;
461 >    class FailingSupplier extends CheckedAction
462 >        implements Supplier<Integer>
463 >    {
464 >        FailingSupplier(ExecutionMode m) { super(m); }
465          public Integer get() {
466 <            invocationCount++;
466 >            invoked();
467              throw new CFException();
468          }
469      }
470 <    static final class FailingConsumer implements Consumer<Integer> {
471 <        int invocationCount = 0;
470 >
471 >    class FailingConsumer extends CheckedIntegerAction
472 >        implements Consumer<Integer>
473 >    {
474 >        FailingConsumer(ExecutionMode m) { super(m); }
475          public void accept(Integer x) {
476 <            invocationCount++;
476 >            invoked();
477 >            value = x;
478              throw new CFException();
479          }
480      }
481 <    static final class FailingBiConsumer implements BiConsumer<Integer, Integer> {
482 <        int invocationCount = 0;
481 >
482 >    class FailingBiConsumer extends CheckedIntegerAction
483 >        implements BiConsumer<Integer, Integer>
484 >    {
485 >        FailingBiConsumer(ExecutionMode m) { super(m); }
486          public void accept(Integer x, Integer y) {
487 <            invocationCount++;
487 >            invoked();
488 >            value = subtract(x, y);
489              throw new CFException();
490          }
491      }
492 <    static final class FailingFunction implements Function<Integer, Integer> {
493 <        int invocationCount = 0;
492 >
493 >    class FailingFunction extends CheckedIntegerAction
494 >        implements Function<Integer, Integer>
495 >    {
496 >        FailingFunction(ExecutionMode m) { super(m); }
497          public Integer apply(Integer x) {
498 <            invocationCount++;
498 >            invoked();
499 >            value = x;
500              throw new CFException();
501          }
502      }
503 <    static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> {
504 <        int invocationCount = 0;
503 >
504 >    class FailingBiFunction extends CheckedIntegerAction
505 >        implements BiFunction<Integer, Integer, Integer>
506 >    {
507 >        FailingBiFunction(ExecutionMode m) { super(m); }
508          public Integer apply(Integer x, Integer y) {
509 <            invocationCount++;
509 >            invoked();
510 >            value = subtract(x, y);
511              throw new CFException();
512          }
513      }
514 <    static final class FailingNoop implements Runnable {
515 <        int invocationCount = 0;
514 >
515 >    class FailingRunnable extends CheckedAction implements Runnable {
516 >        FailingRunnable(ExecutionMode m) { super(m); }
517          public void run() {
518 <            invocationCount++;
518 >            invoked();
519              throw new CFException();
520          }
521      }
522  
523 <    static final class CompletableFutureInc
524 <        implements Function<Integer, CompletableFuture<Integer>> {
525 <        int invocationCount = 0;
523 >
524 >    class CompletableFutureInc extends CheckedIntegerAction
525 >        implements Function<Integer, CompletableFuture<Integer>>
526 >    {
527 >        CompletableFutureInc(ExecutionMode m) { super(m); }
528          public CompletableFuture<Integer> apply(Integer x) {
529 <            invocationCount++;
529 >            invoked();
530 >            value = x;
531              CompletableFuture<Integer> f = new CompletableFuture<>();
532              f.complete(inc(x));
533              return f;
534          }
535      }
536  
537 <    static final class FailingCompletableFutureFunction
538 <        implements Function<Integer, CompletableFuture<Integer>> {
539 <        int invocationCount = 0;
537 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
538 >        implements Function<Integer, CompletableFuture<Integer>>
539 >    {
540 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
541          public CompletableFuture<Integer> apply(Integer x) {
542 <            invocationCount++;
542 >            invoked();
543 >            value = x;
544              throw new CFException();
545          }
546      }
547  
548      // Used for explicit executor tests
549      static final class ThreadExecutor implements Executor {
550 <        AtomicInteger count = new AtomicInteger(0);
550 >        final AtomicInteger count = new AtomicInteger(0);
551 >        static final ThreadGroup tg = new ThreadGroup("ThreadExecutor");
552 >        static boolean startedCurrentThread() {
553 >            return Thread.currentThread().getThreadGroup() == tg;
554 >        }
555  
556          public void execute(Runnable r) {
557              count.getAndIncrement();
558 <            new Thread(r).start();
558 >            new Thread(tg, r).start();
559          }
560      }
561  
562      /**
563       * Permits the testing of parallel code for the 3 different
564 <     * execution modes without repeating all the testing code.
564 >     * execution modes without copy/pasting all the test methods.
565       */
566      enum ExecutionMode {
567          DEFAULT {
568              public void checkExecutionMode() {
569 +                assertFalse(ThreadExecutor.startedCurrentThread());
570                  assertNull(ForkJoinTask.getPool());
571              }
572 +            public CompletableFuture<Void> runAsync(Runnable a) {
573 +                throw new UnsupportedOperationException();
574 +            }
575 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
576 +                throw new UnsupportedOperationException();
577 +            }
578              public <T> CompletableFuture<Void> thenRun
579                  (CompletableFuture<T> f, Runnable a) {
580                  return f.thenRun(a);
# Line 531 | Line 643 | public class CompletableFutureTest exten
643                  assertSame(ForkJoinPool.commonPool(),
644                             ForkJoinTask.getPool());
645              }
646 +            public CompletableFuture<Void> runAsync(Runnable a) {
647 +                return CompletableFuture.runAsync(a);
648 +            }
649 +            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
650 +                return CompletableFuture.supplyAsync(a);
651 +            }
652              public <T> CompletableFuture<Void> thenRun
653                  (CompletableFuture<T> f, Runnable a) {
654                  return f.thenRunAsync(a);
# Line 596 | Line 714 | public class CompletableFutureTest exten
714  
715          EXECUTOR {
716              public void checkExecutionMode() {
717 <                //TODO
717 >                assertTrue(ThreadExecutor.startedCurrentThread());
718 >            }
719 >            public CompletableFuture<Void> runAsync(Runnable a) {
720 >                return CompletableFuture.runAsync(a, new ThreadExecutor());
721 >            }
722 >            public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) {
723 >                return CompletableFuture.supplyAsync(a, new ThreadExecutor());
724              }
725              public <T> CompletableFuture<Void> thenRun
726                  (CompletableFuture<T> f, Runnable a) {
# Line 662 | Line 786 | public class CompletableFutureTest exten
786          };
787  
788          public abstract void checkExecutionMode();
789 +        public abstract CompletableFuture<Void> runAsync(Runnable a);
790 +        public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a);
791          public abstract <T> CompletableFuture<Void> thenRun
792              (CompletableFuture<T> f, Runnable a);
793          public abstract <T> CompletableFuture<Void> thenAccept
# Line 725 | Line 851 | public class CompletableFutureTest exten
851          assertEquals(0, a.get());
852      }}
853  
728
854      /**
855       * exceptionally action completes with function value on source
856       * exception
# Line 740 | Line 865 | public class CompletableFutureTest exten
865          if (!createIncomplete) f.completeExceptionally(ex);
866          final CompletableFuture<Integer> g = f.exceptionally
867              ((Throwable t) -> {
868 +                ExecutionMode.DEFAULT.checkExecutionMode();
869                  threadAssertSame(t, ex);
870                  a.getAndIncrement();
871                  return v1;
# Line 761 | Line 887 | public class CompletableFutureTest exten
887          if (!createIncomplete) f.completeExceptionally(ex1);
888          final CompletableFuture<Integer> g = f.exceptionally
889              ((Throwable t) -> {
890 +                ExecutionMode.DEFAULT.checkExecutionMode();
891                  threadAssertSame(t, ex1);
892                  a.getAndIncrement();
893                  throw ex2;
894              });
895          if (createIncomplete) f.completeExceptionally(ex1);
896  
897 <        checkCompletedWithWrappedCFException(g, ex2);
897 >        checkCompletedWithWrappedException(g, ex2);
898 >        assertEquals(1, a.get());
899 >    }}
900 >
901 >    /**
902 >     * whenComplete action executes on normal completion, propagating
903 >     * source result.
904 >     */
905 >    public void testWhenComplete_normalCompletion1() {
906 >        for (ExecutionMode m : ExecutionMode.values())
907 >        for (boolean createIncomplete : new boolean[] { true, false })
908 >        for (Integer v1 : new Integer[] { 1, null })
909 >    {
910 >        final AtomicInteger a = new AtomicInteger(0);
911 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
912 >        if (!createIncomplete) f.complete(v1);
913 >        final CompletableFuture<Integer> g = m.whenComplete
914 >            (f,
915 >             (Integer x, Throwable t) -> {
916 >                m.checkExecutionMode();
917 >                threadAssertSame(x, v1);
918 >                threadAssertNull(t);
919 >                a.getAndIncrement();
920 >            });
921 >        if (createIncomplete) f.complete(v1);
922 >
923 >        checkCompletedNormally(g, v1);
924 >        checkCompletedNormally(f, v1);
925 >        assertEquals(1, a.get());
926 >    }}
927 >
928 >    /**
929 >     * whenComplete action executes on exceptional completion, propagating
930 >     * source result.
931 >     */
932 >    public void testWhenComplete_exceptionalCompletion() {
933 >        for (ExecutionMode m : ExecutionMode.values())
934 >        for (boolean createIncomplete : new boolean[] { true, false })
935 >        for (Integer v1 : new Integer[] { 1, null })
936 >    {
937 >        final AtomicInteger a = new AtomicInteger(0);
938 >        final CFException ex = new CFException();
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) f.completeExceptionally(ex);
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertNull(x);
946 >                threadAssertSame(t, ex);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) f.completeExceptionally(ex);
950 >
951 >        checkCompletedWithWrappedException(g, ex);
952 >        checkCompletedExceptionally(f, ex);
953 >        assertEquals(1, a.get());
954 >    }}
955 >
956 >    /**
957 >     * whenComplete action executes on cancelled source, propagating
958 >     * CancellationException.
959 >     */
960 >    public void testWhenComplete_sourceCancelled() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
963 >        for (boolean createIncomplete : new boolean[] { true, false })
964 >    {
965 >        final AtomicInteger a = new AtomicInteger(0);
966 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
967 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
968 >        final CompletableFuture<Integer> g = m.whenComplete
969 >            (f,
970 >             (Integer x, Throwable t) -> {
971 >                m.checkExecutionMode();
972 >                threadAssertNull(x);
973 >                threadAssertTrue(t instanceof CancellationException);
974 >                a.getAndIncrement();
975 >            });
976 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
977 >
978 >        checkCompletedWithWrappedCancellationException(g);
979 >        checkCancelled(f);
980 >        assertEquals(1, a.get());
981 >    }}
982 >
983 >    /**
984 >     * If a whenComplete action throws an exception when triggered by
985 >     * a normal completion, it completes exceptionally
986 >     */
987 >    public void testWhenComplete_actionFailed() {
988 >        for (boolean createIncomplete : new boolean[] { true, false })
989 >        for (ExecutionMode m : ExecutionMode.values())
990 >        for (Integer v1 : new Integer[] { 1, null })
991 >    {
992 >        final AtomicInteger a = new AtomicInteger(0);
993 >        final CFException ex = new CFException();
994 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
995 >        if (!createIncomplete) f.complete(v1);
996 >        final CompletableFuture<Integer> g = m.whenComplete
997 >            (f,
998 >             (Integer x, Throwable t) -> {
999 >                m.checkExecutionMode();
1000 >                threadAssertSame(x, v1);
1001 >                threadAssertNull(t);
1002 >                a.getAndIncrement();
1003 >                throw ex;
1004 >            });
1005 >        if (createIncomplete) f.complete(v1);
1006 >
1007 >        checkCompletedWithWrappedException(g, ex);
1008 >        checkCompletedNormally(f, v1);
1009 >        assertEquals(1, a.get());
1010 >    }}
1011 >
1012 >    /**
1013 >     * If a whenComplete action throws an exception when triggered by
1014 >     * a source completion that also throws an exception, the source
1015 >     * exception takes precedence.
1016 >     */
1017 >    public void testWhenComplete_actionFailedSourceFailed() {
1018 >        for (boolean createIncomplete : new boolean[] { true, false })
1019 >        for (ExecutionMode m : ExecutionMode.values())
1020 >        for (Integer v1 : new Integer[] { 1, null })
1021 >    {
1022 >        final AtomicInteger a = new AtomicInteger(0);
1023 >        final CFException ex1 = new CFException();
1024 >        final CFException ex2 = new CFException();
1025 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1026 >
1027 >        if (!createIncomplete) f.completeExceptionally(ex1);
1028 >        final CompletableFuture<Integer> g = m.whenComplete
1029 >            (f,
1030 >             (Integer x, Throwable t) -> {
1031 >                m.checkExecutionMode();
1032 >                threadAssertSame(t, ex1);
1033 >                threadAssertNull(x);
1034 >                a.getAndIncrement();
1035 >                throw ex2;
1036 >            });
1037 >        if (createIncomplete) f.completeExceptionally(ex1);
1038 >
1039 >        checkCompletedWithWrappedException(g, ex1);
1040 >        checkCompletedExceptionally(f, ex1);
1041          assertEquals(1, a.get());
1042      }}
1043  
# Line 786 | Line 1056 | public class CompletableFutureTest exten
1056          final CompletableFuture<Integer> g = m.handle
1057              (f,
1058               (Integer x, Throwable t) -> {
1059 +                m.checkExecutionMode();
1060                  threadAssertSame(x, v1);
1061                  threadAssertNull(t);
1062                  a.getAndIncrement();
# Line 814 | Line 1085 | public class CompletableFutureTest exten
1085          final CompletableFuture<Integer> g = m.handle
1086              (f,
1087               (Integer x, Throwable t) -> {
1088 +                m.checkExecutionMode();
1089                  threadAssertNull(x);
1090                  threadAssertSame(t, ex);
1091                  a.getAndIncrement();
# Line 822 | Line 1094 | public class CompletableFutureTest exten
1094          if (createIncomplete) f.completeExceptionally(ex);
1095  
1096          checkCompletedNormally(g, v1);
1097 <        checkCompletedWithWrappedCFException(f, ex);
1097 >        checkCompletedExceptionally(f, ex);
1098          assertEquals(1, a.get());
1099      }}
1100  
# Line 842 | Line 1114 | public class CompletableFutureTest exten
1114          final CompletableFuture<Integer> g = m.handle
1115              (f,
1116               (Integer x, Throwable t) -> {
1117 +                m.checkExecutionMode();
1118                  threadAssertNull(x);
1119                  threadAssertTrue(t instanceof CancellationException);
1120                  a.getAndIncrement();
# Line 869 | Line 1142 | public class CompletableFutureTest exten
1142          final CompletableFuture<Integer> g = m.handle
1143              (f,
1144               (Integer x, Throwable t) -> {
1145 +                m.checkExecutionMode();
1146                  threadAssertNull(x);
1147                  threadAssertSame(ex1, t);
1148                  a.getAndIncrement();
# Line 876 | Line 1150 | public class CompletableFutureTest exten
1150              });
1151          if (createIncomplete) f.completeExceptionally(ex1);
1152  
1153 <        checkCompletedWithWrappedCFException(g, ex2);
1154 <        checkCompletedWithWrappedCFException(f, ex1);
1153 >        checkCompletedWithWrappedException(g, ex2);
1154 >        checkCompletedExceptionally(f, ex1);
1155          assertEquals(1, a.get());
1156      }}
1157  
# Line 893 | Line 1167 | public class CompletableFutureTest exten
1167          final CompletableFuture<Integer> g = m.handle
1168              (f,
1169               (Integer x, Throwable t) -> {
1170 +                m.checkExecutionMode();
1171                  threadAssertSame(x, v1);
1172                  threadAssertNull(t);
1173                  a.getAndIncrement();
# Line 900 | Line 1175 | public class CompletableFutureTest exten
1175              });
1176          if (createIncomplete) f.complete(v1);
1177  
1178 <        checkCompletedWithWrappedCFException(g, ex);
1178 >        checkCompletedWithWrappedException(g, ex);
1179          checkCompletedNormally(f, v1);
1180          assertEquals(1, a.get());
1181      }}
# Line 908 | Line 1183 | public class CompletableFutureTest exten
1183      /**
1184       * runAsync completes after running Runnable
1185       */
1186 <    public void testRunAsync() {
1187 <        Noop r = new Noop();
1188 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1189 <        assertNull(f.join());
1190 <        assertEquals(1, r.invocationCount);
1191 <        checkCompletedNormally(f, null);
1192 <    }
1193 <
1194 <    /**
920 <     * runAsync with executor completes after running Runnable
921 <     */
922 <    public void testRunAsync2() {
923 <        Noop r = new Noop();
924 <        ThreadExecutor exec = new ThreadExecutor();
925 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
1186 >    public void testRunAsync_normalCompletion() {
1187 >        ExecutionMode[] executionModes = {
1188 >            ExecutionMode.ASYNC,
1189 >            ExecutionMode.EXECUTOR,
1190 >        };
1191 >        for (ExecutionMode m : executionModes)
1192 >    {
1193 >        final Noop r = new Noop(m);
1194 >        final CompletableFuture<Void> f = m.runAsync(r);
1195          assertNull(f.join());
927        assertEquals(1, r.invocationCount);
1196          checkCompletedNormally(f, null);
1197 <        assertEquals(1, exec.count.get());
1198 <    }
1197 >        r.assertInvoked();
1198 >    }}
1199  
1200      /**
1201       * failing runAsync completes exceptionally after running Runnable
1202       */
1203 <    public void testRunAsync3() {
1204 <        FailingNoop r = new FailingNoop();
1205 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r);
1203 >    public void testRunAsync_exceptionalCompletion() {
1204 >        ExecutionMode[] executionModes = {
1205 >            ExecutionMode.ASYNC,
1206 >            ExecutionMode.EXECUTOR,
1207 >        };
1208 >        for (ExecutionMode m : executionModes)
1209 >    {
1210 >        final FailingRunnable r = new FailingRunnable(m);
1211 >        final CompletableFuture<Void> f = m.runAsync(r);
1212          checkCompletedWithWrappedCFException(f);
1213 <        assertEquals(1, r.invocationCount);
1214 <    }
1213 >        r.assertInvoked();
1214 >    }}
1215  
1216      /**
1217       * supplyAsync completes with result of supplier
1218       */
1219 <    public void testSupplyAsync() {
1220 <        CompletableFuture<Integer> f;
1221 <        f = CompletableFuture.supplyAsync(supplyOne);
1222 <        assertEquals(f.join(), one);
1223 <        checkCompletedNormally(f, one);
1224 <    }
1225 <
1226 <    /**
1227 <     * supplyAsync with executor completes with result of supplier
1228 <     */
1229 <    public void testSupplyAsync2() {
1230 <        CompletableFuture<Integer> f;
1231 <        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
1232 <        assertEquals(f.join(), one);
959 <        checkCompletedNormally(f, one);
960 <    }
1219 >    public void testSupplyAsync_normalCompletion() {
1220 >        ExecutionMode[] executionModes = {
1221 >            ExecutionMode.ASYNC,
1222 >            ExecutionMode.EXECUTOR,
1223 >        };
1224 >        for (ExecutionMode m : executionModes)
1225 >        for (Integer v1 : new Integer[] { 1, null })
1226 >    {
1227 >        final IntegerSupplier r = new IntegerSupplier(m, v1);
1228 >        final CompletableFuture<Integer> f = m.supplyAsync(r);
1229 >        assertSame(v1, f.join());
1230 >        checkCompletedNormally(f, v1);
1231 >        r.assertInvoked();
1232 >    }}
1233  
1234      /**
1235       * Failing supplyAsync completes exceptionally
1236       */
1237 <    public void testSupplyAsync3() {
1238 <        FailingSupplier r = new FailingSupplier();
1239 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r);
1237 >    public void testSupplyAsync_exceptionalCompletion() {
1238 >        ExecutionMode[] executionModes = {
1239 >            ExecutionMode.ASYNC,
1240 >            ExecutionMode.EXECUTOR,
1241 >        };
1242 >        for (ExecutionMode m : executionModes)
1243 >    {
1244 >        FailingSupplier r = new FailingSupplier(m);
1245 >        CompletableFuture<Integer> f = m.supplyAsync(r);
1246          checkCompletedWithWrappedCFException(f);
1247 <        assertEquals(1, r.invocationCount);
1248 <    }
1247 >        r.assertInvoked();
1248 >    }}
1249  
1250      // seq completion methods
1251  
# Line 980 | Line 1258 | public class CompletableFutureTest exten
1258          for (Integer v1 : new Integer[] { 1, null })
1259      {
1260          final CompletableFuture<Integer> f = new CompletableFuture<>();
1261 <        final Noop r = new Noop();
1261 >        final Noop r = new Noop(m);
1262          if (!createIncomplete) f.complete(v1);
1263          final CompletableFuture<Void> g = m.thenRun(f, r);
1264 <        if (createIncomplete) f.complete(v1);
1264 >        if (createIncomplete) {
1265 >            checkIncomplete(g);
1266 >            f.complete(v1);
1267 >        }
1268  
1269          checkCompletedNormally(g, null);
1270          checkCompletedNormally(f, v1);
1271 <        assertEquals(1, r.invocationCount);
1271 >        r.assertInvoked();
1272      }}
1273  
1274      /**
# Line 1000 | Line 1281 | public class CompletableFutureTest exten
1281      {
1282          final CFException ex = new CFException();
1283          final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 <        final Noop r = new Noop();
1284 >        final Noop r = new Noop(m);
1285          if (!createIncomplete) f.completeExceptionally(ex);
1286          final CompletableFuture<Void> g = m.thenRun(f, r);
1287 <        if (createIncomplete) f.completeExceptionally(ex);
1287 >        if (createIncomplete) {
1288 >            checkIncomplete(g);
1289 >            f.completeExceptionally(ex);
1290 >        }
1291  
1292 <        checkCompletedWithWrappedCFException(g, ex);
1293 <        checkCompletedWithWrappedCFException(f, ex);
1294 <        assertEquals(0, r.invocationCount);
1292 >        checkCompletedWithWrappedException(g, ex);
1293 >        checkCompletedExceptionally(f, ex);
1294 >        r.assertNotInvoked();
1295      }}
1296  
1297      /**
# Line 1019 | Line 1303 | public class CompletableFutureTest exten
1303          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1304      {
1305          final CompletableFuture<Integer> f = new CompletableFuture<>();
1306 <        final Noop r = new Noop();
1306 >        final Noop r = new Noop(m);
1307          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 <        final CompletableFuture<Void> g = f.thenRun(r);
1309 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1309 >        if (createIncomplete) {
1310 >            checkIncomplete(g);
1311 >            assertTrue(f.cancel(mayInterruptIfRunning));
1312 >        }
1313  
1314          checkCompletedWithWrappedCancellationException(g);
1315          checkCancelled(f);
1316 <        assertEquals(0, r.invocationCount);
1316 >        r.assertNotInvoked();
1317      }}
1318  
1319      /**
# Line 1038 | Line 1325 | public class CompletableFutureTest exten
1325          for (Integer v1 : new Integer[] { 1, null })
1326      {
1327          final CompletableFuture<Integer> f = new CompletableFuture<>();
1328 <        final FailingNoop r = new FailingNoop();
1328 >        final FailingRunnable r = new FailingRunnable(m);
1329          if (!createIncomplete) f.complete(v1);
1330 <        final CompletableFuture<Void> g = f.thenRun(r);
1331 <        if (createIncomplete) f.complete(v1);
1330 >        final CompletableFuture<Void> g = m.thenRun(f, r);
1331 >        if (createIncomplete) {
1332 >            checkIncomplete(g);
1333 >            f.complete(v1);
1334 >        }
1335  
1336          checkCompletedWithWrappedCFException(g);
1337          checkCompletedNormally(f, v1);
# Line 1056 | Line 1346 | public class CompletableFutureTest exten
1346          for (Integer v1 : new Integer[] { 1, null })
1347      {
1348          final CompletableFuture<Integer> f = new CompletableFuture<>();
1349 <        final IncFunction r = new IncFunction();
1349 >        final IncFunction r = new IncFunction(m);
1350          if (!createIncomplete) f.complete(v1);
1351          final CompletableFuture<Integer> g = m.thenApply(f, r);
1352          if (createIncomplete) {
# Line 1066 | Line 1356 | public class CompletableFutureTest exten
1356  
1357          checkCompletedNormally(g, inc(v1));
1358          checkCompletedNormally(f, v1);
1359 <        assertEquals(1, r.invocationCount);
1359 >        r.assertValue(inc(v1));
1360      }}
1361  
1362      /**
# Line 1079 | Line 1369 | public class CompletableFutureTest exten
1369      {
1370          final CFException ex = new CFException();
1371          final CompletableFuture<Integer> f = new CompletableFuture<>();
1372 <        final IncFunction r = new IncFunction();
1372 >        final IncFunction r = new IncFunction(m);
1373          if (!createIncomplete) f.completeExceptionally(ex);
1374          final CompletableFuture<Integer> g = m.thenApply(f, r);
1375 <        if (createIncomplete) f.completeExceptionally(ex);
1375 >        if (createIncomplete) {
1376 >            checkIncomplete(g);
1377 >            f.completeExceptionally(ex);
1378 >        }
1379  
1380 <        checkCompletedWithWrappedCFException(g, ex);
1381 <        checkCompletedWithWrappedCFException(f, ex);
1382 <        assertEquals(0, r.invocationCount);
1380 >        checkCompletedWithWrappedException(g, ex);
1381 >        checkCompletedExceptionally(f, ex);
1382 >        r.assertNotInvoked();
1383      }}
1384  
1385      /**
# Line 1098 | Line 1391 | public class CompletableFutureTest exten
1391          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1392      {
1393          final CompletableFuture<Integer> f = new CompletableFuture<>();
1394 <        final IncFunction r = new IncFunction();
1394 >        final IncFunction r = new IncFunction(m);
1395          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 <        final CompletableFuture<Integer> g = f.thenApply(r);
1397 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1397 >        if (createIncomplete) {
1398 >            checkIncomplete(g);
1399 >            assertTrue(f.cancel(mayInterruptIfRunning));
1400 >        }
1401  
1402          checkCompletedWithWrappedCancellationException(g);
1403          checkCancelled(f);
1404 <        assertEquals(0, r.invocationCount);
1404 >        r.assertNotInvoked();
1405      }}
1406  
1407      /**
# Line 1117 | Line 1413 | public class CompletableFutureTest exten
1413          for (Integer v1 : new Integer[] { 1, null })
1414      {
1415          final CompletableFuture<Integer> f = new CompletableFuture<>();
1416 <        final FailingFunction r = new FailingFunction();
1416 >        final FailingFunction r = new FailingFunction(m);
1417          if (!createIncomplete) f.complete(v1);
1418 <        final CompletableFuture<Integer> g = f.thenApply(r);
1419 <        if (createIncomplete) f.complete(v1);
1418 >        final CompletableFuture<Integer> g = m.thenApply(f, r);
1419 >        if (createIncomplete) {
1420 >            checkIncomplete(g);
1421 >            f.complete(v1);
1422 >        }
1423  
1424          checkCompletedWithWrappedCFException(g);
1425          checkCompletedNormally(f, v1);
# Line 1129 | Line 1428 | public class CompletableFutureTest exten
1428      /**
1429       * thenAccept result completes normally after normal completion of source
1430       */
1431 <    public void testThenAccept() {
1133 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1134 <        IncAction r = new IncAction();
1135 <        CompletableFuture<Void> g = f.thenAccept(r);
1136 <        f.complete(one);
1137 <        checkCompletedNormally(g, null);
1138 <        assertEquals(r.value, (Integer) 2);
1139 <    }
1140 <
1141 <    /**
1142 <     * thenAccept result completes exceptionally after exceptional
1143 <     * completion of source
1144 <     */
1145 <    public void testThenAccept2() {
1146 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1147 <        IncAction r = new IncAction();
1148 <        CompletableFuture<Void> g = f.thenAccept(r);
1149 <        f.completeExceptionally(new CFException());
1150 <        checkCompletedWithWrappedCFException(g);
1151 <    }
1152 <
1153 <    /**
1154 <     * thenAccept result completes exceptionally if action does
1155 <     */
1156 <    public void testThenAccept3() {
1157 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1158 <        FailingConsumer r = new FailingConsumer();
1159 <        CompletableFuture<Void> g = f.thenAccept(r);
1160 <        f.complete(one);
1161 <        checkCompletedWithWrappedCFException(g);
1162 <        assertEquals(1, r.invocationCount);
1163 <    }
1164 <
1165 <    /**
1166 <     * thenAccept result completes exceptionally if source cancelled
1167 <     */
1168 <    public void testThenAccept4() {
1169 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1170 <        IncAction r = new IncAction();
1171 <        CompletableFuture<Void> g = f.thenAccept(r);
1172 <        assertTrue(f.cancel(true));
1173 <        checkCompletedWithWrappedCancellationException(g);
1174 <    }
1175 <
1176 <    /**
1177 <     * thenCombine result completes normally after normal completion
1178 <     * of sources
1179 <     */
1180 <    public void testThenCombine_normalCompletion1() {
1181 <        for (boolean createIncomplete : new boolean[] { true, false })
1182 <        for (boolean fFirst : new boolean[] { true, false })
1431 >    public void testThenAccept_normalCompletion() {
1432          for (ExecutionMode m : ExecutionMode.values())
1433 +        for (boolean createIncomplete : new boolean[] { true, false })
1434          for (Integer v1 : new Integer[] { 1, null })
1185        for (Integer v2 : new Integer[] { 2, null })
1435      {
1436          final CompletableFuture<Integer> f = new CompletableFuture<>();
1437 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1438 <        final SubtractFunction r = new SubtractFunction();
1439 <        CompletableFuture<Integer> h = null;
1440 <        if (createIncomplete) h = m.thenCombine(f, g, r);
1441 <
1193 <        if (fFirst)
1194 <            f.complete(v1);
1195 <        else
1196 <            g.complete(v2);
1197 <        if (createIncomplete) checkIncomplete(h);
1198 <        assertEquals(0, r.invocationCount);
1199 <        if (!fFirst)
1437 >        final NoopConsumer r = new NoopConsumer(m);
1438 >        if (!createIncomplete) f.complete(v1);
1439 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1440 >        if (createIncomplete) {
1441 >            checkIncomplete(g);
1442              f.complete(v1);
1443 <        else
1202 <            g.complete(v2);
1203 <        if (!createIncomplete) h = m.thenCombine(f, g, r);
1443 >        }
1444  
1445 <        checkCompletedNormally(h, subtract(v1, v2));
1445 >        checkCompletedNormally(g, null);
1446 >        r.assertValue(v1);
1447          checkCompletedNormally(f, v1);
1207        checkCompletedNormally(g, v2);
1208        assertEquals(1, r.invocationCount);
1448      }}
1449  
1450      /**
1451 <     * thenCombine result completes exceptionally after exceptional
1452 <     * completion of either source
1451 >     * thenAccept result completes exceptionally after exceptional
1452 >     * completion of source
1453       */
1454 <    public void testThenCombine_exceptionalCompletion1() {
1454 >    public void testThenAccept_exceptionalCompletion() {
1455          for (ExecutionMode m : ExecutionMode.values())
1456 <        for (Integer v1 : new Integer[] { 1, null })
1456 >        for (boolean createIncomplete : new boolean[] { true, false })
1457      {
1219        final CompletableFuture<Integer> f = new CompletableFuture<>();
1220        final CompletableFuture<Integer> g = new CompletableFuture<>();
1221        final SubtractFunction r = new SubtractFunction();
1222        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1458          final CFException ex = new CFException();
1224
1225        f.completeExceptionally(ex);
1226        checkIncomplete(h);
1227        g.complete(v1);
1228
1229        checkCompletedWithWrappedCFException(h, ex);
1230        checkCompletedWithWrappedCFException(f, ex);
1231        assertEquals(0, r.invocationCount);
1232        checkCompletedNormally(g, v1);
1233    }}
1234
1235    public void testThenCombine_exceptionalCompletion2() {
1236        for (ExecutionMode m : ExecutionMode.values())
1237        for (Integer v1 : new Integer[] { 1, null })
1238    {
1459          final CompletableFuture<Integer> f = new CompletableFuture<>();
1460 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1461 <        final SubtractFunction r = new SubtractFunction();
1462 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1463 <        final CFException ex = new CFException();
1464 <
1465 <        g.completeExceptionally(ex);
1466 <        checkIncomplete(h);
1247 <        f.complete(v1);
1248 <
1249 <        checkCompletedWithWrappedCFException(h, ex);
1250 <        checkCompletedWithWrappedCFException(g, ex);
1251 <        assertEquals(0, r.invocationCount);
1252 <        checkCompletedNormally(f, v1);
1253 <    }}
1254 <
1255 <    public void testThenCombine_exceptionalCompletion3() {
1256 <        for (ExecutionMode m : ExecutionMode.values())
1257 <        for (Integer v1 : new Integer[] { 1, null })
1258 <    {
1259 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1260 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1261 <        final SubtractFunction r = new SubtractFunction();
1262 <        final CFException ex = new CFException();
1263 <
1264 <        g.completeExceptionally(ex);
1265 <        f.complete(v1);
1266 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1460 >        final NoopConsumer r = new NoopConsumer(m);
1461 >        if (!createIncomplete) f.completeExceptionally(ex);
1462 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1463 >        if (createIncomplete) {
1464 >            checkIncomplete(g);
1465 >            f.completeExceptionally(ex);
1466 >        }
1467  
1468 <        checkCompletedWithWrappedCFException(h, ex);
1469 <        checkCompletedWithWrappedCFException(g, ex);
1470 <        assertEquals(0, r.invocationCount);
1271 <        checkCompletedNormally(f, v1);
1468 >        checkCompletedWithWrappedException(g, ex);
1469 >        checkCompletedExceptionally(f, ex);
1470 >        r.assertNotInvoked();
1471      }}
1472  
1473 <    public void testThenCombine_exceptionalCompletion4() {
1473 >    /**
1474 >     * thenAccept result completes exceptionally if source cancelled
1475 >     */
1476 >    public void testThenAccept_sourceCancelled() {
1477          for (ExecutionMode m : ExecutionMode.values())
1478 <        for (Integer v1 : new Integer[] { 1, null })
1478 >        for (boolean createIncomplete : new boolean[] { true, false })
1479 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1480      {
1481          final CompletableFuture<Integer> f = new CompletableFuture<>();
1482 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1483 <        final SubtractFunction r = new SubtractFunction();
1484 <        final CFException ex = new CFException();
1485 <
1486 <        f.completeExceptionally(ex);
1487 <        g.complete(v1);
1488 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1482 >        final NoopConsumer r = new NoopConsumer(m);
1483 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1484 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1485 >        if (createIncomplete) {
1486 >            checkIncomplete(g);
1487 >            assertTrue(f.cancel(mayInterruptIfRunning));
1488 >        }
1489  
1490 <        checkCompletedWithWrappedCFException(h, ex);
1491 <        checkCompletedWithWrappedCFException(f, ex);
1492 <        assertEquals(0, r.invocationCount);
1290 <        checkCompletedNormally(g, v1);
1490 >        checkCompletedWithWrappedCancellationException(g);
1491 >        checkCancelled(f);
1492 >        r.assertNotInvoked();
1493      }}
1494  
1495      /**
1496 <     * thenCombine result completes exceptionally if action does
1496 >     * thenAccept result completes exceptionally if action does
1497       */
1498 <    public void testThenCombine_actionFailed1() {
1498 >    public void testThenAccept_actionFailed() {
1499          for (ExecutionMode m : ExecutionMode.values())
1500 +        for (boolean createIncomplete : new boolean[] { true, false })
1501          for (Integer v1 : new Integer[] { 1, null })
1299        for (Integer v2 : new Integer[] { 2, null })
1502      {
1503          final CompletableFuture<Integer> f = new CompletableFuture<>();
1504 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1505 <        final FailingBiFunction r = new FailingBiFunction();
1506 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1507 <
1508 <        f.complete(v1);
1509 <        checkIncomplete(h);
1510 <        g.complete(v2);
1504 >        final FailingConsumer r = new FailingConsumer(m);
1505 >        if (!createIncomplete) f.complete(v1);
1506 >        final CompletableFuture<Void> g = m.thenAccept(f, r);
1507 >        if (createIncomplete) {
1508 >            checkIncomplete(g);
1509 >            f.complete(v1);
1510 >        }
1511  
1512 <        checkCompletedWithWrappedCFException(h);
1512 >        checkCompletedWithWrappedCFException(g);
1513          checkCompletedNormally(f, v1);
1312        checkCompletedNormally(g, v2);
1514      }}
1515  
1516 <    public void testThenCombine_actionFailed2() {
1516 >    /**
1517 >     * thenCombine result completes normally after normal completion
1518 >     * of sources
1519 >     */
1520 >    public void testThenCombine_normalCompletion() {
1521          for (ExecutionMode m : ExecutionMode.values())
1522 +        for (boolean createIncomplete : new boolean[] { true, false })
1523 +        for (boolean fFirst : new boolean[] { true, false })
1524          for (Integer v1 : new Integer[] { 1, null })
1525          for (Integer v2 : new Integer[] { 2, null })
1526      {
1527          final CompletableFuture<Integer> f = new CompletableFuture<>();
1528          final CompletableFuture<Integer> g = new CompletableFuture<>();
1529 <        final FailingBiFunction r = new FailingBiFunction();
1323 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1529 >        final SubtractFunction r = new SubtractFunction(m);
1530  
1531 <        g.complete(v2);
1532 <        checkIncomplete(h);
1533 <        f.complete(v1);
1531 >        if (fFirst) f.complete(v1); else g.complete(v2);
1532 >        if (!createIncomplete)
1533 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1534 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1535 >        if (createIncomplete) {
1536 >            checkIncomplete(h);
1537 >            r.assertNotInvoked();
1538 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1539 >        }
1540  
1541 <        checkCompletedWithWrappedCFException(h);
1541 >        checkCompletedNormally(h, subtract(v1, v2));
1542          checkCompletedNormally(f, v1);
1543          checkCompletedNormally(g, v2);
1544 +        r.assertValue(subtract(v1, v2));
1545      }}
1546  
1547      /**
1548 <     * thenCombine result completes exceptionally if either source cancelled
1548 >     * thenCombine result completes exceptionally after exceptional
1549 >     * completion of either source
1550       */
1551 <    public void testThenCombine_sourceCancelled1() {
1338 <        for (ExecutionMode m : ExecutionMode.values())
1339 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1340 <        for (Integer v1 : new Integer[] { 1, null })
1341 <    {
1342 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1343 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1344 <        final SubtractFunction r = new SubtractFunction();
1345 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1346 <
1347 <        assertTrue(f.cancel(mayInterruptIfRunning));
1348 <        checkIncomplete(h);
1349 <        g.complete(v1);
1350 <
1351 <        checkCompletedWithWrappedCancellationException(h);
1352 <        checkCancelled(f);
1353 <        assertEquals(0, r.invocationCount);
1354 <        checkCompletedNormally(g, v1);
1355 <    }}
1356 <
1357 <    public void testThenCombine_sourceCancelled2() {
1358 <        for (ExecutionMode m : ExecutionMode.values())
1359 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1360 <        for (Integer v1 : new Integer[] { 1, null })
1361 <    {
1362 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1363 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1364 <        final SubtractFunction r = new SubtractFunction();
1365 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1366 <
1367 <        assertTrue(g.cancel(mayInterruptIfRunning));
1368 <        checkIncomplete(h);
1369 <        f.complete(v1);
1370 <
1371 <        checkCompletedWithWrappedCancellationException(h);
1372 <        checkCancelled(g);
1373 <        assertEquals(0, r.invocationCount);
1374 <        checkCompletedNormally(f, v1);
1375 <    }}
1376 <
1377 <    public void testThenCombine_sourceCancelled3() {
1551 >    public void testThenCombine_exceptionalCompletion() {
1552          for (ExecutionMode m : ExecutionMode.values())
1553 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1553 >        for (boolean createIncomplete : new boolean[] { true, false })
1554 >        for (boolean fFirst : new boolean[] { true, false })
1555          for (Integer v1 : new Integer[] { 1, null })
1556      {
1557          final CompletableFuture<Integer> f = new CompletableFuture<>();
1558          final CompletableFuture<Integer> g = new CompletableFuture<>();
1559 <        final SubtractFunction r = new SubtractFunction();
1559 >        final CFException ex = new CFException();
1560 >        final SubtractFunction r = new SubtractFunction(m);
1561  
1562 <        assertTrue(g.cancel(mayInterruptIfRunning));
1563 <        f.complete(v1);
1562 >        (fFirst ? f : g).complete(v1);
1563 >        if (!createIncomplete)
1564 >            (!fFirst ? f : g).completeExceptionally(ex);
1565          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1566 +        if (createIncomplete) {
1567 +            checkIncomplete(h);
1568 +            (!fFirst ? f : g).completeExceptionally(ex);
1569 +        }
1570  
1571 <        checkCompletedWithWrappedCancellationException(h);
1572 <        checkCancelled(g);
1573 <        assertEquals(0, r.invocationCount);
1574 <        checkCompletedNormally(f, v1);
1571 >        checkCompletedWithWrappedException(h, ex);
1572 >        r.assertNotInvoked();
1573 >        checkCompletedNormally(fFirst ? f : g, v1);
1574 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1575      }}
1576  
1577 <    public void testThenCombine_sourceCancelled4() {
1577 >    /**
1578 >     * thenCombine result completes exceptionally if either source cancelled
1579 >     */
1580 >    public void testThenCombine_sourceCancelled() {
1581          for (ExecutionMode m : ExecutionMode.values())
1582          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583 +        for (boolean createIncomplete : new boolean[] { true, false })
1584 +        for (boolean fFirst : new boolean[] { true, false })
1585          for (Integer v1 : new Integer[] { 1, null })
1586      {
1587          final CompletableFuture<Integer> f = new CompletableFuture<>();
1588          final CompletableFuture<Integer> g = new CompletableFuture<>();
1589 <        final SubtractFunction r = new SubtractFunction();
1589 >        final SubtractFunction r = new SubtractFunction(m);
1590  
1591 <        assertTrue(f.cancel(mayInterruptIfRunning));
1592 <        g.complete(v1);
1591 >        (fFirst ? f : g).complete(v1);
1592 >        if (!createIncomplete)
1593 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1594          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1595 +        if (createIncomplete) {
1596 +            checkIncomplete(h);
1597 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1598 +        }
1599  
1600          checkCompletedWithWrappedCancellationException(h);
1601 <        checkCancelled(f);
1602 <        assertEquals(0, r.invocationCount);
1603 <        checkCompletedNormally(g, v1);
1601 >        checkCancelled(!fFirst ? f : g);
1602 >        r.assertNotInvoked();
1603 >        checkCompletedNormally(fFirst ? f : g, v1);
1604      }}
1605  
1606      /**
1607 <     * thenAcceptBoth result completes normally after normal
1417 <     * completion of sources
1607 >     * thenCombine result completes exceptionally if action does
1608       */
1609 <    public void testThenAcceptBoth_normalCompletion1() {
1420 <        for (ExecutionMode m : ExecutionMode.values())
1421 <        for (Integer v1 : new Integer[] { 1, null })
1422 <        for (Integer v2 : new Integer[] { 2, null })
1423 <    {
1424 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1425 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1426 <        final SubtractAction r = new SubtractAction();
1427 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1428 <
1429 <        f.complete(v1);
1430 <        checkIncomplete(h);
1431 <        assertEquals(0, r.invocationCount);
1432 <        g.complete(v2);
1433 <
1434 <        checkCompletedNormally(h, null);
1435 <        assertEquals(subtract(v1, v2), r.value);
1436 <        checkCompletedNormally(f, v1);
1437 <        checkCompletedNormally(g, v2);
1438 <    }}
1439 <
1440 <    public void testThenAcceptBoth_normalCompletion2() {
1441 <        for (ExecutionMode m : ExecutionMode.values())
1442 <        for (Integer v1 : new Integer[] { 1, null })
1443 <        for (Integer v2 : new Integer[] { 2, null })
1444 <    {
1445 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1446 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1447 <        final SubtractAction r = new SubtractAction();
1448 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1449 <
1450 <        g.complete(v2);
1451 <        checkIncomplete(h);
1452 <        assertEquals(0, r.invocationCount);
1453 <        f.complete(v1);
1454 <
1455 <        checkCompletedNormally(h, null);
1456 <        assertEquals(subtract(v1, v2), r.value);
1457 <        checkCompletedNormally(f, v1);
1458 <        checkCompletedNormally(g, v2);
1459 <    }}
1460 <
1461 <    public void testThenAcceptBoth_normalCompletion3() {
1609 >    public void testThenCombine_actionFailed() {
1610          for (ExecutionMode m : ExecutionMode.values())
1611 +        for (boolean fFirst : new boolean[] { true, false })
1612          for (Integer v1 : new Integer[] { 1, null })
1613          for (Integer v2 : new Integer[] { 2, null })
1614      {
1615          final CompletableFuture<Integer> f = new CompletableFuture<>();
1616          final CompletableFuture<Integer> g = new CompletableFuture<>();
1617 <        final SubtractAction r = new SubtractAction();
1617 >        final FailingBiFunction r = new FailingBiFunction(m);
1618 >        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1619  
1620 <        g.complete(v2);
1621 <        f.complete(v1);
1622 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1620 >        if (fFirst) {
1621 >            f.complete(v1);
1622 >            g.complete(v2);
1623 >        } else {
1624 >            g.complete(v2);
1625 >            f.complete(v1);
1626 >        }
1627  
1628 <        checkCompletedNormally(h, null);
1475 <        assertEquals(subtract(v1, v2), r.value);
1628 >        checkCompletedWithWrappedCFException(h);
1629          checkCompletedNormally(f, v1);
1630          checkCompletedNormally(g, v2);
1631      }}
1632  
1633 <    public void testThenAcceptBoth_normalCompletion4() {
1633 >    /**
1634 >     * thenAcceptBoth result completes normally after normal
1635 >     * completion of sources
1636 >     */
1637 >    public void testThenAcceptBoth_normalCompletion() {
1638          for (ExecutionMode m : ExecutionMode.values())
1639 +        for (boolean createIncomplete : new boolean[] { true, false })
1640 +        for (boolean fFirst : new boolean[] { true, false })
1641          for (Integer v1 : new Integer[] { 1, null })
1642          for (Integer v2 : new Integer[] { 2, null })
1643      {
1644          final CompletableFuture<Integer> f = new CompletableFuture<>();
1645          final CompletableFuture<Integer> g = new CompletableFuture<>();
1646 <        final SubtractAction r = new SubtractAction();
1646 >        final SubtractAction r = new SubtractAction(m);
1647  
1648 <        f.complete(v1);
1649 <        g.complete(v2);
1648 >        if (fFirst) f.complete(v1); else g.complete(v2);
1649 >        if (!createIncomplete)
1650 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1651          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1652 +        if (createIncomplete) {
1653 +            checkIncomplete(h);
1654 +            r.assertNotInvoked();
1655 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1656 +        }
1657  
1658          checkCompletedNormally(h, null);
1659 <        assertEquals(subtract(v1, v2), r.value);
1659 >        r.assertValue(subtract(v1, v2));
1660          checkCompletedNormally(f, v1);
1661          checkCompletedNormally(g, v2);
1662      }}
# Line 1500 | Line 1665 | public class CompletableFutureTest exten
1665       * thenAcceptBoth result completes exceptionally after exceptional
1666       * completion of either source
1667       */
1668 <    public void testThenAcceptBoth_exceptionalCompletion1() {
1504 <        for (ExecutionMode m : ExecutionMode.values())
1505 <        for (Integer v1 : new Integer[] { 1, null })
1506 <    {
1507 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1508 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1509 <        final SubtractAction r = new SubtractAction();
1510 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1511 <        final CFException ex = new CFException();
1512 <
1513 <        f.completeExceptionally(ex);
1514 <        checkIncomplete(h);
1515 <        g.complete(v1);
1516 <
1517 <        checkCompletedWithWrappedCFException(h, ex);
1518 <        checkCompletedWithWrappedCFException(f, ex);
1519 <        assertEquals(0, r.invocationCount);
1520 <        checkCompletedNormally(g, v1);
1521 <    }}
1522 <
1523 <    public void testThenAcceptBoth_exceptionalCompletion2() {
1524 <        for (ExecutionMode m : ExecutionMode.values())
1525 <        for (Integer v1 : new Integer[] { 1, null })
1526 <    {
1527 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1528 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1529 <        final SubtractAction r = new SubtractAction();
1530 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1531 <        final CFException ex = new CFException();
1532 <
1533 <        g.completeExceptionally(ex);
1534 <        checkIncomplete(h);
1535 <        f.complete(v1);
1536 <
1537 <        checkCompletedWithWrappedCFException(h, ex);
1538 <        checkCompletedWithWrappedCFException(g, ex);
1539 <        assertEquals(0, r.invocationCount);
1540 <        checkCompletedNormally(f, v1);
1541 <    }}
1542 <
1543 <    public void testThenAcceptBoth_exceptionalCompletion3() {
1668 >    public void testThenAcceptBoth_exceptionalCompletion() {
1669          for (ExecutionMode m : ExecutionMode.values())
1670 +        for (boolean createIncomplete : new boolean[] { true, false })
1671 +        for (boolean fFirst : new boolean[] { true, false })
1672          for (Integer v1 : new Integer[] { 1, null })
1673      {
1674          final CompletableFuture<Integer> f = new CompletableFuture<>();
1675          final CompletableFuture<Integer> g = new CompletableFuture<>();
1549        final SubtractAction r = new SubtractAction();
1676          final CFException ex = new CFException();
1677 +        final SubtractAction r = new SubtractAction(m);
1678  
1679 <        g.completeExceptionally(ex);
1680 <        f.complete(v1);
1679 >        (fFirst ? f : g).complete(v1);
1680 >        if (!createIncomplete)
1681 >            (!fFirst ? f : g).completeExceptionally(ex);
1682          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1683 +        if (createIncomplete) {
1684 +            checkIncomplete(h);
1685 +            (!fFirst ? f : g).completeExceptionally(ex);
1686 +        }
1687  
1688 <        checkCompletedWithWrappedCFException(h, ex);
1689 <        checkCompletedWithWrappedCFException(g, ex);
1690 <        assertEquals(0, r.invocationCount);
1691 <        checkCompletedNormally(f, v1);
1688 >        checkCompletedWithWrappedException(h, ex);
1689 >        r.assertNotInvoked();
1690 >        checkCompletedNormally(fFirst ? f : g, v1);
1691 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1692      }}
1693  
1694 <    public void testThenAcceptBoth_exceptionalCompletion4() {
1694 >    /**
1695 >     * thenAcceptBoth result completes exceptionally if either source cancelled
1696 >     */
1697 >    public void testThenAcceptBoth_sourceCancelled() {
1698          for (ExecutionMode m : ExecutionMode.values())
1699 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700 +        for (boolean createIncomplete : new boolean[] { true, false })
1701 +        for (boolean fFirst : new boolean[] { true, false })
1702          for (Integer v1 : new Integer[] { 1, null })
1703      {
1704          final CompletableFuture<Integer> f = new CompletableFuture<>();
1705          final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 <        final SubtractAction r = new SubtractAction();
1569 <        final CFException ex = new CFException();
1706 >        final SubtractAction r = new SubtractAction(m);
1707  
1708 <        f.completeExceptionally(ex);
1709 <        g.complete(v1);
1708 >        (fFirst ? f : g).complete(v1);
1709 >        if (!createIncomplete)
1710 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1711          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1712 +        if (createIncomplete) {
1713 +            checkIncomplete(h);
1714 +            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1715 +        }
1716  
1717 <        checkCompletedWithWrappedCFException(h, ex);
1718 <        checkCompletedWithWrappedCFException(f, ex);
1719 <        assertEquals(0, r.invocationCount);
1720 <        checkCompletedNormally(g, v1);
1717 >        checkCompletedWithWrappedCancellationException(h);
1718 >        checkCancelled(!fFirst ? f : g);
1719 >        r.assertNotInvoked();
1720 >        checkCompletedNormally(fFirst ? f : g, v1);
1721      }}
1722  
1723      /**
1724       * thenAcceptBoth result completes exceptionally if action does
1725       */
1726 <    public void testThenAcceptBoth_actionFailed1() {
1585 <        for (ExecutionMode m : ExecutionMode.values())
1586 <        for (Integer v1 : new Integer[] { 1, null })
1587 <        for (Integer v2 : new Integer[] { 2, null })
1588 <    {
1589 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1590 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1591 <        final FailingBiConsumer r = new FailingBiConsumer();
1592 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1593 <
1594 <        f.complete(v1);
1595 <        checkIncomplete(h);
1596 <        g.complete(v2);
1597 <
1598 <        checkCompletedWithWrappedCFException(h);
1599 <        checkCompletedNormally(f, v1);
1600 <        checkCompletedNormally(g, v2);
1601 <    }}
1602 <
1603 <    public void testThenAcceptBoth_actionFailed2() {
1726 >    public void testThenAcceptBoth_actionFailed() {
1727          for (ExecutionMode m : ExecutionMode.values())
1728 +        for (boolean fFirst : new boolean[] { true, false })
1729          for (Integer v1 : new Integer[] { 1, null })
1730          for (Integer v2 : new Integer[] { 2, null })
1731      {
1732          final CompletableFuture<Integer> f = new CompletableFuture<>();
1733          final CompletableFuture<Integer> g = new CompletableFuture<>();
1734 <        final FailingBiConsumer r = new FailingBiConsumer();
1734 >        final FailingBiConsumer r = new FailingBiConsumer(m);
1735          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1736  
1737 <        g.complete(v2);
1738 <        checkIncomplete(h);
1739 <        f.complete(v1);
1737 >        if (fFirst) {
1738 >            f.complete(v1);
1739 >            g.complete(v2);
1740 >        } else {
1741 >            g.complete(v2);
1742 >            f.complete(v1);
1743 >        }
1744  
1745          checkCompletedWithWrappedCFException(h);
1746          checkCompletedNormally(f, v1);
# Line 1620 | Line 1748 | public class CompletableFutureTest exten
1748      }}
1749  
1750      /**
1623     * thenAcceptBoth result completes exceptionally if either source cancelled
1624     */
1625    public void testThenAcceptBoth_sourceCancelled1() {
1626        for (ExecutionMode m : ExecutionMode.values())
1627        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1628        for (Integer v1 : new Integer[] { 1, null })
1629    {
1630        final CompletableFuture<Integer> f = new CompletableFuture<>();
1631        final CompletableFuture<Integer> g = new CompletableFuture<>();
1632        final SubtractAction r = new SubtractAction();
1633        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1634
1635        assertTrue(f.cancel(mayInterruptIfRunning));
1636        checkIncomplete(h);
1637        g.complete(v1);
1638
1639        checkCompletedWithWrappedCancellationException(h);
1640        checkCancelled(f);
1641        assertEquals(0, r.invocationCount);
1642        checkCompletedNormally(g, v1);
1643    }}
1644
1645    public void testThenAcceptBoth_sourceCancelled2() {
1646        for (ExecutionMode m : ExecutionMode.values())
1647        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1648        for (Integer v1 : new Integer[] { 1, null })
1649    {
1650        final CompletableFuture<Integer> f = new CompletableFuture<>();
1651        final CompletableFuture<Integer> g = new CompletableFuture<>();
1652        final SubtractAction r = new SubtractAction();
1653        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1654
1655        assertTrue(g.cancel(mayInterruptIfRunning));
1656        checkIncomplete(h);
1657        f.complete(v1);
1658
1659        checkCompletedWithWrappedCancellationException(h);
1660        checkCancelled(g);
1661        assertEquals(0, r.invocationCount);
1662        checkCompletedNormally(f, v1);
1663    }}
1664
1665    public void testThenAcceptBoth_sourceCancelled3() {
1666        for (ExecutionMode m : ExecutionMode.values())
1667        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1668        for (Integer v1 : new Integer[] { 1, null })
1669    {
1670        final CompletableFuture<Integer> f = new CompletableFuture<>();
1671        final CompletableFuture<Integer> g = new CompletableFuture<>();
1672        final SubtractAction r = new SubtractAction();
1673
1674        assertTrue(g.cancel(mayInterruptIfRunning));
1675        f.complete(v1);
1676        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1677
1678        checkCompletedWithWrappedCancellationException(h);
1679        checkCancelled(g);
1680        assertEquals(0, r.invocationCount);
1681        checkCompletedNormally(f, v1);
1682    }}
1683
1684    public void testThenAcceptBoth_sourceCancelled4() {
1685        for (ExecutionMode m : ExecutionMode.values())
1686        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1687        for (Integer v1 : new Integer[] { 1, null })
1688    {
1689        final CompletableFuture<Integer> f = new CompletableFuture<>();
1690        final CompletableFuture<Integer> g = new CompletableFuture<>();
1691        final SubtractAction r = new SubtractAction();
1692
1693        assertTrue(f.cancel(mayInterruptIfRunning));
1694        g.complete(v1);
1695        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1696
1697        checkCompletedWithWrappedCancellationException(h);
1698        checkCancelled(f);
1699        assertEquals(0, r.invocationCount);
1700        checkCompletedNormally(g, v1);
1701    }}
1702
1703    /**
1751       * runAfterBoth result completes normally after normal
1752       * completion of sources
1753       */
1754 <    public void testRunAfterBoth_normalCompletion1() {
1708 <        for (ExecutionMode m : ExecutionMode.values())
1709 <        for (Integer v1 : new Integer[] { 1, null })
1710 <        for (Integer v2 : new Integer[] { 2, null })
1711 <    {
1712 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1713 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1714 <        final Noop r = new Noop();
1715 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1716 <
1717 <        f.complete(v1);
1718 <        checkIncomplete(h);
1719 <        assertEquals(0, r.invocationCount);
1720 <        g.complete(v2);
1721 <
1722 <        checkCompletedNormally(h, null);
1723 <        assertEquals(1, r.invocationCount);
1724 <        checkCompletedNormally(f, v1);
1725 <        checkCompletedNormally(g, v2);
1726 <    }}
1727 <
1728 <    public void testRunAfterBoth_normalCompletion2() {
1729 <        for (ExecutionMode m : ExecutionMode.values())
1730 <        for (Integer v1 : new Integer[] { 1, null })
1731 <        for (Integer v2 : new Integer[] { 2, null })
1732 <    {
1733 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1734 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1735 <        final Noop r = new Noop();
1736 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1737 <
1738 <        g.complete(v2);
1739 <        checkIncomplete(h);
1740 <        assertEquals(0, r.invocationCount);
1741 <        f.complete(v1);
1742 <
1743 <        checkCompletedNormally(h, null);
1744 <        assertEquals(1, r.invocationCount);
1745 <        checkCompletedNormally(f, v1);
1746 <        checkCompletedNormally(g, v2);
1747 <    }}
1748 <
1749 <    public void testRunAfterBoth_normalCompletion3() {
1750 <        for (ExecutionMode m : ExecutionMode.values())
1751 <        for (Integer v1 : new Integer[] { 1, null })
1752 <        for (Integer v2 : new Integer[] { 2, null })
1753 <    {
1754 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1755 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1756 <        final Noop r = new Noop();
1757 <
1758 <        g.complete(v2);
1759 <        f.complete(v1);
1760 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1761 <
1762 <        checkCompletedNormally(h, null);
1763 <        assertEquals(1, r.invocationCount);
1764 <        checkCompletedNormally(f, v1);
1765 <        checkCompletedNormally(g, v2);
1766 <    }}
1767 <
1768 <    public void testRunAfterBoth_normalCompletion4() {
1754 >    public void testRunAfterBoth_normalCompletion() {
1755          for (ExecutionMode m : ExecutionMode.values())
1756 +        for (boolean createIncomplete : new boolean[] { true, false })
1757 +        for (boolean fFirst : new boolean[] { true, false })
1758          for (Integer v1 : new Integer[] { 1, null })
1759          for (Integer v2 : new Integer[] { 2, null })
1760      {
1761          final CompletableFuture<Integer> f = new CompletableFuture<>();
1762          final CompletableFuture<Integer> g = new CompletableFuture<>();
1763 <        final Noop r = new Noop();
1763 >        final Noop r = new Noop(m);
1764  
1765 <        f.complete(v1);
1766 <        g.complete(v2);
1765 >        if (fFirst) f.complete(v1); else g.complete(v2);
1766 >        if (!createIncomplete)
1767 >            if (!fFirst) f.complete(v1); else g.complete(v2);
1768          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1769 +        if (createIncomplete) {
1770 +            checkIncomplete(h);
1771 +            r.assertNotInvoked();
1772 +            if (!fFirst) f.complete(v1); else g.complete(v2);
1773 +        }
1774  
1775          checkCompletedNormally(h, null);
1776 <        assertEquals(1, r.invocationCount);
1776 >        r.assertInvoked();
1777          checkCompletedNormally(f, v1);
1778          checkCompletedNormally(g, v2);
1779      }}
# Line 1788 | Line 1782 | public class CompletableFutureTest exten
1782       * runAfterBoth result completes exceptionally after exceptional
1783       * completion of either source
1784       */
1785 <    public void testRunAfterBoth_exceptionalCompletion1() {
1792 <        for (ExecutionMode m : ExecutionMode.values())
1793 <        for (Integer v1 : new Integer[] { 1, null })
1794 <    {
1795 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1796 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1797 <        final Noop r = new Noop();
1798 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1799 <        final CFException ex = new CFException();
1800 <
1801 <        f.completeExceptionally(ex);
1802 <        checkIncomplete(h);
1803 <        g.complete(v1);
1804 <
1805 <        checkCompletedWithWrappedCFException(h, ex);
1806 <        checkCompletedWithWrappedCFException(f, ex);
1807 <        assertEquals(0, r.invocationCount);
1808 <        checkCompletedNormally(g, v1);
1809 <    }}
1810 <
1811 <    public void testRunAfterBoth_exceptionalCompletion2() {
1812 <        for (ExecutionMode m : ExecutionMode.values())
1813 <        for (Integer v1 : new Integer[] { 1, null })
1814 <    {
1815 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1816 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1817 <        final Noop r = new Noop();
1818 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1819 <        final CFException ex = new CFException();
1820 <
1821 <        g.completeExceptionally(ex);
1822 <        checkIncomplete(h);
1823 <        f.complete(v1);
1824 <
1825 <        checkCompletedWithWrappedCFException(h, ex);
1826 <        checkCompletedWithWrappedCFException(g, ex);
1827 <        assertEquals(0, r.invocationCount);
1828 <        checkCompletedNormally(f, v1);
1829 <    }}
1830 <
1831 <    public void testRunAfterBoth_exceptionalCompletion3() {
1832 <        for (ExecutionMode m : ExecutionMode.values())
1833 <        for (Integer v1 : new Integer[] { 1, null })
1834 <    {
1835 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1836 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1837 <        final Noop r = new Noop();
1838 <        final CFException ex = new CFException();
1839 <
1840 <        g.completeExceptionally(ex);
1841 <        f.complete(v1);
1842 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1843 <
1844 <        checkCompletedWithWrappedCFException(h, ex);
1845 <        checkCompletedWithWrappedCFException(g, ex);
1846 <        assertEquals(0, r.invocationCount);
1847 <        checkCompletedNormally(f, v1);
1848 <    }}
1849 <
1850 <    public void testRunAfterBoth_exceptionalCompletion4() {
1785 >    public void testRunAfterBoth_exceptionalCompletion() {
1786          for (ExecutionMode m : ExecutionMode.values())
1787 +        for (boolean createIncomplete : new boolean[] { true, false })
1788 +        for (boolean fFirst : new boolean[] { true, false })
1789          for (Integer v1 : new Integer[] { 1, null })
1790      {
1791          final CompletableFuture<Integer> f = new CompletableFuture<>();
1792          final CompletableFuture<Integer> g = new CompletableFuture<>();
1856        final Noop r = new Noop();
1793          final CFException ex = new CFException();
1794 +        final Noop r = new Noop(m);
1795  
1796 <        f.completeExceptionally(ex);
1797 <        g.complete(v1);
1796 >        (fFirst ? f : g).complete(v1);
1797 >        if (!createIncomplete)
1798 >            (!fFirst ? f : g).completeExceptionally(ex);
1799          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1800 +        if (createIncomplete) {
1801 +            checkIncomplete(h);
1802 +            (!fFirst ? f : g).completeExceptionally(ex);
1803 +        }
1804  
1805 <        checkCompletedWithWrappedCFException(h, ex);
1806 <        checkCompletedWithWrappedCFException(f, ex);
1807 <        assertEquals(0, r.invocationCount);
1808 <        checkCompletedNormally(g, v1);
1867 <    }}
1868 <
1869 <    /**
1870 <     * runAfterBoth result completes exceptionally if action does
1871 <     */
1872 <    public void testRunAfterBoth_actionFailed1() {
1873 <        for (ExecutionMode m : ExecutionMode.values())
1874 <        for (Integer v1 : new Integer[] { 1, null })
1875 <        for (Integer v2 : new Integer[] { 2, null })
1876 <    {
1877 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1878 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1879 <        final FailingNoop r = new FailingNoop();
1880 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1881 <
1882 <        f.complete(v1);
1883 <        checkIncomplete(h);
1884 <        g.complete(v2);
1885 <
1886 <        checkCompletedWithWrappedCFException(h);
1887 <        checkCompletedNormally(f, v1);
1888 <        checkCompletedNormally(g, v2);
1889 <    }}
1890 <
1891 <    public void testRunAfterBoth_actionFailed2() {
1892 <        for (ExecutionMode m : ExecutionMode.values())
1893 <        for (Integer v1 : new Integer[] { 1, null })
1894 <        for (Integer v2 : new Integer[] { 2, null })
1895 <    {
1896 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1897 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1898 <        final FailingNoop r = new FailingNoop();
1899 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1900 <
1901 <        g.complete(v2);
1902 <        checkIncomplete(h);
1903 <        f.complete(v1);
1904 <
1905 <        checkCompletedWithWrappedCFException(h);
1906 <        checkCompletedNormally(f, v1);
1907 <        checkCompletedNormally(g, v2);
1805 >        checkCompletedWithWrappedException(h, ex);
1806 >        r.assertNotInvoked();
1807 >        checkCompletedNormally(fFirst ? f : g, v1);
1808 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1809      }}
1810  
1811      /**
1812       * runAfterBoth result completes exceptionally if either source cancelled
1813       */
1814 <    public void testRunAfterBoth_sourceCancelled1() {
1814 >    public void testRunAfterBoth_sourceCancelled() {
1815          for (ExecutionMode m : ExecutionMode.values())
1816          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817 +        for (boolean createIncomplete : new boolean[] { true, false })
1818 +        for (boolean fFirst : new boolean[] { true, false })
1819          for (Integer v1 : new Integer[] { 1, null })
1820      {
1821          final CompletableFuture<Integer> f = new CompletableFuture<>();
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823 <        final Noop r = new Noop();
1921 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1823 >        final Noop r = new Noop(m);
1824  
1825 <        assertTrue(f.cancel(mayInterruptIfRunning));
1826 <        checkIncomplete(h);
1827 <        g.complete(v1);
1926 <
1927 <        checkCompletedWithWrappedCancellationException(h);
1928 <        checkCancelled(f);
1929 <        assertEquals(0, r.invocationCount);
1930 <        checkCompletedNormally(g, v1);
1931 <    }}
1932 <
1933 <    public void testRunAfterBoth_sourceCancelled2() {
1934 <        for (ExecutionMode m : ExecutionMode.values())
1935 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1936 <        for (Integer v1 : new Integer[] { 1, null })
1937 <    {
1938 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1939 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1940 <        final Noop r = new Noop();
1825 >        (fFirst ? f : g).complete(v1);
1826 >        if (!createIncomplete)
1827 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1828          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1829 <
1830 <        assertTrue(g.cancel(mayInterruptIfRunning));
1831 <        checkIncomplete(h);
1832 <        f.complete(v1);
1829 >        if (createIncomplete) {
1830 >            checkIncomplete(h);
1831 >            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1832 >        }
1833  
1834          checkCompletedWithWrappedCancellationException(h);
1835 <        checkCancelled(g);
1836 <        assertEquals(0, r.invocationCount);
1837 <        checkCompletedNormally(f, v1);
1835 >        checkCancelled(!fFirst ? f : g);
1836 >        r.assertNotInvoked();
1837 >        checkCompletedNormally(fFirst ? f : g, v1);
1838      }}
1839  
1840 <    public void testRunAfterBoth_sourceCancelled3() {
1840 >    /**
1841 >     * runAfterBoth result completes exceptionally if action does
1842 >     */
1843 >    public void testRunAfterBoth_actionFailed() {
1844          for (ExecutionMode m : ExecutionMode.values())
1845 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1845 >        for (boolean fFirst : new boolean[] { true, false })
1846          for (Integer v1 : new Integer[] { 1, null })
1847 +        for (Integer v2 : new Integer[] { 2, null })
1848      {
1849          final CompletableFuture<Integer> f = new CompletableFuture<>();
1850          final CompletableFuture<Integer> g = new CompletableFuture<>();
1851 <        final Noop r = new Noop();
1851 >        final FailingRunnable r1 = new FailingRunnable(m);
1852 >        final FailingRunnable r2 = new FailingRunnable(m);
1853  
1854 <        assertTrue(g.cancel(mayInterruptIfRunning));
1855 <        f.complete(v1);
1856 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1854 >        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1855 >        if (fFirst) {
1856 >            f.complete(v1);
1857 >            g.complete(v2);
1858 >        } else {
1859 >            g.complete(v2);
1860 >            f.complete(v1);
1861 >        }
1862 >        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1863  
1864 <        checkCompletedWithWrappedCancellationException(h);
1865 <        checkCancelled(g);
1968 <        assertEquals(0, r.invocationCount);
1864 >        checkCompletedWithWrappedCFException(h1);
1865 >        checkCompletedWithWrappedCFException(h2);
1866          checkCompletedNormally(f, v1);
1867 <    }}
1971 <
1972 <    public void testRunAfterBoth_sourceCancelled4() {
1973 <        for (ExecutionMode m : ExecutionMode.values())
1974 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1975 <        for (Integer v1 : new Integer[] { 1, null })
1976 <    {
1977 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1978 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1979 <        final Noop r = new Noop();
1980 <
1981 <        assertTrue(f.cancel(mayInterruptIfRunning));
1982 <        g.complete(v1);
1983 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1984 <
1985 <        checkCompletedWithWrappedCancellationException(h);
1986 <        checkCancelled(f);
1987 <        assertEquals(0, r.invocationCount);
1988 <        checkCompletedNormally(g, v1);
1867 >        checkCompletedNormally(g, v2);
1868      }}
1869  
1870      /**
1871       * applyToEither result completes normally after normal completion
1872       * of either source
1873       */
1874 <    public void testApplyToEither_normalCompletion1() {
1874 >    public void testApplyToEither_normalCompletion() {
1875          for (ExecutionMode m : ExecutionMode.values())
1876          for (Integer v1 : new Integer[] { 1, null })
1877          for (Integer v2 : new Integer[] { 2, null })
1878      {
1879          final CompletableFuture<Integer> f = new CompletableFuture<>();
1880          final CompletableFuture<Integer> g = new CompletableFuture<>();
1881 <        final IncFunction r = new IncFunction();
1882 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1881 >        final IncFunction[] rs = new IncFunction[6];
1882 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1883  
1884 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1885 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1886 +        checkIncomplete(h0);
1887 +        checkIncomplete(h1);
1888 +        rs[0].assertNotInvoked();
1889 +        rs[1].assertNotInvoked();
1890          f.complete(v1);
1891 <        checkCompletedNormally(h, inc(v1));
1891 >        checkCompletedNormally(h0, inc(v1));
1892 >        checkCompletedNormally(h1, inc(v1));
1893 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1894 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1895 >        checkCompletedNormally(h2, inc(v1));
1896 >        checkCompletedNormally(h3, inc(v1));
1897          g.complete(v2);
1898  
1899 <        checkCompletedNormally(f, v1);
1900 <        checkCompletedNormally(g, v2);
1901 <        checkCompletedNormally(h, inc(v1));
1902 <    }}
1903 <
1904 <    public void testApplyToEither_normalCompletion2() {
1905 <        for (ExecutionMode m : ExecutionMode.values())
1906 <        for (Integer v1 : new Integer[] { 1, null })
1907 <        for (Integer v2 : new Integer[] { 2, null })
2018 <    {
2019 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2020 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2021 <        final IncFunction r = new IncFunction();
2022 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2023 <
2024 <        g.complete(v2);
2025 <        checkCompletedNormally(h, inc(v2));
2026 <        f.complete(v1);
2027 <
2028 <        checkCompletedNormally(f, v1);
2029 <        checkCompletedNormally(g, v2);
2030 <        checkCompletedNormally(h, inc(v2));
2031 <        }}
2032 <
2033 <    public void testApplyToEither_normalCompletion3() {
2034 <        for (ExecutionMode m : ExecutionMode.values())
2035 <        for (Integer v1 : new Integer[] { 1, null })
2036 <        for (Integer v2 : new Integer[] { 2, null })
2037 <    {
2038 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2039 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2040 <        final IncFunction r = new IncFunction();
2041 <
2042 <        f.complete(v1);
2043 <        g.complete(v2);
2044 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1899 >        // unspecified behavior - both source completions available
1900 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1901 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1902 >        rs[4].assertValue(h4.join());
1903 >        rs[5].assertValue(h5.join());
1904 >        assertTrue(Objects.equals(inc(v1), h4.join()) ||
1905 >                   Objects.equals(inc(v2), h4.join()));
1906 >        assertTrue(Objects.equals(inc(v1), h5.join()) ||
1907 >                   Objects.equals(inc(v2), h5.join()));
1908  
1909          checkCompletedNormally(f, v1);
1910          checkCompletedNormally(g, v2);
1911 <
1912 <        // unspecified behavior
1913 <        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1914 <                   Objects.equals(h.join(), inc(v2)));
1915 <        assertEquals(1, r.invocationCount);
1911 >        checkCompletedNormally(h0, inc(v1));
1912 >        checkCompletedNormally(h1, inc(v1));
1913 >        checkCompletedNormally(h2, inc(v1));
1914 >        checkCompletedNormally(h3, inc(v1));
1915 >        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
1916      }}
1917  
1918      /**
1919       * applyToEither result completes exceptionally after exceptional
1920       * completion of either source
1921       */
1922 <    public void testApplyToEither_exceptionalCompletion1() {
1922 >    public void testApplyToEither_exceptionalCompletion() {
1923          for (ExecutionMode m : ExecutionMode.values())
1924          for (Integer v1 : new Integer[] { 1, null })
1925      {
1926          final CompletableFuture<Integer> f = new CompletableFuture<>();
1927          final CompletableFuture<Integer> g = new CompletableFuture<>();
2065        final IncFunction r = new IncFunction();
2066        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1928          final CFException ex = new CFException();
1929 +        final IncFunction[] rs = new IncFunction[6];
1930 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1931  
1932 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1933 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1934 +        checkIncomplete(h0);
1935 +        checkIncomplete(h1);
1936 +        rs[0].assertNotInvoked();
1937 +        rs[1].assertNotInvoked();
1938          f.completeExceptionally(ex);
1939 <        checkCompletedWithWrappedCFException(h, ex);
1939 >        checkCompletedWithWrappedException(h0, ex);
1940 >        checkCompletedWithWrappedException(h1, ex);
1941 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1942 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1943 >        checkCompletedWithWrappedException(h2, ex);
1944 >        checkCompletedWithWrappedException(h3, ex);
1945          g.complete(v1);
1946  
1947 <        assertEquals(0, r.invocationCount);
1947 >        // unspecified behavior - both source completions available
1948 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1949 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1950 >        try {
1951 >            assertEquals(inc(v1), h4.join());
1952 >            rs[4].assertValue(inc(v1));
1953 >        } catch (CompletionException ok) {
1954 >            checkCompletedWithWrappedException(h4, ex);
1955 >            rs[4].assertNotInvoked();
1956 >        }
1957 >        try {
1958 >            assertEquals(inc(v1), h5.join());
1959 >            rs[5].assertValue(inc(v1));
1960 >        } catch (CompletionException ok) {
1961 >            checkCompletedWithWrappedException(h5, ex);
1962 >            rs[5].assertNotInvoked();
1963 >        }
1964 >
1965 >        checkCompletedExceptionally(f, ex);
1966          checkCompletedNormally(g, v1);
1967 <        checkCompletedWithWrappedCFException(f, ex);
1968 <        checkCompletedWithWrappedCFException(h, ex);
1967 >        checkCompletedWithWrappedException(h0, ex);
1968 >        checkCompletedWithWrappedException(h1, ex);
1969 >        checkCompletedWithWrappedException(h2, ex);
1970 >        checkCompletedWithWrappedException(h3, ex);
1971 >        checkCompletedWithWrappedException(h4, ex);
1972 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1973      }}
1974  
1975      public void testApplyToEither_exceptionalCompletion2() {
1976          for (ExecutionMode m : ExecutionMode.values())
1977 +        for (boolean fFirst : new boolean[] { true, false })
1978          for (Integer v1 : new Integer[] { 1, null })
1979      {
1980          final CompletableFuture<Integer> f = new CompletableFuture<>();
1981          final CompletableFuture<Integer> g = new CompletableFuture<>();
2085        final IncFunction r = new IncFunction();
2086        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2087        final CFException ex = new CFException();
2088
2089        g.completeExceptionally(ex);
2090        checkCompletedWithWrappedCFException(h, ex);
2091        f.complete(v1);
2092
2093        assertEquals(0, r.invocationCount);
2094        checkCompletedNormally(f, v1);
2095        checkCompletedWithWrappedCFException(g, ex);
2096        checkCompletedWithWrappedCFException(h, ex);
2097    }}
2098
2099    public void testApplyToEither_exceptionalCompletion3() {
2100        for (ExecutionMode m : ExecutionMode.values())
2101        for (Integer v1 : new Integer[] { 1, null })
2102    {
2103        final CompletableFuture<Integer> f = new CompletableFuture<>();
2104        final CompletableFuture<Integer> g = new CompletableFuture<>();
2105        final IncFunction r = new IncFunction();
1982          final CFException ex = new CFException();
1983 +        final IncFunction[] rs = new IncFunction[6];
1984 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1985  
1986 <        g.completeExceptionally(ex);
1987 <        f.complete(v1);
1988 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1986 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1987 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1988 >        if (fFirst) {
1989 >            f.complete(v1);
1990 >            g.completeExceptionally(ex);
1991 >        } else {
1992 >            g.completeExceptionally(ex);
1993 >            f.complete(v1);
1994 >        }
1995 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1996 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1997  
1998 <        // unspecified behavior
2113 <        Integer v;
1998 >        // unspecified behavior - both source completions available
1999          try {
2000 <            assertEquals(inc(v1), h.join());
2001 <            assertEquals(1, r.invocationCount);
2000 >            assertEquals(inc(v1), h0.join());
2001 >            rs[0].assertValue(inc(v1));
2002          } catch (CompletionException ok) {
2003 <            checkCompletedWithWrappedCFException(h, ex);
2004 <            assertEquals(0, r.invocationCount);
2003 >            checkCompletedWithWrappedException(h0, ex);
2004 >            rs[0].assertNotInvoked();
2005          }
2121
2122        checkCompletedWithWrappedCFException(g, ex);
2123        checkCompletedNormally(f, v1);
2124    }}
2125
2126    public void testApplyToEither_exceptionalCompletion4() {
2127        for (ExecutionMode m : ExecutionMode.values())
2128        for (Integer v1 : new Integer[] { 1, null })
2129    {
2130        final CompletableFuture<Integer> f = new CompletableFuture<>();
2131        final CompletableFuture<Integer> g = new CompletableFuture<>();
2132        final IncFunction r = new IncFunction();
2133        final CFException ex = new CFException();
2134
2135        f.completeExceptionally(ex);
2136        g.complete(v1);
2137        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2138
2139        // unspecified behavior
2140        Integer v;
2006          try {
2007 <            assertEquals(inc(v1), h.join());
2008 <            assertEquals(1, r.invocationCount);
2007 >            assertEquals(inc(v1), h1.join());
2008 >            rs[1].assertValue(inc(v1));
2009          } catch (CompletionException ok) {
2010 <            checkCompletedWithWrappedCFException(h, ex);
2011 <            assertEquals(0, r.invocationCount);
2010 >            checkCompletedWithWrappedException(h1, ex);
2011 >            rs[1].assertNotInvoked();
2012 >        }
2013 >        try {
2014 >            assertEquals(inc(v1), h2.join());
2015 >            rs[2].assertValue(inc(v1));
2016 >        } catch (CompletionException ok) {
2017 >            checkCompletedWithWrappedException(h2, ex);
2018 >            rs[2].assertNotInvoked();
2019 >        }
2020 >        try {
2021 >            assertEquals(inc(v1), h3.join());
2022 >            rs[3].assertValue(inc(v1));
2023 >        } catch (CompletionException ok) {
2024 >            checkCompletedWithWrappedException(h3, ex);
2025 >            rs[3].assertNotInvoked();
2026          }
2027  
2149        checkCompletedWithWrappedCFException(f, ex);
2150        checkCompletedNormally(g, v1);
2151    }}
2152
2153    /**
2154     * applyToEither result completes exceptionally if action does
2155     */
2156    public void testApplyToEither_actionFailed1() {
2157        for (ExecutionMode m : ExecutionMode.values())
2158        for (Integer v1 : new Integer[] { 1, null })
2159        for (Integer v2 : new Integer[] { 2, null })
2160    {
2161        final CompletableFuture<Integer> f = new CompletableFuture<>();
2162        final CompletableFuture<Integer> g = new CompletableFuture<>();
2163        final FailingFunction r = new FailingFunction();
2164        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2165
2166        f.complete(v1);
2167        checkCompletedWithWrappedCFException(h);
2168        g.complete(v2);
2169        checkCompletedNormally(f, v1);
2170        checkCompletedNormally(g, v2);
2171    }}
2172
2173    public void testApplyToEither_actionFailed2() {
2174        for (ExecutionMode m : ExecutionMode.values())
2175        for (Integer v1 : new Integer[] { 1, null })
2176        for (Integer v2 : new Integer[] { 2, null })
2177    {
2178        final CompletableFuture<Integer> f = new CompletableFuture<>();
2179        final CompletableFuture<Integer> g = new CompletableFuture<>();
2180        final FailingFunction r = new FailingFunction();
2181        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2182
2183        g.complete(v2);
2184        checkCompletedWithWrappedCFException(h);
2185        f.complete(v1);
2028          checkCompletedNormally(f, v1);
2029 <        checkCompletedNormally(g, v2);
2029 >        checkCompletedExceptionally(g, ex);
2030      }}
2031  
2032      /**
2033       * applyToEither result completes exceptionally if either source cancelled
2034       */
2035 <    public void testApplyToEither_sourceCancelled1() {
2035 >    public void testApplyToEither_sourceCancelled() {
2036          for (ExecutionMode m : ExecutionMode.values())
2037          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2038          for (Integer v1 : new Integer[] { 1, null })
2039      {
2040          final CompletableFuture<Integer> f = new CompletableFuture<>();
2041          final CompletableFuture<Integer> g = new CompletableFuture<>();
2042 <        final IncFunction r = new IncFunction();
2043 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2042 >        final IncFunction[] rs = new IncFunction[6];
2043 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2044  
2045 <        assertTrue(f.cancel(mayInterruptIfRunning));
2046 <        checkCompletedWithWrappedCancellationException(h);
2045 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2046 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2047 >        checkIncomplete(h0);
2048 >        checkIncomplete(h1);
2049 >        rs[0].assertNotInvoked();
2050 >        rs[1].assertNotInvoked();
2051 >        f.cancel(mayInterruptIfRunning);
2052 >        checkCompletedWithWrappedCancellationException(h0);
2053 >        checkCompletedWithWrappedCancellationException(h1);
2054 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2055 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2056 >        checkCompletedWithWrappedCancellationException(h2);
2057 >        checkCompletedWithWrappedCancellationException(h3);
2058          g.complete(v1);
2059  
2060 +        // unspecified behavior - both source completions available
2061 +        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2062 +        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2063 +        try {
2064 +            assertEquals(inc(v1), h4.join());
2065 +            rs[4].assertValue(inc(v1));
2066 +        } catch (CompletionException ok) {
2067 +            checkCompletedWithWrappedCancellationException(h4);
2068 +            rs[4].assertNotInvoked();
2069 +        }
2070 +        try {
2071 +            assertEquals(inc(v1), h5.join());
2072 +            rs[5].assertValue(inc(v1));
2073 +        } catch (CompletionException ok) {
2074 +            checkCompletedWithWrappedCancellationException(h5);
2075 +            rs[5].assertNotInvoked();
2076 +        }
2077 +
2078          checkCancelled(f);
2208        assertEquals(0, r.invocationCount);
2079          checkCompletedNormally(g, v1);
2080 <        checkCompletedWithWrappedCancellationException(h);
2080 >        checkCompletedWithWrappedCancellationException(h0);
2081 >        checkCompletedWithWrappedCancellationException(h1);
2082 >        checkCompletedWithWrappedCancellationException(h2);
2083 >        checkCompletedWithWrappedCancellationException(h3);
2084 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2085      }}
2086  
2087      public void testApplyToEither_sourceCancelled2() {
2088          for (ExecutionMode m : ExecutionMode.values())
2089          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2090 +        for (boolean fFirst : new boolean[] { true, false })
2091          for (Integer v1 : new Integer[] { 1, null })
2092      {
2093          final CompletableFuture<Integer> f = new CompletableFuture<>();
2094          final CompletableFuture<Integer> g = new CompletableFuture<>();
2095 <        final IncFunction r = new IncFunction();
2096 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2222 <
2223 <        assertTrue(g.cancel(mayInterruptIfRunning));
2224 <        checkCompletedWithWrappedCancellationException(h);
2225 <        f.complete(v1);
2226 <
2227 <        checkCancelled(g);
2228 <        assertEquals(0, r.invocationCount);
2229 <        checkCompletedNormally(f, v1);
2230 <        checkCompletedWithWrappedCancellationException(h);
2231 <    }}
2232 <
2233 <    public void testApplyToEither_sourceCancelled3() {
2234 <        for (ExecutionMode m : ExecutionMode.values())
2235 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2236 <        for (Integer v1 : new Integer[] { 1, null })
2237 <    {
2238 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2239 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2240 <        final IncFunction r = new IncFunction();
2095 >        final IncFunction[] rs = new IncFunction[6];
2096 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2097  
2098 <        assertTrue(g.cancel(mayInterruptIfRunning));
2099 <        f.complete(v1);
2100 <        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2098 >        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2099 >        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2100 >        if (fFirst) {
2101 >            f.complete(v1);
2102 >            g.cancel(mayInterruptIfRunning);
2103 >        } else {
2104 >            g.cancel(mayInterruptIfRunning);
2105 >            f.complete(v1);
2106 >        }
2107 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2108 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2109  
2110 <        // unspecified behavior
2247 <        Integer v;
2110 >        // unspecified behavior - both source completions available
2111          try {
2112 <            assertEquals(inc(v1), h.join());
2113 <            assertEquals(1, r.invocationCount);
2112 >            assertEquals(inc(v1), h0.join());
2113 >            rs[0].assertValue(inc(v1));
2114          } catch (CompletionException ok) {
2115 <            checkCompletedWithWrappedCancellationException(h);
2116 <            assertEquals(0, r.invocationCount);
2115 >            checkCompletedWithWrappedCancellationException(h0);
2116 >            rs[0].assertNotInvoked();
2117 >        }
2118 >        try {
2119 >            assertEquals(inc(v1), h1.join());
2120 >            rs[1].assertValue(inc(v1));
2121 >        } catch (CompletionException ok) {
2122 >            checkCompletedWithWrappedCancellationException(h1);
2123 >            rs[1].assertNotInvoked();
2124 >        }
2125 >        try {
2126 >            assertEquals(inc(v1), h2.join());
2127 >            rs[2].assertValue(inc(v1));
2128 >        } catch (CompletionException ok) {
2129 >            checkCompletedWithWrappedCancellationException(h2);
2130 >            rs[2].assertNotInvoked();
2131          }
2255
2256        checkCancelled(g);
2257        checkCompletedNormally(f, v1);
2258    }}
2259
2260    public void testApplyToEither_sourceCancelled4() {
2261        for (ExecutionMode m : ExecutionMode.values())
2262        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2263        for (Integer v1 : new Integer[] { 1, null })
2264    {
2265        final CompletableFuture<Integer> f = new CompletableFuture<>();
2266        final CompletableFuture<Integer> g = new CompletableFuture<>();
2267        final IncFunction r = new IncFunction();
2268
2269        assertTrue(f.cancel(mayInterruptIfRunning));
2270        g.complete(v1);
2271        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2272
2273        // unspecified behavior
2274        Integer v;
2132          try {
2133 <            assertEquals(inc(v1), h.join());
2134 <            assertEquals(1, r.invocationCount);
2133 >            assertEquals(inc(v1), h3.join());
2134 >            rs[3].assertValue(inc(v1));
2135          } catch (CompletionException ok) {
2136 <            checkCompletedWithWrappedCancellationException(h);
2137 <            assertEquals(0, r.invocationCount);
2136 >            checkCompletedWithWrappedCancellationException(h3);
2137 >            rs[3].assertNotInvoked();
2138          }
2139  
2140 <        checkCancelled(f);
2141 <        checkCompletedNormally(g, v1);
2140 >        checkCompletedNormally(f, v1);
2141 >        checkCancelled(g);
2142      }}
2143  
2144      /**
2145 <     * acceptEither result completes normally after normal completion
2289 <     * of either source
2145 >     * applyToEither result completes exceptionally if action does
2146       */
2147 <    public void testAcceptEither_normalCompletion1() {
2147 >    public void testApplyToEither_actionFailed() {
2148          for (ExecutionMode m : ExecutionMode.values())
2149          for (Integer v1 : new Integer[] { 1, null })
2150          for (Integer v2 : new Integer[] { 2, null })
2151      {
2152          final CompletableFuture<Integer> f = new CompletableFuture<>();
2153          final CompletableFuture<Integer> g = new CompletableFuture<>();
2154 <        final IncAction r = new IncAction();
2155 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2154 >        final FailingFunction[] rs = new FailingFunction[6];
2155 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
2156  
2157 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2158 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2159          f.complete(v1);
2160 <        checkCompletedNormally(h, null);
2161 <        assertEquals(inc(v1), r.value);
2162 <        g.complete(v2);
2160 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2161 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2162 >        checkCompletedWithWrappedCFException(h0);
2163 >        checkCompletedWithWrappedCFException(h1);
2164 >        checkCompletedWithWrappedCFException(h2);
2165 >        checkCompletedWithWrappedCFException(h3);
2166 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2167  
2168 <        checkCompletedNormally(f, v1);
2307 <        checkCompletedNormally(g, v2);
2308 <        checkCompletedNormally(h, null);
2309 <    }}
2168 >        g.complete(v2);
2169  
2170 <    public void testAcceptEither_normalCompletion2() {
2171 <        for (ExecutionMode m : ExecutionMode.values())
2172 <        for (Integer v1 : new Integer[] { 1, null })
2314 <        for (Integer v2 : new Integer[] { 2, null })
2315 <    {
2316 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2317 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2318 <        final IncAction r = new IncAction();
2319 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2170 >        // unspecified behavior - both source completions available
2171 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2172 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2173  
2174 <        g.complete(v2);
2175 <        checkCompletedNormally(h, null);
2176 <        assertEquals(inc(v2), r.value);
2177 <        f.complete(v1);
2174 >        checkCompletedWithWrappedCFException(h4);
2175 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2176 >                   Objects.equals(v2, rs[4].value));
2177 >        checkCompletedWithWrappedCFException(h5);
2178 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2179 >                   Objects.equals(v2, rs[5].value));
2180  
2181          checkCompletedNormally(f, v1);
2182          checkCompletedNormally(g, v2);
2328        checkCompletedNormally(h, null);
2183      }}
2184  
2185 <    public void testAcceptEither_normalCompletion3() {
2185 >    /**
2186 >     * acceptEither result completes normally after normal completion
2187 >     * of either source
2188 >     */
2189 >    public void testAcceptEither_normalCompletion() {
2190          for (ExecutionMode m : ExecutionMode.values())
2191          for (Integer v1 : new Integer[] { 1, null })
2192          for (Integer v2 : new Integer[] { 2, null })
2193      {
2194          final CompletableFuture<Integer> f = new CompletableFuture<>();
2195          final CompletableFuture<Integer> g = new CompletableFuture<>();
2196 <        final IncAction r = new IncAction();
2196 >        final NoopConsumer[] rs = new NoopConsumer[6];
2197 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2198  
2199 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2200 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2201 +        checkIncomplete(h0);
2202 +        checkIncomplete(h1);
2203 +        rs[0].assertNotInvoked();
2204 +        rs[1].assertNotInvoked();
2205          f.complete(v1);
2206 +        checkCompletedNormally(h0, null);
2207 +        checkCompletedNormally(h1, null);
2208 +        rs[0].assertValue(v1);
2209 +        rs[1].assertValue(v1);
2210 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2211 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2212 +        checkCompletedNormally(h2, null);
2213 +        checkCompletedNormally(h3, null);
2214 +        rs[2].assertValue(v1);
2215 +        rs[3].assertValue(v1);
2216          g.complete(v2);
2342        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2217  
2218 <        checkCompletedNormally(h, null);
2218 >        // unspecified behavior - both source completions available
2219 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2220 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2221 >        checkCompletedNormally(h4, null);
2222 >        checkCompletedNormally(h5, null);
2223 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2224 >                   Objects.equals(v2, rs[4].value));
2225 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2226 >                   Objects.equals(v2, rs[5].value));
2227 >
2228          checkCompletedNormally(f, v1);
2229          checkCompletedNormally(g, v2);
2230 <
2231 <        // unspecified behavior
2232 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2233 <                   Objects.equals(r.value, inc(v2)));
2230 >        checkCompletedNormally(h0, null);
2231 >        checkCompletedNormally(h1, null);
2232 >        checkCompletedNormally(h2, null);
2233 >        checkCompletedNormally(h3, null);
2234 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2235      }}
2236  
2237      /**
2238       * acceptEither result completes exceptionally after exceptional
2239       * completion of either source
2240       */
2241 <    public void testAcceptEither_exceptionalCompletion1() {
2241 >    public void testAcceptEither_exceptionalCompletion() {
2242          for (ExecutionMode m : ExecutionMode.values())
2243          for (Integer v1 : new Integer[] { 1, null })
2244      {
2245          final CompletableFuture<Integer> f = new CompletableFuture<>();
2246          final CompletableFuture<Integer> g = new CompletableFuture<>();
2363        final IncAction r = new IncAction();
2364        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2247          final CFException ex = new CFException();
2248 +        final NoopConsumer[] rs = new NoopConsumer[6];
2249 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2250  
2251 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2252 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2253 +        checkIncomplete(h0);
2254 +        checkIncomplete(h1);
2255 +        rs[0].assertNotInvoked();
2256 +        rs[1].assertNotInvoked();
2257          f.completeExceptionally(ex);
2258 <        checkCompletedWithWrappedCFException(h, ex);
2258 >        checkCompletedWithWrappedException(h0, ex);
2259 >        checkCompletedWithWrappedException(h1, ex);
2260 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2261 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2262 >        checkCompletedWithWrappedException(h2, ex);
2263 >        checkCompletedWithWrappedException(h3, ex);
2264 >
2265          g.complete(v1);
2266  
2267 <        assertEquals(0, r.invocationCount);
2267 >        // unspecified behavior - both source completions available
2268 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2269 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2270 >        try {
2271 >            assertNull(h4.join());
2272 >            rs[4].assertValue(v1);
2273 >        } catch (CompletionException ok) {
2274 >            checkCompletedWithWrappedException(h4, ex);
2275 >            rs[4].assertNotInvoked();
2276 >        }
2277 >        try {
2278 >            assertNull(h5.join());
2279 >            rs[5].assertValue(v1);
2280 >        } catch (CompletionException ok) {
2281 >            checkCompletedWithWrappedException(h5, ex);
2282 >            rs[5].assertNotInvoked();
2283 >        }
2284 >
2285 >        checkCompletedExceptionally(f, ex);
2286          checkCompletedNormally(g, v1);
2287 <        checkCompletedWithWrappedCFException(f, ex);
2288 <        checkCompletedWithWrappedCFException(h, ex);
2287 >        checkCompletedWithWrappedException(h0, ex);
2288 >        checkCompletedWithWrappedException(h1, ex);
2289 >        checkCompletedWithWrappedException(h2, ex);
2290 >        checkCompletedWithWrappedException(h3, ex);
2291 >        checkCompletedWithWrappedException(h4, ex);
2292 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2293      }}
2294  
2295      public void testAcceptEither_exceptionalCompletion2() {
2296          for (ExecutionMode m : ExecutionMode.values())
2297 +        for (boolean fFirst : new boolean[] { true, false })
2298          for (Integer v1 : new Integer[] { 1, null })
2299      {
2300          final CompletableFuture<Integer> f = new CompletableFuture<>();
2301          final CompletableFuture<Integer> g = new CompletableFuture<>();
2383        final IncAction r = new IncAction();
2384        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2385        final CFException ex = new CFException();
2386
2387        g.completeExceptionally(ex);
2388        checkCompletedWithWrappedCFException(h, ex);
2389        f.complete(v1);
2390
2391        assertEquals(0, r.invocationCount);
2392        checkCompletedNormally(f, v1);
2393        checkCompletedWithWrappedCFException(g, ex);
2394        checkCompletedWithWrappedCFException(h, ex);
2395    }}
2396
2397    public void testAcceptEither_exceptionalCompletion3() {
2398        for (ExecutionMode m : ExecutionMode.values())
2399        for (Integer v1 : new Integer[] { 1, null })
2400    {
2401        final CompletableFuture<Integer> f = new CompletableFuture<>();
2402        final CompletableFuture<Integer> g = new CompletableFuture<>();
2403        final IncAction r = new IncAction();
2302          final CFException ex = new CFException();
2303 +        final NoopConsumer[] rs = new NoopConsumer[6];
2304 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2305  
2306 <        g.completeExceptionally(ex);
2307 <        f.complete(v1);
2308 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2306 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2307 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2308 >        if (fFirst) {
2309 >            f.complete(v1);
2310 >            g.completeExceptionally(ex);
2311 >        } else {
2312 >            g.completeExceptionally(ex);
2313 >            f.complete(v1);
2314 >        }
2315 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2316 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2317  
2318 <        // unspecified behavior
2411 <        Integer v;
2318 >        // unspecified behavior - both source completions available
2319          try {
2320 <            assertNull(h.join());
2321 <            assertEquals(1, r.invocationCount);
2415 <            assertEquals(inc(v1), r.value);
2320 >            assertEquals(null, h0.join());
2321 >            rs[0].assertValue(v1);
2322          } catch (CompletionException ok) {
2323 <            checkCompletedWithWrappedCFException(h, ex);
2324 <            assertEquals(0, r.invocationCount);
2323 >            checkCompletedWithWrappedException(h0, ex);
2324 >            rs[0].assertNotInvoked();
2325          }
2420
2421        checkCompletedWithWrappedCFException(g, ex);
2422        checkCompletedNormally(f, v1);
2423    }}
2424
2425    public void testAcceptEither_exceptionalCompletion4() {
2426        for (ExecutionMode m : ExecutionMode.values())
2427        for (Integer v1 : new Integer[] { 1, null })
2428    {
2429        final CompletableFuture<Integer> f = new CompletableFuture<>();
2430        final CompletableFuture<Integer> g = new CompletableFuture<>();
2431        final IncAction r = new IncAction();
2432        final CFException ex = new CFException();
2433
2434        f.completeExceptionally(ex);
2435        g.complete(v1);
2436        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2437
2438        // unspecified behavior
2439        Integer v;
2326          try {
2327 <            assertNull(h.join());
2328 <            assertEquals(1, r.invocationCount);
2443 <            assertEquals(inc(v1), r.value);
2327 >            assertEquals(null, h1.join());
2328 >            rs[1].assertValue(v1);
2329          } catch (CompletionException ok) {
2330 <            checkCompletedWithWrappedCFException(h, ex);
2331 <            assertEquals(0, r.invocationCount);
2330 >            checkCompletedWithWrappedException(h1, ex);
2331 >            rs[1].assertNotInvoked();
2332 >        }
2333 >        try {
2334 >            assertEquals(null, h2.join());
2335 >            rs[2].assertValue(v1);
2336 >        } catch (CompletionException ok) {
2337 >            checkCompletedWithWrappedException(h2, ex);
2338 >            rs[2].assertNotInvoked();
2339 >        }
2340 >        try {
2341 >            assertEquals(null, h3.join());
2342 >            rs[3].assertValue(v1);
2343 >        } catch (CompletionException ok) {
2344 >            checkCompletedWithWrappedException(h3, ex);
2345 >            rs[3].assertNotInvoked();
2346          }
2347  
2449        checkCompletedWithWrappedCFException(f, ex);
2450        checkCompletedNormally(g, v1);
2451    }}
2452
2453    /**
2454     * acceptEither result completes exceptionally if action does
2455     */
2456    public void testAcceptEither_actionFailed1() {
2457        for (ExecutionMode m : ExecutionMode.values())
2458        for (Integer v1 : new Integer[] { 1, null })
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 FailingConsumer r = new FailingConsumer();
2464        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2465
2466        f.complete(v1);
2467        checkCompletedWithWrappedCFException(h);
2468        g.complete(v2);
2469        checkCompletedNormally(f, v1);
2470        checkCompletedNormally(g, v2);
2471    }}
2472
2473    public void testAcceptEither_actionFailed2() {
2474        for (ExecutionMode m : ExecutionMode.values())
2475        for (Integer v1 : new Integer[] { 1, null })
2476        for (Integer v2 : new Integer[] { 2, null })
2477    {
2478        final CompletableFuture<Integer> f = new CompletableFuture<>();
2479        final CompletableFuture<Integer> g = new CompletableFuture<>();
2480        final FailingConsumer r = new FailingConsumer();
2481        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2482
2483        g.complete(v2);
2484        checkCompletedWithWrappedCFException(h);
2485        f.complete(v1);
2348          checkCompletedNormally(f, v1);
2349 <        checkCompletedNormally(g, v2);
2349 >        checkCompletedExceptionally(g, ex);
2350      }}
2351  
2352      /**
2353       * acceptEither result completes exceptionally if either source cancelled
2354       */
2355 <    public void testAcceptEither_sourceCancelled1() {
2494 <        for (ExecutionMode m : ExecutionMode.values())
2495 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2496 <        for (Integer v1 : new Integer[] { 1, null })
2497 <    {
2498 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2499 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2500 <        final IncAction r = new IncAction();
2501 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2502 <
2503 <        assertTrue(f.cancel(mayInterruptIfRunning));
2504 <        checkCompletedWithWrappedCancellationException(h);
2505 <        g.complete(v1);
2506 <
2507 <        checkCancelled(f);
2508 <        assertEquals(0, r.invocationCount);
2509 <        checkCompletedNormally(g, v1);
2510 <        checkCompletedWithWrappedCancellationException(h);
2511 <    }}
2512 <
2513 <    public void testAcceptEither_sourceCancelled2() {
2355 >    public void testAcceptEither_sourceCancelled() {
2356          for (ExecutionMode m : ExecutionMode.values())
2357          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2358          for (Integer v1 : new Integer[] { 1, null })
2359      {
2360          final CompletableFuture<Integer> f = new CompletableFuture<>();
2361          final CompletableFuture<Integer> g = new CompletableFuture<>();
2362 <        final IncAction r = new IncAction();
2363 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2522 <
2523 <        assertTrue(g.cancel(mayInterruptIfRunning));
2524 <        checkCompletedWithWrappedCancellationException(h);
2525 <        f.complete(v1);
2526 <
2527 <        checkCancelled(g);
2528 <        assertEquals(0, r.invocationCount);
2529 <        checkCompletedNormally(f, v1);
2530 <        checkCompletedWithWrappedCancellationException(h);
2531 <    }}
2362 >        final NoopConsumer[] rs = new NoopConsumer[6];
2363 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2364  
2365 <    public void testAcceptEither_sourceCancelled3() {
2366 <        for (ExecutionMode m : ExecutionMode.values())
2367 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2368 <        for (Integer v1 : new Integer[] { 1, null })
2369 <    {
2370 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2371 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2372 <        final IncAction r = new IncAction();
2365 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2366 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2367 >        checkIncomplete(h0);
2368 >        checkIncomplete(h1);
2369 >        rs[0].assertNotInvoked();
2370 >        rs[1].assertNotInvoked();
2371 >        f.cancel(mayInterruptIfRunning);
2372 >        checkCompletedWithWrappedCancellationException(h0);
2373 >        checkCompletedWithWrappedCancellationException(h1);
2374 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2375 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2376 >        checkCompletedWithWrappedCancellationException(h2);
2377 >        checkCompletedWithWrappedCancellationException(h3);
2378  
2379 <        assertTrue(g.cancel(mayInterruptIfRunning));
2543 <        f.complete(v1);
2544 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2379 >        g.complete(v1);
2380  
2381 <        // unspecified behavior
2382 <        Integer v;
2381 >        // unspecified behavior - both source completions available
2382 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2383 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2384          try {
2385 <            assertNull(h.join());
2386 <            assertEquals(1, r.invocationCount);
2551 <            assertEquals(inc(v1), r.value);
2385 >            assertNull(h4.join());
2386 >            rs[4].assertValue(v1);
2387          } catch (CompletionException ok) {
2388 <            checkCompletedWithWrappedCancellationException(h);
2389 <            assertEquals(0, r.invocationCount);
2388 >            checkCompletedWithWrappedCancellationException(h4);
2389 >            rs[4].assertNotInvoked();
2390          }
2556
2557        checkCancelled(g);
2558        checkCompletedNormally(f, v1);
2559    }}
2560
2561    public void testAcceptEither_sourceCancelled4() {
2562        for (ExecutionMode m : ExecutionMode.values())
2563        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2564        for (Integer v1 : new Integer[] { 1, null })
2565    {
2566        final CompletableFuture<Integer> f = new CompletableFuture<>();
2567        final CompletableFuture<Integer> g = new CompletableFuture<>();
2568        final IncAction r = new IncAction();
2569
2570        assertTrue(f.cancel(mayInterruptIfRunning));
2571        g.complete(v1);
2572        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2573
2574        // unspecified behavior
2575        Integer v;
2391          try {
2392 <            assertNull(h.join());
2393 <            assertEquals(1, r.invocationCount);
2579 <            assertEquals(inc(v1), r.value);
2392 >            assertNull(h5.join());
2393 >            rs[5].assertValue(v1);
2394          } catch (CompletionException ok) {
2395 <            checkCompletedWithWrappedCancellationException(h);
2396 <            assertEquals(0, r.invocationCount);
2395 >            checkCompletedWithWrappedCancellationException(h5);
2396 >            rs[5].assertNotInvoked();
2397          }
2398  
2399          checkCancelled(f);
2400          checkCompletedNormally(g, v1);
2401 +        checkCompletedWithWrappedCancellationException(h0);
2402 +        checkCompletedWithWrappedCancellationException(h1);
2403 +        checkCompletedWithWrappedCancellationException(h2);
2404 +        checkCompletedWithWrappedCancellationException(h3);
2405 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2406      }}
2407  
2408      /**
2409 <     * runAfterEither result completes normally after normal completion
2591 <     * of either source
2409 >     * acceptEither result completes exceptionally if action does
2410       */
2411 <    public void testRunAfterEither_normalCompletion1() {
2411 >    public void testAcceptEither_actionFailed() {
2412          for (ExecutionMode m : ExecutionMode.values())
2413          for (Integer v1 : new Integer[] { 1, null })
2414          for (Integer v2 : new Integer[] { 2, null })
2415      {
2416          final CompletableFuture<Integer> f = new CompletableFuture<>();
2417          final CompletableFuture<Integer> g = new CompletableFuture<>();
2418 <        final Noop r = new Noop();
2419 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2418 >        final FailingConsumer[] rs = new FailingConsumer[6];
2419 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2420  
2421 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2422 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2423          f.complete(v1);
2424 <        checkCompletedNormally(h, null);
2425 <        assertEquals(1, r.invocationCount);
2426 <        g.complete(v2);
2424 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2425 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2426 >        checkCompletedWithWrappedCFException(h0);
2427 >        checkCompletedWithWrappedCFException(h1);
2428 >        checkCompletedWithWrappedCFException(h2);
2429 >        checkCompletedWithWrappedCFException(h3);
2430 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2431  
2432 <        checkCompletedNormally(f, v1);
2609 <        checkCompletedNormally(g, v2);
2610 <        checkCompletedNormally(h, null);
2611 <        assertEquals(1, r.invocationCount);
2612 <    }}
2432 >        g.complete(v2);
2433  
2434 <    public void testRunAfterEither_normalCompletion2() {
2435 <        for (ExecutionMode m : ExecutionMode.values())
2436 <        for (Integer v1 : new Integer[] { 1, null })
2617 <        for (Integer v2 : new Integer[] { 2, null })
2618 <    {
2619 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2620 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2621 <        final Noop r = new Noop();
2622 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2434 >        // unspecified behavior - both source completions available
2435 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2436 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2437  
2438 <        g.complete(v2);
2439 <        checkCompletedNormally(h, null);
2440 <        assertEquals(1, r.invocationCount);
2441 <        f.complete(v1);
2438 >        checkCompletedWithWrappedCFException(h4);
2439 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2440 >                   Objects.equals(v2, rs[4].value));
2441 >        checkCompletedWithWrappedCFException(h5);
2442 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2443 >                   Objects.equals(v2, rs[5].value));
2444  
2445          checkCompletedNormally(f, v1);
2446          checkCompletedNormally(g, v2);
2447 <        checkCompletedNormally(h, null);
2632 <        assertEquals(1, r.invocationCount);
2633 <        }}
2447 >    }}
2448  
2449 <    public void testRunAfterEither_normalCompletion3() {
2449 >    /**
2450 >     * runAfterEither result completes normally after normal completion
2451 >     * of either source
2452 >     */
2453 >    public void testRunAfterEither_normalCompletion() {
2454          for (ExecutionMode m : ExecutionMode.values())
2455          for (Integer v1 : new Integer[] { 1, null })
2456          for (Integer v2 : new Integer[] { 2, null })
2457      {
2458          final CompletableFuture<Integer> f = new CompletableFuture<>();
2459          final CompletableFuture<Integer> g = new CompletableFuture<>();
2460 <        final Noop r = new Noop();
2460 >        final Noop[] rs = new Noop[6];
2461 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2462  
2463 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2464 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2465 +        checkIncomplete(h0);
2466 +        checkIncomplete(h1);
2467 +        rs[0].assertNotInvoked();
2468 +        rs[1].assertNotInvoked();
2469          f.complete(v1);
2470 +        checkCompletedNormally(h0, null);
2471 +        checkCompletedNormally(h1, null);
2472 +        rs[0].assertInvoked();
2473 +        rs[1].assertInvoked();
2474 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2475 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2476 +        checkCompletedNormally(h2, null);
2477 +        checkCompletedNormally(h3, null);
2478 +        rs[2].assertInvoked();
2479 +        rs[3].assertInvoked();
2480 +
2481          g.complete(v2);
2646        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2482  
2483 <        checkCompletedNormally(h, null);
2483 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2484 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2485 >
2486          checkCompletedNormally(f, v1);
2487          checkCompletedNormally(g, v2);
2488 <        assertEquals(1, r.invocationCount);
2488 >        checkCompletedNormally(h0, null);
2489 >        checkCompletedNormally(h1, null);
2490 >        checkCompletedNormally(h2, null);
2491 >        checkCompletedNormally(h3, null);
2492 >        checkCompletedNormally(h4, null);
2493 >        checkCompletedNormally(h5, null);
2494 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2495      }}
2496  
2497      /**
2498       * runAfterEither result completes exceptionally after exceptional
2499       * completion of either source
2500       */
2501 <    public void testRunAfterEither_exceptionalCompletion1() {
2501 >    public void testRunAfterEither_exceptionalCompletion() {
2502          for (ExecutionMode m : ExecutionMode.values())
2503          for (Integer v1 : new Integer[] { 1, null })
2504      {
2505          final CompletableFuture<Integer> f = new CompletableFuture<>();
2506          final CompletableFuture<Integer> g = new CompletableFuture<>();
2664        final Noop r = new Noop();
2665        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2507          final CFException ex = new CFException();
2508 +        final Noop[] rs = new Noop[6];
2509 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2510  
2511 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2512 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2513 +        checkIncomplete(h0);
2514 +        checkIncomplete(h1);
2515 +        rs[0].assertNotInvoked();
2516 +        rs[1].assertNotInvoked();
2517          f.completeExceptionally(ex);
2518 <        checkCompletedWithWrappedCFException(h, ex);
2518 >        checkCompletedWithWrappedException(h0, ex);
2519 >        checkCompletedWithWrappedException(h1, ex);
2520 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2521 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2522 >        checkCompletedWithWrappedException(h2, ex);
2523 >        checkCompletedWithWrappedException(h3, ex);
2524 >
2525          g.complete(v1);
2526  
2527 <        assertEquals(0, r.invocationCount);
2527 >        // unspecified behavior - both source completions available
2528 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2529 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2530 >        try {
2531 >            assertNull(h4.join());
2532 >            rs[4].assertInvoked();
2533 >        } catch (CompletionException ok) {
2534 >            checkCompletedWithWrappedException(h4, ex);
2535 >            rs[4].assertNotInvoked();
2536 >        }
2537 >        try {
2538 >            assertNull(h5.join());
2539 >            rs[5].assertInvoked();
2540 >        } catch (CompletionException ok) {
2541 >            checkCompletedWithWrappedException(h5, ex);
2542 >            rs[5].assertNotInvoked();
2543 >        }
2544 >
2545 >        checkCompletedExceptionally(f, ex);
2546          checkCompletedNormally(g, v1);
2547 <        checkCompletedWithWrappedCFException(f, ex);
2548 <        checkCompletedWithWrappedCFException(h, ex);
2547 >        checkCompletedWithWrappedException(h0, ex);
2548 >        checkCompletedWithWrappedException(h1, ex);
2549 >        checkCompletedWithWrappedException(h2, ex);
2550 >        checkCompletedWithWrappedException(h3, ex);
2551 >        checkCompletedWithWrappedException(h4, ex);
2552 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2553      }}
2554  
2555      public void testRunAfterEither_exceptionalCompletion2() {
2556          for (ExecutionMode m : ExecutionMode.values())
2557 +        for (boolean fFirst : new boolean[] { true, false })
2558          for (Integer v1 : new Integer[] { 1, null })
2559      {
2560          final CompletableFuture<Integer> f = new CompletableFuture<>();
2561          final CompletableFuture<Integer> g = new CompletableFuture<>();
2684        final Noop r = new Noop();
2685        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2686        final CFException ex = new CFException();
2687
2688        g.completeExceptionally(ex);
2689        checkCompletedWithWrappedCFException(h, ex);
2690        f.complete(v1);
2691
2692        assertEquals(0, r.invocationCount);
2693        checkCompletedNormally(f, v1);
2694        checkCompletedWithWrappedCFException(g, ex);
2695        checkCompletedWithWrappedCFException(h, ex);
2696    }}
2697
2698    public void testRunAfterEither_exceptionalCompletion3() {
2699        for (ExecutionMode m : ExecutionMode.values())
2700        for (Integer v1 : new Integer[] { 1, null })
2701    {
2702        final CompletableFuture<Integer> f = new CompletableFuture<>();
2703        final CompletableFuture<Integer> g = new CompletableFuture<>();
2704        final Noop r = new Noop();
2562          final CFException ex = new CFException();
2563 +        final Noop[] rs = new Noop[6];
2564 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2565  
2566 <        g.completeExceptionally(ex);
2567 <        f.complete(v1);
2568 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2566 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2567 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2568 >        if (fFirst) {
2569 >            f.complete(v1);
2570 >            g.completeExceptionally(ex);
2571 >        } else {
2572 >            g.completeExceptionally(ex);
2573 >            f.complete(v1);
2574 >        }
2575 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2576 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2577  
2578 <        // unspecified behavior
2579 <        Integer v;
2578 >        // unspecified behavior - both source completions available
2579 >        try {
2580 >            assertEquals(null, h0.join());
2581 >            rs[0].assertInvoked();
2582 >        } catch (CompletionException ok) {
2583 >            checkCompletedWithWrappedException(h0, ex);
2584 >            rs[0].assertNotInvoked();
2585 >        }
2586 >        try {
2587 >            assertEquals(null, h1.join());
2588 >            rs[1].assertInvoked();
2589 >        } catch (CompletionException ok) {
2590 >            checkCompletedWithWrappedException(h1, ex);
2591 >            rs[1].assertNotInvoked();
2592 >        }
2593          try {
2594 <            assertNull(h.join());
2595 <            assertEquals(1, r.invocationCount);
2594 >            assertEquals(null, h2.join());
2595 >            rs[2].assertInvoked();
2596          } catch (CompletionException ok) {
2597 <            checkCompletedWithWrappedCFException(h, ex);
2598 <            assertEquals(0, r.invocationCount);
2597 >            checkCompletedWithWrappedException(h2, ex);
2598 >            rs[2].assertNotInvoked();
2599 >        }
2600 >        try {
2601 >            assertEquals(null, h3.join());
2602 >            rs[3].assertInvoked();
2603 >        } catch (CompletionException ok) {
2604 >            checkCompletedWithWrappedException(h3, ex);
2605 >            rs[3].assertNotInvoked();
2606          }
2607  
2721        checkCompletedWithWrappedCFException(g, ex);
2608          checkCompletedNormally(f, v1);
2609 +        checkCompletedExceptionally(g, ex);
2610      }}
2611  
2612 <    public void testRunAfterEither_exceptionalCompletion4() {
2612 >    /**
2613 >     * runAfterEither result completes exceptionally if either source cancelled
2614 >     */
2615 >    public void testRunAfterEither_sourceCancelled() {
2616          for (ExecutionMode m : ExecutionMode.values())
2617 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2618          for (Integer v1 : new Integer[] { 1, null })
2619      {
2620          final CompletableFuture<Integer> f = new CompletableFuture<>();
2621          final CompletableFuture<Integer> g = new CompletableFuture<>();
2622 <        final Noop r = new Noop();
2623 <        final CFException ex = new CFException();
2622 >        final Noop[] rs = new Noop[6];
2623 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2624 >
2625 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2626 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2627 >        checkIncomplete(h0);
2628 >        checkIncomplete(h1);
2629 >        rs[0].assertNotInvoked();
2630 >        rs[1].assertNotInvoked();
2631 >        f.cancel(mayInterruptIfRunning);
2632 >        checkCompletedWithWrappedCancellationException(h0);
2633 >        checkCompletedWithWrappedCancellationException(h1);
2634 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2635 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2636 >        checkCompletedWithWrappedCancellationException(h2);
2637 >        checkCompletedWithWrappedCancellationException(h3);
2638  
2734        f.completeExceptionally(ex);
2639          g.complete(v1);
2736        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2640  
2641 <        // unspecified behavior
2642 <        Integer v;
2641 >        // unspecified behavior - both source completions available
2642 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2643 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2644 >        try {
2645 >            assertNull(h4.join());
2646 >            rs[4].assertInvoked();
2647 >        } catch (CompletionException ok) {
2648 >            checkCompletedWithWrappedCancellationException(h4);
2649 >            rs[4].assertNotInvoked();
2650 >        }
2651          try {
2652 <            assertNull(h.join());
2653 <            assertEquals(1, r.invocationCount);
2652 >            assertNull(h5.join());
2653 >            rs[5].assertInvoked();
2654          } catch (CompletionException ok) {
2655 <            checkCompletedWithWrappedCFException(h, ex);
2656 <            assertEquals(0, r.invocationCount);
2655 >            checkCompletedWithWrappedCancellationException(h5);
2656 >            rs[5].assertNotInvoked();
2657          }
2658  
2659 <        checkCompletedWithWrappedCFException(f, ex);
2659 >        checkCancelled(f);
2660          checkCompletedNormally(g, v1);
2661 +        checkCompletedWithWrappedCancellationException(h0);
2662 +        checkCompletedWithWrappedCancellationException(h1);
2663 +        checkCompletedWithWrappedCancellationException(h2);
2664 +        checkCompletedWithWrappedCancellationException(h3);
2665 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2666      }}
2667  
2668      /**
2669       * runAfterEither result completes exceptionally if action does
2670       */
2671 <    public void testRunAfterEither_actionFailed1() {
2671 >    public void testRunAfterEither_actionFailed() {
2672          for (ExecutionMode m : ExecutionMode.values())
2673          for (Integer v1 : new Integer[] { 1, null })
2674          for (Integer v2 : new Integer[] { 2, null })
2675      {
2676          final CompletableFuture<Integer> f = new CompletableFuture<>();
2677          final CompletableFuture<Integer> g = new CompletableFuture<>();
2678 <        final FailingNoop r = new FailingNoop();
2679 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2678 >        final FailingRunnable[] rs = new FailingRunnable[6];
2679 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2680  
2681 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2682 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2683          f.complete(v1);
2684 <        checkCompletedWithWrappedCFException(h);
2684 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2685 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2686 >        checkCompletedWithWrappedCFException(h0);
2687 >        checkCompletedWithWrappedCFException(h1);
2688 >        checkCompletedWithWrappedCFException(h2);
2689 >        checkCompletedWithWrappedCFException(h3);
2690 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2691          g.complete(v2);
2692 <        checkCompletedNormally(f, v1);
2693 <        checkCompletedNormally(g, v2);
2694 <    }}
2695 <
2772 <    public void testRunAfterEither_actionFailed2() {
2773 <        for (ExecutionMode m : ExecutionMode.values())
2774 <        for (Integer v1 : new Integer[] { 1, null })
2775 <        for (Integer v2 : new Integer[] { 2, null })
2776 <    {
2777 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2778 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2779 <        final FailingNoop r = new FailingNoop();
2780 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2692 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2693 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2694 >        checkCompletedWithWrappedCFException(h4);
2695 >        checkCompletedWithWrappedCFException(h5);
2696  
2782        g.complete(v2);
2783        checkCompletedWithWrappedCFException(h);
2784        f.complete(v1);
2697          checkCompletedNormally(f, v1);
2698          checkCompletedNormally(g, v2);
2699 <    }}
2788 <
2789 <    /**
2790 <     * runAfterEither result completes exceptionally if either source cancelled
2791 <     */
2792 <    public void testRunAfterEither_sourceCancelled1() {
2793 <        for (ExecutionMode m : ExecutionMode.values())
2794 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2795 <        for (Integer v1 : new Integer[] { 1, null })
2796 <    {
2797 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2798 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2799 <        final Noop r = new Noop();
2800 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2801 <
2802 <        assertTrue(f.cancel(mayInterruptIfRunning));
2803 <        checkCompletedWithWrappedCancellationException(h);
2804 <        g.complete(v1);
2805 <
2806 <        checkCancelled(f);
2807 <        assertEquals(0, r.invocationCount);
2808 <        checkCompletedNormally(g, v1);
2809 <        checkCompletedWithWrappedCancellationException(h);
2810 <    }}
2811 <
2812 <    public void testRunAfterEither_sourceCancelled2() {
2813 <        for (ExecutionMode m : ExecutionMode.values())
2814 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2815 <        for (Integer v1 : new Integer[] { 1, null })
2816 <    {
2817 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2818 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2819 <        final Noop r = new Noop();
2820 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2821 <
2822 <        assertTrue(g.cancel(mayInterruptIfRunning));
2823 <        checkCompletedWithWrappedCancellationException(h);
2824 <        f.complete(v1);
2825 <
2826 <        checkCancelled(g);
2827 <        assertEquals(0, r.invocationCount);
2828 <        checkCompletedNormally(f, v1);
2829 <        checkCompletedWithWrappedCancellationException(h);
2830 <    }}
2831 <
2832 <    public void testRunAfterEither_sourceCancelled3() {
2833 <        for (ExecutionMode m : ExecutionMode.values())
2834 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2835 <        for (Integer v1 : new Integer[] { 1, null })
2836 <    {
2837 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2838 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2839 <        final Noop r = new Noop();
2840 <
2841 <        assertTrue(g.cancel(mayInterruptIfRunning));
2842 <        f.complete(v1);
2843 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2844 <
2845 <        // unspecified behavior
2846 <        Integer v;
2847 <        try {
2848 <            assertNull(h.join());
2849 <            assertEquals(1, r.invocationCount);
2850 <        } catch (CompletionException ok) {
2851 <            checkCompletedWithWrappedCancellationException(h);
2852 <            assertEquals(0, r.invocationCount);
2853 <        }
2854 <
2855 <        checkCancelled(g);
2856 <        checkCompletedNormally(f, v1);
2857 <    }}
2858 <
2859 <    public void testRunAfterEither_sourceCancelled4() {
2860 <        for (ExecutionMode m : ExecutionMode.values())
2861 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2862 <        for (Integer v1 : new Integer[] { 1, null })
2863 <    {
2864 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2865 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2866 <        final Noop r = new Noop();
2867 <
2868 <        assertTrue(f.cancel(mayInterruptIfRunning));
2869 <        g.complete(v1);
2870 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2871 <
2872 <        // unspecified behavior
2873 <        Integer v;
2874 <        try {
2875 <            assertNull(h.join());
2876 <            assertEquals(1, r.invocationCount);
2877 <        } catch (CompletionException ok) {
2878 <            checkCompletedWithWrappedCancellationException(h);
2879 <            assertEquals(0, r.invocationCount);
2880 <        }
2881 <
2882 <        checkCancelled(f);
2883 <        checkCompletedNormally(g, v1);
2699 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2700      }}
2701  
2702      /**
# Line 2892 | Line 2708 | public class CompletableFutureTest exten
2708          for (Integer v1 : new Integer[] { 1, null })
2709      {
2710          final CompletableFuture<Integer> f = new CompletableFuture<>();
2711 <        final CompletableFutureInc r = new CompletableFutureInc();
2711 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2712          if (!createIncomplete) f.complete(v1);
2713 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2713 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2714          if (createIncomplete) f.complete(v1);
2715  
2716          checkCompletedNormally(g, inc(v1));
2717          checkCompletedNormally(f, v1);
2718 <        assertEquals(1, r.invocationCount);
2718 >        r.assertValue(v1);
2719      }}
2720  
2721      /**
# Line 2911 | Line 2727 | public class CompletableFutureTest exten
2727          for (boolean createIncomplete : new boolean[] { true, false })
2728      {
2729          final CFException ex = new CFException();
2730 <        final CompletableFutureInc r = new CompletableFutureInc();
2730 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2731          final CompletableFuture<Integer> f = new CompletableFuture<>();
2732          if (!createIncomplete) f.completeExceptionally(ex);
2733 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2733 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2734          if (createIncomplete) f.completeExceptionally(ex);
2735  
2736 <        checkCompletedWithWrappedCFException(g, ex);
2737 <        checkCompletedWithWrappedCFException(f, ex);
2738 <        assertEquals(0, r.invocationCount);
2736 >        checkCompletedWithWrappedException(g, ex);
2737 >        checkCompletedExceptionally(f, ex);
2738 >        r.assertNotInvoked();
2739      }}
2740  
2741      /**
# Line 2932 | Line 2748 | public class CompletableFutureTest exten
2748      {
2749          final CompletableFuture<Integer> f = new CompletableFuture<>();
2750          final FailingCompletableFutureFunction r
2751 <            = new FailingCompletableFutureFunction();
2751 >            = new FailingCompletableFutureFunction(m);
2752          if (!createIncomplete) f.complete(v1);
2753 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2753 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2754          if (createIncomplete) f.complete(v1);
2755  
2756          checkCompletedWithWrappedCFException(g);
# Line 2950 | Line 2766 | public class CompletableFutureTest exten
2766          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2767      {
2768          final CompletableFuture<Integer> f = new CompletableFuture<>();
2769 <        final CompletableFutureInc r = new CompletableFutureInc();
2769 >        final CompletableFutureInc r = new CompletableFutureInc(m);
2770          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2771 <        final CompletableFuture<Integer> g = f.thenCompose(r);
2772 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2771 >        final CompletableFuture<Integer> g = m.thenCompose(f, r);
2772 >        if (createIncomplete) {
2773 >            checkIncomplete(g);
2774 >            assertTrue(f.cancel(mayInterruptIfRunning));
2775 >        }
2776  
2777          checkCompletedWithWrappedCancellationException(g);
2778          checkCancelled(f);
2779      }}
2780  
2962    // asyncs
2963
2964    /**
2965     * thenAcceptAsync result completes normally after normal
2966     * completion of source
2967     */
2968    public void testThenAcceptAsync() {
2969        CompletableFuture<Integer> f = new CompletableFuture<>();
2970        IncAction r = new IncAction();
2971        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2972        f.complete(one);
2973        checkCompletedNormally(g, null);
2974        assertEquals(r.value, (Integer) 2);
2975    }
2976
2977    /**
2978     * thenAcceptAsync result completes exceptionally after exceptional
2979     * completion of source
2980     */
2981    public void testThenAcceptAsync2() {
2982        CompletableFuture<Integer> f = new CompletableFuture<>();
2983        IncAction r = new IncAction();
2984        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2985        f.completeExceptionally(new CFException());
2986        checkCompletedWithWrappedCFException(g);
2987    }
2988
2989    /**
2990     * thenAcceptAsync result completes exceptionally if action does
2991     */
2992    public void testThenAcceptAsync3() {
2993        CompletableFuture<Integer> f = new CompletableFuture<>();
2994        FailingConsumer r = new FailingConsumer();
2995        CompletableFuture<Void> g = f.thenAcceptAsync(r);
2996        f.complete(null);
2997        checkCompletedWithWrappedCFException(g);
2998    }
2999
3000    /**
3001     * thenAcceptAsync result completes exceptionally if source cancelled
3002     */
3003    public void testThenAcceptAsync4() {
3004        CompletableFuture<Integer> f = new CompletableFuture<>();
3005        IncAction r = new IncAction();
3006        CompletableFuture<Void> g = f.thenAcceptAsync(r);
3007        assertTrue(f.cancel(true));
3008        checkCompletedWithWrappedCancellationException(g);
3009    }
3010
3011    // async with explicit executors
3012
3013    /**
3014     * thenAcceptAsync result completes normally after normal
3015     * completion of source
3016     */
3017    public void testThenAcceptAsyncE() {
3018        CompletableFuture<Integer> f = new CompletableFuture<>();
3019        IncAction r = new IncAction();
3020        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3021        f.complete(one);
3022        checkCompletedNormally(g, null);
3023        assertEquals(r.value, (Integer) 2);
3024    }
3025
3026    /**
3027     * thenAcceptAsync result completes exceptionally after exceptional
3028     * completion of source
3029     */
3030    public void testThenAcceptAsync2E() {
3031        CompletableFuture<Integer> f = new CompletableFuture<>();
3032        IncAction r = new IncAction();
3033        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3034        f.completeExceptionally(new CFException());
3035        checkCompletedWithWrappedCFException(g);
3036    }
3037
3038    /**
3039     * thenAcceptAsync result completes exceptionally if action does
3040     */
3041    public void testThenAcceptAsync3E() {
3042        CompletableFuture<Integer> f = new CompletableFuture<>();
3043        FailingConsumer r = new FailingConsumer();
3044        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3045        f.complete(null);
3046        checkCompletedWithWrappedCFException(g);
3047    }
3048
3049    /**
3050     * thenAcceptAsync result completes exceptionally if source cancelled
3051     */
3052    public void testThenAcceptAsync4E() {
3053        CompletableFuture<Integer> f = new CompletableFuture<>();
3054        IncAction r = new IncAction();
3055        CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor());
3056        assertTrue(f.cancel(true));
3057        checkCompletedWithWrappedCancellationException(g);
3058    }
3059
2781      // other static methods
2782  
2783      /**
# Line 3074 | Line 2795 | public class CompletableFutureTest exten
2795       */
2796      public void testAllOf_normal() throws Exception {
2797          for (int k = 1; k < 20; ++k) {
2798 <            CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2798 >            CompletableFuture<Integer>[] fs
2799 >                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2800              for (int i = 0; i < k; ++i)
2801                  fs[i] = new CompletableFuture<>();
2802              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
# Line 3088 | Line 2810 | public class CompletableFutureTest exten
2810          }
2811      }
2812  
2813 +    public void testAllOf_backwards() throws Exception {
2814 +        for (int k = 1; k < 20; ++k) {
2815 +            CompletableFuture<Integer>[] fs
2816 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
2817 +            for (int i = 0; i < k; ++i)
2818 +                fs[i] = new CompletableFuture<>();
2819 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2820 +            for (int i = k - 1; i >= 0; i--) {
2821 +                checkIncomplete(f);
2822 +                checkIncomplete(CompletableFuture.allOf(fs));
2823 +                fs[i].complete(one);
2824 +            }
2825 +            checkCompletedNormally(f, null);
2826 +            checkCompletedNormally(CompletableFuture.allOf(fs), null);
2827 +        }
2828 +    }
2829 +
2830      /**
2831       * anyOf(no component futures) returns an incomplete future
2832       */
# Line 3146 | Line 2885 | public class CompletableFutureTest exten
2885          Runnable[] throwingActions = {
2886              () -> CompletableFuture.supplyAsync(null),
2887              () -> CompletableFuture.supplyAsync(null, exec),
2888 <            () -> CompletableFuture.supplyAsync(supplyOne, null),
2888 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
2889  
2890              () -> CompletableFuture.runAsync(null),
2891              () -> CompletableFuture.runAsync(null, exec),
# Line 3219 | Line 2958 | public class CompletableFutureTest exten
2958  
2959              () -> f.thenCompose(null),
2960              () -> f.thenComposeAsync(null),
2961 <            () -> f.thenComposeAsync(new CompletableFutureInc(), null),
2961 >            () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null),
2962              () -> f.thenComposeAsync(null, exec),
2963  
2964              () -> f.exceptionally(null),
# Line 3251 | Line 2990 | public class CompletableFutureTest exten
2990          assertSame(f, f.toCompletableFuture());
2991      }
2992  
3254    /**
3255     * whenComplete action executes on normal completion, propagating
3256     * source result.
3257     */
3258    public void testWhenComplete_normalCompletion1() {
3259        for (ExecutionMode m : ExecutionMode.values())
3260        for (boolean createIncomplete : new boolean[] { true, false })
3261        for (Integer v1 : new Integer[] { 1, null })
3262    {
3263        final AtomicInteger a = new AtomicInteger(0);
3264        final CompletableFuture<Integer> f = new CompletableFuture<>();
3265        if (!createIncomplete) f.complete(v1);
3266        final CompletableFuture<Integer> g = m.whenComplete
3267            (f,
3268             (Integer x, Throwable t) -> {
3269                threadAssertSame(x, v1);
3270                threadAssertNull(t);
3271                a.getAndIncrement();
3272            });
3273        if (createIncomplete) f.complete(v1);
3274
3275        checkCompletedNormally(g, v1);
3276        checkCompletedNormally(f, v1);
3277        assertEquals(1, a.get());
3278    }}
3279
3280    /**
3281     * whenComplete action executes on exceptional completion, propagating
3282     * source result.
3283     */
3284    public void testWhenComplete_exceptionalCompletion() {
3285        for (ExecutionMode m : ExecutionMode.values())
3286        for (boolean createIncomplete : new boolean[] { true, false })
3287        for (Integer v1 : new Integer[] { 1, null })
3288    {
3289        final AtomicInteger a = new AtomicInteger(0);
3290        final CFException ex = new CFException();
3291        final CompletableFuture<Integer> f = new CompletableFuture<>();
3292        if (!createIncomplete) f.completeExceptionally(ex);
3293        final CompletableFuture<Integer> g = m.whenComplete
3294            (f,
3295             (Integer x, Throwable t) -> {
3296                threadAssertNull(x);
3297                threadAssertSame(t, ex);
3298                a.getAndIncrement();
3299            });
3300        if (createIncomplete) f.completeExceptionally(ex);
3301        checkCompletedWithWrappedCFException(f, ex);
3302        checkCompletedWithWrappedCFException(g, ex);
3303        assertEquals(1, a.get());
3304    }}
3305
3306    /**
3307     * whenComplete action executes on cancelled source, propagating
3308     * CancellationException.
3309     */
3310    public void testWhenComplete_sourceCancelled() {
3311        for (ExecutionMode m : ExecutionMode.values())
3312        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3313        for (boolean createIncomplete : new boolean[] { true, false })
3314    {
3315        final AtomicInteger a = new AtomicInteger(0);
3316        final CompletableFuture<Integer> f = new CompletableFuture<>();
3317        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3318        final CompletableFuture<Integer> g = m.whenComplete
3319            (f,
3320             (Integer x, Throwable t) -> {
3321                threadAssertNull(x);
3322                threadAssertTrue(t instanceof CancellationException);
3323                a.getAndIncrement();
3324            });
3325        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3326
3327        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3328        checkCompletedWithWrappedCancellationException(g);
3329        checkCancelled(f);
3330        assertEquals(1, a.get());
3331    }}
3332
3333    /**
3334     * If a whenComplete action throws an exception when triggered by
3335     * a normal completion, it completes exceptionally
3336     */
3337    public void testWhenComplete_actionFailed() {
3338        for (boolean createIncomplete : new boolean[] { true, false })
3339        for (ExecutionMode m : ExecutionMode.values())
3340        for (Integer v1 : new Integer[] { 1, null })
3341    {
3342        final AtomicInteger a = new AtomicInteger(0);
3343        final CFException ex = new CFException();
3344        final CompletableFuture<Integer> f = new CompletableFuture<>();
3345        if (!createIncomplete) f.complete(v1);
3346        final CompletableFuture<Integer> g = m.whenComplete
3347            (f,
3348             (Integer x, Throwable t) -> {
3349                threadAssertSame(x, v1);
3350                threadAssertNull(t);
3351                a.getAndIncrement();
3352                throw ex;
3353            });
3354        if (createIncomplete) f.complete(v1);
3355        checkCompletedNormally(f, v1);
3356        checkCompletedWithWrappedCFException(g, ex);
3357        assertEquals(1, a.get());
3358    }}
3359
3360    /**
3361     * If a whenComplete action throws an exception when triggered by
3362     * a source completion that also throws an exception, the source
3363     * exception takes precedence.
3364     */
3365    public void testWhenComplete_actionFailedSourceFailed() {
3366        for (boolean createIncomplete : new boolean[] { true, false })
3367        for (ExecutionMode m : ExecutionMode.values())
3368        for (Integer v1 : new Integer[] { 1, null })
3369    {
3370        final AtomicInteger a = new AtomicInteger(0);
3371        final CFException ex1 = new CFException();
3372        final CFException ex2 = new CFException();
3373        final CompletableFuture<Integer> f = new CompletableFuture<>();
3374
3375        if (!createIncomplete) f.completeExceptionally(ex1);
3376        final CompletableFuture<Integer> g = m.whenComplete
3377            (f,
3378             (Integer x, Throwable t) -> {
3379                threadAssertSame(t, ex1);
3380                threadAssertNull(x);
3381                a.getAndIncrement();
3382                throw ex2;
3383            });
3384        if (createIncomplete) f.completeExceptionally(ex1);
3385
3386        checkCompletedWithWrappedCFException(f, ex1);
3387        checkCompletedWithWrappedCFException(g, ex1);
3388        assertEquals(1, a.get());
3389    }}
3390
2993   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines