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.72 by jsr166, Fri Jun 6 21:10:34 2014 UTC vs.
Revision 1.95 by jsr166, Wed Jun 25 15:32:10 2014 UTC

# Line 57 | Line 57 | public class CompletableFutureTest exten
57      }
58  
59      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
60 <        try {
61 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
60 >        checkTimedGet(f, value);
61 >
62          try {
63              assertEquals(value, f.join());
64          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 75 | public class CompletableFutureTest exten
75      }
76  
77      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
78 +        long startTime = System.nanoTime();
79 +        long timeoutMillis = LONG_DELAY_MS;
80          try {
81 <            f.get(LONG_DELAY_MS, MILLISECONDS);
81 >            f.get(timeoutMillis, MILLISECONDS);
82              shouldThrow();
83          } catch (ExecutionException success) {
84              assertTrue(success.getCause() instanceof CFException);
85          } catch (Throwable fail) { threadUnexpectedException(fail); }
86 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
87 +
88          try {
89              f.join();
90              shouldThrow();
# Line 107 | Line 110 | public class CompletableFutureTest exten
110  
111      <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
112                                                        Throwable ex) {
113 +        long startTime = System.nanoTime();
114 +        long timeoutMillis = LONG_DELAY_MS;
115          try {
116 <            f.get(LONG_DELAY_MS, MILLISECONDS);
116 >            f.get(timeoutMillis, MILLISECONDS);
117              shouldThrow();
118          } catch (ExecutionException success) {
119              assertSame(ex, success.getCause());
120          } catch (Throwable fail) { threadUnexpectedException(fail); }
121 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
122 +
123          try {
124              f.join();
125              shouldThrow();
# Line 131 | Line 138 | public class CompletableFutureTest exten
138          } catch (ExecutionException success) {
139              assertSame(ex, success.getCause());
140          } catch (Throwable fail) { threadUnexpectedException(fail); }
141 <                                                            
141 >
142          assertTrue(f.isDone());
143          assertFalse(f.isCancelled());
144          assertTrue(f.toString().contains("[Completed exceptionally]"));
# Line 158 | Line 165 | public class CompletableFutureTest exten
165      }
166  
167      void checkCancelled(CompletableFuture<?> f) {
168 +        long startTime = System.nanoTime();
169 +        long timeoutMillis = LONG_DELAY_MS;
170          try {
171 <            f.get(LONG_DELAY_MS, MILLISECONDS);
171 >            f.get(timeoutMillis, MILLISECONDS);
172              shouldThrow();
173          } catch (CancellationException success) {
174          } catch (Throwable fail) { threadUnexpectedException(fail); }
175 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
176 +
177          try {
178              f.join();
179              shouldThrow();
# Line 183 | Line 194 | public class CompletableFutureTest exten
194      }
195  
196      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
197 +        long startTime = System.nanoTime();
198 +        long timeoutMillis = LONG_DELAY_MS;
199          try {
200 <            f.get(LONG_DELAY_MS, MILLISECONDS);
200 >            f.get(timeoutMillis, MILLISECONDS);
201              shouldThrow();
202          } catch (ExecutionException success) {
203              assertTrue(success.getCause() instanceof CancellationException);
204          } catch (Throwable fail) { threadUnexpectedException(fail); }
205 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
206 +
207          try {
208              f.join();
209              shouldThrow();
# Line 227 | Line 242 | public class CompletableFutureTest exten
242       * isCancelled, join, get, and getNow
243       */
244      public void testComplete() {
245 +        for (Integer v1 : new Integer[] { 1, null })
246 +    {
247          CompletableFuture<Integer> f = new CompletableFuture<>();
248          checkIncomplete(f);
249 <        f.complete(one);
250 <        checkCompletedNormally(f, one);
251 <    }
249 >        assertTrue(f.complete(v1));
250 >        assertFalse(f.complete(v1));
251 >        checkCompletedNormally(f, v1);
252 >    }}
253  
254      /**
255       * completeExceptionally completes exceptionally, as indicated by
# Line 250 | Line 268 | public class CompletableFutureTest exten
268       * methods isDone, isCancelled, join, get, and getNow
269       */
270      public void testCancel() {
271 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
272 +    {
273          CompletableFuture<Integer> f = new CompletableFuture<>();
274          checkIncomplete(f);
275          assertTrue(f.cancel(true));
276 +        assertTrue(f.cancel(true));
277          checkCancelled(f);
278 <    }
278 >    }}
279  
280      /**
281       * obtrudeValue forces completion with given value
# Line 262 | Line 283 | public class CompletableFutureTest exten
283      public void testObtrudeValue() {
284          CompletableFuture<Integer> f = new CompletableFuture<>();
285          checkIncomplete(f);
286 <        f.complete(one);
286 >        assertTrue(f.complete(one));
287          checkCompletedNormally(f, one);
288          f.obtrudeValue(three);
289          checkCompletedNormally(f, three);
# Line 289 | Line 310 | public class CompletableFutureTest exten
310          CompletableFuture<Integer> f;
311  
312          f = new CompletableFuture<>();
313 <        f.complete(v1);
313 >        assertTrue(f.complete(v1));
314          for (int i = 0; i < 2; i++) {
315              f.obtrudeException(ex = new CFException());
316              checkCompletedExceptionally(f, ex);
# Line 309 | Line 330 | public class CompletableFutureTest exten
330          checkCompletedExceptionally(f, ex);
331          f.completeExceptionally(new CFException());
332          checkCompletedExceptionally(f, ex);
333 <        f.complete(v1);
333 >        assertFalse(f.complete(v1));
334          checkCompletedExceptionally(f, ex);
335      }}
336  
# Line 317 | Line 338 | public class CompletableFutureTest exten
338       * getNumberOfDependents returns number of dependent tasks
339       */
340      public void testGetNumberOfDependents() {
341 +        for (ExecutionMode m : ExecutionMode.values())
342 +        for (Integer v1 : new Integer[] { 1, null })
343 +    {
344          CompletableFuture<Integer> f = new CompletableFuture<>();
345          assertEquals(0, f.getNumberOfDependents());
346 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
346 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
347          assertEquals(1, f.getNumberOfDependents());
348          assertEquals(0, g.getNumberOfDependents());
349 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
349 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
350          assertEquals(2, f.getNumberOfDependents());
351 <        f.complete(1);
351 >        assertEquals(0, h.getNumberOfDependents());
352 >        assertTrue(f.complete(v1));
353          checkCompletedNormally(g, null);
354 +        checkCompletedNormally(h, null);
355          assertEquals(0, f.getNumberOfDependents());
356          assertEquals(0, g.getNumberOfDependents());
357 <    }
357 >        assertEquals(0, h.getNumberOfDependents());
358 >    }}
359  
360      /**
361       * toString indicates current completion state
# Line 339 | Line 366 | public class CompletableFutureTest exten
366          f = new CompletableFuture<String>();
367          assertTrue(f.toString().contains("[Not completed]"));
368  
369 <        f.complete("foo");
369 >        assertTrue(f.complete("foo"));
370          assertTrue(f.toString().contains("[Completed normally]"));
371  
372          f = new CompletableFuture<String>();
373 <        f.completeExceptionally(new IndexOutOfBoundsException());
373 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
374          assertTrue(f.toString().contains("[Completed exceptionally]"));
375 +
376 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
377 +            f = new CompletableFuture<String>();
378 +            assertTrue(f.cancel(mayInterruptIfRunning));
379 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
380 +        }
381      }
382  
383      /**
# Line 521 | Line 554 | public class CompletableFutureTest exten
554              invoked();
555              value = x;
556              CompletableFuture<Integer> f = new CompletableFuture<>();
557 <            f.complete(inc(x));
557 >            assertTrue(f.complete(inc(x)));
558              return f;
559          }
560      }
# Line 556 | Line 589 | public class CompletableFutureTest exten
589       * execution modes without copy/pasting all the test methods.
590       */
591      enum ExecutionMode {
592 <        DEFAULT {
592 >        SYNC {
593              public void checkExecutionMode() {
594                  assertFalse(ThreadExecutor.startedCurrentThread());
595                  assertNull(ForkJoinTask.getPool());
# Line 829 | Line 862 | public class CompletableFutureTest exten
862      {
863          final AtomicInteger a = new AtomicInteger(0);
864          final CompletableFuture<Integer> f = new CompletableFuture<>();
865 <        if (!createIncomplete) f.complete(v1);
865 >        if (!createIncomplete) assertTrue(f.complete(v1));
866          final CompletableFuture<Integer> g = f.exceptionally
867              ((Throwable t) -> {
868                  // Should not be called
869                  a.getAndIncrement();
870                  throw new AssertionError();
871              });
872 <        if (createIncomplete) f.complete(v1);
872 >        if (createIncomplete) assertTrue(f.complete(v1));
873  
874          checkCompletedNormally(g, v1);
875          checkCompletedNormally(f, v1);
876          assertEquals(0, a.get());
877      }}
878  
846
879      /**
880       * exceptionally action completes with function value on source
881       * exception
# Line 858 | Line 890 | public class CompletableFutureTest exten
890          if (!createIncomplete) f.completeExceptionally(ex);
891          final CompletableFuture<Integer> g = f.exceptionally
892              ((Throwable t) -> {
893 <                ExecutionMode.DEFAULT.checkExecutionMode();
893 >                ExecutionMode.SYNC.checkExecutionMode();
894                  threadAssertSame(t, ex);
895                  a.getAndIncrement();
896                  return v1;
# Line 880 | Line 912 | public class CompletableFutureTest exten
912          if (!createIncomplete) f.completeExceptionally(ex1);
913          final CompletableFuture<Integer> g = f.exceptionally
914              ((Throwable t) -> {
915 <                ExecutionMode.DEFAULT.checkExecutionMode();
915 >                ExecutionMode.SYNC.checkExecutionMode();
916                  threadAssertSame(t, ex1);
917                  a.getAndIncrement();
918                  throw ex2;
# Line 892 | Line 924 | public class CompletableFutureTest exten
924      }}
925  
926      /**
927 +     * whenComplete action executes on normal completion, propagating
928 +     * source result.
929 +     */
930 +    public void testWhenComplete_normalCompletion1() {
931 +        for (ExecutionMode m : ExecutionMode.values())
932 +        for (boolean createIncomplete : new boolean[] { true, false })
933 +        for (Integer v1 : new Integer[] { 1, null })
934 +    {
935 +        final AtomicInteger a = new AtomicInteger(0);
936 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
937 +        if (!createIncomplete) assertTrue(f.complete(v1));
938 +        final CompletableFuture<Integer> g = m.whenComplete
939 +            (f,
940 +             (Integer x, Throwable t) -> {
941 +                m.checkExecutionMode();
942 +                threadAssertSame(x, v1);
943 +                threadAssertNull(t);
944 +                a.getAndIncrement();
945 +            });
946 +        if (createIncomplete) assertTrue(f.complete(v1));
947 +
948 +        checkCompletedNormally(g, v1);
949 +        checkCompletedNormally(f, v1);
950 +        assertEquals(1, a.get());
951 +    }}
952 +
953 +    /**
954 +     * whenComplete action executes on exceptional completion, propagating
955 +     * source result.
956 +     */
957 +    public void testWhenComplete_exceptionalCompletion() {
958 +        for (ExecutionMode m : ExecutionMode.values())
959 +        for (boolean createIncomplete : new boolean[] { true, false })
960 +        for (Integer v1 : new Integer[] { 1, null })
961 +    {
962 +        final AtomicInteger a = new AtomicInteger(0);
963 +        final CFException ex = new CFException();
964 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
965 +        if (!createIncomplete) f.completeExceptionally(ex);
966 +        final CompletableFuture<Integer> g = m.whenComplete
967 +            (f,
968 +             (Integer x, Throwable t) -> {
969 +                m.checkExecutionMode();
970 +                threadAssertNull(x);
971 +                threadAssertSame(t, ex);
972 +                a.getAndIncrement();
973 +            });
974 +        if (createIncomplete) f.completeExceptionally(ex);
975 +
976 +        checkCompletedWithWrappedException(g, ex);
977 +        checkCompletedExceptionally(f, ex);
978 +        assertEquals(1, a.get());
979 +    }}
980 +
981 +    /**
982 +     * whenComplete action executes on cancelled source, propagating
983 +     * CancellationException.
984 +     */
985 +    public void testWhenComplete_sourceCancelled() {
986 +        for (ExecutionMode m : ExecutionMode.values())
987 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
988 +        for (boolean createIncomplete : new boolean[] { true, false })
989 +    {
990 +        final AtomicInteger a = new AtomicInteger(0);
991 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
992 +        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
993 +        final CompletableFuture<Integer> g = m.whenComplete
994 +            (f,
995 +             (Integer x, Throwable t) -> {
996 +                m.checkExecutionMode();
997 +                threadAssertNull(x);
998 +                threadAssertTrue(t instanceof CancellationException);
999 +                a.getAndIncrement();
1000 +            });
1001 +        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1002 +
1003 +        checkCompletedWithWrappedCancellationException(g);
1004 +        checkCancelled(f);
1005 +        assertEquals(1, a.get());
1006 +    }}
1007 +
1008 +    /**
1009 +     * If a whenComplete action throws an exception when triggered by
1010 +     * a normal completion, it completes exceptionally
1011 +     */
1012 +    public void testWhenComplete_actionFailed() {
1013 +        for (boolean createIncomplete : new boolean[] { true, false })
1014 +        for (ExecutionMode m : ExecutionMode.values())
1015 +        for (Integer v1 : new Integer[] { 1, null })
1016 +    {
1017 +        final AtomicInteger a = new AtomicInteger(0);
1018 +        final CFException ex = new CFException();
1019 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1020 +        if (!createIncomplete) assertTrue(f.complete(v1));
1021 +        final CompletableFuture<Integer> g = m.whenComplete
1022 +            (f,
1023 +             (Integer x, Throwable t) -> {
1024 +                m.checkExecutionMode();
1025 +                threadAssertSame(x, v1);
1026 +                threadAssertNull(t);
1027 +                a.getAndIncrement();
1028 +                throw ex;
1029 +            });
1030 +        if (createIncomplete) assertTrue(f.complete(v1));
1031 +
1032 +        checkCompletedWithWrappedException(g, ex);
1033 +        checkCompletedNormally(f, v1);
1034 +        assertEquals(1, a.get());
1035 +    }}
1036 +
1037 +    /**
1038 +     * If a whenComplete action throws an exception when triggered by
1039 +     * a source completion that also throws an exception, the source
1040 +     * exception takes precedence.
1041 +     */
1042 +    public void testWhenComplete_actionFailedSourceFailed() {
1043 +        for (boolean createIncomplete : new boolean[] { true, false })
1044 +        for (ExecutionMode m : ExecutionMode.values())
1045 +        for (Integer v1 : new Integer[] { 1, null })
1046 +    {
1047 +        final AtomicInteger a = new AtomicInteger(0);
1048 +        final CFException ex1 = new CFException();
1049 +        final CFException ex2 = new CFException();
1050 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1051 +
1052 +        if (!createIncomplete) f.completeExceptionally(ex1);
1053 +        final CompletableFuture<Integer> g = m.whenComplete
1054 +            (f,
1055 +             (Integer x, Throwable t) -> {
1056 +                m.checkExecutionMode();
1057 +                threadAssertSame(t, ex1);
1058 +                threadAssertNull(x);
1059 +                a.getAndIncrement();
1060 +                throw ex2;
1061 +            });
1062 +        if (createIncomplete) f.completeExceptionally(ex1);
1063 +
1064 +        checkCompletedWithWrappedException(g, ex1);
1065 +        checkCompletedExceptionally(f, ex1);
1066 +        assertEquals(1, a.get());
1067 +    }}
1068 +
1069 +    /**
1070       * handle action completes normally with function value on normal
1071       * completion of source
1072       */
# Line 902 | Line 1077 | public class CompletableFutureTest exten
1077      {
1078          final CompletableFuture<Integer> f = new CompletableFuture<>();
1079          final AtomicInteger a = new AtomicInteger(0);
1080 <        if (!createIncomplete) f.complete(v1);
1080 >        if (!createIncomplete) assertTrue(f.complete(v1));
1081          final CompletableFuture<Integer> g = m.handle
1082              (f,
1083               (Integer x, Throwable t) -> {
# Line 912 | Line 1087 | public class CompletableFutureTest exten
1087                  a.getAndIncrement();
1088                  return inc(v1);
1089              });
1090 <        if (createIncomplete) f.complete(v1);
1090 >        if (createIncomplete) assertTrue(f.complete(v1));
1091  
1092          checkCompletedNormally(g, inc(v1));
1093          checkCompletedNormally(f, v1);
# Line 1013 | Line 1188 | public class CompletableFutureTest exten
1188          final CompletableFuture<Integer> f = new CompletableFuture<>();
1189          final AtomicInteger a = new AtomicInteger(0);
1190          final CFException ex = new CFException();
1191 <        if (!createIncomplete) f.complete(v1);
1191 >        if (!createIncomplete) assertTrue(f.complete(v1));
1192          final CompletableFuture<Integer> g = m.handle
1193              (f,
1194               (Integer x, Throwable t) -> {
# Line 1023 | Line 1198 | public class CompletableFutureTest exten
1198                  a.getAndIncrement();
1199                  throw ex;
1200              });
1201 <        if (createIncomplete) f.complete(v1);
1201 >        if (createIncomplete) assertTrue(f.complete(v1));
1202  
1203          checkCompletedWithWrappedException(g, ex);
1204          checkCompletedNormally(f, v1);
# Line 1104 | Line 1279 | public class CompletableFutureTest exten
1279       */
1280      public void testThenRun_normalCompletion() {
1281          for (ExecutionMode m : ExecutionMode.values())
1107        for (boolean createIncomplete : new boolean[] { true, false })
1282          for (Integer v1 : new Integer[] { 1, null })
1283      {
1284          final CompletableFuture<Integer> f = new CompletableFuture<>();
1285 <        final Noop r = new Noop(m);
1286 <        if (!createIncomplete) f.complete(v1);
1113 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1114 <        if (createIncomplete) {
1115 <            checkIncomplete(g);
1116 <            f.complete(v1);
1117 <        }
1285 >        final Noop[] rs = new Noop[6];
1286 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1287  
1288 <        checkCompletedNormally(g, null);
1288 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1289 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1290 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1291 >        checkIncomplete(h0);
1292 >        checkIncomplete(h1);
1293 >        checkIncomplete(h2);
1294 >        assertTrue(f.complete(v1));
1295 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1296 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1297 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1298 >
1299 >        checkCompletedNormally(h0, null);
1300 >        checkCompletedNormally(h1, null);
1301 >        checkCompletedNormally(h2, null);
1302 >        checkCompletedNormally(h3, null);
1303 >        checkCompletedNormally(h4, null);
1304 >        checkCompletedNormally(h5, null);
1305          checkCompletedNormally(f, v1);
1306 <        r.assertInvoked();
1306 >        for (Noop r : rs) r.assertInvoked();
1307      }}
1308  
1309      /**
# Line 1127 | Line 1312 | public class CompletableFutureTest exten
1312       */
1313      public void testThenRun_exceptionalCompletion() {
1314          for (ExecutionMode m : ExecutionMode.values())
1130        for (boolean createIncomplete : new boolean[] { true, false })
1315      {
1316          final CFException ex = new CFException();
1317          final CompletableFuture<Integer> f = new CompletableFuture<>();
1318 <        final Noop r = new Noop(m);
1319 <        if (!createIncomplete) f.completeExceptionally(ex);
1136 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1137 <        if (createIncomplete) {
1138 <            checkIncomplete(g);
1139 <            f.completeExceptionally(ex);
1140 <        }
1318 >        final Noop[] rs = new Noop[6];
1319 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1320  
1321 <        checkCompletedWithWrappedException(g, ex);
1321 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1322 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1323 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1324 >        checkIncomplete(h0);
1325 >        checkIncomplete(h1);
1326 >        checkIncomplete(h2);
1327 >        assertTrue(f.completeExceptionally(ex));
1328 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1329 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1330 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1331 >
1332 >        checkCompletedWithWrappedException(h0, ex);
1333 >        checkCompletedWithWrappedException(h1, ex);
1334 >        checkCompletedWithWrappedException(h2, ex);
1335 >        checkCompletedWithWrappedException(h3, ex);
1336 >        checkCompletedWithWrappedException(h4, ex);
1337 >        checkCompletedWithWrappedException(h5, ex);
1338          checkCompletedExceptionally(f, ex);
1339 <        r.assertNotInvoked();
1339 >        for (Noop r : rs) r.assertNotInvoked();
1340      }}
1341  
1342      /**
# Line 1149 | Line 1344 | public class CompletableFutureTest exten
1344       */
1345      public void testThenRun_sourceCancelled() {
1346          for (ExecutionMode m : ExecutionMode.values())
1152        for (boolean createIncomplete : new boolean[] { true, false })
1347          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1348      {
1349          final CompletableFuture<Integer> f = new CompletableFuture<>();
1350 <        final Noop r = new Noop(m);
1351 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1158 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1159 <        if (createIncomplete) {
1160 <            checkIncomplete(g);
1161 <            assertTrue(f.cancel(mayInterruptIfRunning));
1162 <        }
1350 >        final Noop[] rs = new Noop[6];
1351 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1352  
1353 <        checkCompletedWithWrappedCancellationException(g);
1353 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1354 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1355 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1356 >        checkIncomplete(h0);
1357 >        checkIncomplete(h1);
1358 >        checkIncomplete(h2);
1359 >        assertTrue(f.cancel(mayInterruptIfRunning));
1360 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1361 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1362 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1363 >
1364 >        checkCompletedWithWrappedCancellationException(h0);
1365 >        checkCompletedWithWrappedCancellationException(h1);
1366 >        checkCompletedWithWrappedCancellationException(h2);
1367 >        checkCompletedWithWrappedCancellationException(h3);
1368 >        checkCompletedWithWrappedCancellationException(h4);
1369 >        checkCompletedWithWrappedCancellationException(h5);
1370          checkCancelled(f);
1371 <        r.assertNotInvoked();
1371 >        for (Noop r : rs) r.assertNotInvoked();
1372      }}
1373  
1374      /**
# Line 1171 | Line 1376 | public class CompletableFutureTest exten
1376       */
1377      public void testThenRun_actionFailed() {
1378          for (ExecutionMode m : ExecutionMode.values())
1174        for (boolean createIncomplete : new boolean[] { true, false })
1379          for (Integer v1 : new Integer[] { 1, null })
1380      {
1381          final CompletableFuture<Integer> f = new CompletableFuture<>();
1382 <        final FailingRunnable r = new FailingRunnable(m);
1383 <        if (!createIncomplete) f.complete(v1);
1180 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1181 <        if (createIncomplete) {
1182 <            checkIncomplete(g);
1183 <            f.complete(v1);
1184 <        }
1382 >        final FailingRunnable[] rs = new FailingRunnable[6];
1383 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1384  
1385 <        checkCompletedWithWrappedCFException(g);
1385 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1386 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1387 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1388 >        assertTrue(f.complete(v1));
1389 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1390 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1391 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1392 >
1393 >        checkCompletedWithWrappedCFException(h0);
1394 >        checkCompletedWithWrappedCFException(h1);
1395 >        checkCompletedWithWrappedCFException(h2);
1396 >        checkCompletedWithWrappedCFException(h3);
1397 >        checkCompletedWithWrappedCFException(h4);
1398 >        checkCompletedWithWrappedCFException(h5);
1399          checkCompletedNormally(f, v1);
1400      }}
1401  
# Line 1192 | Line 1404 | public class CompletableFutureTest exten
1404       */
1405      public void testThenApply_normalCompletion() {
1406          for (ExecutionMode m : ExecutionMode.values())
1195        for (boolean createIncomplete : new boolean[] { true, false })
1407          for (Integer v1 : new Integer[] { 1, null })
1408      {
1409          final CompletableFuture<Integer> f = new CompletableFuture<>();
1410 <        final IncFunction r = new IncFunction(m);
1411 <        if (!createIncomplete) f.complete(v1);
1201 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1202 <        if (createIncomplete) {
1203 <            checkIncomplete(g);
1204 <            f.complete(v1);
1205 <        }
1410 >        final IncFunction[] rs = new IncFunction[4];
1411 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1412  
1413 <        checkCompletedNormally(g, inc(v1));
1413 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1414 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1415 >        checkIncomplete(h0);
1416 >        checkIncomplete(h1);
1417 >        assertTrue(f.complete(v1));
1418 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1419 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1420 >
1421 >        checkCompletedNormally(h0, inc(v1));
1422 >        checkCompletedNormally(h1, inc(v1));
1423 >        checkCompletedNormally(h2, inc(v1));
1424 >        checkCompletedNormally(h3, inc(v1));
1425          checkCompletedNormally(f, v1);
1426 <        r.assertValue(inc(v1));
1426 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1427      }}
1428  
1429      /**
# Line 1215 | Line 1432 | public class CompletableFutureTest exten
1432       */
1433      public void testThenApply_exceptionalCompletion() {
1434          for (ExecutionMode m : ExecutionMode.values())
1218        for (boolean createIncomplete : new boolean[] { true, false })
1435      {
1436          final CFException ex = new CFException();
1437          final CompletableFuture<Integer> f = new CompletableFuture<>();
1438 <        final IncFunction r = new IncFunction(m);
1439 <        if (!createIncomplete) f.completeExceptionally(ex);
1224 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1225 <        if (createIncomplete) {
1226 <            checkIncomplete(g);
1227 <            f.completeExceptionally(ex);
1228 <        }
1438 >        final IncFunction[] rs = new IncFunction[4];
1439 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1440  
1441 <        checkCompletedWithWrappedException(g, ex);
1441 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1442 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1443 >        assertTrue(f.completeExceptionally(ex));
1444 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1445 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1446 >
1447 >        checkCompletedWithWrappedException(h0, ex);
1448 >        checkCompletedWithWrappedException(h1, ex);
1449 >        checkCompletedWithWrappedException(h2, ex);
1450 >        checkCompletedWithWrappedException(h3, ex);
1451          checkCompletedExceptionally(f, ex);
1452 <        r.assertNotInvoked();
1452 >        for (IncFunction r : rs) r.assertNotInvoked();
1453      }}
1454  
1455      /**
# Line 1237 | Line 1457 | public class CompletableFutureTest exten
1457       */
1458      public void testThenApply_sourceCancelled() {
1459          for (ExecutionMode m : ExecutionMode.values())
1240        for (boolean createIncomplete : new boolean[] { true, false })
1460          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1461      {
1462          final CompletableFuture<Integer> f = new CompletableFuture<>();
1463 <        final IncFunction r = new IncFunction(m);
1464 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1246 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1247 <        if (createIncomplete) {
1248 <            checkIncomplete(g);
1249 <            assertTrue(f.cancel(mayInterruptIfRunning));
1250 <        }
1463 >        final IncFunction[] rs = new IncFunction[4];
1464 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1465  
1466 <        checkCompletedWithWrappedCancellationException(g);
1466 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1467 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1468 >        assertTrue(f.cancel(mayInterruptIfRunning));
1469 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1470 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1471 >
1472 >        checkCompletedWithWrappedCancellationException(h0);
1473 >        checkCompletedWithWrappedCancellationException(h1);
1474 >        checkCompletedWithWrappedCancellationException(h2);
1475 >        checkCompletedWithWrappedCancellationException(h3);
1476          checkCancelled(f);
1477 <        r.assertNotInvoked();
1477 >        for (IncFunction r : rs) r.assertNotInvoked();
1478      }}
1479  
1480      /**
# Line 1259 | Line 1482 | public class CompletableFutureTest exten
1482       */
1483      public void testThenApply_actionFailed() {
1484          for (ExecutionMode m : ExecutionMode.values())
1262        for (boolean createIncomplete : new boolean[] { true, false })
1485          for (Integer v1 : new Integer[] { 1, null })
1486      {
1487          final CompletableFuture<Integer> f = new CompletableFuture<>();
1488 <        final FailingFunction r = new FailingFunction(m);
1489 <        if (!createIncomplete) f.complete(v1);
1268 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1269 <        if (createIncomplete) {
1270 <            checkIncomplete(g);
1271 <            f.complete(v1);
1272 <        }
1488 >        final FailingFunction[] rs = new FailingFunction[4];
1489 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1490  
1491 <        checkCompletedWithWrappedCFException(g);
1491 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1492 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1493 >        assertTrue(f.complete(v1));
1494 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1495 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1496 >
1497 >        checkCompletedWithWrappedCFException(h0);
1498 >        checkCompletedWithWrappedCFException(h1);
1499 >        checkCompletedWithWrappedCFException(h2);
1500 >        checkCompletedWithWrappedCFException(h3);
1501          checkCompletedNormally(f, v1);
1502      }}
1503  
# Line 1280 | Line 1506 | public class CompletableFutureTest exten
1506       */
1507      public void testThenAccept_normalCompletion() {
1508          for (ExecutionMode m : ExecutionMode.values())
1283        for (boolean createIncomplete : new boolean[] { true, false })
1509          for (Integer v1 : new Integer[] { 1, null })
1510      {
1511          final CompletableFuture<Integer> f = new CompletableFuture<>();
1512 <        final NoopConsumer r = new NoopConsumer(m);
1513 <        if (!createIncomplete) f.complete(v1);
1289 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1290 <        if (createIncomplete) {
1291 <            checkIncomplete(g);
1292 <            f.complete(v1);
1293 <        }
1512 >        final NoopConsumer[] rs = new NoopConsumer[4];
1513 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1514  
1515 <        checkCompletedNormally(g, null);
1516 <        r.assertValue(v1);
1515 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1516 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1517 >        checkIncomplete(h0);
1518 >        checkIncomplete(h1);
1519 >        assertTrue(f.complete(v1));
1520 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1521 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1522 >
1523 >        checkCompletedNormally(h0, null);
1524 >        checkCompletedNormally(h1, null);
1525 >        checkCompletedNormally(h2, null);
1526 >        checkCompletedNormally(h3, null);
1527          checkCompletedNormally(f, v1);
1528 +        for (NoopConsumer r : rs) r.assertValue(v1);
1529      }}
1530  
1531      /**
# Line 1303 | Line 1534 | public class CompletableFutureTest exten
1534       */
1535      public void testThenAccept_exceptionalCompletion() {
1536          for (ExecutionMode m : ExecutionMode.values())
1306        for (boolean createIncomplete : new boolean[] { true, false })
1537      {
1538          final CFException ex = new CFException();
1539          final CompletableFuture<Integer> f = new CompletableFuture<>();
1540 <        final NoopConsumer r = new NoopConsumer(m);
1541 <        if (!createIncomplete) f.completeExceptionally(ex);
1312 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1313 <        if (createIncomplete) {
1314 <            checkIncomplete(g);
1315 <            f.completeExceptionally(ex);
1316 <        }
1540 >        final NoopConsumer[] rs = new NoopConsumer[4];
1541 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1542  
1543 <        checkCompletedWithWrappedException(g, ex);
1543 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1544 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1545 >        assertTrue(f.completeExceptionally(ex));
1546 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1547 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1548 >
1549 >        checkCompletedWithWrappedException(h0, ex);
1550 >        checkCompletedWithWrappedException(h1, ex);
1551 >        checkCompletedWithWrappedException(h2, ex);
1552 >        checkCompletedWithWrappedException(h3, ex);
1553          checkCompletedExceptionally(f, ex);
1554 <        r.assertNotInvoked();
1554 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1555      }}
1556  
1557      /**
# Line 1325 | Line 1559 | public class CompletableFutureTest exten
1559       */
1560      public void testThenAccept_sourceCancelled() {
1561          for (ExecutionMode m : ExecutionMode.values())
1328        for (boolean createIncomplete : new boolean[] { true, false })
1562          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1563      {
1564          final CompletableFuture<Integer> f = new CompletableFuture<>();
1565 <        final NoopConsumer r = new NoopConsumer(m);
1566 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1334 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1335 <        if (createIncomplete) {
1336 <            checkIncomplete(g);
1337 <            assertTrue(f.cancel(mayInterruptIfRunning));
1338 <        }
1565 >        final NoopConsumer[] rs = new NoopConsumer[4];
1566 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1567  
1568 <        checkCompletedWithWrappedCancellationException(g);
1568 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1569 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1570 >        assertTrue(f.cancel(mayInterruptIfRunning));
1571 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1572 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1573 >
1574 >        checkCompletedWithWrappedCancellationException(h0);
1575 >        checkCompletedWithWrappedCancellationException(h1);
1576 >        checkCompletedWithWrappedCancellationException(h2);
1577 >        checkCompletedWithWrappedCancellationException(h3);
1578          checkCancelled(f);
1579 <        r.assertNotInvoked();
1579 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1580      }}
1581  
1582      /**
# Line 1347 | Line 1584 | public class CompletableFutureTest exten
1584       */
1585      public void testThenAccept_actionFailed() {
1586          for (ExecutionMode m : ExecutionMode.values())
1350        for (boolean createIncomplete : new boolean[] { true, false })
1587          for (Integer v1 : new Integer[] { 1, null })
1588      {
1589          final CompletableFuture<Integer> f = new CompletableFuture<>();
1590 <        final FailingConsumer r = new FailingConsumer(m);
1591 <        if (!createIncomplete) f.complete(v1);
1356 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1357 <        if (createIncomplete) {
1358 <            checkIncomplete(g);
1359 <            f.complete(v1);
1360 <        }
1590 >        final FailingConsumer[] rs = new FailingConsumer[4];
1591 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1592  
1593 <        checkCompletedWithWrappedCFException(g);
1593 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1594 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1595 >        assertTrue(f.complete(v1));
1596 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1597 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1598 >
1599 >        checkCompletedWithWrappedCFException(h0);
1600 >        checkCompletedWithWrappedCFException(h1);
1601 >        checkCompletedWithWrappedCFException(h2);
1602 >        checkCompletedWithWrappedCFException(h3);
1603          checkCompletedNormally(f, v1);
1604      }}
1605  
# Line 1369 | Line 1609 | public class CompletableFutureTest exten
1609       */
1610      public void testThenCombine_normalCompletion() {
1611          for (ExecutionMode m : ExecutionMode.values())
1372        for (boolean createIncomplete : new boolean[] { true, false })
1612          for (boolean fFirst : new boolean[] { true, false })
1613          for (Integer v1 : new Integer[] { 1, null })
1614          for (Integer v2 : new Integer[] { 2, null })
1615      {
1616          final CompletableFuture<Integer> f = new CompletableFuture<>();
1617          final CompletableFuture<Integer> g = new CompletableFuture<>();
1618 <        final SubtractFunction r = new SubtractFunction(m);
1618 >        final SubtractFunction[] rs = new SubtractFunction[6];
1619 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1620  
1621 <        if (fFirst) f.complete(v1); else g.complete(v2);
1622 <        if (!createIncomplete)
1623 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1624 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1625 <        if (createIncomplete) {
1626 <            checkIncomplete(h);
1627 <            r.assertNotInvoked();
1628 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1629 <        }
1621 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1622 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1623 >        final Integer w1 =  fFirst ? v1 : v2;
1624 >        final Integer w2 = !fFirst ? v1 : v2;
1625 >
1626 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1627 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1628 >        assertTrue(fst.complete(w1));
1629 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1630 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1631 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1632 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1633 >        checkCompletedNormally(h1, subtract(w1, w1));
1634 >        checkCompletedNormally(h3, subtract(w1, w1));
1635 >        rs[1].assertValue(subtract(w1, w1));
1636 >        rs[3].assertValue(subtract(w1, w1));
1637 >        assertTrue(snd.complete(w2));
1638 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1639 >
1640 >        checkCompletedNormally(h0, subtract(v1, v2));
1641 >        checkCompletedNormally(h2, subtract(v1, v2));
1642 >        checkCompletedNormally(h4, subtract(v1, v2));
1643 >        rs[0].assertValue(subtract(v1, v2));
1644 >        rs[2].assertValue(subtract(v1, v2));
1645 >        rs[4].assertValue(subtract(v1, v2));
1646  
1391        checkCompletedNormally(h, subtract(v1, v2));
1647          checkCompletedNormally(f, v1);
1648          checkCompletedNormally(g, v2);
1394        r.assertValue(subtract(v1, v2));
1649      }}
1650  
1651      /**
1652       * thenCombine result completes exceptionally after exceptional
1653       * completion of either source
1654       */
1655 <    public void testThenCombine_exceptionalCompletion() {
1655 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1656          for (ExecutionMode m : ExecutionMode.values())
1403        for (boolean createIncomplete : new boolean[] { true, false })
1657          for (boolean fFirst : new boolean[] { true, false })
1658 +        for (boolean failFirst : new boolean[] { true, false })
1659          for (Integer v1 : new Integer[] { 1, null })
1660      {
1661          final CompletableFuture<Integer> f = new CompletableFuture<>();
1662          final CompletableFuture<Integer> g = new CompletableFuture<>();
1663          final CFException ex = new CFException();
1664 <        final SubtractFunction r = new SubtractFunction(m);
1665 <
1666 <        (fFirst ? f : g).complete(v1);
1667 <        if (!createIncomplete)
1668 <            (!fFirst ? f : g).completeExceptionally(ex);
1669 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1670 <        if (createIncomplete) {
1671 <            checkIncomplete(h);
1672 <            (!fFirst ? f : g).completeExceptionally(ex);
1673 <        }
1664 >        final SubtractFunction r1 = new SubtractFunction(m);
1665 >        final SubtractFunction r2 = new SubtractFunction(m);
1666 >        final SubtractFunction r3 = new SubtractFunction(m);
1667 >
1668 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1669 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1670 >        final Callable<Boolean> complete1 = failFirst ?
1671 >            () -> fst.completeExceptionally(ex) :
1672 >            () -> fst.complete(v1);
1673 >        final Callable<Boolean> complete2 = failFirst ?
1674 >            () -> snd.complete(v1) :
1675 >            () -> snd.completeExceptionally(ex);
1676 >
1677 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1678 >        assertTrue(complete1.call());
1679 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1680 >        checkIncomplete(h1);
1681 >        checkIncomplete(h2);
1682 >        assertTrue(complete2.call());
1683 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1684  
1685 <        checkCompletedWithWrappedException(h, ex);
1686 <        r.assertNotInvoked();
1687 <        checkCompletedNormally(fFirst ? f : g, v1);
1688 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1685 >        checkCompletedWithWrappedException(h1, ex);
1686 >        checkCompletedWithWrappedException(h2, ex);
1687 >        checkCompletedWithWrappedException(h3, ex);
1688 >        r1.assertNotInvoked();
1689 >        r2.assertNotInvoked();
1690 >        r3.assertNotInvoked();
1691 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1692 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1693      }}
1694  
1695      /**
1696       * thenCombine result completes exceptionally if either source cancelled
1697       */
1698 <    public void testThenCombine_sourceCancelled() {
1698 >    public void testThenCombine_sourceCancelled() throws Throwable {
1699          for (ExecutionMode m : ExecutionMode.values())
1700          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1433        for (boolean createIncomplete : new boolean[] { true, false })
1701          for (boolean fFirst : new boolean[] { true, false })
1702 +        for (boolean failFirst : new boolean[] { true, false })
1703          for (Integer v1 : new Integer[] { 1, null })
1704      {
1705          final CompletableFuture<Integer> f = new CompletableFuture<>();
1706          final CompletableFuture<Integer> g = new CompletableFuture<>();
1707 <        final SubtractFunction r = new SubtractFunction(m);
1707 >        final SubtractFunction r1 = new SubtractFunction(m);
1708 >        final SubtractFunction r2 = new SubtractFunction(m);
1709 >        final SubtractFunction r3 = new SubtractFunction(m);
1710  
1711 <        (fFirst ? f : g).complete(v1);
1712 <        if (!createIncomplete)
1713 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1714 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1715 <        if (createIncomplete) {
1716 <            checkIncomplete(h);
1717 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1718 <        }
1711 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1712 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1713 >        final Callable<Boolean> complete1 = failFirst ?
1714 >            () -> fst.cancel(mayInterruptIfRunning) :
1715 >            () -> fst.complete(v1);
1716 >        final Callable<Boolean> complete2 = failFirst ?
1717 >            () -> snd.complete(v1) :
1718 >            () -> snd.cancel(mayInterruptIfRunning);
1719  
1720 <        checkCompletedWithWrappedCancellationException(h);
1721 <        checkCancelled(!fFirst ? f : g);
1722 <        r.assertNotInvoked();
1723 <        checkCompletedNormally(fFirst ? f : g, v1);
1720 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1721 >        assertTrue(complete1.call());
1722 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1723 >        checkIncomplete(h1);
1724 >        checkIncomplete(h2);
1725 >        assertTrue(complete2.call());
1726 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1727 >
1728 >        checkCompletedWithWrappedCancellationException(h1);
1729 >        checkCompletedWithWrappedCancellationException(h2);
1730 >        checkCompletedWithWrappedCancellationException(h3);
1731 >        r1.assertNotInvoked();
1732 >        r2.assertNotInvoked();
1733 >        r3.assertNotInvoked();
1734 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1735 >        checkCancelled(failFirst ? fst : snd);
1736      }}
1737  
1738      /**
# Line 1464 | Line 1746 | public class CompletableFutureTest exten
1746      {
1747          final CompletableFuture<Integer> f = new CompletableFuture<>();
1748          final CompletableFuture<Integer> g = new CompletableFuture<>();
1749 <        final FailingBiFunction r = new FailingBiFunction(m);
1750 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1751 <
1752 <        if (fFirst) {
1753 <            f.complete(v1);
1754 <            g.complete(v2);
1755 <        } else {
1756 <            g.complete(v2);
1757 <            f.complete(v1);
1758 <        }
1749 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1750 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1751 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1752 >
1753 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1754 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1755 >        final Integer w1 =  fFirst ? v1 : v2;
1756 >        final Integer w2 = !fFirst ? v1 : v2;
1757 >
1758 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1759 >        assertTrue(fst.complete(w1));
1760 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1761 >        assertTrue(snd.complete(w2));
1762 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1763  
1764 <        checkCompletedWithWrappedCFException(h);
1764 >        checkCompletedWithWrappedCFException(h1);
1765 >        checkCompletedWithWrappedCFException(h2);
1766 >        checkCompletedWithWrappedCFException(h3);
1767 >        r1.assertInvoked();
1768 >        r2.assertInvoked();
1769 >        r3.assertInvoked();
1770          checkCompletedNormally(f, v1);
1771          checkCompletedNormally(g, v2);
1772      }}
# Line 1486 | Line 1777 | public class CompletableFutureTest exten
1777       */
1778      public void testThenAcceptBoth_normalCompletion() {
1779          for (ExecutionMode m : ExecutionMode.values())
1489        for (boolean createIncomplete : new boolean[] { true, false })
1780          for (boolean fFirst : new boolean[] { true, false })
1781          for (Integer v1 : new Integer[] { 1, null })
1782          for (Integer v2 : new Integer[] { 2, null })
1783      {
1784          final CompletableFuture<Integer> f = new CompletableFuture<>();
1785          final CompletableFuture<Integer> g = new CompletableFuture<>();
1786 <        final SubtractAction r = new SubtractAction(m);
1787 <
1788 <        if (fFirst) f.complete(v1); else g.complete(v2);
1789 <        if (!createIncomplete)
1790 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1791 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1792 <        if (createIncomplete) {
1793 <            checkIncomplete(h);
1794 <            r.assertNotInvoked();
1795 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1796 <        }
1786 >        final SubtractAction r1 = new SubtractAction(m);
1787 >        final SubtractAction r2 = new SubtractAction(m);
1788 >        final SubtractAction r3 = new SubtractAction(m);
1789 >
1790 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1791 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1792 >        final Integer w1 =  fFirst ? v1 : v2;
1793 >        final Integer w2 = !fFirst ? v1 : v2;
1794 >
1795 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1796 >        assertTrue(fst.complete(w1));
1797 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1798 >        checkIncomplete(h1);
1799 >        checkIncomplete(h2);
1800 >        r1.assertNotInvoked();
1801 >        r2.assertNotInvoked();
1802 >        assertTrue(snd.complete(w2));
1803 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1804  
1805 <        checkCompletedNormally(h, null);
1806 <        r.assertValue(subtract(v1, v2));
1805 >        checkCompletedNormally(h1, null);
1806 >        checkCompletedNormally(h2, null);
1807 >        checkCompletedNormally(h3, null);
1808 >        r1.assertValue(subtract(v1, v2));
1809 >        r2.assertValue(subtract(v1, v2));
1810 >        r3.assertValue(subtract(v1, v2));
1811          checkCompletedNormally(f, v1);
1812          checkCompletedNormally(g, v2);
1813      }}
# Line 1515 | Line 1816 | public class CompletableFutureTest exten
1816       * thenAcceptBoth result completes exceptionally after exceptional
1817       * completion of either source
1818       */
1819 <    public void testThenAcceptBoth_exceptionalCompletion() {
1819 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1820          for (ExecutionMode m : ExecutionMode.values())
1520        for (boolean createIncomplete : new boolean[] { true, false })
1821          for (boolean fFirst : new boolean[] { true, false })
1822 +        for (boolean failFirst : new boolean[] { true, false })
1823          for (Integer v1 : new Integer[] { 1, null })
1824      {
1825          final CompletableFuture<Integer> f = new CompletableFuture<>();
1826          final CompletableFuture<Integer> g = new CompletableFuture<>();
1827          final CFException ex = new CFException();
1828 <        final SubtractAction r = new SubtractAction(m);
1829 <
1830 <        (fFirst ? f : g).complete(v1);
1831 <        if (!createIncomplete)
1832 <            (!fFirst ? f : g).completeExceptionally(ex);
1833 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1834 <        if (createIncomplete) {
1835 <            checkIncomplete(h);
1836 <            (!fFirst ? f : g).completeExceptionally(ex);
1837 <        }
1828 >        final SubtractAction r1 = new SubtractAction(m);
1829 >        final SubtractAction r2 = new SubtractAction(m);
1830 >        final SubtractAction r3 = new SubtractAction(m);
1831 >
1832 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1833 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1834 >        final Callable<Boolean> complete1 = failFirst ?
1835 >            () -> fst.completeExceptionally(ex) :
1836 >            () -> fst.complete(v1);
1837 >        final Callable<Boolean> complete2 = failFirst ?
1838 >            () -> snd.complete(v1) :
1839 >            () -> snd.completeExceptionally(ex);
1840 >
1841 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1842 >        assertTrue(complete1.call());
1843 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1844 >        checkIncomplete(h1);
1845 >        checkIncomplete(h2);
1846 >        assertTrue(complete2.call());
1847 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1848  
1849 <        checkCompletedWithWrappedException(h, ex);
1850 <        r.assertNotInvoked();
1851 <        checkCompletedNormally(fFirst ? f : g, v1);
1852 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1849 >        checkCompletedWithWrappedException(h1, ex);
1850 >        checkCompletedWithWrappedException(h2, ex);
1851 >        checkCompletedWithWrappedException(h3, ex);
1852 >        r1.assertNotInvoked();
1853 >        r2.assertNotInvoked();
1854 >        r3.assertNotInvoked();
1855 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1856 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1857      }}
1858  
1859      /**
1860       * thenAcceptBoth result completes exceptionally if either source cancelled
1861       */
1862 <    public void testThenAcceptBoth_sourceCancelled() {
1862 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1863          for (ExecutionMode m : ExecutionMode.values())
1864          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1550        for (boolean createIncomplete : new boolean[] { true, false })
1865          for (boolean fFirst : new boolean[] { true, false })
1866 +        for (boolean failFirst : new boolean[] { true, false })
1867          for (Integer v1 : new Integer[] { 1, null })
1868      {
1869          final CompletableFuture<Integer> f = new CompletableFuture<>();
1870          final CompletableFuture<Integer> g = new CompletableFuture<>();
1871 <        final SubtractAction r = new SubtractAction(m);
1871 >        final SubtractAction r1 = new SubtractAction(m);
1872 >        final SubtractAction r2 = new SubtractAction(m);
1873 >        final SubtractAction r3 = new SubtractAction(m);
1874  
1875 <        (fFirst ? f : g).complete(v1);
1876 <        if (!createIncomplete)
1877 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1878 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1879 <        if (createIncomplete) {
1880 <            checkIncomplete(h);
1881 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1882 <        }
1875 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1876 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1877 >        final Callable<Boolean> complete1 = failFirst ?
1878 >            () -> fst.cancel(mayInterruptIfRunning) :
1879 >            () -> fst.complete(v1);
1880 >        final Callable<Boolean> complete2 = failFirst ?
1881 >            () -> snd.complete(v1) :
1882 >            () -> snd.cancel(mayInterruptIfRunning);
1883  
1884 <        checkCompletedWithWrappedCancellationException(h);
1885 <        checkCancelled(!fFirst ? f : g);
1886 <        r.assertNotInvoked();
1887 <        checkCompletedNormally(fFirst ? f : g, v1);
1884 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1885 >        assertTrue(complete1.call());
1886 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1887 >        checkIncomplete(h1);
1888 >        checkIncomplete(h2);
1889 >        assertTrue(complete2.call());
1890 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1891 >
1892 >        checkCompletedWithWrappedCancellationException(h1);
1893 >        checkCompletedWithWrappedCancellationException(h2);
1894 >        checkCompletedWithWrappedCancellationException(h3);
1895 >        r1.assertNotInvoked();
1896 >        r2.assertNotInvoked();
1897 >        r3.assertNotInvoked();
1898 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1899 >        checkCancelled(failFirst ? fst : snd);
1900      }}
1901  
1902      /**
# Line 1581 | Line 1910 | public class CompletableFutureTest exten
1910      {
1911          final CompletableFuture<Integer> f = new CompletableFuture<>();
1912          final CompletableFuture<Integer> g = new CompletableFuture<>();
1913 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1914 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1913 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1914 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1915 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1916 >
1917 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1918 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1919 >        final Integer w1 =  fFirst ? v1 : v2;
1920 >        final Integer w2 = !fFirst ? v1 : v2;
1921 >
1922 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1923 >        assertTrue(fst.complete(w1));
1924 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1925 >        assertTrue(snd.complete(w2));
1926 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1927  
1928 <        if (fFirst) {
1929 <            f.complete(v1);
1930 <            g.complete(v2);
1931 <        } else {
1932 <            g.complete(v2);
1933 <            f.complete(v1);
1593 <        }
1594 <
1595 <        checkCompletedWithWrappedCFException(h);
1928 >        checkCompletedWithWrappedCFException(h1);
1929 >        checkCompletedWithWrappedCFException(h2);
1930 >        checkCompletedWithWrappedCFException(h3);
1931 >        r1.assertInvoked();
1932 >        r2.assertInvoked();
1933 >        r3.assertInvoked();
1934          checkCompletedNormally(f, v1);
1935          checkCompletedNormally(g, v2);
1936      }}
# Line 1603 | Line 1941 | public class CompletableFutureTest exten
1941       */
1942      public void testRunAfterBoth_normalCompletion() {
1943          for (ExecutionMode m : ExecutionMode.values())
1606        for (boolean createIncomplete : new boolean[] { true, false })
1944          for (boolean fFirst : new boolean[] { true, false })
1945          for (Integer v1 : new Integer[] { 1, null })
1946          for (Integer v2 : new Integer[] { 2, null })
1947      {
1948          final CompletableFuture<Integer> f = new CompletableFuture<>();
1949          final CompletableFuture<Integer> g = new CompletableFuture<>();
1950 <        final Noop r = new Noop(m);
1951 <
1952 <        if (fFirst) f.complete(v1); else g.complete(v2);
1953 <        if (!createIncomplete)
1954 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1955 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1956 <        if (createIncomplete) {
1957 <            checkIncomplete(h);
1958 <            r.assertNotInvoked();
1959 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1960 <        }
1950 >        final Noop r1 = new Noop(m);
1951 >        final Noop r2 = new Noop(m);
1952 >        final Noop r3 = new Noop(m);
1953 >
1954 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1955 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1956 >        final Integer w1 =  fFirst ? v1 : v2;
1957 >        final Integer w2 = !fFirst ? v1 : v2;
1958 >
1959 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1960 >        assertTrue(fst.complete(w1));
1961 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1962 >        checkIncomplete(h1);
1963 >        checkIncomplete(h2);
1964 >        r1.assertNotInvoked();
1965 >        r2.assertNotInvoked();
1966 >        assertTrue(snd.complete(w2));
1967 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1968  
1969 <        checkCompletedNormally(h, null);
1970 <        r.assertInvoked();
1969 >        checkCompletedNormally(h1, null);
1970 >        checkCompletedNormally(h2, null);
1971 >        checkCompletedNormally(h3, null);
1972 >        r1.assertInvoked();
1973 >        r2.assertInvoked();
1974 >        r3.assertInvoked();
1975          checkCompletedNormally(f, v1);
1976          checkCompletedNormally(g, v2);
1977      }}
# Line 1632 | Line 1980 | public class CompletableFutureTest exten
1980       * runAfterBoth result completes exceptionally after exceptional
1981       * completion of either source
1982       */
1983 <    public void testRunAfterBoth_exceptionalCompletion() {
1983 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1984          for (ExecutionMode m : ExecutionMode.values())
1637        for (boolean createIncomplete : new boolean[] { true, false })
1985          for (boolean fFirst : new boolean[] { true, false })
1986 +        for (boolean failFirst : new boolean[] { true, false })
1987          for (Integer v1 : new Integer[] { 1, null })
1988      {
1989          final CompletableFuture<Integer> f = new CompletableFuture<>();
1990          final CompletableFuture<Integer> g = new CompletableFuture<>();
1991          final CFException ex = new CFException();
1992 <        final Noop r = new Noop(m);
1993 <
1994 <        (fFirst ? f : g).complete(v1);
1995 <        if (!createIncomplete)
1996 <            (!fFirst ? f : g).completeExceptionally(ex);
1997 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1998 <        if (createIncomplete) {
1999 <            checkIncomplete(h);
2000 <            (!fFirst ? f : g).completeExceptionally(ex);
2001 <        }
1992 >        final Noop r1 = new Noop(m);
1993 >        final Noop r2 = new Noop(m);
1994 >        final Noop r3 = new Noop(m);
1995 >
1996 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1997 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1998 >        final Callable<Boolean> complete1 = failFirst ?
1999 >            () -> fst.completeExceptionally(ex) :
2000 >            () -> fst.complete(v1);
2001 >        final Callable<Boolean> complete2 = failFirst ?
2002 >            () -> snd.complete(v1) :
2003 >            () -> snd.completeExceptionally(ex);
2004 >
2005 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2006 >        assertTrue(complete1.call());
2007 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2008 >        checkIncomplete(h1);
2009 >        checkIncomplete(h2);
2010 >        assertTrue(complete2.call());
2011 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2012  
2013 <        checkCompletedWithWrappedException(h, ex);
2014 <        r.assertNotInvoked();
2015 <        checkCompletedNormally(fFirst ? f : g, v1);
2016 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
2013 >        checkCompletedWithWrappedException(h1, ex);
2014 >        checkCompletedWithWrappedException(h2, ex);
2015 >        checkCompletedWithWrappedException(h3, ex);
2016 >        r1.assertNotInvoked();
2017 >        r2.assertNotInvoked();
2018 >        r3.assertNotInvoked();
2019 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2020 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2021      }}
2022  
2023      /**
2024       * runAfterBoth result completes exceptionally if either source cancelled
2025       */
2026 <    public void testRunAfterBoth_sourceCancelled() {
2026 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2027          for (ExecutionMode m : ExecutionMode.values())
2028          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1667        for (boolean createIncomplete : new boolean[] { true, false })
2029          for (boolean fFirst : new boolean[] { true, false })
2030 +        for (boolean failFirst : new boolean[] { true, false })
2031          for (Integer v1 : new Integer[] { 1, null })
2032      {
2033          final CompletableFuture<Integer> f = new CompletableFuture<>();
2034          final CompletableFuture<Integer> g = new CompletableFuture<>();
2035 <        final Noop r = new Noop(m);
2035 >        final Noop r1 = new Noop(m);
2036 >        final Noop r2 = new Noop(m);
2037 >        final Noop r3 = new Noop(m);
2038  
2039 +        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2040 +        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2041 +        final Callable<Boolean> complete1 = failFirst ?
2042 +            () -> fst.cancel(mayInterruptIfRunning) :
2043 +            () -> fst.complete(v1);
2044 +        final Callable<Boolean> complete2 = failFirst ?
2045 +            () -> snd.complete(v1) :
2046 +            () -> snd.cancel(mayInterruptIfRunning);
2047  
2048 <        (fFirst ? f : g).complete(v1);
2049 <        if (!createIncomplete)
2050 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2051 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2052 <        if (createIncomplete) {
2053 <            checkIncomplete(h);
2054 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1683 <        }
2048 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2049 >        assertTrue(complete1.call());
2050 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2051 >        checkIncomplete(h1);
2052 >        checkIncomplete(h2);
2053 >        assertTrue(complete2.call());
2054 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2055  
2056 <        checkCompletedWithWrappedCancellationException(h);
2057 <        checkCancelled(!fFirst ? f : g);
2058 <        r.assertNotInvoked();
2059 <        checkCompletedNormally(fFirst ? f : g, v1);
2056 >        checkCompletedWithWrappedCancellationException(h1);
2057 >        checkCompletedWithWrappedCancellationException(h2);
2058 >        checkCompletedWithWrappedCancellationException(h3);
2059 >        r1.assertNotInvoked();
2060 >        r2.assertNotInvoked();
2061 >        r3.assertNotInvoked();
2062 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2063 >        checkCancelled(failFirst ? fst : snd);
2064      }}
2065  
2066      /**
# Line 1701 | Line 2076 | public class CompletableFutureTest exten
2076          final CompletableFuture<Integer> g = new CompletableFuture<>();
2077          final FailingRunnable r1 = new FailingRunnable(m);
2078          final FailingRunnable r2 = new FailingRunnable(m);
2079 +        final FailingRunnable r3 = new FailingRunnable(m);
2080  
2081 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2082 <        if (fFirst) {
2083 <            f.complete(v1);
2084 <            g.complete(v2);
2085 <        } else {
2086 <            g.complete(v2);
2087 <            f.complete(v1);
2088 <        }
2089 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2081 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2082 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2083 >        final Integer w1 =  fFirst ? v1 : v2;
2084 >        final Integer w2 = !fFirst ? v1 : v2;
2085 >
2086 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2087 >        assertTrue(fst.complete(w1));
2088 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2089 >        assertTrue(snd.complete(w2));
2090 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2091  
2092          checkCompletedWithWrappedCFException(h1);
2093          checkCompletedWithWrappedCFException(h2);
2094 +        checkCompletedWithWrappedCFException(h3);
2095 +        r1.assertInvoked();
2096 +        r2.assertInvoked();
2097 +        r3.assertInvoked();
2098          checkCompletedNormally(f, v1);
2099          checkCompletedNormally(g, v2);
2100      }}
# Line 1836 | Line 2217 | public class CompletableFutureTest exten
2217  
2218          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2219          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2220 <        if (fFirst) {
2221 <            f.complete(v1);
1841 <            g.completeExceptionally(ex);
1842 <        } else {
1843 <            g.completeExceptionally(ex);
1844 <            f.complete(v1);
1845 <        }
2220 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2221 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2222          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2223          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2224  
# Line 1948 | Line 2324 | public class CompletableFutureTest exten
2324  
2325          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2326          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2327 <        if (fFirst) {
2328 <            f.complete(v1);
1953 <            g.cancel(mayInterruptIfRunning);
1954 <        } else {
1955 <            g.cancel(mayInterruptIfRunning);
1956 <            f.complete(v1);
1957 <        }
2327 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2328 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2329          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2330          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2331  
# Line 2156 | Line 2527 | public class CompletableFutureTest exten
2527  
2528          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2529          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2530 <        if (fFirst) {
2531 <            f.complete(v1);
2161 <            g.completeExceptionally(ex);
2162 <        } else {
2163 <            g.completeExceptionally(ex);
2164 <            f.complete(v1);
2165 <        }
2530 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2531 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2532          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2533          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2534  
# Line 2365 | Line 2731 | public class CompletableFutureTest exten
2731          checkIncomplete(h1);
2732          rs[0].assertNotInvoked();
2733          rs[1].assertNotInvoked();
2734 <        f.completeExceptionally(ex);
2734 >        assertTrue(f.completeExceptionally(ex));
2735          checkCompletedWithWrappedException(h0, ex);
2736          checkCompletedWithWrappedException(h1, ex);
2737          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2373 | Line 2739 | public class CompletableFutureTest exten
2739          checkCompletedWithWrappedException(h2, ex);
2740          checkCompletedWithWrappedException(h3, ex);
2741  
2742 <        g.complete(v1);
2742 >        assertTrue(g.complete(v1));
2743  
2744          // unspecified behavior - both source completions available
2745          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2416 | Line 2782 | public class CompletableFutureTest exten
2782  
2783          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2784          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2785 <        if (fFirst) {
2786 <            f.complete(v1);
2421 <            g.completeExceptionally(ex);
2422 <        } else {
2423 <            g.completeExceptionally(ex);
2424 <            f.complete(v1);
2425 <        }
2785 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2786 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2787          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2788          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2789  
# Line 2487 | Line 2848 | public class CompletableFutureTest exten
2848          checkCompletedWithWrappedCancellationException(h2);
2849          checkCompletedWithWrappedCancellationException(h3);
2850  
2851 <        g.complete(v1);
2851 >        assertTrue(g.complete(v1));
2852  
2853          // unspecified behavior - both source completions available
2854          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2531 | Line 2892 | public class CompletableFutureTest exten
2892  
2893          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2894          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2895 <        f.complete(v1);
2895 >        assertTrue(f.complete(v1));
2896          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2897          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2898          checkCompletedWithWrappedCFException(h0);
# Line 2539 | Line 2900 | public class CompletableFutureTest exten
2900          checkCompletedWithWrappedCFException(h2);
2901          checkCompletedWithWrappedCFException(h3);
2902          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2903 <        g.complete(v2);
2903 >        assertTrue(g.complete(v2));
2904          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2905          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2906          checkCompletedWithWrappedCFException(h4);
# Line 2560 | Line 2921 | public class CompletableFutureTest exten
2921      {
2922          final CompletableFuture<Integer> f = new CompletableFuture<>();
2923          final CompletableFutureInc r = new CompletableFutureInc(m);
2924 <        if (!createIncomplete) f.complete(v1);
2924 >        if (!createIncomplete) assertTrue(f.complete(v1));
2925          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2926 <        if (createIncomplete) f.complete(v1);
2926 >        if (createIncomplete) assertTrue(f.complete(v1));
2927  
2928          checkCompletedNormally(g, inc(v1));
2929          checkCompletedNormally(f, v1);
# Line 2600 | Line 2961 | public class CompletableFutureTest exten
2961          final CompletableFuture<Integer> f = new CompletableFuture<>();
2962          final FailingCompletableFutureFunction r
2963              = new FailingCompletableFutureFunction(m);
2964 <        if (!createIncomplete) f.complete(v1);
2964 >        if (!createIncomplete) assertTrue(f.complete(v1));
2965          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2966 <        if (createIncomplete) f.complete(v1);
2966 >        if (createIncomplete) assertTrue(f.complete(v1));
2967  
2968          checkCompletedWithWrappedCFException(g);
2969          checkCompletedNormally(f, v1);
# Line 2645 | Line 3006 | public class CompletableFutureTest exten
3006       * when all components complete normally
3007       */
3008      public void testAllOf_normal() throws Exception {
3009 <        for (int k = 1; k < 20; ++k) {
3009 >        for (int k = 1; k < 10; k++) {
3010              CompletableFuture<Integer>[] fs
3011                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3012 <            for (int i = 0; i < k; ++i)
3012 >            for (int i = 0; i < k; i++)
3013                  fs[i] = new CompletableFuture<>();
3014              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3015 <            for (int i = 0; i < k; ++i) {
3015 >            for (int i = 0; i < k; i++) {
3016                  checkIncomplete(f);
3017                  checkIncomplete(CompletableFuture.allOf(fs));
3018                  fs[i].complete(one);
# Line 2662 | Line 3023 | public class CompletableFutureTest exten
3023      }
3024  
3025      public void testAllOf_backwards() throws Exception {
3026 <        for (int k = 1; k < 20; ++k) {
3026 >        for (int k = 1; k < 10; k++) {
3027              CompletableFuture<Integer>[] fs
3028                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3029 <            for (int i = 0; i < k; ++i)
3029 >            for (int i = 0; i < k; i++)
3030                  fs[i] = new CompletableFuture<>();
3031              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3032              for (int i = k - 1; i >= 0; i--) {
# Line 2678 | Line 3039 | public class CompletableFutureTest exten
3039          }
3040      }
3041  
3042 +    public void testAllOf_exceptional() throws Exception {
3043 +        for (int k = 1; k < 10; k++) {
3044 +            CompletableFuture<Integer>[] fs
3045 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3046 +            CFException ex = new CFException();
3047 +            for (int i = 0; i < k; i++)
3048 +                fs[i] = new CompletableFuture<>();
3049 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3050 +            for (int i = 0; i < k; i++) {
3051 +                checkIncomplete(f);
3052 +                checkIncomplete(CompletableFuture.allOf(fs));
3053 +                if (i != k/2) {
3054 +                    fs[i].complete(i);
3055 +                    checkCompletedNormally(fs[i], i);
3056 +                } else {
3057 +                    fs[i].completeExceptionally(ex);
3058 +                    checkCompletedExceptionally(fs[i], ex);
3059 +                }
3060 +            }
3061 +            checkCompletedWithWrappedException(f, ex);
3062 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3063 +        }
3064 +    }
3065 +
3066      /**
3067       * anyOf(no component futures) returns an incomplete future
3068       */
3069      public void testAnyOf_empty() throws Exception {
3070 +        for (Integer v1 : new Integer[] { 1, null })
3071 +    {
3072          CompletableFuture<Object> f = CompletableFuture.anyOf();
3073          checkIncomplete(f);
3074 <    }
3074 >
3075 >        f.complete(v1);
3076 >        checkCompletedNormally(f, v1);
3077 >    }}
3078  
3079      /**
3080       * anyOf returns a future completed normally with a value when
3081       * a component future does
3082       */
3083      public void testAnyOf_normal() throws Exception {
3084 <        for (int k = 0; k < 10; ++k) {
3084 >        for (int k = 0; k < 10; k++) {
3085              CompletableFuture[] fs = new CompletableFuture[k];
3086 <            for (int i = 0; i < k; ++i)
3086 >            for (int i = 0; i < k; i++)
3087                  fs[i] = new CompletableFuture<>();
3088              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3089              checkIncomplete(f);
3090 <            for (int i = 0; i < k; ++i) {
3091 <                fs[i].complete(one);
3092 <                checkCompletedNormally(f, one);
3093 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3090 >            for (int i = 0; i < k; i++) {
3091 >                fs[i].complete(i);
3092 >                checkCompletedNormally(f, 0);
3093 >                int x = (int) CompletableFuture.anyOf(fs).join();
3094 >                assertTrue(0 <= x && x <= i);
3095 >            }
3096 >        }
3097 >    }
3098 >    public void testAnyOf_normal_backwards() throws Exception {
3099 >        for (int k = 0; k < 10; k++) {
3100 >            CompletableFuture[] fs = new CompletableFuture[k];
3101 >            for (int i = 0; i < k; i++)
3102 >                fs[i] = new CompletableFuture<>();
3103 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3104 >            checkIncomplete(f);
3105 >            for (int i = k - 1; i >= 0; i--) {
3106 >                fs[i].complete(i);
3107 >                checkCompletedNormally(f, k - 1);
3108 >                int x = (int) CompletableFuture.anyOf(fs).join();
3109 >                assertTrue(i <= x && x <= k - 1);
3110              }
3111          }
3112      }
# Line 2709 | Line 3115 | public class CompletableFutureTest exten
3115       * anyOf result completes exceptionally when any component does.
3116       */
3117      public void testAnyOf_exceptional() throws Exception {
3118 <        for (int k = 0; k < 10; ++k) {
3118 >        for (int k = 0; k < 10; k++) {
3119 >            CompletableFuture[] fs = new CompletableFuture[k];
3120 >            CFException[] exs = new CFException[k];
3121 >            for (int i = 0; i < k; i++) {
3122 >                fs[i] = new CompletableFuture<>();
3123 >                exs[i] = new CFException();
3124 >            }
3125 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3126 >            checkIncomplete(f);
3127 >            for (int i = 0; i < k; i++) {
3128 >                fs[i].completeExceptionally(exs[i]);
3129 >                checkCompletedWithWrappedException(f, exs[0]);
3130 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3131 >            }
3132 >        }
3133 >    }
3134 >
3135 >    public void testAnyOf_exceptional_backwards() throws Exception {
3136 >        for (int k = 0; k < 10; k++) {
3137              CompletableFuture[] fs = new CompletableFuture[k];
3138 <            for (int i = 0; i < k; ++i)
3138 >            CFException[] exs = new CFException[k];
3139 >            for (int i = 0; i < k; i++) {
3140                  fs[i] = new CompletableFuture<>();
3141 +                exs[i] = new CFException();
3142 +            }
3143              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3144              checkIncomplete(f);
3145 <            for (int i = 0; i < k; ++i) {
3146 <                fs[i].completeExceptionally(new CFException());
3147 <                checkCompletedWithWrappedCFException(f);
3145 >            for (int i = k - 1; i >= 0; i--) {
3146 >                fs[i].completeExceptionally(exs[i]);
3147 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3148                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3149              }
3150          }
# Line 2736 | Line 3163 | public class CompletableFutureTest exten
3163          Runnable[] throwingActions = {
3164              () -> CompletableFuture.supplyAsync(null),
3165              () -> CompletableFuture.supplyAsync(null, exec),
3166 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3166 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3167  
3168              () -> CompletableFuture.runAsync(null),
3169              () -> CompletableFuture.runAsync(null, exec),
# Line 2841 | Line 3268 | public class CompletableFutureTest exten
3268          assertSame(f, f.toCompletableFuture());
3269      }
3270  
3271 <    /**
2845 <     * whenComplete action executes on normal completion, propagating
2846 <     * source result.
2847 <     */
2848 <    public void testWhenComplete_normalCompletion1() {
2849 <        for (ExecutionMode m : ExecutionMode.values())
2850 <        for (boolean createIncomplete : new boolean[] { true, false })
2851 <        for (Integer v1 : new Integer[] { 1, null })
2852 <    {
2853 <        final AtomicInteger a = new AtomicInteger(0);
2854 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2855 <        if (!createIncomplete) f.complete(v1);
2856 <        final CompletableFuture<Integer> g = m.whenComplete
2857 <            (f,
2858 <             (Integer x, Throwable t) -> {
2859 <                m.checkExecutionMode();
2860 <                threadAssertSame(x, v1);
2861 <                threadAssertNull(t);
2862 <                a.getAndIncrement();
2863 <            });
2864 <        if (createIncomplete) f.complete(v1);
3271 >    //--- tests of implementation details; not part of official tck ---
3272  
3273 <        checkCompletedNormally(g, v1);
3274 <        checkCompletedNormally(f, v1);
3275 <        assertEquals(1, a.get());
3276 <    }}
3273 >    Object resultOf(CompletableFuture<?> f) {
3274 >        try {
3275 >            java.lang.reflect.Field resultField
3276 >                = CompletableFuture.class.getDeclaredField("result");
3277 >            resultField.setAccessible(true);
3278 >            return resultField.get(f);
3279 >        } catch (Throwable t) { throw new AssertionError(t); }
3280 >    }
3281  
3282 <    /**
3283 <     * whenComplete action executes on exceptional completion, propagating
2873 <     * source result.
2874 <     */
2875 <    public void testWhenComplete_exceptionalCompletion() {
3282 >    public void testExceptionPropagationReusesResultObject() {
3283 >        if (!testImplementationDetails) return;
3284          for (ExecutionMode m : ExecutionMode.values())
2877        for (boolean createIncomplete : new boolean[] { true, false })
2878        for (Integer v1 : new Integer[] { 1, null })
3285      {
2880        final AtomicInteger a = new AtomicInteger(0);
3286          final CFException ex = new CFException();
3287 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3288 <        if (!createIncomplete) f.completeExceptionally(ex);
2884 <        final CompletableFuture<Integer> g = m.whenComplete
2885 <            (f,
2886 <             (Integer x, Throwable t) -> {
2887 <                m.checkExecutionMode();
2888 <                threadAssertNull(x);
2889 <                threadAssertSame(t, ex);
2890 <                a.getAndIncrement();
2891 <            });
2892 <        if (createIncomplete) f.completeExceptionally(ex);
2893 <        checkCompletedExceptionally(f, ex);
2894 <        checkCompletedWithWrappedException(g, ex);
2895 <        assertEquals(1, a.get());
2896 <    }}
3287 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3288 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3289  
3290 <    /**
3291 <     * whenComplete action executes on cancelled source, propagating
2900 <     * CancellationException.
2901 <     */
2902 <    public void testWhenComplete_sourceCancelled() {
2903 <        for (ExecutionMode m : ExecutionMode.values())
2904 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2905 <        for (boolean createIncomplete : new boolean[] { true, false })
2906 <    {
2907 <        final AtomicInteger a = new AtomicInteger(0);
2908 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2909 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2910 <        final CompletableFuture<Integer> g = m.whenComplete
2911 <            (f,
2912 <             (Integer x, Throwable t) -> {
2913 <                m.checkExecutionMode();
2914 <                threadAssertNull(x);
2915 <                threadAssertTrue(t instanceof CancellationException);
2916 <                a.getAndIncrement();
2917 <            });
2918 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
3290 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3291 >            = new ArrayList<>();
3292  
3293 <        checkCompletedWithWrappedCancellationException(g);
3294 <        checkCancelled(f);
3295 <        assertEquals(1, a.get());
2923 <    }}
3293 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3294 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3295 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3296  
3297 <    /**
3298 <     * If a whenComplete action throws an exception when triggered by
3299 <     * a normal completion, it completes exceptionally
2928 <     */
2929 <    public void testWhenComplete_actionFailed() {
2930 <        for (boolean createIncomplete : new boolean[] { true, false })
2931 <        for (ExecutionMode m : ExecutionMode.values())
2932 <        for (Integer v1 : new Integer[] { 1, null })
2933 <    {
2934 <        final AtomicInteger a = new AtomicInteger(0);
2935 <        final CFException ex = new CFException();
2936 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2937 <        if (!createIncomplete) f.complete(v1);
2938 <        final CompletableFuture<Integer> g = m.whenComplete
2939 <            (f,
2940 <             (Integer x, Throwable t) -> {
2941 <                m.checkExecutionMode();
2942 <                threadAssertSame(x, v1);
2943 <                threadAssertNull(t);
2944 <                a.getAndIncrement();
2945 <                throw ex;
2946 <            });
2947 <        if (createIncomplete) f.complete(v1);
2948 <        checkCompletedNormally(f, v1);
2949 <        checkCompletedWithWrappedException(g, ex);
2950 <        assertEquals(1, a.get());
2951 <    }}
3297 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3298 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3299 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3300  
3301 <    /**
3302 <     * If a whenComplete action throws an exception when triggered by
3303 <     * a source completion that also throws an exception, the source
2956 <     * exception takes precedence.
2957 <     */
2958 <    public void testWhenComplete_actionFailedSourceFailed() {
2959 <        for (boolean createIncomplete : new boolean[] { true, false })
2960 <        for (ExecutionMode m : ExecutionMode.values())
2961 <        for (Integer v1 : new Integer[] { 1, null })
2962 <    {
2963 <        final AtomicInteger a = new AtomicInteger(0);
2964 <        final CFException ex1 = new CFException();
2965 <        final CFException ex2 = new CFException();
2966 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3301 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3302 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3303 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3304  
3305 <        if (!createIncomplete) f.completeExceptionally(ex1);
2969 <        final CompletableFuture<Integer> g = m.whenComplete
2970 <            (f,
2971 <             (Integer x, Throwable t) -> {
2972 <                m.checkExecutionMode();
2973 <                threadAssertSame(t, ex1);
2974 <                threadAssertNull(x);
2975 <                a.getAndIncrement();
2976 <                throw ex2;
2977 <            });
2978 <        if (createIncomplete) f.completeExceptionally(ex1);
3305 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3306  
3307 <        checkCompletedExceptionally(f, ex1);
3308 <        checkCompletedWithWrappedException(g, ex1);
3309 <        assertEquals(1, a.get());
3307 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3308 >
3309 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3310 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3311 >
3312 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3313 >                 fun : funs) {
3314 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3315 >            f.completeExceptionally(ex);
3316 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3317 >            checkCompletedWithWrappedException(src, ex);
3318 >            CompletableFuture<?> dep = fun.apply(src);
3319 >            checkCompletedWithWrappedException(dep, ex);
3320 >            assertSame(resultOf(src), resultOf(dep));
3321 >        }
3322 >
3323 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3324 >                 fun : funs) {
3325 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3326 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3327 >            CompletableFuture<?> dep = fun.apply(src);
3328 >            f.completeExceptionally(ex);
3329 >            checkCompletedWithWrappedException(src, ex);
3330 >            checkCompletedWithWrappedException(dep, ex);
3331 >            assertSame(resultOf(src), resultOf(dep));
3332 >        }
3333 >
3334 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3335 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3336 >                 fun : funs) {
3337 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3338 >            f.cancel(mayInterruptIfRunning);
3339 >            checkCancelled(f);
3340 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3341 >            checkCompletedWithWrappedCancellationException(src);
3342 >            CompletableFuture<?> dep = fun.apply(src);
3343 >            checkCompletedWithWrappedCancellationException(dep);
3344 >            assertSame(resultOf(src), resultOf(dep));
3345 >        }
3346 >
3347 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3348 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3349 >                 fun : funs) {
3350 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3351 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3352 >            CompletableFuture<?> dep = fun.apply(src);
3353 >            f.cancel(mayInterruptIfRunning);
3354 >            checkCancelled(f);
3355 >            checkCompletedWithWrappedCancellationException(src);
3356 >            checkCompletedWithWrappedCancellationException(dep);
3357 >            assertSame(resultOf(src), resultOf(dep));
3358 >        }
3359      }}
3360  
3361   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines