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.74 by jsr166, Fri Jun 6 21:19:22 2014 UTC vs.
Revision 1.78 by jsr166, Sat Jun 7 23:32:17 2014 UTC

# Line 227 | 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 250 | 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 262 | 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 289 | Line 295 | public class CompletableFutureTest exten
295          CompletableFuture<Integer> f;
296  
297          f = new CompletableFuture<>();
298 <        f.complete(v1);
298 >        assertTrue(f.complete(v1));
299          for (int i = 0; i < 2; i++) {
300              f.obtrudeException(ex = new CFException());
301              checkCompletedExceptionally(f, ex);
# Line 309 | Line 315 | public class CompletableFutureTest exten
315          checkCompletedExceptionally(f, ex);
316          f.completeExceptionally(new CFException());
317          checkCompletedExceptionally(f, ex);
318 <        f.complete(v1);
318 >        assertFalse(f.complete(v1));
319          checkCompletedExceptionally(f, ex);
320      }}
321  
# Line 317 | Line 323 | public class CompletableFutureTest exten
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 339 | 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());
347 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
348 <
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(true);
358 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359          assertTrue(f.toString().contains("[Completed exceptionally]"));
360  
361 <        f = new CompletableFuture<String>();
362 <        f.cancel(false);
363 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
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 529 | 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 837 | 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  
854
864      /**
865       * exceptionally action completes with function value on source
866       * exception
# Line 900 | Line 909 | public class CompletableFutureTest exten
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 +
1054 +    /**
1055       * handle action completes normally with function value on normal
1056       * completion of source
1057       */
# Line 910 | 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 920 | 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 1021 | 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 1031 | 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          checkCompletedWithWrappedException(g, ex);
1189          checkCompletedNormally(f, v1);
# Line 1117 | 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 1184 | 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 1205 | 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));
# Line 1272 | 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 1293 | 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 1386 | 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));
# Line 1417 | 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          checkCompletedWithWrappedException(h, ex);
# Line 1446 | 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 1475 | 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);
1480 <            g.complete(v2);
1481 <        } else {
1482 <            g.complete(v2);
1483 <            f.complete(v1);
1484 <        }
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 1503 | 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 1534 | 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          checkCompletedWithWrappedException(h, ex);
# Line 1563 | 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 1592 | 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);
1597 <            g.complete(v2);
1598 <        } else {
1599 <            g.complete(v2);
1600 <            f.complete(v1);
1601 <        }
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 1620 | 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 1651 | 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          checkCompletedWithWrappedException(h, ex);
# Line 1680 | Line 1822 | public class CompletableFutureTest exten
1822          final CompletableFuture<Integer> g = new CompletableFuture<>();
1823          final Noop r = new Noop(m);
1824  
1825 <
1684 <        (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 1711 | 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);
1716 <            g.complete(v2);
1717 <        } else {
1718 <            g.complete(v2);
1719 <            f.complete(v1);
1720 <        }
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 1844 | Line 1980 | public class CompletableFutureTest exten
1980  
1981          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1982          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1983 <        if (fFirst) {
1984 <            f.complete(v1);
1849 <            g.completeExceptionally(ex);
1850 <        } else {
1851 <            g.completeExceptionally(ex);
1852 <            f.complete(v1);
1853 <        }
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  
# Line 1956 | Line 2087 | public class CompletableFutureTest exten
2087  
2088          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2089          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2090 <        if (fFirst) {
2091 <            f.complete(v1);
1961 <            g.cancel(mayInterruptIfRunning);
1962 <        } else {
1963 <            g.cancel(mayInterruptIfRunning);
1964 <            f.complete(v1);
1965 <        }
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  
# Line 2164 | Line 2290 | public class CompletableFutureTest exten
2290  
2291          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2292          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2293 <        if (fFirst) {
2294 <            f.complete(v1);
2169 <            g.completeExceptionally(ex);
2170 <        } else {
2171 <            g.completeExceptionally(ex);
2172 <            f.complete(v1);
2173 <        }
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  
# Line 2373 | Line 2494 | public class CompletableFutureTest exten
2494          checkIncomplete(h1);
2495          rs[0].assertNotInvoked();
2496          rs[1].assertNotInvoked();
2497 <        f.completeExceptionally(ex);
2497 >        assertTrue(f.completeExceptionally(ex));
2498          checkCompletedWithWrappedException(h0, ex);
2499          checkCompletedWithWrappedException(h1, ex);
2500          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2381 | Line 2502 | public class CompletableFutureTest exten
2502          checkCompletedWithWrappedException(h2, ex);
2503          checkCompletedWithWrappedException(h3, ex);
2504  
2505 <        g.complete(v1);
2505 >        assertTrue(g.complete(v1));
2506  
2507          // unspecified behavior - both source completions available
2508          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2424 | Line 2545 | public class CompletableFutureTest exten
2545  
2546          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2547          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2548 <        if (fFirst) {
2549 <            f.complete(v1);
2429 <            g.completeExceptionally(ex);
2430 <        } else {
2431 <            g.completeExceptionally(ex);
2432 <            f.complete(v1);
2433 <        }
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  
# Line 2495 | Line 2611 | public class CompletableFutureTest exten
2611          checkCompletedWithWrappedCancellationException(h2);
2612          checkCompletedWithWrappedCancellationException(h3);
2613  
2614 <        g.complete(v1);
2614 >        assertTrue(g.complete(v1));
2615  
2616          // unspecified behavior - both source completions available
2617          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2539 | Line 2655 | public class CompletableFutureTest exten
2655  
2656          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2657          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2658 <        f.complete(v1);
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);
# Line 2547 | Line 2663 | public class CompletableFutureTest exten
2663          checkCompletedWithWrappedCFException(h2);
2664          checkCompletedWithWrappedCFException(h3);
2665          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2666 <        g.complete(v2);
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);
# Line 2568 | 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);
# Line 2608 | 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 2849 | 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 <                m.checkExecutionMode();
2984 <                threadAssertSame(x, v1);
2869 <                threadAssertNull(t);
2870 <                a.getAndIncrement();
2871 <            });
2872 <        if (createIncomplete) f.complete(v1);
2873 <
2874 <        checkCompletedNormally(g, v1);
2875 <        checkCompletedNormally(f, v1);
2876 <        assertEquals(1, a.get());
2877 <    }}
2878 <
2879 <    /**
2880 <     * whenComplete action executes on exceptional completion, propagating
2881 <     * source result.
2882 <     */
2883 <    public void testWhenComplete_exceptionalCompletion() {
2884 <        for (ExecutionMode m : ExecutionMode.values())
2885 <        for (boolean createIncomplete : new boolean[] { true, false })
2886 <        for (Integer v1 : new Integer[] { 1, null })
2887 <    {
2888 <        final AtomicInteger a = new AtomicInteger(0);
2889 <        final CFException ex = new CFException();
2890 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2891 <        if (!createIncomplete) f.completeExceptionally(ex);
2892 <        final CompletableFuture<Integer> g = m.whenComplete
2893 <            (f,
2894 <             (Integer x, Throwable t) -> {
2895 <                m.checkExecutionMode();
2896 <                threadAssertNull(x);
2897 <                threadAssertSame(t, ex);
2898 <                a.getAndIncrement();
2899 <            });
2900 <        if (createIncomplete) f.completeExceptionally(ex);
2901 <        checkCompletedExceptionally(f, ex);
2902 <        checkCompletedWithWrappedException(g, ex);
2903 <        assertEquals(1, a.get());
2904 <    }}
2905 <
2906 <    /**
2907 <     * whenComplete action executes on cancelled source, propagating
2908 <     * CancellationException.
2909 <     */
2910 <    public void testWhenComplete_sourceCancelled() {
2911 <        for (ExecutionMode m : ExecutionMode.values())
2912 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2913 <        for (boolean createIncomplete : new boolean[] { true, false })
2914 <    {
2915 <        final AtomicInteger a = new AtomicInteger(0);
2916 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2917 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2918 <        final CompletableFuture<Integer> g = m.whenComplete
2919 <            (f,
2920 <             (Integer x, Throwable t) -> {
2921 <                m.checkExecutionMode();
2922 <                threadAssertNull(x);
2923 <                threadAssertTrue(t instanceof CancellationException);
2924 <                a.getAndIncrement();
2925 <            });
2926 <        if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
2927 <
2928 <        checkCompletedWithWrappedCancellationException(g);
2929 <        checkCancelled(f);
2930 <        assertEquals(1, a.get());
2931 <    }}
2932 <
2933 <    /**
2934 <     * If a whenComplete action throws an exception when triggered by
2935 <     * a normal completion, it completes exceptionally
2936 <     */
2937 <    public void testWhenComplete_actionFailed() {
2938 <        for (boolean createIncomplete : new boolean[] { true, false })
2939 <        for (ExecutionMode m : ExecutionMode.values())
2940 <        for (Integer v1 : new Integer[] { 1, null })
2941 <    {
2942 <        final AtomicInteger a = new AtomicInteger(0);
2943 <        final CFException ex = new CFException();
2944 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2945 <        if (!createIncomplete) f.complete(v1);
2946 <        final CompletableFuture<Integer> g = m.whenComplete
2947 <            (f,
2948 <             (Integer x, Throwable t) -> {
2949 <                m.checkExecutionMode();
2950 <                threadAssertSame(x, v1);
2951 <                threadAssertNull(t);
2952 <                a.getAndIncrement();
2953 <                throw ex;
2954 <            });
2955 <        if (createIncomplete) f.complete(v1);
2956 <        checkCompletedNormally(f, v1);
2957 <        checkCompletedWithWrappedException(g, ex);
2958 <        assertEquals(1, a.get());
2959 <    }}
2960 <
2961 <    /**
2962 <     * If a whenComplete action throws an exception when triggered by
2963 <     * a source completion that also throws an exception, the source
2964 <     * exception takes precedence.
2965 <     */
2966 <    public void testWhenComplete_actionFailedSourceFailed() {
2967 <        for (boolean createIncomplete : new boolean[] { true, false })
2968 <        for (ExecutionMode m : ExecutionMode.values())
2969 <        for (Integer v1 : new Integer[] { 1, null })
2970 <    {
2971 <        final AtomicInteger a = new AtomicInteger(0);
2972 <        final CFException ex1 = new CFException();
2973 <        final CFException ex2 = new CFException();
2974 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2975 <
2976 <        if (!createIncomplete) f.completeExceptionally(ex1);
2977 <        final CompletableFuture<Integer> g = m.whenComplete
2978 <            (f,
2979 <             (Integer x, Throwable t) -> {
2980 <                m.checkExecutionMode();
2981 <                threadAssertSame(t, ex1);
2982 <                threadAssertNull(x);
2983 <                a.getAndIncrement();
2984 <                throw ex2;
2985 <            });
2986 <        if (createIncomplete) f.completeExceptionally(ex1);
2987 <
2988 <        checkCompletedExceptionally(f, ex1);
2989 <        checkCompletedWithWrappedException(g, ex1);
2990 <        assertEquals(1, a.get());
2991 <    }}
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