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.63 by jsr166, Fri Jun 6 16:54:16 2014 UTC vs.
Revision 1.100 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.Objects;
14   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
15   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
16   import java.util.concurrent.CompletableFuture;
17   import java.util.concurrent.CompletionException;
18   import java.util.concurrent.CompletionStage;
19 + import java.util.concurrent.ExecutionException;
20 + import java.util.concurrent.Executor;
21   import java.util.concurrent.ForkJoinPool;
22   import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24   import java.util.concurrent.atomic.AtomicInteger;
24 import static java.util.concurrent.TimeUnit.MILLISECONDS;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import java.util.*;
27 import java.util.function.Supplier;
28 import java.util.function.Consumer;
25   import java.util.function.BiConsumer;
30 import java.util.function.Function;
26   import java.util.function.BiFunction;
27 + import java.util.function.Consumer;
28 + import java.util.function.Function;
29 + import java.util.function.Supplier;
30 +
31 + import junit.framework.Test;
32 + import junit.framework.TestSuite;
33  
34   public class CompletableFutureTest extends JSR166TestCase {
35  
# Line 57 | Line 58 | public class CompletableFutureTest exten
58      }
59  
60      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
61 <        try {
62 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
61 >        checkTimedGet(f, value);
62 >
63          try {
64              assertEquals(value, f.join());
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 76 | public class CompletableFutureTest exten
76      }
77  
78      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
79 +        long startTime = System.nanoTime();
80 +        long timeoutMillis = LONG_DELAY_MS;
81          try {
82 <            f.get(LONG_DELAY_MS, MILLISECONDS);
82 >            f.get(timeoutMillis, MILLISECONDS);
83              shouldThrow();
84          } catch (ExecutionException success) {
85              assertTrue(success.getCause() instanceof CFException);
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
88 +
89          try {
90              f.join();
91              shouldThrow();
# Line 105 | Line 109 | public class CompletableFutureTest exten
109          assertTrue(f.toString().contains("[Completed exceptionally]"));
110      }
111  
112 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
113 <                                              CFException ex) {
112 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
113 >                                                      Throwable ex) {
114 >        long startTime = System.nanoTime();
115 >        long timeoutMillis = LONG_DELAY_MS;
116          try {
117 <            f.get(LONG_DELAY_MS, MILLISECONDS);
117 >            f.get(timeoutMillis, MILLISECONDS);
118              shouldThrow();
119          } catch (ExecutionException success) {
120              assertSame(ex, success.getCause());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
123 +
124          try {
125              f.join();
126              shouldThrow();
# Line 131 | Line 139 | public class CompletableFutureTest exten
139          } catch (ExecutionException success) {
140              assertSame(ex, success.getCause());
141          } catch (Throwable fail) { threadUnexpectedException(fail); }
142 +
143          assertTrue(f.isDone());
144          assertFalse(f.isCancelled());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
149 +                                                Throwable ex) {
150 +        checkCompletedExceptionallyWithRootCause(f, ex);
151 +        try {
152 +            CompletableFuture<Throwable> spy = f.handle
153 +                ((U u, Throwable t) -> t);
154 +            assertTrue(spy.join() instanceof CompletionException);
155 +            assertSame(ex, spy.join().getCause());
156 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
157 +    }
158 +
159 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
160 +        checkCompletedExceptionallyWithRootCause(f, ex);
161 +        try {
162 +            CompletableFuture<Throwable> spy = f.handle
163 +                ((U u, Throwable t) -> t);
164 +            assertSame(ex, spy.join());
165 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
166 +    }
167 +
168      void checkCancelled(CompletableFuture<?> f) {
169 +        long startTime = System.nanoTime();
170 +        long timeoutMillis = LONG_DELAY_MS;
171          try {
172 <            f.get(LONG_DELAY_MS, MILLISECONDS);
172 >            f.get(timeoutMillis, MILLISECONDS);
173              shouldThrow();
174          } catch (CancellationException success) {
175          } catch (Throwable fail) { threadUnexpectedException(fail); }
176 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
177 +
178          try {
179              f.join();
180              shouldThrow();
# Line 162 | Line 195 | public class CompletableFutureTest exten
195      }
196  
197      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
198 +        long startTime = System.nanoTime();
199 +        long timeoutMillis = LONG_DELAY_MS;
200          try {
201 <            f.get(LONG_DELAY_MS, MILLISECONDS);
201 >            f.get(timeoutMillis, MILLISECONDS);
202              shouldThrow();
203          } catch (ExecutionException success) {
204              assertTrue(success.getCause() instanceof CancellationException);
205          } catch (Throwable fail) { threadUnexpectedException(fail); }
206 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
207 +
208          try {
209              f.join();
210              shouldThrow();
# Line 206 | Line 243 | public class CompletableFutureTest exten
243       * isCancelled, join, get, and getNow
244       */
245      public void testComplete() {
246 +        for (Integer v1 : new Integer[] { 1, null })
247 +    {
248          CompletableFuture<Integer> f = new CompletableFuture<>();
249          checkIncomplete(f);
250 <        f.complete(one);
251 <        checkCompletedNormally(f, one);
252 <    }
250 >        assertTrue(f.complete(v1));
251 >        assertFalse(f.complete(v1));
252 >        checkCompletedNormally(f, v1);
253 >    }}
254  
255      /**
256       * completeExceptionally completes exceptionally, as indicated by
# Line 218 | Line 258 | public class CompletableFutureTest exten
258       */
259      public void testCompleteExceptionally() {
260          CompletableFuture<Integer> f = new CompletableFuture<>();
261 +        CFException ex = new CFException();
262          checkIncomplete(f);
263 <        f.completeExceptionally(new CFException());
264 <        checkCompletedWithWrappedCFException(f);
263 >        f.completeExceptionally(ex);
264 >        checkCompletedExceptionally(f, ex);
265      }
266  
267      /**
# Line 228 | Line 269 | public class CompletableFutureTest exten
269       * methods isDone, isCancelled, join, get, and getNow
270       */
271      public void testCancel() {
272 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
273 +    {
274          CompletableFuture<Integer> f = new CompletableFuture<>();
275          checkIncomplete(f);
276          assertTrue(f.cancel(true));
277 +        assertTrue(f.cancel(true));
278          checkCancelled(f);
279 <    }
279 >    }}
280  
281      /**
282       * obtrudeValue forces completion with given value
# Line 240 | Line 284 | public class CompletableFutureTest exten
284      public void testObtrudeValue() {
285          CompletableFuture<Integer> f = new CompletableFuture<>();
286          checkIncomplete(f);
287 <        f.complete(one);
287 >        assertTrue(f.complete(one));
288          checkCompletedNormally(f, one);
289          f.obtrudeValue(three);
290          checkCompletedNormally(f, three);
# Line 261 | Line 305 | public class CompletableFutureTest exten
305       * obtrudeException forces completion with given exception
306       */
307      public void testObtrudeException() {
308 <        CompletableFuture<Integer> f = new CompletableFuture<>();
309 <        checkIncomplete(f);
310 <        f.complete(one);
311 <        checkCompletedNormally(f, one);
312 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
308 >        for (Integer v1 : new Integer[] { 1, null })
309 >    {
310 >        CFException ex;
311 >        CompletableFuture<Integer> f;
312 >
313          f = new CompletableFuture<>();
314 <        f.obtrudeException(new CFException());
315 <        checkCompletedWithWrappedCFException(f);
314 >        assertTrue(f.complete(v1));
315 >        for (int i = 0; i < 2; i++) {
316 >            f.obtrudeException(ex = new CFException());
317 >            checkCompletedExceptionally(f, ex);
318 >        }
319 >
320 >        f = new CompletableFuture<>();
321 >        for (int i = 0; i < 2; i++) {
322 >            f.obtrudeException(ex = new CFException());
323 >            checkCompletedExceptionally(f, ex);
324 >        }
325 >
326          f = new CompletableFuture<>();
327 +        f.completeExceptionally(ex = new CFException());
328 +        f.obtrudeValue(v1);
329 +        checkCompletedNormally(f, v1);
330 +        f.obtrudeException(ex = new CFException());
331 +        checkCompletedExceptionally(f, ex);
332          f.completeExceptionally(new CFException());
333 <        f.obtrudeValue(four);
334 <        checkCompletedNormally(f, four);
335 <        f.obtrudeException(new CFException());
336 <        checkCompletedWithWrappedCFException(f);
279 <    }
333 >        checkCompletedExceptionally(f, ex);
334 >        assertFalse(f.complete(v1));
335 >        checkCompletedExceptionally(f, ex);
336 >    }}
337  
338      /**
339       * getNumberOfDependents returns number of dependent tasks
340       */
341      public void testGetNumberOfDependents() {
342 +        for (ExecutionMode m : ExecutionMode.values())
343 +        for (Integer v1 : new Integer[] { 1, null })
344 +    {
345          CompletableFuture<Integer> f = new CompletableFuture<>();
346          assertEquals(0, f.getNumberOfDependents());
347 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
347 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
348          assertEquals(1, f.getNumberOfDependents());
349          assertEquals(0, g.getNumberOfDependents());
350 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
350 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
351          assertEquals(2, f.getNumberOfDependents());
352 <        f.complete(1);
352 >        assertEquals(0, h.getNumberOfDependents());
353 >        assertTrue(f.complete(v1));
354          checkCompletedNormally(g, null);
355 +        checkCompletedNormally(h, null);
356          assertEquals(0, f.getNumberOfDependents());
357          assertEquals(0, g.getNumberOfDependents());
358 <    }
358 >        assertEquals(0, h.getNumberOfDependents());
359 >    }}
360  
361      /**
362       * toString indicates current completion state
# Line 304 | Line 367 | public class CompletableFutureTest exten
367          f = new CompletableFuture<String>();
368          assertTrue(f.toString().contains("[Not completed]"));
369  
370 <        f.complete("foo");
370 >        assertTrue(f.complete("foo"));
371          assertTrue(f.toString().contains("[Completed normally]"));
372  
373          f = new CompletableFuture<String>();
374 <        f.completeExceptionally(new IndexOutOfBoundsException());
374 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
375          assertTrue(f.toString().contains("[Completed exceptionally]"));
376 +
377 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
378 +            f = new CompletableFuture<String>();
379 +            assertTrue(f.cancel(mayInterruptIfRunning));
380 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
381 +        }
382      }
383  
384      /**
# Line 360 | Line 429 | public class CompletableFutureTest exten
429          return (x == null) ? null : x + 1;
430      }
431  
432 <    class IncAction extends CheckedIntegerAction
432 >    class NoopConsumer extends CheckedIntegerAction
433          implements Consumer<Integer>
434      {
435 <        IncAction(ExecutionMode m) { super(m); }
435 >        NoopConsumer(ExecutionMode m) { super(m); }
436          public void accept(Integer x) {
437              invoked();
438 <            value = inc(x);
438 >            value = x;
439          }
440      }
441  
# Line 477 | Line 546 | public class CompletableFutureTest exten
546          }
547      }
548  
480
549      class CompletableFutureInc extends CheckedIntegerAction
550          implements Function<Integer, CompletableFuture<Integer>>
551      {
# Line 486 | Line 554 | public class CompletableFutureTest exten
554              invoked();
555              value = x;
556              CompletableFuture<Integer> f = new CompletableFuture<>();
557 <            f.complete(inc(x));
557 >            assertTrue(f.complete(inc(x)));
558              return f;
559          }
560      }
# Line 516 | Line 584 | public class CompletableFutureTest exten
584          }
585      }
586  
587 +    static final boolean defaultExecutorIsCommonPool
588 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
589 +
590      /**
591       * Permits the testing of parallel code for the 3 different
592       * execution modes without copy/pasting all the test methods.
593       */
594      enum ExecutionMode {
595 <        DEFAULT {
595 >        SYNC {
596              public void checkExecutionMode() {
597                  assertFalse(ThreadExecutor.startedCurrentThread());
598                  assertNull(ForkJoinTask.getPool());
# Line 597 | Line 668 | public class CompletableFutureTest exten
668  
669          ASYNC {
670              public void checkExecutionMode() {
671 <                assertSame(ForkJoinPool.commonPool(),
672 <                           ForkJoinTask.getPool());
671 >                assertEquals(defaultExecutorIsCommonPool,
672 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
673              }
674              public CompletableFuture<Void> runAsync(Runnable a) {
675                  return CompletableFuture.runAsync(a);
# Line 794 | Line 865 | public class CompletableFutureTest exten
865      {
866          final AtomicInteger a = new AtomicInteger(0);
867          final CompletableFuture<Integer> f = new CompletableFuture<>();
868 <        if (!createIncomplete) f.complete(v1);
868 >        if (!createIncomplete) assertTrue(f.complete(v1));
869          final CompletableFuture<Integer> g = f.exceptionally
870              ((Throwable t) -> {
871                  // Should not be called
872                  a.getAndIncrement();
873                  throw new AssertionError();
874              });
875 <        if (createIncomplete) f.complete(v1);
875 >        if (createIncomplete) assertTrue(f.complete(v1));
876  
877          checkCompletedNormally(g, v1);
878          checkCompletedNormally(f, v1);
879          assertEquals(0, a.get());
880      }}
881  
811
882      /**
883       * exceptionally action completes with function value on source
884       * exception
# Line 823 | Line 893 | public class CompletableFutureTest exten
893          if (!createIncomplete) f.completeExceptionally(ex);
894          final CompletableFuture<Integer> g = f.exceptionally
895              ((Throwable t) -> {
896 <                ExecutionMode.DEFAULT.checkExecutionMode();
896 >                ExecutionMode.SYNC.checkExecutionMode();
897                  threadAssertSame(t, ex);
898                  a.getAndIncrement();
899                  return v1;
# Line 845 | Line 915 | public class CompletableFutureTest exten
915          if (!createIncomplete) f.completeExceptionally(ex1);
916          final CompletableFuture<Integer> g = f.exceptionally
917              ((Throwable t) -> {
918 <                ExecutionMode.DEFAULT.checkExecutionMode();
918 >                ExecutionMode.SYNC.checkExecutionMode();
919 >                threadAssertSame(t, ex1);
920 >                a.getAndIncrement();
921 >                throw ex2;
922 >            });
923 >        if (createIncomplete) f.completeExceptionally(ex1);
924 >
925 >        checkCompletedWithWrappedException(g, ex2);
926 >        assertEquals(1, a.get());
927 >    }}
928 >
929 >    /**
930 >     * whenComplete action executes on normal completion, propagating
931 >     * source result.
932 >     */
933 >    public void testWhenComplete_normalCompletion1() {
934 >        for (ExecutionMode m : ExecutionMode.values())
935 >        for (boolean createIncomplete : new boolean[] { true, false })
936 >        for (Integer v1 : new Integer[] { 1, null })
937 >    {
938 >        final AtomicInteger a = new AtomicInteger(0);
939 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
940 >        if (!createIncomplete) assertTrue(f.complete(v1));
941 >        final CompletableFuture<Integer> g = m.whenComplete
942 >            (f,
943 >             (Integer x, Throwable t) -> {
944 >                m.checkExecutionMode();
945 >                threadAssertSame(x, v1);
946 >                threadAssertNull(t);
947 >                a.getAndIncrement();
948 >            });
949 >        if (createIncomplete) assertTrue(f.complete(v1));
950 >
951 >        checkCompletedNormally(g, v1);
952 >        checkCompletedNormally(f, v1);
953 >        assertEquals(1, a.get());
954 >    }}
955 >
956 >    /**
957 >     * whenComplete action executes on exceptional completion, propagating
958 >     * source result.
959 >     */
960 >    public void testWhenComplete_exceptionalCompletion() {
961 >        for (ExecutionMode m : ExecutionMode.values())
962 >        for (boolean createIncomplete : new boolean[] { true, false })
963 >        for (Integer v1 : new Integer[] { 1, null })
964 >    {
965 >        final AtomicInteger a = new AtomicInteger(0);
966 >        final CFException ex = new CFException();
967 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
968 >        if (!createIncomplete) f.completeExceptionally(ex);
969 >        final CompletableFuture<Integer> g = m.whenComplete
970 >            (f,
971 >             (Integer x, Throwable t) -> {
972 >                m.checkExecutionMode();
973 >                threadAssertNull(x);
974 >                threadAssertSame(t, ex);
975 >                a.getAndIncrement();
976 >            });
977 >        if (createIncomplete) f.completeExceptionally(ex);
978 >
979 >        checkCompletedWithWrappedException(g, ex);
980 >        checkCompletedExceptionally(f, ex);
981 >        assertEquals(1, a.get());
982 >    }}
983 >
984 >    /**
985 >     * whenComplete action executes on cancelled source, propagating
986 >     * CancellationException.
987 >     */
988 >    public void testWhenComplete_sourceCancelled() {
989 >        for (ExecutionMode m : ExecutionMode.values())
990 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
991 >        for (boolean createIncomplete : new boolean[] { true, false })
992 >    {
993 >        final AtomicInteger a = new AtomicInteger(0);
994 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
995 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
996 >        final CompletableFuture<Integer> g = m.whenComplete
997 >            (f,
998 >             (Integer x, Throwable t) -> {
999 >                m.checkExecutionMode();
1000 >                threadAssertNull(x);
1001 >                threadAssertTrue(t instanceof CancellationException);
1002 >                a.getAndIncrement();
1003 >            });
1004 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1005 >
1006 >        checkCompletedWithWrappedCancellationException(g);
1007 >        checkCancelled(f);
1008 >        assertEquals(1, a.get());
1009 >    }}
1010 >
1011 >    /**
1012 >     * If a whenComplete action throws an exception when triggered by
1013 >     * a normal completion, it completes exceptionally
1014 >     */
1015 >    public void testWhenComplete_actionFailed() {
1016 >        for (boolean createIncomplete : new boolean[] { true, false })
1017 >        for (ExecutionMode m : ExecutionMode.values())
1018 >        for (Integer v1 : new Integer[] { 1, null })
1019 >    {
1020 >        final AtomicInteger a = new AtomicInteger(0);
1021 >        final CFException ex = new CFException();
1022 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1023 >        if (!createIncomplete) assertTrue(f.complete(v1));
1024 >        final CompletableFuture<Integer> g = m.whenComplete
1025 >            (f,
1026 >             (Integer x, Throwable t) -> {
1027 >                m.checkExecutionMode();
1028 >                threadAssertSame(x, v1);
1029 >                threadAssertNull(t);
1030 >                a.getAndIncrement();
1031 >                throw ex;
1032 >            });
1033 >        if (createIncomplete) assertTrue(f.complete(v1));
1034 >
1035 >        checkCompletedWithWrappedException(g, ex);
1036 >        checkCompletedNormally(f, v1);
1037 >        assertEquals(1, a.get());
1038 >    }}
1039 >
1040 >    /**
1041 >     * If a whenComplete action throws an exception when triggered by
1042 >     * a source completion that also throws an exception, the source
1043 >     * exception takes precedence.
1044 >     */
1045 >    public void testWhenComplete_actionFailedSourceFailed() {
1046 >        for (boolean createIncomplete : new boolean[] { true, false })
1047 >        for (ExecutionMode m : ExecutionMode.values())
1048 >        for (Integer v1 : new Integer[] { 1, null })
1049 >    {
1050 >        final AtomicInteger a = new AtomicInteger(0);
1051 >        final CFException ex1 = new CFException();
1052 >        final CFException ex2 = new CFException();
1053 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1054 >
1055 >        if (!createIncomplete) f.completeExceptionally(ex1);
1056 >        final CompletableFuture<Integer> g = m.whenComplete
1057 >            (f,
1058 >             (Integer x, Throwable t) -> {
1059 >                m.checkExecutionMode();
1060                  threadAssertSame(t, ex1);
1061 +                threadAssertNull(x);
1062                  a.getAndIncrement();
1063                  throw ex2;
1064              });
1065          if (createIncomplete) f.completeExceptionally(ex1);
1066  
1067 <        checkCompletedWithWrappedCFException(g, ex2);
1067 >        checkCompletedWithWrappedException(g, ex1);
1068 >        checkCompletedExceptionally(f, ex1);
1069          assertEquals(1, a.get());
1070      }}
1071  
# Line 867 | Line 1080 | public class CompletableFutureTest exten
1080      {
1081          final CompletableFuture<Integer> f = new CompletableFuture<>();
1082          final AtomicInteger a = new AtomicInteger(0);
1083 <        if (!createIncomplete) f.complete(v1);
1083 >        if (!createIncomplete) assertTrue(f.complete(v1));
1084          final CompletableFuture<Integer> g = m.handle
1085              (f,
1086               (Integer x, Throwable t) -> {
# Line 877 | Line 1090 | public class CompletableFutureTest exten
1090                  a.getAndIncrement();
1091                  return inc(v1);
1092              });
1093 <        if (createIncomplete) f.complete(v1);
1093 >        if (createIncomplete) assertTrue(f.complete(v1));
1094  
1095          checkCompletedNormally(g, inc(v1));
1096          checkCompletedNormally(f, v1);
# Line 909 | Line 1122 | public class CompletableFutureTest exten
1122          if (createIncomplete) f.completeExceptionally(ex);
1123  
1124          checkCompletedNormally(g, v1);
1125 <        checkCompletedWithWrappedCFException(f, ex);
1125 >        checkCompletedExceptionally(f, ex);
1126          assertEquals(1, a.get());
1127      }}
1128  
# Line 965 | Line 1178 | public class CompletableFutureTest exten
1178              });
1179          if (createIncomplete) f.completeExceptionally(ex1);
1180  
1181 <        checkCompletedWithWrappedCFException(g, ex2);
1182 <        checkCompletedWithWrappedCFException(f, ex1);
1181 >        checkCompletedWithWrappedException(g, ex2);
1182 >        checkCompletedExceptionally(f, ex1);
1183          assertEquals(1, a.get());
1184      }}
1185  
# Line 978 | Line 1191 | public class CompletableFutureTest exten
1191          final CompletableFuture<Integer> f = new CompletableFuture<>();
1192          final AtomicInteger a = new AtomicInteger(0);
1193          final CFException ex = new CFException();
1194 <        if (!createIncomplete) f.complete(v1);
1194 >        if (!createIncomplete) assertTrue(f.complete(v1));
1195          final CompletableFuture<Integer> g = m.handle
1196              (f,
1197               (Integer x, Throwable t) -> {
# Line 988 | Line 1201 | public class CompletableFutureTest exten
1201                  a.getAndIncrement();
1202                  throw ex;
1203              });
1204 <        if (createIncomplete) f.complete(v1);
1204 >        if (createIncomplete) assertTrue(f.complete(v1));
1205  
1206 <        checkCompletedWithWrappedCFException(g, ex);
1206 >        checkCompletedWithWrappedException(g, ex);
1207          checkCompletedNormally(f, v1);
1208          assertEquals(1, a.get());
1209      }}
# Line 1069 | Line 1282 | public class CompletableFutureTest exten
1282       */
1283      public void testThenRun_normalCompletion() {
1284          for (ExecutionMode m : ExecutionMode.values())
1072        for (boolean createIncomplete : new boolean[] { true, false })
1285          for (Integer v1 : new Integer[] { 1, null })
1286      {
1287          final CompletableFuture<Integer> f = new CompletableFuture<>();
1288 <        final Noop r = new Noop(m);
1289 <        if (!createIncomplete) f.complete(v1);
1078 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1079 <        if (createIncomplete) {
1080 <            checkIncomplete(g);
1081 <            f.complete(v1);
1082 <        }
1288 >        final Noop[] rs = new Noop[6];
1289 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1290  
1291 <        checkCompletedNormally(g, null);
1291 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1292 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1293 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1294 >        checkIncomplete(h0);
1295 >        checkIncomplete(h1);
1296 >        checkIncomplete(h2);
1297 >        assertTrue(f.complete(v1));
1298 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1299 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1300 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1301 >
1302 >        checkCompletedNormally(h0, null);
1303 >        checkCompletedNormally(h1, null);
1304 >        checkCompletedNormally(h2, null);
1305 >        checkCompletedNormally(h3, null);
1306 >        checkCompletedNormally(h4, null);
1307 >        checkCompletedNormally(h5, null);
1308          checkCompletedNormally(f, v1);
1309 <        r.assertInvoked();
1309 >        for (Noop r : rs) r.assertInvoked();
1310      }}
1311  
1312      /**
# Line 1092 | Line 1315 | public class CompletableFutureTest exten
1315       */
1316      public void testThenRun_exceptionalCompletion() {
1317          for (ExecutionMode m : ExecutionMode.values())
1095        for (boolean createIncomplete : new boolean[] { true, false })
1318      {
1319          final CFException ex = new CFException();
1320          final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 <        final Noop r = new Noop(m);
1322 <        if (!createIncomplete) f.completeExceptionally(ex);
1101 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1102 <        if (createIncomplete) {
1103 <            checkIncomplete(g);
1104 <            f.completeExceptionally(ex);
1105 <        }
1321 >        final Noop[] rs = new Noop[6];
1322 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1323  
1324 <        checkCompletedWithWrappedCFException(g, ex);
1325 <        checkCompletedWithWrappedCFException(f, ex);
1326 <        r.assertNotInvoked();
1324 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1325 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1326 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1327 >        checkIncomplete(h0);
1328 >        checkIncomplete(h1);
1329 >        checkIncomplete(h2);
1330 >        assertTrue(f.completeExceptionally(ex));
1331 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1332 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1333 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1334 >
1335 >        checkCompletedWithWrappedException(h0, ex);
1336 >        checkCompletedWithWrappedException(h1, ex);
1337 >        checkCompletedWithWrappedException(h2, ex);
1338 >        checkCompletedWithWrappedException(h3, ex);
1339 >        checkCompletedWithWrappedException(h4, ex);
1340 >        checkCompletedWithWrappedException(h5, ex);
1341 >        checkCompletedExceptionally(f, ex);
1342 >        for (Noop r : rs) r.assertNotInvoked();
1343      }}
1344  
1345      /**
# Line 1114 | Line 1347 | public class CompletableFutureTest exten
1347       */
1348      public void testThenRun_sourceCancelled() {
1349          for (ExecutionMode m : ExecutionMode.values())
1117        for (boolean createIncomplete : new boolean[] { true, false })
1350          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1351      {
1352          final CompletableFuture<Integer> f = new CompletableFuture<>();
1353 <        final Noop r = new Noop(m);
1354 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1123 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1124 <        if (createIncomplete) {
1125 <            checkIncomplete(g);
1126 <            assertTrue(f.cancel(mayInterruptIfRunning));
1127 <        }
1353 >        final Noop[] rs = new Noop[6];
1354 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1355  
1356 <        checkCompletedWithWrappedCancellationException(g);
1356 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1357 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1358 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1359 >        checkIncomplete(h0);
1360 >        checkIncomplete(h1);
1361 >        checkIncomplete(h2);
1362 >        assertTrue(f.cancel(mayInterruptIfRunning));
1363 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1364 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1365 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1366 >
1367 >        checkCompletedWithWrappedCancellationException(h0);
1368 >        checkCompletedWithWrappedCancellationException(h1);
1369 >        checkCompletedWithWrappedCancellationException(h2);
1370 >        checkCompletedWithWrappedCancellationException(h3);
1371 >        checkCompletedWithWrappedCancellationException(h4);
1372 >        checkCompletedWithWrappedCancellationException(h5);
1373          checkCancelled(f);
1374 <        r.assertNotInvoked();
1374 >        for (Noop r : rs) r.assertNotInvoked();
1375      }}
1376  
1377      /**
# Line 1136 | Line 1379 | public class CompletableFutureTest exten
1379       */
1380      public void testThenRun_actionFailed() {
1381          for (ExecutionMode m : ExecutionMode.values())
1139        for (boolean createIncomplete : new boolean[] { true, false })
1382          for (Integer v1 : new Integer[] { 1, null })
1383      {
1384          final CompletableFuture<Integer> f = new CompletableFuture<>();
1385 <        final FailingRunnable r = new FailingRunnable(m);
1386 <        if (!createIncomplete) f.complete(v1);
1145 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1146 <        if (createIncomplete) {
1147 <            checkIncomplete(g);
1148 <            f.complete(v1);
1149 <        }
1385 >        final FailingRunnable[] rs = new FailingRunnable[6];
1386 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1387  
1388 <        checkCompletedWithWrappedCFException(g);
1388 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1389 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1390 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1391 >        assertTrue(f.complete(v1));
1392 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1393 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1394 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1395 >
1396 >        checkCompletedWithWrappedCFException(h0);
1397 >        checkCompletedWithWrappedCFException(h1);
1398 >        checkCompletedWithWrappedCFException(h2);
1399 >        checkCompletedWithWrappedCFException(h3);
1400 >        checkCompletedWithWrappedCFException(h4);
1401 >        checkCompletedWithWrappedCFException(h5);
1402          checkCompletedNormally(f, v1);
1403      }}
1404  
# Line 1157 | Line 1407 | public class CompletableFutureTest exten
1407       */
1408      public void testThenApply_normalCompletion() {
1409          for (ExecutionMode m : ExecutionMode.values())
1160        for (boolean createIncomplete : new boolean[] { true, false })
1410          for (Integer v1 : new Integer[] { 1, null })
1411      {
1412          final CompletableFuture<Integer> f = new CompletableFuture<>();
1413 <        final IncFunction r = new IncFunction(m);
1414 <        if (!createIncomplete) f.complete(v1);
1166 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1167 <        if (createIncomplete) {
1168 <            checkIncomplete(g);
1169 <            f.complete(v1);
1170 <        }
1413 >        final IncFunction[] rs = new IncFunction[4];
1414 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1415  
1416 <        checkCompletedNormally(g, inc(v1));
1416 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1417 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1418 >        checkIncomplete(h0);
1419 >        checkIncomplete(h1);
1420 >        assertTrue(f.complete(v1));
1421 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1422 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1423 >
1424 >        checkCompletedNormally(h0, inc(v1));
1425 >        checkCompletedNormally(h1, inc(v1));
1426 >        checkCompletedNormally(h2, inc(v1));
1427 >        checkCompletedNormally(h3, inc(v1));
1428          checkCompletedNormally(f, v1);
1429 <        r.assertInvoked();
1429 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1430      }}
1431  
1432      /**
# Line 1180 | Line 1435 | public class CompletableFutureTest exten
1435       */
1436      public void testThenApply_exceptionalCompletion() {
1437          for (ExecutionMode m : ExecutionMode.values())
1183        for (boolean createIncomplete : new boolean[] { true, false })
1438      {
1439          final CFException ex = new CFException();
1440          final CompletableFuture<Integer> f = new CompletableFuture<>();
1441 <        final IncFunction r = new IncFunction(m);
1442 <        if (!createIncomplete) f.completeExceptionally(ex);
1189 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1190 <        if (createIncomplete) {
1191 <            checkIncomplete(g);
1192 <            f.completeExceptionally(ex);
1193 <        }
1441 >        final IncFunction[] rs = new IncFunction[4];
1442 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1443  
1444 <        checkCompletedWithWrappedCFException(g, ex);
1445 <        checkCompletedWithWrappedCFException(f, ex);
1446 <        r.assertNotInvoked();
1444 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1445 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1446 >        assertTrue(f.completeExceptionally(ex));
1447 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1448 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1449 >
1450 >        checkCompletedWithWrappedException(h0, ex);
1451 >        checkCompletedWithWrappedException(h1, ex);
1452 >        checkCompletedWithWrappedException(h2, ex);
1453 >        checkCompletedWithWrappedException(h3, ex);
1454 >        checkCompletedExceptionally(f, ex);
1455 >        for (IncFunction r : rs) r.assertNotInvoked();
1456      }}
1457  
1458      /**
# Line 1202 | Line 1460 | public class CompletableFutureTest exten
1460       */
1461      public void testThenApply_sourceCancelled() {
1462          for (ExecutionMode m : ExecutionMode.values())
1205        for (boolean createIncomplete : new boolean[] { true, false })
1463          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1464      {
1465          final CompletableFuture<Integer> f = new CompletableFuture<>();
1466 <        final IncFunction r = new IncFunction(m);
1467 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1211 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1212 <        if (createIncomplete) {
1213 <            checkIncomplete(g);
1214 <            assertTrue(f.cancel(mayInterruptIfRunning));
1215 <        }
1466 >        final IncFunction[] rs = new IncFunction[4];
1467 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1468  
1469 <        checkCompletedWithWrappedCancellationException(g);
1469 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1470 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1471 >        assertTrue(f.cancel(mayInterruptIfRunning));
1472 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1473 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1474 >
1475 >        checkCompletedWithWrappedCancellationException(h0);
1476 >        checkCompletedWithWrappedCancellationException(h1);
1477 >        checkCompletedWithWrappedCancellationException(h2);
1478 >        checkCompletedWithWrappedCancellationException(h3);
1479          checkCancelled(f);
1480 <        r.assertNotInvoked();
1480 >        for (IncFunction r : rs) r.assertNotInvoked();
1481      }}
1482  
1483      /**
# Line 1224 | Line 1485 | public class CompletableFutureTest exten
1485       */
1486      public void testThenApply_actionFailed() {
1487          for (ExecutionMode m : ExecutionMode.values())
1227        for (boolean createIncomplete : new boolean[] { true, false })
1488          for (Integer v1 : new Integer[] { 1, null })
1489      {
1490          final CompletableFuture<Integer> f = new CompletableFuture<>();
1491 <        final FailingFunction r = new FailingFunction(m);
1492 <        if (!createIncomplete) f.complete(v1);
1233 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1234 <        if (createIncomplete) {
1235 <            checkIncomplete(g);
1236 <            f.complete(v1);
1237 <        }
1491 >        final FailingFunction[] rs = new FailingFunction[4];
1492 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1493  
1494 <        checkCompletedWithWrappedCFException(g);
1494 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1495 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1496 >        assertTrue(f.complete(v1));
1497 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1498 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1499 >
1500 >        checkCompletedWithWrappedCFException(h0);
1501 >        checkCompletedWithWrappedCFException(h1);
1502 >        checkCompletedWithWrappedCFException(h2);
1503 >        checkCompletedWithWrappedCFException(h3);
1504          checkCompletedNormally(f, v1);
1505      }}
1506  
# Line 1245 | Line 1509 | public class CompletableFutureTest exten
1509       */
1510      public void testThenAccept_normalCompletion() {
1511          for (ExecutionMode m : ExecutionMode.values())
1248        for (boolean createIncomplete : new boolean[] { true, false })
1512          for (Integer v1 : new Integer[] { 1, null })
1513      {
1514          final CompletableFuture<Integer> f = new CompletableFuture<>();
1515 <        final IncAction r = new IncAction(m);
1516 <        if (!createIncomplete) f.complete(v1);
1254 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1255 <        if (createIncomplete) {
1256 <            checkIncomplete(g);
1257 <            f.complete(v1);
1258 <        }
1515 >        final NoopConsumer[] rs = new NoopConsumer[4];
1516 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1517  
1518 <        checkCompletedNormally(g, null);
1518 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1519 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1520 >        checkIncomplete(h0);
1521 >        checkIncomplete(h1);
1522 >        assertTrue(f.complete(v1));
1523 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1524 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1525 >
1526 >        checkCompletedNormally(h0, null);
1527 >        checkCompletedNormally(h1, null);
1528 >        checkCompletedNormally(h2, null);
1529 >        checkCompletedNormally(h3, null);
1530          checkCompletedNormally(f, v1);
1531 <        r.assertInvoked();
1263 <        r.assertValue(inc(v1));
1531 >        for (NoopConsumer r : rs) r.assertValue(v1);
1532      }}
1533  
1534      /**
# Line 1269 | Line 1537 | public class CompletableFutureTest exten
1537       */
1538      public void testThenAccept_exceptionalCompletion() {
1539          for (ExecutionMode m : ExecutionMode.values())
1272        for (boolean createIncomplete : new boolean[] { true, false })
1540      {
1541          final CFException ex = new CFException();
1542          final CompletableFuture<Integer> f = new CompletableFuture<>();
1543 <        final IncAction r = new IncAction(m);
1544 <        if (!createIncomplete) f.completeExceptionally(ex);
1278 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1279 <        if (createIncomplete) {
1280 <            checkIncomplete(g);
1281 <            f.completeExceptionally(ex);
1282 <        }
1543 >        final NoopConsumer[] rs = new NoopConsumer[4];
1544 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1545  
1546 <        checkCompletedWithWrappedCFException(g, ex);
1547 <        checkCompletedWithWrappedCFException(f, ex);
1548 <        r.assertNotInvoked();
1546 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1547 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1548 >        assertTrue(f.completeExceptionally(ex));
1549 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1550 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1551 >
1552 >        checkCompletedWithWrappedException(h0, ex);
1553 >        checkCompletedWithWrappedException(h1, ex);
1554 >        checkCompletedWithWrappedException(h2, ex);
1555 >        checkCompletedWithWrappedException(h3, ex);
1556 >        checkCompletedExceptionally(f, ex);
1557 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1558      }}
1559  
1560      /**
# Line 1291 | Line 1562 | public class CompletableFutureTest exten
1562       */
1563      public void testThenAccept_sourceCancelled() {
1564          for (ExecutionMode m : ExecutionMode.values())
1294        for (boolean createIncomplete : new boolean[] { true, false })
1565          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1566      {
1567          final CompletableFuture<Integer> f = new CompletableFuture<>();
1568 <        final IncAction r = new IncAction(m);
1569 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1300 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1301 <        if (createIncomplete) {
1302 <            checkIncomplete(g);
1303 <            assertTrue(f.cancel(mayInterruptIfRunning));
1304 <        }
1568 >        final NoopConsumer[] rs = new NoopConsumer[4];
1569 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1570  
1571 <        checkCompletedWithWrappedCancellationException(g);
1571 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1572 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1573 >        assertTrue(f.cancel(mayInterruptIfRunning));
1574 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1575 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1576 >
1577 >        checkCompletedWithWrappedCancellationException(h0);
1578 >        checkCompletedWithWrappedCancellationException(h1);
1579 >        checkCompletedWithWrappedCancellationException(h2);
1580 >        checkCompletedWithWrappedCancellationException(h3);
1581          checkCancelled(f);
1582 <        r.assertNotInvoked();
1582 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1583      }}
1584  
1585      /**
# Line 1313 | Line 1587 | public class CompletableFutureTest exten
1587       */
1588      public void testThenAccept_actionFailed() {
1589          for (ExecutionMode m : ExecutionMode.values())
1316        for (boolean createIncomplete : new boolean[] { true, false })
1590          for (Integer v1 : new Integer[] { 1, null })
1591      {
1592          final CompletableFuture<Integer> f = new CompletableFuture<>();
1593 <        final FailingConsumer r = new FailingConsumer(m);
1594 <        if (!createIncomplete) f.complete(v1);
1322 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1323 <        if (createIncomplete) {
1324 <            checkIncomplete(g);
1325 <            f.complete(v1);
1326 <        }
1593 >        final FailingConsumer[] rs = new FailingConsumer[4];
1594 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1595  
1596 <        checkCompletedWithWrappedCFException(g);
1596 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1597 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1598 >        assertTrue(f.complete(v1));
1599 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1600 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1601 >
1602 >        checkCompletedWithWrappedCFException(h0);
1603 >        checkCompletedWithWrappedCFException(h1);
1604 >        checkCompletedWithWrappedCFException(h2);
1605 >        checkCompletedWithWrappedCFException(h3);
1606          checkCompletedNormally(f, v1);
1607      }}
1608  
# Line 1335 | Line 1612 | public class CompletableFutureTest exten
1612       */
1613      public void testThenCombine_normalCompletion() {
1614          for (ExecutionMode m : ExecutionMode.values())
1338        for (boolean createIncomplete : new boolean[] { true, false })
1615          for (boolean fFirst : new boolean[] { true, false })
1616          for (Integer v1 : new Integer[] { 1, null })
1617          for (Integer v2 : new Integer[] { 2, null })
1618      {
1619          final CompletableFuture<Integer> f = new CompletableFuture<>();
1620          final CompletableFuture<Integer> g = new CompletableFuture<>();
1621 <        final SubtractFunction r = new SubtractFunction(m);
1621 >        final SubtractFunction[] rs = new SubtractFunction[6];
1622 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1623  
1624 <        if (fFirst) f.complete(v1); else g.complete(v2);
1625 <        if (!createIncomplete)
1626 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1627 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1628 <        if (createIncomplete) {
1629 <            checkIncomplete(h);
1630 <            r.assertNotInvoked();
1631 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1632 <        }
1624 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1625 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1626 >        final Integer w1 =  fFirst ? v1 : v2;
1627 >        final Integer w2 = !fFirst ? v1 : v2;
1628 >
1629 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1630 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1631 >        assertTrue(fst.complete(w1));
1632 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1633 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1634 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1635 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1636 >        checkCompletedNormally(h1, subtract(w1, w1));
1637 >        checkCompletedNormally(h3, subtract(w1, w1));
1638 >        rs[1].assertValue(subtract(w1, w1));
1639 >        rs[3].assertValue(subtract(w1, w1));
1640 >        assertTrue(snd.complete(w2));
1641 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1642 >
1643 >        checkCompletedNormally(h0, subtract(v1, v2));
1644 >        checkCompletedNormally(h2, subtract(v1, v2));
1645 >        checkCompletedNormally(h4, subtract(v1, v2));
1646 >        rs[0].assertValue(subtract(v1, v2));
1647 >        rs[2].assertValue(subtract(v1, v2));
1648 >        rs[4].assertValue(subtract(v1, v2));
1649  
1357        checkCompletedNormally(h, subtract(v1, v2));
1650          checkCompletedNormally(f, v1);
1651          checkCompletedNormally(g, v2);
1360        r.assertInvoked();
1652      }}
1653  
1654      /**
1655       * thenCombine result completes exceptionally after exceptional
1656       * completion of either source
1657       */
1658 <    public void testThenCombine_exceptionalCompletion() {
1658 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1659          for (ExecutionMode m : ExecutionMode.values())
1369        for (boolean createIncomplete : new boolean[] { true, false })
1660          for (boolean fFirst : new boolean[] { true, false })
1661 +        for (boolean failFirst : new boolean[] { true, false })
1662          for (Integer v1 : new Integer[] { 1, null })
1663      {
1664          final CompletableFuture<Integer> f = new CompletableFuture<>();
1665          final CompletableFuture<Integer> g = new CompletableFuture<>();
1666          final CFException ex = new CFException();
1667 <        final SubtractFunction r = new SubtractFunction(m);
1668 <
1669 <        (fFirst ? f : g).complete(v1);
1670 <        if (!createIncomplete)
1671 <            (!fFirst ? f : g).completeExceptionally(ex);
1672 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1673 <        if (createIncomplete) {
1674 <            checkIncomplete(h);
1675 <            (!fFirst ? f : g).completeExceptionally(ex);
1676 <        }
1677 <
1678 <        checkCompletedWithWrappedCFException(h, ex);
1679 <        r.assertNotInvoked();
1680 <        checkCompletedNormally(fFirst ? f : g, v1);
1681 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1667 >        final SubtractFunction r1 = new SubtractFunction(m);
1668 >        final SubtractFunction r2 = new SubtractFunction(m);
1669 >        final SubtractFunction r3 = new SubtractFunction(m);
1670 >
1671 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1672 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1673 >        final Callable<Boolean> complete1 = failFirst ?
1674 >            () -> fst.completeExceptionally(ex) :
1675 >            () -> fst.complete(v1);
1676 >        final Callable<Boolean> complete2 = failFirst ?
1677 >            () -> snd.complete(v1) :
1678 >            () -> snd.completeExceptionally(ex);
1679 >
1680 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1681 >        assertTrue(complete1.call());
1682 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1683 >        checkIncomplete(h1);
1684 >        checkIncomplete(h2);
1685 >        assertTrue(complete2.call());
1686 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1687 >
1688 >        checkCompletedWithWrappedException(h1, ex);
1689 >        checkCompletedWithWrappedException(h2, ex);
1690 >        checkCompletedWithWrappedException(h3, ex);
1691 >        r1.assertNotInvoked();
1692 >        r2.assertNotInvoked();
1693 >        r3.assertNotInvoked();
1694 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1695 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1696      }}
1697  
1698      /**
1699       * thenCombine result completes exceptionally if either source cancelled
1700       */
1701 <    public void testThenCombine_sourceCancelled() {
1701 >    public void testThenCombine_sourceCancelled() throws Throwable {
1702          for (ExecutionMode m : ExecutionMode.values())
1703          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1399        for (boolean createIncomplete : new boolean[] { true, false })
1704          for (boolean fFirst : new boolean[] { true, false })
1705 +        for (boolean failFirst : new boolean[] { true, false })
1706          for (Integer v1 : new Integer[] { 1, null })
1707      {
1708          final CompletableFuture<Integer> f = new CompletableFuture<>();
1709          final CompletableFuture<Integer> g = new CompletableFuture<>();
1710 <        final SubtractFunction r = new SubtractFunction(m);
1710 >        final SubtractFunction r1 = new SubtractFunction(m);
1711 >        final SubtractFunction r2 = new SubtractFunction(m);
1712 >        final SubtractFunction r3 = new SubtractFunction(m);
1713  
1714 <        (fFirst ? f : g).complete(v1);
1715 <        if (!createIncomplete)
1716 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1717 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1718 <        if (createIncomplete) {
1719 <            checkIncomplete(h);
1720 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1721 <        }
1714 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1715 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1716 >        final Callable<Boolean> complete1 = failFirst ?
1717 >            () -> fst.cancel(mayInterruptIfRunning) :
1718 >            () -> fst.complete(v1);
1719 >        final Callable<Boolean> complete2 = failFirst ?
1720 >            () -> snd.complete(v1) :
1721 >            () -> snd.cancel(mayInterruptIfRunning);
1722  
1723 <        checkCompletedWithWrappedCancellationException(h);
1724 <        checkCancelled(!fFirst ? f : g);
1725 <        r.assertNotInvoked();
1726 <        checkCompletedNormally(fFirst ? f : g, v1);
1723 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1724 >        assertTrue(complete1.call());
1725 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1726 >        checkIncomplete(h1);
1727 >        checkIncomplete(h2);
1728 >        assertTrue(complete2.call());
1729 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1730 >
1731 >        checkCompletedWithWrappedCancellationException(h1);
1732 >        checkCompletedWithWrappedCancellationException(h2);
1733 >        checkCompletedWithWrappedCancellationException(h3);
1734 >        r1.assertNotInvoked();
1735 >        r2.assertNotInvoked();
1736 >        r3.assertNotInvoked();
1737 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1738 >        checkCancelled(failFirst ? fst : snd);
1739      }}
1740  
1741      /**
# Line 1430 | Line 1749 | public class CompletableFutureTest exten
1749      {
1750          final CompletableFuture<Integer> f = new CompletableFuture<>();
1751          final CompletableFuture<Integer> g = new CompletableFuture<>();
1752 <        final FailingBiFunction r = new FailingBiFunction(m);
1753 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1754 <
1755 <        if (fFirst) {
1756 <            f.complete(v1);
1757 <            g.complete(v2);
1758 <        } else {
1759 <            g.complete(v2);
1760 <            f.complete(v1);
1761 <        }
1752 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1753 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1754 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1755 >
1756 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1757 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1758 >        final Integer w1 =  fFirst ? v1 : v2;
1759 >        final Integer w2 = !fFirst ? v1 : v2;
1760 >
1761 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1762 >        assertTrue(fst.complete(w1));
1763 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1764 >        assertTrue(snd.complete(w2));
1765 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1766  
1767 <        checkCompletedWithWrappedCFException(h);
1767 >        checkCompletedWithWrappedCFException(h1);
1768 >        checkCompletedWithWrappedCFException(h2);
1769 >        checkCompletedWithWrappedCFException(h3);
1770 >        r1.assertInvoked();
1771 >        r2.assertInvoked();
1772 >        r3.assertInvoked();
1773          checkCompletedNormally(f, v1);
1774          checkCompletedNormally(g, v2);
1775      }}
# Line 1452 | Line 1780 | public class CompletableFutureTest exten
1780       */
1781      public void testThenAcceptBoth_normalCompletion() {
1782          for (ExecutionMode m : ExecutionMode.values())
1455        for (boolean createIncomplete : new boolean[] { true, false })
1783          for (boolean fFirst : new boolean[] { true, false })
1784          for (Integer v1 : new Integer[] { 1, null })
1785          for (Integer v2 : new Integer[] { 2, null })
1786      {
1787          final CompletableFuture<Integer> f = new CompletableFuture<>();
1788          final CompletableFuture<Integer> g = new CompletableFuture<>();
1789 <        final SubtractAction r = new SubtractAction(m);
1790 <
1791 <        if (fFirst) f.complete(v1); else g.complete(v2);
1792 <        if (!createIncomplete)
1793 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1794 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1795 <        if (createIncomplete) {
1796 <            checkIncomplete(h);
1797 <            r.assertNotInvoked();
1798 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1799 <        }
1789 >        final SubtractAction r1 = new SubtractAction(m);
1790 >        final SubtractAction r2 = new SubtractAction(m);
1791 >        final SubtractAction r3 = new SubtractAction(m);
1792 >
1793 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1794 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1795 >        final Integer w1 =  fFirst ? v1 : v2;
1796 >        final Integer w2 = !fFirst ? v1 : v2;
1797 >
1798 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1799 >        assertTrue(fst.complete(w1));
1800 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1801 >        checkIncomplete(h1);
1802 >        checkIncomplete(h2);
1803 >        r1.assertNotInvoked();
1804 >        r2.assertNotInvoked();
1805 >        assertTrue(snd.complete(w2));
1806 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1807  
1808 <        checkCompletedNormally(h, null);
1809 <        r.assertValue(subtract(v1, v2));
1808 >        checkCompletedNormally(h1, null);
1809 >        checkCompletedNormally(h2, null);
1810 >        checkCompletedNormally(h3, null);
1811 >        r1.assertValue(subtract(v1, v2));
1812 >        r2.assertValue(subtract(v1, v2));
1813 >        r3.assertValue(subtract(v1, v2));
1814          checkCompletedNormally(f, v1);
1815          checkCompletedNormally(g, v2);
1816      }}
# Line 1481 | Line 1819 | public class CompletableFutureTest exten
1819       * thenAcceptBoth result completes exceptionally after exceptional
1820       * completion of either source
1821       */
1822 <    public void testThenAcceptBoth_exceptionalCompletion() {
1822 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1823          for (ExecutionMode m : ExecutionMode.values())
1486        for (boolean createIncomplete : new boolean[] { true, false })
1824          for (boolean fFirst : new boolean[] { true, false })
1825 +        for (boolean failFirst : new boolean[] { true, false })
1826          for (Integer v1 : new Integer[] { 1, null })
1827      {
1828          final CompletableFuture<Integer> f = new CompletableFuture<>();
1829          final CompletableFuture<Integer> g = new CompletableFuture<>();
1830          final CFException ex = new CFException();
1831 <        final SubtractAction r = new SubtractAction(m);
1832 <
1833 <        (fFirst ? f : g).complete(v1);
1834 <        if (!createIncomplete)
1835 <            (!fFirst ? f : g).completeExceptionally(ex);
1836 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1837 <        if (createIncomplete) {
1838 <            checkIncomplete(h);
1839 <            (!fFirst ? f : g).completeExceptionally(ex);
1840 <        }
1841 <
1842 <        checkCompletedWithWrappedCFException(h, ex);
1843 <        r.assertNotInvoked();
1844 <        checkCompletedNormally(fFirst ? f : g, v1);
1845 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1831 >        final SubtractAction r1 = new SubtractAction(m);
1832 >        final SubtractAction r2 = new SubtractAction(m);
1833 >        final SubtractAction r3 = new SubtractAction(m);
1834 >
1835 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1836 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1837 >        final Callable<Boolean> complete1 = failFirst ?
1838 >            () -> fst.completeExceptionally(ex) :
1839 >            () -> fst.complete(v1);
1840 >        final Callable<Boolean> complete2 = failFirst ?
1841 >            () -> snd.complete(v1) :
1842 >            () -> snd.completeExceptionally(ex);
1843 >
1844 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1845 >        assertTrue(complete1.call());
1846 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1847 >        checkIncomplete(h1);
1848 >        checkIncomplete(h2);
1849 >        assertTrue(complete2.call());
1850 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1851 >
1852 >        checkCompletedWithWrappedException(h1, ex);
1853 >        checkCompletedWithWrappedException(h2, ex);
1854 >        checkCompletedWithWrappedException(h3, ex);
1855 >        r1.assertNotInvoked();
1856 >        r2.assertNotInvoked();
1857 >        r3.assertNotInvoked();
1858 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1859 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1860      }}
1861  
1862      /**
1863       * thenAcceptBoth result completes exceptionally if either source cancelled
1864       */
1865 <    public void testThenAcceptBoth_sourceCancelled() {
1865 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1866          for (ExecutionMode m : ExecutionMode.values())
1867          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1516        for (boolean createIncomplete : new boolean[] { true, false })
1868          for (boolean fFirst : new boolean[] { true, false })
1869 +        for (boolean failFirst : new boolean[] { true, false })
1870          for (Integer v1 : new Integer[] { 1, null })
1871      {
1872          final CompletableFuture<Integer> f = new CompletableFuture<>();
1873          final CompletableFuture<Integer> g = new CompletableFuture<>();
1874 <        final SubtractAction r = new SubtractAction(m);
1874 >        final SubtractAction r1 = new SubtractAction(m);
1875 >        final SubtractAction r2 = new SubtractAction(m);
1876 >        final SubtractAction r3 = new SubtractAction(m);
1877  
1878 <        (fFirst ? f : g).complete(v1);
1879 <        if (!createIncomplete)
1880 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1881 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1882 <        if (createIncomplete) {
1883 <            checkIncomplete(h);
1884 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1885 <        }
1878 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1879 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1880 >        final Callable<Boolean> complete1 = failFirst ?
1881 >            () -> fst.cancel(mayInterruptIfRunning) :
1882 >            () -> fst.complete(v1);
1883 >        final Callable<Boolean> complete2 = failFirst ?
1884 >            () -> snd.complete(v1) :
1885 >            () -> snd.cancel(mayInterruptIfRunning);
1886  
1887 <        checkCompletedWithWrappedCancellationException(h);
1888 <        checkCancelled(!fFirst ? f : g);
1889 <        r.assertNotInvoked();
1890 <        checkCompletedNormally(fFirst ? f : g, v1);
1887 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1888 >        assertTrue(complete1.call());
1889 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1890 >        checkIncomplete(h1);
1891 >        checkIncomplete(h2);
1892 >        assertTrue(complete2.call());
1893 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1894 >
1895 >        checkCompletedWithWrappedCancellationException(h1);
1896 >        checkCompletedWithWrappedCancellationException(h2);
1897 >        checkCompletedWithWrappedCancellationException(h3);
1898 >        r1.assertNotInvoked();
1899 >        r2.assertNotInvoked();
1900 >        r3.assertNotInvoked();
1901 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1902 >        checkCancelled(failFirst ? fst : snd);
1903      }}
1904  
1905      /**
# Line 1547 | Line 1913 | public class CompletableFutureTest exten
1913      {
1914          final CompletableFuture<Integer> f = new CompletableFuture<>();
1915          final CompletableFuture<Integer> g = new CompletableFuture<>();
1916 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1917 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1918 <
1919 <        if (fFirst) {
1920 <            f.complete(v1);
1921 <            g.complete(v2);
1922 <        } else {
1923 <            g.complete(v2);
1924 <            f.complete(v1);
1925 <        }
1916 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1917 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1918 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1919 >
1920 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1921 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1922 >        final Integer w1 =  fFirst ? v1 : v2;
1923 >        final Integer w2 = !fFirst ? v1 : v2;
1924 >
1925 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1926 >        assertTrue(fst.complete(w1));
1927 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1928 >        assertTrue(snd.complete(w2));
1929 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1930  
1931 <        checkCompletedWithWrappedCFException(h);
1931 >        checkCompletedWithWrappedCFException(h1);
1932 >        checkCompletedWithWrappedCFException(h2);
1933 >        checkCompletedWithWrappedCFException(h3);
1934 >        r1.assertInvoked();
1935 >        r2.assertInvoked();
1936 >        r3.assertInvoked();
1937          checkCompletedNormally(f, v1);
1938          checkCompletedNormally(g, v2);
1939      }}
# Line 1569 | Line 1944 | public class CompletableFutureTest exten
1944       */
1945      public void testRunAfterBoth_normalCompletion() {
1946          for (ExecutionMode m : ExecutionMode.values())
1572        for (boolean createIncomplete : new boolean[] { true, false })
1947          for (boolean fFirst : new boolean[] { true, false })
1948          for (Integer v1 : new Integer[] { 1, null })
1949          for (Integer v2 : new Integer[] { 2, null })
1950      {
1951          final CompletableFuture<Integer> f = new CompletableFuture<>();
1952          final CompletableFuture<Integer> g = new CompletableFuture<>();
1953 <        final Noop r = new Noop(m);
1954 <
1955 <        if (fFirst) f.complete(v1); else g.complete(v2);
1956 <        if (!createIncomplete)
1957 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1958 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1959 <        if (createIncomplete) {
1960 <            checkIncomplete(h);
1961 <            r.assertNotInvoked();
1962 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1963 <        }
1953 >        final Noop r1 = new Noop(m);
1954 >        final Noop r2 = new Noop(m);
1955 >        final Noop r3 = new Noop(m);
1956 >
1957 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1958 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1959 >        final Integer w1 =  fFirst ? v1 : v2;
1960 >        final Integer w2 = !fFirst ? v1 : v2;
1961 >
1962 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1963 >        assertTrue(fst.complete(w1));
1964 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1965 >        checkIncomplete(h1);
1966 >        checkIncomplete(h2);
1967 >        r1.assertNotInvoked();
1968 >        r2.assertNotInvoked();
1969 >        assertTrue(snd.complete(w2));
1970 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1971  
1972 <        checkCompletedNormally(h, null);
1973 <        r.assertInvoked();
1972 >        checkCompletedNormally(h1, null);
1973 >        checkCompletedNormally(h2, null);
1974 >        checkCompletedNormally(h3, null);
1975 >        r1.assertInvoked();
1976 >        r2.assertInvoked();
1977 >        r3.assertInvoked();
1978          checkCompletedNormally(f, v1);
1979          checkCompletedNormally(g, v2);
1980      }}
# Line 1598 | Line 1983 | public class CompletableFutureTest exten
1983       * runAfterBoth result completes exceptionally after exceptional
1984       * completion of either source
1985       */
1986 <    public void testRunAfterBoth_exceptionalCompletion() {
1986 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1987          for (ExecutionMode m : ExecutionMode.values())
1603        for (boolean createIncomplete : new boolean[] { true, false })
1988          for (boolean fFirst : new boolean[] { true, false })
1989 +        for (boolean failFirst : new boolean[] { true, false })
1990          for (Integer v1 : new Integer[] { 1, null })
1991      {
1992          final CompletableFuture<Integer> f = new CompletableFuture<>();
1993          final CompletableFuture<Integer> g = new CompletableFuture<>();
1994          final CFException ex = new CFException();
1995 <        final Noop r = new Noop(m);
1996 <
1997 <        (fFirst ? f : g).complete(v1);
1998 <        if (!createIncomplete)
1999 <            (!fFirst ? f : g).completeExceptionally(ex);
2000 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2001 <        if (createIncomplete) {
2002 <            checkIncomplete(h);
2003 <            (!fFirst ? f : g).completeExceptionally(ex);
2004 <        }
2005 <
2006 <        checkCompletedWithWrappedCFException(h, ex);
2007 <        r.assertNotInvoked();
2008 <        checkCompletedNormally(fFirst ? f : g, v1);
2009 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1995 >        final Noop r1 = new Noop(m);
1996 >        final Noop r2 = new Noop(m);
1997 >        final Noop r3 = new Noop(m);
1998 >
1999 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2000 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2001 >        final Callable<Boolean> complete1 = failFirst ?
2002 >            () -> fst.completeExceptionally(ex) :
2003 >            () -> fst.complete(v1);
2004 >        final Callable<Boolean> complete2 = failFirst ?
2005 >            () -> snd.complete(v1) :
2006 >            () -> snd.completeExceptionally(ex);
2007 >
2008 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2009 >        assertTrue(complete1.call());
2010 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2011 >        checkIncomplete(h1);
2012 >        checkIncomplete(h2);
2013 >        assertTrue(complete2.call());
2014 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2015 >
2016 >        checkCompletedWithWrappedException(h1, ex);
2017 >        checkCompletedWithWrappedException(h2, ex);
2018 >        checkCompletedWithWrappedException(h3, ex);
2019 >        r1.assertNotInvoked();
2020 >        r2.assertNotInvoked();
2021 >        r3.assertNotInvoked();
2022 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2023 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2024      }}
2025  
2026      /**
2027       * runAfterBoth result completes exceptionally if either source cancelled
2028       */
2029 <    public void testRunAfterBoth_sourceCancelled() {
2029 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2030          for (ExecutionMode m : ExecutionMode.values())
2031          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1633        for (boolean createIncomplete : new boolean[] { true, false })
2032          for (boolean fFirst : new boolean[] { true, false })
2033 +        for (boolean failFirst : new boolean[] { true, false })
2034          for (Integer v1 : new Integer[] { 1, null })
2035      {
2036          final CompletableFuture<Integer> f = new CompletableFuture<>();
2037          final CompletableFuture<Integer> g = new CompletableFuture<>();
2038 <        final Noop r = new Noop(m);
2038 >        final Noop r1 = new Noop(m);
2039 >        final Noop r2 = new Noop(m);
2040 >        final Noop r3 = new Noop(m);
2041  
2042 +        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2043 +        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2044 +        final Callable<Boolean> complete1 = failFirst ?
2045 +            () -> fst.cancel(mayInterruptIfRunning) :
2046 +            () -> fst.complete(v1);
2047 +        final Callable<Boolean> complete2 = failFirst ?
2048 +            () -> snd.complete(v1) :
2049 +            () -> snd.cancel(mayInterruptIfRunning);
2050  
2051 <        (fFirst ? f : g).complete(v1);
2052 <        if (!createIncomplete)
2053 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2054 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2055 <        if (createIncomplete) {
2056 <            checkIncomplete(h);
2057 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1649 <        }
2051 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2052 >        assertTrue(complete1.call());
2053 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2054 >        checkIncomplete(h1);
2055 >        checkIncomplete(h2);
2056 >        assertTrue(complete2.call());
2057 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2058  
2059 <        checkCompletedWithWrappedCancellationException(h);
2060 <        checkCancelled(!fFirst ? f : g);
2061 <        r.assertNotInvoked();
2062 <        checkCompletedNormally(fFirst ? f : g, v1);
2059 >        checkCompletedWithWrappedCancellationException(h1);
2060 >        checkCompletedWithWrappedCancellationException(h2);
2061 >        checkCompletedWithWrappedCancellationException(h3);
2062 >        r1.assertNotInvoked();
2063 >        r2.assertNotInvoked();
2064 >        r3.assertNotInvoked();
2065 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2066 >        checkCancelled(failFirst ? fst : snd);
2067      }}
2068  
2069      /**
# Line 1667 | Line 2079 | public class CompletableFutureTest exten
2079          final CompletableFuture<Integer> g = new CompletableFuture<>();
2080          final FailingRunnable r1 = new FailingRunnable(m);
2081          final FailingRunnable r2 = new FailingRunnable(m);
2082 +        final FailingRunnable r3 = new FailingRunnable(m);
2083  
2084 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2085 <        if (fFirst) {
2086 <            f.complete(v1);
2087 <            g.complete(v2);
2088 <        } else {
2089 <            g.complete(v2);
2090 <            f.complete(v1);
2091 <        }
2092 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2084 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2085 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2086 >        final Integer w1 =  fFirst ? v1 : v2;
2087 >        final Integer w2 = !fFirst ? v1 : v2;
2088 >
2089 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2090 >        assertTrue(fst.complete(w1));
2091 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2092 >        assertTrue(snd.complete(w2));
2093 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2094  
2095          checkCompletedWithWrappedCFException(h1);
2096          checkCompletedWithWrappedCFException(h2);
2097 +        checkCompletedWithWrappedCFException(h3);
2098 +        r1.assertInvoked();
2099 +        r2.assertInvoked();
2100 +        r3.assertInvoked();
2101          checkCompletedNormally(f, v1);
2102          checkCompletedNormally(g, v2);
2103      }}
# Line 1753 | Line 2171 | public class CompletableFutureTest exten
2171          rs[0].assertNotInvoked();
2172          rs[1].assertNotInvoked();
2173          f.completeExceptionally(ex);
2174 <        checkCompletedWithWrappedCFException(h0, ex);
2175 <        checkCompletedWithWrappedCFException(h1, ex);
2174 >        checkCompletedWithWrappedException(h0, ex);
2175 >        checkCompletedWithWrappedException(h1, ex);
2176          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2177          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2178 <        checkCompletedWithWrappedCFException(h2, ex);
2179 <        checkCompletedWithWrappedCFException(h3, ex);
2178 >        checkCompletedWithWrappedException(h2, ex);
2179 >        checkCompletedWithWrappedException(h3, ex);
2180          g.complete(v1);
2181  
2182          // unspecified behavior - both source completions available
# Line 1766 | Line 2184 | public class CompletableFutureTest exten
2184          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2185          try {
2186              assertEquals(inc(v1), h4.join());
2187 <            rs[4].assertInvoked();
2187 >            rs[4].assertValue(inc(v1));
2188          } catch (CompletionException ok) {
2189 <            checkCompletedWithWrappedCFException(h4, ex);
2189 >            checkCompletedWithWrappedException(h4, ex);
2190              rs[4].assertNotInvoked();
2191          }
2192          try {
2193              assertEquals(inc(v1), h5.join());
2194 <            rs[5].assertInvoked();
2194 >            rs[5].assertValue(inc(v1));
2195          } catch (CompletionException ok) {
2196 <            checkCompletedWithWrappedCFException(h5, ex);
2196 >            checkCompletedWithWrappedException(h5, ex);
2197              rs[5].assertNotInvoked();
2198          }
2199  
2200 <        checkCompletedWithWrappedCFException(f, ex);
2200 >        checkCompletedExceptionally(f, ex);
2201          checkCompletedNormally(g, v1);
2202 <        checkCompletedWithWrappedCFException(h0, ex);
2203 <        checkCompletedWithWrappedCFException(h1, ex);
2204 <        checkCompletedWithWrappedCFException(h2, ex);
2205 <        checkCompletedWithWrappedCFException(h3, ex);
2206 <        checkCompletedWithWrappedCFException(h4, ex);
2202 >        checkCompletedWithWrappedException(h0, ex);
2203 >        checkCompletedWithWrappedException(h1, ex);
2204 >        checkCompletedWithWrappedException(h2, ex);
2205 >        checkCompletedWithWrappedException(h3, ex);
2206 >        checkCompletedWithWrappedException(h4, ex);
2207          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2208      }}
2209  
2210 +    public void testApplyToEither_exceptionalCompletion2() {
2211 +        for (ExecutionMode m : ExecutionMode.values())
2212 +        for (boolean fFirst : new boolean[] { true, false })
2213 +        for (Integer v1 : new Integer[] { 1, null })
2214 +    {
2215 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2216 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2217 +        final CFException ex = new CFException();
2218 +        final IncFunction[] rs = new IncFunction[6];
2219 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2220 +
2221 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2222 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2223 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2224 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2225 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2226 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2227 +
2228 +        // unspecified behavior - both source completions available
2229 +        try {
2230 +            assertEquals(inc(v1), h0.join());
2231 +            rs[0].assertValue(inc(v1));
2232 +        } catch (CompletionException ok) {
2233 +            checkCompletedWithWrappedException(h0, ex);
2234 +            rs[0].assertNotInvoked();
2235 +        }
2236 +        try {
2237 +            assertEquals(inc(v1), h1.join());
2238 +            rs[1].assertValue(inc(v1));
2239 +        } catch (CompletionException ok) {
2240 +            checkCompletedWithWrappedException(h1, ex);
2241 +            rs[1].assertNotInvoked();
2242 +        }
2243 +        try {
2244 +            assertEquals(inc(v1), h2.join());
2245 +            rs[2].assertValue(inc(v1));
2246 +        } catch (CompletionException ok) {
2247 +            checkCompletedWithWrappedException(h2, ex);
2248 +            rs[2].assertNotInvoked();
2249 +        }
2250 +        try {
2251 +            assertEquals(inc(v1), h3.join());
2252 +            rs[3].assertValue(inc(v1));
2253 +        } catch (CompletionException ok) {
2254 +            checkCompletedWithWrappedException(h3, ex);
2255 +            rs[3].assertNotInvoked();
2256 +        }
2257 +
2258 +        checkCompletedNormally(f, v1);
2259 +        checkCompletedExceptionally(g, ex);
2260 +    }}
2261 +
2262      /**
2263       * applyToEither result completes exceptionally if either source cancelled
2264       */
# Line 1822 | Line 2292 | public class CompletableFutureTest exten
2292          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2293          try {
2294              assertEquals(inc(v1), h4.join());
2295 <            rs[4].assertInvoked();
2295 >            rs[4].assertValue(inc(v1));
2296          } catch (CompletionException ok) {
2297              checkCompletedWithWrappedCancellationException(h4);
2298              rs[4].assertNotInvoked();
2299          }
2300          try {
2301              assertEquals(inc(v1), h5.join());
2302 <            rs[5].assertInvoked();
2302 >            rs[5].assertValue(inc(v1));
2303          } catch (CompletionException ok) {
2304              checkCompletedWithWrappedCancellationException(h5);
2305              rs[5].assertNotInvoked();
# Line 1844 | Line 2314 | public class CompletableFutureTest exten
2314          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2315      }}
2316  
2317 +    public void testApplyToEither_sourceCancelled2() {
2318 +        for (ExecutionMode m : ExecutionMode.values())
2319 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2320 +        for (boolean fFirst : new boolean[] { true, false })
2321 +        for (Integer v1 : new Integer[] { 1, null })
2322 +    {
2323 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2324 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2325 +        final IncFunction[] rs = new IncFunction[6];
2326 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2327 +
2328 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2329 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2330 +        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2331 +        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2332 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2333 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2334 +
2335 +        // unspecified behavior - both source completions available
2336 +        try {
2337 +            assertEquals(inc(v1), h0.join());
2338 +            rs[0].assertValue(inc(v1));
2339 +        } catch (CompletionException ok) {
2340 +            checkCompletedWithWrappedCancellationException(h0);
2341 +            rs[0].assertNotInvoked();
2342 +        }
2343 +        try {
2344 +            assertEquals(inc(v1), h1.join());
2345 +            rs[1].assertValue(inc(v1));
2346 +        } catch (CompletionException ok) {
2347 +            checkCompletedWithWrappedCancellationException(h1);
2348 +            rs[1].assertNotInvoked();
2349 +        }
2350 +        try {
2351 +            assertEquals(inc(v1), h2.join());
2352 +            rs[2].assertValue(inc(v1));
2353 +        } catch (CompletionException ok) {
2354 +            checkCompletedWithWrappedCancellationException(h2);
2355 +            rs[2].assertNotInvoked();
2356 +        }
2357 +        try {
2358 +            assertEquals(inc(v1), h3.join());
2359 +            rs[3].assertValue(inc(v1));
2360 +        } catch (CompletionException ok) {
2361 +            checkCompletedWithWrappedCancellationException(h3);
2362 +            rs[3].assertNotInvoked();
2363 +        }
2364 +
2365 +        checkCompletedNormally(f, v1);
2366 +        checkCancelled(g);
2367 +    }}
2368 +
2369      /**
2370       * applyToEither result completes exceptionally if action does
2371       */
# Line 1896 | Line 2418 | public class CompletableFutureTest exten
2418      {
2419          final CompletableFuture<Integer> f = new CompletableFuture<>();
2420          final CompletableFuture<Integer> g = new CompletableFuture<>();
2421 <        final IncAction[] rs = new IncAction[6];
2422 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2421 >        final NoopConsumer[] rs = new NoopConsumer[6];
2422 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2423  
2424          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2425          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 1908 | Line 2430 | public class CompletableFutureTest exten
2430          f.complete(v1);
2431          checkCompletedNormally(h0, null);
2432          checkCompletedNormally(h1, null);
2433 <        rs[0].assertValue(inc(v1));
2434 <        rs[1].assertValue(inc(v1));
2433 >        rs[0].assertValue(v1);
2434 >        rs[1].assertValue(v1);
2435          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2436          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2437          checkCompletedNormally(h2, null);
2438          checkCompletedNormally(h3, null);
2439 <        rs[2].assertValue(inc(v1));
2440 <        rs[3].assertValue(inc(v1));
2439 >        rs[2].assertValue(v1);
2440 >        rs[3].assertValue(v1);
2441          g.complete(v2);
2442  
2443          // unspecified behavior - both source completions available
# Line 1923 | Line 2445 | public class CompletableFutureTest exten
2445          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2446          checkCompletedNormally(h4, null);
2447          checkCompletedNormally(h5, null);
2448 <        assertTrue(Objects.equals(inc(v1), rs[4].value) ||
2449 <                   Objects.equals(inc(v2), rs[4].value));
2450 <        assertTrue(Objects.equals(inc(v1), rs[5].value) ||
2451 <                   Objects.equals(inc(v2), rs[5].value));
2448 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2449 >                   Objects.equals(v2, rs[4].value));
2450 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2451 >                   Objects.equals(v2, rs[5].value));
2452  
2453          checkCompletedNormally(f, v1);
2454          checkCompletedNormally(g, v2);
# Line 1934 | Line 2456 | public class CompletableFutureTest exten
2456          checkCompletedNormally(h1, null);
2457          checkCompletedNormally(h2, null);
2458          checkCompletedNormally(h3, null);
2459 <        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2459 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2460      }}
2461  
2462      /**
# Line 1948 | Line 2470 | public class CompletableFutureTest exten
2470          final CompletableFuture<Integer> f = new CompletableFuture<>();
2471          final CompletableFuture<Integer> g = new CompletableFuture<>();
2472          final CFException ex = new CFException();
2473 <        final IncAction[] rs = new IncAction[6];
2474 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2473 >        final NoopConsumer[] rs = new NoopConsumer[6];
2474 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2475  
2476          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2477          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 1958 | Line 2480 | public class CompletableFutureTest exten
2480          rs[0].assertNotInvoked();
2481          rs[1].assertNotInvoked();
2482          f.completeExceptionally(ex);
2483 <        checkCompletedWithWrappedCFException(h0, ex);
2484 <        checkCompletedWithWrappedCFException(h1, ex);
2483 >        checkCompletedWithWrappedException(h0, ex);
2484 >        checkCompletedWithWrappedException(h1, ex);
2485          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2486          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2487 <        checkCompletedWithWrappedCFException(h2, ex);
2488 <        checkCompletedWithWrappedCFException(h3, ex);
2487 >        checkCompletedWithWrappedException(h2, ex);
2488 >        checkCompletedWithWrappedException(h3, ex);
2489  
2490          g.complete(v1);
2491  
# Line 1972 | Line 2494 | public class CompletableFutureTest exten
2494          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2495          try {
2496              assertNull(h4.join());
2497 <            rs[4].assertValue(inc(v1));
2497 >            rs[4].assertValue(v1);
2498          } catch (CompletionException ok) {
2499 <            checkCompletedWithWrappedCFException(h4, ex);
2499 >            checkCompletedWithWrappedException(h4, ex);
2500              rs[4].assertNotInvoked();
2501          }
2502          try {
2503              assertNull(h5.join());
2504 <            rs[5].assertValue(inc(v1));
2504 >            rs[5].assertValue(v1);
2505          } catch (CompletionException ok) {
2506 <            checkCompletedWithWrappedCFException(h5, ex);
2506 >            checkCompletedWithWrappedException(h5, ex);
2507              rs[5].assertNotInvoked();
2508          }
2509  
2510 <        checkCompletedWithWrappedCFException(f, ex);
2510 >        checkCompletedExceptionally(f, ex);
2511          checkCompletedNormally(g, v1);
2512 <        checkCompletedWithWrappedCFException(h0, ex);
2513 <        checkCompletedWithWrappedCFException(h1, ex);
2514 <        checkCompletedWithWrappedCFException(h2, ex);
2515 <        checkCompletedWithWrappedCFException(h3, ex);
2516 <        checkCompletedWithWrappedCFException(h4, ex);
2512 >        checkCompletedWithWrappedException(h0, ex);
2513 >        checkCompletedWithWrappedException(h1, ex);
2514 >        checkCompletedWithWrappedException(h2, ex);
2515 >        checkCompletedWithWrappedException(h3, ex);
2516 >        checkCompletedWithWrappedException(h4, ex);
2517          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2518      }}
2519  
2520 +    public void testAcceptEither_exceptionalCompletion2() {
2521 +        for (ExecutionMode m : ExecutionMode.values())
2522 +        for (boolean fFirst : new boolean[] { true, false })
2523 +        for (Integer v1 : new Integer[] { 1, null })
2524 +    {
2525 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2526 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2527 +        final CFException ex = new CFException();
2528 +        final NoopConsumer[] rs = new NoopConsumer[6];
2529 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2530 +
2531 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2532 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2533 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2534 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2535 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2536 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2537 +
2538 +        // unspecified behavior - both source completions available
2539 +        try {
2540 +            assertEquals(null, h0.join());
2541 +            rs[0].assertValue(v1);
2542 +        } catch (CompletionException ok) {
2543 +            checkCompletedWithWrappedException(h0, ex);
2544 +            rs[0].assertNotInvoked();
2545 +        }
2546 +        try {
2547 +            assertEquals(null, h1.join());
2548 +            rs[1].assertValue(v1);
2549 +        } catch (CompletionException ok) {
2550 +            checkCompletedWithWrappedException(h1, ex);
2551 +            rs[1].assertNotInvoked();
2552 +        }
2553 +        try {
2554 +            assertEquals(null, h2.join());
2555 +            rs[2].assertValue(v1);
2556 +        } catch (CompletionException ok) {
2557 +            checkCompletedWithWrappedException(h2, ex);
2558 +            rs[2].assertNotInvoked();
2559 +        }
2560 +        try {
2561 +            assertEquals(null, h3.join());
2562 +            rs[3].assertValue(v1);
2563 +        } catch (CompletionException ok) {
2564 +            checkCompletedWithWrappedException(h3, ex);
2565 +            rs[3].assertNotInvoked();
2566 +        }
2567 +
2568 +        checkCompletedNormally(f, v1);
2569 +        checkCompletedExceptionally(g, ex);
2570 +    }}
2571 +
2572      /**
2573       * acceptEither result completes exceptionally if either source cancelled
2574       */
# Line 2005 | Line 2579 | public class CompletableFutureTest exten
2579      {
2580          final CompletableFuture<Integer> f = new CompletableFuture<>();
2581          final CompletableFuture<Integer> g = new CompletableFuture<>();
2582 <        final IncAction[] rs = new IncAction[6];
2583 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2582 >        final NoopConsumer[] rs = new NoopConsumer[6];
2583 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2584  
2585          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2586          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 2029 | Line 2603 | public class CompletableFutureTest exten
2603          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2604          try {
2605              assertNull(h4.join());
2606 <            rs[4].assertValue(inc(v1));
2606 >            rs[4].assertValue(v1);
2607          } catch (CompletionException ok) {
2608              checkCompletedWithWrappedCancellationException(h4);
2609              rs[4].assertNotInvoked();
2610          }
2611          try {
2612              assertNull(h5.join());
2613 <            rs[5].assertValue(inc(v1));
2613 >            rs[5].assertValue(v1);
2614          } catch (CompletionException ok) {
2615              checkCompletedWithWrappedCancellationException(h5);
2616              rs[5].assertNotInvoked();
# Line 2096 | Line 2670 | public class CompletableFutureTest exten
2670       * runAfterEither result completes normally after normal completion
2671       * of either source
2672       */
2673 <    public void testRunAfterEither_normalCompletion1() {
2673 >    public void testRunAfterEither_normalCompletion() {
2674          for (ExecutionMode m : ExecutionMode.values())
2675          for (Integer v1 : new Integer[] { 1, null })
2676          for (Integer v2 : new Integer[] { 2, null })
2677      {
2678          final CompletableFuture<Integer> f = new CompletableFuture<>();
2679          final CompletableFuture<Integer> g = new CompletableFuture<>();
2680 <        final Noop r = new Noop(m);
2681 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2680 >        final Noop[] rs = new Noop[6];
2681 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2682  
2683 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2684 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2685 +        checkIncomplete(h0);
2686 +        checkIncomplete(h1);
2687 +        rs[0].assertNotInvoked();
2688 +        rs[1].assertNotInvoked();
2689          f.complete(v1);
2690 <        checkCompletedNormally(h, null);
2691 <        r.assertInvoked();
2692 <        g.complete(v2);
2693 <
2694 <        checkCompletedNormally(f, v1);
2695 <        checkCompletedNormally(g, v2);
2696 <        checkCompletedNormally(h, null);
2697 <        r.assertInvoked();
2698 <    }}
2699 <
2120 <    public void testRunAfterEither_normalCompletion2() {
2121 <        for (ExecutionMode m : ExecutionMode.values())
2122 <        for (Integer v1 : new Integer[] { 1, null })
2123 <        for (Integer v2 : new Integer[] { 2, null })
2124 <    {
2125 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2126 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2127 <        final Noop r = new Noop(m);
2128 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2690 >        checkCompletedNormally(h0, null);
2691 >        checkCompletedNormally(h1, null);
2692 >        rs[0].assertInvoked();
2693 >        rs[1].assertInvoked();
2694 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2695 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2696 >        checkCompletedNormally(h2, null);
2697 >        checkCompletedNormally(h3, null);
2698 >        rs[2].assertInvoked();
2699 >        rs[3].assertInvoked();
2700  
2701          g.complete(v2);
2131        checkCompletedNormally(h, null);
2132        r.assertInvoked();
2133        f.complete(v1);
2134
2135        checkCompletedNormally(f, v1);
2136        checkCompletedNormally(g, v2);
2137        checkCompletedNormally(h, null);
2138        r.assertInvoked();
2139        }}
2140
2141    public void testRunAfterEither_normalCompletion3() {
2142        for (ExecutionMode m : ExecutionMode.values())
2143        for (Integer v1 : new Integer[] { 1, null })
2144        for (Integer v2 : new Integer[] { 2, null })
2145    {
2146        final CompletableFuture<Integer> f = new CompletableFuture<>();
2147        final CompletableFuture<Integer> g = new CompletableFuture<>();
2148        final Noop r = new Noop(m);
2702  
2703 <        f.complete(v1);
2704 <        g.complete(v2);
2152 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2703 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2704 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2705  
2154        checkCompletedNormally(h, null);
2706          checkCompletedNormally(f, v1);
2707          checkCompletedNormally(g, v2);
2708 <        r.assertInvoked();
2708 >        checkCompletedNormally(h0, null);
2709 >        checkCompletedNormally(h1, null);
2710 >        checkCompletedNormally(h2, null);
2711 >        checkCompletedNormally(h3, null);
2712 >        checkCompletedNormally(h4, null);
2713 >        checkCompletedNormally(h5, null);
2714 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2715      }}
2716  
2717      /**
2718       * runAfterEither result completes exceptionally after exceptional
2719       * completion of either source
2720       */
2721 <    public void testRunAfterEither_exceptionalCompletion1() {
2721 >    public void testRunAfterEither_exceptionalCompletion() {
2722          for (ExecutionMode m : ExecutionMode.values())
2723          for (Integer v1 : new Integer[] { 1, null })
2724      {
2725          final CompletableFuture<Integer> f = new CompletableFuture<>();
2726          final CompletableFuture<Integer> g = new CompletableFuture<>();
2170        final Noop r = new Noop(m);
2171        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2727          final CFException ex = new CFException();
2728 +        final Noop[] rs = new Noop[6];
2729 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2730  
2731 <        f.completeExceptionally(ex);
2732 <        checkCompletedWithWrappedCFException(h, ex);
2733 <        g.complete(v1);
2734 <
2735 <        r.assertNotInvoked();
2736 <        checkCompletedNormally(g, v1);
2737 <        checkCompletedWithWrappedCFException(f, ex);
2738 <        checkCompletedWithWrappedCFException(h, ex);
2739 <    }}
2740 <
2741 <    public void testRunAfterEither_exceptionalCompletion2() {
2742 <        for (ExecutionMode m : ExecutionMode.values())
2743 <        for (Integer v1 : new Integer[] { 1, null })
2187 <    {
2188 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2189 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2190 <        final Noop r = new Noop(m);
2191 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2192 <        final CFException ex = new CFException();
2193 <
2194 <        g.completeExceptionally(ex);
2195 <        checkCompletedWithWrappedCFException(h, ex);
2196 <        f.complete(v1);
2197 <
2198 <        r.assertNotInvoked();
2199 <        checkCompletedNormally(f, v1);
2200 <        checkCompletedWithWrappedCFException(g, ex);
2201 <        checkCompletedWithWrappedCFException(h, ex);
2202 <    }}
2203 <
2204 <    public void testRunAfterEither_exceptionalCompletion3() {
2205 <        for (ExecutionMode m : ExecutionMode.values())
2206 <        for (Integer v1 : new Integer[] { 1, null })
2207 <    {
2208 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2209 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2210 <        final Noop r = new Noop(m);
2211 <        final CFException ex = new CFException();
2731 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2732 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2733 >        checkIncomplete(h0);
2734 >        checkIncomplete(h1);
2735 >        rs[0].assertNotInvoked();
2736 >        rs[1].assertNotInvoked();
2737 >        assertTrue(f.completeExceptionally(ex));
2738 >        checkCompletedWithWrappedException(h0, ex);
2739 >        checkCompletedWithWrappedException(h1, ex);
2740 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2741 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2742 >        checkCompletedWithWrappedException(h2, ex);
2743 >        checkCompletedWithWrappedException(h3, ex);
2744  
2745 <        g.completeExceptionally(ex);
2214 <        f.complete(v1);
2215 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2745 >        assertTrue(g.complete(v1));
2746  
2747 <        // unspecified behavior
2748 <        Integer v;
2747 >        // unspecified behavior - both source completions available
2748 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2749 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2750          try {
2751 <            assertNull(h.join());
2752 <            r.assertInvoked();
2751 >            assertNull(h4.join());
2752 >            rs[4].assertInvoked();
2753          } catch (CompletionException ok) {
2754 <            checkCompletedWithWrappedCFException(h, ex);
2755 <            r.assertNotInvoked();
2754 >            checkCompletedWithWrappedException(h4, ex);
2755 >            rs[4].assertNotInvoked();
2756          }
2226
2227        checkCompletedWithWrappedCFException(g, ex);
2228        checkCompletedNormally(f, v1);
2229    }}
2230
2231    public void testRunAfterEither_exceptionalCompletion4() {
2232        for (ExecutionMode m : ExecutionMode.values())
2233        for (Integer v1 : new Integer[] { 1, null })
2234    {
2235        final CompletableFuture<Integer> f = new CompletableFuture<>();
2236        final CompletableFuture<Integer> g = new CompletableFuture<>();
2237        final Noop r = new Noop(m);
2238        final CFException ex = new CFException();
2239
2240        f.completeExceptionally(ex);
2241        g.complete(v1);
2242        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2243
2244        // unspecified behavior
2245        Integer v;
2757          try {
2758 <            assertNull(h.join());
2759 <            r.assertInvoked();
2758 >            assertNull(h5.join());
2759 >            rs[5].assertInvoked();
2760          } catch (CompletionException ok) {
2761 <            checkCompletedWithWrappedCFException(h, ex);
2762 <            r.assertNotInvoked();
2761 >            checkCompletedWithWrappedException(h5, ex);
2762 >            rs[5].assertNotInvoked();
2763          }
2764  
2765 <        checkCompletedWithWrappedCFException(f, ex);
2765 >        checkCompletedExceptionally(f, ex);
2766          checkCompletedNormally(g, v1);
2767 +        checkCompletedWithWrappedException(h0, ex);
2768 +        checkCompletedWithWrappedException(h1, ex);
2769 +        checkCompletedWithWrappedException(h2, ex);
2770 +        checkCompletedWithWrappedException(h3, ex);
2771 +        checkCompletedWithWrappedException(h4, ex);
2772 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2773      }}
2774  
2775 <    /**
2259 <     * runAfterEither result completes exceptionally if action does
2260 <     */
2261 <    public void testRunAfterEither_actionFailed1() {
2775 >    public void testRunAfterEither_exceptionalCompletion2() {
2776          for (ExecutionMode m : ExecutionMode.values())
2777 +        for (boolean fFirst : new boolean[] { true, false })
2778          for (Integer v1 : new Integer[] { 1, null })
2264        for (Integer v2 : new Integer[] { 2, null })
2779      {
2780          final CompletableFuture<Integer> f = new CompletableFuture<>();
2781          final CompletableFuture<Integer> g = new CompletableFuture<>();
2782 <        final FailingRunnable r = new FailingRunnable(m);
2783 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2782 >        final CFException ex = new CFException();
2783 >        final Noop[] rs = new Noop[6];
2784 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2785  
2786 <        f.complete(v1);
2787 <        checkCompletedWithWrappedCFException(h);
2788 <        g.complete(v2);
2789 <        checkCompletedNormally(f, v1);
2790 <        checkCompletedNormally(g, v2);
2791 <    }}
2786 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2787 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2788 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2789 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2790 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2791 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2792  
2793 <    public void testRunAfterEither_actionFailed2() {
2794 <        for (ExecutionMode m : ExecutionMode.values())
2795 <        for (Integer v1 : new Integer[] { 1, null })
2796 <        for (Integer v2 : new Integer[] { 2, null })
2797 <    {
2798 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2799 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2800 <        final FailingRunnable r = new FailingRunnable(m);
2801 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2793 >        // unspecified behavior - both source completions available
2794 >        try {
2795 >            assertEquals(null, h0.join());
2796 >            rs[0].assertInvoked();
2797 >        } catch (CompletionException ok) {
2798 >            checkCompletedWithWrappedException(h0, ex);
2799 >            rs[0].assertNotInvoked();
2800 >        }
2801 >        try {
2802 >            assertEquals(null, h1.join());
2803 >            rs[1].assertInvoked();
2804 >        } catch (CompletionException ok) {
2805 >            checkCompletedWithWrappedException(h1, ex);
2806 >            rs[1].assertNotInvoked();
2807 >        }
2808 >        try {
2809 >            assertEquals(null, h2.join());
2810 >            rs[2].assertInvoked();
2811 >        } catch (CompletionException ok) {
2812 >            checkCompletedWithWrappedException(h2, ex);
2813 >            rs[2].assertNotInvoked();
2814 >        }
2815 >        try {
2816 >            assertEquals(null, h3.join());
2817 >            rs[3].assertInvoked();
2818 >        } catch (CompletionException ok) {
2819 >            checkCompletedWithWrappedException(h3, ex);
2820 >            rs[3].assertNotInvoked();
2821 >        }
2822  
2288        g.complete(v2);
2289        checkCompletedWithWrappedCFException(h);
2290        f.complete(v1);
2823          checkCompletedNormally(f, v1);
2824 <        checkCompletedNormally(g, v2);
2824 >        checkCompletedExceptionally(g, ex);
2825      }}
2826  
2827      /**
2828       * runAfterEither result completes exceptionally if either source cancelled
2829       */
2830 <    public void testRunAfterEither_sourceCancelled1() {
2299 <        for (ExecutionMode m : ExecutionMode.values())
2300 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2301 <        for (Integer v1 : new Integer[] { 1, null })
2302 <    {
2303 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2304 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2305 <        final Noop r = new Noop(m);
2306 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2307 <
2308 <        assertTrue(f.cancel(mayInterruptIfRunning));
2309 <        checkCompletedWithWrappedCancellationException(h);
2310 <        g.complete(v1);
2311 <
2312 <        checkCancelled(f);
2313 <        r.assertNotInvoked();
2314 <        checkCompletedNormally(g, v1);
2315 <        checkCompletedWithWrappedCancellationException(h);
2316 <    }}
2317 <
2318 <    public void testRunAfterEither_sourceCancelled2() {
2830 >    public void testRunAfterEither_sourceCancelled() {
2831          for (ExecutionMode m : ExecutionMode.values())
2832          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2833          for (Integer v1 : new Integer[] { 1, null })
2834      {
2835          final CompletableFuture<Integer> f = new CompletableFuture<>();
2836          final CompletableFuture<Integer> g = new CompletableFuture<>();
2837 <        final Noop r = new Noop(m);
2838 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2327 <
2328 <        assertTrue(g.cancel(mayInterruptIfRunning));
2329 <        checkCompletedWithWrappedCancellationException(h);
2330 <        f.complete(v1);
2837 >        final Noop[] rs = new Noop[6];
2838 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2839  
2840 <        checkCancelled(g);
2841 <        r.assertNotInvoked();
2842 <        checkCompletedNormally(f, v1);
2843 <        checkCompletedWithWrappedCancellationException(h);
2844 <    }}
2845 <
2846 <    public void testRunAfterEither_sourceCancelled3() {
2847 <        for (ExecutionMode m : ExecutionMode.values())
2848 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2849 <        for (Integer v1 : new Integer[] { 1, null })
2850 <    {
2851 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2852 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2345 <        final Noop r = new Noop(m);
2840 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2841 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2842 >        checkIncomplete(h0);
2843 >        checkIncomplete(h1);
2844 >        rs[0].assertNotInvoked();
2845 >        rs[1].assertNotInvoked();
2846 >        f.cancel(mayInterruptIfRunning);
2847 >        checkCompletedWithWrappedCancellationException(h0);
2848 >        checkCompletedWithWrappedCancellationException(h1);
2849 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2850 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2851 >        checkCompletedWithWrappedCancellationException(h2);
2852 >        checkCompletedWithWrappedCancellationException(h3);
2853  
2854 <        assertTrue(g.cancel(mayInterruptIfRunning));
2348 <        f.complete(v1);
2349 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2854 >        assertTrue(g.complete(v1));
2855  
2856 <        // unspecified behavior
2857 <        Integer v;
2856 >        // unspecified behavior - both source completions available
2857 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2858 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2859          try {
2860 <            assertNull(h.join());
2861 <            r.assertInvoked();
2860 >            assertNull(h4.join());
2861 >            rs[4].assertInvoked();
2862          } catch (CompletionException ok) {
2863 <            checkCompletedWithWrappedCancellationException(h);
2864 <            r.assertNotInvoked();
2863 >            checkCompletedWithWrappedCancellationException(h4);
2864 >            rs[4].assertNotInvoked();
2865 >        }
2866 >        try {
2867 >            assertNull(h5.join());
2868 >            rs[5].assertInvoked();
2869 >        } catch (CompletionException ok) {
2870 >            checkCompletedWithWrappedCancellationException(h5);
2871 >            rs[5].assertNotInvoked();
2872          }
2873  
2874 <        checkCancelled(g);
2875 <        checkCompletedNormally(f, v1);
2874 >        checkCancelled(f);
2875 >        checkCompletedNormally(g, v1);
2876 >        checkCompletedWithWrappedCancellationException(h0);
2877 >        checkCompletedWithWrappedCancellationException(h1);
2878 >        checkCompletedWithWrappedCancellationException(h2);
2879 >        checkCompletedWithWrappedCancellationException(h3);
2880 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2881      }}
2882  
2883 <    public void testRunAfterEither_sourceCancelled4() {
2883 >    /**
2884 >     * runAfterEither result completes exceptionally if action does
2885 >     */
2886 >    public void testRunAfterEither_actionFailed() {
2887          for (ExecutionMode m : ExecutionMode.values())
2367        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2888          for (Integer v1 : new Integer[] { 1, null })
2889 +        for (Integer v2 : new Integer[] { 2, null })
2890      {
2891          final CompletableFuture<Integer> f = new CompletableFuture<>();
2892          final CompletableFuture<Integer> g = new CompletableFuture<>();
2893 <        final Noop r = new Noop(m);
2893 >        final FailingRunnable[] rs = new FailingRunnable[6];
2894 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2895  
2896 <        assertTrue(f.cancel(mayInterruptIfRunning));
2897 <        g.complete(v1);
2898 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2899 <
2900 <        // unspecified behavior
2901 <        Integer v;
2902 <        try {
2903 <            assertNull(h.join());
2904 <            r.assertInvoked();
2905 <        } catch (CompletionException ok) {
2906 <            checkCompletedWithWrappedCancellationException(h);
2907 <            r.assertNotInvoked();
2908 <        }
2896 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2897 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2898 >        assertTrue(f.complete(v1));
2899 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2900 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2901 >        checkCompletedWithWrappedCFException(h0);
2902 >        checkCompletedWithWrappedCFException(h1);
2903 >        checkCompletedWithWrappedCFException(h2);
2904 >        checkCompletedWithWrappedCFException(h3);
2905 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2906 >        assertTrue(g.complete(v2));
2907 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2908 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2909 >        checkCompletedWithWrappedCFException(h4);
2910 >        checkCompletedWithWrappedCFException(h5);
2911  
2912 <        checkCancelled(f);
2913 <        checkCompletedNormally(g, v1);
2912 >        checkCompletedNormally(f, v1);
2913 >        checkCompletedNormally(g, v2);
2914 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2915      }}
2916  
2917      /**
# Line 2399 | Line 2924 | public class CompletableFutureTest exten
2924      {
2925          final CompletableFuture<Integer> f = new CompletableFuture<>();
2926          final CompletableFutureInc r = new CompletableFutureInc(m);
2927 <        if (!createIncomplete) f.complete(v1);
2927 >        if (!createIncomplete) assertTrue(f.complete(v1));
2928          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2929 <        if (createIncomplete) f.complete(v1);
2929 >        if (createIncomplete) assertTrue(f.complete(v1));
2930  
2931          checkCompletedNormally(g, inc(v1));
2932          checkCompletedNormally(f, v1);
2933 <        r.assertInvoked();
2933 >        r.assertValue(v1);
2934      }}
2935  
2936      /**
# Line 2423 | Line 2948 | public class CompletableFutureTest exten
2948          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2949          if (createIncomplete) f.completeExceptionally(ex);
2950  
2951 <        checkCompletedWithWrappedCFException(g, ex);
2952 <        checkCompletedWithWrappedCFException(f, ex);
2951 >        checkCompletedWithWrappedException(g, ex);
2952 >        checkCompletedExceptionally(f, ex);
2953          r.assertNotInvoked();
2954      }}
2955  
# Line 2439 | Line 2964 | public class CompletableFutureTest exten
2964          final CompletableFuture<Integer> f = new CompletableFuture<>();
2965          final FailingCompletableFutureFunction r
2966              = new FailingCompletableFutureFunction(m);
2967 <        if (!createIncomplete) f.complete(v1);
2967 >        if (!createIncomplete) assertTrue(f.complete(v1));
2968          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2969 <        if (createIncomplete) f.complete(v1);
2969 >        if (createIncomplete) assertTrue(f.complete(v1));
2970  
2971          checkCompletedWithWrappedCFException(g);
2972          checkCompletedNormally(f, v1);
# Line 2468 | Line 2993 | public class CompletableFutureTest exten
2993          checkCancelled(f);
2994      }}
2995  
2996 +    /**
2997 +     * thenCompose result completes exceptionally if the result of the action does
2998 +     */
2999 +    public void testThenCompose_actionReturnsFailingFuture() {
3000 +        for (ExecutionMode m : ExecutionMode.values())
3001 +        for (int order = 0; order < 6; order++)
3002 +        for (Integer v1 : new Integer[] { 1, null })
3003 +    {
3004 +        final CFException ex = new CFException();
3005 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3006 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3007 +        final CompletableFuture<Integer> h;
3008 +        // Test all permutations of orders
3009 +        switch (order) {
3010 +        case 0:
3011 +            assertTrue(f.complete(v1));
3012 +            assertTrue(g.completeExceptionally(ex));
3013 +            h = m.thenCompose(f, (x -> g));
3014 +            break;
3015 +        case 1:
3016 +            assertTrue(f.complete(v1));
3017 +            h = m.thenCompose(f, (x -> g));
3018 +            assertTrue(g.completeExceptionally(ex));
3019 +            break;
3020 +        case 2:
3021 +            assertTrue(g.completeExceptionally(ex));
3022 +            assertTrue(f.complete(v1));
3023 +            h = m.thenCompose(f, (x -> g));
3024 +            break;
3025 +        case 3:
3026 +            assertTrue(g.completeExceptionally(ex));
3027 +            h = m.thenCompose(f, (x -> g));
3028 +            assertTrue(f.complete(v1));
3029 +            break;
3030 +        case 4:
3031 +            h = m.thenCompose(f, (x -> g));
3032 +            assertTrue(f.complete(v1));
3033 +            assertTrue(g.completeExceptionally(ex));
3034 +            break;
3035 +        case 5:
3036 +            h = m.thenCompose(f, (x -> g));
3037 +            assertTrue(f.complete(v1));
3038 +            assertTrue(g.completeExceptionally(ex));
3039 +            break;
3040 +        default: throw new AssertionError();
3041 +        }
3042 +
3043 +        checkCompletedExceptionally(g, ex);
3044 +        checkCompletedWithWrappedException(h, ex);
3045 +        checkCompletedNormally(f, v1);
3046 +    }}
3047 +
3048      // other static methods
3049  
3050      /**
# Line 2484 | Line 3061 | public class CompletableFutureTest exten
3061       * when all components complete normally
3062       */
3063      public void testAllOf_normal() throws Exception {
3064 <        for (int k = 1; k < 20; ++k) {
3064 >        for (int k = 1; k < 10; k++) {
3065              CompletableFuture<Integer>[] fs
3066                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3067 <            for (int i = 0; i < k; ++i)
3067 >            for (int i = 0; i < k; i++)
3068                  fs[i] = new CompletableFuture<>();
3069              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3070 <            for (int i = 0; i < k; ++i) {
3070 >            for (int i = 0; i < k; i++) {
3071                  checkIncomplete(f);
3072                  checkIncomplete(CompletableFuture.allOf(fs));
3073                  fs[i].complete(one);
# Line 2501 | Line 3078 | public class CompletableFutureTest exten
3078      }
3079  
3080      public void testAllOf_backwards() throws Exception {
3081 <        for (int k = 1; k < 20; ++k) {
3081 >        for (int k = 1; k < 10; k++) {
3082              CompletableFuture<Integer>[] fs
3083                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3084 <            for (int i = 0; i < k; ++i)
3084 >            for (int i = 0; i < k; i++)
3085                  fs[i] = new CompletableFuture<>();
3086              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3087              for (int i = k - 1; i >= 0; i--) {
# Line 2517 | Line 3094 | public class CompletableFutureTest exten
3094          }
3095      }
3096  
3097 +    public void testAllOf_exceptional() throws Exception {
3098 +        for (int k = 1; k < 10; k++) {
3099 +            CompletableFuture<Integer>[] fs
3100 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3101 +            CFException ex = new CFException();
3102 +            for (int i = 0; i < k; i++)
3103 +                fs[i] = new CompletableFuture<>();
3104 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3105 +            for (int i = 0; i < k; i++) {
3106 +                checkIncomplete(f);
3107 +                checkIncomplete(CompletableFuture.allOf(fs));
3108 +                if (i != k/2) {
3109 +                    fs[i].complete(i);
3110 +                    checkCompletedNormally(fs[i], i);
3111 +                } else {
3112 +                    fs[i].completeExceptionally(ex);
3113 +                    checkCompletedExceptionally(fs[i], ex);
3114 +                }
3115 +            }
3116 +            checkCompletedWithWrappedException(f, ex);
3117 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3118 +        }
3119 +    }
3120 +
3121      /**
3122       * anyOf(no component futures) returns an incomplete future
3123       */
3124      public void testAnyOf_empty() throws Exception {
3125 +        for (Integer v1 : new Integer[] { 1, null })
3126 +    {
3127          CompletableFuture<Object> f = CompletableFuture.anyOf();
3128          checkIncomplete(f);
3129 <    }
3129 >
3130 >        f.complete(v1);
3131 >        checkCompletedNormally(f, v1);
3132 >    }}
3133  
3134      /**
3135       * anyOf returns a future completed normally with a value when
3136       * a component future does
3137       */
3138      public void testAnyOf_normal() throws Exception {
3139 <        for (int k = 0; k < 10; ++k) {
3139 >        for (int k = 0; k < 10; k++) {
3140              CompletableFuture[] fs = new CompletableFuture[k];
3141 <            for (int i = 0; i < k; ++i)
3141 >            for (int i = 0; i < k; i++)
3142                  fs[i] = new CompletableFuture<>();
3143              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3144              checkIncomplete(f);
3145 <            for (int i = 0; i < k; ++i) {
3146 <                fs[i].complete(one);
3147 <                checkCompletedNormally(f, one);
3148 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3145 >            for (int i = 0; i < k; i++) {
3146 >                fs[i].complete(i);
3147 >                checkCompletedNormally(f, 0);
3148 >                int x = (int) CompletableFuture.anyOf(fs).join();
3149 >                assertTrue(0 <= x && x <= i);
3150 >            }
3151 >        }
3152 >    }
3153 >    public void testAnyOf_normal_backwards() throws Exception {
3154 >        for (int k = 0; k < 10; k++) {
3155 >            CompletableFuture[] fs = new CompletableFuture[k];
3156 >            for (int i = 0; i < k; i++)
3157 >                fs[i] = new CompletableFuture<>();
3158 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3159 >            checkIncomplete(f);
3160 >            for (int i = k - 1; i >= 0; i--) {
3161 >                fs[i].complete(i);
3162 >                checkCompletedNormally(f, k - 1);
3163 >                int x = (int) CompletableFuture.anyOf(fs).join();
3164 >                assertTrue(i <= x && x <= k - 1);
3165              }
3166          }
3167      }
# Line 2548 | Line 3170 | public class CompletableFutureTest exten
3170       * anyOf result completes exceptionally when any component does.
3171       */
3172      public void testAnyOf_exceptional() throws Exception {
3173 <        for (int k = 0; k < 10; ++k) {
3173 >        for (int k = 0; k < 10; k++) {
3174 >            CompletableFuture[] fs = new CompletableFuture[k];
3175 >            CFException[] exs = new CFException[k];
3176 >            for (int i = 0; i < k; i++) {
3177 >                fs[i] = new CompletableFuture<>();
3178 >                exs[i] = new CFException();
3179 >            }
3180 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3181 >            checkIncomplete(f);
3182 >            for (int i = 0; i < k; i++) {
3183 >                fs[i].completeExceptionally(exs[i]);
3184 >                checkCompletedWithWrappedException(f, exs[0]);
3185 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3186 >            }
3187 >        }
3188 >    }
3189 >
3190 >    public void testAnyOf_exceptional_backwards() throws Exception {
3191 >        for (int k = 0; k < 10; k++) {
3192              CompletableFuture[] fs = new CompletableFuture[k];
3193 <            for (int i = 0; i < k; ++i)
3193 >            CFException[] exs = new CFException[k];
3194 >            for (int i = 0; i < k; i++) {
3195                  fs[i] = new CompletableFuture<>();
3196 +                exs[i] = new CFException();
3197 +            }
3198              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3199              checkIncomplete(f);
3200 <            for (int i = 0; i < k; ++i) {
3201 <                fs[i].completeExceptionally(new CFException());
3202 <                checkCompletedWithWrappedCFException(f);
3200 >            for (int i = k - 1; i >= 0; i--) {
3201 >                fs[i].completeExceptionally(exs[i]);
3202 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3203                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3204              }
3205          }
# Line 2575 | Line 3218 | public class CompletableFutureTest exten
3218          Runnable[] throwingActions = {
3219              () -> CompletableFuture.supplyAsync(null),
3220              () -> CompletableFuture.supplyAsync(null, exec),
3221 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3221 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3222  
3223              () -> CompletableFuture.runAsync(null),
3224              () -> CompletableFuture.runAsync(null, exec),
# Line 2680 | Line 3323 | public class CompletableFutureTest exten
3323          assertSame(f, f.toCompletableFuture());
3324      }
3325  
3326 <    /**
2684 <     * whenComplete action executes on normal completion, propagating
2685 <     * source result.
2686 <     */
2687 <    public void testWhenComplete_normalCompletion1() {
2688 <        for (ExecutionMode m : ExecutionMode.values())
2689 <        for (boolean createIncomplete : new boolean[] { true, false })
2690 <        for (Integer v1 : new Integer[] { 1, null })
2691 <    {
2692 <        final AtomicInteger a = new AtomicInteger(0);
2693 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2694 <        if (!createIncomplete) f.complete(v1);
2695 <        final CompletableFuture<Integer> g = m.whenComplete
2696 <            (f,
2697 <             (Integer x, Throwable t) -> {
2698 <                threadAssertSame(x, v1);
2699 <                threadAssertNull(t);
2700 <                a.getAndIncrement();
2701 <            });
2702 <        if (createIncomplete) f.complete(v1);
3326 >    //--- tests of implementation details; not part of official tck ---
3327  
3328 <        checkCompletedNormally(g, v1);
3329 <        checkCompletedNormally(f, v1);
3330 <        assertEquals(1, a.get());
3331 <    }}
3328 >    Object resultOf(CompletableFuture<?> f) {
3329 >        try {
3330 >            java.lang.reflect.Field resultField
3331 >                = CompletableFuture.class.getDeclaredField("result");
3332 >            resultField.setAccessible(true);
3333 >            return resultField.get(f);
3334 >        } catch (Throwable t) { throw new AssertionError(t); }
3335 >    }
3336  
3337 <    /**
3338 <     * whenComplete action executes on exceptional completion, propagating
2711 <     * source result.
2712 <     */
2713 <    public void testWhenComplete_exceptionalCompletion() {
3337 >    public void testExceptionPropagationReusesResultObject() {
3338 >        if (!testImplementationDetails) return;
3339          for (ExecutionMode m : ExecutionMode.values())
2715        for (boolean createIncomplete : new boolean[] { true, false })
2716        for (Integer v1 : new Integer[] { 1, null })
3340      {
2718        final AtomicInteger a = new AtomicInteger(0);
3341          final CFException ex = new CFException();
3342 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3343 <        if (!createIncomplete) f.completeExceptionally(ex);
2722 <        final CompletableFuture<Integer> g = m.whenComplete
2723 <            (f,
2724 <             (Integer x, Throwable t) -> {
2725 <                threadAssertNull(x);
2726 <                threadAssertSame(t, ex);
2727 <                a.getAndIncrement();
2728 <            });
2729 <        if (createIncomplete) f.completeExceptionally(ex);
2730 <        checkCompletedWithWrappedCFException(f, ex);
2731 <        checkCompletedWithWrappedCFException(g, ex);
2732 <        assertEquals(1, a.get());
2733 <    }}
3342 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3343 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3344  
3345 <    /**
3346 <     * whenComplete action executes on cancelled source, propagating
2737 <     * CancellationException.
2738 <     */
2739 <    public void testWhenComplete_sourceCancelled() {
2740 <        for (ExecutionMode m : ExecutionMode.values())
2741 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2742 <        for (boolean createIncomplete : new boolean[] { true, false })
2743 <    {
2744 <        final AtomicInteger a = new AtomicInteger(0);
2745 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2746 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2747 <        final CompletableFuture<Integer> g = m.whenComplete
2748 <            (f,
2749 <             (Integer x, Throwable t) -> {
2750 <                threadAssertNull(x);
2751 <                threadAssertTrue(t instanceof CancellationException);
2752 <                a.getAndIncrement();
2753 <            });
2754 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3345 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3346 >            = new ArrayList<>();
3347  
3348 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3349 <        checkCompletedWithWrappedCancellationException(g);
3350 <        checkCancelled(f);
2759 <        assertEquals(1, a.get());
2760 <    }}
3348 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3349 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3350 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3351  
3352 <    /**
3353 <     * If a whenComplete action throws an exception when triggered by
3354 <     * a normal completion, it completes exceptionally
2765 <     */
2766 <    public void testWhenComplete_actionFailed() {
2767 <        for (boolean createIncomplete : new boolean[] { true, false })
2768 <        for (ExecutionMode m : ExecutionMode.values())
2769 <        for (Integer v1 : new Integer[] { 1, null })
2770 <    {
2771 <        final AtomicInteger a = new AtomicInteger(0);
2772 <        final CFException ex = new CFException();
2773 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2774 <        if (!createIncomplete) f.complete(v1);
2775 <        final CompletableFuture<Integer> g = m.whenComplete
2776 <            (f,
2777 <             (Integer x, Throwable t) -> {
2778 <                threadAssertSame(x, v1);
2779 <                threadAssertNull(t);
2780 <                a.getAndIncrement();
2781 <                throw ex;
2782 <            });
2783 <        if (createIncomplete) f.complete(v1);
2784 <        checkCompletedNormally(f, v1);
2785 <        checkCompletedWithWrappedCFException(g, ex);
2786 <        assertEquals(1, a.get());
2787 <    }}
3352 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3353 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3354 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3355  
3356 <    /**
3357 <     * If a whenComplete action throws an exception when triggered by
3358 <     * a source completion that also throws an exception, the source
2792 <     * exception takes precedence.
2793 <     */
2794 <    public void testWhenComplete_actionFailedSourceFailed() {
2795 <        for (boolean createIncomplete : new boolean[] { true, false })
2796 <        for (ExecutionMode m : ExecutionMode.values())
2797 <        for (Integer v1 : new Integer[] { 1, null })
2798 <    {
2799 <        final AtomicInteger a = new AtomicInteger(0);
2800 <        final CFException ex1 = new CFException();
2801 <        final CFException ex2 = new CFException();
2802 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3356 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3357 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3358 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3359  
3360 <        if (!createIncomplete) f.completeExceptionally(ex1);
2805 <        final CompletableFuture<Integer> g = m.whenComplete
2806 <            (f,
2807 <             (Integer x, Throwable t) -> {
2808 <                threadAssertSame(t, ex1);
2809 <                threadAssertNull(x);
2810 <                a.getAndIncrement();
2811 <                throw ex2;
2812 <            });
2813 <        if (createIncomplete) f.completeExceptionally(ex1);
3360 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3361  
3362 <        checkCompletedWithWrappedCFException(f, ex1);
3363 <        checkCompletedWithWrappedCFException(g, ex1);
3364 <        assertEquals(1, a.get());
3362 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3363 >
3364 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3365 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3366 >
3367 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3368 >                 fun : funs) {
3369 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3370 >            f.completeExceptionally(ex);
3371 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3372 >            checkCompletedWithWrappedException(src, ex);
3373 >            CompletableFuture<?> dep = fun.apply(src);
3374 >            checkCompletedWithWrappedException(dep, ex);
3375 >            assertSame(resultOf(src), resultOf(dep));
3376 >        }
3377 >
3378 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3379 >                 fun : funs) {
3380 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3381 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3382 >            CompletableFuture<?> dep = fun.apply(src);
3383 >            f.completeExceptionally(ex);
3384 >            checkCompletedWithWrappedException(src, ex);
3385 >            checkCompletedWithWrappedException(dep, ex);
3386 >            assertSame(resultOf(src), resultOf(dep));
3387 >        }
3388 >
3389 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3390 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3391 >                 fun : funs) {
3392 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3393 >            f.cancel(mayInterruptIfRunning);
3394 >            checkCancelled(f);
3395 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3396 >            checkCompletedWithWrappedCancellationException(src);
3397 >            CompletableFuture<?> dep = fun.apply(src);
3398 >            checkCompletedWithWrappedCancellationException(dep);
3399 >            assertSame(resultOf(src), resultOf(dep));
3400 >        }
3401 >
3402 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3403 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3404 >                 fun : funs) {
3405 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3406 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3407 >            CompletableFuture<?> dep = fun.apply(src);
3408 >            f.cancel(mayInterruptIfRunning);
3409 >            checkCancelled(f);
3410 >            checkCompletedWithWrappedCancellationException(src);
3411 >            checkCompletedWithWrappedCancellationException(dep);
3412 >            assertSame(resultOf(src), resultOf(dep));
3413 >        }
3414      }}
3415  
3416   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines