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.68 by jsr166, Fri Jun 6 19:35:54 2014 UTC vs.
Revision 1.79 by jsr166, Mon Jun 16 17:08:15 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 1334 | Line 1529 | public class CompletableFutureTest exten
1529       */
1530      public void testThenCombine_normalCompletion() {
1531          for (ExecutionMode m : ExecutionMode.values())
1337        for (boolean createIncomplete : new boolean[] { true, false })
1532          for (boolean fFirst : new boolean[] { true, false })
1533          for (Integer v1 : new Integer[] { 1, null })
1534          for (Integer v2 : new Integer[] { 2, null })
1535      {
1536          final CompletableFuture<Integer> f = new CompletableFuture<>();
1537          final CompletableFuture<Integer> g = new CompletableFuture<>();
1538 <        final SubtractFunction r = new SubtractFunction(m);
1539 <
1540 <        if (fFirst) f.complete(v1); else g.complete(v2);
1541 <        if (!createIncomplete)
1542 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1543 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1544 <        if (createIncomplete) {
1545 <            checkIncomplete(h);
1546 <            r.assertNotInvoked();
1547 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1548 <        }
1549 <
1550 <        checkCompletedNormally(h, subtract(v1, v2));
1538 >        final SubtractFunction r1 = new SubtractFunction(m);
1539 >        final SubtractFunction r2 = new SubtractFunction(m);
1540 >        final SubtractFunction r3 = new SubtractFunction(m);
1541 >
1542 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1543 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1544 >        final Integer w1 =  fFirst ? v1 : v2;
1545 >        final Integer w2 = !fFirst ? v1 : v2;
1546 >
1547 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1548 >        assertTrue(fst.complete(w1));
1549 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1550 >        checkIncomplete(h1);
1551 >        checkIncomplete(h2);
1552 >        r1.assertNotInvoked();
1553 >        r2.assertNotInvoked();
1554 >        assertTrue(snd.complete(w2));
1555 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1556 >
1557 >        checkCompletedNormally(h1, subtract(v1, v2));
1558 >        checkCompletedNormally(h2, subtract(v1, v2));
1559 >        checkCompletedNormally(h3, subtract(v1, v2));
1560 >        r1.assertValue(subtract(v1, v2));
1561 >        r2.assertValue(subtract(v1, v2));
1562 >        r3.assertValue(subtract(v1, v2));
1563          checkCompletedNormally(f, v1);
1564          checkCompletedNormally(g, v2);
1359        r.assertInvoked();
1565      }}
1566  
1567      /**
1568       * thenCombine result completes exceptionally after exceptional
1569       * completion of either source
1570       */
1571 <    public void testThenCombine_exceptionalCompletion() {
1571 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1572          for (ExecutionMode m : ExecutionMode.values())
1368        for (boolean createIncomplete : new boolean[] { true, false })
1573          for (boolean fFirst : new boolean[] { true, false })
1574 +        for (boolean failFirst : new boolean[] { true, false })
1575          for (Integer v1 : new Integer[] { 1, null })
1576      {
1577          final CompletableFuture<Integer> f = new CompletableFuture<>();
1578          final CompletableFuture<Integer> g = new CompletableFuture<>();
1579          final CFException ex = new CFException();
1580 <        final SubtractFunction r = new SubtractFunction(m);
1581 <
1582 <        (fFirst ? f : g).complete(v1);
1583 <        if (!createIncomplete)
1584 <            (!fFirst ? f : g).completeExceptionally(ex);
1585 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1586 <        if (createIncomplete) {
1587 <            checkIncomplete(h);
1588 <            (!fFirst ? f : g).completeExceptionally(ex);
1589 <        }
1590 <
1591 <        checkCompletedWithWrappedCFException(h, ex);
1592 <        r.assertNotInvoked();
1593 <        checkCompletedNormally(fFirst ? f : g, v1);
1594 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1580 >        final SubtractFunction r1 = new SubtractFunction(m);
1581 >        final SubtractFunction r2 = new SubtractFunction(m);
1582 >        final SubtractFunction r3 = new SubtractFunction(m);
1583 >
1584 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1585 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1586 >        final Callable<Boolean> complete1 = failFirst ?
1587 >            () -> fst.completeExceptionally(ex) :
1588 >            () -> fst.complete(v1);
1589 >        final Callable<Boolean> complete2 = failFirst ?
1590 >            () -> snd.complete(v1) :
1591 >            () -> snd.completeExceptionally(ex);
1592 >
1593 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1594 >        assertTrue(complete1.call());
1595 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1596 >        checkIncomplete(h1);
1597 >        checkIncomplete(h2);
1598 >        assertTrue(complete2.call());
1599 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1600 >
1601 >        checkCompletedWithWrappedException(h1, ex);
1602 >        checkCompletedWithWrappedException(h2, ex);
1603 >        checkCompletedWithWrappedException(h3, ex);
1604 >        r1.assertNotInvoked();
1605 >        r2.assertNotInvoked();
1606 >        r3.assertNotInvoked();
1607 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1608 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1609      }}
1610  
1611      /**
1612       * thenCombine result completes exceptionally if either source cancelled
1613       */
1614 <    public void testThenCombine_sourceCancelled() {
1614 >    public void testThenCombine_sourceCancelled() throws Throwable {
1615          for (ExecutionMode m : ExecutionMode.values())
1616          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1398        for (boolean createIncomplete : new boolean[] { true, false })
1617          for (boolean fFirst : new boolean[] { true, false })
1618 +        for (boolean failFirst : new boolean[] { true, false })
1619          for (Integer v1 : new Integer[] { 1, null })
1620      {
1621          final CompletableFuture<Integer> f = new CompletableFuture<>();
1622          final CompletableFuture<Integer> g = new CompletableFuture<>();
1623 <        final SubtractFunction r = new SubtractFunction(m);
1623 >        final SubtractFunction r1 = new SubtractFunction(m);
1624 >        final SubtractFunction r2 = new SubtractFunction(m);
1625 >        final SubtractFunction r3 = new SubtractFunction(m);
1626  
1627 <        (fFirst ? f : g).complete(v1);
1628 <        if (!createIncomplete)
1629 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1630 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1631 <        if (createIncomplete) {
1632 <            checkIncomplete(h);
1633 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1634 <        }
1627 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1628 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1629 >        final Callable<Boolean> complete1 = failFirst ?
1630 >            () -> fst.cancel(mayInterruptIfRunning) :
1631 >            () -> fst.complete(v1);
1632 >        final Callable<Boolean> complete2 = failFirst ?
1633 >            () -> snd.complete(v1) :
1634 >            () -> snd.cancel(mayInterruptIfRunning);
1635  
1636 <        checkCompletedWithWrappedCancellationException(h);
1637 <        checkCancelled(!fFirst ? f : g);
1638 <        r.assertNotInvoked();
1639 <        checkCompletedNormally(fFirst ? f : g, v1);
1636 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1637 >        assertTrue(complete1.call());
1638 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1639 >        checkIncomplete(h1);
1640 >        checkIncomplete(h2);
1641 >        assertTrue(complete2.call());
1642 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1643 >
1644 >        checkCompletedWithWrappedCancellationException(h1);
1645 >        checkCompletedWithWrappedCancellationException(h2);
1646 >        checkCompletedWithWrappedCancellationException(h3);
1647 >        r1.assertNotInvoked();
1648 >        r2.assertNotInvoked();
1649 >        r3.assertNotInvoked();
1650 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1651 >        checkCancelled(failFirst ? fst : snd);
1652      }}
1653  
1654      /**
# Line 1429 | Line 1662 | public class CompletableFutureTest exten
1662      {
1663          final CompletableFuture<Integer> f = new CompletableFuture<>();
1664          final CompletableFuture<Integer> g = new CompletableFuture<>();
1665 <        final FailingBiFunction r = new FailingBiFunction(m);
1666 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1667 <
1668 <        if (fFirst) {
1669 <            f.complete(v1);
1670 <            g.complete(v2);
1671 <        } else {
1672 <            g.complete(v2);
1673 <            f.complete(v1);
1674 <        }
1665 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1666 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1667 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1668 >
1669 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1670 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1671 >        final Integer w1 =  fFirst ? v1 : v2;
1672 >        final Integer w2 = !fFirst ? v1 : v2;
1673 >
1674 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1675 >        assertTrue(fst.complete(w1));
1676 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1677 >        assertTrue(snd.complete(w2));
1678 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1679  
1680 <        checkCompletedWithWrappedCFException(h);
1680 >        checkCompletedWithWrappedCFException(h1);
1681 >        checkCompletedWithWrappedCFException(h2);
1682 >        checkCompletedWithWrappedCFException(h3);
1683          checkCompletedNormally(f, v1);
1684          checkCompletedNormally(g, v2);
1685      }}
# Line 1460 | Line 1699 | public class CompletableFutureTest exten
1699          final CompletableFuture<Integer> g = new CompletableFuture<>();
1700          final SubtractAction r = new SubtractAction(m);
1701  
1702 <        if (fFirst) f.complete(v1); else g.complete(v2);
1702 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1703          if (!createIncomplete)
1704 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1704 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1705          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1706          if (createIncomplete) {
1707              checkIncomplete(h);
1708              r.assertNotInvoked();
1709 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1709 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1710          }
1711  
1712          checkCompletedNormally(h, null);
# Line 1491 | Line 1730 | public class CompletableFutureTest exten
1730          final CFException ex = new CFException();
1731          final SubtractAction r = new SubtractAction(m);
1732  
1733 <        (fFirst ? f : g).complete(v1);
1733 >        assertTrue((fFirst ? f : g).complete(v1));
1734          if (!createIncomplete)
1735 <            (!fFirst ? f : g).completeExceptionally(ex);
1735 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1736          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1737          if (createIncomplete) {
1738              checkIncomplete(h);
1739 <            (!fFirst ? f : g).completeExceptionally(ex);
1739 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1740          }
1741  
1742 <        checkCompletedWithWrappedCFException(h, ex);
1742 >        checkCompletedWithWrappedException(h, ex);
1743          r.assertNotInvoked();
1744          checkCompletedNormally(fFirst ? f : g, v1);
1745 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1745 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1746      }}
1747  
1748      /**
# Line 1520 | Line 1759 | public class CompletableFutureTest exten
1759          final CompletableFuture<Integer> g = new CompletableFuture<>();
1760          final SubtractAction r = new SubtractAction(m);
1761  
1762 <        (fFirst ? f : g).complete(v1);
1762 >        assertTrue((fFirst ? f : g).complete(v1));
1763          if (!createIncomplete)
1764              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1765          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
# Line 1549 | Line 1788 | public class CompletableFutureTest exten
1788          final FailingBiConsumer r = new FailingBiConsumer(m);
1789          final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1790  
1791 <        if (fFirst) {
1792 <            f.complete(v1);
1554 <            g.complete(v2);
1555 <        } else {
1556 <            g.complete(v2);
1557 <            f.complete(v1);
1558 <        }
1791 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1792 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1793  
1794          checkCompletedWithWrappedCFException(h);
1795          checkCompletedNormally(f, v1);
# Line 1577 | Line 1811 | public class CompletableFutureTest exten
1811          final CompletableFuture<Integer> g = new CompletableFuture<>();
1812          final Noop r = new Noop(m);
1813  
1814 <        if (fFirst) f.complete(v1); else g.complete(v2);
1814 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1815          if (!createIncomplete)
1816 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1816 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1817          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1818          if (createIncomplete) {
1819              checkIncomplete(h);
1820              r.assertNotInvoked();
1821 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1821 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1822          }
1823  
1824          checkCompletedNormally(h, null);
# Line 1608 | Line 1842 | public class CompletableFutureTest exten
1842          final CFException ex = new CFException();
1843          final Noop r = new Noop(m);
1844  
1845 <        (fFirst ? f : g).complete(v1);
1845 >        assertTrue((fFirst ? f : g).complete(v1));
1846          if (!createIncomplete)
1847 <            (!fFirst ? f : g).completeExceptionally(ex);
1847 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1848          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1849          if (createIncomplete) {
1850              checkIncomplete(h);
1851 <            (!fFirst ? f : g).completeExceptionally(ex);
1851 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1852          }
1853  
1854 <        checkCompletedWithWrappedCFException(h, ex);
1854 >        checkCompletedWithWrappedException(h, ex);
1855          r.assertNotInvoked();
1856          checkCompletedNormally(fFirst ? f : g, v1);
1857 <        checkCompletedWithWrappedCFException(!fFirst ? f : g, ex);
1857 >        checkCompletedExceptionally(!fFirst ? f : g, ex);
1858      }}
1859  
1860      /**
# Line 1637 | Line 1871 | public class CompletableFutureTest exten
1871          final CompletableFuture<Integer> g = new CompletableFuture<>();
1872          final Noop r = new Noop(m);
1873  
1874 <
1641 <        (fFirst ? f : g).complete(v1);
1874 >        assertTrue((fFirst ? f : g).complete(v1));
1875          if (!createIncomplete)
1876              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1877          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
# Line 1668 | Line 1901 | public class CompletableFutureTest exten
1901          final FailingRunnable r2 = new FailingRunnable(m);
1902  
1903          CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1904 <        if (fFirst) {
1905 <            f.complete(v1);
1673 <            g.complete(v2);
1674 <        } else {
1675 <            g.complete(v2);
1676 <            f.complete(v1);
1677 <        }
1904 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1905 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1906          CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1907  
1908          checkCompletedWithWrappedCFException(h1);
# Line 1752 | Line 1980 | public class CompletableFutureTest exten
1980          rs[0].assertNotInvoked();
1981          rs[1].assertNotInvoked();
1982          f.completeExceptionally(ex);
1983 <        checkCompletedWithWrappedCFException(h0, ex);
1984 <        checkCompletedWithWrappedCFException(h1, ex);
1983 >        checkCompletedWithWrappedException(h0, ex);
1984 >        checkCompletedWithWrappedException(h1, ex);
1985          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1986          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1987 <        checkCompletedWithWrappedCFException(h2, ex);
1988 <        checkCompletedWithWrappedCFException(h3, ex);
1987 >        checkCompletedWithWrappedException(h2, ex);
1988 >        checkCompletedWithWrappedException(h3, ex);
1989          g.complete(v1);
1990  
1991          // unspecified behavior - both source completions available
# Line 1767 | Line 1995 | public class CompletableFutureTest exten
1995              assertEquals(inc(v1), h4.join());
1996              rs[4].assertValue(inc(v1));
1997          } catch (CompletionException ok) {
1998 <            checkCompletedWithWrappedCFException(h4, ex);
1998 >            checkCompletedWithWrappedException(h4, ex);
1999              rs[4].assertNotInvoked();
2000          }
2001          try {
2002              assertEquals(inc(v1), h5.join());
2003              rs[5].assertValue(inc(v1));
2004          } catch (CompletionException ok) {
2005 <            checkCompletedWithWrappedCFException(h5, ex);
2005 >            checkCompletedWithWrappedException(h5, ex);
2006              rs[5].assertNotInvoked();
2007          }
2008  
2009 <        checkCompletedWithWrappedCFException(f, ex);
2009 >        checkCompletedExceptionally(f, ex);
2010          checkCompletedNormally(g, v1);
2011 <        checkCompletedWithWrappedCFException(h0, ex);
2012 <        checkCompletedWithWrappedCFException(h1, ex);
2013 <        checkCompletedWithWrappedCFException(h2, ex);
2014 <        checkCompletedWithWrappedCFException(h3, ex);
2015 <        checkCompletedWithWrappedCFException(h4, ex);
2011 >        checkCompletedWithWrappedException(h0, ex);
2012 >        checkCompletedWithWrappedException(h1, ex);
2013 >        checkCompletedWithWrappedException(h2, ex);
2014 >        checkCompletedWithWrappedException(h3, ex);
2015 >        checkCompletedWithWrappedException(h4, ex);
2016          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2017      }}
2018  
# Line 1801 | Line 2029 | public class CompletableFutureTest exten
2029  
2030          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2031          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2032 <        if (fFirst) {
2033 <            f.complete(v1);
1806 <            g.completeExceptionally(ex);
1807 <        } else {
1808 <            g.completeExceptionally(ex);
1809 <            f.complete(v1);
1810 <        }
2032 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2033 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2034          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2035          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2036  
# Line 1816 | Line 2039 | public class CompletableFutureTest exten
2039              assertEquals(inc(v1), h0.join());
2040              rs[0].assertValue(inc(v1));
2041          } catch (CompletionException ok) {
2042 <            checkCompletedWithWrappedCFException(h0, ex);
2042 >            checkCompletedWithWrappedException(h0, ex);
2043              rs[0].assertNotInvoked();
2044          }
2045          try {
2046              assertEquals(inc(v1), h1.join());
2047              rs[1].assertValue(inc(v1));
2048          } catch (CompletionException ok) {
2049 <            checkCompletedWithWrappedCFException(h1, ex);
2049 >            checkCompletedWithWrappedException(h1, ex);
2050              rs[1].assertNotInvoked();
2051          }
2052          try {
2053              assertEquals(inc(v1), h2.join());
2054              rs[2].assertValue(inc(v1));
2055          } catch (CompletionException ok) {
2056 <            checkCompletedWithWrappedCFException(h2, ex);
2056 >            checkCompletedWithWrappedException(h2, ex);
2057              rs[2].assertNotInvoked();
2058          }
2059          try {
2060              assertEquals(inc(v1), h3.join());
2061              rs[3].assertValue(inc(v1));
2062          } catch (CompletionException ok) {
2063 <            checkCompletedWithWrappedCFException(h3, ex);
2063 >            checkCompletedWithWrappedException(h3, ex);
2064              rs[3].assertNotInvoked();
2065          }
2066  
2067          checkCompletedNormally(f, v1);
2068 <        checkCompletedWithWrappedCFException(g, ex);
2068 >        checkCompletedExceptionally(g, ex);
2069      }}
2070  
2071      /**
# Line 1913 | Line 2136 | public class CompletableFutureTest exten
2136  
2137          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2138          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2139 <        if (fFirst) {
2140 <            f.complete(v1);
1918 <            g.cancel(mayInterruptIfRunning);
1919 <        } else {
1920 <            g.cancel(mayInterruptIfRunning);
1921 <            f.complete(v1);
1922 <        }
2139 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2140 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2141          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2142          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2143  
# Line 2071 | Line 2289 | public class CompletableFutureTest exten
2289          rs[0].assertNotInvoked();
2290          rs[1].assertNotInvoked();
2291          f.completeExceptionally(ex);
2292 <        checkCompletedWithWrappedCFException(h0, ex);
2293 <        checkCompletedWithWrappedCFException(h1, ex);
2292 >        checkCompletedWithWrappedException(h0, ex);
2293 >        checkCompletedWithWrappedException(h1, ex);
2294          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2295          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2296 <        checkCompletedWithWrappedCFException(h2, ex);
2297 <        checkCompletedWithWrappedCFException(h3, ex);
2296 >        checkCompletedWithWrappedException(h2, ex);
2297 >        checkCompletedWithWrappedException(h3, ex);
2298  
2299          g.complete(v1);
2300  
# Line 2087 | Line 2305 | public class CompletableFutureTest exten
2305              assertNull(h4.join());
2306              rs[4].assertValue(v1);
2307          } catch (CompletionException ok) {
2308 <            checkCompletedWithWrappedCFException(h4, ex);
2308 >            checkCompletedWithWrappedException(h4, ex);
2309              rs[4].assertNotInvoked();
2310          }
2311          try {
2312              assertNull(h5.join());
2313              rs[5].assertValue(v1);
2314          } catch (CompletionException ok) {
2315 <            checkCompletedWithWrappedCFException(h5, ex);
2315 >            checkCompletedWithWrappedException(h5, ex);
2316              rs[5].assertNotInvoked();
2317          }
2318  
2319 <        checkCompletedWithWrappedCFException(f, ex);
2319 >        checkCompletedExceptionally(f, ex);
2320          checkCompletedNormally(g, v1);
2321 <        checkCompletedWithWrappedCFException(h0, ex);
2322 <        checkCompletedWithWrappedCFException(h1, ex);
2323 <        checkCompletedWithWrappedCFException(h2, ex);
2324 <        checkCompletedWithWrappedCFException(h3, ex);
2325 <        checkCompletedWithWrappedCFException(h4, ex);
2321 >        checkCompletedWithWrappedException(h0, ex);
2322 >        checkCompletedWithWrappedException(h1, ex);
2323 >        checkCompletedWithWrappedException(h2, ex);
2324 >        checkCompletedWithWrappedException(h3, ex);
2325 >        checkCompletedWithWrappedException(h4, ex);
2326          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2327      }}
2328  
# Line 2121 | Line 2339 | public class CompletableFutureTest exten
2339  
2340          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2341          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2342 <        if (fFirst) {
2343 <            f.complete(v1);
2126 <            g.completeExceptionally(ex);
2127 <        } else {
2128 <            g.completeExceptionally(ex);
2129 <            f.complete(v1);
2130 <        }
2342 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2343 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2344          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2345          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2346  
# Line 2136 | Line 2349 | public class CompletableFutureTest exten
2349              assertEquals(null, h0.join());
2350              rs[0].assertValue(v1);
2351          } catch (CompletionException ok) {
2352 <            checkCompletedWithWrappedCFException(h0, ex);
2352 >            checkCompletedWithWrappedException(h0, ex);
2353              rs[0].assertNotInvoked();
2354          }
2355          try {
2356              assertEquals(null, h1.join());
2357              rs[1].assertValue(v1);
2358          } catch (CompletionException ok) {
2359 <            checkCompletedWithWrappedCFException(h1, ex);
2359 >            checkCompletedWithWrappedException(h1, ex);
2360              rs[1].assertNotInvoked();
2361          }
2362          try {
2363              assertEquals(null, h2.join());
2364              rs[2].assertValue(v1);
2365          } catch (CompletionException ok) {
2366 <            checkCompletedWithWrappedCFException(h2, ex);
2366 >            checkCompletedWithWrappedException(h2, ex);
2367              rs[2].assertNotInvoked();
2368          }
2369          try {
2370              assertEquals(null, h3.join());
2371              rs[3].assertValue(v1);
2372          } catch (CompletionException ok) {
2373 <            checkCompletedWithWrappedCFException(h3, ex);
2373 >            checkCompletedWithWrappedException(h3, ex);
2374              rs[3].assertNotInvoked();
2375          }
2376  
2377          checkCompletedNormally(f, v1);
2378 <        checkCompletedWithWrappedCFException(g, ex);
2378 >        checkCompletedExceptionally(g, ex);
2379      }}
2380  
2381      /**
# Line 2330 | Line 2543 | public class CompletableFutureTest exten
2543          checkIncomplete(h1);
2544          rs[0].assertNotInvoked();
2545          rs[1].assertNotInvoked();
2546 <        f.completeExceptionally(ex);
2547 <        checkCompletedWithWrappedCFException(h0, ex);
2548 <        checkCompletedWithWrappedCFException(h1, ex);
2546 >        assertTrue(f.completeExceptionally(ex));
2547 >        checkCompletedWithWrappedException(h0, ex);
2548 >        checkCompletedWithWrappedException(h1, ex);
2549          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2550          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2551 <        checkCompletedWithWrappedCFException(h2, ex);
2552 <        checkCompletedWithWrappedCFException(h3, ex);
2551 >        checkCompletedWithWrappedException(h2, ex);
2552 >        checkCompletedWithWrappedException(h3, ex);
2553  
2554 <        g.complete(v1);
2554 >        assertTrue(g.complete(v1));
2555  
2556          // unspecified behavior - both source completions available
2557          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2347 | Line 2560 | public class CompletableFutureTest exten
2560              assertNull(h4.join());
2561              rs[4].assertInvoked();
2562          } catch (CompletionException ok) {
2563 <            checkCompletedWithWrappedCFException(h4, ex);
2563 >            checkCompletedWithWrappedException(h4, ex);
2564              rs[4].assertNotInvoked();
2565          }
2566          try {
2567              assertNull(h5.join());
2568              rs[5].assertInvoked();
2569          } catch (CompletionException ok) {
2570 <            checkCompletedWithWrappedCFException(h5, ex);
2570 >            checkCompletedWithWrappedException(h5, ex);
2571              rs[5].assertNotInvoked();
2572          }
2573  
2574 <        checkCompletedWithWrappedCFException(f, ex);
2574 >        checkCompletedExceptionally(f, ex);
2575          checkCompletedNormally(g, v1);
2576 <        checkCompletedWithWrappedCFException(h0, ex);
2577 <        checkCompletedWithWrappedCFException(h1, ex);
2578 <        checkCompletedWithWrappedCFException(h2, ex);
2579 <        checkCompletedWithWrappedCFException(h3, ex);
2580 <        checkCompletedWithWrappedCFException(h4, ex);
2576 >        checkCompletedWithWrappedException(h0, ex);
2577 >        checkCompletedWithWrappedException(h1, ex);
2578 >        checkCompletedWithWrappedException(h2, ex);
2579 >        checkCompletedWithWrappedException(h3, ex);
2580 >        checkCompletedWithWrappedException(h4, ex);
2581          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2582      }}
2583  
2584 +    public void testRunAfterEither_exceptionalCompletion2() {
2585 +        for (ExecutionMode m : ExecutionMode.values())
2586 +        for (boolean fFirst : new boolean[] { true, false })
2587 +        for (Integer v1 : new Integer[] { 1, null })
2588 +    {
2589 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2590 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2591 +        final CFException ex = new CFException();
2592 +        final Noop[] rs = new Noop[6];
2593 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2594 +
2595 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2596 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2597 +        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2598 +        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2599 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2600 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2601 +
2602 +        // unspecified behavior - both source completions available
2603 +        try {
2604 +            assertEquals(null, h0.join());
2605 +            rs[0].assertInvoked();
2606 +        } catch (CompletionException ok) {
2607 +            checkCompletedWithWrappedException(h0, ex);
2608 +            rs[0].assertNotInvoked();
2609 +        }
2610 +        try {
2611 +            assertEquals(null, h1.join());
2612 +            rs[1].assertInvoked();
2613 +        } catch (CompletionException ok) {
2614 +            checkCompletedWithWrappedException(h1, ex);
2615 +            rs[1].assertNotInvoked();
2616 +        }
2617 +        try {
2618 +            assertEquals(null, h2.join());
2619 +            rs[2].assertInvoked();
2620 +        } catch (CompletionException ok) {
2621 +            checkCompletedWithWrappedException(h2, ex);
2622 +            rs[2].assertNotInvoked();
2623 +        }
2624 +        try {
2625 +            assertEquals(null, h3.join());
2626 +            rs[3].assertInvoked();
2627 +        } catch (CompletionException ok) {
2628 +            checkCompletedWithWrappedException(h3, ex);
2629 +            rs[3].assertNotInvoked();
2630 +        }
2631 +
2632 +        checkCompletedNormally(f, v1);
2633 +        checkCompletedExceptionally(g, ex);
2634 +    }}
2635 +
2636      /**
2637       * runAfterEither result completes exceptionally if either source cancelled
2638       */
# Line 2395 | Line 2660 | public class CompletableFutureTest exten
2660          checkCompletedWithWrappedCancellationException(h2);
2661          checkCompletedWithWrappedCancellationException(h3);
2662  
2663 <        g.complete(v1);
2663 >        assertTrue(g.complete(v1));
2664  
2665          // unspecified behavior - both source completions available
2666          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2439 | Line 2704 | public class CompletableFutureTest exten
2704  
2705          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2706          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2707 <        f.complete(v1);
2707 >        assertTrue(f.complete(v1));
2708          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2709          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2710          checkCompletedWithWrappedCFException(h0);
# Line 2447 | Line 2712 | public class CompletableFutureTest exten
2712          checkCompletedWithWrappedCFException(h2);
2713          checkCompletedWithWrappedCFException(h3);
2714          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2715 <        g.complete(v2);
2715 >        assertTrue(g.complete(v2));
2716          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2717          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2718          checkCompletedWithWrappedCFException(h4);
# Line 2468 | Line 2733 | public class CompletableFutureTest exten
2733      {
2734          final CompletableFuture<Integer> f = new CompletableFuture<>();
2735          final CompletableFutureInc r = new CompletableFutureInc(m);
2736 <        if (!createIncomplete) f.complete(v1);
2736 >        if (!createIncomplete) assertTrue(f.complete(v1));
2737          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2738 <        if (createIncomplete) f.complete(v1);
2738 >        if (createIncomplete) assertTrue(f.complete(v1));
2739  
2740          checkCompletedNormally(g, inc(v1));
2741          checkCompletedNormally(f, v1);
2742 <        r.assertInvoked();
2742 >        r.assertValue(v1);
2743      }}
2744  
2745      /**
# Line 2492 | Line 2757 | public class CompletableFutureTest exten
2757          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2758          if (createIncomplete) f.completeExceptionally(ex);
2759  
2760 <        checkCompletedWithWrappedCFException(g, ex);
2761 <        checkCompletedWithWrappedCFException(f, ex);
2760 >        checkCompletedWithWrappedException(g, ex);
2761 >        checkCompletedExceptionally(f, ex);
2762          r.assertNotInvoked();
2763      }}
2764  
# Line 2508 | Line 2773 | public class CompletableFutureTest exten
2773          final CompletableFuture<Integer> f = new CompletableFuture<>();
2774          final FailingCompletableFutureFunction r
2775              = new FailingCompletableFutureFunction(m);
2776 <        if (!createIncomplete) f.complete(v1);
2776 >        if (!createIncomplete) assertTrue(f.complete(v1));
2777          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2778 <        if (createIncomplete) f.complete(v1);
2778 >        if (createIncomplete) assertTrue(f.complete(v1));
2779  
2780          checkCompletedWithWrappedCFException(g);
2781          checkCompletedNormally(f, v1);
# Line 2749 | Line 3014 | public class CompletableFutureTest exten
3014          assertSame(f, f.toCompletableFuture());
3015      }
3016  
3017 <    /**
3018 <     * whenComplete action executes on normal completion, propagating
3019 <     * source result.
3020 <     */
3021 <    public void testWhenComplete_normalCompletion1() {
3022 <        for (ExecutionMode m : ExecutionMode.values())
3023 <        for (boolean createIncomplete : new boolean[] { true, false })
3024 <        for (Integer v1 : new Integer[] { 1, null })
3025 <    {
3026 <        final AtomicInteger a = new AtomicInteger(0);
3027 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
3028 <        if (!createIncomplete) f.complete(v1);
3029 <        final CompletableFuture<Integer> g = m.whenComplete
3030 <            (f,
3031 <             (Integer x, Throwable t) -> {
3032 <                threadAssertSame(x, v1);
3033 <                threadAssertNull(t);
2769 <                a.getAndIncrement();
2770 <            });
2771 <        if (createIncomplete) f.complete(v1);
2772 <
2773 <        checkCompletedNormally(g, v1);
2774 <        checkCompletedNormally(f, v1);
2775 <        assertEquals(1, a.get());
2776 <    }}
2777 <
2778 <    /**
2779 <     * whenComplete action executes on exceptional completion, propagating
2780 <     * source result.
2781 <     */
2782 <    public void testWhenComplete_exceptionalCompletion() {
2783 <        for (ExecutionMode m : ExecutionMode.values())
2784 <        for (boolean createIncomplete : new boolean[] { true, false })
2785 <        for (Integer v1 : new Integer[] { 1, null })
2786 <    {
2787 <        final AtomicInteger a = new AtomicInteger(0);
2788 <        final CFException ex = new CFException();
2789 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2790 <        if (!createIncomplete) f.completeExceptionally(ex);
2791 <        final CompletableFuture<Integer> g = m.whenComplete
2792 <            (f,
2793 <             (Integer x, Throwable t) -> {
2794 <                threadAssertNull(x);
2795 <                threadAssertSame(t, ex);
2796 <                a.getAndIncrement();
2797 <            });
2798 <        if (createIncomplete) f.completeExceptionally(ex);
2799 <        checkCompletedWithWrappedCFException(f, ex);
2800 <        checkCompletedWithWrappedCFException(g, ex);
2801 <        assertEquals(1, a.get());
2802 <    }}
2803 <
2804 <    /**
2805 <     * whenComplete action executes on cancelled source, propagating
2806 <     * CancellationException.
2807 <     */
2808 <    public void testWhenComplete_sourceCancelled() {
2809 <        for (ExecutionMode m : ExecutionMode.values())
2810 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2811 <        for (boolean createIncomplete : new boolean[] { true, false })
2812 <    {
2813 <        final AtomicInteger a = new AtomicInteger(0);
2814 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2815 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2816 <        final CompletableFuture<Integer> g = m.whenComplete
2817 <            (f,
2818 <             (Integer x, Throwable t) -> {
2819 <                threadAssertNull(x);
2820 <                threadAssertTrue(t instanceof CancellationException);
2821 <                a.getAndIncrement();
2822 <            });
2823 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2824 <
2825 <        //try { g.join(); } catch (Throwable t) { throw new Error(t); }
2826 <        checkCompletedWithWrappedCancellationException(g);
2827 <        checkCancelled(f);
2828 <        assertEquals(1, a.get());
2829 <    }}
2830 <
2831 <    /**
2832 <     * If a whenComplete action throws an exception when triggered by
2833 <     * a normal completion, it completes exceptionally
2834 <     */
2835 <    public void testWhenComplete_actionFailed() {
2836 <        for (boolean createIncomplete : new boolean[] { true, false })
2837 <        for (ExecutionMode m : ExecutionMode.values())
2838 <        for (Integer v1 : new Integer[] { 1, null })
2839 <    {
2840 <        final AtomicInteger a = new AtomicInteger(0);
2841 <        final CFException ex = new CFException();
2842 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2843 <        if (!createIncomplete) f.complete(v1);
2844 <        final CompletableFuture<Integer> g = m.whenComplete
2845 <            (f,
2846 <             (Integer x, Throwable t) -> {
2847 <                threadAssertSame(x, v1);
2848 <                threadAssertNull(t);
2849 <                a.getAndIncrement();
2850 <                throw ex;
2851 <            });
2852 <        if (createIncomplete) f.complete(v1);
2853 <        checkCompletedNormally(f, v1);
2854 <        checkCompletedWithWrappedCFException(g, ex);
2855 <        assertEquals(1, a.get());
2856 <    }}
2857 <
2858 <    /**
2859 <     * If a whenComplete action throws an exception when triggered by
2860 <     * a source completion that also throws an exception, the source
2861 <     * exception takes precedence.
2862 <     */
2863 <    public void testWhenComplete_actionFailedSourceFailed() {
2864 <        for (boolean createIncomplete : new boolean[] { true, false })
2865 <        for (ExecutionMode m : ExecutionMode.values())
2866 <        for (Integer v1 : new Integer[] { 1, null })
2867 <    {
2868 <        final AtomicInteger a = new AtomicInteger(0);
2869 <        final CFException ex1 = new CFException();
2870 <        final CFException ex2 = new CFException();
2871 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2872 <
2873 <        if (!createIncomplete) f.completeExceptionally(ex1);
2874 <        final CompletableFuture<Integer> g = m.whenComplete
2875 <            (f,
2876 <             (Integer x, Throwable t) -> {
2877 <                threadAssertSame(t, ex1);
2878 <                threadAssertNull(x);
2879 <                a.getAndIncrement();
2880 <                throw ex2;
2881 <            });
2882 <        if (createIncomplete) f.completeExceptionally(ex1);
2883 <
2884 <        checkCompletedWithWrappedCFException(f, ex1);
2885 <        checkCompletedWithWrappedCFException(g, ex1);
2886 <        assertEquals(1, a.get());
2887 <    }}
3017 > //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3018 > //         for (ExecutionMode m : ExecutionMode.values())
3019 > //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3020 > //         for (Integer v1 : new Integer[] { 1, null })
3021 > //     {
3022 > //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3023 > //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3024 > //         final Noop[] rs = new Noop[2];
3025 > //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3026 > //         f.complete(v1);
3027 > //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3028 > //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3029 > //         assertTrue(g.cancel(mayInterruptIfRunning));
3030 > //         checkCompletedNormally(h0, null);
3031 > //         checkCompletedNormally(h1, null);
3032 > //         for (Noop r : rs) r.assertInvoked();
3033 > //     }}
3034  
3035   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines