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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.64 by jsr166, Fri Jun 6 17:08:48 2014 UTC vs.
Revision 1.78 by jsr166, Sat Jun 7 23:32:17 2014 UTC

# Line 105 | Line 105 | public class CompletableFutureTest exten
105          assertTrue(f.toString().contains("[Completed exceptionally]"));
106      }
107  
108 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f,
109 <                                              CFException ex) {
108 >    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 >                                                      Throwable ex) {
110          try {
111              f.get(LONG_DELAY_MS, MILLISECONDS);
112              shouldThrow();
# Line 131 | Line 131 | public class CompletableFutureTest exten
131          } catch (ExecutionException success) {
132              assertSame(ex, success.getCause());
133          } catch (Throwable fail) { threadUnexpectedException(fail); }
134 +
135          assertTrue(f.isDone());
136          assertFalse(f.isCancelled());
137          assertTrue(f.toString().contains("[Completed exceptionally]"));
138      }
139  
140 +    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
141 +                                                Throwable ex) {
142 +        checkCompletedExceptionallyWithRootCause(f, ex);
143 +        try {
144 +            CompletableFuture<Throwable> spy = f.handle
145 +                ((U u, Throwable t) -> t);
146 +            assertTrue(spy.join() instanceof CompletionException);
147 +            assertSame(ex, spy.join().getCause());
148 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 +    }
150 +
151 +    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
152 +        checkCompletedExceptionallyWithRootCause(f, ex);
153 +        try {
154 +            CompletableFuture<Throwable> spy = f.handle
155 +                ((U u, Throwable t) -> t);
156 +            assertSame(ex, spy.join());
157 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
158 +    }
159 +
160      void checkCancelled(CompletableFuture<?> f) {
161          try {
162              f.get(LONG_DELAY_MS, MILLISECONDS);
# Line 206 | Line 227 | public class CompletableFutureTest exten
227       * isCancelled, join, get, and getNow
228       */
229      public void testComplete() {
230 +        for (Integer v1 : new Integer[] { 1, null })
231 +    {
232          CompletableFuture<Integer> f = new CompletableFuture<>();
233          checkIncomplete(f);
234 <        f.complete(one);
235 <        checkCompletedNormally(f, one);
236 <    }
234 >        assertTrue(f.complete(v1));
235 >        assertFalse(f.complete(v1));
236 >        checkCompletedNormally(f, v1);
237 >    }}
238  
239      /**
240       * completeExceptionally completes exceptionally, as indicated by
# Line 218 | Line 242 | public class CompletableFutureTest exten
242       */
243      public void testCompleteExceptionally() {
244          CompletableFuture<Integer> f = new CompletableFuture<>();
245 +        CFException ex = new CFException();
246          checkIncomplete(f);
247 <        f.completeExceptionally(new CFException());
248 <        checkCompletedWithWrappedCFException(f);
247 >        f.completeExceptionally(ex);
248 >        checkCompletedExceptionally(f, ex);
249      }
250  
251      /**
# Line 228 | Line 253 | public class CompletableFutureTest exten
253       * methods isDone, isCancelled, join, get, and getNow
254       */
255      public void testCancel() {
256 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
257 +    {
258          CompletableFuture<Integer> f = new CompletableFuture<>();
259          checkIncomplete(f);
260          assertTrue(f.cancel(true));
261 +        assertTrue(f.cancel(true));
262          checkCancelled(f);
263 <    }
263 >    }}
264  
265      /**
266       * obtrudeValue forces completion with given value
# Line 240 | Line 268 | public class CompletableFutureTest exten
268      public void testObtrudeValue() {
269          CompletableFuture<Integer> f = new CompletableFuture<>();
270          checkIncomplete(f);
271 <        f.complete(one);
271 >        assertTrue(f.complete(one));
272          checkCompletedNormally(f, one);
273          f.obtrudeValue(three);
274          checkCompletedNormally(f, three);
# Line 261 | Line 289 | public class CompletableFutureTest exten
289       * obtrudeException forces completion with given exception
290       */
291      public void testObtrudeException() {
292 <        CompletableFuture<Integer> f = new CompletableFuture<>();
293 <        checkIncomplete(f);
294 <        f.complete(one);
295 <        checkCompletedNormally(f, one);
296 <        f.obtrudeException(new CFException());
269 <        checkCompletedWithWrappedCFException(f);
292 >        for (Integer v1 : new Integer[] { 1, null })
293 >    {
294 >        CFException ex;
295 >        CompletableFuture<Integer> f;
296 >
297          f = new CompletableFuture<>();
298 <        f.obtrudeException(new CFException());
299 <        checkCompletedWithWrappedCFException(f);
298 >        assertTrue(f.complete(v1));
299 >        for (int i = 0; i < 2; i++) {
300 >            f.obtrudeException(ex = new CFException());
301 >            checkCompletedExceptionally(f, ex);
302 >        }
303 >
304          f = new CompletableFuture<>();
305 +        for (int i = 0; i < 2; i++) {
306 +            f.obtrudeException(ex = new CFException());
307 +            checkCompletedExceptionally(f, ex);
308 +        }
309 +
310 +        f = new CompletableFuture<>();
311 +        f.completeExceptionally(ex = new CFException());
312 +        f.obtrudeValue(v1);
313 +        checkCompletedNormally(f, v1);
314 +        f.obtrudeException(ex = new CFException());
315 +        checkCompletedExceptionally(f, ex);
316          f.completeExceptionally(new CFException());
317 <        f.obtrudeValue(four);
318 <        checkCompletedNormally(f, four);
319 <        f.obtrudeException(new CFException());
320 <        checkCompletedWithWrappedCFException(f);
279 <    }
317 >        checkCompletedExceptionally(f, ex);
318 >        assertFalse(f.complete(v1));
319 >        checkCompletedExceptionally(f, ex);
320 >    }}
321  
322      /**
323       * getNumberOfDependents returns number of dependent tasks
324       */
325      public void testGetNumberOfDependents() {
326 +        for (ExecutionMode m : ExecutionMode.values())
327 +        for (Integer v1 : new Integer[] { 1, null })
328 +    {
329          CompletableFuture<Integer> f = new CompletableFuture<>();
330          assertEquals(0, f.getNumberOfDependents());
331 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
331 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
332          assertEquals(1, f.getNumberOfDependents());
333          assertEquals(0, g.getNumberOfDependents());
334 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
334 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
335          assertEquals(2, f.getNumberOfDependents());
336 <        f.complete(1);
336 >        assertEquals(0, h.getNumberOfDependents());
337 >        assertTrue(f.complete(v1));
338          checkCompletedNormally(g, null);
339 +        checkCompletedNormally(h, null);
340          assertEquals(0, f.getNumberOfDependents());
341          assertEquals(0, g.getNumberOfDependents());
342 <    }
342 >        assertEquals(0, h.getNumberOfDependents());
343 >    }}
344  
345      /**
346       * toString indicates current completion state
# Line 304 | Line 351 | public class CompletableFutureTest exten
351          f = new CompletableFuture<String>();
352          assertTrue(f.toString().contains("[Not completed]"));
353  
354 <        f.complete("foo");
354 >        assertTrue(f.complete("foo"));
355          assertTrue(f.toString().contains("[Completed normally]"));
356  
357          f = new CompletableFuture<String>();
358 <        f.completeExceptionally(new IndexOutOfBoundsException());
358 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359          assertTrue(f.toString().contains("[Completed exceptionally]"));
360 +
361 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
362 +            f = new CompletableFuture<String>();
363 +            assertTrue(f.cancel(mayInterruptIfRunning));
364 +            assertTrue(f.toString().contains("[Completed exceptionally]"));
365 +        }
366      }
367  
368      /**
# Line 486 | Line 539 | public class CompletableFutureTest exten
539              invoked();
540              value = x;
541              CompletableFuture<Integer> f = new CompletableFuture<>();
542 <            f.complete(inc(x));
542 >            assertTrue(f.complete(inc(x)));
543              return f;
544          }
545      }
# Line 794 | Line 847 | public class CompletableFutureTest exten
847      {
848          final AtomicInteger a = new AtomicInteger(0);
849          final CompletableFuture<Integer> f = new CompletableFuture<>();
850 <        if (!createIncomplete) f.complete(v1);
850 >        if (!createIncomplete) assertTrue(f.complete(v1));
851          final CompletableFuture<Integer> g = f.exceptionally
852              ((Throwable t) -> {
853                  // Should not be called
854                  a.getAndIncrement();
855                  throw new AssertionError();
856              });
857 <        if (createIncomplete) f.complete(v1);
857 >        if (createIncomplete) assertTrue(f.complete(v1));
858  
859          checkCompletedNormally(g, v1);
860          checkCompletedNormally(f, v1);
861          assertEquals(0, a.get());
862      }}
863  
811
864      /**
865       * exceptionally action completes with function value on source
866       * exception
# Line 852 | Line 904 | public class CompletableFutureTest exten
904              });
905          if (createIncomplete) f.completeExceptionally(ex1);
906  
907 <        checkCompletedWithWrappedCFException(g, ex2);
907 >        checkCompletedWithWrappedException(g, ex2);
908 >        assertEquals(1, a.get());
909 >    }}
910 >
911 >    /**
912 >     * whenComplete action executes on normal completion, propagating
913 >     * source result.
914 >     */
915 >    public void testWhenComplete_normalCompletion1() {
916 >        for (ExecutionMode m : ExecutionMode.values())
917 >        for (boolean createIncomplete : new boolean[] { true, false })
918 >        for (Integer v1 : new Integer[] { 1, null })
919 >    {
920 >        final AtomicInteger a = new AtomicInteger(0);
921 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
922 >        if (!createIncomplete) assertTrue(f.complete(v1));
923 >        final CompletableFuture<Integer> g = m.whenComplete
924 >            (f,
925 >             (Integer x, Throwable t) -> {
926 >                m.checkExecutionMode();
927 >                threadAssertSame(x, v1);
928 >                threadAssertNull(t);
929 >                a.getAndIncrement();
930 >            });
931 >        if (createIncomplete) assertTrue(f.complete(v1));
932 >
933 >        checkCompletedNormally(g, v1);
934 >        checkCompletedNormally(f, v1);
935 >        assertEquals(1, a.get());
936 >    }}
937 >
938 >    /**
939 >     * whenComplete action executes on exceptional completion, propagating
940 >     * source result.
941 >     */
942 >    public void testWhenComplete_exceptionalCompletion() {
943 >        for (ExecutionMode m : ExecutionMode.values())
944 >        for (boolean createIncomplete : new boolean[] { true, false })
945 >        for (Integer v1 : new Integer[] { 1, null })
946 >    {
947 >        final AtomicInteger a = new AtomicInteger(0);
948 >        final CFException ex = new CFException();
949 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
950 >        if (!createIncomplete) f.completeExceptionally(ex);
951 >        final CompletableFuture<Integer> g = m.whenComplete
952 >            (f,
953 >             (Integer x, Throwable t) -> {
954 >                m.checkExecutionMode();
955 >                threadAssertNull(x);
956 >                threadAssertSame(t, ex);
957 >                a.getAndIncrement();
958 >            });
959 >        if (createIncomplete) f.completeExceptionally(ex);
960 >
961 >        checkCompletedWithWrappedException(g, ex);
962 >        checkCompletedExceptionally(f, ex);
963 >        assertEquals(1, a.get());
964 >    }}
965 >
966 >    /**
967 >     * whenComplete action executes on cancelled source, propagating
968 >     * CancellationException.
969 >     */
970 >    public void testWhenComplete_sourceCancelled() {
971 >        for (ExecutionMode m : ExecutionMode.values())
972 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
973 >        for (boolean createIncomplete : new boolean[] { true, false })
974 >    {
975 >        final AtomicInteger a = new AtomicInteger(0);
976 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
977 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
978 >        final CompletableFuture<Integer> g = m.whenComplete
979 >            (f,
980 >             (Integer x, Throwable t) -> {
981 >                m.checkExecutionMode();
982 >                threadAssertNull(x);
983 >                threadAssertTrue(t instanceof CancellationException);
984 >                a.getAndIncrement();
985 >            });
986 >        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
987 >
988 >        checkCompletedWithWrappedCancellationException(g);
989 >        checkCancelled(f);
990 >        assertEquals(1, a.get());
991 >    }}
992 >
993 >    /**
994 >     * If a whenComplete action throws an exception when triggered by
995 >     * a normal completion, it completes exceptionally
996 >     */
997 >    public void testWhenComplete_actionFailed() {
998 >        for (boolean createIncomplete : new boolean[] { true, false })
999 >        for (ExecutionMode m : ExecutionMode.values())
1000 >        for (Integer v1 : new Integer[] { 1, null })
1001 >    {
1002 >        final AtomicInteger a = new AtomicInteger(0);
1003 >        final CFException ex = new CFException();
1004 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1005 >        if (!createIncomplete) assertTrue(f.complete(v1));
1006 >        final CompletableFuture<Integer> g = m.whenComplete
1007 >            (f,
1008 >             (Integer x, Throwable t) -> {
1009 >                m.checkExecutionMode();
1010 >                threadAssertSame(x, v1);
1011 >                threadAssertNull(t);
1012 >                a.getAndIncrement();
1013 >                throw ex;
1014 >            });
1015 >        if (createIncomplete) assertTrue(f.complete(v1));
1016 >
1017 >        checkCompletedWithWrappedException(g, ex);
1018 >        checkCompletedNormally(f, v1);
1019 >        assertEquals(1, a.get());
1020 >    }}
1021 >
1022 >    /**
1023 >     * If a whenComplete action throws an exception when triggered by
1024 >     * a source completion that also throws an exception, the source
1025 >     * exception takes precedence.
1026 >     */
1027 >    public void testWhenComplete_actionFailedSourceFailed() {
1028 >        for (boolean createIncomplete : new boolean[] { true, false })
1029 >        for (ExecutionMode m : ExecutionMode.values())
1030 >        for (Integer v1 : new Integer[] { 1, null })
1031 >    {
1032 >        final AtomicInteger a = new AtomicInteger(0);
1033 >        final CFException ex1 = new CFException();
1034 >        final CFException ex2 = new CFException();
1035 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1036 >
1037 >        if (!createIncomplete) f.completeExceptionally(ex1);
1038 >        final CompletableFuture<Integer> g = m.whenComplete
1039 >            (f,
1040 >             (Integer x, Throwable t) -> {
1041 >                m.checkExecutionMode();
1042 >                threadAssertSame(t, ex1);
1043 >                threadAssertNull(x);
1044 >                a.getAndIncrement();
1045 >                throw ex2;
1046 >            });
1047 >        if (createIncomplete) f.completeExceptionally(ex1);
1048 >
1049 >        checkCompletedWithWrappedException(g, ex1);
1050 >        checkCompletedExceptionally(f, ex1);
1051          assertEquals(1, a.get());
1052      }}
1053  
# Line 867 | Line 1062 | public class CompletableFutureTest exten
1062      {
1063          final CompletableFuture<Integer> f = new CompletableFuture<>();
1064          final AtomicInteger a = new AtomicInteger(0);
1065 <        if (!createIncomplete) f.complete(v1);
1065 >        if (!createIncomplete) assertTrue(f.complete(v1));
1066          final CompletableFuture<Integer> g = m.handle
1067              (f,
1068               (Integer x, Throwable t) -> {
# Line 877 | Line 1072 | public class CompletableFutureTest exten
1072                  a.getAndIncrement();
1073                  return inc(v1);
1074              });
1075 <        if (createIncomplete) f.complete(v1);
1075 >        if (createIncomplete) assertTrue(f.complete(v1));
1076  
1077          checkCompletedNormally(g, inc(v1));
1078          checkCompletedNormally(f, v1);
# Line 909 | Line 1104 | public class CompletableFutureTest exten
1104          if (createIncomplete) f.completeExceptionally(ex);
1105  
1106          checkCompletedNormally(g, v1);
1107 <        checkCompletedWithWrappedCFException(f, ex);
1107 >        checkCompletedExceptionally(f, ex);
1108          assertEquals(1, a.get());
1109      }}
1110  
# Line 965 | Line 1160 | public class CompletableFutureTest exten
1160              });
1161          if (createIncomplete) f.completeExceptionally(ex1);
1162  
1163 <        checkCompletedWithWrappedCFException(g, ex2);
1164 <        checkCompletedWithWrappedCFException(f, ex1);
1163 >        checkCompletedWithWrappedException(g, ex2);
1164 >        checkCompletedExceptionally(f, ex1);
1165          assertEquals(1, a.get());
1166      }}
1167  
# Line 978 | Line 1173 | public class CompletableFutureTest exten
1173          final CompletableFuture<Integer> f = new CompletableFuture<>();
1174          final AtomicInteger a = new AtomicInteger(0);
1175          final CFException ex = new CFException();
1176 <        if (!createIncomplete) f.complete(v1);
1176 >        if (!createIncomplete) assertTrue(f.complete(v1));
1177          final CompletableFuture<Integer> g = m.handle
1178              (f,
1179               (Integer x, Throwable t) -> {
# Line 988 | Line 1183 | public class CompletableFutureTest exten
1183                  a.getAndIncrement();
1184                  throw ex;
1185              });
1186 <        if (createIncomplete) f.complete(v1);
1186 >        if (createIncomplete) assertTrue(f.complete(v1));
1187  
1188 <        checkCompletedWithWrappedCFException(g, ex);
1188 >        checkCompletedWithWrappedException(g, ex);
1189          checkCompletedNormally(f, v1);
1190          assertEquals(1, a.get());
1191      }}
# Line 1074 | Line 1269 | public class CompletableFutureTest exten
1269      {
1270          final CompletableFuture<Integer> f = new CompletableFuture<>();
1271          final Noop r = new Noop(m);
1272 <        if (!createIncomplete) f.complete(v1);
1272 >        if (!createIncomplete) assertTrue(f.complete(v1));
1273          final CompletableFuture<Void> g = m.thenRun(f, r);
1274          if (createIncomplete) {
1275              checkIncomplete(g);
1276 <            f.complete(v1);
1276 >            assertTrue(f.complete(v1));
1277          }
1278  
1279          checkCompletedNormally(g, null);
# Line 1104 | Line 1299 | public class CompletableFutureTest exten
1299              f.completeExceptionally(ex);
1300          }
1301  
1302 <        checkCompletedWithWrappedCFException(g, ex);
1303 <        checkCompletedWithWrappedCFException(f, ex);
1302 >        checkCompletedWithWrappedException(g, ex);
1303 >        checkCompletedExceptionally(f, ex);
1304          r.assertNotInvoked();
1305      }}
1306  
# Line 1141 | Line 1336 | public class CompletableFutureTest exten
1336      {
1337          final CompletableFuture<Integer> f = new CompletableFuture<>();
1338          final FailingRunnable r = new FailingRunnable(m);
1339 <        if (!createIncomplete) f.complete(v1);
1339 >        if (!createIncomplete) assertTrue(f.complete(v1));
1340          final CompletableFuture<Void> g = m.thenRun(f, r);
1341          if (createIncomplete) {
1342              checkIncomplete(g);
1343 <            f.complete(v1);
1343 >            assertTrue(f.complete(v1));
1344          }
1345  
1346          checkCompletedWithWrappedCFException(g);
# Line 1162 | Line 1357 | public class CompletableFutureTest exten
1357      {
1358          final CompletableFuture<Integer> f = new CompletableFuture<>();
1359          final IncFunction r = new IncFunction(m);
1360 <        if (!createIncomplete) f.complete(v1);
1360 >        if (!createIncomplete) assertTrue(f.complete(v1));
1361          final CompletableFuture<Integer> g = m.thenApply(f, r);
1362          if (createIncomplete) {
1363              checkIncomplete(g);
1364 <            f.complete(v1);
1364 >            assertTrue(f.complete(v1));
1365          }
1366  
1367          checkCompletedNormally(g, inc(v1));
1368          checkCompletedNormally(f, v1);
1369 <        r.assertInvoked();
1369 >        r.assertValue(inc(v1));
1370      }}
1371  
1372      /**
# Line 1192 | Line 1387 | public class CompletableFutureTest exten
1387              f.completeExceptionally(ex);
1388          }
1389  
1390 <        checkCompletedWithWrappedCFException(g, ex);
1391 <        checkCompletedWithWrappedCFException(f, ex);
1390 >        checkCompletedWithWrappedException(g, ex);
1391 >        checkCompletedExceptionally(f, ex);
1392          r.assertNotInvoked();
1393      }}
1394  
# Line 1229 | Line 1424 | public class CompletableFutureTest exten
1424      {
1425          final CompletableFuture<Integer> f = new CompletableFuture<>();
1426          final FailingFunction r = new FailingFunction(m);
1427 <        if (!createIncomplete) f.complete(v1);
1427 >        if (!createIncomplete) assertTrue(f.complete(v1));
1428          final CompletableFuture<Integer> g = m.thenApply(f, r);
1429          if (createIncomplete) {
1430              checkIncomplete(g);
1431 <            f.complete(v1);
1431 >            assertTrue(f.complete(v1));
1432          }
1433  
1434          checkCompletedWithWrappedCFException(g);
# Line 1250 | Line 1445 | public class CompletableFutureTest exten
1445      {
1446          final CompletableFuture<Integer> f = new CompletableFuture<>();
1447          final NoopConsumer r = new NoopConsumer(m);
1448 <        if (!createIncomplete) f.complete(v1);
1448 >        if (!createIncomplete) assertTrue(f.complete(v1));
1449          final CompletableFuture<Void> g = m.thenAccept(f, r);
1450          if (createIncomplete) {
1451              checkIncomplete(g);
1452 <            f.complete(v1);
1452 >            assertTrue(f.complete(v1));
1453          }
1454  
1455          checkCompletedNormally(g, null);
# Line 1280 | Line 1475 | public class CompletableFutureTest exten
1475              f.completeExceptionally(ex);
1476          }
1477  
1478 <        checkCompletedWithWrappedCFException(g, ex);
1479 <        checkCompletedWithWrappedCFException(f, ex);
1478 >        checkCompletedWithWrappedException(g, ex);
1479 >        checkCompletedExceptionally(f, ex);
1480          r.assertNotInvoked();
1481      }}
1482  
# Line 1343 | Line 1538 | public class CompletableFutureTest exten
1538          final CompletableFuture<Integer> g = new CompletableFuture<>();
1539          final SubtractFunction r = new SubtractFunction(m);
1540  
1541 <        if (fFirst) f.complete(v1); else g.complete(v2);
1541 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1542          if (!createIncomplete)
1543 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1543 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1544          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1545          if (createIncomplete) {
1546              checkIncomplete(h);
1547              r.assertNotInvoked();
1548 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1548 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1549          }
1550  
1551          checkCompletedNormally(h, subtract(v1, v2));
1552          checkCompletedNormally(f, v1);
1553          checkCompletedNormally(g, v2);
1554 <        r.assertInvoked();
1554 >        r.assertValue(subtract(v1, v2));
1555      }}
1556  
1557      /**
# Line 1374 | Line 1569 | public class CompletableFutureTest exten
1569          final CFException ex = new CFException();
1570          final SubtractFunction r = new SubtractFunction(m);
1571  
1572 <        (fFirst ? f : g).complete(v1);
1572 >        assertTrue((fFirst ? f : g).complete(v1));
1573          if (!createIncomplete)
1574 <            (!fFirst ? f : g).completeExceptionally(ex);
1574 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1575          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1576          if (createIncomplete) {
1577              checkIncomplete(h);
1578 <            (!fFirst ? f : g).completeExceptionally(ex);
1578 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1579          }
1580  
1581 <        checkCompletedWithWrappedCFException(h, ex);
1581 >        checkCompletedWithWrappedException(h, ex);
1582          r.assertNotInvoked();
1583          checkCompletedNormally(fFirst ? f : g, v1);
1584 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1584 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1585      }}
1586  
1587      /**
# Line 1403 | Line 1598 | public class CompletableFutureTest exten
1598          final CompletableFuture<Integer> g = new CompletableFuture<>();
1599          final SubtractFunction r = new SubtractFunction(m);
1600  
1601 <        (fFirst ? f : g).complete(v1);
1601 >        assertTrue((fFirst ? f : g).complete(v1));
1602          if (!createIncomplete)
1603              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1604          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
# Line 1432 | Line 1627 | public class CompletableFutureTest exten
1627          final FailingBiFunction r = new FailingBiFunction(m);
1628          final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1629  
1630 <        if (fFirst) {
1631 <            f.complete(v1);
1437 <            g.complete(v2);
1438 <        } else {
1439 <            g.complete(v2);
1440 <            f.complete(v1);
1441 <        }
1630 >        assertTrue( fFirst ? f.complete(v1) : g.complete(v2));
1631 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1632  
1633          checkCompletedWithWrappedCFException(h);
1634          checkCompletedNormally(f, v1);
# Line 1460 | Line 1650 | public class CompletableFutureTest exten
1650          final CompletableFuture<Integer> g = new CompletableFuture<>();
1651          final SubtractAction r = new SubtractAction(m);
1652  
1653 <        if (fFirst) f.complete(v1); else g.complete(v2);
1653 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1654          if (!createIncomplete)
1655 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1655 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1656          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1657          if (createIncomplete) {
1658              checkIncomplete(h);
1659              r.assertNotInvoked();
1660 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1660 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1661          }
1662  
1663          checkCompletedNormally(h, null);
# Line 1491 | Line 1681 | public class CompletableFutureTest exten
1681          final CFException ex = new CFException();
1682          final SubtractAction r = new SubtractAction(m);
1683  
1684 <        (fFirst ? f : g).complete(v1);
1684 >        assertTrue((fFirst ? f : g).complete(v1));
1685          if (!createIncomplete)
1686 <            (!fFirst ? f : g).completeExceptionally(ex);
1686 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1687          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1688          if (createIncomplete) {
1689              checkIncomplete(h);
1690 <            (!fFirst ? f : g).completeExceptionally(ex);
1690 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1691          }
1692  
1693 <        checkCompletedWithWrappedCFException(h, ex);
1693 >        checkCompletedWithWrappedException(h, ex);
1694          r.assertNotInvoked();
1695          checkCompletedNormally(fFirst ? f : g, v1);
1696 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1696 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1697      }}
1698  
1699      /**
# Line 1520 | Line 1710 | public class CompletableFutureTest exten
1710          final CompletableFuture<Integer> g = new CompletableFuture<>();
1711          final SubtractAction r = new SubtractAction(m);
1712  
1713 <        (fFirst ? f : g).complete(v1);
1713 >        assertTrue((fFirst ? f : g).complete(v1));
1714          if (!createIncomplete)
1715              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1716          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
# Line 1549 | Line 1739 | public class CompletableFutureTest exten
1739          final FailingBiConsumer r = new FailingBiConsumer(m);
1740          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1741  
1742 <        if (fFirst) {
1743 <            f.complete(v1);
1554 <            g.complete(v2);
1555 <        } else {
1556 <            g.complete(v2);
1557 <            f.complete(v1);
1558 <        }
1742 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1743 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1744  
1745          checkCompletedWithWrappedCFException(h);
1746          checkCompletedNormally(f, v1);
# Line 1577 | Line 1762 | public class CompletableFutureTest exten
1762          final CompletableFuture<Integer> g = new CompletableFuture<>();
1763          final Noop r = new Noop(m);
1764  
1765 <        if (fFirst) f.complete(v1); else g.complete(v2);
1765 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1766          if (!createIncomplete)
1767 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1767 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1768          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1769          if (createIncomplete) {
1770              checkIncomplete(h);
1771              r.assertNotInvoked();
1772 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1772 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1773          }
1774  
1775          checkCompletedNormally(h, null);
# Line 1608 | Line 1793 | public class CompletableFutureTest exten
1793          final CFException ex = new CFException();
1794          final Noop r = new Noop(m);
1795  
1796 <        (fFirst ? f : g).complete(v1);
1796 >        assertTrue((fFirst ? f : g).complete(v1));
1797          if (!createIncomplete)
1798 <            (!fFirst ? f : g).completeExceptionally(ex);
1798 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1799          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1800          if (createIncomplete) {
1801              checkIncomplete(h);
1802 <            (!fFirst ? f : g).completeExceptionally(ex);
1802 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1803          }
1804  
1805 <        checkCompletedWithWrappedCFException(h, ex);
1805 >        checkCompletedWithWrappedException(h, ex);
1806          r.assertNotInvoked();
1807          checkCompletedNormally(fFirst ? f : g, v1);
1808 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1808 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1809      }}
1810  
1811      /**
# Line 1637 | Line 1822 | public class CompletableFutureTest exten
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823          final Noop r = new Noop(m);
1824  
1825 <
1641 <        (fFirst ? f : g).complete(v1);
1825 >        assertTrue((fFirst ? f : g).complete(v1));
1826          if (!createIncomplete)
1827              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1828          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
# Line 1668 | Line 1852 | public class CompletableFutureTest exten
1852          final FailingRunnable r2 = new FailingRunnable(m);
1853  
1854          CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1855 <        if (fFirst) {
1856 <            f.complete(v1);
1673 <            g.complete(v2);
1674 <        } else {
1675 <            g.complete(v2);
1676 <            f.complete(v1);
1677 <        }
1855 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1856 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1857          CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1858  
1859          checkCompletedWithWrappedCFException(h1);
# Line 1752 | Line 1931 | public class CompletableFutureTest exten
1931          rs[0].assertNotInvoked();
1932          rs[1].assertNotInvoked();
1933          f.completeExceptionally(ex);
1934 <        checkCompletedWithWrappedCFException(h0, ex);
1935 <        checkCompletedWithWrappedCFException(h1, ex);
1934 >        checkCompletedWithWrappedException(h0, ex);
1935 >        checkCompletedWithWrappedException(h1, ex);
1936          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1937          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1938 <        checkCompletedWithWrappedCFException(h2, ex);
1939 <        checkCompletedWithWrappedCFException(h3, ex);
1938 >        checkCompletedWithWrappedException(h2, ex);
1939 >        checkCompletedWithWrappedException(h3, ex);
1940          g.complete(v1);
1941  
1942          // unspecified behavior - both source completions available
# Line 1765 | Line 1944 | public class CompletableFutureTest exten
1944          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1945          try {
1946              assertEquals(inc(v1), h4.join());
1947 <            rs[4].assertInvoked();
1947 >            rs[4].assertValue(inc(v1));
1948          } catch (CompletionException ok) {
1949 <            checkCompletedWithWrappedCFException(h4, ex);
1949 >            checkCompletedWithWrappedException(h4, ex);
1950              rs[4].assertNotInvoked();
1951          }
1952          try {
1953              assertEquals(inc(v1), h5.join());
1954 <            rs[5].assertInvoked();
1954 >            rs[5].assertValue(inc(v1));
1955          } catch (CompletionException ok) {
1956 <            checkCompletedWithWrappedCFException(h5, ex);
1956 >            checkCompletedWithWrappedException(h5, ex);
1957              rs[5].assertNotInvoked();
1958          }
1959  
1960 <        checkCompletedWithWrappedCFException(f, ex);
1960 >        checkCompletedExceptionally(f, ex);
1961          checkCompletedNormally(g, v1);
1962 <        checkCompletedWithWrappedCFException(h0, ex);
1963 <        checkCompletedWithWrappedCFException(h1, ex);
1964 <        checkCompletedWithWrappedCFException(h2, ex);
1965 <        checkCompletedWithWrappedCFException(h3, ex);
1966 <        checkCompletedWithWrappedCFException(h4, ex);
1962 >        checkCompletedWithWrappedException(h0, ex);
1963 >        checkCompletedWithWrappedException(h1, ex);
1964 >        checkCompletedWithWrappedException(h2, ex);
1965 >        checkCompletedWithWrappedException(h3, ex);
1966 >        checkCompletedWithWrappedException(h4, ex);
1967          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1968      }}
1969  
1970 +    public void testApplyToEither_exceptionalCompletion2() {
1971 +        for (ExecutionMode m : ExecutionMode.values())
1972 +        for (boolean fFirst : new boolean[] { true, false })
1973 +        for (Integer v1 : new Integer[] { 1, null })
1974 +    {
1975 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1976 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1977 +        final CFException ex = new CFException();
1978 +        final IncFunction[] rs = new IncFunction[6];
1979 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1980 +
1981 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1982 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1983 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
1984 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
1985 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1986 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1987 +
1988 +        // unspecified behavior - both source completions available
1989 +        try {
1990 +            assertEquals(inc(v1), h0.join());
1991 +            rs[0].assertValue(inc(v1));
1992 +        } catch (CompletionException ok) {
1993 +            checkCompletedWithWrappedException(h0, ex);
1994 +            rs[0].assertNotInvoked();
1995 +        }
1996 +        try {
1997 +            assertEquals(inc(v1), h1.join());
1998 +            rs[1].assertValue(inc(v1));
1999 +        } catch (CompletionException ok) {
2000 +            checkCompletedWithWrappedException(h1, ex);
2001 +            rs[1].assertNotInvoked();
2002 +        }
2003 +        try {
2004 +            assertEquals(inc(v1), h2.join());
2005 +            rs[2].assertValue(inc(v1));
2006 +        } catch (CompletionException ok) {
2007 +            checkCompletedWithWrappedException(h2, ex);
2008 +            rs[2].assertNotInvoked();
2009 +        }
2010 +        try {
2011 +            assertEquals(inc(v1), h3.join());
2012 +            rs[3].assertValue(inc(v1));
2013 +        } catch (CompletionException ok) {
2014 +            checkCompletedWithWrappedException(h3, ex);
2015 +            rs[3].assertNotInvoked();
2016 +        }
2017 +
2018 +        checkCompletedNormally(f, v1);
2019 +        checkCompletedExceptionally(g, ex);
2020 +    }}
2021 +
2022      /**
2023       * applyToEither result completes exceptionally if either source cancelled
2024       */
# Line 1821 | Line 2052 | public class CompletableFutureTest exten
2052          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2053          try {
2054              assertEquals(inc(v1), h4.join());
2055 <            rs[4].assertInvoked();
2055 >            rs[4].assertValue(inc(v1));
2056          } catch (CompletionException ok) {
2057              checkCompletedWithWrappedCancellationException(h4);
2058              rs[4].assertNotInvoked();
2059          }
2060          try {
2061              assertEquals(inc(v1), h5.join());
2062 <            rs[5].assertInvoked();
2062 >            rs[5].assertValue(inc(v1));
2063          } catch (CompletionException ok) {
2064              checkCompletedWithWrappedCancellationException(h5);
2065              rs[5].assertNotInvoked();
# Line 1843 | Line 2074 | public class CompletableFutureTest exten
2074          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2075      }}
2076  
2077 +    public void testApplyToEither_sourceCancelled2() {
2078 +        for (ExecutionMode m : ExecutionMode.values())
2079 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2080 +        for (boolean fFirst : new boolean[] { true, false })
2081 +        for (Integer v1 : new Integer[] { 1, null })
2082 +    {
2083 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2084 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2085 +        final IncFunction[] rs = new IncFunction[6];
2086 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
2087 +
2088 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2089 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2090 +        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2091 +        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2092 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2093 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2094 +
2095 +        // unspecified behavior - both source completions available
2096 +        try {
2097 +            assertEquals(inc(v1), h0.join());
2098 +            rs[0].assertValue(inc(v1));
2099 +        } catch (CompletionException ok) {
2100 +            checkCompletedWithWrappedCancellationException(h0);
2101 +            rs[0].assertNotInvoked();
2102 +        }
2103 +        try {
2104 +            assertEquals(inc(v1), h1.join());
2105 +            rs[1].assertValue(inc(v1));
2106 +        } catch (CompletionException ok) {
2107 +            checkCompletedWithWrappedCancellationException(h1);
2108 +            rs[1].assertNotInvoked();
2109 +        }
2110 +        try {
2111 +            assertEquals(inc(v1), h2.join());
2112 +            rs[2].assertValue(inc(v1));
2113 +        } catch (CompletionException ok) {
2114 +            checkCompletedWithWrappedCancellationException(h2);
2115 +            rs[2].assertNotInvoked();
2116 +        }
2117 +        try {
2118 +            assertEquals(inc(v1), h3.join());
2119 +            rs[3].assertValue(inc(v1));
2120 +        } catch (CompletionException ok) {
2121 +            checkCompletedWithWrappedCancellationException(h3);
2122 +            rs[3].assertNotInvoked();
2123 +        }
2124 +
2125 +        checkCompletedNormally(f, v1);
2126 +        checkCancelled(g);
2127 +    }}
2128 +
2129      /**
2130       * applyToEither result completes exceptionally if action does
2131       */
# Line 1957 | Line 2240 | public class CompletableFutureTest exten
2240          rs[0].assertNotInvoked();
2241          rs[1].assertNotInvoked();
2242          f.completeExceptionally(ex);
2243 <        checkCompletedWithWrappedCFException(h0, ex);
2244 <        checkCompletedWithWrappedCFException(h1, ex);
2243 >        checkCompletedWithWrappedException(h0, ex);
2244 >        checkCompletedWithWrappedException(h1, ex);
2245          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2246          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2247 <        checkCompletedWithWrappedCFException(h2, ex);
2248 <        checkCompletedWithWrappedCFException(h3, ex);
2247 >        checkCompletedWithWrappedException(h2, ex);
2248 >        checkCompletedWithWrappedException(h3, ex);
2249  
2250          g.complete(v1);
2251  
# Line 1973 | Line 2256 | public class CompletableFutureTest exten
2256              assertNull(h4.join());
2257              rs[4].assertValue(v1);
2258          } catch (CompletionException ok) {
2259 <            checkCompletedWithWrappedCFException(h4, ex);
2259 >            checkCompletedWithWrappedException(h4, ex);
2260              rs[4].assertNotInvoked();
2261          }
2262          try {
2263              assertNull(h5.join());
2264              rs[5].assertValue(v1);
2265          } catch (CompletionException ok) {
2266 <            checkCompletedWithWrappedCFException(h5, ex);
2266 >            checkCompletedWithWrappedException(h5, ex);
2267              rs[5].assertNotInvoked();
2268          }
2269  
2270 <        checkCompletedWithWrappedCFException(f, ex);
2270 >        checkCompletedExceptionally(f, ex);
2271          checkCompletedNormally(g, v1);
2272 <        checkCompletedWithWrappedCFException(h0, ex);
2273 <        checkCompletedWithWrappedCFException(h1, ex);
2274 <        checkCompletedWithWrappedCFException(h2, ex);
2275 <        checkCompletedWithWrappedCFException(h3, ex);
2276 <        checkCompletedWithWrappedCFException(h4, ex);
2272 >        checkCompletedWithWrappedException(h0, ex);
2273 >        checkCompletedWithWrappedException(h1, ex);
2274 >        checkCompletedWithWrappedException(h2, ex);
2275 >        checkCompletedWithWrappedException(h3, ex);
2276 >        checkCompletedWithWrappedException(h4, ex);
2277          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2278      }}
2279  
2280 +    public void testAcceptEither_exceptionalCompletion2() {
2281 +        for (ExecutionMode m : ExecutionMode.values())
2282 +        for (boolean fFirst : new boolean[] { true, false })
2283 +        for (Integer v1 : new Integer[] { 1, null })
2284 +    {
2285 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2286 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2287 +        final CFException ex = new CFException();
2288 +        final NoopConsumer[] rs = new NoopConsumer[6];
2289 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2290 +
2291 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2292 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2293 +        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2294 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2295 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2296 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2297 +
2298 +        // unspecified behavior - both source completions available
2299 +        try {
2300 +            assertEquals(null, h0.join());
2301 +            rs[0].assertValue(v1);
2302 +        } catch (CompletionException ok) {
2303 +            checkCompletedWithWrappedException(h0, ex);
2304 +            rs[0].assertNotInvoked();
2305 +        }
2306 +        try {
2307 +            assertEquals(null, h1.join());
2308 +            rs[1].assertValue(v1);
2309 +        } catch (CompletionException ok) {
2310 +            checkCompletedWithWrappedException(h1, ex);
2311 +            rs[1].assertNotInvoked();
2312 +        }
2313 +        try {
2314 +            assertEquals(null, h2.join());
2315 +            rs[2].assertValue(v1);
2316 +        } catch (CompletionException ok) {
2317 +            checkCompletedWithWrappedException(h2, ex);
2318 +            rs[2].assertNotInvoked();
2319 +        }
2320 +        try {
2321 +            assertEquals(null, h3.join());
2322 +            rs[3].assertValue(v1);
2323 +        } catch (CompletionException ok) {
2324 +            checkCompletedWithWrappedException(h3, ex);
2325 +            rs[3].assertNotInvoked();
2326 +        }
2327 +
2328 +        checkCompletedNormally(f, v1);
2329 +        checkCompletedExceptionally(g, ex);
2330 +    }}
2331 +
2332      /**
2333       * acceptEither result completes exceptionally if either source cancelled
2334       */
# Line 2095 | Line 2430 | public class CompletableFutureTest exten
2430       * runAfterEither result completes normally after normal completion
2431       * of either source
2432       */
2433 <    public void testRunAfterEither_normalCompletion1() {
2433 >    public void testRunAfterEither_normalCompletion() {
2434          for (ExecutionMode m : ExecutionMode.values())
2435          for (Integer v1 : new Integer[] { 1, null })
2436          for (Integer v2 : new Integer[] { 2, null })
2437      {
2438          final CompletableFuture<Integer> f = new CompletableFuture<>();
2439          final CompletableFuture<Integer> g = new CompletableFuture<>();
2440 <        final Noop r = new Noop(m);
2441 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2440 >        final Noop[] rs = new Noop[6];
2441 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2442  
2443 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2444 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2445 +        checkIncomplete(h0);
2446 +        checkIncomplete(h1);
2447 +        rs[0].assertNotInvoked();
2448 +        rs[1].assertNotInvoked();
2449          f.complete(v1);
2450 <        checkCompletedNormally(h, null);
2451 <        r.assertInvoked();
2452 <        g.complete(v2);
2453 <
2454 <        checkCompletedNormally(f, v1);
2455 <        checkCompletedNormally(g, v2);
2456 <        checkCompletedNormally(h, null);
2457 <        r.assertInvoked();
2458 <    }}
2459 <
2119 <    public void testRunAfterEither_normalCompletion2() {
2120 <        for (ExecutionMode m : ExecutionMode.values())
2121 <        for (Integer v1 : new Integer[] { 1, null })
2122 <        for (Integer v2 : new Integer[] { 2, null })
2123 <    {
2124 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2125 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2126 <        final Noop r = new Noop(m);
2127 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2450 >        checkCompletedNormally(h0, null);
2451 >        checkCompletedNormally(h1, null);
2452 >        rs[0].assertInvoked();
2453 >        rs[1].assertInvoked();
2454 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2455 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2456 >        checkCompletedNormally(h2, null);
2457 >        checkCompletedNormally(h3, null);
2458 >        rs[2].assertInvoked();
2459 >        rs[3].assertInvoked();
2460  
2461          g.complete(v2);
2130        checkCompletedNormally(h, null);
2131        r.assertInvoked();
2132        f.complete(v1);
2462  
2463 <        checkCompletedNormally(f, v1);
2464 <        checkCompletedNormally(g, v2);
2136 <        checkCompletedNormally(h, null);
2137 <        r.assertInvoked();
2138 <        }}
2139 <
2140 <    public void testRunAfterEither_normalCompletion3() {
2141 <        for (ExecutionMode m : ExecutionMode.values())
2142 <        for (Integer v1 : new Integer[] { 1, null })
2143 <        for (Integer v2 : new Integer[] { 2, null })
2144 <    {
2145 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2146 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2147 <        final Noop r = new Noop(m);
2148 <
2149 <        f.complete(v1);
2150 <        g.complete(v2);
2151 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2463 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2464 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2465  
2153        checkCompletedNormally(h, null);
2466          checkCompletedNormally(f, v1);
2467          checkCompletedNormally(g, v2);
2468 <        r.assertInvoked();
2468 >        checkCompletedNormally(h0, null);
2469 >        checkCompletedNormally(h1, null);
2470 >        checkCompletedNormally(h2, null);
2471 >        checkCompletedNormally(h3, null);
2472 >        checkCompletedNormally(h4, null);
2473 >        checkCompletedNormally(h5, null);
2474 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2475      }}
2476  
2477      /**
2478       * runAfterEither result completes exceptionally after exceptional
2479       * completion of either source
2480       */
2481 <    public void testRunAfterEither_exceptionalCompletion1() {
2481 >    public void testRunAfterEither_exceptionalCompletion() {
2482          for (ExecutionMode m : ExecutionMode.values())
2483          for (Integer v1 : new Integer[] { 1, null })
2484      {
2485          final CompletableFuture<Integer> f = new CompletableFuture<>();
2486          final CompletableFuture<Integer> g = new CompletableFuture<>();
2169        final Noop r = new Noop(m);
2170        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2487          final CFException ex = new CFException();
2488 +        final Noop[] rs = new Noop[6];
2489 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2490  
2491 <        f.completeExceptionally(ex);
2492 <        checkCompletedWithWrappedCFException(h, ex);
2493 <        g.complete(v1);
2494 <
2495 <        r.assertNotInvoked();
2496 <        checkCompletedNormally(g, v1);
2497 <        checkCompletedWithWrappedCFException(f, ex);
2498 <        checkCompletedWithWrappedCFException(h, ex);
2499 <    }}
2500 <
2501 <    public void testRunAfterEither_exceptionalCompletion2() {
2502 <        for (ExecutionMode m : ExecutionMode.values())
2503 <        for (Integer v1 : new Integer[] { 1, null })
2186 <    {
2187 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2188 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2189 <        final Noop r = new Noop(m);
2190 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2191 <        final CFException ex = new CFException();
2192 <
2193 <        g.completeExceptionally(ex);
2194 <        checkCompletedWithWrappedCFException(h, ex);
2195 <        f.complete(v1);
2196 <
2197 <        r.assertNotInvoked();
2198 <        checkCompletedNormally(f, v1);
2199 <        checkCompletedWithWrappedCFException(g, ex);
2200 <        checkCompletedWithWrappedCFException(h, ex);
2201 <    }}
2202 <
2203 <    public void testRunAfterEither_exceptionalCompletion3() {
2204 <        for (ExecutionMode m : ExecutionMode.values())
2205 <        for (Integer v1 : new Integer[] { 1, null })
2206 <    {
2207 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2208 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2209 <        final Noop r = new Noop(m);
2210 <        final CFException ex = new CFException();
2491 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2492 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2493 >        checkIncomplete(h0);
2494 >        checkIncomplete(h1);
2495 >        rs[0].assertNotInvoked();
2496 >        rs[1].assertNotInvoked();
2497 >        assertTrue(f.completeExceptionally(ex));
2498 >        checkCompletedWithWrappedException(h0, ex);
2499 >        checkCompletedWithWrappedException(h1, ex);
2500 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2501 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2502 >        checkCompletedWithWrappedException(h2, ex);
2503 >        checkCompletedWithWrappedException(h3, ex);
2504  
2505 <        g.completeExceptionally(ex);
2213 <        f.complete(v1);
2214 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2505 >        assertTrue(g.complete(v1));
2506  
2507 <        // unspecified behavior
2508 <        Integer v;
2507 >        // unspecified behavior - both source completions available
2508 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2509 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2510          try {
2511 <            assertNull(h.join());
2512 <            r.assertInvoked();
2511 >            assertNull(h4.join());
2512 >            rs[4].assertInvoked();
2513          } catch (CompletionException ok) {
2514 <            checkCompletedWithWrappedCFException(h, ex);
2515 <            r.assertNotInvoked();
2514 >            checkCompletedWithWrappedException(h4, ex);
2515 >            rs[4].assertNotInvoked();
2516          }
2225
2226        checkCompletedWithWrappedCFException(g, ex);
2227        checkCompletedNormally(f, v1);
2228    }}
2229
2230    public void testRunAfterEither_exceptionalCompletion4() {
2231        for (ExecutionMode m : ExecutionMode.values())
2232        for (Integer v1 : new Integer[] { 1, null })
2233    {
2234        final CompletableFuture<Integer> f = new CompletableFuture<>();
2235        final CompletableFuture<Integer> g = new CompletableFuture<>();
2236        final Noop r = new Noop(m);
2237        final CFException ex = new CFException();
2238
2239        f.completeExceptionally(ex);
2240        g.complete(v1);
2241        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2242
2243        // unspecified behavior
2244        Integer v;
2517          try {
2518 <            assertNull(h.join());
2519 <            r.assertInvoked();
2518 >            assertNull(h5.join());
2519 >            rs[5].assertInvoked();
2520          } catch (CompletionException ok) {
2521 <            checkCompletedWithWrappedCFException(h, ex);
2522 <            r.assertNotInvoked();
2521 >            checkCompletedWithWrappedException(h5, ex);
2522 >            rs[5].assertNotInvoked();
2523          }
2524  
2525 <        checkCompletedWithWrappedCFException(f, ex);
2525 >        checkCompletedExceptionally(f, ex);
2526          checkCompletedNormally(g, v1);
2527 +        checkCompletedWithWrappedException(h0, ex);
2528 +        checkCompletedWithWrappedException(h1, ex);
2529 +        checkCompletedWithWrappedException(h2, ex);
2530 +        checkCompletedWithWrappedException(h3, ex);
2531 +        checkCompletedWithWrappedException(h4, ex);
2532 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2533      }}
2534  
2535 <    /**
2258 <     * runAfterEither result completes exceptionally if action does
2259 <     */
2260 <    public void testRunAfterEither_actionFailed1() {
2535 >    public void testRunAfterEither_exceptionalCompletion2() {
2536          for (ExecutionMode m : ExecutionMode.values())
2537 +        for (boolean fFirst : new boolean[] { true, false })
2538          for (Integer v1 : new Integer[] { 1, null })
2263        for (Integer v2 : new Integer[] { 2, null })
2539      {
2540          final CompletableFuture<Integer> f = new CompletableFuture<>();
2541          final CompletableFuture<Integer> g = new CompletableFuture<>();
2542 <        final FailingRunnable r = new FailingRunnable(m);
2543 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2542 >        final CFException ex = new CFException();
2543 >        final Noop[] rs = new Noop[6];
2544 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2545  
2546 <        f.complete(v1);
2547 <        checkCompletedWithWrappedCFException(h);
2548 <        g.complete(v2);
2549 <        checkCompletedNormally(f, v1);
2550 <        checkCompletedNormally(g, v2);
2551 <    }}
2546 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2547 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2548 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2549 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2550 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2551 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2552  
2553 <    public void testRunAfterEither_actionFailed2() {
2554 <        for (ExecutionMode m : ExecutionMode.values())
2555 <        for (Integer v1 : new Integer[] { 1, null })
2556 <        for (Integer v2 : new Integer[] { 2, null })
2557 <    {
2558 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2559 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2560 <        final FailingRunnable r = new FailingRunnable(m);
2561 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2553 >        // unspecified behavior - both source completions available
2554 >        try {
2555 >            assertEquals(null, h0.join());
2556 >            rs[0].assertInvoked();
2557 >        } catch (CompletionException ok) {
2558 >            checkCompletedWithWrappedException(h0, ex);
2559 >            rs[0].assertNotInvoked();
2560 >        }
2561 >        try {
2562 >            assertEquals(null, h1.join());
2563 >            rs[1].assertInvoked();
2564 >        } catch (CompletionException ok) {
2565 >            checkCompletedWithWrappedException(h1, ex);
2566 >            rs[1].assertNotInvoked();
2567 >        }
2568 >        try {
2569 >            assertEquals(null, h2.join());
2570 >            rs[2].assertInvoked();
2571 >        } catch (CompletionException ok) {
2572 >            checkCompletedWithWrappedException(h2, ex);
2573 >            rs[2].assertNotInvoked();
2574 >        }
2575 >        try {
2576 >            assertEquals(null, h3.join());
2577 >            rs[3].assertInvoked();
2578 >        } catch (CompletionException ok) {
2579 >            checkCompletedWithWrappedException(h3, ex);
2580 >            rs[3].assertNotInvoked();
2581 >        }
2582  
2287        g.complete(v2);
2288        checkCompletedWithWrappedCFException(h);
2289        f.complete(v1);
2583          checkCompletedNormally(f, v1);
2584 <        checkCompletedNormally(g, v2);
2584 >        checkCompletedExceptionally(g, ex);
2585      }}
2586  
2587      /**
2588       * runAfterEither result completes exceptionally if either source cancelled
2589       */
2590 <    public void testRunAfterEither_sourceCancelled1() {
2590 >    public void testRunAfterEither_sourceCancelled() {
2591          for (ExecutionMode m : ExecutionMode.values())
2592          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2593          for (Integer v1 : new Integer[] { 1, null })
2594      {
2595          final CompletableFuture<Integer> f = new CompletableFuture<>();
2596          final CompletableFuture<Integer> g = new CompletableFuture<>();
2597 <        final Noop r = new Noop(m);
2598 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2306 <
2307 <        assertTrue(f.cancel(mayInterruptIfRunning));
2308 <        checkCompletedWithWrappedCancellationException(h);
2309 <        g.complete(v1);
2597 >        final Noop[] rs = new Noop[6];
2598 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2599  
2600 <        checkCancelled(f);
2601 <        r.assertNotInvoked();
2602 <        checkCompletedNormally(g, v1);
2603 <        checkCompletedWithWrappedCancellationException(h);
2604 <    }}
2605 <
2606 <    public void testRunAfterEither_sourceCancelled2() {
2607 <        for (ExecutionMode m : ExecutionMode.values())
2608 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2609 <        for (Integer v1 : new Integer[] { 1, null })
2610 <    {
2611 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2612 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2324 <        final Noop r = new Noop(m);
2325 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2326 <
2327 <        assertTrue(g.cancel(mayInterruptIfRunning));
2328 <        checkCompletedWithWrappedCancellationException(h);
2329 <        f.complete(v1);
2330 <
2331 <        checkCancelled(g);
2332 <        r.assertNotInvoked();
2333 <        checkCompletedNormally(f, v1);
2334 <        checkCompletedWithWrappedCancellationException(h);
2335 <    }}
2336 <
2337 <    public void testRunAfterEither_sourceCancelled3() {
2338 <        for (ExecutionMode m : ExecutionMode.values())
2339 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2340 <        for (Integer v1 : new Integer[] { 1, null })
2341 <    {
2342 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2343 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2344 <        final Noop r = new Noop(m);
2600 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2601 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2602 >        checkIncomplete(h0);
2603 >        checkIncomplete(h1);
2604 >        rs[0].assertNotInvoked();
2605 >        rs[1].assertNotInvoked();
2606 >        f.cancel(mayInterruptIfRunning);
2607 >        checkCompletedWithWrappedCancellationException(h0);
2608 >        checkCompletedWithWrappedCancellationException(h1);
2609 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2610 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2611 >        checkCompletedWithWrappedCancellationException(h2);
2612 >        checkCompletedWithWrappedCancellationException(h3);
2613  
2614 <        assertTrue(g.cancel(mayInterruptIfRunning));
2347 <        f.complete(v1);
2348 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2614 >        assertTrue(g.complete(v1));
2615  
2616 <        // unspecified behavior
2617 <        Integer v;
2616 >        // unspecified behavior - both source completions available
2617 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2618 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2619          try {
2620 <            assertNull(h.join());
2621 <            r.assertInvoked();
2620 >            assertNull(h4.join());
2621 >            rs[4].assertInvoked();
2622          } catch (CompletionException ok) {
2623 <            checkCompletedWithWrappedCancellationException(h);
2624 <            r.assertNotInvoked();
2623 >            checkCompletedWithWrappedCancellationException(h4);
2624 >            rs[4].assertNotInvoked();
2625 >        }
2626 >        try {
2627 >            assertNull(h5.join());
2628 >            rs[5].assertInvoked();
2629 >        } catch (CompletionException ok) {
2630 >            checkCompletedWithWrappedCancellationException(h5);
2631 >            rs[5].assertNotInvoked();
2632          }
2633  
2634 <        checkCancelled(g);
2635 <        checkCompletedNormally(f, v1);
2634 >        checkCancelled(f);
2635 >        checkCompletedNormally(g, v1);
2636 >        checkCompletedWithWrappedCancellationException(h0);
2637 >        checkCompletedWithWrappedCancellationException(h1);
2638 >        checkCompletedWithWrappedCancellationException(h2);
2639 >        checkCompletedWithWrappedCancellationException(h3);
2640 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2641      }}
2642  
2643 <    public void testRunAfterEither_sourceCancelled4() {
2643 >    /**
2644 >     * runAfterEither result completes exceptionally if action does
2645 >     */
2646 >    public void testRunAfterEither_actionFailed() {
2647          for (ExecutionMode m : ExecutionMode.values())
2366        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2648          for (Integer v1 : new Integer[] { 1, null })
2649 +        for (Integer v2 : new Integer[] { 2, null })
2650      {
2651          final CompletableFuture<Integer> f = new CompletableFuture<>();
2652          final CompletableFuture<Integer> g = new CompletableFuture<>();
2653 <        final Noop r = new Noop(m);
2653 >        final FailingRunnable[] rs = new FailingRunnable[6];
2654 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2655  
2656 <        assertTrue(f.cancel(mayInterruptIfRunning));
2657 <        g.complete(v1);
2658 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2659 <
2660 <        // unspecified behavior
2661 <        Integer v;
2662 <        try {
2663 <            assertNull(h.join());
2664 <            r.assertInvoked();
2665 <        } catch (CompletionException ok) {
2666 <            checkCompletedWithWrappedCancellationException(h);
2667 <            r.assertNotInvoked();
2668 <        }
2656 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2657 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2658 >        assertTrue(f.complete(v1));
2659 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2660 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2661 >        checkCompletedWithWrappedCFException(h0);
2662 >        checkCompletedWithWrappedCFException(h1);
2663 >        checkCompletedWithWrappedCFException(h2);
2664 >        checkCompletedWithWrappedCFException(h3);
2665 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2666 >        assertTrue(g.complete(v2));
2667 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2668 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2669 >        checkCompletedWithWrappedCFException(h4);
2670 >        checkCompletedWithWrappedCFException(h5);
2671  
2672 <        checkCancelled(f);
2673 <        checkCompletedNormally(g, v1);
2672 >        checkCompletedNormally(f, v1);
2673 >        checkCompletedNormally(g, v2);
2674 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2675      }}
2676  
2677      /**
# Line 2398 | Line 2684 | public class CompletableFutureTest exten
2684      {
2685          final CompletableFuture<Integer> f = new CompletableFuture<>();
2686          final CompletableFutureInc r = new CompletableFutureInc(m);
2687 <        if (!createIncomplete) f.complete(v1);
2687 >        if (!createIncomplete) assertTrue(f.complete(v1));
2688          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2689 <        if (createIncomplete) f.complete(v1);
2689 >        if (createIncomplete) assertTrue(f.complete(v1));
2690  
2691          checkCompletedNormally(g, inc(v1));
2692          checkCompletedNormally(f, v1);
2693 <        r.assertInvoked();
2693 >        r.assertValue(v1);
2694      }}
2695  
2696      /**
# Line 2422 | Line 2708 | public class CompletableFutureTest exten
2708          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2709          if (createIncomplete) f.completeExceptionally(ex);
2710  
2711 <        checkCompletedWithWrappedCFException(g, ex);
2712 <        checkCompletedWithWrappedCFException(f, ex);
2711 >        checkCompletedWithWrappedException(g, ex);
2712 >        checkCompletedExceptionally(f, ex);
2713          r.assertNotInvoked();
2714      }}
2715  
# Line 2438 | Line 2724 | public class CompletableFutureTest exten
2724          final CompletableFuture<Integer> f = new CompletableFuture<>();
2725          final FailingCompletableFutureFunction r
2726              = new FailingCompletableFutureFunction(m);
2727 <        if (!createIncomplete) f.complete(v1);
2727 >        if (!createIncomplete) assertTrue(f.complete(v1));
2728          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2729 <        if (createIncomplete) f.complete(v1);
2729 >        if (createIncomplete) assertTrue(f.complete(v1));
2730  
2731          checkCompletedWithWrappedCFException(g);
2732          checkCompletedNormally(f, v1);
# Line 2679 | Line 2965 | public class CompletableFutureTest exten
2965          assertSame(f, f.toCompletableFuture());
2966      }
2967  
2968 <    /**
2969 <     * whenComplete action executes on normal completion, propagating
2970 <     * source result.
2971 <     */
2972 <    public void testWhenComplete_normalCompletion1() {
2973 <        for (ExecutionMode m : ExecutionMode.values())
2974 <        for (boolean createIncomplete : new boolean[] { true, false })
2975 <        for (Integer v1 : new Integer[] { 1, null })
2976 <    {
2977 <        final AtomicInteger a = new AtomicInteger(0);
2978 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2979 <        if (!createIncomplete) f.complete(v1);
2980 <        final CompletableFuture<Integer> g = m.whenComplete
2981 <            (f,
2982 <             (Integer x, Throwable t) -> {
2983 <                threadAssertSame(x, v1);
2984 <                threadAssertNull(t);
2699 <                a.getAndIncrement();
2700 <            });
2701 <        if (createIncomplete) f.complete(v1);
2702 <
2703 <        checkCompletedNormally(g, v1);
2704 <        checkCompletedNormally(f, v1);
2705 <        assertEquals(1, a.get());
2706 <    }}
2707 <
2708 <    /**
2709 <     * whenComplete action executes on exceptional completion, propagating
2710 <     * source result.
2711 <     */
2712 <    public void testWhenComplete_exceptionalCompletion() {
2713 <        for (ExecutionMode m : ExecutionMode.values())
2714 <        for (boolean createIncomplete : new boolean[] { true, false })
2715 <        for (Integer v1 : new Integer[] { 1, null })
2716 <    {
2717 <        final AtomicInteger a = new AtomicInteger(0);
2718 <        final CFException ex = new CFException();
2719 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2720 <        if (!createIncomplete) f.completeExceptionally(ex);
2721 <        final CompletableFuture<Integer> g = m.whenComplete
2722 <            (f,
2723 <             (Integer x, Throwable t) -> {
2724 <                threadAssertNull(x);
2725 <                threadAssertSame(t, ex);
2726 <                a.getAndIncrement();
2727 <            });
2728 <        if (createIncomplete) f.completeExceptionally(ex);
2729 <        checkCompletedWithWrappedCFException(f, ex);
2730 <        checkCompletedWithWrappedCFException(g, ex);
2731 <        assertEquals(1, a.get());
2732 <    }}
2733 <
2734 <    /**
2735 <     * whenComplete action executes on cancelled source, propagating
2736 <     * CancellationException.
2737 <     */
2738 <    public void testWhenComplete_sourceCancelled() {
2739 <        for (ExecutionMode m : ExecutionMode.values())
2740 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2741 <        for (boolean createIncomplete : new boolean[] { true, false })
2742 <    {
2743 <        final AtomicInteger a = new AtomicInteger(0);
2744 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2745 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2746 <        final CompletableFuture<Integer> g = m.whenComplete
2747 <            (f,
2748 <             (Integer x, Throwable t) -> {
2749 <                threadAssertNull(x);
2750 <                threadAssertTrue(t instanceof CancellationException);
2751 <                a.getAndIncrement();
2752 <            });
2753 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2754 <
2755 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2756 <        checkCompletedWithWrappedCancellationException(g);
2757 <        checkCancelled(f);
2758 <        assertEquals(1, a.get());
2759 <    }}
2760 <
2761 <    /**
2762 <     * If a whenComplete action throws an exception when triggered by
2763 <     * a normal completion, it completes exceptionally
2764 <     */
2765 <    public void testWhenComplete_actionFailed() {
2766 <        for (boolean createIncomplete : new boolean[] { true, false })
2767 <        for (ExecutionMode m : ExecutionMode.values())
2768 <        for (Integer v1 : new Integer[] { 1, null })
2769 <    {
2770 <        final AtomicInteger a = new AtomicInteger(0);
2771 <        final CFException ex = new CFException();
2772 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2773 <        if (!createIncomplete) f.complete(v1);
2774 <        final CompletableFuture<Integer> g = m.whenComplete
2775 <            (f,
2776 <             (Integer x, Throwable t) -> {
2777 <                threadAssertSame(x, v1);
2778 <                threadAssertNull(t);
2779 <                a.getAndIncrement();
2780 <                throw ex;
2781 <            });
2782 <        if (createIncomplete) f.complete(v1);
2783 <        checkCompletedNormally(f, v1);
2784 <        checkCompletedWithWrappedCFException(g, ex);
2785 <        assertEquals(1, a.get());
2786 <    }}
2787 <
2788 <    /**
2789 <     * If a whenComplete action throws an exception when triggered by
2790 <     * a source completion that also throws an exception, the source
2791 <     * exception takes precedence.
2792 <     */
2793 <    public void testWhenComplete_actionFailedSourceFailed() {
2794 <        for (boolean createIncomplete : new boolean[] { true, false })
2795 <        for (ExecutionMode m : ExecutionMode.values())
2796 <        for (Integer v1 : new Integer[] { 1, null })
2797 <    {
2798 <        final AtomicInteger a = new AtomicInteger(0);
2799 <        final CFException ex1 = new CFException();
2800 <        final CFException ex2 = new CFException();
2801 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2802 <
2803 <        if (!createIncomplete) f.completeExceptionally(ex1);
2804 <        final CompletableFuture<Integer> g = m.whenComplete
2805 <            (f,
2806 <             (Integer x, Throwable t) -> {
2807 <                threadAssertSame(t, ex1);
2808 <                threadAssertNull(x);
2809 <                a.getAndIncrement();
2810 <                throw ex2;
2811 <            });
2812 <        if (createIncomplete) f.completeExceptionally(ex1);
2813 <
2814 <        checkCompletedWithWrappedCFException(f, ex1);
2815 <        checkCompletedWithWrappedCFException(g, ex1);
2816 <        assertEquals(1, a.get());
2817 <    }}
2968 > //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
2969 > //         for (ExecutionMode m : ExecutionMode.values())
2970 > //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2971 > //         for (Integer v1 : new Integer[] { 1, null })
2972 > //     {
2973 > //         final CompletableFuture<Integer> f = new CompletableFuture<>();
2974 > //         final CompletableFuture<Integer> g = new CompletableFuture<>();
2975 > //         final Noop[] rs = new Noop[2];
2976 > //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2977 > //         f.complete(v1);
2978 > //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2979 > //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2980 > //         assertTrue(g.cancel(mayInterruptIfRunning));
2981 > //         checkCompletedNormally(h0, null);
2982 > //         checkCompletedNormally(h1, null);
2983 > //         for (Noop r : rs) r.assertInvoked();
2984 > //     }}
2985  
2986   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines