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.63 by jsr166, Fri Jun 6 16:54:16 2014 UTC vs.
Revision 1.70 by jsr166, Fri Jun 6 19:48:38 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 1171 | Line 1171 | public class CompletableFutureTest exten
1171  
1172          checkCompletedNormally(g, inc(v1));
1173          checkCompletedNormally(f, v1);
1174 <        r.assertInvoked();
1174 >        r.assertValue(inc(v1));
1175      }}
1176  
1177      /**
# Line 1249 | 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 1258 | Line 1258 | public class CompletableFutureTest exten
1258          }
1259  
1260          checkCompletedNormally(g, null);
1261 +        r.assertValue(v1);
1262          checkCompletedNormally(f, v1);
1262        r.assertInvoked();
1263        r.assertValue(inc(v1));
1263      }}
1264  
1265      /**
# Line 1273 | 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 1295 | 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 1357 | Line 1356 | public class CompletableFutureTest exten
1356          checkCompletedNormally(h, subtract(v1, v2));
1357          checkCompletedNormally(f, v1);
1358          checkCompletedNormally(g, v2);
1359 <        r.assertInvoked();
1359 >        r.assertValue(subtract(v1, v2));
1360      }}
1361  
1362      /**
# Line 1766 | 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 1789 | 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 1822 | 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 1844 | Line 1900 | public class CompletableFutureTest exten
1900          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
1901      }}
1902  
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 })
1908 +    {
1909 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1910 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
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 +        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 +
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 +        checkCancelled(g);
1958 +    }}
1959 +
1960      /**
1961       * applyToEither result completes exceptionally if action does
1962       */
# Line 1896 | Line 2009 | public class CompletableFutureTest exten
2009      {
2010          final CompletableFuture<Integer> f = new CompletableFuture<>();
2011          final CompletableFuture<Integer> g = new CompletableFuture<>();
2012 <        final IncAction[] rs = new IncAction[6];
2013 <        for (int i = 0; i < rs.length; i++) rs[i] = 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]);
# Line 1908 | Line 2021 | public class CompletableFutureTest exten
2021          f.complete(v1);
2022          checkCompletedNormally(h0, null);
2023          checkCompletedNormally(h1, null);
2024 <        rs[0].assertValue(inc(v1));
2025 <        rs[1].assertValue(inc(v1));
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(inc(v1));
2031 <        rs[3].assertValue(inc(v1));
2030 >        rs[2].assertValue(v1);
2031 >        rs[3].assertValue(v1);
2032          g.complete(v2);
2033  
2034          // unspecified behavior - both source completions available
# Line 1923 | Line 2036 | public class CompletableFutureTest exten
2036          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2037          checkCompletedNormally(h4, null);
2038          checkCompletedNormally(h5, null);
2039 <        assertTrue(Objects.equals(inc(v1), rs[4].value) ||
2040 <                   Objects.equals(inc(v2), rs[4].value));
2041 <        assertTrue(Objects.equals(inc(v1), rs[5].value) ||
2042 <                   Objects.equals(inc(v2), rs[5].value));
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);
# Line 1934 | Line 2047 | public class CompletableFutureTest exten
2047          checkCompletedNormally(h1, null);
2048          checkCompletedNormally(h2, null);
2049          checkCompletedNormally(h3, null);
2050 <        for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1));
2050 >        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2051      }}
2052  
2053      /**
# Line 1948 | Line 2061 | public class CompletableFutureTest exten
2061          final CompletableFuture<Integer> f = new CompletableFuture<>();
2062          final CompletableFuture<Integer> g = new CompletableFuture<>();
2063          final CFException ex = new CFException();
2064 <        final IncAction[] rs = new IncAction[6];
2065 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
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]);
# Line 1972 | Line 2085 | public class CompletableFutureTest exten
2085          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2086          try {
2087              assertNull(h4.join());
2088 <            rs[4].assertValue(inc(v1));
2088 >            rs[4].assertValue(v1);
2089          } catch (CompletionException ok) {
2090              checkCompletedWithWrappedCFException(h4, ex);
2091              rs[4].assertNotInvoked();
2092          }
2093          try {
2094              assertNull(h5.join());
2095 <            rs[5].assertValue(inc(v1));
2095 >            rs[5].assertValue(v1);
2096          } catch (CompletionException ok) {
2097              checkCompletedWithWrappedCFException(h5, ex);
2098              rs[5].assertNotInvoked();
# Line 1995 | Line 2108 | public class CompletableFutureTest exten
2108          for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2109      }}
2110  
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 })
2115 +    {
2116 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2117 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
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 +        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 +        // 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 +
2164 +        checkCompletedNormally(f, v1);
2165 +        checkCompletedWithWrappedCFException(g, ex);
2166 +    }}
2167 +
2168      /**
2169       * acceptEither result completes exceptionally if either source cancelled
2170       */
# Line 2005 | Line 2175 | public class CompletableFutureTest exten
2175      {
2176          final CompletableFuture<Integer> f = new CompletableFuture<>();
2177          final CompletableFuture<Integer> g = new CompletableFuture<>();
2178 <        final IncAction[] rs = new IncAction[6];
2179 <        for (int i = 0; i < rs.length; i++) rs[i] = new IncAction(m);
2178 >        final NoopConsumer[] rs = new NoopConsumer[6];
2179 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
2180  
2181          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2182          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
# Line 2029 | Line 2199 | public class CompletableFutureTest exten
2199          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2200          try {
2201              assertNull(h4.join());
2202 <            rs[4].assertValue(inc(v1));
2202 >            rs[4].assertValue(v1);
2203          } catch (CompletionException ok) {
2204              checkCompletedWithWrappedCancellationException(h4);
2205              rs[4].assertNotInvoked();
2206          }
2207          try {
2208              assertNull(h5.join());
2209 <            rs[5].assertValue(inc(v1));
2209 >            rs[5].assertValue(v1);
2210          } catch (CompletionException ok) {
2211              checkCompletedWithWrappedCancellationException(h5);
2212              rs[5].assertNotInvoked();
# Line 2096 | Line 2266 | public class CompletableFutureTest exten
2266       * runAfterEither result completes normally after normal completion
2267       * of either source
2268       */
2269 <    public void testRunAfterEither_normalCompletion1() {
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);
2277 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
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(h, null);
2287 <        r.assertInvoked();
2288 <        g.complete(v2);
2289 <
2290 <        checkCompletedNormally(f, v1);
2291 <        checkCompletedNormally(g, v2);
2292 <        checkCompletedNormally(h, null);
2293 <        r.assertInvoked();
2294 <    }}
2295 <
2120 <    public void testRunAfterEither_normalCompletion2() {
2121 <        for (ExecutionMode m : ExecutionMode.values())
2122 <        for (Integer v1 : new Integer[] { 1, null })
2123 <        for (Integer v2 : new Integer[] { 2, null })
2124 <    {
2125 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2126 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2127 <        final Noop r = new Noop(m);
2128 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
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);
2131        checkCompletedNormally(h, null);
2132        r.assertInvoked();
2133        f.complete(v1);
2134
2135        checkCompletedNormally(f, v1);
2136        checkCompletedNormally(g, v2);
2137        checkCompletedNormally(h, null);
2138        r.assertInvoked();
2139        }}
2140
2141    public void testRunAfterEither_normalCompletion3() {
2142        for (ExecutionMode m : ExecutionMode.values())
2143        for (Integer v1 : new Integer[] { 1, null })
2144        for (Integer v2 : new Integer[] { 2, null })
2145    {
2146        final CompletableFuture<Integer> f = new CompletableFuture<>();
2147        final CompletableFuture<Integer> g = new CompletableFuture<>();
2148        final Noop r = new Noop(m);
2298  
2299 <        f.complete(v1);
2300 <        g.complete(v2);
2152 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2299 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2300 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2301  
2154        checkCompletedNormally(h, null);
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<>();
2170        final Noop r = new Noop(m);
2171        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);
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.complete(v1);
2342  
2343 <        r.assertNotInvoked();
2344 <        checkCompletedNormally(g, v1);
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(h4.join());
2348 >            rs[4].assertInvoked();
2349 >        } catch (CompletionException ok) {
2350 >            checkCompletedWithWrappedCFException(h4, ex);
2351 >            rs[4].assertNotInvoked();
2352 >        }
2353 >        try {
2354 >            assertNull(h5.join());
2355 >            rs[5].assertInvoked();
2356 >        } catch (CompletionException ok) {
2357 >            checkCompletedWithWrappedCFException(h5, ex);
2358 >            rs[5].assertNotInvoked();
2359 >        }
2360 >
2361          checkCompletedWithWrappedCFException(f, ex);
2362 <        checkCompletedWithWrappedCFException(h, ex);
2362 >        checkCompletedNormally(g, v1);
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      public void testRunAfterEither_exceptionalCompletion2() {
2372          for (ExecutionMode m : ExecutionMode.values())
2373 +        for (boolean fFirst : new boolean[] { true, false })
2374          for (Integer v1 : new Integer[] { 1, null })
2375      {
2376          final CompletableFuture<Integer> f = new CompletableFuture<>();
2377          final CompletableFuture<Integer> g = new CompletableFuture<>();
2190        final Noop r = new Noop(m);
2191        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2192        final CFException ex = new CFException();
2193
2194        g.completeExceptionally(ex);
2195        checkCompletedWithWrappedCFException(h, ex);
2196        f.complete(v1);
2197
2198        r.assertNotInvoked();
2199        checkCompletedNormally(f, v1);
2200        checkCompletedWithWrappedCFException(g, ex);
2201        checkCompletedWithWrappedCFException(h, ex);
2202    }}
2203
2204    public void testRunAfterEither_exceptionalCompletion3() {
2205        for (ExecutionMode m : ExecutionMode.values())
2206        for (Integer v1 : new Integer[] { 1, null })
2207    {
2208        final CompletableFuture<Integer> f = new CompletableFuture<>();
2209        final CompletableFuture<Integer> g = new CompletableFuture<>();
2210        final Noop r = new Noop(m);
2378          final CFException ex = new CFException();
2379 +        final Noop[] rs = new Noop[6];
2380 +        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2381  
2382 <        g.completeExceptionally(ex);
2383 <        f.complete(v1);
2384 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2382 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2383 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2384 >        if (fFirst) {
2385 >            f.complete(v1);
2386 >            g.completeExceptionally(ex);
2387 >        } else {
2388 >            g.completeExceptionally(ex);
2389 >            f.complete(v1);
2390 >        }
2391 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2392 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2393  
2394 <        // unspecified behavior
2218 <        Integer v;
2394 >        // unspecified behavior - both source completions available
2395          try {
2396 <            assertNull(h.join());
2397 <            r.assertInvoked();
2396 >            assertEquals(null, h0.join());
2397 >            rs[0].assertInvoked();
2398          } catch (CompletionException ok) {
2399 <            checkCompletedWithWrappedCFException(h, ex);
2400 <            r.assertNotInvoked();
2399 >            checkCompletedWithWrappedCFException(h0, ex);
2400 >            rs[0].assertNotInvoked();
2401 >        }
2402 >        try {
2403 >            assertEquals(null, h1.join());
2404 >            rs[1].assertInvoked();
2405 >        } catch (CompletionException ok) {
2406 >            checkCompletedWithWrappedCFException(h1, ex);
2407 >            rs[1].assertNotInvoked();
2408 >        }
2409 >        try {
2410 >            assertEquals(null, h2.join());
2411 >            rs[2].assertInvoked();
2412 >        } catch (CompletionException ok) {
2413 >            checkCompletedWithWrappedCFException(h2, ex);
2414 >            rs[2].assertNotInvoked();
2415 >        }
2416 >        try {
2417 >            assertEquals(null, h3.join());
2418 >            rs[3].assertInvoked();
2419 >        } catch (CompletionException ok) {
2420 >            checkCompletedWithWrappedCFException(h3, ex);
2421 >            rs[3].assertNotInvoked();
2422          }
2423  
2227        checkCompletedWithWrappedCFException(g, ex);
2424          checkCompletedNormally(f, v1);
2425 +        checkCompletedWithWrappedCFException(g, ex);
2426      }}
2427  
2428 <    public void testRunAfterEither_exceptionalCompletion4() {
2428 >    /**
2429 >     * runAfterEither result completes exceptionally if either source cancelled
2430 >     */
2431 >    public void testRunAfterEither_sourceCancelled() {
2432          for (ExecutionMode m : ExecutionMode.values())
2433 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2434          for (Integer v1 : new Integer[] { 1, null })
2435      {
2436          final CompletableFuture<Integer> f = new CompletableFuture<>();
2437          final CompletableFuture<Integer> g = new CompletableFuture<>();
2438 <        final Noop r = new Noop(m);
2439 <        final CFException ex = new CFException();
2438 >        final Noop[] rs = new Noop[6];
2439 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
2440 >
2441 >        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2442 >        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2443 >        checkIncomplete(h0);
2444 >        checkIncomplete(h1);
2445 >        rs[0].assertNotInvoked();
2446 >        rs[1].assertNotInvoked();
2447 >        f.cancel(mayInterruptIfRunning);
2448 >        checkCompletedWithWrappedCancellationException(h0);
2449 >        checkCompletedWithWrappedCancellationException(h1);
2450 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2451 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2452 >        checkCompletedWithWrappedCancellationException(h2);
2453 >        checkCompletedWithWrappedCancellationException(h3);
2454  
2240        f.completeExceptionally(ex);
2455          g.complete(v1);
2242        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2456  
2457 <        // unspecified behavior
2458 <        Integer v;
2457 >        // unspecified behavior - both source completions available
2458 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2459 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2460          try {
2461 <            assertNull(h.join());
2462 <            r.assertInvoked();
2461 >            assertNull(h4.join());
2462 >            rs[4].assertInvoked();
2463          } catch (CompletionException ok) {
2464 <            checkCompletedWithWrappedCFException(h, ex);
2465 <            r.assertNotInvoked();
2464 >            checkCompletedWithWrappedCancellationException(h4);
2465 >            rs[4].assertNotInvoked();
2466 >        }
2467 >        try {
2468 >            assertNull(h5.join());
2469 >            rs[5].assertInvoked();
2470 >        } catch (CompletionException ok) {
2471 >            checkCompletedWithWrappedCancellationException(h5);
2472 >            rs[5].assertNotInvoked();
2473          }
2474  
2475 <        checkCompletedWithWrappedCFException(f, ex);
2475 >        checkCancelled(f);
2476          checkCompletedNormally(g, v1);
2477 +        checkCompletedWithWrappedCancellationException(h0);
2478 +        checkCompletedWithWrappedCancellationException(h1);
2479 +        checkCompletedWithWrappedCancellationException(h2);
2480 +        checkCompletedWithWrappedCancellationException(h3);
2481 +        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
2482      }}
2483  
2484      /**
2485       * runAfterEither result completes exceptionally if action does
2486       */
2487 <    public void testRunAfterEither_actionFailed1() {
2487 >    public void testRunAfterEither_actionFailed() {
2488          for (ExecutionMode m : ExecutionMode.values())
2489          for (Integer v1 : new Integer[] { 1, null })
2490          for (Integer v2 : new Integer[] { 2, null })
2491      {
2492          final CompletableFuture<Integer> f = new CompletableFuture<>();
2493          final CompletableFuture<Integer> g = new CompletableFuture<>();
2494 <        final FailingRunnable r = new FailingRunnable(m);
2495 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2494 >        final FailingRunnable[] rs = new FailingRunnable[6];
2495 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
2496  
2497 +        final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2498 +        final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2499          f.complete(v1);
2500 <        checkCompletedWithWrappedCFException(h);
2500 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2501 >        final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2502 >        checkCompletedWithWrappedCFException(h0);
2503 >        checkCompletedWithWrappedCFException(h1);
2504 >        checkCompletedWithWrappedCFException(h2);
2505 >        checkCompletedWithWrappedCFException(h3);
2506 >        for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2507          g.complete(v2);
2508 <        checkCompletedNormally(f, v1);
2509 <        checkCompletedNormally(g, v2);
2510 <    }}
2511 <
2278 <    public void testRunAfterEither_actionFailed2() {
2279 <        for (ExecutionMode m : ExecutionMode.values())
2280 <        for (Integer v1 : new Integer[] { 1, null })
2281 <        for (Integer v2 : new Integer[] { 2, null })
2282 <    {
2283 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2284 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2285 <        final FailingRunnable r = new FailingRunnable(m);
2286 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2508 >        final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2509 >        final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2510 >        checkCompletedWithWrappedCFException(h4);
2511 >        checkCompletedWithWrappedCFException(h5);
2512  
2288        g.complete(v2);
2289        checkCompletedWithWrappedCFException(h);
2290        f.complete(v1);
2513          checkCompletedNormally(f, v1);
2514          checkCompletedNormally(g, v2);
2515 <    }}
2294 <
2295 <    /**
2296 <     * runAfterEither result completes exceptionally if either source cancelled
2297 <     */
2298 <    public void testRunAfterEither_sourceCancelled1() {
2299 <        for (ExecutionMode m : ExecutionMode.values())
2300 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2301 <        for (Integer v1 : new Integer[] { 1, null })
2302 <    {
2303 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2304 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2305 <        final Noop r = new Noop(m);
2306 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2307 <
2308 <        assertTrue(f.cancel(mayInterruptIfRunning));
2309 <        checkCompletedWithWrappedCancellationException(h);
2310 <        g.complete(v1);
2311 <
2312 <        checkCancelled(f);
2313 <        r.assertNotInvoked();
2314 <        checkCompletedNormally(g, v1);
2315 <        checkCompletedWithWrappedCancellationException(h);
2316 <    }}
2317 <
2318 <    public void testRunAfterEither_sourceCancelled2() {
2319 <        for (ExecutionMode m : ExecutionMode.values())
2320 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2321 <        for (Integer v1 : new Integer[] { 1, null })
2322 <    {
2323 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2324 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2325 <        final Noop r = new Noop(m);
2326 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2327 <
2328 <        assertTrue(g.cancel(mayInterruptIfRunning));
2329 <        checkCompletedWithWrappedCancellationException(h);
2330 <        f.complete(v1);
2331 <
2332 <        checkCancelled(g);
2333 <        r.assertNotInvoked();
2334 <        checkCompletedNormally(f, v1);
2335 <        checkCompletedWithWrappedCancellationException(h);
2336 <    }}
2337 <
2338 <    public void testRunAfterEither_sourceCancelled3() {
2339 <        for (ExecutionMode m : ExecutionMode.values())
2340 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2341 <        for (Integer v1 : new Integer[] { 1, null })
2342 <    {
2343 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2344 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2345 <        final Noop r = new Noop(m);
2346 <
2347 <        assertTrue(g.cancel(mayInterruptIfRunning));
2348 <        f.complete(v1);
2349 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2350 <
2351 <        // unspecified behavior
2352 <        Integer v;
2353 <        try {
2354 <            assertNull(h.join());
2355 <            r.assertInvoked();
2356 <        } catch (CompletionException ok) {
2357 <            checkCompletedWithWrappedCancellationException(h);
2358 <            r.assertNotInvoked();
2359 <        }
2360 <
2361 <        checkCancelled(g);
2362 <        checkCompletedNormally(f, v1);
2363 <    }}
2364 <
2365 <    public void testRunAfterEither_sourceCancelled4() {
2366 <        for (ExecutionMode m : ExecutionMode.values())
2367 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2368 <        for (Integer v1 : new Integer[] { 1, null })
2369 <    {
2370 <        final CompletableFuture<Integer> f = new CompletableFuture<>();
2371 <        final CompletableFuture<Integer> g = new CompletableFuture<>();
2372 <        final Noop r = new Noop(m);
2373 <
2374 <        assertTrue(f.cancel(mayInterruptIfRunning));
2375 <        g.complete(v1);
2376 <        final CompletableFuture<Void> h = m.runAfterEither(f, g, r);
2377 <
2378 <        // unspecified behavior
2379 <        Integer v;
2380 <        try {
2381 <            assertNull(h.join());
2382 <            r.assertInvoked();
2383 <        } catch (CompletionException ok) {
2384 <            checkCompletedWithWrappedCancellationException(h);
2385 <            r.assertNotInvoked();
2386 <        }
2387 <
2388 <        checkCancelled(f);
2389 <        checkCompletedNormally(g, v1);
2515 >        for (int i = 0; i < 6; i++) rs[i].assertInvoked();
2516      }}
2517  
2518      /**
# Line 2405 | Line 2531 | public class CompletableFutureTest exten
2531  
2532          checkCompletedNormally(g, inc(v1));
2533          checkCompletedNormally(f, v1);
2534 <        r.assertInvoked();
2534 >        r.assertValue(v1);
2535      }}
2536  
2537      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines