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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.39 by jsr166, Mon Jun 2 00:46:52 2014 UTC vs.
Revision 1.40 by jsr166, Mon Jun 2 01:11:53 2014 UTC

# Line 2152 | Line 2152 | public class CompletableFutureTest exten
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 2517 | Line 2759 | public class CompletableFutureTest exten
2759      }
2760  
2761      /**
2520     * acceptEitherAsync result completes normally after normal
2521     * completion of sources
2522     */
2523    public void testAcceptEitherAsync() {
2524        CompletableFuture<Integer> f = new CompletableFuture<>();
2525        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2526        IncAction r = new IncAction();
2527        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2528        f.complete(one);
2529        checkCompletedNormally(g, null);
2530        assertEquals(r.value, (Integer) 2);
2531
2532        r = new IncAction();
2533        f = new CompletableFuture<>();
2534        f.complete(one);
2535        f2 = new CompletableFuture<>();
2536        g = f.acceptEitherAsync(f2, r);
2537        checkCompletedNormally(g, null);
2538        assertEquals(r.value, (Integer) 2);
2539    }
2540
2541    /**
2542     * acceptEitherAsync result completes exceptionally after exceptional
2543     * completion of source
2544     */
2545    public void testAcceptEitherAsync2() {
2546        CompletableFuture<Integer> f = new CompletableFuture<>();
2547        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2548        IncAction r = new IncAction();
2549        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2550        f.completeExceptionally(new CFException());
2551        checkCompletedWithWrappedCFException(g);
2552
2553        r = new IncAction();
2554        f = new CompletableFuture<>();
2555        f2 = new CompletableFuture<>();
2556        f2.completeExceptionally(new CFException());
2557        g = f.acceptEitherAsync(f2, r);
2558        f.complete(one);
2559        checkCompletedWithWrappedCFException(g);
2560    }
2561
2562    /**
2563     * acceptEitherAsync result completes exceptionally if action does
2564     */
2565    public void testAcceptEitherAsync3() {
2566        CompletableFuture<Integer> f = new CompletableFuture<>();
2567        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2568        FailingConsumer r = new FailingConsumer();
2569        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2570        f.complete(one);
2571        checkCompletedWithWrappedCFException(g);
2572    }
2573
2574    /**
2575     * acceptEitherAsync result completes exceptionally if either
2576     * source cancelled
2577     */
2578    public void testAcceptEitherAsync4() {
2579        CompletableFuture<Integer> f = new CompletableFuture<>();
2580        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2581        IncAction r = new IncAction();
2582        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r);
2583        assertTrue(f.cancel(true));
2584        checkCompletedWithWrappedCancellationException(g);
2585
2586        r = new IncAction();
2587        f = new CompletableFuture<>();
2588        f2 = new CompletableFuture<>();
2589        assertTrue(f2.cancel(true));
2590        g = f.acceptEitherAsync(f2, r);
2591        checkCompletedWithWrappedCancellationException(g);
2592    }
2593
2594    /**
2762       * runAfterEitherAsync result completes normally after normal
2763       * completion of sources
2764       */
# Line 2889 | Line 3056 | public class CompletableFutureTest exten
3056          checkCompletedWithWrappedCancellationException(g);
3057      }
3058  
2892    /**
2893     * acceptEitherAsync result completes normally after normal
2894     * completion of sources
2895     */
2896    public void testAcceptEitherAsyncE() {
2897        CompletableFuture<Integer> f = new CompletableFuture<>();
2898        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2899        IncAction r = new IncAction();
2900        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2901        f.complete(one);
2902        checkCompletedNormally(g, null);
2903        assertEquals(r.value, (Integer) 2);
2904
2905        r = new IncAction();
2906        f = new CompletableFuture<>();
2907        f.complete(one);
2908        f2 = new CompletableFuture<>();
2909        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2910        checkCompletedNormally(g, null);
2911        assertEquals(r.value, (Integer) 2);
2912    }
2913
2914    /**
2915     * acceptEitherAsync result completes exceptionally after exceptional
2916     * completion of source
2917     */
2918    public void testAcceptEitherAsync2E() {
2919        CompletableFuture<Integer> f = new CompletableFuture<>();
2920        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2921        IncAction r = new IncAction();
2922        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2923        f.completeExceptionally(new CFException());
2924        checkCompletedWithWrappedCFException(g);
2925
2926        r = new IncAction();
2927        f = new CompletableFuture<>();
2928        f2 = new CompletableFuture<>();
2929        f2.completeExceptionally(new CFException());
2930        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2931        f.complete(one);
2932        checkCompletedWithWrappedCFException(g);
2933    }
2934
2935    /**
2936     * acceptEitherAsync result completes exceptionally if action does
2937     */
2938    public void testAcceptEitherAsync3E() {
2939        CompletableFuture<Integer> f = new CompletableFuture<>();
2940        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2941        FailingConsumer r = new FailingConsumer();
2942        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2943        f.complete(one);
2944        checkCompletedWithWrappedCFException(g);
2945    }
2946
2947    /**
2948     * acceptEitherAsync result completes exceptionally if either
2949     * source cancelled
2950     */
2951    public void testAcceptEitherAsync4E() {
2952        CompletableFuture<Integer> f = new CompletableFuture<>();
2953        CompletableFuture<Integer> f2 = new CompletableFuture<>();
2954        IncAction r = new IncAction();
2955        CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2956        assertTrue(f.cancel(true));
2957        checkCompletedWithWrappedCancellationException(g);
2958
2959        r = new IncAction();
2960        f = new CompletableFuture<>();
2961        f2 = new CompletableFuture<>();
2962        assertTrue(f2.cancel(true));
2963        g = f.acceptEitherAsync(f2, r, new ThreadExecutor());
2964        checkCompletedWithWrappedCancellationException(g);
2965    }
2966
3059      /**
3060       * runAfterEitherAsync result completes normally after normal
3061       * completion of sources

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines