ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.38 by jsr166, Sun Jun 1 23:51:44 2014 UTC vs.
Revision 1.40 by jsr166, Mon Jun 2 01:11:53 2014 UTC

# Line 1844 | Line 1844 | public class CompletableFutureTest exten
1844       * applyToEither result completes normally after normal completion
1845       * of either source
1846       */
1847 <    public void testApplyToEither() {
1848 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1849 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1850 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1851 <        f.complete(one);
1852 <        checkCompletedNormally(g, two);
1853 <        f2.complete(one);
1854 <        checkCompletedNormally(g, two);
1855 <
1856 <        f = new CompletableFuture<>();
1857 <        f.complete(one);
1858 <        f2 = new CompletableFuture<>();
1859 <        g = f.applyToEither(f2, inc);
1860 <        checkCompletedNormally(g, two);
1847 >    public void testApplyToEither_normalCompletion1() {
1848 >        for (ExecutionMode m : ExecutionMode.values())
1849 >        for (Integer v1 : new Integer[] { 1, null })
1850 >        for (Integer v2 : new Integer[] { 2, null }) {
1851 >
1852 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1853 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1854 >        final IncFunction r = new IncFunction();
1855 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1856 >
1857 >        f.complete(v1);
1858 >        checkCompletedNormally(h, inc(v1));
1859 >        g.complete(v2);
1860 >
1861 >        checkCompletedNormally(f, v1);
1862 >        checkCompletedNormally(g, v2);
1863 >        checkCompletedNormally(h, inc(v1));
1864 >        }
1865 >    }
1866 >
1867 >    public void testApplyToEither_normalCompletion2() {
1868 >        for (ExecutionMode m : ExecutionMode.values())
1869 >        for (Integer v1 : new Integer[] { 1, null })
1870 >        for (Integer v2 : new Integer[] { 2, null }) {
1871 >
1872 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1873 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1874 >        final IncFunction r = new IncFunction();
1875 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1876 >
1877 >        g.complete(v2);
1878 >        checkCompletedNormally(h, inc(v2));
1879 >        f.complete(v1);
1880 >
1881 >        checkCompletedNormally(f, v1);
1882 >        checkCompletedNormally(g, v2);
1883 >        checkCompletedNormally(h, inc(v2));
1884 >        }
1885 >    }
1886 >    public void testApplyToEither_normalCompletion3() {
1887 >        for (ExecutionMode m : ExecutionMode.values())
1888 >        for (Integer v1 : new Integer[] { 1, null })
1889 >        for (Integer v2 : new Integer[] { 2, null }) {
1890 >
1891 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1892 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1893 >        final IncFunction r = new IncFunction();
1894 >
1895 >        f.complete(v1);
1896 >        g.complete(v2);
1897 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1898 >
1899 >        checkCompletedNormally(f, v1);
1900 >        checkCompletedNormally(g, v2);
1901 >
1902 >        // unspecified behavior
1903 >        assertTrue(Objects.equals(h.join(), inc(v1)) ||
1904 >                   Objects.equals(h.join(), inc(v2)));
1905 >        }
1906      }
1907  
1908      /**
1909       * applyToEither result completes exceptionally after exceptional
1910       * completion of either source
1911       */
1912 <    public void testApplyToEither2() {
1913 <        CompletableFuture<Integer> f = new CompletableFuture<>();
1914 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
1915 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
1916 <        f.completeExceptionally(new CFException());
1917 <        f2.complete(one);
1918 <        checkCompletedWithWrappedCFException(g);
1912 >    public void testApplyToEither_exceptionalCompletion1() {
1913 >        for (ExecutionMode m : ExecutionMode.values())
1914 >        for (Integer v1 : new Integer[] { 1, null }) {
1915 >
1916 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1917 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1918 >        final IncFunction r = new IncFunction();
1919 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1920 >        final CFException ex = new CFException();
1921 >
1922 >        f.completeExceptionally(ex);
1923 >        checkCompletedWithWrappedCFException(h, ex);
1924 >        g.complete(v1);
1925 >
1926 >        assertFalse(r.ran());
1927 >        checkCompletedNormally(g, v1);
1928 >        checkCompletedWithWrappedCFException(f, ex);
1929 >        checkCompletedWithWrappedCFException(h, ex);
1930 >        }
1931 >    }
1932 >
1933 >    public void testApplyToEither_exceptionalCompletion2() {
1934 >        for (ExecutionMode m : ExecutionMode.values())
1935 >        for (Integer v1 : new Integer[] { 1, null }) {
1936 >
1937 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1938 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1939 >        final IncFunction r = new IncFunction();
1940 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1941 >        final CFException ex = new CFException();
1942 >
1943 >        g.completeExceptionally(ex);
1944 >        checkCompletedWithWrappedCFException(h, ex);
1945 >        f.complete(v1);
1946 >
1947 >        assertFalse(r.ran());
1948 >        checkCompletedNormally(f, v1);
1949 >        checkCompletedWithWrappedCFException(g, ex);
1950 >        checkCompletedWithWrappedCFException(h, ex);
1951 >        }
1952 >    }
1953 >
1954 >    public void testApplyToEither_exceptionalCompletion3() {
1955 >        for (ExecutionMode m : ExecutionMode.values())
1956 >        for (Integer v1 : new Integer[] { 1, null }) {
1957 >
1958 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1959 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1960 >        final IncFunction r = new IncFunction();
1961 >        final CFException ex = new CFException();
1962 >
1963 >        g.completeExceptionally(ex);
1964 >        f.complete(v1);
1965 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1966  
1967 <        f = new CompletableFuture<>();
1968 <        f2 = new CompletableFuture<>();
1969 <        f2.completeExceptionally(new CFException());
1970 <        g = f.applyToEither(f2, inc);
1971 <        checkCompletedWithWrappedCFException(g);
1967 >        // unspecified behavior
1968 >        Integer v;
1969 >        try {
1970 >            assertEquals(h.join(), inc(v1));
1971 >            assertTrue(r.ran());
1972 >        } catch (CompletionException ok) {
1973 >            checkCompletedWithWrappedCFException(h, ex);
1974 >            assertFalse(r.ran());
1975 >        }
1976 >
1977 >        checkCompletedWithWrappedCFException(g, ex);
1978 >        checkCompletedNormally(f, v1);
1979 >        }
1980 >    }
1981 >
1982 >    public void testApplyToEither_exceptionalCompletion4() {
1983 >        for (ExecutionMode m : ExecutionMode.values())
1984 >        for (Integer v1 : new Integer[] { 1, null }) {
1985 >
1986 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1987 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
1988 >        final IncFunction r = new IncFunction();
1989 >        final CFException ex = new CFException();
1990 >
1991 >        f.completeExceptionally(ex);
1992 >        g.complete(v1);
1993 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
1994 >
1995 >        // unspecified behavior
1996 >        Integer v;
1997 >        try {
1998 >            assertEquals(h.join(), inc(v1));
1999 >            assertTrue(r.ran());
2000 >        } catch (CompletionException ok) {
2001 >            checkCompletedWithWrappedCFException(h, ex);
2002 >            assertFalse(r.ran());
2003 >        }
2004 >
2005 >        checkCompletedWithWrappedCFException(f, ex);
2006 >        assertFalse(r.ran());
2007 >        checkCompletedNormally(g, v1);
2008 >        }
2009      }
2010  
2011      /**
2012       * applyToEither result completes exceptionally if action does
2013       */
2014 <    public void testApplyToEither3() {
2015 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2016 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2017 <        FailingFunction r = new FailingFunction();
2018 <        CompletableFuture<Integer> g = f.applyToEither(f2, r);
2019 <        f2.complete(two);
2020 <        checkCompletedWithWrappedCFException(g);
2014 >    public void testApplyToEither_actionFailed1() {
2015 >        for (ExecutionMode m : ExecutionMode.values())
2016 >        for (Integer v1 : new Integer[] { 1, null })
2017 >        for (Integer v2 : new Integer[] { 2, null }) {
2018 >
2019 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2020 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2021 >        final FailingFunction r = new FailingFunction();
2022 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2023 >
2024 >        f.complete(v1);
2025 >        checkCompletedWithWrappedCFException(h);
2026 >        g.complete(v2);
2027 >        checkCompletedNormally(f, v1);
2028 >        checkCompletedNormally(g, v2);
2029 >        }
2030 >    }
2031 >
2032 >    public void testApplyToEither_actionFailed2() {
2033 >        for (ExecutionMode m : ExecutionMode.values())
2034 >        for (Integer v1 : new Integer[] { 1, null })
2035 >        for (Integer v2 : new Integer[] { 2, null }) {
2036 >
2037 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2038 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2039 >        final FailingFunction r = new FailingFunction();
2040 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2041 >
2042 >        g.complete(v2);
2043 >        checkCompletedWithWrappedCFException(h);
2044 >        f.complete(v1);
2045 >        checkCompletedNormally(f, v1);
2046 >        checkCompletedNormally(g, v2);
2047 >        }
2048      }
2049  
2050      /**
2051       * applyToEither result completes exceptionally if either source cancelled
2052       */
2053 <    public void testApplyToEither4() {
2054 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2055 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2056 <        CompletableFuture<Integer> g = f.applyToEither(f2, inc);
2057 <        assertTrue(f.cancel(true));
2058 <        checkCompletedWithWrappedCancellationException(g);
2059 <        f = new CompletableFuture<>();
2060 <        f2 = new CompletableFuture<>();
2061 <        assertTrue(f2.cancel(true));
2062 <        checkCompletedWithWrappedCancellationException(g);
2053 >    public void testApplyToEither_sourceCancelled1() {
2054 >        for (ExecutionMode m : ExecutionMode.values())
2055 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2056 >        for (Integer v1 : new Integer[] { 1, null }) {
2057 >
2058 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2059 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2060 >        final IncFunction r = new IncFunction();
2061 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2062 >
2063 >        assertTrue(f.cancel(mayInterruptIfRunning));
2064 >        checkCompletedWithWrappedCancellationException(h);
2065 >        g.complete(v1);
2066 >
2067 >        checkCancelled(f);
2068 >        assertFalse(r.ran());
2069 >        checkCompletedNormally(g, v1);
2070 >        checkCompletedWithWrappedCancellationException(h);
2071 >        }
2072 >    }
2073 >
2074 >    public void testApplyToEither_sourceCancelled2() {
2075 >        for (ExecutionMode m : ExecutionMode.values())
2076 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2077 >        for (Integer v1 : new Integer[] { 1, null }) {
2078 >
2079 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2080 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2081 >        final IncFunction r = new IncFunction();
2082 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2083 >
2084 >        assertTrue(g.cancel(mayInterruptIfRunning));
2085 >        checkCompletedWithWrappedCancellationException(h);
2086 >        f.complete(v1);
2087 >
2088 >        checkCancelled(g);
2089 >        assertFalse(r.ran());
2090 >        checkCompletedNormally(f, v1);
2091 >        checkCompletedWithWrappedCancellationException(h);
2092 >        }
2093 >    }
2094 >
2095 >    public void testApplyToEither_sourceCancelled3() {
2096 >        for (ExecutionMode m : ExecutionMode.values())
2097 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2098 >        for (Integer v1 : new Integer[] { 1, null }) {
2099 >
2100 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2101 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2102 >        final IncFunction r = new IncFunction();
2103 >
2104 >        assertTrue(g.cancel(mayInterruptIfRunning));
2105 >        f.complete(v1);
2106 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2107 >
2108 >        // unspecified behavior
2109 >        Integer v;
2110 >        try {
2111 >            assertEquals(h.join(), inc(v1));
2112 >            assertTrue(r.ran());
2113 >        } catch (CompletionException ok) {
2114 >            checkCompletedWithWrappedCancellationException(h);
2115 >            assertFalse(r.ran());
2116 >        }
2117 >
2118 >        checkCancelled(g);
2119 >        checkCompletedNormally(f, v1);
2120 >        }
2121 >    }
2122 >
2123 >    public void testApplyToEither_sourceCancelled4() {
2124 >        for (ExecutionMode m : ExecutionMode.values())
2125 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2126 >        for (Integer v1 : new Integer[] { 1, null }) {
2127 >
2128 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2129 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2130 >        final IncFunction r = new IncFunction();
2131 >
2132 >        assertTrue(f.cancel(mayInterruptIfRunning));
2133 >        g.complete(v1);
2134 >        final CompletableFuture<Integer> h = m.applyToEither(f, g, r);
2135 >
2136 >        // unspecified behavior
2137 >        Integer v;
2138 >        try {
2139 >            assertEquals(h.join(), inc(v1));
2140 >            assertTrue(r.ran());
2141 >        } catch (CompletionException ok) {
2142 >            checkCompletedWithWrappedCancellationException(h);
2143 >            assertFalse(r.ran());
2144 >        }
2145 >
2146 >        checkCancelled(f);
2147 >        checkCompletedNormally(g, v1);
2148 >        }
2149      }
2150  
2151      /**
2152       * acceptEither result completes normally after normal completion
2153       * of either source
2154       */
2155 <    public void testAcceptEither() {
2156 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2157 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2158 <        IncAction r = new IncAction();
2159 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2160 <        f.complete(one);
2161 <        checkCompletedNormally(g, null);
2162 <        f2.complete(one);
2163 <        checkCompletedNormally(g, null);
2164 <        assertEquals(r.value, (Integer) 2);
2165 <
2166 <        r = new IncAction();
2167 <        f = new CompletableFuture<>();
2168 <        f.complete(one);
2169 <        f2 = new CompletableFuture<>();
2170 <        g = f.acceptEither(f2, r);
2171 <        checkCompletedNormally(g, null);
2172 <        assertEquals(r.value, (Integer) 2);
2155 >    public void testAcceptEither_normalCompletion1() {
2156 >        for (ExecutionMode m : ExecutionMode.values())
2157 >        for (Integer v1 : new Integer[] { 1, null })
2158 >        for (Integer v2 : new Integer[] { 2, null }) {
2159 >
2160 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2161 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2162 >        final IncAction r = new IncAction();
2163 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2164 >
2165 >        f.complete(v1);
2166 >        checkCompletedNormally(h, null);
2167 >        assertEquals(r.value, inc(v1));
2168 >        g.complete(v2);
2169 >
2170 >        checkCompletedNormally(f, v1);
2171 >        checkCompletedNormally(g, v2);
2172 >        checkCompletedNormally(h, null);
2173 >        }
2174 >    }
2175 >
2176 >    public void testAcceptEither_normalCompletion2() {
2177 >        for (ExecutionMode m : ExecutionMode.values())
2178 >        for (Integer v1 : new Integer[] { 1, null })
2179 >        for (Integer v2 : new Integer[] { 2, null }) {
2180 >
2181 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2182 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2183 >        final IncAction r = new IncAction();
2184 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2185 >
2186 >        g.complete(v2);
2187 >        checkCompletedNormally(h, null);
2188 >        assertEquals(r.value, inc(v2));
2189 >        f.complete(v1);
2190 >
2191 >        checkCompletedNormally(f, v1);
2192 >        checkCompletedNormally(g, v2);
2193 >        checkCompletedNormally(h, null);
2194 >        }
2195 >    }
2196 >    public void testAcceptEither_normalCompletion3() {
2197 >        for (ExecutionMode m : ExecutionMode.values())
2198 >        for (Integer v1 : new Integer[] { 1, null })
2199 >        for (Integer v2 : new Integer[] { 2, null }) {
2200 >
2201 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2202 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2203 >        final IncAction r = new IncAction();
2204 >
2205 >        f.complete(v1);
2206 >        g.complete(v2);
2207 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2208 >
2209 >        checkCompletedNormally(h, null);
2210 >        checkCompletedNormally(f, v1);
2211 >        checkCompletedNormally(g, v2);
2212 >
2213 >        // unspecified behavior
2214 >        assertTrue(Objects.equals(r.value, inc(v1)) ||
2215 >                   Objects.equals(r.value, inc(v2)));
2216 >        }
2217      }
2218  
2219      /**
2220       * acceptEither result completes exceptionally after exceptional
2221       * completion of either source
2222       */
2223 <    public void testAcceptEither2() {
2224 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2225 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2226 <        IncAction r = new IncAction();
2227 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2228 <        f.completeExceptionally(new CFException());
2229 <        f2.complete(one);
2230 <        checkCompletedWithWrappedCFException(g);
2223 >    public void testAcceptEither_exceptionalCompletion1() {
2224 >        for (ExecutionMode m : ExecutionMode.values())
2225 >        for (Integer v1 : new Integer[] { 1, null }) {
2226 >
2227 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2228 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2229 >        final IncAction r = new IncAction();
2230 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2231 >        final CFException ex = new CFException();
2232 >
2233 >        f.completeExceptionally(ex);
2234 >        checkCompletedWithWrappedCFException(h, ex);
2235 >        g.complete(v1);
2236 >
2237 >        assertFalse(r.ran());
2238 >        checkCompletedNormally(g, v1);
2239 >        checkCompletedWithWrappedCFException(f, ex);
2240 >        checkCompletedWithWrappedCFException(h, ex);
2241 >        }
2242 >    }
2243 >
2244 >    public void testAcceptEither_exceptionalCompletion2() {
2245 >        for (ExecutionMode m : ExecutionMode.values())
2246 >        for (Integer v1 : new Integer[] { 1, null }) {
2247 >
2248 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2249 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2250 >        final IncAction r = new IncAction();
2251 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2252 >        final CFException ex = new CFException();
2253 >
2254 >        g.completeExceptionally(ex);
2255 >        checkCompletedWithWrappedCFException(h, ex);
2256 >        f.complete(v1);
2257 >
2258 >        assertFalse(r.ran());
2259 >        checkCompletedNormally(f, v1);
2260 >        checkCompletedWithWrappedCFException(g, ex);
2261 >        checkCompletedWithWrappedCFException(h, ex);
2262 >        }
2263 >    }
2264 >
2265 >    public void testAcceptEither_exceptionalCompletion3() {
2266 >        for (ExecutionMode m : ExecutionMode.values())
2267 >        for (Integer v1 : new Integer[] { 1, null }) {
2268 >
2269 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2270 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2271 >        final IncAction r = new IncAction();
2272 >        final CFException ex = new CFException();
2273 >
2274 >        g.completeExceptionally(ex);
2275 >        f.complete(v1);
2276 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2277  
2278 <        r = new IncAction();
2279 <        f = new CompletableFuture<>();
2280 <        f2 = new CompletableFuture<>();
2281 <        f2.completeExceptionally(new CFException());
2282 <        g = f.acceptEither(f2, r);
2283 <        checkCompletedWithWrappedCFException(g);
2278 >        // unspecified behavior
2279 >        Integer v;
2280 >        try {
2281 >            assertEquals(h.join(), null);
2282 >            assertTrue(r.ran());
2283 >            assertEquals(inc(v1), r.value);
2284 >        } catch (CompletionException ok) {
2285 >            checkCompletedWithWrappedCFException(h, ex);
2286 >            assertFalse(r.ran());
2287 >        }
2288 >
2289 >        checkCompletedWithWrappedCFException(g, ex);
2290 >        checkCompletedNormally(f, v1);
2291 >        }
2292 >    }
2293 >
2294 >    public void testAcceptEither_exceptionalCompletion4() {
2295 >        for (ExecutionMode m : ExecutionMode.values())
2296 >        for (Integer v1 : new Integer[] { 1, null }) {
2297 >
2298 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2299 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2300 >        final IncAction r = new IncAction();
2301 >        final CFException ex = new CFException();
2302 >
2303 >        f.completeExceptionally(ex);
2304 >        g.complete(v1);
2305 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2306 >
2307 >        // unspecified behavior
2308 >        Integer v;
2309 >        try {
2310 >            assertEquals(h.join(), null);
2311 >            assertTrue(r.ran());
2312 >            assertEquals(inc(v1), r.value);
2313 >        } catch (CompletionException ok) {
2314 >            checkCompletedWithWrappedCFException(h, ex);
2315 >            assertFalse(r.ran());
2316 >        }
2317 >
2318 >        checkCompletedWithWrappedCFException(f, ex);
2319 >        assertFalse(r.ran());
2320 >        checkCompletedNormally(g, v1);
2321 >        }
2322      }
2323  
2324      /**
2325       * acceptEither result completes exceptionally if action does
2326       */
2327 <    public void testAcceptEither3() {
2328 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2329 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2330 <        FailingConsumer r = new FailingConsumer();
2331 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2332 <        f2.complete(two);
2333 <        checkCompletedWithWrappedCFException(g);
2327 >    public void testAcceptEither_actionFailed1() {
2328 >        for (ExecutionMode m : ExecutionMode.values())
2329 >        for (Integer v1 : new Integer[] { 1, null })
2330 >        for (Integer v2 : new Integer[] { 2, null }) {
2331 >
2332 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2333 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2334 >        final FailingConsumer r = new FailingConsumer();
2335 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2336 >
2337 >        f.complete(v1);
2338 >        checkCompletedWithWrappedCFException(h);
2339 >        g.complete(v2);
2340 >        checkCompletedNormally(f, v1);
2341 >        checkCompletedNormally(g, v2);
2342 >        }
2343 >    }
2344 >
2345 >    public void testAcceptEither_actionFailed2() {
2346 >        for (ExecutionMode m : ExecutionMode.values())
2347 >        for (Integer v1 : new Integer[] { 1, null })
2348 >        for (Integer v2 : new Integer[] { 2, null }) {
2349 >
2350 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2351 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2352 >        final FailingConsumer r = new FailingConsumer();
2353 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2354 >
2355 >        g.complete(v2);
2356 >        checkCompletedWithWrappedCFException(h);
2357 >        f.complete(v1);
2358 >        checkCompletedNormally(f, v1);
2359 >        checkCompletedNormally(g, v2);
2360 >        }
2361      }
2362  
2363      /**
2364       * acceptEither result completes exceptionally if either source cancelled
2365       */
2366 <    public void testAcceptEither4() {
2367 <        CompletableFuture<Integer> f = new CompletableFuture<>();
2368 <        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2369 <        IncAction r = new IncAction();
2370 <        CompletableFuture<Void> g = f.acceptEither(f2, r);
2371 <        assertTrue(f.cancel(true));
2372 <        checkCompletedWithWrappedCancellationException(g);
2373 <        f = new CompletableFuture<>();
2374 <        f2 = new CompletableFuture<>();
2375 <        assertTrue(f2.cancel(true));
2376 <        checkCompletedWithWrappedCancellationException(g);
2366 >    public void testAcceptEither_sourceCancelled1() {
2367 >        for (ExecutionMode m : ExecutionMode.values())
2368 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2369 >        for (Integer v1 : new Integer[] { 1, null }) {
2370 >
2371 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2372 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2373 >        final IncAction r = new IncAction();
2374 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2375 >
2376 >        assertTrue(f.cancel(mayInterruptIfRunning));
2377 >        checkCompletedWithWrappedCancellationException(h);
2378 >        g.complete(v1);
2379 >
2380 >        checkCancelled(f);
2381 >        assertFalse(r.ran());
2382 >        checkCompletedNormally(g, v1);
2383 >        checkCompletedWithWrappedCancellationException(h);
2384 >        }
2385 >    }
2386 >
2387 >    public void testAcceptEither_sourceCancelled2() {
2388 >        for (ExecutionMode m : ExecutionMode.values())
2389 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2390 >        for (Integer v1 : new Integer[] { 1, null }) {
2391 >
2392 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2393 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2394 >        final IncAction r = new IncAction();
2395 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2396 >
2397 >        assertTrue(g.cancel(mayInterruptIfRunning));
2398 >        checkCompletedWithWrappedCancellationException(h);
2399 >        f.complete(v1);
2400 >
2401 >        checkCancelled(g);
2402 >        assertFalse(r.ran());
2403 >        checkCompletedNormally(f, v1);
2404 >        checkCompletedWithWrappedCancellationException(h);
2405 >        }
2406 >    }
2407 >
2408 >    public void testAcceptEither_sourceCancelled3() {
2409 >        for (ExecutionMode m : ExecutionMode.values())
2410 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2411 >        for (Integer v1 : new Integer[] { 1, null }) {
2412 >
2413 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2414 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2415 >        final IncAction r = new IncAction();
2416 >
2417 >        assertTrue(g.cancel(mayInterruptIfRunning));
2418 >        f.complete(v1);
2419 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2420 >
2421 >        // unspecified behavior
2422 >        Integer v;
2423 >        try {
2424 >            assertEquals(h.join(), null);
2425 >            assertTrue(r.ran());
2426 >            assertEquals(inc(v1), r.value);
2427 >        } catch (CompletionException ok) {
2428 >            checkCompletedWithWrappedCancellationException(h);
2429 >            assertFalse(r.ran());
2430 >        }
2431 >
2432 >        checkCancelled(g);
2433 >        checkCompletedNormally(f, v1);
2434 >        }
2435 >    }
2436 >
2437 >    public void testAcceptEither_sourceCancelled4() {
2438 >        for (ExecutionMode m : ExecutionMode.values())
2439 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
2440 >        for (Integer v1 : new Integer[] { 1, null }) {
2441 >
2442 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
2443 >        final CompletableFuture<Integer> g = new CompletableFuture<>();
2444 >        final IncAction r = new IncAction();
2445 >
2446 >        assertTrue(f.cancel(mayInterruptIfRunning));
2447 >        g.complete(v1);
2448 >        final CompletableFuture<Void> h = m.acceptEither(f, g, r);
2449 >
2450 >        // unspecified behavior
2451 >        Integer v;
2452 >        try {
2453 >            assertEquals(h.join(), null);
2454 >            assertTrue(r.ran());
2455 >            assertEquals(inc(v1), r.value);
2456 >        } catch (CompletionException ok) {
2457 >            checkCompletedWithWrappedCancellationException(h);
2458 >            assertFalse(r.ran());
2459 >        }
2460 >
2461 >        checkCancelled(f);
2462 >        checkCompletedNormally(g, v1);
2463 >        }
2464      }
2465  
2466      /**
# Line 2275 | Line 2759 | public class CompletableFutureTest exten
2759      }
2760  
2761      /**
2278     * applyToEitherAsync result completes normally after normal
2279     * completion of sources
2280     */
2281    public void testApplyToEitherAsync() {
2282        CompletableFuture<Integer> f = new CompletableFuture<>();
2283        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2284        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2285        f.complete(one);
2286        checkCompletedNormally(g, two);
2287
2288        f = new CompletableFuture<>();
2289        f.complete(one);
2290        f2 = new CompletableFuture<>();
2291        g = f.applyToEitherAsync(f2, inc);
2292        checkCompletedNormally(g, two);
2293    }
2294
2295    /**
2296     * applyToEitherAsync result completes exceptionally after exceptional
2297     * completion of source
2298     */
2299    public void testApplyToEitherAsync2() {
2300        CompletableFuture<Integer> f = new CompletableFuture<>();
2301        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2302        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2303        f.completeExceptionally(new CFException());
2304        checkCompletedWithWrappedCFException(g);
2305
2306        f = new CompletableFuture<>();
2307        f2 = new CompletableFuture<>();
2308        f2.completeExceptionally(new CFException());
2309        g = f.applyToEitherAsync(f2, inc);
2310        f.complete(one);
2311        checkCompletedWithWrappedCFException(g);
2312    }
2313
2314    /**
2315     * applyToEitherAsync result completes exceptionally if action does
2316     */
2317    public void testApplyToEitherAsync3() {
2318        CompletableFuture<Integer> f = new CompletableFuture<>();
2319        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2320        FailingFunction r = new FailingFunction();
2321        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r);
2322        f.complete(one);
2323        checkCompletedWithWrappedCFException(g);
2324    }
2325
2326    /**
2327     * applyToEitherAsync result completes exceptionally if either source cancelled
2328     */
2329    public void testApplyToEitherAsync4() {
2330        CompletableFuture<Integer> f = new CompletableFuture<>();
2331        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2332        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc);
2333        assertTrue(f.cancel(true));
2334        checkCompletedWithWrappedCancellationException(g);
2335
2336        f = new CompletableFuture<>();
2337        f2 = new CompletableFuture<>();
2338        assertTrue(f2.cancel(true));
2339        g = f.applyToEitherAsync(f2, inc);
2340        checkCompletedWithWrappedCancellationException(g);
2341    }
2342
2343    /**
2344     * acceptEitherAsync result completes normally after normal
2345     * completion of sources
2346     */
2347    public void testAcceptEitherAsync() {
2348        CompletableFuture<Integer> f = new CompletableFuture<>();
2349        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2350        IncAction r = new IncAction();
2351        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2352        f.complete(one);
2353        checkCompletedNormally(g, null);
2354        assertEquals(r.value, (Integer) 2);
2355
2356        r = new IncAction();
2357        f = new CompletableFuture<>();
2358        f.complete(one);
2359        f2 = new CompletableFuture<>();
2360        g = f.acceptEitherAsync(f2, r);
2361        checkCompletedNormally(g, null);
2362        assertEquals(r.value, (Integer) 2);
2363    }
2364
2365    /**
2366     * acceptEitherAsync result completes exceptionally after exceptional
2367     * completion of source
2368     */
2369    public void testAcceptEitherAsync2() {
2370        CompletableFuture<Integer> f = new CompletableFuture<>();
2371        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2372        IncAction r = new IncAction();
2373        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2374        f.completeExceptionally(new CFException());
2375        checkCompletedWithWrappedCFException(g);
2376
2377        r = new IncAction();
2378        f = new CompletableFuture<>();
2379        f2 = new CompletableFuture<>();
2380        f2.completeExceptionally(new CFException());
2381        g = f.acceptEitherAsync(f2, r);
2382        f.complete(one);
2383        checkCompletedWithWrappedCFException(g);
2384    }
2385
2386    /**
2387     * acceptEitherAsync result completes exceptionally if action does
2388     */
2389    public void testAcceptEitherAsync3() {
2390        CompletableFuture<Integer> f = new CompletableFuture<>();
2391        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2392        FailingConsumer r = new FailingConsumer();
2393        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2394        f.complete(one);
2395        checkCompletedWithWrappedCFException(g);
2396    }
2397
2398    /**
2399     * acceptEitherAsync result completes exceptionally if either
2400     * source cancelled
2401     */
2402    public void testAcceptEitherAsync4() {
2403        CompletableFuture<Integer> f = new CompletableFuture<>();
2404        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2405        IncAction r = new IncAction();
2406        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2407        assertTrue(f.cancel(true));
2408        checkCompletedWithWrappedCancellationException(g);
2409
2410        r = new IncAction();
2411        f = new CompletableFuture<>();
2412        f2 = new CompletableFuture<>();
2413        assertTrue(f2.cancel(true));
2414        g = f.acceptEitherAsync(f2, r);
2415        checkCompletedWithWrappedCancellationException(g);
2416    }
2417
2418    /**
2762       * runAfterEitherAsync result completes normally after normal
2763       * completion of sources
2764       */
# Line 2713 | Line 3056 | public class CompletableFutureTest exten
3056          checkCompletedWithWrappedCancellationException(g);
3057      }
3058  
2716    /**
2717     * applyToEitherAsync result completes normally after normal
2718     * completion of sources
2719     */
2720    public void testApplyToEitherAsyncE() {
2721        CompletableFuture<Integer> f = new CompletableFuture<>();
2722        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2723        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2724        f.complete(one);
2725        checkCompletedNormally(g, two);
2726
2727        f = new CompletableFuture<>();
2728        f.complete(one);
2729        f2 = new CompletableFuture<>();
2730        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2731        checkCompletedNormally(g, two);
2732    }
2733
2734    /**
2735     * applyToEitherAsync result completes exceptionally after exceptional
2736     * completion of source
2737     */
2738    public void testApplyToEitherAsync2E() {
2739        CompletableFuture<Integer> f = new CompletableFuture<>();
2740        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2741        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2742        f.completeExceptionally(new CFException());
2743        checkCompletedWithWrappedCFException(g);
2744
2745        f = new CompletableFuture<>();
2746        f2 = new CompletableFuture<>();
2747        f2.completeExceptionally(new CFException());
2748        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2749        f.complete(one);
2750        checkCompletedWithWrappedCFException(g);
2751    }
2752
2753    /**
2754     * applyToEitherAsync result completes exceptionally if action does
2755     */
2756    public void testApplyToEitherAsync3E() {
2757        CompletableFuture<Integer> f = new CompletableFuture<>();
2758        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2759        FailingFunction r = new FailingFunction();
2760        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor());
2761        f.complete(one);
2762        checkCompletedWithWrappedCFException(g);
2763    }
2764
2765    /**
2766     * applyToEitherAsync result completes exceptionally if either source cancelled
2767     */
2768    public void testApplyToEitherAsync4E() {
2769        CompletableFuture<Integer> f = new CompletableFuture<>();
2770        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2771        CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2772        assertTrue(f.cancel(true));
2773        checkCompletedWithWrappedCancellationException(g);
2774
2775        f = new CompletableFuture<>();
2776        f2 = new CompletableFuture<>();
2777        assertTrue(f2.cancel(true));
2778        g = f.applyToEitherAsync(f2, inc, new ThreadExecutor());
2779        checkCompletedWithWrappedCancellationException(g);
2780    }
2781
2782    /**
2783     * acceptEitherAsync result completes normally after normal
2784     * completion of sources
2785     */
2786    public void testAcceptEitherAsyncE() {
2787        CompletableFuture<Integer> f = new CompletableFuture<>();
2788        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2789        IncAction r = new IncAction();
2790        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2791        f.complete(one);
2792        checkCompletedNormally(g, null);
2793        assertEquals(r.value, (Integer) 2);
2794
2795        r = new IncAction();
2796        f = new CompletableFuture<>();
2797        f.complete(one);
2798        f2 = new CompletableFuture<>();
2799        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2800        checkCompletedNormally(g, null);
2801        assertEquals(r.value, (Integer) 2);
2802    }
2803
2804    /**
2805     * acceptEitherAsync result completes exceptionally after exceptional
2806     * completion of source
2807     */
2808    public void testAcceptEitherAsync2E() {
2809        CompletableFuture<Integer> f = new CompletableFuture<>();
2810        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2811        IncAction r = new IncAction();
2812        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2813        f.completeExceptionally(new CFException());
2814        checkCompletedWithWrappedCFException(g);
2815
2816        r = new IncAction();
2817        f = new CompletableFuture<>();
2818        f2 = new CompletableFuture<>();
2819        f2.completeExceptionally(new CFException());
2820        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2821        f.complete(one);
2822        checkCompletedWithWrappedCFException(g);
2823    }
2824
2825    /**
2826     * acceptEitherAsync result completes exceptionally if action does
2827     */
2828    public void testAcceptEitherAsync3E() {
2829        CompletableFuture<Integer> f = new CompletableFuture<>();
2830        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2831        FailingConsumer r = new FailingConsumer();
2832        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2833        f.complete(one);
2834        checkCompletedWithWrappedCFException(g);
2835    }
2836
2837    /**
2838     * acceptEitherAsync result completes exceptionally if either
2839     * source cancelled
2840     */
2841    public void testAcceptEitherAsync4E() {
2842        CompletableFuture<Integer> f = new CompletableFuture<>();
2843        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2844        IncAction r = new IncAction();
2845        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2846        assertTrue(f.cancel(true));
2847        checkCompletedWithWrappedCancellationException(g);
2848
2849        r = new IncAction();
2850        f = new CompletableFuture<>();
2851        f2 = new CompletableFuture<>();
2852        assertTrue(f2.cancel(true));
2853        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2854        checkCompletedWithWrappedCancellationException(g);
2855    }
2856
3059      /**
3060       * runAfterEitherAsync result completes normally after normal
3061       * completion of sources

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines