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.62 by jsr166, Fri Jun 6 01:19:22 2014 UTC vs.
Revision 1.68 by jsr166, Fri Jun 6 19:35:54 2014 UTC

# Line 360 | Line 360 | public class CompletableFutureTest exten
360          return (x == null) ? null : x + 1;
361      }
362  
363 <    class IncAction extends CheckedIntegerAction
363 >    class NoopConsumer extends CheckedIntegerAction
364          implements Consumer<Integer>
365      {
366 <        IncAction(ExecutionMode m) { super(m); }
366 >        NoopConsumer(ExecutionMode m) { super(m); }
367          public void accept(Integer x) {
368              invoked();
369 <            value = inc(x);
369 >            value = x;
370          }
371      }
372  
# Line 415 | Line 415 | public class CompletableFutureTest exten
415          }
416      }
417  
418 <    class FailingSupplier extends CheckedAction implements Supplier<Integer>
418 >    class FailingSupplier extends CheckedAction
419 >        implements Supplier<Integer>
420      {
421          FailingSupplier(ExecutionMode m) { super(m); }
422          public Integer get() {
# Line 424 | Line 425 | public class CompletableFutureTest exten
425          }
426      }
427  
428 <    class FailingConsumer extends CheckedAction implements Consumer<Integer> {
428 >    class FailingConsumer extends CheckedIntegerAction
429 >        implements Consumer<Integer>
430 >    {
431          FailingConsumer(ExecutionMode m) { super(m); }
432          public void accept(Integer x) {
433              invoked();
434 +            value = x;
435              throw new CFException();
436          }
437      }
438  
439 <    class FailingBiConsumer extends CheckedAction
439 >    class FailingBiConsumer extends CheckedIntegerAction
440          implements BiConsumer<Integer, Integer>
441      {
442          FailingBiConsumer(ExecutionMode m) { super(m); }
443          public void accept(Integer x, Integer y) {
444              invoked();
445 +            value = subtract(x, y);
446              throw new CFException();
447          }
448      }
449  
450 <    class FailingFunction extends CheckedAction
450 >    class FailingFunction extends CheckedIntegerAction
451          implements Function<Integer, Integer>
452      {
453          FailingFunction(ExecutionMode m) { super(m); }
454          public Integer apply(Integer x) {
455              invoked();
456 +            value = x;
457              throw new CFException();
458          }
459      }
460  
461 <    class FailingBiFunction extends CheckedAction
461 >    class FailingBiFunction extends CheckedIntegerAction
462          implements BiFunction<Integer, Integer, Integer>
463      {
464          FailingBiFunction(ExecutionMode m) { super(m); }
465          public Integer apply(Integer x, Integer y) {
466              invoked();
467 +            value = subtract(x, y);
468              throw new CFException();
469          }
470      }
# Line 471 | Line 478 | public class CompletableFutureTest exten
478      }
479  
480  
481 <    class CompletableFutureInc extends CheckedAction
481 >    class CompletableFutureInc extends CheckedIntegerAction
482          implements Function<Integer, CompletableFuture<Integer>>
483      {
484          CompletableFutureInc(ExecutionMode m) { super(m); }
485          public CompletableFuture<Integer> apply(Integer x) {
486              invoked();
487 +            value = x;
488              CompletableFuture<Integer> f = new CompletableFuture<>();
489              f.complete(inc(x));
490              return f;
491          }
492      }
493  
494 <    class FailingCompletableFutureFunction extends CheckedAction
494 >    class FailingCompletableFutureFunction extends CheckedIntegerAction
495          implements Function<Integer, CompletableFuture<Integer>>
496      {
497          FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
498          public CompletableFuture<Integer> apply(Integer x) {
499              invoked();
500 +            value = x;
501              throw new CFException();
502          }
503      }
# Line 1240 | Line 1249 | public class CompletableFutureTest exten
1249          for (Integer v1 : new Integer[] { 1, null })
1250      {
1251          final CompletableFuture<Integer> f = new CompletableFuture<>();
1252 <        final IncAction r = new IncAction(m);
1252 >        final NoopConsumer r = new NoopConsumer(m);
1253          if (!createIncomplete) f.complete(v1);
1254          final CompletableFuture<Void> g = m.thenAccept(f, r);
1255          if (createIncomplete) {
# Line 1249 | Line 1258 | public class CompletableFutureTest exten
1258          }
1259  
1260          checkCompletedNormally(g, null);
1261 +        r.assertValue(v1);
1262          checkCompletedNormally(f, v1);
1253        r.assertInvoked();
1254        r.assertValue(inc(v1));
1263      }}
1264  
1265      /**
# Line 1264 | Line 1272 | public class CompletableFutureTest exten
1272      {
1273          final CFException ex = new CFException();
1274          final CompletableFuture<Integer> f = new CompletableFuture<>();
1275 <        final IncAction r = new IncAction(m);
1275 >        final NoopConsumer r = new NoopConsumer(m);
1276          if (!createIncomplete) f.completeExceptionally(ex);
1277          final CompletableFuture<Void> g = m.thenAccept(f, r);
1278          if (createIncomplete) {
# Line 1286 | Line 1294 | public class CompletableFutureTest exten
1294          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1295      {
1296          final CompletableFuture<Integer> f = new CompletableFuture<>();
1297 <        final IncAction r = new IncAction(m);
1297 >        final NoopConsumer r = new NoopConsumer(m);
1298          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1299          final CompletableFuture<Void> g = m.thenAccept(f, r);
1300          if (createIncomplete) {
# Line 1757 | Line 1765 | public class CompletableFutureTest exten
1765          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1766          try {
1767              assertEquals(inc(v1), h4.join());
1768 <            rs[4].assertInvoked();
1768 >            rs[4].assertValue(inc(v1));
1769          } catch (CompletionException ok) {
1770              checkCompletedWithWrappedCFException(h4, ex);
1771              rs[4].assertNotInvoked();
1772          }
1773          try {
1774              assertEquals(inc(v1), h5.join());
1775 <            rs[5].assertInvoked();
1775 >            rs[5].assertValue(inc(v1));
1776          } catch (CompletionException ok) {
1777              checkCompletedWithWrappedCFException(h5, ex);
1778              rs[5].assertNotInvoked();
# Line 1780 | Line 1788 | public class CompletableFutureTest exten
1788          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1789      }}
1790  
1791 +    public void testApplyToEither_exceptionalCompletion2() {
1792 +        for (ExecutionMode m : ExecutionMode.values())
1793 +        for (boolean fFirst : new boolean[] { true, false })
1794 +        for (Integer v1 : new Integer[] { 1, null })
1795 +    {
1796 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1797 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1798 +        final CFException ex = new CFException();
1799 +        final IncFunction[] rs = new IncFunction[6];
1800 +        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1801 +
1802 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1803 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1804 +        if (fFirst) {
1805 +            f.complete(v1);
1806 +            g.completeExceptionally(ex);
1807 +        } else {
1808 +            g.completeExceptionally(ex);
1809 +            f.complete(v1);
1810 +        }
1811 +        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1812 +        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1813 +
1814 +        // unspecified behavior - both source completions available
1815 +        try {
1816 +            assertEquals(inc(v1), h0.join());
1817 +            rs[0].assertValue(inc(v1));
1818 +        } catch (CompletionException ok) {
1819 +            checkCompletedWithWrappedCFException(h0, ex);
1820 +            rs[0].assertNotInvoked();
1821 +        }
1822 +        try {
1823 +            assertEquals(inc(v1), h1.join());
1824 +            rs[1].assertValue(inc(v1));
1825 +        } catch (CompletionException ok) {
1826 +            checkCompletedWithWrappedCFException(h1, ex);
1827 +            rs[1].assertNotInvoked();
1828 +        }
1829 +        try {
1830 +            assertEquals(inc(v1), h2.join());
1831 +            rs[2].assertValue(inc(v1));
1832 +        } catch (CompletionException ok) {
1833 +            checkCompletedWithWrappedCFException(h2, ex);
1834 +            rs[2].assertNotInvoked();
1835 +        }
1836 +        try {
1837 +            assertEquals(inc(v1), h3.join());
1838 +            rs[3].assertValue(inc(v1));
1839 +        } catch (CompletionException ok) {
1840 +            checkCompletedWithWrappedCFException(h3, ex);
1841 +            rs[3].assertNotInvoked();
1842 +        }
1843 +
1844 +        checkCompletedNormally(f, v1);
1845 +        checkCompletedWithWrappedCFException(g, ex);
1846 +    }}
1847 +
1848      /**
1849       * applyToEither result completes exceptionally if either source cancelled
1850       */
# Line 1813 | Line 1878 | public class CompletableFutureTest exten
1878          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1879          try {
1880              assertEquals(inc(v1), h4.join());
1881 <            rs[4].assertInvoked();
1881 >            rs[4].assertValue(inc(v1));
1882          } catch (CompletionException ok) {
1883              checkCompletedWithWrappedCancellationException(h4);
1884              rs[4].assertNotInvoked();
1885          }
1886          try {
1887              assertEquals(inc(v1), h5.join());
1888 <            rs[5].assertInvoked();
1888 >            rs[5].assertValue(inc(v1));
1889          } catch (CompletionException ok) {
1890              checkCompletedWithWrappedCancellationException(h5);
1891              rs[5].assertNotInvoked();
# Line 1835 | Line 1900 | public class CompletableFutureTest exten
1900          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1901      }}
1902  
1903 <    /**
1839 <     * applyToEither result completes exceptionally if action does
1840 <     */
1841 <    public void testApplyToEither_actionFailed() {
1903 >    public void testApplyToEither_sourceCancelled2() {
1904          for (ExecutionMode m : ExecutionMode.values())
1905 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1906 +        for (boolean fFirst : new boolean[] { true, false })
1907          for (Integer v1 : new Integer[] { 1, null })
1844        for (Integer v2 : new Integer[] { 2, null })
1908      {
1909          final CompletableFuture<Integer> f = new CompletableFuture<>();
1910          final CompletableFuture<Integer> g = new CompletableFuture<>();
1911 <        final FailingFunction[] rs = new FailingFunction[6];
1912 <        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1911 >        final IncFunction[] rs = new IncFunction[6];
1912 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1913  
1914          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1915          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1916 <        f.complete(v1);
1916 >        if (fFirst) {
1917 >            f.complete(v1);
1918 >            g.cancel(mayInterruptIfRunning);
1919 >        } else {
1920 >            g.cancel(mayInterruptIfRunning);
1921 >            f.complete(v1);
1922 >        }
1923          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1924          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1925 <        g.complete(v2);
1926 <        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1927 <        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1928 <        
1925 >
1926 >        // unspecified behavior - both source completions available
1927 >        try {
1928 >            assertEquals(inc(v1), h0.join());
1929 >            rs[0].assertValue(inc(v1));
1930 >        } catch (CompletionException ok) {
1931 >            checkCompletedWithWrappedCancellationException(h0);
1932 >            rs[0].assertNotInvoked();
1933 >        }
1934 >        try {
1935 >            assertEquals(inc(v1), h1.join());
1936 >            rs[1].assertValue(inc(v1));
1937 >        } catch (CompletionException ok) {
1938 >            checkCompletedWithWrappedCancellationException(h1);
1939 >            rs[1].assertNotInvoked();
1940 >        }
1941 >        try {
1942 >            assertEquals(inc(v1), h2.join());
1943 >            rs[2].assertValue(inc(v1));
1944 >        } catch (CompletionException ok) {
1945 >            checkCompletedWithWrappedCancellationException(h2);
1946 >            rs[2].assertNotInvoked();
1947 >        }
1948 >        try {
1949 >            assertEquals(inc(v1), h3.join());
1950 >            rs[3].assertValue(inc(v1));
1951 >        } catch (CompletionException ok) {
1952 >            checkCompletedWithWrappedCancellationException(h3);
1953 >            rs[3].assertNotInvoked();
1954 >        }
1955 >
1956          checkCompletedNormally(f, v1);
1957 <        checkCompletedNormally(g, v2);
1862 <        checkCompletedWithWrappedCFException(h0);
1863 <        checkCompletedWithWrappedCFException(h1);
1864 <        checkCompletedWithWrappedCFException(h2);
1865 <        checkCompletedWithWrappedCFException(h3);
1866 <        checkCompletedWithWrappedCFException(h4);
1867 <        checkCompletedWithWrappedCFException(h5);
1868 <        for (int i = 0; i < rs.length; i++) rs[i].assertInvoked();
1957 >        checkCancelled(g);
1958      }}
1959  
1960      /**
1961 <     * acceptEither result completes normally after normal completion
1873 <     * of either source
1961 >     * applyToEither result completes exceptionally if action does
1962       */
1963 <    public void testAcceptEither_normalCompletion1() {
1963 >    public void testApplyToEither_actionFailed() {
1964          for (ExecutionMode m : ExecutionMode.values())
1965          for (Integer v1 : new Integer[] { 1, null })
1966          for (Integer v2 : new Integer[] { 2, null })
1967      {
1968          final CompletableFuture<Integer> f = new CompletableFuture<>();
1969          final CompletableFuture<Integer> g = new CompletableFuture<>();
1970 <        final IncAction r = new IncAction(m);
1971 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1970 >        final FailingFunction[] rs = new FailingFunction[6];
1971 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1972  
1973 +        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
1974 +        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
1975          f.complete(v1);
1976 <        checkCompletedNormally(h, null);
1977 <        assertEquals(inc(v1), r.value);
1978 <        g.complete(v2);
1976 >        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
1977 >        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
1978 >        checkCompletedWithWrappedCFException(h0);
1979 >        checkCompletedWithWrappedCFException(h1);
1980 >        checkCompletedWithWrappedCFException(h2);
1981 >        checkCompletedWithWrappedCFException(h3);
1982 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
1983  
1984 <        checkCompletedNormally(f, v1);
1891 <        checkCompletedNormally(g, v2);
1892 <        checkCompletedNormally(h, null);
1893 <    }}
1984 >        g.complete(v2);
1985  
1986 <    public void testAcceptEither_normalCompletion2() {
1987 <        for (ExecutionMode m : ExecutionMode.values())
1988 <        for (Integer v1 : new Integer[] { 1, null })
1898 <        for (Integer v2 : new Integer[] { 2, null })
1899 <    {
1900 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1901 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1902 <        final IncAction r = new IncAction(m);
1903 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1986 >        // unspecified behavior - both source completions available
1987 >        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
1988 >        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
1989  
1990 <        g.complete(v2);
1991 <        checkCompletedNormally(h, null);
1992 <        assertEquals(inc(v2), r.value);
1993 <        f.complete(v1);
1990 >        checkCompletedWithWrappedCFException(h4);
1991 >        assertTrue(Objects.equals(v1, rs[4].value) ||
1992 >                   Objects.equals(v2, rs[4].value));
1993 >        checkCompletedWithWrappedCFException(h5);
1994 >        assertTrue(Objects.equals(v1, rs[5].value) ||
1995 >                   Objects.equals(v2, rs[5].value));
1996  
1997          checkCompletedNormally(f, v1);
1998          checkCompletedNormally(g, v2);
1912        checkCompletedNormally(h, null);
1999      }}
2000  
2001 <    public void testAcceptEither_normalCompletion3() {
2001 >    /**
2002 >     * acceptEither result completes normally after normal completion
2003 >     * of either source
2004 >     */
2005 >    public void testAcceptEither_normalCompletion() {
2006          for (ExecutionMode m : ExecutionMode.values())
2007          for (Integer v1 : new Integer[] { 1, null })
2008          for (Integer v2 : new Integer[] { 2, null })
2009      {
2010          final CompletableFuture<Integer> f = new CompletableFuture<>();
2011          final CompletableFuture<Integer> g = new CompletableFuture<>();
2012 <        final IncAction r = new IncAction(m);
2012 >        final NoopConsumer[] rs = new NoopConsumer[6];
2013 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2014  
2015 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2016 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2017 +        checkIncomplete(h0);
2018 +        checkIncomplete(h1);
2019 +        rs[0].assertNotInvoked();
2020 +        rs[1].assertNotInvoked();
2021          f.complete(v1);
2022 +        checkCompletedNormally(h0, null);
2023 +        checkCompletedNormally(h1, null);
2024 +        rs[0].assertValue(v1);
2025 +        rs[1].assertValue(v1);
2026 +        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2027 +        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2028 +        checkCompletedNormally(h2, null);
2029 +        checkCompletedNormally(h3, null);
2030 +        rs[2].assertValue(v1);
2031 +        rs[3].assertValue(v1);
2032          g.complete(v2);
1926        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2033  
2034 <        checkCompletedNormally(h, null);
2034 >        // unspecified behavior - both source completions available
2035 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2036 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2037 >        checkCompletedNormally(h4, null);
2038 >        checkCompletedNormally(h5, null);
2039 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2040 >                   Objects.equals(v2, rs[4].value));
2041 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2042 >                   Objects.equals(v2, rs[5].value));
2043 >
2044          checkCompletedNormally(f, v1);
2045          checkCompletedNormally(g, v2);
2046 <
2047 <        // unspecified behavior
2048 <        assertTrue(Objects.equals(r.value, inc(v1)) ||
2049 <                   Objects.equals(r.value, inc(v2)));
2046 >        checkCompletedNormally(h0, null);
2047 >        checkCompletedNormally(h1, null);
2048 >        checkCompletedNormally(h2, null);
2049 >        checkCompletedNormally(h3, null);
2050 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2051      }}
2052  
2053      /**
2054       * acceptEither result completes exceptionally after exceptional
2055       * completion of either source
2056       */
2057 <    public void testAcceptEither_exceptionalCompletion1() {
2057 >    public void testAcceptEither_exceptionalCompletion() {
2058          for (ExecutionMode m : ExecutionMode.values())
2059          for (Integer v1 : new Integer[] { 1, null })
2060      {
2061          final CompletableFuture<Integer> f = new CompletableFuture<>();
2062          final CompletableFuture<Integer> g = new CompletableFuture<>();
1947        final IncAction r = new IncAction(m);
1948        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2063          final CFException ex = new CFException();
2064 +        final NoopConsumer[] rs = new NoopConsumer[6];
2065 +        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2066  
2067 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2068 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2069 +        checkIncomplete(h0);
2070 +        checkIncomplete(h1);
2071 +        rs[0].assertNotInvoked();
2072 +        rs[1].assertNotInvoked();
2073          f.completeExceptionally(ex);
2074 <        checkCompletedWithWrappedCFException(h, ex);
2075 <        g.complete(v1);
2076 <
2077 <        r.assertNotInvoked();
2078 <        checkCompletedNormally(g, v1);
2079 <        checkCompletedWithWrappedCFException(f, ex);
1958 <        checkCompletedWithWrappedCFException(h, ex);
1959 <    }}
1960 <
1961 <    public void testAcceptEither_exceptionalCompletion2() {
1962 <        for (ExecutionMode m : ExecutionMode.values())
1963 <        for (Integer v1 : new Integer[] { 1, null })
1964 <    {
1965 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1966 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1967 <        final IncAction r = new IncAction(m);
1968 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
1969 <        final CFException ex = new CFException();
1970 <
1971 <        g.completeExceptionally(ex);
1972 <        checkCompletedWithWrappedCFException(h, ex);
1973 <        f.complete(v1);
1974 <
1975 <        r.assertNotInvoked();
1976 <        checkCompletedNormally(f, v1);
1977 <        checkCompletedWithWrappedCFException(g, ex);
1978 <        checkCompletedWithWrappedCFException(h, ex);
1979 <    }}
1980 <
1981 <    public void testAcceptEither_exceptionalCompletion3() {
1982 <        for (ExecutionMode m : ExecutionMode.values())
1983 <        for (Integer v1 : new Integer[] { 1, null })
1984 <    {
1985 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
1986 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
1987 <        final IncAction r = new IncAction(m);
1988 <        final CFException ex = new CFException();
2074 >        checkCompletedWithWrappedCFException(h0, ex);
2075 >        checkCompletedWithWrappedCFException(h1, ex);
2076 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2077 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2078 >        checkCompletedWithWrappedCFException(h2, ex);
2079 >        checkCompletedWithWrappedCFException(h3, ex);
2080  
2081 <        g.completeExceptionally(ex);
1991 <        f.complete(v1);
1992 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2081 >        g.complete(v1);
2082  
2083 <        // unspecified behavior
2084 <        Integer v;
2083 >        // unspecified behavior - both source completions available
2084 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2085 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2086          try {
2087 <            assertNull(h.join());
2088 <            r.assertInvoked();
1999 <            assertEquals(inc(v1), r.value);
2087 >            assertNull(h4.join());
2088 >            rs[4].assertValue(v1);
2089          } catch (CompletionException ok) {
2090 <            checkCompletedWithWrappedCFException(h, ex);
2091 <            r.assertNotInvoked();
2090 >            checkCompletedWithWrappedCFException(h4, ex);
2091 >            rs[4].assertNotInvoked();
2092          }
2004
2005        checkCompletedWithWrappedCFException(g, ex);
2006        checkCompletedNormally(f, v1);
2007    }}
2008
2009    public void testAcceptEither_exceptionalCompletion4() {
2010        for (ExecutionMode m : ExecutionMode.values())
2011        for (Integer v1 : new Integer[] { 1, null })
2012    {
2013        final CompletableFuture<Integer> f = new CompletableFuture<>();
2014        final CompletableFuture<Integer> g = new CompletableFuture<>();
2015        final IncAction r = new IncAction(m);
2016        final CFException ex = new CFException();
2017
2018        f.completeExceptionally(ex);
2019        g.complete(v1);
2020        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2021
2022        // unspecified behavior
2023        Integer v;
2093          try {
2094 <            assertNull(h.join());
2095 <            r.assertInvoked();
2027 <            assertEquals(inc(v1), r.value);
2094 >            assertNull(h5.join());
2095 >            rs[5].assertValue(v1);
2096          } catch (CompletionException ok) {
2097 <            checkCompletedWithWrappedCFException(h, ex);
2098 <            r.assertNotInvoked();
2097 >            checkCompletedWithWrappedCFException(h5, ex);
2098 >            rs[5].assertNotInvoked();
2099          }
2100  
2101          checkCompletedWithWrappedCFException(f, ex);
2102          checkCompletedNormally(g, v1);
2103 +        checkCompletedWithWrappedCFException(h0, ex);
2104 +        checkCompletedWithWrappedCFException(h1, ex);
2105 +        checkCompletedWithWrappedCFException(h2, ex);
2106 +        checkCompletedWithWrappedCFException(h3, ex);
2107 +        checkCompletedWithWrappedCFException(h4, ex);
2108 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2109      }}
2110  
2111 <    /**
2038 <     * acceptEither result completes exceptionally if action does
2039 <     */
2040 <    public void testAcceptEither_actionFailed1() {
2111 >    public void testAcceptEither_exceptionalCompletion2() {
2112          for (ExecutionMode m : ExecutionMode.values())
2113 +        for (boolean fFirst : new boolean[] { true, false })
2114          for (Integer v1 : new Integer[] { 1, null })
2043        for (Integer v2 : new Integer[] { 2, null })
2115      {
2116          final CompletableFuture<Integer> f = new CompletableFuture<>();
2117          final CompletableFuture<Integer> g = new CompletableFuture<>();
2118 <        final FailingConsumer r = new FailingConsumer(m);
2119 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2118 >        final CFException ex = new CFException();
2119 >        final NoopConsumer[] rs = new NoopConsumer[6];
2120 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2121  
2122 <        f.complete(v1);
2123 <        checkCompletedWithWrappedCFException(h);
2124 <        g.complete(v2);
2125 <        checkCompletedNormally(f, v1);
2126 <        checkCompletedNormally(g, v2);
2127 <    }}
2122 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2123 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2124 >        if (fFirst) {
2125 >            f.complete(v1);
2126 >            g.completeExceptionally(ex);
2127 >        } else {
2128 >            g.completeExceptionally(ex);
2129 >            f.complete(v1);
2130 >        }
2131 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2132 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2133  
2134 <    public void testAcceptEither_actionFailed2() {
2135 <        for (ExecutionMode m : ExecutionMode.values())
2136 <        for (Integer v1 : new Integer[] { 1, null })
2137 <        for (Integer v2 : new Integer[] { 2, null })
2138 <    {
2139 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2140 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2141 <        final FailingConsumer r = new FailingConsumer(m);
2142 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2134 >        // unspecified behavior - both source completions available
2135 >        try {
2136 >            assertEquals(null, h0.join());
2137 >            rs[0].assertValue(v1);
2138 >        } catch (CompletionException ok) {
2139 >            checkCompletedWithWrappedCFException(h0, ex);
2140 >            rs[0].assertNotInvoked();
2141 >        }
2142 >        try {
2143 >            assertEquals(null, h1.join());
2144 >            rs[1].assertValue(v1);
2145 >        } catch (CompletionException ok) {
2146 >            checkCompletedWithWrappedCFException(h1, ex);
2147 >            rs[1].assertNotInvoked();
2148 >        }
2149 >        try {
2150 >            assertEquals(null, h2.join());
2151 >            rs[2].assertValue(v1);
2152 >        } catch (CompletionException ok) {
2153 >            checkCompletedWithWrappedCFException(h2, ex);
2154 >            rs[2].assertNotInvoked();
2155 >        }
2156 >        try {
2157 >            assertEquals(null, h3.join());
2158 >            rs[3].assertValue(v1);
2159 >        } catch (CompletionException ok) {
2160 >            checkCompletedWithWrappedCFException(h3, ex);
2161 >            rs[3].assertNotInvoked();
2162 >        }
2163  
2067        g.complete(v2);
2068        checkCompletedWithWrappedCFException(h);
2069        f.complete(v1);
2164          checkCompletedNormally(f, v1);
2165 <        checkCompletedNormally(g, v2);
2165 >        checkCompletedWithWrappedCFException(g, ex);
2166      }}
2167  
2168      /**
2169       * acceptEither result completes exceptionally if either source cancelled
2170       */
2171 <    public void testAcceptEither_sourceCancelled1() {
2171 >    public void testAcceptEither_sourceCancelled() {
2172          for (ExecutionMode m : ExecutionMode.values())
2173          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2174          for (Integer v1 : new Integer[] { 1, null })
2175      {
2176          final CompletableFuture<Integer> f = new CompletableFuture<>();
2177          final CompletableFuture<Integer> g = new CompletableFuture<>();
2178 <        final IncAction r = new IncAction(m);
2179 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2086 <
2087 <        assertTrue(f.cancel(mayInterruptIfRunning));
2088 <        checkCompletedWithWrappedCancellationException(h);
2089 <        g.complete(v1);
2090 <
2091 <        checkCancelled(f);
2092 <        r.assertNotInvoked();
2093 <        checkCompletedNormally(g, v1);
2094 <        checkCompletedWithWrappedCancellationException(h);
2095 <    }}
2178 >        final NoopConsumer[] rs = new NoopConsumer[6];
2179 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2180  
2181 <    public void testAcceptEither_sourceCancelled2() {
2182 <        for (ExecutionMode m : ExecutionMode.values())
2183 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2184 <        for (Integer v1 : new Integer[] { 1, null })
2185 <    {
2186 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2187 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2188 <        final IncAction r = new IncAction(m);
2189 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2190 <
2191 <        assertTrue(g.cancel(mayInterruptIfRunning));
2192 <        checkCompletedWithWrappedCancellationException(h);
2193 <        f.complete(v1);
2110 <
2111 <        checkCancelled(g);
2112 <        r.assertNotInvoked();
2113 <        checkCompletedNormally(f, v1);
2114 <        checkCompletedWithWrappedCancellationException(h);
2115 <    }}
2116 <
2117 <    public void testAcceptEither_sourceCancelled3() {
2118 <        for (ExecutionMode m : ExecutionMode.values())
2119 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2120 <        for (Integer v1 : new Integer[] { 1, null })
2121 <    {
2122 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2123 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2124 <        final IncAction r = new IncAction(m);
2181 >        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2182 >        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2183 >        checkIncomplete(h0);
2184 >        checkIncomplete(h1);
2185 >        rs[0].assertNotInvoked();
2186 >        rs[1].assertNotInvoked();
2187 >        f.cancel(mayInterruptIfRunning);
2188 >        checkCompletedWithWrappedCancellationException(h0);
2189 >        checkCompletedWithWrappedCancellationException(h1);
2190 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2191 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2192 >        checkCompletedWithWrappedCancellationException(h2);
2193 >        checkCompletedWithWrappedCancellationException(h3);
2194  
2195 <        assertTrue(g.cancel(mayInterruptIfRunning));
2127 <        f.complete(v1);
2128 <        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2195 >        g.complete(v1);
2196  
2197 <        // unspecified behavior
2198 <        Integer v;
2197 >        // unspecified behavior - both source completions available
2198 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2199 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2200          try {
2201 <            assertNull(h.join());
2202 <            r.assertInvoked();
2135 <            assertEquals(inc(v1), r.value);
2201 >            assertNull(h4.join());
2202 >            rs[4].assertValue(v1);
2203          } catch (CompletionException ok) {
2204 <            checkCompletedWithWrappedCancellationException(h);
2205 <            r.assertNotInvoked();
2204 >            checkCompletedWithWrappedCancellationException(h4);
2205 >            rs[4].assertNotInvoked();
2206          }
2140
2141        checkCancelled(g);
2142        checkCompletedNormally(f, v1);
2143    }}
2144
2145    public void testAcceptEither_sourceCancelled4() {
2146        for (ExecutionMode m : ExecutionMode.values())
2147        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2148        for (Integer v1 : new Integer[] { 1, null })
2149    {
2150        final CompletableFuture<Integer> f = new CompletableFuture<>();
2151        final CompletableFuture<Integer> g = new CompletableFuture<>();
2152        final IncAction r = new IncAction(m);
2153
2154        assertTrue(f.cancel(mayInterruptIfRunning));
2155        g.complete(v1);
2156        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2157
2158        // unspecified behavior
2159        Integer v;
2207          try {
2208 <            assertNull(h.join());
2209 <            r.assertInvoked();
2163 <            assertEquals(inc(v1), r.value);
2208 >            assertNull(h5.join());
2209 >            rs[5].assertValue(v1);
2210          } catch (CompletionException ok) {
2211 <            checkCompletedWithWrappedCancellationException(h);
2212 <            r.assertNotInvoked();
2211 >            checkCompletedWithWrappedCancellationException(h5);
2212 >            rs[5].assertNotInvoked();
2213          }
2214  
2215          checkCancelled(f);
2216          checkCompletedNormally(g, v1);
2217 +        checkCompletedWithWrappedCancellationException(h0);
2218 +        checkCompletedWithWrappedCancellationException(h1);
2219 +        checkCompletedWithWrappedCancellationException(h2);
2220 +        checkCompletedWithWrappedCancellationException(h3);
2221 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2222      }}
2223  
2224      /**
2225 <     * runAfterEither result completes normally after normal completion
2175 <     * of either source
2225 >     * acceptEither result completes exceptionally if action does
2226       */
2227 <    public void testRunAfterEither_normalCompletion1() {
2227 >    public void testAcceptEither_actionFailed() {
2228          for (ExecutionMode m : ExecutionMode.values())
2229          for (Integer v1 : new Integer[] { 1, null })
2230          for (Integer v2 : new Integer[] { 2, null })
2231      {
2232          final CompletableFuture<Integer> f = new CompletableFuture<>();
2233          final CompletableFuture<Integer> g = new CompletableFuture<>();
2234 <        final Noop r = new Noop(m);
2235 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2234 >        final FailingConsumer[] rs = new FailingConsumer[6];
2235 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
2236  
2237 +        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2238 +        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2239          f.complete(v1);
2240 <        checkCompletedNormally(h, null);
2241 <        r.assertInvoked();
2242 <        g.complete(v2);
2240 >        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2241 >        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2242 >        checkCompletedWithWrappedCFException(h0);
2243 >        checkCompletedWithWrappedCFException(h1);
2244 >        checkCompletedWithWrappedCFException(h2);
2245 >        checkCompletedWithWrappedCFException(h3);
2246 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2247  
2248 <        checkCompletedNormally(f, v1);
2193 <        checkCompletedNormally(g, v2);
2194 <        checkCompletedNormally(h, null);
2195 <        r.assertInvoked();
2196 <    }}
2248 >        g.complete(v2);
2249  
2250 <    public void testRunAfterEither_normalCompletion2() {
2251 <        for (ExecutionMode m : ExecutionMode.values())
2252 <        for (Integer v1 : new Integer[] { 1, null })
2201 <        for (Integer v2 : new Integer[] { 2, null })
2202 <    {
2203 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2204 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2205 <        final Noop r = new Noop(m);
2206 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2250 >        // unspecified behavior - both source completions available
2251 >        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2252 >        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2253  
2254 <        g.complete(v2);
2255 <        checkCompletedNormally(h, null);
2256 <        r.assertInvoked();
2257 <        f.complete(v1);
2254 >        checkCompletedWithWrappedCFException(h4);
2255 >        assertTrue(Objects.equals(v1, rs[4].value) ||
2256 >                   Objects.equals(v2, rs[4].value));
2257 >        checkCompletedWithWrappedCFException(h5);
2258 >        assertTrue(Objects.equals(v1, rs[5].value) ||
2259 >                   Objects.equals(v2, rs[5].value));
2260  
2261          checkCompletedNormally(f, v1);
2262          checkCompletedNormally(g, v2);
2263 <        checkCompletedNormally(h, null);
2216 <        r.assertInvoked();
2217 <        }}
2263 >    }}
2264  
2265 <    public void testRunAfterEither_normalCompletion3() {
2265 >    /**
2266 >     * runAfterEither result completes normally after normal completion
2267 >     * of either source
2268 >     */
2269 >    public void testRunAfterEither_normalCompletion() {
2270          for (ExecutionMode m : ExecutionMode.values())
2271          for (Integer v1 : new Integer[] { 1, null })
2272          for (Integer v2 : new Integer[] { 2, null })
2273      {
2274          final CompletableFuture<Integer> f = new CompletableFuture<>();
2275          final CompletableFuture<Integer> g = new CompletableFuture<>();
2276 <        final Noop r = new Noop(m);
2276 >        final Noop[] rs = new Noop[6];
2277 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2278  
2279 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2280 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2281 +        checkIncomplete(h0);
2282 +        checkIncomplete(h1);
2283 +        rs[0].assertNotInvoked();
2284 +        rs[1].assertNotInvoked();
2285          f.complete(v1);
2286 +        checkCompletedNormally(h0, null);
2287 +        checkCompletedNormally(h1, null);
2288 +        rs[0].assertInvoked();
2289 +        rs[1].assertInvoked();
2290 +        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2291 +        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2292 +        checkCompletedNormally(h2, null);
2293 +        checkCompletedNormally(h3, null);
2294 +        rs[2].assertInvoked();
2295 +        rs[3].assertInvoked();
2296 +
2297          g.complete(v2);
2230        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2298  
2299 <        checkCompletedNormally(h, null);
2299 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2300 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2301 >
2302          checkCompletedNormally(f, v1);
2303          checkCompletedNormally(g, v2);
2304 <        r.assertInvoked();
2304 >        checkCompletedNormally(h0, null);
2305 >        checkCompletedNormally(h1, null);
2306 >        checkCompletedNormally(h2, null);
2307 >        checkCompletedNormally(h3, null);
2308 >        checkCompletedNormally(h4, null);
2309 >        checkCompletedNormally(h5, null);
2310 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2311      }}
2312  
2313      /**
2314       * runAfterEither result completes exceptionally after exceptional
2315       * completion of either source
2316       */
2317 <    public void testRunAfterEither_exceptionalCompletion1() {
2317 >    public void testRunAfterEither_exceptionalCompletion() {
2318          for (ExecutionMode m : ExecutionMode.values())
2319          for (Integer v1 : new Integer[] { 1, null })
2320      {
2321          final CompletableFuture<Integer> f = new CompletableFuture<>();
2322          final CompletableFuture<Integer> g = new CompletableFuture<>();
2248        final Noop r = new Noop(m);
2249        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2323          final CFException ex = new CFException();
2324 +        final Noop[] rs = new Noop[6];
2325 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2326  
2327 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2328 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2329 +        checkIncomplete(h0);
2330 +        checkIncomplete(h1);
2331 +        rs[0].assertNotInvoked();
2332 +        rs[1].assertNotInvoked();
2333          f.completeExceptionally(ex);
2334 <        checkCompletedWithWrappedCFException(h, ex);
2335 <        g.complete(v1);
2336 <
2337 <        r.assertNotInvoked();
2338 <        checkCompletedNormally(g, v1);
2339 <        checkCompletedWithWrappedCFException(f, ex);
2259 <        checkCompletedWithWrappedCFException(h, ex);
2260 <    }}
2261 <
2262 <    public void testRunAfterEither_exceptionalCompletion2() {
2263 <        for (ExecutionMode m : ExecutionMode.values())
2264 <        for (Integer v1 : new Integer[] { 1, null })
2265 <    {
2266 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2267 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2268 <        final Noop r = new Noop(m);
2269 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2270 <        final CFException ex = new CFException();
2271 <
2272 <        g.completeExceptionally(ex);
2273 <        checkCompletedWithWrappedCFException(h, ex);
2274 <        f.complete(v1);
2275 <
2276 <        r.assertNotInvoked();
2277 <        checkCompletedNormally(f, v1);
2278 <        checkCompletedWithWrappedCFException(g, ex);
2279 <        checkCompletedWithWrappedCFException(h, ex);
2280 <    }}
2281 <
2282 <    public void testRunAfterEither_exceptionalCompletion3() {
2283 <        for (ExecutionMode m : ExecutionMode.values())
2284 <        for (Integer v1 : new Integer[] { 1, null })
2285 <    {
2286 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2287 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2288 <        final Noop r = new Noop(m);
2289 <        final CFException ex = new CFException();
2334 >        checkCompletedWithWrappedCFException(h0, ex);
2335 >        checkCompletedWithWrappedCFException(h1, ex);
2336 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2337 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2338 >        checkCompletedWithWrappedCFException(h2, ex);
2339 >        checkCompletedWithWrappedCFException(h3, ex);
2340  
2341 <        g.completeExceptionally(ex);
2292 <        f.complete(v1);
2293 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2341 >        g.complete(v1);
2342  
2343 <        // unspecified behavior
2344 <        Integer v;
2343 >        // unspecified behavior - both source completions available
2344 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2345 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2346          try {
2347 <            assertNull(h.join());
2348 <            r.assertInvoked();
2347 >            assertNull(h4.join());
2348 >            rs[4].assertInvoked();
2349          } catch (CompletionException ok) {
2350 <            checkCompletedWithWrappedCFException(h, ex);
2351 <            r.assertNotInvoked();
2350 >            checkCompletedWithWrappedCFException(h4, ex);
2351 >            rs[4].assertNotInvoked();
2352          }
2304
2305        checkCompletedWithWrappedCFException(g, ex);
2306        checkCompletedNormally(f, v1);
2307    }}
2308
2309    public void testRunAfterEither_exceptionalCompletion4() {
2310        for (ExecutionMode m : ExecutionMode.values())
2311        for (Integer v1 : new Integer[] { 1, null })
2312    {
2313        final CompletableFuture<Integer> f = new CompletableFuture<>();
2314        final CompletableFuture<Integer> g = new CompletableFuture<>();
2315        final Noop r = new Noop(m);
2316        final CFException ex = new CFException();
2317
2318        f.completeExceptionally(ex);
2319        g.complete(v1);
2320        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2321
2322        // unspecified behavior
2323        Integer v;
2353          try {
2354 <            assertNull(h.join());
2355 <            r.assertInvoked();
2354 >            assertNull(h5.join());
2355 >            rs[5].assertInvoked();
2356          } catch (CompletionException ok) {
2357 <            checkCompletedWithWrappedCFException(h, ex);
2358 <            r.assertNotInvoked();
2357 >            checkCompletedWithWrappedCFException(h5, ex);
2358 >            rs[5].assertNotInvoked();
2359          }
2360  
2361          checkCompletedWithWrappedCFException(f, ex);
2362          checkCompletedNormally(g, v1);
2363 <    }}
2364 <
2365 <    /**
2366 <     * runAfterEither result completes exceptionally if action does
2367 <     */
2368 <    public void testRunAfterEither_actionFailed1() {
2340 <        for (ExecutionMode m : ExecutionMode.values())
2341 <        for (Integer v1 : new Integer[] { 1, null })
2342 <        for (Integer v2 : new Integer[] { 2, null })
2343 <    {
2344 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2345 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2346 <        final FailingRunnable r = new FailingRunnable(m);
2347 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2348 <
2349 <        f.complete(v1);
2350 <        checkCompletedWithWrappedCFException(h);
2351 <        g.complete(v2);
2352 <        checkCompletedNormally(f, v1);
2353 <        checkCompletedNormally(g, v2);
2354 <    }}
2355 <
2356 <    public void testRunAfterEither_actionFailed2() {
2357 <        for (ExecutionMode m : ExecutionMode.values())
2358 <        for (Integer v1 : new Integer[] { 1, null })
2359 <        for (Integer v2 : new Integer[] { 2, null })
2360 <    {
2361 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2362 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2363 <        final FailingRunnable r = new FailingRunnable(m);
2364 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2365 <
2366 <        g.complete(v2);
2367 <        checkCompletedWithWrappedCFException(h);
2368 <        f.complete(v1);
2369 <        checkCompletedNormally(f, v1);
2370 <        checkCompletedNormally(g, v2);
2363 >        checkCompletedWithWrappedCFException(h0, ex);
2364 >        checkCompletedWithWrappedCFException(h1, ex);
2365 >        checkCompletedWithWrappedCFException(h2, ex);
2366 >        checkCompletedWithWrappedCFException(h3, ex);
2367 >        checkCompletedWithWrappedCFException(h4, ex);
2368 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2369      }}
2370  
2371      /**
2372       * runAfterEither result completes exceptionally if either source cancelled
2373       */
2374 <    public void testRunAfterEither_sourceCancelled1() {
2374 >    public void testRunAfterEither_sourceCancelled() {
2375          for (ExecutionMode m : ExecutionMode.values())
2376          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2377          for (Integer v1 : new Integer[] { 1, null })
2378      {
2379          final CompletableFuture<Integer> f = new CompletableFuture<>();
2380          final CompletableFuture<Integer> g = new CompletableFuture<>();
2381 <        final Noop r = new Noop(m);
2382 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2381 >        final Noop[] rs = new Noop[6];
2382 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2383  
2384 <        assertTrue(f.cancel(mayInterruptIfRunning));
2385 <        checkCompletedWithWrappedCancellationException(h);
2386 <        g.complete(v1);
2387 <
2388 <        checkCancelled(f);
2389 <        r.assertNotInvoked();
2390 <        checkCompletedNormally(g, v1);
2391 <        checkCompletedWithWrappedCancellationException(h);
2392 <    }}
2393 <
2394 <    public void testRunAfterEither_sourceCancelled2() {
2395 <        for (ExecutionMode m : ExecutionMode.values())
2396 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2399 <        for (Integer v1 : new Integer[] { 1, null })
2400 <    {
2401 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2402 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2403 <        final Noop r = new Noop(m);
2404 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2405 <
2406 <        assertTrue(g.cancel(mayInterruptIfRunning));
2407 <        checkCompletedWithWrappedCancellationException(h);
2408 <        f.complete(v1);
2409 <
2410 <        checkCancelled(g);
2411 <        r.assertNotInvoked();
2412 <        checkCompletedNormally(f, v1);
2413 <        checkCompletedWithWrappedCancellationException(h);
2414 <    }}
2415 <
2416 <    public void testRunAfterEither_sourceCancelled3() {
2417 <        for (ExecutionMode m : ExecutionMode.values())
2418 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2419 <        for (Integer v1 : new Integer[] { 1, null })
2420 <    {
2421 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2422 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2423 <        final Noop r = new Noop(m);
2384 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2385 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2386 >        checkIncomplete(h0);
2387 >        checkIncomplete(h1);
2388 >        rs[0].assertNotInvoked();
2389 >        rs[1].assertNotInvoked();
2390 >        f.cancel(mayInterruptIfRunning);
2391 >        checkCompletedWithWrappedCancellationException(h0);
2392 >        checkCompletedWithWrappedCancellationException(h1);
2393 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2394 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2395 >        checkCompletedWithWrappedCancellationException(h2);
2396 >        checkCompletedWithWrappedCancellationException(h3);
2397  
2398 <        assertTrue(g.cancel(mayInterruptIfRunning));
2426 <        f.complete(v1);
2427 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2398 >        g.complete(v1);
2399  
2400 <        // unspecified behavior
2401 <        Integer v;
2400 >        // unspecified behavior - both source completions available
2401 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2402 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2403          try {
2404 <            assertNull(h.join());
2405 <            r.assertInvoked();
2404 >            assertNull(h4.join());
2405 >            rs[4].assertInvoked();
2406          } catch (CompletionException ok) {
2407 <            checkCompletedWithWrappedCancellationException(h);
2408 <            r.assertNotInvoked();
2407 >            checkCompletedWithWrappedCancellationException(h4);
2408 >            rs[4].assertNotInvoked();
2409 >        }
2410 >        try {
2411 >            assertNull(h5.join());
2412 >            rs[5].assertInvoked();
2413 >        } catch (CompletionException ok) {
2414 >            checkCompletedWithWrappedCancellationException(h5);
2415 >            rs[5].assertNotInvoked();
2416          }
2417  
2418 <        checkCancelled(g);
2419 <        checkCompletedNormally(f, v1);
2418 >        checkCancelled(f);
2419 >        checkCompletedNormally(g, v1);
2420 >        checkCompletedWithWrappedCancellationException(h0);
2421 >        checkCompletedWithWrappedCancellationException(h1);
2422 >        checkCompletedWithWrappedCancellationException(h2);
2423 >        checkCompletedWithWrappedCancellationException(h3);
2424 >        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2425      }}
2426  
2427 <    public void testRunAfterEither_sourceCancelled4() {
2427 >    /**
2428 >     * runAfterEither result completes exceptionally if action does
2429 >     */
2430 >    public void testRunAfterEither_actionFailed() {
2431          for (ExecutionMode m : ExecutionMode.values())
2445        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2432          for (Integer v1 : new Integer[] { 1, null })
2433 +        for (Integer v2 : new Integer[] { 2, null })
2434      {
2435          final CompletableFuture<Integer> f = new CompletableFuture<>();
2436          final CompletableFuture<Integer> g = new CompletableFuture<>();
2437 <        final Noop r = new Noop(m);
2437 >        final FailingRunnable[] rs = new FailingRunnable[6];
2438 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2439  
2440 <        assertTrue(f.cancel(mayInterruptIfRunning));
2441 <        g.complete(v1);
2442 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2443 <
2444 <        // unspecified behavior
2445 <        Integer v;
2446 <        try {
2447 <            assertNull(h.join());
2448 <            r.assertInvoked();
2449 <        } catch (CompletionException ok) {
2450 <            checkCompletedWithWrappedCancellationException(h);
2451 <            r.assertNotInvoked();
2452 <        }
2440 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2441 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2442 >        f.complete(v1);
2443 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2444 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2445 >        checkCompletedWithWrappedCFException(h0);
2446 >        checkCompletedWithWrappedCFException(h1);
2447 >        checkCompletedWithWrappedCFException(h2);
2448 >        checkCompletedWithWrappedCFException(h3);
2449 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2450 >        g.complete(v2);
2451 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2452 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2453 >        checkCompletedWithWrappedCFException(h4);
2454 >        checkCompletedWithWrappedCFException(h5);
2455  
2456 <        checkCancelled(f);
2457 <        checkCompletedNormally(g, v1);
2456 >        checkCompletedNormally(f, v1);
2457 >        checkCompletedNormally(g, v2);
2458 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2459      }}
2460  
2461      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines