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.38 by jsr166, Sun Jun 1 23:51:44 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);
959 >        CompletableFuture<Integer> h = null;
960 >        if (createdIncomplete) h = m.thenCombine(f, g, r);
961  
962 <        f.complete(v1);
963 <        checkIncomplete(h);
964 <        assertFalse(r.ran());
965 <        g.complete(v2);
966 <
967 <        checkCompletedNormally(h, subtract(v1, v2));
968 <        checkCompletedNormally(f, v1);
969 <        checkCompletedNormally(g, v2);
970 <        }
971 <    }
972 <
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();
995 <
996 <        g.complete(v2);
997 <        f.complete(v1);
998 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
999 <
1000 <        checkCompletedNormally(h, subtract(v1, v2));
1001 <        checkCompletedNormally(f, v1);
1002 <        checkCompletedNormally(g, v2);
1003 <        }
1004 <    }
1005 <
1006 <    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 1844 | Line 1800 | public class CompletableFutureTest exten
1800       * applyToEither result completes normally after normal completion
1801       * of either source
1802       */
1803 <    public void testApplyToEither() {
1804 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1805 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1806 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1851 <        f.complete(one);
1852 <        checkCompletedNormally(g, two);
1853 <        f2.complete(one);
1854 <        checkCompletedNormally(g, two);
1803 >    public void testApplyToEither_normalCompletion1() {
1804 >        for (ExecutionMode m : ExecutionMode.values())
1805 >        for (Integer v1 : new Integer[] { 1, null })
1806 >        for (Integer v2 : new Integer[] { 2, null }) {
1807  
1808 <        f = new CompletableFuture<>();
1809 <        f.complete(one);
1810 <        f2 = new CompletableFuture<>();
1811 <        g = f.applyToEither(f2, inc);
1812 <        checkCompletedNormally(g, two);
1808 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1809 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1810 >        final IncFunction r = new IncFunction();
1811 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1812 >
1813 >        f.complete(v1);
1814 >        checkCompletedNormally(h, inc(v1));
1815 >        g.complete(v2);
1816 >
1817 >        checkCompletedNormally(f, v1);
1818 >        checkCompletedNormally(g, v2);
1819 >        checkCompletedNormally(h, inc(v1));
1820 >        }
1821 >    }
1822 >
1823 >    public void testApplyToEither_normalCompletion2() {
1824 >        for (ExecutionMode m : ExecutionMode.values())
1825 >        for (Integer v1 : new Integer[] { 1, null })
1826 >        for (Integer v2 : new Integer[] { 2, null }) {
1827 >
1828 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1829 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1830 >        final IncFunction r = new IncFunction();
1831 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1832 >
1833 >        g.complete(v2);
1834 >        checkCompletedNormally(h, inc(v2));
1835 >        f.complete(v1);
1836 >
1837 >        checkCompletedNormally(f, v1);
1838 >        checkCompletedNormally(g, v2);
1839 >        checkCompletedNormally(h, inc(v2));
1840 >        }
1841 >    }
1842 >    public void testApplyToEither_normalCompletion3() {
1843 >        for (ExecutionMode m : ExecutionMode.values())
1844 >        for (Integer v1 : new Integer[] { 1, null })
1845 >        for (Integer v2 : new Integer[] { 2, null }) {
1846 >
1847 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1848 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1849 >        final IncFunction r = new IncFunction();
1850 >
1851 >        f.complete(v1);
1852 >        g.complete(v2);
1853 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1854 >
1855 >        checkCompletedNormally(f, v1);
1856 >        checkCompletedNormally(g, v2);
1857 >
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  
1865      /**
1866       * applyToEither result completes exceptionally after exceptional
1867       * completion of either source
1868       */
1869 <    public void testApplyToEither2() {
1870 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1871 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1870 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1871 <        f.completeExceptionally(new CFException());
1872 <        f2.complete(one);
1873 <        checkCompletedWithWrappedCFException(g);
1869 >    public void testApplyToEither_exceptionalCompletion1() {
1870 >        for (ExecutionMode m : ExecutionMode.values())
1871 >        for (Integer v1 : new Integer[] { 1, null }) {
1872  
1873 <        f = new CompletableFuture<>();
1874 <        f2 = new CompletableFuture<>();
1875 <        f2.completeExceptionally(new CFException());
1876 <        g = f.applyToEither(f2, inc);
1877 <        checkCompletedWithWrappedCFException(g);
1873 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1874 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1875 >        final IncFunction r = new IncFunction();
1876 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1877 >        final CFException ex = new CFException();
1878 >
1879 >        f.completeExceptionally(ex);
1880 >        checkCompletedWithWrappedCFException(h, ex);
1881 >        g.complete(v1);
1882 >
1883 >        assertEquals(r.invocationCount, 0);
1884 >        checkCompletedNormally(g, v1);
1885 >        checkCompletedWithWrappedCFException(f, ex);
1886 >        checkCompletedWithWrappedCFException(h, ex);
1887 >        }
1888 >    }
1889 >
1890 >    public void testApplyToEither_exceptionalCompletion2() {
1891 >        for (ExecutionMode m : ExecutionMode.values())
1892 >        for (Integer v1 : new Integer[] { 1, null }) {
1893 >
1894 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1895 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1896 >        final IncFunction r = new IncFunction();
1897 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1898 >        final CFException ex = new CFException();
1899 >
1900 >        g.completeExceptionally(ex);
1901 >        checkCompletedWithWrappedCFException(h, ex);
1902 >        f.complete(v1);
1903 >
1904 >        assertEquals(r.invocationCount, 0);
1905 >        checkCompletedNormally(f, v1);
1906 >        checkCompletedWithWrappedCFException(g, ex);
1907 >        checkCompletedWithWrappedCFException(h, ex);
1908 >        }
1909 >    }
1910 >
1911 >    public void testApplyToEither_exceptionalCompletion3() {
1912 >        for (ExecutionMode m : ExecutionMode.values())
1913 >        for (Integer v1 : new Integer[] { 1, null }) {
1914 >
1915 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1916 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1917 >        final IncFunction r = new IncFunction();
1918 >        final CFException ex = new CFException();
1919 >
1920 >        g.completeExceptionally(ex);
1921 >        f.complete(v1);
1922 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1923 >
1924 >        // unspecified behavior
1925 >        Integer v;
1926 >        try {
1927 >            assertEquals(h.join(), inc(v1));
1928 >            assertEquals(r.invocationCount, 1);
1929 >        } catch (CompletionException ok) {
1930 >            checkCompletedWithWrappedCFException(h, ex);
1931 >            assertEquals(r.invocationCount, 0);
1932 >        }
1933 >
1934 >        checkCompletedWithWrappedCFException(g, ex);
1935 >        checkCompletedNormally(f, v1);
1936 >        }
1937 >    }
1938 >
1939 >    public void testApplyToEither_exceptionalCompletion4() {
1940 >        for (ExecutionMode m : ExecutionMode.values())
1941 >        for (Integer v1 : new Integer[] { 1, null }) {
1942 >
1943 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1944 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1945 >        final IncFunction r = new IncFunction();
1946 >        final CFException ex = new CFException();
1947 >
1948 >        f.completeExceptionally(ex);
1949 >        g.complete(v1);
1950 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1951 >
1952 >        // unspecified behavior
1953 >        Integer v;
1954 >        try {
1955 >            assertEquals(h.join(), inc(v1));
1956 >            assertEquals(r.invocationCount, 1);
1957 >        } catch (CompletionException ok) {
1958 >            checkCompletedWithWrappedCFException(h, ex);
1959 >            assertEquals(r.invocationCount, 0);
1960 >        }
1961 >
1962 >        checkCompletedWithWrappedCFException(f, ex);
1963 >        checkCompletedNormally(g, v1);
1964 >        }
1965      }
1966  
1967      /**
1968       * applyToEither result completes exceptionally if action does
1969       */
1970 <    public void testApplyToEither3() {
1971 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1972 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1973 <        FailingFunction r = new FailingFunction();
1974 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
1975 <        f2.complete(two);
1976 <        checkCompletedWithWrappedCFException(g);
1970 >    public void testApplyToEither_actionFailed1() {
1971 >        for (ExecutionMode m : ExecutionMode.values())
1972 >        for (Integer v1 : new Integer[] { 1, null })
1973 >        for (Integer v2 : new Integer[] { 2, null }) {
1974 >
1975 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1976 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1977 >        final FailingFunction r = new FailingFunction();
1978 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1979 >
1980 >        f.complete(v1);
1981 >        checkCompletedWithWrappedCFException(h);
1982 >        g.complete(v2);
1983 >        checkCompletedNormally(f, v1);
1984 >        checkCompletedNormally(g, v2);
1985 >        }
1986 >    }
1987 >
1988 >    public void testApplyToEither_actionFailed2() {
1989 >        for (ExecutionMode m : ExecutionMode.values())
1990 >        for (Integer v1 : new Integer[] { 1, null })
1991 >        for (Integer v2 : new Integer[] { 2, null }) {
1992 >
1993 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1994 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1995 >        final FailingFunction r = new FailingFunction();
1996 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1997 >
1998 >        g.complete(v2);
1999 >        checkCompletedWithWrappedCFException(h);
2000 >        f.complete(v1);
2001 >        checkCompletedNormally(f, v1);
2002 >        checkCompletedNormally(g, v2);
2003 >        }
2004      }
2005  
2006      /**
2007       * applyToEither result completes exceptionally if either source cancelled
2008       */
2009 <    public void testApplyToEither4() {
2010 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2011 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2012 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2013 <        assertTrue(f.cancel(true));
2014 <        checkCompletedWithWrappedCancellationException(g);
2015 <        f = new CompletableFuture<>();
2016 <        f2 = new CompletableFuture<>();
2017 <        assertTrue(f2.cancel(true));
2018 <        checkCompletedWithWrappedCancellationException(g);
2009 >    public void testApplyToEither_sourceCancelled1() {
2010 >        for (ExecutionMode m : ExecutionMode.values())
2011 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2012 >        for (Integer v1 : new Integer[] { 1, null }) {
2013 >
2014 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2015 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2016 >        final IncFunction r = new IncFunction();
2017 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2018 >
2019 >        assertTrue(f.cancel(mayInterruptIfRunning));
2020 >        checkCompletedWithWrappedCancellationException(h);
2021 >        g.complete(v1);
2022 >
2023 >        checkCancelled(f);
2024 >        assertEquals(r.invocationCount, 0);
2025 >        checkCompletedNormally(g, v1);
2026 >        checkCompletedWithWrappedCancellationException(h);
2027 >        }
2028 >    }
2029 >
2030 >    public void testApplyToEither_sourceCancelled2() {
2031 >        for (ExecutionMode m : ExecutionMode.values())
2032 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2033 >        for (Integer v1 : new Integer[] { 1, null }) {
2034 >
2035 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2036 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2037 >        final IncFunction r = new IncFunction();
2038 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2039 >
2040 >        assertTrue(g.cancel(mayInterruptIfRunning));
2041 >        checkCompletedWithWrappedCancellationException(h);
2042 >        f.complete(v1);
2043 >
2044 >        checkCancelled(g);
2045 >        assertEquals(r.invocationCount, 0);
2046 >        checkCompletedNormally(f, v1);
2047 >        checkCompletedWithWrappedCancellationException(h);
2048 >        }
2049 >    }
2050 >
2051 >    public void testApplyToEither_sourceCancelled3() {
2052 >        for (ExecutionMode m : ExecutionMode.values())
2053 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2054 >        for (Integer v1 : new Integer[] { 1, null }) {
2055 >
2056 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2057 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2058 >        final IncFunction r = new IncFunction();
2059 >
2060 >        assertTrue(g.cancel(mayInterruptIfRunning));
2061 >        f.complete(v1);
2062 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2063 >
2064 >        // unspecified behavior
2065 >        Integer v;
2066 >        try {
2067 >            assertEquals(h.join(), inc(v1));
2068 >            assertEquals(r.invocationCount, 1);
2069 >        } catch (CompletionException ok) {
2070 >            checkCompletedWithWrappedCancellationException(h);
2071 >            assertEquals(r.invocationCount, 0);
2072 >        }
2073 >
2074 >        checkCancelled(g);
2075 >        checkCompletedNormally(f, v1);
2076 >        }
2077 >    }
2078 >
2079 >    public void testApplyToEither_sourceCancelled4() {
2080 >        for (ExecutionMode m : ExecutionMode.values())
2081 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2082 >        for (Integer v1 : new Integer[] { 1, null }) {
2083 >
2084 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2085 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2086 >        final IncFunction r = new IncFunction();
2087 >
2088 >        assertTrue(f.cancel(mayInterruptIfRunning));
2089 >        g.complete(v1);
2090 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2091 >
2092 >        // unspecified behavior
2093 >        Integer v;
2094 >        try {
2095 >            assertEquals(h.join(), inc(v1));
2096 >            assertEquals(r.invocationCount, 1);
2097 >        } catch (CompletionException ok) {
2098 >            checkCompletedWithWrappedCancellationException(h);
2099 >            assertEquals(r.invocationCount, 0);
2100 >        }
2101 >
2102 >        checkCancelled(f);
2103 >        checkCompletedNormally(g, v1);
2104 >        }
2105      }
2106  
2107      /**
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();
1917 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1918 <        f.complete(one);
1919 <        checkCompletedNormally(g, null);
1920 <        f2.complete(one);
1921 <        checkCompletedNormally(g, null);
1922 <        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<>();
1940 <        IncAction r = new IncAction();
1941 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
1942 <        f.completeExceptionally(new CFException());
1943 <        f2.complete(one);
1944 <        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();
1990 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
1991 <        f.complete(one);
1992 <        checkCompletedNormally(g, null);
1993 <        f2.complete(one);
1994 <        checkCompletedNormally(g, null);
1995 <        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<>();
2013 <        Noop r = new Noop();
2014 <        CompletableFuture<Void> g = f.runAfterEither(f2, r);
2015 <        f.completeExceptionally(new CFException());
2016 <        f2.complete(one);
2017 <        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 2275 | Line 2950 | public class CompletableFutureTest exten
2950      }
2951  
2952      /**
2278     * applyToEitherAsync result completes normally after normal
2279     * completion of sources
2280     */
2281    public void testApplyToEitherAsync() {
2282        CompletableFuture<Integer> f = new CompletableFuture<>();
2283        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2284        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2285        f.complete(one);
2286        checkCompletedNormally(g, two);
2287
2288        f = new CompletableFuture<>();
2289        f.complete(one);
2290        f2 = new CompletableFuture<>();
2291        g = f.applyToEitherAsync(f2, inc);
2292        checkCompletedNormally(g, two);
2293    }
2294
2295    /**
2296     * applyToEitherAsync result completes exceptionally after exceptional
2297     * completion of source
2298     */
2299    public void testApplyToEitherAsync2() {
2300        CompletableFuture<Integer> f = new CompletableFuture<>();
2301        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2302        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2303        f.completeExceptionally(new CFException());
2304        checkCompletedWithWrappedCFException(g);
2305
2306        f = new CompletableFuture<>();
2307        f2 = new CompletableFuture<>();
2308        f2.completeExceptionally(new CFException());
2309        g = f.applyToEitherAsync(f2, inc);
2310        f.complete(one);
2311        checkCompletedWithWrappedCFException(g);
2312    }
2313
2314    /**
2315     * applyToEitherAsync result completes exceptionally if action does
2316     */
2317    public void testApplyToEitherAsync3() {
2318        CompletableFuture<Integer> f = new CompletableFuture<>();
2319        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2320        FailingFunction r = new FailingFunction();
2321        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2322        f.complete(one);
2323        checkCompletedWithWrappedCFException(g);
2324    }
2325
2326    /**
2327     * applyToEitherAsync result completes exceptionally if either source cancelled
2328     */
2329    public void testApplyToEitherAsync4() {
2330        CompletableFuture<Integer> f = new CompletableFuture<>();
2331        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2332        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2333        assertTrue(f.cancel(true));
2334        checkCompletedWithWrappedCancellationException(g);
2335
2336        f = new CompletableFuture<>();
2337        f2 = new CompletableFuture<>();
2338        assertTrue(f2.cancel(true));
2339        g = f.applyToEitherAsync(f2, inc);
2340        checkCompletedWithWrappedCancellationException(g);
2341    }
2342
2343    /**
2344     * acceptEitherAsync result completes normally after normal
2345     * completion of sources
2346     */
2347    public void testAcceptEitherAsync() {
2348        CompletableFuture<Integer> f = new CompletableFuture<>();
2349        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2350        IncAction r = new IncAction();
2351        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2352        f.complete(one);
2353        checkCompletedNormally(g, null);
2354        assertEquals(r.value, (Integer) 2);
2355
2356        r = new IncAction();
2357        f = new CompletableFuture<>();
2358        f.complete(one);
2359        f2 = new CompletableFuture<>();
2360        g = f.acceptEitherAsync(f2, r);
2361        checkCompletedNormally(g, null);
2362        assertEquals(r.value, (Integer) 2);
2363    }
2364
2365    /**
2366     * acceptEitherAsync result completes exceptionally after exceptional
2367     * completion of source
2368     */
2369    public void testAcceptEitherAsync2() {
2370        CompletableFuture<Integer> f = new CompletableFuture<>();
2371        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2372        IncAction r = new IncAction();
2373        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2374        f.completeExceptionally(new CFException());
2375        checkCompletedWithWrappedCFException(g);
2376
2377        r = new IncAction();
2378        f = new CompletableFuture<>();
2379        f2 = new CompletableFuture<>();
2380        f2.completeExceptionally(new CFException());
2381        g = f.acceptEitherAsync(f2, r);
2382        f.complete(one);
2383        checkCompletedWithWrappedCFException(g);
2384    }
2385
2386    /**
2387     * acceptEitherAsync result completes exceptionally if action does
2388     */
2389    public void testAcceptEitherAsync3() {
2390        CompletableFuture<Integer> f = new CompletableFuture<>();
2391        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2392        FailingConsumer r = new FailingConsumer();
2393        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2394        f.complete(one);
2395        checkCompletedWithWrappedCFException(g);
2396    }
2397
2398    /**
2399     * acceptEitherAsync result completes exceptionally if either
2400     * source cancelled
2401     */
2402    public void testAcceptEitherAsync4() {
2403        CompletableFuture<Integer> f = new CompletableFuture<>();
2404        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2405        IncAction r = new IncAction();
2406        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2407        assertTrue(f.cancel(true));
2408        checkCompletedWithWrappedCancellationException(g);
2409
2410        r = new IncAction();
2411        f = new CompletableFuture<>();
2412        f2 = new CompletableFuture<>();
2413        assertTrue(f2.cancel(true));
2414        g = f.acceptEitherAsync(f2, r);
2415        checkCompletedWithWrappedCancellationException(g);
2416    }
2417
2418    /**
2419     * runAfterEitherAsync result completes normally after normal
2420     * completion of sources
2421     */
2422    public void testRunAfterEitherAsync() {
2423        CompletableFuture<Integer> f = new CompletableFuture<>();
2424        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2425        Noop r = new Noop();
2426        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2427        f.complete(one);
2428        checkCompletedNormally(g, null);
2429        assertTrue(r.ran);
2430
2431        r = new Noop();
2432        f = new CompletableFuture<>();
2433        f.complete(one);
2434        f2 = new CompletableFuture<>();
2435        g = f.runAfterEitherAsync(f2, r);
2436        checkCompletedNormally(g, null);
2437        assertTrue(r.ran);
2438    }
2439
2440    /**
2441     * runAfterEitherAsync result completes exceptionally after exceptional
2442     * completion of source
2443     */
2444    public void testRunAfterEitherAsync2() {
2445        CompletableFuture<Integer> f = new CompletableFuture<>();
2446        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2447        Noop r = new Noop();
2448        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2449        f.completeExceptionally(new CFException());
2450        checkCompletedWithWrappedCFException(g);
2451
2452        r = new Noop();
2453        f = new CompletableFuture<>();
2454        f2 = new CompletableFuture<>();
2455        f2.completeExceptionally(new CFException());
2456        g = f.runAfterEitherAsync(f2, r);
2457        f.complete(one);
2458        checkCompletedWithWrappedCFException(g);
2459    }
2460
2461    /**
2462     * runAfterEitherAsync result completes exceptionally if action does
2463     */
2464    public void testRunAfterEitherAsync3() {
2465        CompletableFuture<Integer> f = new CompletableFuture<>();
2466        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2467        FailingNoop r = new FailingNoop();
2468        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2469        f.complete(one);
2470        checkCompletedWithWrappedCFException(g);
2471    }
2472
2473    /**
2474     * runAfterEitherAsync result completes exceptionally if either
2475     * source cancelled
2476     */
2477    public void testRunAfterEitherAsync4() {
2478        CompletableFuture<Integer> f = new CompletableFuture<>();
2479        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2480        Noop r = new Noop();
2481        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r);
2482        assertTrue(f.cancel(true));
2483        checkCompletedWithWrappedCancellationException(g);
2484
2485        r = new Noop();
2486        f = new CompletableFuture<>();
2487        f2 = new CompletableFuture<>();
2488        assertTrue(f2.cancel(true));
2489        g = f.runAfterEitherAsync(f2, r);
2490        checkCompletedWithWrappedCancellationException(g);
2491    }
2492
2493    /**
2953       * thenComposeAsync result completes normally after normal
2954       * completion of source
2955       */
# Line 2714 | Line 3173 | public class CompletableFutureTest exten
3173      }
3174  
3175      /**
2717     * applyToEitherAsync result completes normally after normal
2718     * completion of sources
2719     */
2720    public void testApplyToEitherAsyncE() {
2721        CompletableFuture<Integer> f = new CompletableFuture<>();
2722        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2723        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2724        f.complete(one);
2725        checkCompletedNormally(g, two);
2726
2727        f = new CompletableFuture<>();
2728        f.complete(one);
2729        f2 = new CompletableFuture<>();
2730        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2731        checkCompletedNormally(g, two);
2732    }
2733
2734    /**
2735     * applyToEitherAsync result completes exceptionally after exceptional
2736     * completion of source
2737     */
2738    public void testApplyToEitherAsync2E() {
2739        CompletableFuture<Integer> f = new CompletableFuture<>();
2740        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2741        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2742        f.completeExceptionally(new CFException());
2743        checkCompletedWithWrappedCFException(g);
2744
2745        f = new CompletableFuture<>();
2746        f2 = new CompletableFuture<>();
2747        f2.completeExceptionally(new CFException());
2748        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2749        f.complete(one);
2750        checkCompletedWithWrappedCFException(g);
2751    }
2752
2753    /**
2754     * applyToEitherAsync result completes exceptionally if action does
2755     */
2756    public void testApplyToEitherAsync3E() {
2757        CompletableFuture<Integer> f = new CompletableFuture<>();
2758        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2759        FailingFunction r = new FailingFunction();
2760        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2761        f.complete(one);
2762        checkCompletedWithWrappedCFException(g);
2763    }
2764
2765    /**
2766     * applyToEitherAsync result completes exceptionally if either source cancelled
2767     */
2768    public void testApplyToEitherAsync4E() {
2769        CompletableFuture<Integer> f = new CompletableFuture<>();
2770        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2771        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2772        assertTrue(f.cancel(true));
2773        checkCompletedWithWrappedCancellationException(g);
2774
2775        f = new CompletableFuture<>();
2776        f2 = new CompletableFuture<>();
2777        assertTrue(f2.cancel(true));
2778        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2779        checkCompletedWithWrappedCancellationException(g);
2780    }
2781
2782    /**
2783     * acceptEitherAsync result completes normally after normal
2784     * completion of sources
2785     */
2786    public void testAcceptEitherAsyncE() {
2787        CompletableFuture<Integer> f = new CompletableFuture<>();
2788        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2789        IncAction r = new IncAction();
2790        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2791        f.complete(one);
2792        checkCompletedNormally(g, null);
2793        assertEquals(r.value, (Integer) 2);
2794
2795        r = new IncAction();
2796        f = new CompletableFuture<>();
2797        f.complete(one);
2798        f2 = new CompletableFuture<>();
2799        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2800        checkCompletedNormally(g, null);
2801        assertEquals(r.value, (Integer) 2);
2802    }
2803
2804    /**
2805     * acceptEitherAsync result completes exceptionally after exceptional
2806     * completion of source
2807     */
2808    public void testAcceptEitherAsync2E() {
2809        CompletableFuture<Integer> f = new CompletableFuture<>();
2810        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2811        IncAction r = new IncAction();
2812        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2813        f.completeExceptionally(new CFException());
2814        checkCompletedWithWrappedCFException(g);
2815
2816        r = new IncAction();
2817        f = new CompletableFuture<>();
2818        f2 = new CompletableFuture<>();
2819        f2.completeExceptionally(new CFException());
2820        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2821        f.complete(one);
2822        checkCompletedWithWrappedCFException(g);
2823    }
2824
2825    /**
2826     * acceptEitherAsync result completes exceptionally if action does
2827     */
2828    public void testAcceptEitherAsync3E() {
2829        CompletableFuture<Integer> f = new CompletableFuture<>();
2830        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2831        FailingConsumer r = new FailingConsumer();
2832        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2833        f.complete(one);
2834        checkCompletedWithWrappedCFException(g);
2835    }
2836
2837    /**
2838     * acceptEitherAsync result completes exceptionally if either
2839     * source cancelled
2840     */
2841    public void testAcceptEitherAsync4E() {
2842        CompletableFuture<Integer> f = new CompletableFuture<>();
2843        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2844        IncAction r = new IncAction();
2845        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2846        assertTrue(f.cancel(true));
2847        checkCompletedWithWrappedCancellationException(g);
2848
2849        r = new IncAction();
2850        f = new CompletableFuture<>();
2851        f2 = new CompletableFuture<>();
2852        assertTrue(f2.cancel(true));
2853        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2854        checkCompletedWithWrappedCancellationException(g);
2855    }
2856
2857    /**
2858     * runAfterEitherAsync result completes normally after normal
2859     * completion of sources
2860     */
2861    public void testRunAfterEitherAsyncE() {
2862        CompletableFuture<Integer> f = new CompletableFuture<>();
2863        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2864        Noop r = new Noop();
2865        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2866        f.complete(one);
2867        checkCompletedNormally(g, null);
2868        assertTrue(r.ran);
2869
2870        r = new Noop();
2871        f = new CompletableFuture<>();
2872        f.complete(one);
2873        f2 = new CompletableFuture<>();
2874        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2875        checkCompletedNormally(g, null);
2876        assertTrue(r.ran);
2877    }
2878
2879    /**
2880     * runAfterEitherAsync result completes exceptionally after exceptional
2881     * completion of source
2882     */
2883    public void testRunAfterEitherAsync2E() {
2884        CompletableFuture<Integer> f = new CompletableFuture<>();
2885        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2886        Noop r = new Noop();
2887        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2888        f.completeExceptionally(new CFException());
2889        checkCompletedWithWrappedCFException(g);
2890
2891        r = new Noop();
2892        f = new CompletableFuture<>();
2893        f2 = new CompletableFuture<>();
2894        f2.completeExceptionally(new CFException());
2895        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2896        f.complete(one);
2897        checkCompletedWithWrappedCFException(g);
2898    }
2899
2900    /**
2901     * runAfterEitherAsync result completes exceptionally if action does
2902     */
2903    public void testRunAfterEitherAsync3E() {
2904        CompletableFuture<Integer> f = new CompletableFuture<>();
2905        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2906        FailingNoop r = new FailingNoop();
2907        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2908        f.complete(one);
2909        checkCompletedWithWrappedCFException(g);
2910    }
2911
2912    /**
2913     * runAfterEitherAsync result completes exceptionally if either
2914     * source cancelled
2915     */
2916    public void testRunAfterEitherAsync4E() {
2917        CompletableFuture<Integer> f = new CompletableFuture<>();
2918        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2919        Noop r = new Noop();
2920        CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2921        assertTrue(f.cancel(true));
2922        checkCompletedWithWrappedCancellationException(g);
2923
2924        r = new Noop();
2925        f = new CompletableFuture<>();
2926        f2 = new CompletableFuture<>();
2927        assertTrue(f2.cancel(true));
2928        g = f.runAfterEitherAsync(f2, r, new ThreadExecutor());
2929        checkCompletedWithWrappedCancellationException(g);
2930    }
2931
2932    /**
3176       * thenComposeAsync result completes normally after normal
3177       * completion of source
3178       */
# Line 3173 | 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 =
3209 <            f.whenComplete((Integer x, Throwable t) ->
3210 <                           { throw new CFException(); } );
3211 <        f.complete(three);
3212 <        checkCompletedNormally(f, three);
3213 <        assertTrue(g.isCompletedExceptionally());
3214 <        checkCompletedWithWrappedCFException(g);
3215 <    }
3216 <
3217 <    /**
3218 <     * whenCompleteAsync action executes on normal completion, propagating
3219 <     * source result.
3220 <     */
3221 <    public void testWhenCompleteAsync1() {
3222 <        final AtomicInteger a = new AtomicInteger();
3223 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3224 <        CompletableFuture<Integer> g =
3225 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3226 <        f.complete(three);
3227 <        checkCompletedNormally(f, three);
3228 <        checkCompletedNormally(g, three);
3229 <        assertEquals(a.get(), 1);
3230 <    }
3231 <
3232 <    /**
3233 <     * whenCompleteAsync action executes on exceptional completion, propagating
3234 <     * source result.
3235 <     */
3236 <    public void testWhenCompleteAsync2() {
3237 <        final AtomicInteger a = new AtomicInteger();
3238 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3239 <        CompletableFuture<Integer> g =
3240 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement());
3241 <        f.completeExceptionally(new CFException());
3242 <        checkCompletedWithWrappedCFException(f);
3243 <        checkCompletedWithWrappedCFException(g);
3244 <    }
3245 <
3246 <    /**
3247 <     * If a whenCompleteAsync action throws an exception when
3248 <     * triggered by a normal completion, it completes exceptionally
3249 <     */
3250 <    public void testWhenCompleteAsync3() {
3251 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3252 <        CompletableFuture<Integer> g =
3253 <            f.whenCompleteAsync((Integer x, Throwable t) ->
3254 <                           { throw new CFException(); } );
3255 <        f.complete(three);
3256 <        checkCompletedNormally(f, three);
3257 <        checkCompletedWithWrappedCFException(g);
3258 <    }
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);
3273 <        checkCompletedNormally(g, three);
3274 <        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();
3284 <        CompletableFuture<Integer> f = new CompletableFuture<>();
3285 <        CompletableFuture<Integer> g =
3286 <            f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(),
3287 <                                exec);
3288 <        f.completeExceptionally(new CFException());
3289 <        checkCompletedWithWrappedCFException(f);
3290 <        checkCompletedWithWrappedCFException(g);
3291 <    }
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