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.64 by jsr166, Fri Jun 6 17:08:48 2014 UTC vs.
Revision 1.97 by jsr166, Wed Dec 31 16:44:01 2014 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.concurrent.Callable;
10   import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
11   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
12   import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
13   import java.util.concurrent.CompletableFuture;
14   import java.util.concurrent.CompletionException;
15   import java.util.concurrent.CompletionStage;
# Line 57 | Line 53 | public class CompletableFutureTest exten
53      }
54  
55      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
56 <        try {
57 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
56 >        checkTimedGet(f, value);
57 >
58          try {
59              assertEquals(value, f.join());
60          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 71 | public class CompletableFutureTest exten
71      }
72  
73      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
74 +        long startTime = System.nanoTime();
75 +        long timeoutMillis = LONG_DELAY_MS;
76          try {
77 <            f.get(LONG_DELAY_MS, MILLISECONDS);
77 >            f.get(timeoutMillis, MILLISECONDS);
78              shouldThrow();
79          } catch (ExecutionException success) {
80              assertTrue(success.getCause() instanceof CFException);
81          } catch (Throwable fail) { threadUnexpectedException(fail); }
82 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
83 +
84          try {
85              f.join();
86              shouldThrow();
# Line 105 | Line 104 | public class CompletableFutureTest exten
104          assertTrue(f.toString().contains("[Completed exceptionally]"));
105      }
106  
107 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
108 <                                              CFException ex) {
107 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
108 >                                                      Throwable ex) {
109 >        long startTime = System.nanoTime();
110 >        long timeoutMillis = LONG_DELAY_MS;
111          try {
112 <            f.get(LONG_DELAY_MS, MILLISECONDS);
112 >            f.get(timeoutMillis, MILLISECONDS);
113              shouldThrow();
114          } catch (ExecutionException success) {
115              assertSame(ex, success.getCause());
116          } catch (Throwable fail) { threadUnexpectedException(fail); }
117 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
118 +
119          try {
120              f.join();
121              shouldThrow();
# Line 131 | Line 134 | public class CompletableFutureTest exten
134          } catch (ExecutionException success) {
135              assertSame(ex, success.getCause());
136          } catch (Throwable fail) { threadUnexpectedException(fail); }
137 +
138          assertTrue(f.isDone());
139          assertFalse(f.isCancelled());
140          assertTrue(f.toString().contains("[Completed exceptionally]"));
141      }
142  
143 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
144 +                                                Throwable ex) {
145 +        checkCompletedExceptionallyWithRootCause(f, ex);
146 +        try {
147 +            CompletableFuture<Throwable> spy = f.handle
148 +                ((U u, Throwable t) -> t);
149 +            assertTrue(spy.join() instanceof CompletionException);
150 +            assertSame(ex, spy.join().getCause());
151 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
152 +    }
153 +
154 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
155 +        checkCompletedExceptionallyWithRootCause(f, ex);
156 +        try {
157 +            CompletableFuture<Throwable> spy = f.handle
158 +                ((U u, Throwable t) -> t);
159 +            assertSame(ex, spy.join());
160 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
161 +    }
162 +
163      void checkCancelled(CompletableFuture<?> f) {
164 +        long startTime = System.nanoTime();
165 +        long timeoutMillis = LONG_DELAY_MS;
166          try {
167 <            f.get(LONG_DELAY_MS, MILLISECONDS);
167 >            f.get(timeoutMillis, MILLISECONDS);
168              shouldThrow();
169          } catch (CancellationException success) {
170          } catch (Throwable fail) { threadUnexpectedException(fail); }
171 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
172 +
173          try {
174              f.join();
175              shouldThrow();
# Line 162 | Line 190 | public class CompletableFutureTest exten
190      }
191  
192      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
193 +        long startTime = System.nanoTime();
194 +        long timeoutMillis = LONG_DELAY_MS;
195          try {
196 <            f.get(LONG_DELAY_MS, MILLISECONDS);
196 >            f.get(timeoutMillis, MILLISECONDS);
197              shouldThrow();
198          } catch (ExecutionException success) {
199              assertTrue(success.getCause() instanceof CancellationException);
200          } catch (Throwable fail) { threadUnexpectedException(fail); }
201 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
202 +
203          try {
204              f.join();
205              shouldThrow();
# Line 206 | Line 238 | public class CompletableFutureTest exten
238       * isCancelled, join, get, and getNow
239       */
240      public void testComplete() {
241 +        for (Integer v1 : new Integer[] { 1, null })
242 +    {
243          CompletableFuture<Integer> f = new CompletableFuture<>();
244          checkIncomplete(f);
245 <        f.complete(one);
246 <        checkCompletedNormally(f, one);
247 <    }
245 >        assertTrue(f.complete(v1));
246 >        assertFalse(f.complete(v1));
247 >        checkCompletedNormally(f, v1);
248 >    }}
249  
250      /**
251       * completeExceptionally completes exceptionally, as indicated by
# Line 218 | Line 253 | public class CompletableFutureTest exten
253       */
254      public void testCompleteExceptionally() {
255          CompletableFuture<Integer> f = new CompletableFuture<>();
256 +        CFException ex = new CFException();
257          checkIncomplete(f);
258 <        f.completeExceptionally(new CFException());
259 <        checkCompletedWithWrappedCFException(f);
258 >        f.completeExceptionally(ex);
259 >        checkCompletedExceptionally(f, ex);
260      }
261  
262      /**
# Line 228 | Line 264 | public class CompletableFutureTest exten
264       * methods isDone, isCancelled, join, get, and getNow
265       */
266      public void testCancel() {
267 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
268 +    {
269          CompletableFuture<Integer> f = new CompletableFuture<>();
270          checkIncomplete(f);
271          assertTrue(f.cancel(true));
272 +        assertTrue(f.cancel(true));
273          checkCancelled(f);
274 <    }
274 >    }}
275  
276      /**
277       * obtrudeValue forces completion with given value
# Line 240 | Line 279 | public class CompletableFutureTest exten
279      public void testObtrudeValue() {
280          CompletableFuture<Integer> f = new CompletableFuture<>();
281          checkIncomplete(f);
282 <        f.complete(one);
282 >        assertTrue(f.complete(one));
283          checkCompletedNormally(f, one);
284          f.obtrudeValue(three);
285          checkCompletedNormally(f, three);
# Line 261 | Line 300 | public class CompletableFutureTest exten
300       * obtrudeException forces completion with given exception
301       */
302      public void testObtrudeException() {
303 <        CompletableFuture<Integer> f = new CompletableFuture<>();
304 <        checkIncomplete(f);
305 <        f.complete(one);
306 <        checkCompletedNormally(f, one);
307 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
303 >        for (Integer v1 : new Integer[] { 1, null })
304 >    {
305 >        CFException ex;
306 >        CompletableFuture<Integer> f;
307 >
308          f = new CompletableFuture<>();
309 <        f.obtrudeException(new CFException());
310 <        checkCompletedWithWrappedCFException(f);
309 >        assertTrue(f.complete(v1));
310 >        for (int i = 0; i < 2; i++) {
311 >            f.obtrudeException(ex = new CFException());
312 >            checkCompletedExceptionally(f, ex);
313 >        }
314 >
315 >        f = new CompletableFuture<>();
316 >        for (int i = 0; i < 2; i++) {
317 >            f.obtrudeException(ex = new CFException());
318 >            checkCompletedExceptionally(f, ex);
319 >        }
320 >
321          f = new CompletableFuture<>();
322 +        f.completeExceptionally(ex = new CFException());
323 +        f.obtrudeValue(v1);
324 +        checkCompletedNormally(f, v1);
325 +        f.obtrudeException(ex = new CFException());
326 +        checkCompletedExceptionally(f, ex);
327          f.completeExceptionally(new CFException());
328 <        f.obtrudeValue(four);
329 <        checkCompletedNormally(f, four);
330 <        f.obtrudeException(new CFException());
331 <        checkCompletedWithWrappedCFException(f);
279 <    }
328 >        checkCompletedExceptionally(f, ex);
329 >        assertFalse(f.complete(v1));
330 >        checkCompletedExceptionally(f, ex);
331 >    }}
332  
333      /**
334       * getNumberOfDependents returns number of dependent tasks
335       */
336      public void testGetNumberOfDependents() {
337 +        for (ExecutionMode m : ExecutionMode.values())
338 +        for (Integer v1 : new Integer[] { 1, null })
339 +    {
340          CompletableFuture<Integer> f = new CompletableFuture<>();
341          assertEquals(0, f.getNumberOfDependents());
342 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
342 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
343          assertEquals(1, f.getNumberOfDependents());
344          assertEquals(0, g.getNumberOfDependents());
345 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
345 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
346          assertEquals(2, f.getNumberOfDependents());
347 <        f.complete(1);
347 >        assertEquals(0, h.getNumberOfDependents());
348 >        assertTrue(f.complete(v1));
349          checkCompletedNormally(g, null);
350 +        checkCompletedNormally(h, null);
351          assertEquals(0, f.getNumberOfDependents());
352          assertEquals(0, g.getNumberOfDependents());
353 <    }
353 >        assertEquals(0, h.getNumberOfDependents());
354 >    }}
355  
356      /**
357       * toString indicates current completion state
# Line 304 | Line 362 | public class CompletableFutureTest exten
362          f = new CompletableFuture<String>();
363          assertTrue(f.toString().contains("[Not completed]"));
364  
365 <        f.complete("foo");
365 >        assertTrue(f.complete("foo"));
366          assertTrue(f.toString().contains("[Completed normally]"));
367  
368          f = new CompletableFuture<String>();
369 <        f.completeExceptionally(new IndexOutOfBoundsException());
369 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
370          assertTrue(f.toString().contains("[Completed exceptionally]"));
371 +
372 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
373 +            f = new CompletableFuture<String>();
374 +            assertTrue(f.cancel(mayInterruptIfRunning));
375 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
376 +        }
377      }
378  
379      /**
# Line 486 | Line 550 | public class CompletableFutureTest exten
550              invoked();
551              value = x;
552              CompletableFuture<Integer> f = new CompletableFuture<>();
553 <            f.complete(inc(x));
553 >            assertTrue(f.complete(inc(x)));
554              return f;
555          }
556      }
# Line 516 | Line 580 | public class CompletableFutureTest exten
580          }
581      }
582  
583 +    static final boolean defaultExecutorIsCommonPool
584 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
585 +
586      /**
587       * Permits the testing of parallel code for the 3 different
588       * execution modes without copy/pasting all the test methods.
589       */
590      enum ExecutionMode {
591 <        DEFAULT {
591 >        SYNC {
592              public void checkExecutionMode() {
593                  assertFalse(ThreadExecutor.startedCurrentThread());
594                  assertNull(ForkJoinTask.getPool());
# Line 597 | Line 664 | public class CompletableFutureTest exten
664  
665          ASYNC {
666              public void checkExecutionMode() {
667 <                assertSame(ForkJoinPool.commonPool(),
668 <                           ForkJoinTask.getPool());
667 >                assertEquals(defaultExecutorIsCommonPool,
668 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
669              }
670              public CompletableFuture<Void> runAsync(Runnable a) {
671                  return CompletableFuture.runAsync(a);
# Line 794 | Line 861 | public class CompletableFutureTest exten
861      {
862          final AtomicInteger a = new AtomicInteger(0);
863          final CompletableFuture<Integer> f = new CompletableFuture<>();
864 <        if (!createIncomplete) f.complete(v1);
864 >        if (!createIncomplete) assertTrue(f.complete(v1));
865          final CompletableFuture<Integer> g = f.exceptionally
866              ((Throwable t) -> {
867                  // Should not be called
868                  a.getAndIncrement();
869                  throw new AssertionError();
870              });
871 <        if (createIncomplete) f.complete(v1);
871 >        if (createIncomplete) assertTrue(f.complete(v1));
872  
873          checkCompletedNormally(g, v1);
874          checkCompletedNormally(f, v1);
875          assertEquals(0, a.get());
876      }}
877  
811
878      /**
879       * exceptionally action completes with function value on source
880       * exception
# Line 823 | Line 889 | public class CompletableFutureTest exten
889          if (!createIncomplete) f.completeExceptionally(ex);
890          final CompletableFuture<Integer> g = f.exceptionally
891              ((Throwable t) -> {
892 <                ExecutionMode.DEFAULT.checkExecutionMode();
892 >                ExecutionMode.SYNC.checkExecutionMode();
893                  threadAssertSame(t, ex);
894                  a.getAndIncrement();
895                  return v1;
# Line 845 | Line 911 | public class CompletableFutureTest exten
911          if (!createIncomplete) f.completeExceptionally(ex1);
912          final CompletableFuture<Integer> g = f.exceptionally
913              ((Throwable t) -> {
914 <                ExecutionMode.DEFAULT.checkExecutionMode();
914 >                ExecutionMode.SYNC.checkExecutionMode();
915 >                threadAssertSame(t, ex1);
916 >                a.getAndIncrement();
917 >                throw ex2;
918 >            });
919 >        if (createIncomplete) f.completeExceptionally(ex1);
920 >
921 >        checkCompletedWithWrappedException(g, ex2);
922 >        assertEquals(1, a.get());
923 >    }}
924 >
925 >    /**
926 >     * whenComplete action executes on normal completion, propagating
927 >     * source result.
928 >     */
929 >    public void testWhenComplete_normalCompletion1() {
930 >        for (ExecutionMode m : ExecutionMode.values())
931 >        for (boolean createIncomplete : new boolean[] { true, false })
932 >        for (Integer v1 : new Integer[] { 1, null })
933 >    {
934 >        final AtomicInteger a = new AtomicInteger(0);
935 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
936 >        if (!createIncomplete) assertTrue(f.complete(v1));
937 >        final CompletableFuture<Integer> g = m.whenComplete
938 >            (f,
939 >             (Integer x, Throwable t) -> {
940 >                m.checkExecutionMode();
941 >                threadAssertSame(x, v1);
942 >                threadAssertNull(t);
943 >                a.getAndIncrement();
944 >            });
945 >        if (createIncomplete) assertTrue(f.complete(v1));
946 >
947 >        checkCompletedNormally(g, v1);
948 >        checkCompletedNormally(f, v1);
949 >        assertEquals(1, a.get());
950 >    }}
951 >
952 >    /**
953 >     * whenComplete action executes on exceptional completion, propagating
954 >     * source result.
955 >     */
956 >    public void testWhenComplete_exceptionalCompletion() {
957 >        for (ExecutionMode m : ExecutionMode.values())
958 >        for (boolean createIncomplete : new boolean[] { true, false })
959 >        for (Integer v1 : new Integer[] { 1, null })
960 >    {
961 >        final AtomicInteger a = new AtomicInteger(0);
962 >        final CFException ex = new CFException();
963 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
964 >        if (!createIncomplete) f.completeExceptionally(ex);
965 >        final CompletableFuture<Integer> g = m.whenComplete
966 >            (f,
967 >             (Integer x, Throwable t) -> {
968 >                m.checkExecutionMode();
969 >                threadAssertNull(x);
970 >                threadAssertSame(t, ex);
971 >                a.getAndIncrement();
972 >            });
973 >        if (createIncomplete) f.completeExceptionally(ex);
974 >
975 >        checkCompletedWithWrappedException(g, ex);
976 >        checkCompletedExceptionally(f, ex);
977 >        assertEquals(1, a.get());
978 >    }}
979 >
980 >    /**
981 >     * whenComplete action executes on cancelled source, propagating
982 >     * CancellationException.
983 >     */
984 >    public void testWhenComplete_sourceCancelled() {
985 >        for (ExecutionMode m : ExecutionMode.values())
986 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
987 >        for (boolean createIncomplete : new boolean[] { true, false })
988 >    {
989 >        final AtomicInteger a = new AtomicInteger(0);
990 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
991 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
992 >        final CompletableFuture<Integer> g = m.whenComplete
993 >            (f,
994 >             (Integer x, Throwable t) -> {
995 >                m.checkExecutionMode();
996 >                threadAssertNull(x);
997 >                threadAssertTrue(t instanceof CancellationException);
998 >                a.getAndIncrement();
999 >            });
1000 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1001 >
1002 >        checkCompletedWithWrappedCancellationException(g);
1003 >        checkCancelled(f);
1004 >        assertEquals(1, a.get());
1005 >    }}
1006 >
1007 >    /**
1008 >     * If a whenComplete action throws an exception when triggered by
1009 >     * a normal completion, it completes exceptionally
1010 >     */
1011 >    public void testWhenComplete_actionFailed() {
1012 >        for (boolean createIncomplete : new boolean[] { true, false })
1013 >        for (ExecutionMode m : ExecutionMode.values())
1014 >        for (Integer v1 : new Integer[] { 1, null })
1015 >    {
1016 >        final AtomicInteger a = new AtomicInteger(0);
1017 >        final CFException ex = new CFException();
1018 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1019 >        if (!createIncomplete) assertTrue(f.complete(v1));
1020 >        final CompletableFuture<Integer> g = m.whenComplete
1021 >            (f,
1022 >             (Integer x, Throwable t) -> {
1023 >                m.checkExecutionMode();
1024 >                threadAssertSame(x, v1);
1025 >                threadAssertNull(t);
1026 >                a.getAndIncrement();
1027 >                throw ex;
1028 >            });
1029 >        if (createIncomplete) assertTrue(f.complete(v1));
1030 >
1031 >        checkCompletedWithWrappedException(g, ex);
1032 >        checkCompletedNormally(f, v1);
1033 >        assertEquals(1, a.get());
1034 >    }}
1035 >
1036 >    /**
1037 >     * If a whenComplete action throws an exception when triggered by
1038 >     * a source completion that also throws an exception, the source
1039 >     * exception takes precedence.
1040 >     */
1041 >    public void testWhenComplete_actionFailedSourceFailed() {
1042 >        for (boolean createIncomplete : new boolean[] { true, false })
1043 >        for (ExecutionMode m : ExecutionMode.values())
1044 >        for (Integer v1 : new Integer[] { 1, null })
1045 >    {
1046 >        final AtomicInteger a = new AtomicInteger(0);
1047 >        final CFException ex1 = new CFException();
1048 >        final CFException ex2 = new CFException();
1049 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1050 >
1051 >        if (!createIncomplete) f.completeExceptionally(ex1);
1052 >        final CompletableFuture<Integer> g = m.whenComplete
1053 >            (f,
1054 >             (Integer x, Throwable t) -> {
1055 >                m.checkExecutionMode();
1056                  threadAssertSame(t, ex1);
1057 +                threadAssertNull(x);
1058                  a.getAndIncrement();
1059                  throw ex2;
1060              });
1061          if (createIncomplete) f.completeExceptionally(ex1);
1062  
1063 <        checkCompletedWithWrappedCFException(g, ex2);
1063 >        checkCompletedWithWrappedException(g, ex1);
1064 >        checkCompletedExceptionally(f, ex1);
1065          assertEquals(1, a.get());
1066      }}
1067  
# Line 867 | Line 1076 | public class CompletableFutureTest exten
1076      {
1077          final CompletableFuture<Integer> f = new CompletableFuture<>();
1078          final AtomicInteger a = new AtomicInteger(0);
1079 <        if (!createIncomplete) f.complete(v1);
1079 >        if (!createIncomplete) assertTrue(f.complete(v1));
1080          final CompletableFuture<Integer> g = m.handle
1081              (f,
1082               (Integer x, Throwable t) -> {
# Line 877 | Line 1086 | public class CompletableFutureTest exten
1086                  a.getAndIncrement();
1087                  return inc(v1);
1088              });
1089 <        if (createIncomplete) f.complete(v1);
1089 >        if (createIncomplete) assertTrue(f.complete(v1));
1090  
1091          checkCompletedNormally(g, inc(v1));
1092          checkCompletedNormally(f, v1);
# Line 909 | Line 1118 | public class CompletableFutureTest exten
1118          if (createIncomplete) f.completeExceptionally(ex);
1119  
1120          checkCompletedNormally(g, v1);
1121 <        checkCompletedWithWrappedCFException(f, ex);
1121 >        checkCompletedExceptionally(f, ex);
1122          assertEquals(1, a.get());
1123      }}
1124  
# Line 965 | Line 1174 | public class CompletableFutureTest exten
1174              });
1175          if (createIncomplete) f.completeExceptionally(ex1);
1176  
1177 <        checkCompletedWithWrappedCFException(g, ex2);
1178 <        checkCompletedWithWrappedCFException(f, ex1);
1177 >        checkCompletedWithWrappedException(g, ex2);
1178 >        checkCompletedExceptionally(f, ex1);
1179          assertEquals(1, a.get());
1180      }}
1181  
# Line 978 | Line 1187 | public class CompletableFutureTest exten
1187          final CompletableFuture<Integer> f = new CompletableFuture<>();
1188          final AtomicInteger a = new AtomicInteger(0);
1189          final CFException ex = new CFException();
1190 <        if (!createIncomplete) f.complete(v1);
1190 >        if (!createIncomplete) assertTrue(f.complete(v1));
1191          final CompletableFuture<Integer> g = m.handle
1192              (f,
1193               (Integer x, Throwable t) -> {
# Line 988 | Line 1197 | public class CompletableFutureTest exten
1197                  a.getAndIncrement();
1198                  throw ex;
1199              });
1200 <        if (createIncomplete) f.complete(v1);
1200 >        if (createIncomplete) assertTrue(f.complete(v1));
1201  
1202 <        checkCompletedWithWrappedCFException(g, ex);
1202 >        checkCompletedWithWrappedException(g, ex);
1203          checkCompletedNormally(f, v1);
1204          assertEquals(1, a.get());
1205      }}
# Line 1069 | Line 1278 | public class CompletableFutureTest exten
1278       */
1279      public void testThenRun_normalCompletion() {
1280          for (ExecutionMode m : ExecutionMode.values())
1072        for (boolean createIncomplete : new boolean[] { true, false })
1281          for (Integer v1 : new Integer[] { 1, null })
1282      {
1283          final CompletableFuture<Integer> f = new CompletableFuture<>();
1284 <        final Noop r = new Noop(m);
1285 <        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 <        }
1284 >        final Noop[] rs = new Noop[6];
1285 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1286  
1287 <        checkCompletedNormally(g, null);
1287 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1288 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1289 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1290 >        checkIncomplete(h0);
1291 >        checkIncomplete(h1);
1292 >        checkIncomplete(h2);
1293 >        assertTrue(f.complete(v1));
1294 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1295 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1296 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1297 >
1298 >        checkCompletedNormally(h0, null);
1299 >        checkCompletedNormally(h1, null);
1300 >        checkCompletedNormally(h2, null);
1301 >        checkCompletedNormally(h3, null);
1302 >        checkCompletedNormally(h4, null);
1303 >        checkCompletedNormally(h5, null);
1304          checkCompletedNormally(f, v1);
1305 <        r.assertInvoked();
1305 >        for (Noop r : rs) r.assertInvoked();
1306      }}
1307  
1308      /**
# Line 1092 | Line 1311 | public class CompletableFutureTest exten
1311       */
1312      public void testThenRun_exceptionalCompletion() {
1313          for (ExecutionMode m : ExecutionMode.values())
1095        for (boolean createIncomplete : new boolean[] { true, false })
1314      {
1315          final CFException ex = new CFException();
1316          final CompletableFuture<Integer> f = new CompletableFuture<>();
1317 <        final Noop r = new Noop(m);
1318 <        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 <        }
1317 >        final Noop[] rs = new Noop[6];
1318 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1319  
1320 <        checkCompletedWithWrappedCFException(g, ex);
1321 <        checkCompletedWithWrappedCFException(f, ex);
1322 <        r.assertNotInvoked();
1320 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1321 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1322 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1323 >        checkIncomplete(h0);
1324 >        checkIncomplete(h1);
1325 >        checkIncomplete(h2);
1326 >        assertTrue(f.completeExceptionally(ex));
1327 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1328 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1329 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1330 >
1331 >        checkCompletedWithWrappedException(h0, ex);
1332 >        checkCompletedWithWrappedException(h1, ex);
1333 >        checkCompletedWithWrappedException(h2, ex);
1334 >        checkCompletedWithWrappedException(h3, ex);
1335 >        checkCompletedWithWrappedException(h4, ex);
1336 >        checkCompletedWithWrappedException(h5, ex);
1337 >        checkCompletedExceptionally(f, ex);
1338 >        for (Noop r : rs) r.assertNotInvoked();
1339      }}
1340  
1341      /**
# Line 1114 | Line 1343 | public class CompletableFutureTest exten
1343       */
1344      public void testThenRun_sourceCancelled() {
1345          for (ExecutionMode m : ExecutionMode.values())
1117        for (boolean createIncomplete : new boolean[] { true, false })
1346          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1347      {
1348          final CompletableFuture<Integer> f = new CompletableFuture<>();
1349 <        final Noop r = new Noop(m);
1350 <        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 <        }
1349 >        final Noop[] rs = new Noop[6];
1350 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1351  
1352 <        checkCompletedWithWrappedCancellationException(g);
1352 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1353 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1354 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1355 >        checkIncomplete(h0);
1356 >        checkIncomplete(h1);
1357 >        checkIncomplete(h2);
1358 >        assertTrue(f.cancel(mayInterruptIfRunning));
1359 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1360 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1361 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1362 >
1363 >        checkCompletedWithWrappedCancellationException(h0);
1364 >        checkCompletedWithWrappedCancellationException(h1);
1365 >        checkCompletedWithWrappedCancellationException(h2);
1366 >        checkCompletedWithWrappedCancellationException(h3);
1367 >        checkCompletedWithWrappedCancellationException(h4);
1368 >        checkCompletedWithWrappedCancellationException(h5);
1369          checkCancelled(f);
1370 <        r.assertNotInvoked();
1370 >        for (Noop r : rs) r.assertNotInvoked();
1371      }}
1372  
1373      /**
# Line 1136 | Line 1375 | public class CompletableFutureTest exten
1375       */
1376      public void testThenRun_actionFailed() {
1377          for (ExecutionMode m : ExecutionMode.values())
1139        for (boolean createIncomplete : new boolean[] { true, false })
1378          for (Integer v1 : new Integer[] { 1, null })
1379      {
1380          final CompletableFuture<Integer> f = new CompletableFuture<>();
1381 <        final FailingRunnable r = new FailingRunnable(m);
1382 <        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 <        }
1381 >        final FailingRunnable[] rs = new FailingRunnable[6];
1382 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1383  
1384 <        checkCompletedWithWrappedCFException(g);
1384 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1385 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1386 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1387 >        assertTrue(f.complete(v1));
1388 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1389 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1390 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1391 >
1392 >        checkCompletedWithWrappedCFException(h0);
1393 >        checkCompletedWithWrappedCFException(h1);
1394 >        checkCompletedWithWrappedCFException(h2);
1395 >        checkCompletedWithWrappedCFException(h3);
1396 >        checkCompletedWithWrappedCFException(h4);
1397 >        checkCompletedWithWrappedCFException(h5);
1398          checkCompletedNormally(f, v1);
1399      }}
1400  
# Line 1157 | Line 1403 | public class CompletableFutureTest exten
1403       */
1404      public void testThenApply_normalCompletion() {
1405          for (ExecutionMode m : ExecutionMode.values())
1160        for (boolean createIncomplete : new boolean[] { true, false })
1406          for (Integer v1 : new Integer[] { 1, null })
1407      {
1408          final CompletableFuture<Integer> f = new CompletableFuture<>();
1409 <        final IncFunction r = new IncFunction(m);
1410 <        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 <        }
1409 >        final IncFunction[] rs = new IncFunction[4];
1410 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1411  
1412 <        checkCompletedNormally(g, inc(v1));
1412 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1413 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1414 >        checkIncomplete(h0);
1415 >        checkIncomplete(h1);
1416 >        assertTrue(f.complete(v1));
1417 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1418 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1419 >
1420 >        checkCompletedNormally(h0, inc(v1));
1421 >        checkCompletedNormally(h1, inc(v1));
1422 >        checkCompletedNormally(h2, inc(v1));
1423 >        checkCompletedNormally(h3, inc(v1));
1424          checkCompletedNormally(f, v1);
1425 <        r.assertInvoked();
1425 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1426      }}
1427  
1428      /**
# Line 1180 | Line 1431 | public class CompletableFutureTest exten
1431       */
1432      public void testThenApply_exceptionalCompletion() {
1433          for (ExecutionMode m : ExecutionMode.values())
1183        for (boolean createIncomplete : new boolean[] { true, false })
1434      {
1435          final CFException ex = new CFException();
1436          final CompletableFuture<Integer> f = new CompletableFuture<>();
1437 <        final IncFunction r = new IncFunction(m);
1438 <        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 <        }
1437 >        final IncFunction[] rs = new IncFunction[4];
1438 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1439  
1440 <        checkCompletedWithWrappedCFException(g, ex);
1441 <        checkCompletedWithWrappedCFException(f, ex);
1442 <        r.assertNotInvoked();
1440 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1441 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1442 >        assertTrue(f.completeExceptionally(ex));
1443 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1444 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1445 >
1446 >        checkCompletedWithWrappedException(h0, ex);
1447 >        checkCompletedWithWrappedException(h1, ex);
1448 >        checkCompletedWithWrappedException(h2, ex);
1449 >        checkCompletedWithWrappedException(h3, ex);
1450 >        checkCompletedExceptionally(f, ex);
1451 >        for (IncFunction r : rs) r.assertNotInvoked();
1452      }}
1453  
1454      /**
# Line 1202 | Line 1456 | public class CompletableFutureTest exten
1456       */
1457      public void testThenApply_sourceCancelled() {
1458          for (ExecutionMode m : ExecutionMode.values())
1205        for (boolean createIncomplete : new boolean[] { true, false })
1459          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1460      {
1461          final CompletableFuture<Integer> f = new CompletableFuture<>();
1462 <        final IncFunction r = new IncFunction(m);
1463 <        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 <        }
1462 >        final IncFunction[] rs = new IncFunction[4];
1463 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1464  
1465 <        checkCompletedWithWrappedCancellationException(g);
1465 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1466 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1467 >        assertTrue(f.cancel(mayInterruptIfRunning));
1468 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1469 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1470 >
1471 >        checkCompletedWithWrappedCancellationException(h0);
1472 >        checkCompletedWithWrappedCancellationException(h1);
1473 >        checkCompletedWithWrappedCancellationException(h2);
1474 >        checkCompletedWithWrappedCancellationException(h3);
1475          checkCancelled(f);
1476 <        r.assertNotInvoked();
1476 >        for (IncFunction r : rs) r.assertNotInvoked();
1477      }}
1478  
1479      /**
# Line 1224 | Line 1481 | public class CompletableFutureTest exten
1481       */
1482      public void testThenApply_actionFailed() {
1483          for (ExecutionMode m : ExecutionMode.values())
1227        for (boolean createIncomplete : new boolean[] { true, false })
1484          for (Integer v1 : new Integer[] { 1, null })
1485      {
1486          final CompletableFuture<Integer> f = new CompletableFuture<>();
1487 <        final FailingFunction r = new FailingFunction(m);
1488 <        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 <        }
1487 >        final FailingFunction[] rs = new FailingFunction[4];
1488 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1489  
1490 <        checkCompletedWithWrappedCFException(g);
1490 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1491 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1492 >        assertTrue(f.complete(v1));
1493 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1494 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1495 >
1496 >        checkCompletedWithWrappedCFException(h0);
1497 >        checkCompletedWithWrappedCFException(h1);
1498 >        checkCompletedWithWrappedCFException(h2);
1499 >        checkCompletedWithWrappedCFException(h3);
1500          checkCompletedNormally(f, v1);
1501      }}
1502  
# Line 1245 | Line 1505 | public class CompletableFutureTest exten
1505       */
1506      public void testThenAccept_normalCompletion() {
1507          for (ExecutionMode m : ExecutionMode.values())
1248        for (boolean createIncomplete : new boolean[] { true, false })
1508          for (Integer v1 : new Integer[] { 1, null })
1509      {
1510          final CompletableFuture<Integer> f = new CompletableFuture<>();
1511 <        final NoopConsumer r = new NoopConsumer(m);
1512 <        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 <        }
1511 >        final NoopConsumer[] rs = new NoopConsumer[4];
1512 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1513  
1514 <        checkCompletedNormally(g, null);
1515 <        r.assertValue(v1);
1514 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1515 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1516 >        checkIncomplete(h0);
1517 >        checkIncomplete(h1);
1518 >        assertTrue(f.complete(v1));
1519 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1520 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1521 >
1522 >        checkCompletedNormally(h0, null);
1523 >        checkCompletedNormally(h1, null);
1524 >        checkCompletedNormally(h2, null);
1525 >        checkCompletedNormally(h3, null);
1526          checkCompletedNormally(f, v1);
1527 +        for (NoopConsumer r : rs) r.assertValue(v1);
1528      }}
1529  
1530      /**
# Line 1268 | Line 1533 | public class CompletableFutureTest exten
1533       */
1534      public void testThenAccept_exceptionalCompletion() {
1535          for (ExecutionMode m : ExecutionMode.values())
1271        for (boolean createIncomplete : new boolean[] { true, false })
1536      {
1537          final CFException ex = new CFException();
1538          final CompletableFuture<Integer> f = new CompletableFuture<>();
1539 <        final NoopConsumer r = new NoopConsumer(m);
1540 <        if (!createIncomplete) f.completeExceptionally(ex);
1277 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1278 <        if (createIncomplete) {
1279 <            checkIncomplete(g);
1280 <            f.completeExceptionally(ex);
1281 <        }
1539 >        final NoopConsumer[] rs = new NoopConsumer[4];
1540 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1541  
1542 <        checkCompletedWithWrappedCFException(g, ex);
1543 <        checkCompletedWithWrappedCFException(f, ex);
1544 <        r.assertNotInvoked();
1542 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1543 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1544 >        assertTrue(f.completeExceptionally(ex));
1545 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1546 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1547 >
1548 >        checkCompletedWithWrappedException(h0, ex);
1549 >        checkCompletedWithWrappedException(h1, ex);
1550 >        checkCompletedWithWrappedException(h2, ex);
1551 >        checkCompletedWithWrappedException(h3, ex);
1552 >        checkCompletedExceptionally(f, ex);
1553 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1554      }}
1555  
1556      /**
# Line 1290 | Line 1558 | public class CompletableFutureTest exten
1558       */
1559      public void testThenAccept_sourceCancelled() {
1560          for (ExecutionMode m : ExecutionMode.values())
1293        for (boolean createIncomplete : new boolean[] { true, false })
1561          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1562      {
1563          final CompletableFuture<Integer> f = new CompletableFuture<>();
1564 <        final NoopConsumer r = new NoopConsumer(m);
1565 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1299 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1300 <        if (createIncomplete) {
1301 <            checkIncomplete(g);
1302 <            assertTrue(f.cancel(mayInterruptIfRunning));
1303 <        }
1564 >        final NoopConsumer[] rs = new NoopConsumer[4];
1565 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1566  
1567 <        checkCompletedWithWrappedCancellationException(g);
1567 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1568 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1569 >        assertTrue(f.cancel(mayInterruptIfRunning));
1570 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1571 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1572 >
1573 >        checkCompletedWithWrappedCancellationException(h0);
1574 >        checkCompletedWithWrappedCancellationException(h1);
1575 >        checkCompletedWithWrappedCancellationException(h2);
1576 >        checkCompletedWithWrappedCancellationException(h3);
1577          checkCancelled(f);
1578 <        r.assertNotInvoked();
1578 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1579      }}
1580  
1581      /**
# Line 1312 | Line 1583 | public class CompletableFutureTest exten
1583       */
1584      public void testThenAccept_actionFailed() {
1585          for (ExecutionMode m : ExecutionMode.values())
1315        for (boolean createIncomplete : new boolean[] { true, false })
1586          for (Integer v1 : new Integer[] { 1, null })
1587      {
1588          final CompletableFuture<Integer> f = new CompletableFuture<>();
1589 <        final FailingConsumer r = new FailingConsumer(m);
1590 <        if (!createIncomplete) f.complete(v1);
1321 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1322 <        if (createIncomplete) {
1323 <            checkIncomplete(g);
1324 <            f.complete(v1);
1325 <        }
1589 >        final FailingConsumer[] rs = new FailingConsumer[4];
1590 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1591  
1592 <        checkCompletedWithWrappedCFException(g);
1592 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1593 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1594 >        assertTrue(f.complete(v1));
1595 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1596 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1597 >
1598 >        checkCompletedWithWrappedCFException(h0);
1599 >        checkCompletedWithWrappedCFException(h1);
1600 >        checkCompletedWithWrappedCFException(h2);
1601 >        checkCompletedWithWrappedCFException(h3);
1602          checkCompletedNormally(f, v1);
1603      }}
1604  
# Line 1334 | Line 1608 | public class CompletableFutureTest exten
1608       */
1609      public void testThenCombine_normalCompletion() {
1610          for (ExecutionMode m : ExecutionMode.values())
1337        for (boolean createIncomplete : new boolean[] { true, false })
1611          for (boolean fFirst : new boolean[] { true, false })
1612          for (Integer v1 : new Integer[] { 1, null })
1613          for (Integer v2 : new Integer[] { 2, null })
1614      {
1615          final CompletableFuture<Integer> f = new CompletableFuture<>();
1616          final CompletableFuture<Integer> g = new CompletableFuture<>();
1617 <        final SubtractFunction r = new SubtractFunction(m);
1617 >        final SubtractFunction[] rs = new SubtractFunction[6];
1618 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1619  
1620 <        if (fFirst) f.complete(v1); else g.complete(v2);
1621 <        if (!createIncomplete)
1622 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1623 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1624 <        if (createIncomplete) {
1625 <            checkIncomplete(h);
1626 <            r.assertNotInvoked();
1627 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1628 <        }
1620 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1621 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1622 >        final Integer w1 =  fFirst ? v1 : v2;
1623 >        final Integer w2 = !fFirst ? v1 : v2;
1624 >
1625 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1626 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1627 >        assertTrue(fst.complete(w1));
1628 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1629 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1630 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1631 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1632 >        checkCompletedNormally(h1, subtract(w1, w1));
1633 >        checkCompletedNormally(h3, subtract(w1, w1));
1634 >        rs[1].assertValue(subtract(w1, w1));
1635 >        rs[3].assertValue(subtract(w1, w1));
1636 >        assertTrue(snd.complete(w2));
1637 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1638 >
1639 >        checkCompletedNormally(h0, subtract(v1, v2));
1640 >        checkCompletedNormally(h2, subtract(v1, v2));
1641 >        checkCompletedNormally(h4, subtract(v1, v2));
1642 >        rs[0].assertValue(subtract(v1, v2));
1643 >        rs[2].assertValue(subtract(v1, v2));
1644 >        rs[4].assertValue(subtract(v1, v2));
1645  
1356        checkCompletedNormally(h, subtract(v1, v2));
1646          checkCompletedNormally(f, v1);
1647          checkCompletedNormally(g, v2);
1359        r.assertInvoked();
1648      }}
1649  
1650      /**
1651       * thenCombine result completes exceptionally after exceptional
1652       * completion of either source
1653       */
1654 <    public void testThenCombine_exceptionalCompletion() {
1654 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1655          for (ExecutionMode m : ExecutionMode.values())
1368        for (boolean createIncomplete : new boolean[] { true, false })
1656          for (boolean fFirst : new boolean[] { true, false })
1657 +        for (boolean failFirst : new boolean[] { true, false })
1658          for (Integer v1 : new Integer[] { 1, null })
1659      {
1660          final CompletableFuture<Integer> f = new CompletableFuture<>();
1661          final CompletableFuture<Integer> g = new CompletableFuture<>();
1662          final CFException ex = new CFException();
1663 <        final SubtractFunction r = new SubtractFunction(m);
1664 <
1665 <        (fFirst ? f : g).complete(v1);
1666 <        if (!createIncomplete)
1667 <            (!fFirst ? f : g).completeExceptionally(ex);
1668 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1669 <        if (createIncomplete) {
1670 <            checkIncomplete(h);
1671 <            (!fFirst ? f : g).completeExceptionally(ex);
1672 <        }
1673 <
1674 <        checkCompletedWithWrappedCFException(h, ex);
1675 <        r.assertNotInvoked();
1676 <        checkCompletedNormally(fFirst ? f : g, v1);
1677 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1663 >        final SubtractFunction r1 = new SubtractFunction(m);
1664 >        final SubtractFunction r2 = new SubtractFunction(m);
1665 >        final SubtractFunction r3 = new SubtractFunction(m);
1666 >
1667 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1668 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1669 >        final Callable<Boolean> complete1 = failFirst ?
1670 >            () -> fst.completeExceptionally(ex) :
1671 >            () -> fst.complete(v1);
1672 >        final Callable<Boolean> complete2 = failFirst ?
1673 >            () -> snd.complete(v1) :
1674 >            () -> snd.completeExceptionally(ex);
1675 >
1676 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1677 >        assertTrue(complete1.call());
1678 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1679 >        checkIncomplete(h1);
1680 >        checkIncomplete(h2);
1681 >        assertTrue(complete2.call());
1682 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1683 >
1684 >        checkCompletedWithWrappedException(h1, ex);
1685 >        checkCompletedWithWrappedException(h2, ex);
1686 >        checkCompletedWithWrappedException(h3, ex);
1687 >        r1.assertNotInvoked();
1688 >        r2.assertNotInvoked();
1689 >        r3.assertNotInvoked();
1690 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1691 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1692      }}
1693  
1694      /**
1695       * thenCombine result completes exceptionally if either source cancelled
1696       */
1697 <    public void testThenCombine_sourceCancelled() {
1697 >    public void testThenCombine_sourceCancelled() throws Throwable {
1698          for (ExecutionMode m : ExecutionMode.values())
1699          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1398        for (boolean createIncomplete : new boolean[] { true, false })
1700          for (boolean fFirst : new boolean[] { true, false })
1701 +        for (boolean failFirst : new boolean[] { true, false })
1702          for (Integer v1 : new Integer[] { 1, null })
1703      {
1704          final CompletableFuture<Integer> f = new CompletableFuture<>();
1705          final CompletableFuture<Integer> g = new CompletableFuture<>();
1706 <        final SubtractFunction r = new SubtractFunction(m);
1706 >        final SubtractFunction r1 = new SubtractFunction(m);
1707 >        final SubtractFunction r2 = new SubtractFunction(m);
1708 >        final SubtractFunction r3 = new SubtractFunction(m);
1709  
1710 <        (fFirst ? f : g).complete(v1);
1711 <        if (!createIncomplete)
1712 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1713 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1714 <        if (createIncomplete) {
1715 <            checkIncomplete(h);
1716 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1717 <        }
1710 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1711 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1712 >        final Callable<Boolean> complete1 = failFirst ?
1713 >            () -> fst.cancel(mayInterruptIfRunning) :
1714 >            () -> fst.complete(v1);
1715 >        final Callable<Boolean> complete2 = failFirst ?
1716 >            () -> snd.complete(v1) :
1717 >            () -> snd.cancel(mayInterruptIfRunning);
1718  
1719 <        checkCompletedWithWrappedCancellationException(h);
1720 <        checkCancelled(!fFirst ? f : g);
1721 <        r.assertNotInvoked();
1722 <        checkCompletedNormally(fFirst ? f : g, v1);
1719 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1720 >        assertTrue(complete1.call());
1721 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1722 >        checkIncomplete(h1);
1723 >        checkIncomplete(h2);
1724 >        assertTrue(complete2.call());
1725 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1726 >
1727 >        checkCompletedWithWrappedCancellationException(h1);
1728 >        checkCompletedWithWrappedCancellationException(h2);
1729 >        checkCompletedWithWrappedCancellationException(h3);
1730 >        r1.assertNotInvoked();
1731 >        r2.assertNotInvoked();
1732 >        r3.assertNotInvoked();
1733 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1734 >        checkCancelled(failFirst ? fst : snd);
1735      }}
1736  
1737      /**
# Line 1429 | Line 1745 | public class CompletableFutureTest exten
1745      {
1746          final CompletableFuture<Integer> f = new CompletableFuture<>();
1747          final CompletableFuture<Integer> g = new CompletableFuture<>();
1748 <        final FailingBiFunction r = new FailingBiFunction(m);
1749 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1748 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1749 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1750 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1751 >
1752 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1753 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1754 >        final Integer w1 =  fFirst ? v1 : v2;
1755 >        final Integer w2 = !fFirst ? v1 : v2;
1756 >
1757 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1758 >        assertTrue(fst.complete(w1));
1759 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1760 >        assertTrue(snd.complete(w2));
1761 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1762  
1763 <        if (fFirst) {
1764 <            f.complete(v1);
1765 <            g.complete(v2);
1766 <        } else {
1767 <            g.complete(v2);
1768 <            f.complete(v1);
1441 <        }
1442 <
1443 <        checkCompletedWithWrappedCFException(h);
1763 >        checkCompletedWithWrappedCFException(h1);
1764 >        checkCompletedWithWrappedCFException(h2);
1765 >        checkCompletedWithWrappedCFException(h3);
1766 >        r1.assertInvoked();
1767 >        r2.assertInvoked();
1768 >        r3.assertInvoked();
1769          checkCompletedNormally(f, v1);
1770          checkCompletedNormally(g, v2);
1771      }}
# Line 1451 | Line 1776 | public class CompletableFutureTest exten
1776       */
1777      public void testThenAcceptBoth_normalCompletion() {
1778          for (ExecutionMode m : ExecutionMode.values())
1454        for (boolean createIncomplete : new boolean[] { true, false })
1779          for (boolean fFirst : new boolean[] { true, false })
1780          for (Integer v1 : new Integer[] { 1, null })
1781          for (Integer v2 : new Integer[] { 2, null })
1782      {
1783          final CompletableFuture<Integer> f = new CompletableFuture<>();
1784          final CompletableFuture<Integer> g = new CompletableFuture<>();
1785 <        final SubtractAction r = new SubtractAction(m);
1786 <
1787 <        if (fFirst) f.complete(v1); else g.complete(v2);
1788 <        if (!createIncomplete)
1789 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1790 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1791 <        if (createIncomplete) {
1792 <            checkIncomplete(h);
1793 <            r.assertNotInvoked();
1794 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1795 <        }
1785 >        final SubtractAction r1 = new SubtractAction(m);
1786 >        final SubtractAction r2 = new SubtractAction(m);
1787 >        final SubtractAction r3 = new SubtractAction(m);
1788 >
1789 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1790 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1791 >        final Integer w1 =  fFirst ? v1 : v2;
1792 >        final Integer w2 = !fFirst ? v1 : v2;
1793 >
1794 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1795 >        assertTrue(fst.complete(w1));
1796 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1797 >        checkIncomplete(h1);
1798 >        checkIncomplete(h2);
1799 >        r1.assertNotInvoked();
1800 >        r2.assertNotInvoked();
1801 >        assertTrue(snd.complete(w2));
1802 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1803  
1804 <        checkCompletedNormally(h, null);
1805 <        r.assertValue(subtract(v1, v2));
1804 >        checkCompletedNormally(h1, null);
1805 >        checkCompletedNormally(h2, null);
1806 >        checkCompletedNormally(h3, null);
1807 >        r1.assertValue(subtract(v1, v2));
1808 >        r2.assertValue(subtract(v1, v2));
1809 >        r3.assertValue(subtract(v1, v2));
1810          checkCompletedNormally(f, v1);
1811          checkCompletedNormally(g, v2);
1812      }}
# Line 1480 | Line 1815 | public class CompletableFutureTest exten
1815       * thenAcceptBoth result completes exceptionally after exceptional
1816       * completion of either source
1817       */
1818 <    public void testThenAcceptBoth_exceptionalCompletion() {
1818 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1819          for (ExecutionMode m : ExecutionMode.values())
1485        for (boolean createIncomplete : new boolean[] { true, false })
1820          for (boolean fFirst : new boolean[] { true, false })
1821 +        for (boolean failFirst : new boolean[] { true, false })
1822          for (Integer v1 : new Integer[] { 1, null })
1823      {
1824          final CompletableFuture<Integer> f = new CompletableFuture<>();
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826          final CFException ex = new CFException();
1827 <        final SubtractAction r = new SubtractAction(m);
1828 <
1829 <        (fFirst ? f : g).complete(v1);
1830 <        if (!createIncomplete)
1831 <            (!fFirst ? f : g).completeExceptionally(ex);
1832 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1833 <        if (createIncomplete) {
1834 <            checkIncomplete(h);
1835 <            (!fFirst ? f : g).completeExceptionally(ex);
1836 <        }
1837 <
1838 <        checkCompletedWithWrappedCFException(h, ex);
1839 <        r.assertNotInvoked();
1840 <        checkCompletedNormally(fFirst ? f : g, v1);
1841 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1827 >        final SubtractAction r1 = new SubtractAction(m);
1828 >        final SubtractAction r2 = new SubtractAction(m);
1829 >        final SubtractAction r3 = new SubtractAction(m);
1830 >
1831 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1832 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1833 >        final Callable<Boolean> complete1 = failFirst ?
1834 >            () -> fst.completeExceptionally(ex) :
1835 >            () -> fst.complete(v1);
1836 >        final Callable<Boolean> complete2 = failFirst ?
1837 >            () -> snd.complete(v1) :
1838 >            () -> snd.completeExceptionally(ex);
1839 >
1840 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1841 >        assertTrue(complete1.call());
1842 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1843 >        checkIncomplete(h1);
1844 >        checkIncomplete(h2);
1845 >        assertTrue(complete2.call());
1846 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1847 >
1848 >        checkCompletedWithWrappedException(h1, ex);
1849 >        checkCompletedWithWrappedException(h2, ex);
1850 >        checkCompletedWithWrappedException(h3, ex);
1851 >        r1.assertNotInvoked();
1852 >        r2.assertNotInvoked();
1853 >        r3.assertNotInvoked();
1854 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1855 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1856      }}
1857  
1858      /**
1859       * thenAcceptBoth result completes exceptionally if either source cancelled
1860       */
1861 <    public void testThenAcceptBoth_sourceCancelled() {
1861 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1862          for (ExecutionMode m : ExecutionMode.values())
1863          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1515        for (boolean createIncomplete : new boolean[] { true, false })
1864          for (boolean fFirst : new boolean[] { true, false })
1865 +        for (boolean failFirst : new boolean[] { true, false })
1866          for (Integer v1 : new Integer[] { 1, null })
1867      {
1868          final CompletableFuture<Integer> f = new CompletableFuture<>();
1869          final CompletableFuture<Integer> g = new CompletableFuture<>();
1870 <        final SubtractAction r = new SubtractAction(m);
1870 >        final SubtractAction r1 = new SubtractAction(m);
1871 >        final SubtractAction r2 = new SubtractAction(m);
1872 >        final SubtractAction r3 = new SubtractAction(m);
1873  
1874 <        (fFirst ? f : g).complete(v1);
1875 <        if (!createIncomplete)
1876 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1877 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1878 <        if (createIncomplete) {
1879 <            checkIncomplete(h);
1880 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1881 <        }
1874 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1875 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1876 >        final Callable<Boolean> complete1 = failFirst ?
1877 >            () -> fst.cancel(mayInterruptIfRunning) :
1878 >            () -> fst.complete(v1);
1879 >        final Callable<Boolean> complete2 = failFirst ?
1880 >            () -> snd.complete(v1) :
1881 >            () -> snd.cancel(mayInterruptIfRunning);
1882  
1883 <        checkCompletedWithWrappedCancellationException(h);
1884 <        checkCancelled(!fFirst ? f : g);
1885 <        r.assertNotInvoked();
1886 <        checkCompletedNormally(fFirst ? f : g, v1);
1883 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1884 >        assertTrue(complete1.call());
1885 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1886 >        checkIncomplete(h1);
1887 >        checkIncomplete(h2);
1888 >        assertTrue(complete2.call());
1889 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1890 >
1891 >        checkCompletedWithWrappedCancellationException(h1);
1892 >        checkCompletedWithWrappedCancellationException(h2);
1893 >        checkCompletedWithWrappedCancellationException(h3);
1894 >        r1.assertNotInvoked();
1895 >        r2.assertNotInvoked();
1896 >        r3.assertNotInvoked();
1897 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1898 >        checkCancelled(failFirst ? fst : snd);
1899      }}
1900  
1901      /**
# Line 1546 | Line 1909 | public class CompletableFutureTest exten
1909      {
1910          final CompletableFuture<Integer> f = new CompletableFuture<>();
1911          final CompletableFuture<Integer> g = new CompletableFuture<>();
1912 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1913 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1912 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1913 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1914 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1915 >
1916 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1917 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1918 >        final Integer w1 =  fFirst ? v1 : v2;
1919 >        final Integer w2 = !fFirst ? v1 : v2;
1920 >
1921 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1922 >        assertTrue(fst.complete(w1));
1923 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1924 >        assertTrue(snd.complete(w2));
1925 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1926  
1927 <        if (fFirst) {
1928 <            f.complete(v1);
1929 <            g.complete(v2);
1930 <        } else {
1931 <            g.complete(v2);
1932 <            f.complete(v1);
1558 <        }
1559 <
1560 <        checkCompletedWithWrappedCFException(h);
1927 >        checkCompletedWithWrappedCFException(h1);
1928 >        checkCompletedWithWrappedCFException(h2);
1929 >        checkCompletedWithWrappedCFException(h3);
1930 >        r1.assertInvoked();
1931 >        r2.assertInvoked();
1932 >        r3.assertInvoked();
1933          checkCompletedNormally(f, v1);
1934          checkCompletedNormally(g, v2);
1935      }}
# Line 1568 | Line 1940 | public class CompletableFutureTest exten
1940       */
1941      public void testRunAfterBoth_normalCompletion() {
1942          for (ExecutionMode m : ExecutionMode.values())
1571        for (boolean createIncomplete : new boolean[] { true, false })
1943          for (boolean fFirst : new boolean[] { true, false })
1944          for (Integer v1 : new Integer[] { 1, null })
1945          for (Integer v2 : new Integer[] { 2, null })
1946      {
1947          final CompletableFuture<Integer> f = new CompletableFuture<>();
1948          final CompletableFuture<Integer> g = new CompletableFuture<>();
1949 <        final Noop r = new Noop(m);
1950 <
1951 <        if (fFirst) f.complete(v1); else g.complete(v2);
1952 <        if (!createIncomplete)
1953 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1954 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1955 <        if (createIncomplete) {
1956 <            checkIncomplete(h);
1957 <            r.assertNotInvoked();
1958 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1959 <        }
1949 >        final Noop r1 = new Noop(m);
1950 >        final Noop r2 = new Noop(m);
1951 >        final Noop r3 = new Noop(m);
1952 >
1953 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1954 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1955 >        final Integer w1 =  fFirst ? v1 : v2;
1956 >        final Integer w2 = !fFirst ? v1 : v2;
1957 >
1958 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1959 >        assertTrue(fst.complete(w1));
1960 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1961 >        checkIncomplete(h1);
1962 >        checkIncomplete(h2);
1963 >        r1.assertNotInvoked();
1964 >        r2.assertNotInvoked();
1965 >        assertTrue(snd.complete(w2));
1966 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1967  
1968 <        checkCompletedNormally(h, null);
1969 <        r.assertInvoked();
1968 >        checkCompletedNormally(h1, null);
1969 >        checkCompletedNormally(h2, null);
1970 >        checkCompletedNormally(h3, null);
1971 >        r1.assertInvoked();
1972 >        r2.assertInvoked();
1973 >        r3.assertInvoked();
1974          checkCompletedNormally(f, v1);
1975          checkCompletedNormally(g, v2);
1976      }}
# Line 1597 | Line 1979 | public class CompletableFutureTest exten
1979       * runAfterBoth result completes exceptionally after exceptional
1980       * completion of either source
1981       */
1982 <    public void testRunAfterBoth_exceptionalCompletion() {
1982 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1983          for (ExecutionMode m : ExecutionMode.values())
1602        for (boolean createIncomplete : new boolean[] { true, false })
1984          for (boolean fFirst : new boolean[] { true, false })
1985 +        for (boolean failFirst : new boolean[] { true, false })
1986          for (Integer v1 : new Integer[] { 1, null })
1987      {
1988          final CompletableFuture<Integer> f = new CompletableFuture<>();
1989          final CompletableFuture<Integer> g = new CompletableFuture<>();
1990          final CFException ex = new CFException();
1991 <        final Noop r = new Noop(m);
1992 <
1993 <        (fFirst ? f : g).complete(v1);
1994 <        if (!createIncomplete)
1995 <            (!fFirst ? f : g).completeExceptionally(ex);
1996 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1997 <        if (createIncomplete) {
1998 <            checkIncomplete(h);
1999 <            (!fFirst ? f : g).completeExceptionally(ex);
2000 <        }
2001 <
2002 <        checkCompletedWithWrappedCFException(h, ex);
2003 <        r.assertNotInvoked();
2004 <        checkCompletedNormally(fFirst ? f : g, v1);
2005 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1991 >        final Noop r1 = new Noop(m);
1992 >        final Noop r2 = new Noop(m);
1993 >        final Noop r3 = new Noop(m);
1994 >
1995 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1996 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1997 >        final Callable<Boolean> complete1 = failFirst ?
1998 >            () -> fst.completeExceptionally(ex) :
1999 >            () -> fst.complete(v1);
2000 >        final Callable<Boolean> complete2 = failFirst ?
2001 >            () -> snd.complete(v1) :
2002 >            () -> snd.completeExceptionally(ex);
2003 >
2004 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2005 >        assertTrue(complete1.call());
2006 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2007 >        checkIncomplete(h1);
2008 >        checkIncomplete(h2);
2009 >        assertTrue(complete2.call());
2010 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2011 >
2012 >        checkCompletedWithWrappedException(h1, ex);
2013 >        checkCompletedWithWrappedException(h2, ex);
2014 >        checkCompletedWithWrappedException(h3, ex);
2015 >        r1.assertNotInvoked();
2016 >        r2.assertNotInvoked();
2017 >        r3.assertNotInvoked();
2018 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2019 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2020      }}
2021  
2022      /**
2023       * runAfterBoth result completes exceptionally if either source cancelled
2024       */
2025 <    public void testRunAfterBoth_sourceCancelled() {
2025 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2026          for (ExecutionMode m : ExecutionMode.values())
2027          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1632        for (boolean createIncomplete : new boolean[] { true, false })
2028          for (boolean fFirst : new boolean[] { true, false })
2029 +        for (boolean failFirst : new boolean[] { true, false })
2030          for (Integer v1 : new Integer[] { 1, null })
2031      {
2032          final CompletableFuture<Integer> f = new CompletableFuture<>();
2033          final CompletableFuture<Integer> g = new CompletableFuture<>();
2034 <        final Noop r = new Noop(m);
2034 >        final Noop r1 = new Noop(m);
2035 >        final Noop r2 = new Noop(m);
2036 >        final Noop r3 = new Noop(m);
2037  
2038 +        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2039 +        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2040 +        final Callable<Boolean> complete1 = failFirst ?
2041 +            () -> fst.cancel(mayInterruptIfRunning) :
2042 +            () -> fst.complete(v1);
2043 +        final Callable<Boolean> complete2 = failFirst ?
2044 +            () -> snd.complete(v1) :
2045 +            () -> snd.cancel(mayInterruptIfRunning);
2046  
2047 <        (fFirst ? f : g).complete(v1);
2048 <        if (!createIncomplete)
2049 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2050 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2051 <        if (createIncomplete) {
2052 <            checkIncomplete(h);
2053 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1648 <        }
2047 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2048 >        assertTrue(complete1.call());
2049 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2050 >        checkIncomplete(h1);
2051 >        checkIncomplete(h2);
2052 >        assertTrue(complete2.call());
2053 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2054  
2055 <        checkCompletedWithWrappedCancellationException(h);
2056 <        checkCancelled(!fFirst ? f : g);
2057 <        r.assertNotInvoked();
2058 <        checkCompletedNormally(fFirst ? f : g, v1);
2055 >        checkCompletedWithWrappedCancellationException(h1);
2056 >        checkCompletedWithWrappedCancellationException(h2);
2057 >        checkCompletedWithWrappedCancellationException(h3);
2058 >        r1.assertNotInvoked();
2059 >        r2.assertNotInvoked();
2060 >        r3.assertNotInvoked();
2061 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2062 >        checkCancelled(failFirst ? fst : snd);
2063      }}
2064  
2065      /**
# Line 1666 | Line 2075 | public class CompletableFutureTest exten
2075          final CompletableFuture<Integer> g = new CompletableFuture<>();
2076          final FailingRunnable r1 = new FailingRunnable(m);
2077          final FailingRunnable r2 = new FailingRunnable(m);
2078 +        final FailingRunnable r3 = new FailingRunnable(m);
2079  
2080 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2081 <        if (fFirst) {
2082 <            f.complete(v1);
2083 <            g.complete(v2);
2084 <        } else {
2085 <            g.complete(v2);
2086 <            f.complete(v1);
2087 <        }
2088 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2080 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2081 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2082 >        final Integer w1 =  fFirst ? v1 : v2;
2083 >        final Integer w2 = !fFirst ? v1 : v2;
2084 >
2085 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2086 >        assertTrue(fst.complete(w1));
2087 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2088 >        assertTrue(snd.complete(w2));
2089 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2090  
2091          checkCompletedWithWrappedCFException(h1);
2092          checkCompletedWithWrappedCFException(h2);
2093 +        checkCompletedWithWrappedCFException(h3);
2094 +        r1.assertInvoked();
2095 +        r2.assertInvoked();
2096 +        r3.assertInvoked();
2097          checkCompletedNormally(f, v1);
2098          checkCompletedNormally(g, v2);
2099      }}
# Line 1752 | Line 2167 | public class CompletableFutureTest exten
2167          rs[0].assertNotInvoked();
2168          rs[1].assertNotInvoked();
2169          f.completeExceptionally(ex);
2170 <        checkCompletedWithWrappedCFException(h0, ex);
2171 <        checkCompletedWithWrappedCFException(h1, ex);
2170 >        checkCompletedWithWrappedException(h0, ex);
2171 >        checkCompletedWithWrappedException(h1, ex);
2172          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2173          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2174 <        checkCompletedWithWrappedCFException(h2, ex);
2175 <        checkCompletedWithWrappedCFException(h3, ex);
2174 >        checkCompletedWithWrappedException(h2, ex);
2175 >        checkCompletedWithWrappedException(h3, ex);
2176          g.complete(v1);
2177  
2178          // unspecified behavior - both source completions available
# Line 1765 | Line 2180 | public class CompletableFutureTest exten
2180          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2181          try {
2182              assertEquals(inc(v1), h4.join());
2183 <            rs[4].assertInvoked();
2183 >            rs[4].assertValue(inc(v1));
2184          } catch (CompletionException ok) {
2185 <            checkCompletedWithWrappedCFException(h4, ex);
2185 >            checkCompletedWithWrappedException(h4, ex);
2186              rs[4].assertNotInvoked();
2187          }
2188          try {
2189              assertEquals(inc(v1), h5.join());
2190 <            rs[5].assertInvoked();
2190 >            rs[5].assertValue(inc(v1));
2191          } catch (CompletionException ok) {
2192 <            checkCompletedWithWrappedCFException(h5, ex);
2192 >            checkCompletedWithWrappedException(h5, ex);
2193              rs[5].assertNotInvoked();
2194          }
2195  
2196 <        checkCompletedWithWrappedCFException(f, ex);
2196 >        checkCompletedExceptionally(f, ex);
2197          checkCompletedNormally(g, v1);
2198 <        checkCompletedWithWrappedCFException(h0, ex);
2199 <        checkCompletedWithWrappedCFException(h1, ex);
2200 <        checkCompletedWithWrappedCFException(h2, ex);
2201 <        checkCompletedWithWrappedCFException(h3, ex);
2202 <        checkCompletedWithWrappedCFException(h4, ex);
2198 >        checkCompletedWithWrappedException(h0, ex);
2199 >        checkCompletedWithWrappedException(h1, ex);
2200 >        checkCompletedWithWrappedException(h2, ex);
2201 >        checkCompletedWithWrappedException(h3, ex);
2202 >        checkCompletedWithWrappedException(h4, ex);
2203          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2204      }}
2205  
2206 +    public void testApplyToEither_exceptionalCompletion2() {
2207 +        for (ExecutionMode m : ExecutionMode.values())
2208 +        for (boolean fFirst : new boolean[] { true, false })
2209 +        for (Integer v1 : new Integer[] { 1, null })
2210 +    {
2211 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2212 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2213 +        final CFException ex = new CFException();
2214 +        final IncFunction[] rs = new IncFunction[6];
2215 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2216 +
2217 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2218 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2219 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2220 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2221 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2222 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2223 +
2224 +        // unspecified behavior - both source completions available
2225 +        try {
2226 +            assertEquals(inc(v1), h0.join());
2227 +            rs[0].assertValue(inc(v1));
2228 +        } catch (CompletionException ok) {
2229 +            checkCompletedWithWrappedException(h0, ex);
2230 +            rs[0].assertNotInvoked();
2231 +        }
2232 +        try {
2233 +            assertEquals(inc(v1), h1.join());
2234 +            rs[1].assertValue(inc(v1));
2235 +        } catch (CompletionException ok) {
2236 +            checkCompletedWithWrappedException(h1, ex);
2237 +            rs[1].assertNotInvoked();
2238 +        }
2239 +        try {
2240 +            assertEquals(inc(v1), h2.join());
2241 +            rs[2].assertValue(inc(v1));
2242 +        } catch (CompletionException ok) {
2243 +            checkCompletedWithWrappedException(h2, ex);
2244 +            rs[2].assertNotInvoked();
2245 +        }
2246 +        try {
2247 +            assertEquals(inc(v1), h3.join());
2248 +            rs[3].assertValue(inc(v1));
2249 +        } catch (CompletionException ok) {
2250 +            checkCompletedWithWrappedException(h3, ex);
2251 +            rs[3].assertNotInvoked();
2252 +        }
2253 +
2254 +        checkCompletedNormally(f, v1);
2255 +        checkCompletedExceptionally(g, ex);
2256 +    }}
2257 +
2258      /**
2259       * applyToEither result completes exceptionally if either source cancelled
2260       */
# Line 1821 | Line 2288 | public class CompletableFutureTest exten
2288          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2289          try {
2290              assertEquals(inc(v1), h4.join());
2291 <            rs[4].assertInvoked();
2291 >            rs[4].assertValue(inc(v1));
2292          } catch (CompletionException ok) {
2293              checkCompletedWithWrappedCancellationException(h4);
2294              rs[4].assertNotInvoked();
2295          }
2296          try {
2297              assertEquals(inc(v1), h5.join());
2298 <            rs[5].assertInvoked();
2298 >            rs[5].assertValue(inc(v1));
2299          } catch (CompletionException ok) {
2300              checkCompletedWithWrappedCancellationException(h5);
2301              rs[5].assertNotInvoked();
# Line 1843 | Line 2310 | public class CompletableFutureTest exten
2310          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2311      }}
2312  
2313 +    public void testApplyToEither_sourceCancelled2() {
2314 +        for (ExecutionMode m : ExecutionMode.values())
2315 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2316 +        for (boolean fFirst : new boolean[] { true, false })
2317 +        for (Integer v1 : new Integer[] { 1, null })
2318 +    {
2319 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2320 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2321 +        final IncFunction[] rs = new IncFunction[6];
2322 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2323 +
2324 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2325 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2326 +        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2327 +        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2328 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2329 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2330 +
2331 +        // unspecified behavior - both source completions available
2332 +        try {
2333 +            assertEquals(inc(v1), h0.join());
2334 +            rs[0].assertValue(inc(v1));
2335 +        } catch (CompletionException ok) {
2336 +            checkCompletedWithWrappedCancellationException(h0);
2337 +            rs[0].assertNotInvoked();
2338 +        }
2339 +        try {
2340 +            assertEquals(inc(v1), h1.join());
2341 +            rs[1].assertValue(inc(v1));
2342 +        } catch (CompletionException ok) {
2343 +            checkCompletedWithWrappedCancellationException(h1);
2344 +            rs[1].assertNotInvoked();
2345 +        }
2346 +        try {
2347 +            assertEquals(inc(v1), h2.join());
2348 +            rs[2].assertValue(inc(v1));
2349 +        } catch (CompletionException ok) {
2350 +            checkCompletedWithWrappedCancellationException(h2);
2351 +            rs[2].assertNotInvoked();
2352 +        }
2353 +        try {
2354 +            assertEquals(inc(v1), h3.join());
2355 +            rs[3].assertValue(inc(v1));
2356 +        } catch (CompletionException ok) {
2357 +            checkCompletedWithWrappedCancellationException(h3);
2358 +            rs[3].assertNotInvoked();
2359 +        }
2360 +
2361 +        checkCompletedNormally(f, v1);
2362 +        checkCancelled(g);
2363 +    }}
2364 +
2365      /**
2366       * applyToEither result completes exceptionally if action does
2367       */
# Line 1957 | Line 2476 | public class CompletableFutureTest exten
2476          rs[0].assertNotInvoked();
2477          rs[1].assertNotInvoked();
2478          f.completeExceptionally(ex);
2479 <        checkCompletedWithWrappedCFException(h0, ex);
2480 <        checkCompletedWithWrappedCFException(h1, ex);
2479 >        checkCompletedWithWrappedException(h0, ex);
2480 >        checkCompletedWithWrappedException(h1, ex);
2481          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2482          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2483 <        checkCompletedWithWrappedCFException(h2, ex);
2484 <        checkCompletedWithWrappedCFException(h3, ex);
2483 >        checkCompletedWithWrappedException(h2, ex);
2484 >        checkCompletedWithWrappedException(h3, ex);
2485  
2486          g.complete(v1);
2487  
# Line 1973 | Line 2492 | public class CompletableFutureTest exten
2492              assertNull(h4.join());
2493              rs[4].assertValue(v1);
2494          } catch (CompletionException ok) {
2495 <            checkCompletedWithWrappedCFException(h4, ex);
2495 >            checkCompletedWithWrappedException(h4, ex);
2496              rs[4].assertNotInvoked();
2497          }
2498          try {
2499              assertNull(h5.join());
2500              rs[5].assertValue(v1);
2501          } catch (CompletionException ok) {
2502 <            checkCompletedWithWrappedCFException(h5, ex);
2502 >            checkCompletedWithWrappedException(h5, ex);
2503              rs[5].assertNotInvoked();
2504          }
2505  
2506 <        checkCompletedWithWrappedCFException(f, ex);
2506 >        checkCompletedExceptionally(f, ex);
2507          checkCompletedNormally(g, v1);
2508 <        checkCompletedWithWrappedCFException(h0, ex);
2509 <        checkCompletedWithWrappedCFException(h1, ex);
2510 <        checkCompletedWithWrappedCFException(h2, ex);
2511 <        checkCompletedWithWrappedCFException(h3, ex);
2512 <        checkCompletedWithWrappedCFException(h4, ex);
2508 >        checkCompletedWithWrappedException(h0, ex);
2509 >        checkCompletedWithWrappedException(h1, ex);
2510 >        checkCompletedWithWrappedException(h2, ex);
2511 >        checkCompletedWithWrappedException(h3, ex);
2512 >        checkCompletedWithWrappedException(h4, ex);
2513          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2514      }}
2515  
2516 +    public void testAcceptEither_exceptionalCompletion2() {
2517 +        for (ExecutionMode m : ExecutionMode.values())
2518 +        for (boolean fFirst : new boolean[] { true, false })
2519 +        for (Integer v1 : new Integer[] { 1, null })
2520 +    {
2521 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2522 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2523 +        final CFException ex = new CFException();
2524 +        final NoopConsumer[] rs = new NoopConsumer[6];
2525 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2526 +
2527 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2528 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2529 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2530 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2531 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2532 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2533 +
2534 +        // unspecified behavior - both source completions available
2535 +        try {
2536 +            assertEquals(null, h0.join());
2537 +            rs[0].assertValue(v1);
2538 +        } catch (CompletionException ok) {
2539 +            checkCompletedWithWrappedException(h0, ex);
2540 +            rs[0].assertNotInvoked();
2541 +        }
2542 +        try {
2543 +            assertEquals(null, h1.join());
2544 +            rs[1].assertValue(v1);
2545 +        } catch (CompletionException ok) {
2546 +            checkCompletedWithWrappedException(h1, ex);
2547 +            rs[1].assertNotInvoked();
2548 +        }
2549 +        try {
2550 +            assertEquals(null, h2.join());
2551 +            rs[2].assertValue(v1);
2552 +        } catch (CompletionException ok) {
2553 +            checkCompletedWithWrappedException(h2, ex);
2554 +            rs[2].assertNotInvoked();
2555 +        }
2556 +        try {
2557 +            assertEquals(null, h3.join());
2558 +            rs[3].assertValue(v1);
2559 +        } catch (CompletionException ok) {
2560 +            checkCompletedWithWrappedException(h3, ex);
2561 +            rs[3].assertNotInvoked();
2562 +        }
2563 +
2564 +        checkCompletedNormally(f, v1);
2565 +        checkCompletedExceptionally(g, ex);
2566 +    }}
2567 +
2568      /**
2569       * acceptEither result completes exceptionally if either source cancelled
2570       */
# Line 2095 | Line 2666 | public class CompletableFutureTest exten
2666       * runAfterEither result completes normally after normal completion
2667       * of either source
2668       */
2669 <    public void testRunAfterEither_normalCompletion1() {
2669 >    public void testRunAfterEither_normalCompletion() {
2670          for (ExecutionMode m : ExecutionMode.values())
2671          for (Integer v1 : new Integer[] { 1, null })
2672          for (Integer v2 : new Integer[] { 2, null })
2673      {
2674          final CompletableFuture<Integer> f = new CompletableFuture<>();
2675          final CompletableFuture<Integer> g = new CompletableFuture<>();
2676 <        final Noop r = new Noop(m);
2677 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2676 >        final Noop[] rs = new Noop[6];
2677 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2678  
2679 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2680 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2681 +        checkIncomplete(h0);
2682 +        checkIncomplete(h1);
2683 +        rs[0].assertNotInvoked();
2684 +        rs[1].assertNotInvoked();
2685          f.complete(v1);
2686 <        checkCompletedNormally(h, null);
2687 <        r.assertInvoked();
2688 <        g.complete(v2);
2689 <
2690 <        checkCompletedNormally(f, v1);
2691 <        checkCompletedNormally(g, v2);
2692 <        checkCompletedNormally(h, null);
2693 <        r.assertInvoked();
2694 <    }}
2695 <
2119 <    public void testRunAfterEither_normalCompletion2() {
2120 <        for (ExecutionMode m : ExecutionMode.values())
2121 <        for (Integer v1 : new Integer[] { 1, null })
2122 <        for (Integer v2 : new Integer[] { 2, null })
2123 <    {
2124 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2126 <        final Noop r = new Noop(m);
2127 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2686 >        checkCompletedNormally(h0, null);
2687 >        checkCompletedNormally(h1, null);
2688 >        rs[0].assertInvoked();
2689 >        rs[1].assertInvoked();
2690 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2691 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2692 >        checkCompletedNormally(h2, null);
2693 >        checkCompletedNormally(h3, null);
2694 >        rs[2].assertInvoked();
2695 >        rs[3].assertInvoked();
2696  
2697          g.complete(v2);
2130        checkCompletedNormally(h, null);
2131        r.assertInvoked();
2132        f.complete(v1);
2698  
2699 <        checkCompletedNormally(f, v1);
2700 <        checkCompletedNormally(g, v2);
2136 <        checkCompletedNormally(h, null);
2137 <        r.assertInvoked();
2138 <        }}
2699 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2700 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2701  
2140    public void testRunAfterEither_normalCompletion3() {
2141        for (ExecutionMode m : ExecutionMode.values())
2142        for (Integer v1 : new Integer[] { 1, null })
2143        for (Integer v2 : new Integer[] { 2, null })
2144    {
2145        final CompletableFuture<Integer> f = new CompletableFuture<>();
2146        final CompletableFuture<Integer> g = new CompletableFuture<>();
2147        final Noop r = new Noop(m);
2148
2149        f.complete(v1);
2150        g.complete(v2);
2151        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2152
2153        checkCompletedNormally(h, null);
2702          checkCompletedNormally(f, v1);
2703          checkCompletedNormally(g, v2);
2704 <        r.assertInvoked();
2704 >        checkCompletedNormally(h0, null);
2705 >        checkCompletedNormally(h1, null);
2706 >        checkCompletedNormally(h2, null);
2707 >        checkCompletedNormally(h3, null);
2708 >        checkCompletedNormally(h4, null);
2709 >        checkCompletedNormally(h5, null);
2710 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2711      }}
2712  
2713      /**
2714       * runAfterEither result completes exceptionally after exceptional
2715       * completion of either source
2716       */
2717 <    public void testRunAfterEither_exceptionalCompletion1() {
2164 <        for (ExecutionMode m : ExecutionMode.values())
2165 <        for (Integer v1 : new Integer[] { 1, null })
2166 <    {
2167 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2168 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2169 <        final Noop r = new Noop(m);
2170 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2171 <        final CFException ex = new CFException();
2172 <
2173 <        f.completeExceptionally(ex);
2174 <        checkCompletedWithWrappedCFException(h, ex);
2175 <        g.complete(v1);
2176 <
2177 <        r.assertNotInvoked();
2178 <        checkCompletedNormally(g, v1);
2179 <        checkCompletedWithWrappedCFException(f, ex);
2180 <        checkCompletedWithWrappedCFException(h, ex);
2181 <    }}
2182 <
2183 <    public void testRunAfterEither_exceptionalCompletion2() {
2717 >    public void testRunAfterEither_exceptionalCompletion() {
2718          for (ExecutionMode m : ExecutionMode.values())
2719          for (Integer v1 : new Integer[] { 1, null })
2720      {
2721          final CompletableFuture<Integer> f = new CompletableFuture<>();
2722          final CompletableFuture<Integer> g = new CompletableFuture<>();
2189        final Noop r = new Noop(m);
2190        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2723          final CFException ex = new CFException();
2724 +        final Noop[] rs = new Noop[6];
2725 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2726  
2727 <        g.completeExceptionally(ex);
2728 <        checkCompletedWithWrappedCFException(h, ex);
2729 <        f.complete(v1);
2730 <
2731 <        r.assertNotInvoked();
2732 <        checkCompletedNormally(f, v1);
2733 <        checkCompletedWithWrappedCFException(g, ex);
2734 <        checkCompletedWithWrappedCFException(h, ex);
2735 <    }}
2736 <
2737 <    public void testRunAfterEither_exceptionalCompletion3() {
2738 <        for (ExecutionMode m : ExecutionMode.values())
2739 <        for (Integer v1 : new Integer[] { 1, null })
2206 <    {
2207 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2208 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2209 <        final Noop r = new Noop(m);
2210 <        final CFException ex = new CFException();
2727 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2728 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2729 >        checkIncomplete(h0);
2730 >        checkIncomplete(h1);
2731 >        rs[0].assertNotInvoked();
2732 >        rs[1].assertNotInvoked();
2733 >        assertTrue(f.completeExceptionally(ex));
2734 >        checkCompletedWithWrappedException(h0, ex);
2735 >        checkCompletedWithWrappedException(h1, ex);
2736 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2737 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2738 >        checkCompletedWithWrappedException(h2, ex);
2739 >        checkCompletedWithWrappedException(h3, ex);
2740  
2741 <        g.completeExceptionally(ex);
2213 <        f.complete(v1);
2214 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2741 >        assertTrue(g.complete(v1));
2742  
2743 <        // unspecified behavior
2744 <        Integer v;
2743 >        // unspecified behavior - both source completions available
2744 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2745 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2746          try {
2747 <            assertNull(h.join());
2748 <            r.assertInvoked();
2747 >            assertNull(h4.join());
2748 >            rs[4].assertInvoked();
2749          } catch (CompletionException ok) {
2750 <            checkCompletedWithWrappedCFException(h, ex);
2751 <            r.assertNotInvoked();
2750 >            checkCompletedWithWrappedException(h4, ex);
2751 >            rs[4].assertNotInvoked();
2752          }
2225
2226        checkCompletedWithWrappedCFException(g, ex);
2227        checkCompletedNormally(f, v1);
2228    }}
2229
2230    public void testRunAfterEither_exceptionalCompletion4() {
2231        for (ExecutionMode m : ExecutionMode.values())
2232        for (Integer v1 : new Integer[] { 1, null })
2233    {
2234        final CompletableFuture<Integer> f = new CompletableFuture<>();
2235        final CompletableFuture<Integer> g = new CompletableFuture<>();
2236        final Noop r = new Noop(m);
2237        final CFException ex = new CFException();
2238
2239        f.completeExceptionally(ex);
2240        g.complete(v1);
2241        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2242
2243        // unspecified behavior
2244        Integer v;
2753          try {
2754 <            assertNull(h.join());
2755 <            r.assertInvoked();
2754 >            assertNull(h5.join());
2755 >            rs[5].assertInvoked();
2756          } catch (CompletionException ok) {
2757 <            checkCompletedWithWrappedCFException(h, ex);
2758 <            r.assertNotInvoked();
2757 >            checkCompletedWithWrappedException(h5, ex);
2758 >            rs[5].assertNotInvoked();
2759          }
2760  
2761 <        checkCompletedWithWrappedCFException(f, ex);
2761 >        checkCompletedExceptionally(f, ex);
2762          checkCompletedNormally(g, v1);
2763 +        checkCompletedWithWrappedException(h0, ex);
2764 +        checkCompletedWithWrappedException(h1, ex);
2765 +        checkCompletedWithWrappedException(h2, ex);
2766 +        checkCompletedWithWrappedException(h3, ex);
2767 +        checkCompletedWithWrappedException(h4, ex);
2768 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2769      }}
2770  
2771 <    /**
2258 <     * runAfterEither result completes exceptionally if action does
2259 <     */
2260 <    public void testRunAfterEither_actionFailed1() {
2771 >    public void testRunAfterEither_exceptionalCompletion2() {
2772          for (ExecutionMode m : ExecutionMode.values())
2773 +        for (boolean fFirst : new boolean[] { true, false })
2774          for (Integer v1 : new Integer[] { 1, null })
2263        for (Integer v2 : new Integer[] { 2, null })
2775      {
2776          final CompletableFuture<Integer> f = new CompletableFuture<>();
2777          final CompletableFuture<Integer> g = new CompletableFuture<>();
2778 <        final FailingRunnable r = new FailingRunnable(m);
2779 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2778 >        final CFException ex = new CFException();
2779 >        final Noop[] rs = new Noop[6];
2780 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2781  
2782 <        f.complete(v1);
2783 <        checkCompletedWithWrappedCFException(h);
2784 <        g.complete(v2);
2785 <        checkCompletedNormally(f, v1);
2786 <        checkCompletedNormally(g, v2);
2787 <    }}
2782 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2783 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2784 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2785 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2786 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2787 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2788  
2789 <    public void testRunAfterEither_actionFailed2() {
2790 <        for (ExecutionMode m : ExecutionMode.values())
2791 <        for (Integer v1 : new Integer[] { 1, null })
2792 <        for (Integer v2 : new Integer[] { 2, null })
2793 <    {
2794 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2795 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2796 <        final FailingRunnable r = new FailingRunnable(m);
2797 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2789 >        // unspecified behavior - both source completions available
2790 >        try {
2791 >            assertEquals(null, h0.join());
2792 >            rs[0].assertInvoked();
2793 >        } catch (CompletionException ok) {
2794 >            checkCompletedWithWrappedException(h0, ex);
2795 >            rs[0].assertNotInvoked();
2796 >        }
2797 >        try {
2798 >            assertEquals(null, h1.join());
2799 >            rs[1].assertInvoked();
2800 >        } catch (CompletionException ok) {
2801 >            checkCompletedWithWrappedException(h1, ex);
2802 >            rs[1].assertNotInvoked();
2803 >        }
2804 >        try {
2805 >            assertEquals(null, h2.join());
2806 >            rs[2].assertInvoked();
2807 >        } catch (CompletionException ok) {
2808 >            checkCompletedWithWrappedException(h2, ex);
2809 >            rs[2].assertNotInvoked();
2810 >        }
2811 >        try {
2812 >            assertEquals(null, h3.join());
2813 >            rs[3].assertInvoked();
2814 >        } catch (CompletionException ok) {
2815 >            checkCompletedWithWrappedException(h3, ex);
2816 >            rs[3].assertNotInvoked();
2817 >        }
2818  
2287        g.complete(v2);
2288        checkCompletedWithWrappedCFException(h);
2289        f.complete(v1);
2819          checkCompletedNormally(f, v1);
2820 <        checkCompletedNormally(g, v2);
2820 >        checkCompletedExceptionally(g, ex);
2821      }}
2822  
2823      /**
2824       * runAfterEither result completes exceptionally if either source cancelled
2825       */
2826 <    public void testRunAfterEither_sourceCancelled1() {
2826 >    public void testRunAfterEither_sourceCancelled() {
2827          for (ExecutionMode m : ExecutionMode.values())
2828          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2829          for (Integer v1 : new Integer[] { 1, null })
2830      {
2831          final CompletableFuture<Integer> f = new CompletableFuture<>();
2832          final CompletableFuture<Integer> g = new CompletableFuture<>();
2833 <        final Noop r = new Noop(m);
2834 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2306 <
2307 <        assertTrue(f.cancel(mayInterruptIfRunning));
2308 <        checkCompletedWithWrappedCancellationException(h);
2309 <        g.complete(v1);
2310 <
2311 <        checkCancelled(f);
2312 <        r.assertNotInvoked();
2313 <        checkCompletedNormally(g, v1);
2314 <        checkCompletedWithWrappedCancellationException(h);
2315 <    }}
2316 <
2317 <    public void testRunAfterEither_sourceCancelled2() {
2318 <        for (ExecutionMode m : ExecutionMode.values())
2319 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2320 <        for (Integer v1 : new Integer[] { 1, null })
2321 <    {
2322 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final Noop r = new Noop(m);
2325 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2326 <
2327 <        assertTrue(g.cancel(mayInterruptIfRunning));
2328 <        checkCompletedWithWrappedCancellationException(h);
2329 <        f.complete(v1);
2330 <
2331 <        checkCancelled(g);
2332 <        r.assertNotInvoked();
2333 <        checkCompletedNormally(f, v1);
2334 <        checkCompletedWithWrappedCancellationException(h);
2335 <    }}
2833 >        final Noop[] rs = new Noop[6];
2834 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2835  
2836 <    public void testRunAfterEither_sourceCancelled3() {
2837 <        for (ExecutionMode m : ExecutionMode.values())
2838 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2839 <        for (Integer v1 : new Integer[] { 1, null })
2840 <    {
2841 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2842 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2843 <        final Noop r = new Noop(m);
2836 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2837 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2838 >        checkIncomplete(h0);
2839 >        checkIncomplete(h1);
2840 >        rs[0].assertNotInvoked();
2841 >        rs[1].assertNotInvoked();
2842 >        f.cancel(mayInterruptIfRunning);
2843 >        checkCompletedWithWrappedCancellationException(h0);
2844 >        checkCompletedWithWrappedCancellationException(h1);
2845 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2846 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2847 >        checkCompletedWithWrappedCancellationException(h2);
2848 >        checkCompletedWithWrappedCancellationException(h3);
2849  
2850 <        assertTrue(g.cancel(mayInterruptIfRunning));
2347 <        f.complete(v1);
2348 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2850 >        assertTrue(g.complete(v1));
2851  
2852 <        // unspecified behavior
2853 <        Integer v;
2852 >        // unspecified behavior - both source completions available
2853 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2854 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2855          try {
2856 <            assertNull(h.join());
2857 <            r.assertInvoked();
2856 >            assertNull(h4.join());
2857 >            rs[4].assertInvoked();
2858          } catch (CompletionException ok) {
2859 <            checkCompletedWithWrappedCancellationException(h);
2860 <            r.assertNotInvoked();
2859 >            checkCompletedWithWrappedCancellationException(h4);
2860 >            rs[4].assertNotInvoked();
2861 >        }
2862 >        try {
2863 >            assertNull(h5.join());
2864 >            rs[5].assertInvoked();
2865 >        } catch (CompletionException ok) {
2866 >            checkCompletedWithWrappedCancellationException(h5);
2867 >            rs[5].assertNotInvoked();
2868          }
2869  
2870 <        checkCancelled(g);
2871 <        checkCompletedNormally(f, v1);
2870 >        checkCancelled(f);
2871 >        checkCompletedNormally(g, v1);
2872 >        checkCompletedWithWrappedCancellationException(h0);
2873 >        checkCompletedWithWrappedCancellationException(h1);
2874 >        checkCompletedWithWrappedCancellationException(h2);
2875 >        checkCompletedWithWrappedCancellationException(h3);
2876 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2877      }}
2878  
2879 <    public void testRunAfterEither_sourceCancelled4() {
2879 >    /**
2880 >     * runAfterEither result completes exceptionally if action does
2881 >     */
2882 >    public void testRunAfterEither_actionFailed() {
2883          for (ExecutionMode m : ExecutionMode.values())
2366        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2884          for (Integer v1 : new Integer[] { 1, null })
2885 +        for (Integer v2 : new Integer[] { 2, null })
2886      {
2887          final CompletableFuture<Integer> f = new CompletableFuture<>();
2888          final CompletableFuture<Integer> g = new CompletableFuture<>();
2889 <        final Noop r = new Noop(m);
2889 >        final FailingRunnable[] rs = new FailingRunnable[6];
2890 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2891  
2892 <        assertTrue(f.cancel(mayInterruptIfRunning));
2893 <        g.complete(v1);
2894 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2895 <
2896 <        // unspecified behavior
2897 <        Integer v;
2898 <        try {
2899 <            assertNull(h.join());
2900 <            r.assertInvoked();
2901 <        } catch (CompletionException ok) {
2902 <            checkCompletedWithWrappedCancellationException(h);
2903 <            r.assertNotInvoked();
2904 <        }
2892 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2893 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2894 >        assertTrue(f.complete(v1));
2895 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2896 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2897 >        checkCompletedWithWrappedCFException(h0);
2898 >        checkCompletedWithWrappedCFException(h1);
2899 >        checkCompletedWithWrappedCFException(h2);
2900 >        checkCompletedWithWrappedCFException(h3);
2901 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2902 >        assertTrue(g.complete(v2));
2903 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2904 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2905 >        checkCompletedWithWrappedCFException(h4);
2906 >        checkCompletedWithWrappedCFException(h5);
2907  
2908 <        checkCancelled(f);
2909 <        checkCompletedNormally(g, v1);
2908 >        checkCompletedNormally(f, v1);
2909 >        checkCompletedNormally(g, v2);
2910 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2911      }}
2912  
2913      /**
# Line 2398 | Line 2920 | public class CompletableFutureTest exten
2920      {
2921          final CompletableFuture<Integer> f = new CompletableFuture<>();
2922          final CompletableFutureInc r = new CompletableFutureInc(m);
2923 <        if (!createIncomplete) f.complete(v1);
2923 >        if (!createIncomplete) assertTrue(f.complete(v1));
2924          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2925 <        if (createIncomplete) f.complete(v1);
2925 >        if (createIncomplete) assertTrue(f.complete(v1));
2926  
2927          checkCompletedNormally(g, inc(v1));
2928          checkCompletedNormally(f, v1);
2929 <        r.assertInvoked();
2929 >        r.assertValue(v1);
2930      }}
2931  
2932      /**
# Line 2422 | Line 2944 | public class CompletableFutureTest exten
2944          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2945          if (createIncomplete) f.completeExceptionally(ex);
2946  
2947 <        checkCompletedWithWrappedCFException(g, ex);
2948 <        checkCompletedWithWrappedCFException(f, ex);
2947 >        checkCompletedWithWrappedException(g, ex);
2948 >        checkCompletedExceptionally(f, ex);
2949          r.assertNotInvoked();
2950      }}
2951  
# Line 2438 | Line 2960 | public class CompletableFutureTest exten
2960          final CompletableFuture<Integer> f = new CompletableFuture<>();
2961          final FailingCompletableFutureFunction r
2962              = new FailingCompletableFutureFunction(m);
2963 <        if (!createIncomplete) f.complete(v1);
2963 >        if (!createIncomplete) assertTrue(f.complete(v1));
2964          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2965 <        if (createIncomplete) f.complete(v1);
2965 >        if (createIncomplete) assertTrue(f.complete(v1));
2966  
2967          checkCompletedWithWrappedCFException(g);
2968          checkCompletedNormally(f, v1);
# Line 2483 | Line 3005 | public class CompletableFutureTest exten
3005       * when all components complete normally
3006       */
3007      public void testAllOf_normal() throws Exception {
3008 <        for (int k = 1; k < 20; ++k) {
3008 >        for (int k = 1; k < 10; k++) {
3009              CompletableFuture<Integer>[] fs
3010                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3011 <            for (int i = 0; i < k; ++i)
3011 >            for (int i = 0; i < k; i++)
3012                  fs[i] = new CompletableFuture<>();
3013              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3014 <            for (int i = 0; i < k; ++i) {
3014 >            for (int i = 0; i < k; i++) {
3015                  checkIncomplete(f);
3016                  checkIncomplete(CompletableFuture.allOf(fs));
3017                  fs[i].complete(one);
# Line 2500 | Line 3022 | public class CompletableFutureTest exten
3022      }
3023  
3024      public void testAllOf_backwards() throws Exception {
3025 <        for (int k = 1; k < 20; ++k) {
3025 >        for (int k = 1; k < 10; k++) {
3026              CompletableFuture<Integer>[] fs
3027                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3028 <            for (int i = 0; i < k; ++i)
3028 >            for (int i = 0; i < k; i++)
3029                  fs[i] = new CompletableFuture<>();
3030              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3031              for (int i = k - 1; i >= 0; i--) {
# Line 2516 | Line 3038 | public class CompletableFutureTest exten
3038          }
3039      }
3040  
3041 +    public void testAllOf_exceptional() throws Exception {
3042 +        for (int k = 1; k < 10; k++) {
3043 +            CompletableFuture<Integer>[] fs
3044 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3045 +            CFException ex = new CFException();
3046 +            for (int i = 0; i < k; i++)
3047 +                fs[i] = new CompletableFuture<>();
3048 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3049 +            for (int i = 0; i < k; i++) {
3050 +                checkIncomplete(f);
3051 +                checkIncomplete(CompletableFuture.allOf(fs));
3052 +                if (i != k/2) {
3053 +                    fs[i].complete(i);
3054 +                    checkCompletedNormally(fs[i], i);
3055 +                } else {
3056 +                    fs[i].completeExceptionally(ex);
3057 +                    checkCompletedExceptionally(fs[i], ex);
3058 +                }
3059 +            }
3060 +            checkCompletedWithWrappedException(f, ex);
3061 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3062 +        }
3063 +    }
3064 +
3065      /**
3066       * anyOf(no component futures) returns an incomplete future
3067       */
3068      public void testAnyOf_empty() throws Exception {
3069 +        for (Integer v1 : new Integer[] { 1, null })
3070 +    {
3071          CompletableFuture<Object> f = CompletableFuture.anyOf();
3072          checkIncomplete(f);
3073 <    }
3073 >
3074 >        f.complete(v1);
3075 >        checkCompletedNormally(f, v1);
3076 >    }}
3077  
3078      /**
3079       * anyOf returns a future completed normally with a value when
3080       * a component future does
3081       */
3082      public void testAnyOf_normal() throws Exception {
3083 <        for (int k = 0; k < 10; ++k) {
3083 >        for (int k = 0; k < 10; k++) {
3084              CompletableFuture[] fs = new CompletableFuture[k];
3085 <            for (int i = 0; i < k; ++i)
3085 >            for (int i = 0; i < k; i++)
3086                  fs[i] = new CompletableFuture<>();
3087              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3088              checkIncomplete(f);
3089 <            for (int i = 0; i < k; ++i) {
3090 <                fs[i].complete(one);
3091 <                checkCompletedNormally(f, one);
3092 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3089 >            for (int i = 0; i < k; i++) {
3090 >                fs[i].complete(i);
3091 >                checkCompletedNormally(f, 0);
3092 >                int x = (int) CompletableFuture.anyOf(fs).join();
3093 >                assertTrue(0 <= x && x <= i);
3094 >            }
3095 >        }
3096 >    }
3097 >    public void testAnyOf_normal_backwards() throws Exception {
3098 >        for (int k = 0; k < 10; k++) {
3099 >            CompletableFuture[] fs = new CompletableFuture[k];
3100 >            for (int i = 0; i < k; i++)
3101 >                fs[i] = new CompletableFuture<>();
3102 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3103 >            checkIncomplete(f);
3104 >            for (int i = k - 1; i >= 0; i--) {
3105 >                fs[i].complete(i);
3106 >                checkCompletedNormally(f, k - 1);
3107 >                int x = (int) CompletableFuture.anyOf(fs).join();
3108 >                assertTrue(i <= x && x <= k - 1);
3109              }
3110          }
3111      }
# Line 2547 | Line 3114 | public class CompletableFutureTest exten
3114       * anyOf result completes exceptionally when any component does.
3115       */
3116      public void testAnyOf_exceptional() throws Exception {
3117 <        for (int k = 0; k < 10; ++k) {
3117 >        for (int k = 0; k < 10; k++) {
3118              CompletableFuture[] fs = new CompletableFuture[k];
3119 <            for (int i = 0; i < k; ++i)
3119 >            CFException[] exs = new CFException[k];
3120 >            for (int i = 0; i < k; i++) {
3121                  fs[i] = new CompletableFuture<>();
3122 +                exs[i] = new CFException();
3123 +            }
3124              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3125              checkIncomplete(f);
3126 <            for (int i = 0; i < k; ++i) {
3127 <                fs[i].completeExceptionally(new CFException());
3128 <                checkCompletedWithWrappedCFException(f);
3126 >            for (int i = 0; i < k; i++) {
3127 >                fs[i].completeExceptionally(exs[i]);
3128 >                checkCompletedWithWrappedException(f, exs[0]);
3129 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3130 >            }
3131 >        }
3132 >    }
3133 >
3134 >    public void testAnyOf_exceptional_backwards() throws Exception {
3135 >        for (int k = 0; k < 10; k++) {
3136 >            CompletableFuture[] fs = new CompletableFuture[k];
3137 >            CFException[] exs = new CFException[k];
3138 >            for (int i = 0; i < k; i++) {
3139 >                fs[i] = new CompletableFuture<>();
3140 >                exs[i] = new CFException();
3141 >            }
3142 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3143 >            checkIncomplete(f);
3144 >            for (int i = k - 1; i >= 0; i--) {
3145 >                fs[i].completeExceptionally(exs[i]);
3146 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3147                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3148              }
3149          }
# Line 2574 | Line 3162 | public class CompletableFutureTest exten
3162          Runnable[] throwingActions = {
3163              () -> CompletableFuture.supplyAsync(null),
3164              () -> CompletableFuture.supplyAsync(null, exec),
3165 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3165 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3166  
3167              () -> CompletableFuture.runAsync(null),
3168              () -> CompletableFuture.runAsync(null, exec),
# Line 2679 | Line 3267 | public class CompletableFutureTest exten
3267          assertSame(f, f.toCompletableFuture());
3268      }
3269  
3270 <    /**
2683 <     * whenComplete action executes on normal completion, propagating
2684 <     * source result.
2685 <     */
2686 <    public void testWhenComplete_normalCompletion1() {
2687 <        for (ExecutionMode m : ExecutionMode.values())
2688 <        for (boolean createIncomplete : new boolean[] { true, false })
2689 <        for (Integer v1 : new Integer[] { 1, null })
2690 <    {
2691 <        final AtomicInteger a = new AtomicInteger(0);
2692 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2693 <        if (!createIncomplete) f.complete(v1);
2694 <        final CompletableFuture<Integer> g = m.whenComplete
2695 <            (f,
2696 <             (Integer x, Throwable t) -> {
2697 <                threadAssertSame(x, v1);
2698 <                threadAssertNull(t);
2699 <                a.getAndIncrement();
2700 <            });
2701 <        if (createIncomplete) f.complete(v1);
3270 >    //--- tests of implementation details; not part of official tck ---
3271  
3272 <        checkCompletedNormally(g, v1);
3273 <        checkCompletedNormally(f, v1);
3274 <        assertEquals(1, a.get());
3275 <    }}
3272 >    Object resultOf(CompletableFuture<?> f) {
3273 >        try {
3274 >            java.lang.reflect.Field resultField
3275 >                = CompletableFuture.class.getDeclaredField("result");
3276 >            resultField.setAccessible(true);
3277 >            return resultField.get(f);
3278 >        } catch (Throwable t) { throw new AssertionError(t); }
3279 >    }
3280  
3281 <    /**
3282 <     * whenComplete action executes on exceptional completion, propagating
2710 <     * source result.
2711 <     */
2712 <    public void testWhenComplete_exceptionalCompletion() {
3281 >    public void testExceptionPropagationReusesResultObject() {
3282 >        if (!testImplementationDetails) return;
3283          for (ExecutionMode m : ExecutionMode.values())
2714        for (boolean createIncomplete : new boolean[] { true, false })
2715        for (Integer v1 : new Integer[] { 1, null })
3284      {
2717        final AtomicInteger a = new AtomicInteger(0);
3285          final CFException ex = new CFException();
3286 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3287 <        if (!createIncomplete) f.completeExceptionally(ex);
2721 <        final CompletableFuture<Integer> g = m.whenComplete
2722 <            (f,
2723 <             (Integer x, Throwable t) -> {
2724 <                threadAssertNull(x);
2725 <                threadAssertSame(t, ex);
2726 <                a.getAndIncrement();
2727 <            });
2728 <        if (createIncomplete) f.completeExceptionally(ex);
2729 <        checkCompletedWithWrappedCFException(f, ex);
2730 <        checkCompletedWithWrappedCFException(g, ex);
2731 <        assertEquals(1, a.get());
2732 <    }}
3286 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3287 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3288  
3289 <    /**
3290 <     * whenComplete action executes on cancelled source, propagating
2736 <     * CancellationException.
2737 <     */
2738 <    public void testWhenComplete_sourceCancelled() {
2739 <        for (ExecutionMode m : ExecutionMode.values())
2740 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2741 <        for (boolean createIncomplete : new boolean[] { true, false })
2742 <    {
2743 <        final AtomicInteger a = new AtomicInteger(0);
2744 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2745 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2746 <        final CompletableFuture<Integer> g = m.whenComplete
2747 <            (f,
2748 <             (Integer x, Throwable t) -> {
2749 <                threadAssertNull(x);
2750 <                threadAssertTrue(t instanceof CancellationException);
2751 <                a.getAndIncrement();
2752 <            });
2753 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3289 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3290 >            = new ArrayList<>();
3291  
3292 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
3293 <        checkCompletedWithWrappedCancellationException(g);
3294 <        checkCancelled(f);
2758 <        assertEquals(1, a.get());
2759 <    }}
3292 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3293 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3294 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3295  
3296 <    /**
3297 <     * If a whenComplete action throws an exception when triggered by
3298 <     * a normal completion, it completes exceptionally
2764 <     */
2765 <    public void testWhenComplete_actionFailed() {
2766 <        for (boolean createIncomplete : new boolean[] { true, false })
2767 <        for (ExecutionMode m : ExecutionMode.values())
2768 <        for (Integer v1 : new Integer[] { 1, null })
2769 <    {
2770 <        final AtomicInteger a = new AtomicInteger(0);
2771 <        final CFException ex = new CFException();
2772 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2773 <        if (!createIncomplete) f.complete(v1);
2774 <        final CompletableFuture<Integer> g = m.whenComplete
2775 <            (f,
2776 <             (Integer x, Throwable t) -> {
2777 <                threadAssertSame(x, v1);
2778 <                threadAssertNull(t);
2779 <                a.getAndIncrement();
2780 <                throw ex;
2781 <            });
2782 <        if (createIncomplete) f.complete(v1);
2783 <        checkCompletedNormally(f, v1);
2784 <        checkCompletedWithWrappedCFException(g, ex);
2785 <        assertEquals(1, a.get());
2786 <    }}
3296 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3297 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3298 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3299  
3300 <    /**
3301 <     * If a whenComplete action throws an exception when triggered by
3302 <     * a source completion that also throws an exception, the source
2791 <     * exception takes precedence.
2792 <     */
2793 <    public void testWhenComplete_actionFailedSourceFailed() {
2794 <        for (boolean createIncomplete : new boolean[] { true, false })
2795 <        for (ExecutionMode m : ExecutionMode.values())
2796 <        for (Integer v1 : new Integer[] { 1, null })
2797 <    {
2798 <        final AtomicInteger a = new AtomicInteger(0);
2799 <        final CFException ex1 = new CFException();
2800 <        final CFException ex2 = new CFException();
2801 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3300 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3301 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3302 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3303  
3304 <        if (!createIncomplete) f.completeExceptionally(ex1);
2804 <        final CompletableFuture<Integer> g = m.whenComplete
2805 <            (f,
2806 <             (Integer x, Throwable t) -> {
2807 <                threadAssertSame(t, ex1);
2808 <                threadAssertNull(x);
2809 <                a.getAndIncrement();
2810 <                throw ex2;
2811 <            });
2812 <        if (createIncomplete) f.completeExceptionally(ex1);
3304 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3305  
3306 <        checkCompletedWithWrappedCFException(f, ex1);
3307 <        checkCompletedWithWrappedCFException(g, ex1);
3308 <        assertEquals(1, a.get());
3306 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3307 >
3308 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3309 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3310 >
3311 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3312 >                 fun : funs) {
3313 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3314 >            f.completeExceptionally(ex);
3315 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3316 >            checkCompletedWithWrappedException(src, ex);
3317 >            CompletableFuture<?> dep = fun.apply(src);
3318 >            checkCompletedWithWrappedException(dep, ex);
3319 >            assertSame(resultOf(src), resultOf(dep));
3320 >        }
3321 >
3322 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3323 >                 fun : funs) {
3324 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3325 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3326 >            CompletableFuture<?> dep = fun.apply(src);
3327 >            f.completeExceptionally(ex);
3328 >            checkCompletedWithWrappedException(src, ex);
3329 >            checkCompletedWithWrappedException(dep, ex);
3330 >            assertSame(resultOf(src), resultOf(dep));
3331 >        }
3332 >
3333 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3334 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3335 >                 fun : funs) {
3336 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3337 >            f.cancel(mayInterruptIfRunning);
3338 >            checkCancelled(f);
3339 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3340 >            checkCompletedWithWrappedCancellationException(src);
3341 >            CompletableFuture<?> dep = fun.apply(src);
3342 >            checkCompletedWithWrappedCancellationException(dep);
3343 >            assertSame(resultOf(src), resultOf(dep));
3344 >        }
3345 >
3346 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3347 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3348 >                 fun : funs) {
3349 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3350 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3351 >            CompletableFuture<?> dep = fun.apply(src);
3352 >            f.cancel(mayInterruptIfRunning);
3353 >            checkCancelled(f);
3354 >            checkCompletedWithWrappedCancellationException(src);
3355 >            checkCompletedWithWrappedCancellationException(dep);
3356 >            assertSame(resultOf(src), resultOf(dep));
3357 >        }
3358      }}
3359  
3360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines