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.39 by jsr166, Mon Jun 2 00:46:52 2014 UTC vs.
Revision 1.43 by jsr166, Mon Jun 2 04:13:54 2014 UTC

# Line 375 | Line 375 | public class CompletableFutureTest exten
375          }
376      }
377      static final class Noop implements Runnable {
378 +        int invocationCount = 0;
379          boolean ran;
380 <        public void run() { ran = true; }
380 >        public void run() {
381 >            invocationCount++;
382 >            ran = true;
383 >        }
384      }
385  
386      static final class FailingSupplier implements Supplier<Integer> {
# Line 943 | Line 947 | public class CompletableFutureTest exten
947       * of sources
948       */
949      public void testThenCombine_normalCompletion1() {
950 +        for (boolean createdIncomplete : new boolean[] { true, false })
951 +        for (boolean fFirst : new boolean[] { true, false })
952          for (ExecutionMode m : ExecutionMode.values())
953          for (Integer v1 : new Integer[] { 1, null })
954          for (Integer v2 : new Integer[] { 2, null }) {
# Line 950 | Line 956 | public class CompletableFutureTest exten
956          final CompletableFuture<Integer> f = new CompletableFuture<>();
957          final CompletableFuture<Integer> g = new CompletableFuture<>();
958          final SubtractFunction r = new SubtractFunction();
959 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
960 <
955 <        f.complete(v1);
956 <        checkIncomplete(h);
957 <        assertFalse(r.ran());
958 <        g.complete(v2);
959 <
960 <        checkCompletedNormally(h, subtract(v1, v2));
961 <        checkCompletedNormally(f, v1);
962 <        checkCompletedNormally(g, v2);
963 <        }
964 <    }
965 <
966 <    public void testThenCombine_normalCompletion2() {
967 <        for (ExecutionMode m : ExecutionMode.values())
968 <        for (Integer v1 : new Integer[] { 1, null })
969 <        for (Integer v2 : new Integer[] { 2, null }) {
970 <
971 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
972 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
973 <        final SubtractFunction r = new SubtractFunction();
974 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
975 <
976 <        g.complete(v2);
977 <        checkIncomplete(h);
978 <        assertFalse(r.ran());
979 <        f.complete(v1);
980 <
981 <        checkCompletedNormally(h, subtract(v1, v2));
982 <        checkCompletedNormally(f, v1);
983 <        checkCompletedNormally(g, v2);
984 <        }
985 <    }
986 <
987 <    public void testThenCombine_normalCompletion3() {
988 <        for (ExecutionMode m : ExecutionMode.values())
989 <        for (Integer v1 : new Integer[] { 1, null })
990 <        for (Integer v2 : new Integer[] { 2, null }) {
991 <
992 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
993 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
994 <        final SubtractFunction r = new SubtractFunction();
959 >        CompletableFuture<Integer> h = null;
960 >        if (createdIncomplete) h = m.thenCombine(f, g, r);
961  
962 <        g.complete(v2);
963 <        f.complete(v1);
964 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
965 <
966 <        checkCompletedNormally(h, subtract(v1, v2));
967 <        checkCompletedNormally(f, v1);
968 <        checkCompletedNormally(g, v2);
969 <        }
970 <    }
971 <
972 <    public void testThenCombine_normalCompletion4() {
1007 <        for (ExecutionMode m : ExecutionMode.values())
1008 <        for (Integer v1 : new Integer[] { 1, null })
1009 <        for (Integer v2 : new Integer[] { 2, null }) {
1010 <
1011 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1012 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1013 <        final SubtractFunction r = new SubtractFunction();
1014 <
1015 <        f.complete(v1);
1016 <        g.complete(v2);
1017 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
962 >        if (fFirst)
963 >            f.complete(v1);
964 >        else
965 >            g.complete(v2);
966 >        if (createdIncomplete) checkIncomplete(h);
967 >        assertEquals(r.invocationCount, 0);
968 >        if (!fFirst)
969 >            f.complete(v1);
970 >        else
971 >            g.complete(v2);
972 >        if (!createdIncomplete) h = m.thenCombine(f, g, r);
973  
974          checkCompletedNormally(h, subtract(v1, v2));
975          checkCompletedNormally(f, v1);
976          checkCompletedNormally(g, v2);
977 +        assertEquals(r.invocationCount, 1);
978          }
979      }
980  
# Line 1042 | Line 998 | public class CompletableFutureTest exten
998  
999          checkCompletedWithWrappedCFException(h, ex);
1000          checkCompletedWithWrappedCFException(f, ex);
1001 <        assertFalse(r.ran());
1001 >        assertEquals(r.invocationCount, 0);
1002          checkCompletedNormally(g, v1);
1003          }
1004      }
# Line 1063 | Line 1019 | public class CompletableFutureTest exten
1019  
1020          checkCompletedWithWrappedCFException(h, ex);
1021          checkCompletedWithWrappedCFException(g, ex);
1022 <        assertFalse(r.ran());
1022 >        assertEquals(r.invocationCount, 0);
1023          checkCompletedNormally(f, v1);
1024          }
1025      }
# Line 1083 | Line 1039 | public class CompletableFutureTest exten
1039  
1040          checkCompletedWithWrappedCFException(h, ex);
1041          checkCompletedWithWrappedCFException(g, ex);
1042 <        assertFalse(r.ran());
1042 >        assertEquals(r.invocationCount, 0);
1043          checkCompletedNormally(f, v1);
1044          }
1045      }
# Line 1103 | Line 1059 | public class CompletableFutureTest exten
1059  
1060          checkCompletedWithWrappedCFException(h, ex);
1061          checkCompletedWithWrappedCFException(f, ex);
1062 <        assertFalse(r.ran());
1062 >        assertEquals(r.invocationCount, 0);
1063          checkCompletedNormally(g, v1);
1064          }
1065      }
# Line 1170 | Line 1126 | public class CompletableFutureTest exten
1126  
1127          checkCompletedWithWrappedCancellationException(h);
1128          checkCancelled(f);
1129 <        assertFalse(r.ran());
1129 >        assertEquals(r.invocationCount, 0);
1130          checkCompletedNormally(g, v1);
1131          }
1132      }
# Line 1191 | Line 1147 | public class CompletableFutureTest exten
1147  
1148          checkCompletedWithWrappedCancellationException(h);
1149          checkCancelled(g);
1150 <        assertFalse(r.ran());
1150 >        assertEquals(r.invocationCount, 0);
1151          checkCompletedNormally(f, v1);
1152          }
1153      }
# Line 1211 | Line 1167 | public class CompletableFutureTest exten
1167  
1168          checkCompletedWithWrappedCancellationException(h);
1169          checkCancelled(g);
1170 <        assertFalse(r.ran());
1170 >        assertEquals(r.invocationCount, 0);
1171          checkCompletedNormally(f, v1);
1172          }
1173      }
# Line 1231 | Line 1187 | public class CompletableFutureTest exten
1187  
1188          checkCompletedWithWrappedCancellationException(h);
1189          checkCancelled(f);
1190 <        assertFalse(r.ran());
1190 >        assertEquals(r.invocationCount, 0);
1191          checkCompletedNormally(g, v1);
1192          }
1193      }
# Line 1344 | Line 1300 | public class CompletableFutureTest exten
1300  
1301          checkCompletedWithWrappedCFException(h, ex);
1302          checkCompletedWithWrappedCFException(f, ex);
1303 <        assertFalse(r.ran());
1303 >        assertEquals(r.invocationCount, 0);
1304          checkCompletedNormally(g, v1);
1305          }
1306      }
# Line 1365 | Line 1321 | public class CompletableFutureTest exten
1321  
1322          checkCompletedWithWrappedCFException(h, ex);
1323          checkCompletedWithWrappedCFException(g, ex);
1324 <        assertFalse(r.ran());
1324 >        assertEquals(r.invocationCount, 0);
1325          checkCompletedNormally(f, v1);
1326          }
1327      }
# Line 1385 | Line 1341 | public class CompletableFutureTest exten
1341  
1342          checkCompletedWithWrappedCFException(h, ex);
1343          checkCompletedWithWrappedCFException(g, ex);
1344 <        assertFalse(r.ran());
1344 >        assertEquals(r.invocationCount, 0);
1345          checkCompletedNormally(f, v1);
1346          }
1347      }
# Line 1472 | Line 1428 | public class CompletableFutureTest exten
1428  
1429          checkCompletedWithWrappedCancellationException(h);
1430          checkCancelled(f);
1431 <        assertFalse(r.ran());
1431 >        assertEquals(r.invocationCount, 0);
1432          checkCompletedNormally(g, v1);
1433          }
1434      }
# Line 1493 | Line 1449 | public class CompletableFutureTest exten
1449  
1450          checkCompletedWithWrappedCancellationException(h);
1451          checkCancelled(g);
1452 <        assertFalse(r.ran());
1452 >        assertEquals(r.invocationCount, 0);
1453          checkCompletedNormally(f, v1);
1454          }
1455      }
# Line 1513 | Line 1469 | public class CompletableFutureTest exten
1469  
1470          checkCompletedWithWrappedCancellationException(h);
1471          checkCancelled(g);
1472 <        assertFalse(r.ran());
1472 >        assertEquals(r.invocationCount, 0);
1473          checkCompletedNormally(f, v1);
1474          }
1475      }
# Line 1533 | Line 1489 | public class CompletableFutureTest exten
1489  
1490          checkCompletedWithWrappedCancellationException(h);
1491          checkCancelled(f);
1492 <        assertFalse(r.ran());
1492 >        assertEquals(r.invocationCount, 0);
1493          checkCompletedNormally(g, v1);
1494          }
1495      }
# Line 1558 | Line 1514 | public class CompletableFutureTest exten
1514          g.complete(v2);
1515  
1516          checkCompletedNormally(h, null);
1517 <        assertTrue(r.ran);
1517 >        assertEquals(r.invocationCount, 1);
1518          checkCompletedNormally(f, v1);
1519          checkCompletedNormally(g, v2);
1520          }
# Line 1580 | Line 1536 | public class CompletableFutureTest exten
1536          f.complete(v1);
1537  
1538          checkCompletedNormally(h, null);
1539 <        assertTrue(r.ran);
1539 >        assertEquals(r.invocationCount, 1);
1540          checkCompletedNormally(f, v1);
1541          checkCompletedNormally(g, v2);
1542          }
# Line 1620 | Line 1576 | public class CompletableFutureTest exten
1576          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1577  
1578          checkCompletedNormally(h, null);
1579 <        assertTrue(r.ran);
1579 >        assertEquals(r.invocationCount, 1);
1580          checkCompletedNormally(f, v1);
1581          checkCompletedNormally(g, v2);
1582          }
# Line 1646 | Line 1602 | public class CompletableFutureTest exten
1602  
1603          checkCompletedWithWrappedCFException(h, ex);
1604          checkCompletedWithWrappedCFException(f, ex);
1605 <        assertFalse(r.ran);
1605 >        assertEquals(r.invocationCount, 0);
1606          checkCompletedNormally(g, v1);
1607          }
1608      }
# Line 1667 | Line 1623 | public class CompletableFutureTest exten
1623  
1624          checkCompletedWithWrappedCFException(h, ex);
1625          checkCompletedWithWrappedCFException(g, ex);
1626 <        assertFalse(r.ran);
1626 >        assertEquals(r.invocationCount, 0);
1627          checkCompletedNormally(f, v1);
1628          }
1629      }
# Line 1687 | Line 1643 | public class CompletableFutureTest exten
1643  
1644          checkCompletedWithWrappedCFException(h, ex);
1645          checkCompletedWithWrappedCFException(g, ex);
1646 <        assertFalse(r.ran);
1646 >        assertEquals(r.invocationCount, 0);
1647          checkCompletedNormally(f, v1);
1648          }
1649      }
# Line 1707 | Line 1663 | public class CompletableFutureTest exten
1663  
1664          checkCompletedWithWrappedCFException(h, ex);
1665          checkCompletedWithWrappedCFException(f, ex);
1666 <        assertFalse(r.ran);
1666 >        assertEquals(r.invocationCount, 0);
1667          checkCompletedNormally(g, v1);
1668          }
1669      }
# Line 1774 | Line 1730 | public class CompletableFutureTest exten
1730  
1731          checkCompletedWithWrappedCancellationException(h);
1732          checkCancelled(f);
1733 <        assertFalse(r.ran);
1733 >        assertEquals(r.invocationCount, 0);
1734          checkCompletedNormally(g, v1);
1735          }
1736      }
# Line 1795 | Line 1751 | public class CompletableFutureTest exten
1751  
1752          checkCompletedWithWrappedCancellationException(h);
1753          checkCancelled(g);
1754 <        assertFalse(r.ran);
1754 >        assertEquals(r.invocationCount, 0);
1755          checkCompletedNormally(f, v1);
1756          }
1757      }
# Line 1815 | Line 1771 | public class CompletableFutureTest exten
1771  
1772          checkCompletedWithWrappedCancellationException(h);
1773          checkCancelled(g);
1774 <        assertFalse(r.ran);
1774 >        assertEquals(r.invocationCount, 0);
1775          checkCompletedNormally(f, v1);
1776          }
1777      }
# Line 1835 | Line 1791 | public class CompletableFutureTest exten
1791  
1792          checkCompletedWithWrappedCancellationException(h);
1793          checkCancelled(f);
1794 <        assertFalse(r.ran);
1794 >        assertEquals(r.invocationCount, 0);
1795          checkCompletedNormally(g, v1);
1796          }
1797      }
# Line 1902 | Line 1858 | public class CompletableFutureTest exten
1858          // unspecified behavior
1859          assertTrue(Objects.equals(h.join(), inc(v1)) ||
1860                     Objects.equals(h.join(), inc(v2)));
1861 +        assertEquals(r.invocationCount, 1);
1862          }
1863      }
1864  
# Line 1923 | Line 1880 | public class CompletableFutureTest exten
1880          checkCompletedWithWrappedCFException(h, ex);
1881          g.complete(v1);
1882  
1883 <        assertFalse(r.ran());
1883 >        assertEquals(r.invocationCount, 0);
1884          checkCompletedNormally(g, v1);
1885          checkCompletedWithWrappedCFException(f, ex);
1886          checkCompletedWithWrappedCFException(h, ex);
# Line 1944 | Line 1901 | public class CompletableFutureTest exten
1901          checkCompletedWithWrappedCFException(h, ex);
1902          f.complete(v1);
1903  
1904 <        assertFalse(r.ran());
1904 >        assertEquals(r.invocationCount, 0);
1905          checkCompletedNormally(f, v1);
1906          checkCompletedWithWrappedCFException(g, ex);
1907          checkCompletedWithWrappedCFException(h, ex);
# Line 1968 | Line 1925 | public class CompletableFutureTest exten
1925          Integer v;
1926          try {
1927              assertEquals(h.join(), inc(v1));
1928 <            assertTrue(r.ran());
1928 >            assertEquals(r.invocationCount, 1);
1929          } catch (CompletionException ok) {
1930              checkCompletedWithWrappedCFException(h, ex);
1931 <            assertFalse(r.ran());
1931 >            assertEquals(r.invocationCount, 0);
1932          }
1933  
1934          checkCompletedWithWrappedCFException(g, ex);
# Line 1996 | Line 1953 | public class CompletableFutureTest exten
1953          Integer v;
1954          try {
1955              assertEquals(h.join(), inc(v1));
1956 <            assertTrue(r.ran());
1956 >            assertEquals(r.invocationCount, 1);
1957          } catch (CompletionException ok) {
1958              checkCompletedWithWrappedCFException(h, ex);
1959 <            assertFalse(r.ran());
1959 >            assertEquals(r.invocationCount, 0);
1960          }
1961  
1962          checkCompletedWithWrappedCFException(f, ex);
2006        assertFalse(r.ran());
1963          checkCompletedNormally(g, v1);
1964          }
1965      }
# Line 2065 | Line 2021 | public class CompletableFutureTest exten
2021          g.complete(v1);
2022  
2023          checkCancelled(f);
2024 <        assertFalse(r.ran());
2024 >        assertEquals(r.invocationCount, 0);
2025          checkCompletedNormally(g, v1);
2026          checkCompletedWithWrappedCancellationException(h);
2027          }
# Line 2086 | Line 2042 | public class CompletableFutureTest exten
2042          f.complete(v1);
2043  
2044          checkCancelled(g);
2045 <        assertFalse(r.ran());
2045 >        assertEquals(r.invocationCount, 0);
2046          checkCompletedNormally(f, v1);
2047          checkCompletedWithWrappedCancellationException(h);
2048          }
# Line 2109 | Line 2065 | public class CompletableFutureTest exten
2065          Integer v;
2066          try {
2067              assertEquals(h.join(), inc(v1));
2068 <            assertTrue(r.ran());
2068 >            assertEquals(r.invocationCount, 1);
2069          } catch (CompletionException ok) {
2070              checkCompletedWithWrappedCancellationException(h);
2071 <            assertFalse(r.ran());
2071 >            assertEquals(r.invocationCount, 0);
2072          }
2073  
2074          checkCancelled(g);
# Line 2137 | Line 2093 | public class CompletableFutureTest exten
2093          Integer v;
2094          try {
2095              assertEquals(h.join(), inc(v1));
2096 <            assertTrue(r.ran());
2096 >            assertEquals(r.invocationCount, 1);
2097          } catch (CompletionException ok) {
2098              checkCompletedWithWrappedCancellationException(h);
2099 <            assertFalse(r.ran());
2099 >            assertEquals(r.invocationCount, 0);
2100          }
2101  
2102          checkCancelled(f);
# Line 2152 | Line 2108 | public class CompletableFutureTest exten
2108       * acceptEither result completes normally after normal completion
2109       * of either source
2110       */
2111 <    public void testAcceptEither() {
2112 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2113 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2114 <        IncAction r = new IncAction();
2159 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2160 <        f.complete(one);
2161 <        checkCompletedNormally(g, null);
2162 <        f2.complete(one);
2163 <        checkCompletedNormally(g, null);
2164 <        assertEquals(r.value, (Integer) 2);
2111 >    public void testAcceptEither_normalCompletion1() {
2112 >        for (ExecutionMode m : ExecutionMode.values())
2113 >        for (Integer v1 : new Integer[] { 1, null })
2114 >        for (Integer v2 : new Integer[] { 2, null }) {
2115  
2116 <        r = new IncAction();
2117 <        f = new CompletableFuture<>();
2118 <        f.complete(one);
2119 <        f2 = new CompletableFuture<>();
2120 <        g = f.acceptEither(f2, r);
2121 <        checkCompletedNormally(g, null);
2122 <        assertEquals(r.value, (Integer) 2);
2116 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2117 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2118 >        final IncAction r = new IncAction();
2119 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2120 >
2121 >        f.complete(v1);
2122 >        checkCompletedNormally(h, null);
2123 >        assertEquals(r.value, inc(v1));
2124 >        g.complete(v2);
2125 >
2126 >        checkCompletedNormally(f, v1);
2127 >        checkCompletedNormally(g, v2);
2128 >        checkCompletedNormally(h, null);
2129 >        }
2130 >    }
2131 >
2132 >    public void testAcceptEither_normalCompletion2() {
2133 >        for (ExecutionMode m : ExecutionMode.values())
2134 >        for (Integer v1 : new Integer[] { 1, null })
2135 >        for (Integer v2 : new Integer[] { 2, null }) {
2136 >
2137 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2138 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2139 >        final IncAction r = new IncAction();
2140 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2141 >
2142 >        g.complete(v2);
2143 >        checkCompletedNormally(h, null);
2144 >        assertEquals(r.value, inc(v2));
2145 >        f.complete(v1);
2146 >
2147 >        checkCompletedNormally(f, v1);
2148 >        checkCompletedNormally(g, v2);
2149 >        checkCompletedNormally(h, null);
2150 >        }
2151 >    }
2152 >    public void testAcceptEither_normalCompletion3() {
2153 >        for (ExecutionMode m : ExecutionMode.values())
2154 >        for (Integer v1 : new Integer[] { 1, null })
2155 >        for (Integer v2 : new Integer[] { 2, null }) {
2156 >
2157 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2158 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2159 >        final IncAction r = new IncAction();
2160 >
2161 >        f.complete(v1);
2162 >        g.complete(v2);
2163 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2164 >
2165 >        checkCompletedNormally(h, null);
2166 >        checkCompletedNormally(f, v1);
2167 >        checkCompletedNormally(g, v2);
2168 >
2169 >        // unspecified behavior
2170 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2171 >                   Objects.equals(r.value, inc(v2)));
2172 >        }
2173      }
2174  
2175      /**
2176       * acceptEither result completes exceptionally after exceptional
2177       * completion of either source
2178       */
2179 <    public void testAcceptEither2() {
2180 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2181 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2182 <        IncAction r = new IncAction();
2183 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2184 <        f.completeExceptionally(new CFException());
2185 <        f2.complete(one);
2186 <        checkCompletedWithWrappedCFException(g);
2179 >    public void testAcceptEither_exceptionalCompletion1() {
2180 >        for (ExecutionMode m : ExecutionMode.values())
2181 >        for (Integer v1 : new Integer[] { 1, null }) {
2182  
2183 <        r = new IncAction();
2184 <        f = new CompletableFuture<>();
2185 <        f2 = new CompletableFuture<>();
2186 <        f2.completeExceptionally(new CFException());
2187 <        g = f.acceptEither(f2, r);
2188 <        checkCompletedWithWrappedCFException(g);
2183 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2184 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2185 >        final IncAction r = new IncAction();
2186 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2187 >        final CFException ex = new CFException();
2188 >
2189 >        f.completeExceptionally(ex);
2190 >        checkCompletedWithWrappedCFException(h, ex);
2191 >        g.complete(v1);
2192 >
2193 >        assertEquals(r.invocationCount, 0);
2194 >        checkCompletedNormally(g, v1);
2195 >        checkCompletedWithWrappedCFException(f, ex);
2196 >        checkCompletedWithWrappedCFException(h, ex);
2197 >        }
2198 >    }
2199 >
2200 >    public void testAcceptEither_exceptionalCompletion2() {
2201 >        for (ExecutionMode m : ExecutionMode.values())
2202 >        for (Integer v1 : new Integer[] { 1, null }) {
2203 >
2204 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2205 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2206 >        final IncAction r = new IncAction();
2207 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2208 >        final CFException ex = new CFException();
2209 >
2210 >        g.completeExceptionally(ex);
2211 >        checkCompletedWithWrappedCFException(h, ex);
2212 >        f.complete(v1);
2213 >
2214 >        assertEquals(r.invocationCount, 0);
2215 >        checkCompletedNormally(f, v1);
2216 >        checkCompletedWithWrappedCFException(g, ex);
2217 >        checkCompletedWithWrappedCFException(h, ex);
2218 >        }
2219 >    }
2220 >
2221 >    public void testAcceptEither_exceptionalCompletion3() {
2222 >        for (ExecutionMode m : ExecutionMode.values())
2223 >        for (Integer v1 : new Integer[] { 1, null }) {
2224 >
2225 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2226 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2227 >        final IncAction r = new IncAction();
2228 >        final CFException ex = new CFException();
2229 >
2230 >        g.completeExceptionally(ex);
2231 >        f.complete(v1);
2232 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2233 >
2234 >        // unspecified behavior
2235 >        Integer v;
2236 >        try {
2237 >            assertEquals(h.join(), null);
2238 >            assertEquals(r.invocationCount, 1);
2239 >            assertEquals(inc(v1), r.value);
2240 >        } catch (CompletionException ok) {
2241 >            checkCompletedWithWrappedCFException(h, ex);
2242 >            assertEquals(r.invocationCount, 0);
2243 >        }
2244 >
2245 >        checkCompletedWithWrappedCFException(g, ex);
2246 >        checkCompletedNormally(f, v1);
2247 >        }
2248 >    }
2249 >
2250 >    public void testAcceptEither_exceptionalCompletion4() {
2251 >        for (ExecutionMode m : ExecutionMode.values())
2252 >        for (Integer v1 : new Integer[] { 1, null }) {
2253 >
2254 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2255 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2256 >        final IncAction r = new IncAction();
2257 >        final CFException ex = new CFException();
2258 >
2259 >        f.completeExceptionally(ex);
2260 >        g.complete(v1);
2261 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2262 >
2263 >        // unspecified behavior
2264 >        Integer v;
2265 >        try {
2266 >            assertEquals(h.join(), null);
2267 >            assertEquals(r.invocationCount, 1);
2268 >            assertEquals(inc(v1), r.value);
2269 >        } catch (CompletionException ok) {
2270 >            checkCompletedWithWrappedCFException(h, ex);
2271 >            assertEquals(r.invocationCount, 0);
2272 >        }
2273 >
2274 >        checkCompletedWithWrappedCFException(f, ex);
2275 >        checkCompletedNormally(g, v1);
2276 >        }
2277      }
2278  
2279      /**
2280       * acceptEither result completes exceptionally if action does
2281       */
2282 <    public void testAcceptEither3() {
2283 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2284 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2285 <        FailingConsumer r = new FailingConsumer();
2286 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2287 <        f2.complete(two);
2288 <        checkCompletedWithWrappedCFException(g);
2282 >    public void testAcceptEither_actionFailed1() {
2283 >        for (ExecutionMode m : ExecutionMode.values())
2284 >        for (Integer v1 : new Integer[] { 1, null })
2285 >        for (Integer v2 : new Integer[] { 2, null }) {
2286 >
2287 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2288 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2289 >        final FailingConsumer r = new FailingConsumer();
2290 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2291 >
2292 >        f.complete(v1);
2293 >        checkCompletedWithWrappedCFException(h);
2294 >        g.complete(v2);
2295 >        checkCompletedNormally(f, v1);
2296 >        checkCompletedNormally(g, v2);
2297 >        }
2298 >    }
2299 >
2300 >    public void testAcceptEither_actionFailed2() {
2301 >        for (ExecutionMode m : ExecutionMode.values())
2302 >        for (Integer v1 : new Integer[] { 1, null })
2303 >        for (Integer v2 : new Integer[] { 2, null }) {
2304 >
2305 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2306 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2307 >        final FailingConsumer r = new FailingConsumer();
2308 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2309 >
2310 >        g.complete(v2);
2311 >        checkCompletedWithWrappedCFException(h);
2312 >        f.complete(v1);
2313 >        checkCompletedNormally(f, v1);
2314 >        checkCompletedNormally(g, v2);
2315 >        }
2316      }
2317  
2318      /**
2319       * acceptEither result completes exceptionally if either source cancelled
2320       */
2321 <    public void testAcceptEither4() {
2322 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2323 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2324 <        IncAction r = new IncAction();
2325 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2326 <        assertTrue(f.cancel(true));
2327 <        checkCompletedWithWrappedCancellationException(g);
2328 <        f = new CompletableFuture<>();
2329 <        f2 = new CompletableFuture<>();
2330 <        assertTrue(f2.cancel(true));
2331 <        checkCompletedWithWrappedCancellationException(g);
2321 >    public void testAcceptEither_sourceCancelled1() {
2322 >        for (ExecutionMode m : ExecutionMode.values())
2323 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2324 >        for (Integer v1 : new Integer[] { 1, null }) {
2325 >
2326 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2327 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2328 >        final IncAction r = new IncAction();
2329 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2330 >
2331 >        assertTrue(f.cancel(mayInterruptIfRunning));
2332 >        checkCompletedWithWrappedCancellationException(h);
2333 >        g.complete(v1);
2334 >
2335 >        checkCancelled(f);
2336 >        assertEquals(r.invocationCount, 0);
2337 >        checkCompletedNormally(g, v1);
2338 >        checkCompletedWithWrappedCancellationException(h);
2339 >        }
2340 >    }
2341 >
2342 >    public void testAcceptEither_sourceCancelled2() {
2343 >        for (ExecutionMode m : ExecutionMode.values())
2344 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2345 >        for (Integer v1 : new Integer[] { 1, null }) {
2346 >
2347 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2348 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2349 >        final IncAction r = new IncAction();
2350 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2351 >
2352 >        assertTrue(g.cancel(mayInterruptIfRunning));
2353 >        checkCompletedWithWrappedCancellationException(h);
2354 >        f.complete(v1);
2355 >
2356 >        checkCancelled(g);
2357 >        assertEquals(r.invocationCount, 0);
2358 >        checkCompletedNormally(f, v1);
2359 >        checkCompletedWithWrappedCancellationException(h);
2360 >        }
2361 >    }
2362 >
2363 >    public void testAcceptEither_sourceCancelled3() {
2364 >        for (ExecutionMode m : ExecutionMode.values())
2365 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2366 >        for (Integer v1 : new Integer[] { 1, null }) {
2367 >
2368 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2369 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2370 >        final IncAction r = new IncAction();
2371 >
2372 >        assertTrue(g.cancel(mayInterruptIfRunning));
2373 >        f.complete(v1);
2374 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2375 >
2376 >        // unspecified behavior
2377 >        Integer v;
2378 >        try {
2379 >            assertEquals(h.join(), null);
2380 >            assertEquals(r.invocationCount, 1);
2381 >            assertEquals(inc(v1), r.value);
2382 >        } catch (CompletionException ok) {
2383 >            checkCompletedWithWrappedCancellationException(h);
2384 >            assertEquals(r.invocationCount, 0);
2385 >        }
2386 >
2387 >        checkCancelled(g);
2388 >        checkCompletedNormally(f, v1);
2389 >        }
2390 >    }
2391 >
2392 >    public void testAcceptEither_sourceCancelled4() {
2393 >        for (ExecutionMode m : ExecutionMode.values())
2394 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2395 >        for (Integer v1 : new Integer[] { 1, null }) {
2396 >
2397 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2398 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2399 >        final IncAction r = new IncAction();
2400 >
2401 >        assertTrue(f.cancel(mayInterruptIfRunning));
2402 >        g.complete(v1);
2403 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2404 >
2405 >        // unspecified behavior
2406 >        Integer v;
2407 >        try {
2408 >            assertEquals(h.join(), null);
2409 >            assertEquals(r.invocationCount, 1);
2410 >            assertEquals(inc(v1), r.value);
2411 >        } catch (CompletionException ok) {
2412 >            checkCompletedWithWrappedCancellationException(h);
2413 >            assertEquals(r.invocationCount, 0);
2414 >        }
2415 >
2416 >        checkCancelled(f);
2417 >        checkCompletedNormally(g, v1);
2418 >        }
2419      }
2420  
2421      /**
2422       * runAfterEither result completes normally after normal completion
2423       * of either source
2424       */
2425 <    public void testRunAfterEither() {
2426 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2427 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2428 <        Noop r = new Noop();
2232 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2233 <        f.complete(one);
2234 <        checkCompletedNormally(g, null);
2235 <        f2.complete(one);
2236 <        checkCompletedNormally(g, null);
2237 <        assertTrue(r.ran);
2425 >    public void testRunAfterEither_normalCompletion1() {
2426 >        for (ExecutionMode m : ExecutionMode.values())
2427 >        for (Integer v1 : new Integer[] { 1, null })
2428 >        for (Integer v2 : new Integer[] { 2, null }) {
2429  
2430 <        r = new Noop();
2431 <        f = new CompletableFuture<>();
2432 <        f.complete(one);
2433 <        f2 = new CompletableFuture<>();
2434 <        g = f.runAfterEither(f2, r);
2435 <        checkCompletedNormally(g, null);
2436 <        assertTrue(r.ran);
2430 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2431 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2432 >        final Noop r = new Noop();
2433 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2434 >
2435 >        f.complete(v1);
2436 >        checkCompletedNormally(h, null);
2437 >        assertEquals(r.invocationCount, 1);
2438 >        g.complete(v2);
2439 >
2440 >        checkCompletedNormally(f, v1);
2441 >        checkCompletedNormally(g, v2);
2442 >        checkCompletedNormally(h, null);
2443 >        assertEquals(r.invocationCount, 1);
2444 >        }
2445 >    }
2446 >
2447 >    public void testRunAfterEither_normalCompletion2() {
2448 >        for (ExecutionMode m : ExecutionMode.values())
2449 >        for (Integer v1 : new Integer[] { 1, null })
2450 >        for (Integer v2 : new Integer[] { 2, null }) {
2451 >
2452 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2453 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2454 >        final Noop r = new Noop();
2455 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456 >
2457 >        g.complete(v2);
2458 >        checkCompletedNormally(h, null);
2459 >        assertEquals(r.invocationCount, 1);
2460 >        f.complete(v1);
2461 >
2462 >        checkCompletedNormally(f, v1);
2463 >        checkCompletedNormally(g, v2);
2464 >        checkCompletedNormally(h, null);
2465 >        assertEquals(r.invocationCount, 1);
2466 >        }
2467 >    }
2468 >    public void testRunAfterEither_normalCompletion3() {
2469 >        for (ExecutionMode m : ExecutionMode.values())
2470 >        for (Integer v1 : new Integer[] { 1, null })
2471 >        for (Integer v2 : new Integer[] { 2, null }) {
2472 >
2473 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2474 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2475 >        final Noop r = new Noop();
2476 >
2477 >        f.complete(v1);
2478 >        g.complete(v2);
2479 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2480 >
2481 >        checkCompletedNormally(h, null);
2482 >        checkCompletedNormally(f, v1);
2483 >        checkCompletedNormally(g, v2);
2484 >        assertEquals(r.invocationCount, 1);
2485 >        }
2486      }
2487  
2488      /**
2489       * runAfterEither result completes exceptionally after exceptional
2490       * completion of either source
2491       */
2492 <    public void testRunAfterEither2() {
2493 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2494 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2255 <        Noop r = new Noop();
2256 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2257 <        f.completeExceptionally(new CFException());
2258 <        f2.complete(one);
2259 <        checkCompletedWithWrappedCFException(g);
2492 >    public void testRunAfterEither_exceptionalCompletion1() {
2493 >        for (ExecutionMode m : ExecutionMode.values())
2494 >        for (Integer v1 : new Integer[] { 1, null }) {
2495  
2496 <        r = new Noop();
2497 <        f = new CompletableFuture<>();
2498 <        f2 = new CompletableFuture<>();
2499 <        f2.completeExceptionally(new CFException());
2500 <        g = f.runAfterEither(f2, r);
2501 <        checkCompletedWithWrappedCFException(g);
2496 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2497 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2498 >        final Noop r = new Noop();
2499 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2500 >        final CFException ex = new CFException();
2501 >
2502 >        f.completeExceptionally(ex);
2503 >        checkCompletedWithWrappedCFException(h, ex);
2504 >        g.complete(v1);
2505 >
2506 >        assertEquals(r.invocationCount, 0);
2507 >        checkCompletedNormally(g, v1);
2508 >        checkCompletedWithWrappedCFException(f, ex);
2509 >        checkCompletedWithWrappedCFException(h, ex);
2510 >        }
2511 >    }
2512 >
2513 >    public void testRunAfterEither_exceptionalCompletion2() {
2514 >        for (ExecutionMode m : ExecutionMode.values())
2515 >        for (Integer v1 : new Integer[] { 1, null }) {
2516 >
2517 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2518 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2519 >        final Noop r = new Noop();
2520 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2521 >        final CFException ex = new CFException();
2522 >
2523 >        g.completeExceptionally(ex);
2524 >        checkCompletedWithWrappedCFException(h, ex);
2525 >        f.complete(v1);
2526 >
2527 >        assertEquals(r.invocationCount, 0);
2528 >        checkCompletedNormally(f, v1);
2529 >        checkCompletedWithWrappedCFException(g, ex);
2530 >        checkCompletedWithWrappedCFException(h, ex);
2531 >        }
2532 >    }
2533 >
2534 >    public void testRunAfterEither_exceptionalCompletion3() {
2535 >        for (ExecutionMode m : ExecutionMode.values())
2536 >        for (Integer v1 : new Integer[] { 1, null }) {
2537 >
2538 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2539 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2540 >        final Noop r = new Noop();
2541 >        final CFException ex = new CFException();
2542 >
2543 >        g.completeExceptionally(ex);
2544 >        f.complete(v1);
2545 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2546 >
2547 >        // unspecified behavior
2548 >        Integer v;
2549 >        try {
2550 >            assertEquals(h.join(), null);
2551 >            assertEquals(r.invocationCount, 1);
2552 >        } catch (CompletionException ok) {
2553 >            checkCompletedWithWrappedCFException(h, ex);
2554 >            assertEquals(r.invocationCount, 0);
2555 >        }
2556 >
2557 >        checkCompletedWithWrappedCFException(g, ex);
2558 >        checkCompletedNormally(f, v1);
2559 >        }
2560 >    }
2561 >
2562 >    public void testRunAfterEither_exceptionalCompletion4() {
2563 >        for (ExecutionMode m : ExecutionMode.values())
2564 >        for (Integer v1 : new Integer[] { 1, null }) {
2565 >
2566 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2567 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2568 >        final Noop r = new Noop();
2569 >        final CFException ex = new CFException();
2570 >
2571 >        f.completeExceptionally(ex);
2572 >        g.complete(v1);
2573 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2574 >
2575 >        // unspecified behavior
2576 >        Integer v;
2577 >        try {
2578 >            assertEquals(h.join(), null);
2579 >            assertEquals(r.invocationCount, 1);
2580 >        } catch (CompletionException ok) {
2581 >            checkCompletedWithWrappedCFException(h, ex);
2582 >            assertEquals(r.invocationCount, 0);
2583 >        }
2584 >
2585 >        checkCompletedWithWrappedCFException(f, ex);
2586 >        checkCompletedNormally(g, v1);
2587 >        }
2588      }
2589  
2590      /**
2591       * runAfterEither result completes exceptionally if action does
2592       */
2593 <    public void testRunAfterEither3() {
2594 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2595 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2596 <        FailingNoop r = new FailingNoop();
2597 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2598 <        f2.complete(two);
2599 <        checkCompletedWithWrappedCFException(g);
2593 >    public void testRunAfterEither_actionFailed1() {
2594 >        for (ExecutionMode m : ExecutionMode.values())
2595 >        for (Integer v1 : new Integer[] { 1, null })
2596 >        for (Integer v2 : new Integer[] { 2, null }) {
2597 >
2598 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2599 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2600 >        final FailingNoop r = new FailingNoop();
2601 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2602 >
2603 >        f.complete(v1);
2604 >        checkCompletedWithWrappedCFException(h);
2605 >        g.complete(v2);
2606 >        checkCompletedNormally(f, v1);
2607 >        checkCompletedNormally(g, v2);
2608 >        }
2609 >    }
2610 >
2611 >    public void testRunAfterEither_actionFailed2() {
2612 >        for (ExecutionMode m : ExecutionMode.values())
2613 >        for (Integer v1 : new Integer[] { 1, null })
2614 >        for (Integer v2 : new Integer[] { 2, null }) {
2615 >
2616 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2617 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2618 >        final FailingNoop r = new FailingNoop();
2619 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2620 >
2621 >        g.complete(v2);
2622 >        checkCompletedWithWrappedCFException(h);
2623 >        f.complete(v1);
2624 >        checkCompletedNormally(f, v1);
2625 >        checkCompletedNormally(g, v2);
2626 >        }
2627      }
2628  
2629      /**
2630       * runAfterEither result completes exceptionally if either source cancelled
2631       */
2632 <    public void testRunAfterEither4() {
2633 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2634 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2635 <        Noop r = new Noop();
2636 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2637 <        assertTrue(f.cancel(true));
2638 <        checkCompletedWithWrappedCancellationException(g);
2639 <        f = new CompletableFuture<>();
2640 <        f2 = new CompletableFuture<>();
2641 <        assertTrue(f2.cancel(true));
2642 <        checkCompletedWithWrappedCancellationException(g);
2632 >    public void testRunAfterEither_sourceCancelled1() {
2633 >        for (ExecutionMode m : ExecutionMode.values())
2634 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2635 >        for (Integer v1 : new Integer[] { 1, null }) {
2636 >
2637 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2638 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2639 >        final Noop r = new Noop();
2640 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2641 >
2642 >        assertTrue(f.cancel(mayInterruptIfRunning));
2643 >        checkCompletedWithWrappedCancellationException(h);
2644 >        g.complete(v1);
2645 >
2646 >        checkCancelled(f);
2647 >        assertEquals(r.invocationCount, 0);
2648 >        checkCompletedNormally(g, v1);
2649 >        checkCompletedWithWrappedCancellationException(h);
2650 >        }
2651 >    }
2652 >
2653 >    public void testRunAfterEither_sourceCancelled2() {
2654 >        for (ExecutionMode m : ExecutionMode.values())
2655 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2656 >        for (Integer v1 : new Integer[] { 1, null }) {
2657 >
2658 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2659 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2660 >        final Noop r = new Noop();
2661 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2662 >
2663 >        assertTrue(g.cancel(mayInterruptIfRunning));
2664 >        checkCompletedWithWrappedCancellationException(h);
2665 >        f.complete(v1);
2666 >
2667 >        checkCancelled(g);
2668 >        assertEquals(r.invocationCount, 0);
2669 >        checkCompletedNormally(f, v1);
2670 >        checkCompletedWithWrappedCancellationException(h);
2671 >        }
2672 >    }
2673 >
2674 >    public void testRunAfterEither_sourceCancelled3() {
2675 >        for (ExecutionMode m : ExecutionMode.values())
2676 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2677 >        for (Integer v1 : new Integer[] { 1, null }) {
2678 >
2679 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2680 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2681 >        final Noop r = new Noop();
2682 >
2683 >        assertTrue(g.cancel(mayInterruptIfRunning));
2684 >        f.complete(v1);
2685 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2686 >
2687 >        // unspecified behavior
2688 >        Integer v;
2689 >        try {
2690 >            assertEquals(h.join(), null);
2691 >            assertEquals(r.invocationCount, 1);
2692 >        } catch (CompletionException ok) {
2693 >            checkCompletedWithWrappedCancellationException(h);
2694 >            assertEquals(r.invocationCount, 0);
2695 >        }
2696 >
2697 >        checkCancelled(g);
2698 >        checkCompletedNormally(f, v1);
2699 >        }
2700 >    }
2701 >
2702 >    public void testRunAfterEither_sourceCancelled4() {
2703 >        for (ExecutionMode m : ExecutionMode.values())
2704 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2705 >        for (Integer v1 : new Integer[] { 1, null }) {
2706 >
2707 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2708 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2709 >        final Noop r = new Noop();
2710 >
2711 >        assertTrue(f.cancel(mayInterruptIfRunning));
2712 >        g.complete(v1);
2713 >        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2714 >
2715 >        // unspecified behavior
2716 >        Integer v;
2717 >        try {
2718 >            assertEquals(h.join(), null);
2719 >            assertEquals(r.invocationCount, 1);
2720 >        } catch (CompletionException ok) {
2721 >            checkCompletedWithWrappedCancellationException(h);
2722 >            assertEquals(r.invocationCount, 0);
2723 >        }
2724 >
2725 >        checkCancelled(f);
2726 >        checkCompletedNormally(g, v1);
2727 >        }
2728      }
2729  
2730      /**
# Line 2517 | Line 2950 | public class CompletableFutureTest exten
2950      }
2951  
2952      /**
2520     * acceptEitherAsync result completes normally after normal
2521     * completion of sources
2522     */
2523    public void testAcceptEitherAsync() {
2524        CompletableFuture<Integer> f = new CompletableFuture<>();
2525        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2526        IncAction r = new IncAction();
2527        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2528        f.complete(one);
2529        checkCompletedNormally(g, null);
2530        assertEquals(r.value, (Integer) 2);
2531
2532        r = new IncAction();
2533        f = new CompletableFuture<>();
2534        f.complete(one);
2535        f2 = new CompletableFuture<>();
2536        g = f.acceptEitherAsync(f2, r);
2537        checkCompletedNormally(g, null);
2538        assertEquals(r.value, (Integer) 2);
2539    }
2540
2541    /**
2542     * acceptEitherAsync result completes exceptionally after exceptional
2543     * completion of source
2544     */
2545    public void testAcceptEitherAsync2() {
2546        CompletableFuture<Integer> f = new CompletableFuture<>();
2547        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2548        IncAction r = new IncAction();
2549        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2550        f.completeExceptionally(new CFException());
2551        checkCompletedWithWrappedCFException(g);
2552
2553        r = new IncAction();
2554        f = new CompletableFuture<>();
2555        f2 = new CompletableFuture<>();
2556        f2.completeExceptionally(new CFException());
2557        g = f.acceptEitherAsync(f2, r);
2558        f.complete(one);
2559        checkCompletedWithWrappedCFException(g);
2560    }
2561
2562    /**
2563     * acceptEitherAsync result completes exceptionally if action does
2564     */
2565    public void testAcceptEitherAsync3() {
2566        CompletableFuture<Integer> f = new CompletableFuture<>();
2567        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2568        FailingConsumer r = new FailingConsumer();
2569        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2570        f.complete(one);
2571        checkCompletedWithWrappedCFException(g);
2572    }
2573
2574    /**
2575     * acceptEitherAsync result completes exceptionally if either
2576     * source cancelled
2577     */
2578    public void testAcceptEitherAsync4() {
2579        CompletableFuture<Integer> f = new CompletableFuture<>();
2580        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2581        IncAction r = new IncAction();
2582        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2583        assertTrue(f.cancel(true));
2584        checkCompletedWithWrappedCancellationException(g);
2585
2586        r = new IncAction();
2587        f = new CompletableFuture<>();
2588        f2 = new CompletableFuture<>();
2589        assertTrue(f2.cancel(true));
2590        g = f.acceptEitherAsync(f2, r);
2591        checkCompletedWithWrappedCancellationException(g);
2592    }
2593
2594    /**
2595     * runAfterEitherAsync result completes normally after normal
2596     * completion of sources
2597     */
2598    public void testRunAfterEitherAsync() {
2599        CompletableFuture<Integer> f = new CompletableFuture<>();
2600        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2601        Noop r = new Noop();
2602        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2603        f.complete(one);
2604        checkCompletedNormally(g, null);
2605        assertTrue(r.ran);
2606
2607        r = new Noop();
2608        f = new CompletableFuture<>();
2609        f.complete(one);
2610        f2 = new CompletableFuture<>();
2611        g = f.runAfterEitherAsync(f2, r);
2612        checkCompletedNormally(g, null);
2613        assertTrue(r.ran);
2614    }
2615
2616    /**
2617     * runAfterEitherAsync result completes exceptionally after exceptional
2618     * completion of source
2619     */
2620    public void testRunAfterEitherAsync2() {
2621        CompletableFuture<Integer> f = new CompletableFuture<>();
2622        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2623        Noop r = new Noop();
2624        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2625        f.completeExceptionally(new CFException());
2626        checkCompletedWithWrappedCFException(g);
2627
2628        r = new Noop();
2629        f = new CompletableFuture<>();
2630        f2 = new CompletableFuture<>();
2631        f2.completeExceptionally(new CFException());
2632        g = f.runAfterEitherAsync(f2, r);
2633        f.complete(one);
2634        checkCompletedWithWrappedCFException(g);
2635    }
2636
2637    /**
2638     * runAfterEitherAsync result completes exceptionally if action does
2639     */
2640    public void testRunAfterEitherAsync3() {
2641        CompletableFuture<Integer> f = new CompletableFuture<>();
2642        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2643        FailingNoop r = new FailingNoop();
2644        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2645        f.complete(one);
2646        checkCompletedWithWrappedCFException(g);
2647    }
2648
2649    /**
2650     * runAfterEitherAsync result completes exceptionally if either
2651     * source cancelled
2652     */
2653    public void testRunAfterEitherAsync4() {
2654        CompletableFuture<Integer> f = new CompletableFuture<>();
2655        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2656        Noop r = new Noop();
2657        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2658        assertTrue(f.cancel(true));
2659        checkCompletedWithWrappedCancellationException(g);
2660
2661        r = new Noop();
2662        f = new CompletableFuture<>();
2663        f2 = new CompletableFuture<>();
2664        assertTrue(f2.cancel(true));
2665        g = f.runAfterEitherAsync(f2, r);
2666        checkCompletedWithWrappedCancellationException(g);
2667    }
2668
2669    /**
2953       * thenComposeAsync result completes normally after normal
2954       * completion of source
2955       */
# Line 2890 | Line 3173 | public class CompletableFutureTest exten
3173      }
3174  
3175      /**
2893     * acceptEitherAsync result completes normally after normal
2894     * completion of sources
2895     */
2896    public void testAcceptEitherAsyncE() {
2897        CompletableFuture<Integer> f = new CompletableFuture<>();
2898        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2899        IncAction r = new IncAction();
2900        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2901        f.complete(one);
2902        checkCompletedNormally(g, null);
2903        assertEquals(r.value, (Integer) 2);
2904
2905        r = new IncAction();
2906        f = new CompletableFuture<>();
2907        f.complete(one);
2908        f2 = new CompletableFuture<>();
2909        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2910        checkCompletedNormally(g, null);
2911        assertEquals(r.value, (Integer) 2);
2912    }
2913
2914    /**
2915     * acceptEitherAsync result completes exceptionally after exceptional
2916     * completion of source
2917     */
2918    public void testAcceptEitherAsync2E() {
2919        CompletableFuture<Integer> f = new CompletableFuture<>();
2920        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2921        IncAction r = new IncAction();
2922        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2923        f.completeExceptionally(new CFException());
2924        checkCompletedWithWrappedCFException(g);
2925
2926        r = new IncAction();
2927        f = new CompletableFuture<>();
2928        f2 = new CompletableFuture<>();
2929        f2.completeExceptionally(new CFException());
2930        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2931        f.complete(one);
2932        checkCompletedWithWrappedCFException(g);
2933    }
2934
2935    /**
2936     * acceptEitherAsync result completes exceptionally if action does
2937     */
2938    public void testAcceptEitherAsync3E() {
2939        CompletableFuture<Integer> f = new CompletableFuture<>();
2940        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2941        FailingConsumer r = new FailingConsumer();
2942        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2943        f.complete(one);
2944        checkCompletedWithWrappedCFException(g);
2945    }
2946
2947    /**
2948     * acceptEitherAsync result completes exceptionally if either
2949     * source cancelled
2950     */
2951    public void testAcceptEitherAsync4E() {
2952        CompletableFuture<Integer> f = new CompletableFuture<>();
2953        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2954        IncAction r = new IncAction();
2955        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2956        assertTrue(f.cancel(true));
2957        checkCompletedWithWrappedCancellationException(g);
2958
2959        r = new IncAction();
2960        f = new CompletableFuture<>();
2961        f2 = new CompletableFuture<>();
2962        assertTrue(f2.cancel(true));
2963        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2964        checkCompletedWithWrappedCancellationException(g);
2965    }
2966
2967    /**
2968     * runAfterEitherAsync result completes normally after normal
2969     * completion of sources
2970     */
2971    public void testRunAfterEitherAsyncE() {
2972        CompletableFuture<Integer> f = new CompletableFuture<>();
2973        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2974        Noop r = new Noop();
2975        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2976        f.complete(one);
2977        checkCompletedNormally(g, null);
2978        assertTrue(r.ran);
2979
2980        r = new Noop();
2981        f = new CompletableFuture<>();
2982        f.complete(one);
2983        f2 = new CompletableFuture<>();
2984        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2985        checkCompletedNormally(g, null);
2986        assertTrue(r.ran);
2987    }
2988
2989    /**
2990     * runAfterEitherAsync result completes exceptionally after exceptional
2991     * completion of source
2992     */
2993    public void testRunAfterEitherAsync2E() {
2994        CompletableFuture<Integer> f = new CompletableFuture<>();
2995        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2996        Noop r = new Noop();
2997        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2998        f.completeExceptionally(new CFException());
2999        checkCompletedWithWrappedCFException(g);
3000
3001        r = new Noop();
3002        f = new CompletableFuture<>();
3003        f2 = new CompletableFuture<>();
3004        f2.completeExceptionally(new CFException());
3005        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3006        f.complete(one);
3007        checkCompletedWithWrappedCFException(g);
3008    }
3009
3010    /**
3011     * runAfterEitherAsync result completes exceptionally if action does
3012     */
3013    public void testRunAfterEitherAsync3E() {
3014        CompletableFuture<Integer> f = new CompletableFuture<>();
3015        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3016        FailingNoop r = new FailingNoop();
3017        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3018        f.complete(one);
3019        checkCompletedWithWrappedCFException(g);
3020    }
3021
3022    /**
3023     * runAfterEitherAsync result completes exceptionally if either
3024     * source cancelled
3025     */
3026    public void testRunAfterEitherAsync4E() {
3027        CompletableFuture<Integer> f = new CompletableFuture<>();
3028        CompletableFuture<Integer> f2 = new CompletableFuture<>();
3029        Noop r = new Noop();
3030        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3031        assertTrue(f.cancel(true));
3032        checkCompletedWithWrappedCancellationException(g);
3033
3034        r = new Noop();
3035        f = new CompletableFuture<>();
3036        f2 = new CompletableFuture<>();
3037        assertTrue(f2.cancel(true));
3038        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
3039        checkCompletedWithWrappedCancellationException(g);
3040    }
3041
3042    /**
3176       * thenComposeAsync result completes normally after normal
3177       * completion of source
3178       */
# Line 3283 | Line 3416 | public class CompletableFutureTest exten
3416       * whenComplete action executes on normal completion, propagating
3417       * source result.
3418       */
3419 <    public void testWhenComplete1() {
3419 >    public void testWhenComplete_normalCompletion1() {
3420 >        for (ExecutionMode m : ExecutionMode.values())
3421 >        for (Integer v1 : new Integer[] { 1, null }) {
3422 >
3423          final AtomicInteger a = new AtomicInteger();
3424 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3425 <        CompletableFuture<Integer> g =
3426 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3427 <        f.complete(three);
3428 <        checkCompletedNormally(f, three);
3429 <        checkCompletedNormally(g, three);
3424 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3425 >        final CompletableFuture<Integer> g =
3426 >            m.whenComplete(f,
3427 >                           (Integer x, Throwable t) -> {
3428 >                               threadAssertSame(x, v1);
3429 >                               threadAssertNull(t);
3430 >                               a.getAndIncrement();
3431 >                           });
3432 >        f.complete(v1);
3433 >        checkCompletedNormally(f, v1);
3434 >        checkCompletedNormally(g, v1);
3435          assertEquals(a.get(), 1);
3436 +        }
3437      }
3438  
3439      /**
3440       * whenComplete action executes on exceptional completion, propagating
3441       * source result.
3442       */
3443 <    public void testWhenComplete2() {
3443 >    public void testWhenComplete_exceptionalCompletion() {
3444 >        for (ExecutionMode m : ExecutionMode.values())
3445 >        for (Integer v1 : new Integer[] { 1, null }) {
3446 >
3447          final AtomicInteger a = new AtomicInteger();
3448 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3449 <        CompletableFuture<Integer> g =
3450 <            f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement());
3451 <        f.completeExceptionally(new CFException());
3452 <        assertTrue(f.isCompletedExceptionally());
3453 <        assertTrue(g.isCompletedExceptionally());
3448 >        final CFException ex = new CFException();
3449 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3450 >        final CompletableFuture<Integer> g = m.whenComplete
3451 >            (f,
3452 >             (Integer x, Throwable t) -> {
3453 >                threadAssertNull(x);
3454 >                threadAssertSame(t, ex);
3455 >                a.getAndIncrement();
3456 >            });
3457 >        f.completeExceptionally(ex);
3458 >        checkCompletedWithWrappedCFException(f, ex);
3459 >        checkCompletedWithWrappedCFException(g, ex);
3460          assertEquals(a.get(), 1);
3461 +        }
3462      }
3463  
3464      /**
3465       * If a whenComplete action throws an exception when triggered by
3466       * a normal completion, it completes exceptionally
3467       */
3468 <    public void testWhenComplete3() {
3469 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3470 <        CompletableFuture<Integer> g =
3319 <            f.whenComplete((Integer x, Throwable t) ->
3320 <                           { throw new CFException(); } );
3321 <        f.complete(three);
3322 <        checkCompletedNormally(f, three);
3323 <        assertTrue(g.isCompletedExceptionally());
3324 <        checkCompletedWithWrappedCFException(g);
3325 <    }
3326 <
3327 <    /**
3328 <     * whenCompleteAsync action executes on normal completion, propagating
3329 <     * source result.
3330 <     */
3331 <    public void testWhenCompleteAsync1() {
3332 <        final AtomicInteger a = new AtomicInteger();
3333 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3334 <        CompletableFuture<Integer> g =
3335 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3336 <        f.complete(three);
3337 <        checkCompletedNormally(f, three);
3338 <        checkCompletedNormally(g, three);
3339 <        assertEquals(a.get(), 1);
3340 <    }
3341 <
3342 <    /**
3343 <     * whenCompleteAsync action executes on exceptional completion, propagating
3344 <     * source result.
3345 <     */
3346 <    public void testWhenCompleteAsync2() {
3347 <        final AtomicInteger a = new AtomicInteger();
3348 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3349 <        CompletableFuture<Integer> g =
3350 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3351 <        f.completeExceptionally(new CFException());
3352 <        checkCompletedWithWrappedCFException(f);
3353 <        checkCompletedWithWrappedCFException(g);
3354 <    }
3355 <
3356 <    /**
3357 <     * If a whenCompleteAsync action throws an exception when
3358 <     * triggered by a normal completion, it completes exceptionally
3359 <     */
3360 <    public void testWhenCompleteAsync3() {
3361 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3362 <        CompletableFuture<Integer> g =
3363 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3364 <                           { throw new CFException(); } );
3365 <        f.complete(three);
3366 <        checkCompletedNormally(f, three);
3367 <        checkCompletedWithWrappedCFException(g);
3368 <    }
3468 >    public void testWhenComplete_actionFailed() {
3469 >        for (ExecutionMode m : ExecutionMode.values())
3470 >        for (Integer v1 : new Integer[] { 1, null }) {
3471  
3472 <    /**
3473 <     * whenCompleteAsync action executes on normal completion, propagating
3474 <     * source result.
3475 <     */
3476 <    public void testWhenCompleteAsync1e() {
3477 <        final AtomicInteger a = new AtomicInteger();
3478 <        ThreadExecutor exec = new ThreadExecutor();
3479 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3480 <        CompletableFuture<Integer> g =
3481 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3482 <                                exec);
3483 <        f.complete(three);
3484 <        checkCompletedNormally(f, three);
3383 <        checkCompletedNormally(g, three);
3384 <        assertEquals(a.get(), 1);
3472 >        final CFException ex = new CFException();
3473 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3474 >        final CompletableFuture<Integer> g = m.whenComplete
3475 >            (f,
3476 >             (Integer x, Throwable t) -> {
3477 >                threadAssertSame(x, v1);
3478 >                threadAssertNull(t);
3479 >                throw ex;
3480 >            });
3481 >        f.complete(v1);
3482 >        checkCompletedNormally(f, v1);
3483 >        checkCompletedWithWrappedCFException(g, ex);
3484 >        }
3485      }
3486  
3487      /**
3488 <     * whenCompleteAsync action executes on exceptional completion, propagating
3489 <     * source result.
3488 >     * If a whenComplete action throws an exception when triggered by
3489 >     * a source completion that also throws an exception, the source
3490 >     * exception takes precedence.
3491       */
3492 <    public void testWhenCompleteAsync2e() {
3493 <        final AtomicInteger a = new AtomicInteger();
3494 <        ThreadExecutor exec = new ThreadExecutor();
3394 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3395 <        CompletableFuture<Integer> g =
3396 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3397 <                                exec);
3398 <        f.completeExceptionally(new CFException());
3399 <        checkCompletedWithWrappedCFException(f);
3400 <        checkCompletedWithWrappedCFException(g);
3401 <    }
3492 >    public void testWhenComplete_actionFailedSourceFailed() {
3493 >        for (ExecutionMode m : ExecutionMode.values())
3494 >        for (Integer v1 : new Integer[] { 1, null }) {
3495  
3496 <    /**
3497 <     * If a whenCompleteAsync action throws an exception when triggered
3498 <     * by a normal completion, it completes exceptionally
3499 <     */
3500 <    public void testWhenCompleteAsync3e() {
3501 <        ThreadExecutor exec = new ThreadExecutor();
3502 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 <        CompletableFuture<Integer> g =
3504 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3505 <                                { throw new CFException(); },
3506 <                                exec);
3507 <        f.complete(three);
3508 <        checkCompletedNormally(f, three);
3509 <        checkCompletedWithWrappedCFException(g);
3496 >        final CFException ex1 = new CFException();
3497 >        final CFException ex2 = new CFException();
3498 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
3499 >        final CompletableFuture<Integer> g = m.whenComplete
3500 >            (f,
3501 >             (Integer x, Throwable t) -> {
3502 >                threadAssertSame(t, ex1);
3503 >                threadAssertNull(x);
3504 >                throw ex2;
3505 >            });
3506 >        f.completeExceptionally(ex1);
3507 >        checkCompletedWithWrappedCFException(f, ex1);
3508 >        checkCompletedWithWrappedCFException(g, ex1);
3509 >        }
3510      }
3511  
3512      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines