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.8 by jsr166, Fri Mar 22 22:27:04 2013 UTC vs.
Revision 1.14 by jsr166, Mon Apr 1 20:06:25 2013 UTC

# Line 53 | Line 53 | public class CompletableFutureTest exten
53          catch (Throwable fail) { threadUnexpectedException(fail); }
54      }
55  
56 <    void checkCompletedNormally(CompletableFuture<?> f, Object value) {
56 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
57          try {
58              assertEquals(value, f.join());
59          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 98 | Line 98 | public class CompletableFutureTest exten
98          } catch (Throwable fail) { threadUnexpectedException(fail); }
99          assertTrue(f.isDone());
100          assertFalse(f.isCancelled());
101 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
102      }
103  
104      void checkCancelled(CompletableFuture<?> f) {
# Line 121 | Line 122 | public class CompletableFutureTest exten
122          } catch (Throwable fail) { threadUnexpectedException(fail); }
123          assertTrue(f.isDone());
124          assertTrue(f.isCancelled());
125 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
126      }
127  
128      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
# Line 150 | Line 152 | public class CompletableFutureTest exten
152          } catch (Throwable fail) { threadUnexpectedException(fail); }
153          assertTrue(f.isDone());
154          assertFalse(f.isCancelled());
155 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
156      }
157  
158      /**
# Line 271 | Line 274 | public class CompletableFutureTest exten
274          assertTrue(f.toString().contains("[Completed exceptionally]"));
275      }
276  
277 +    /**
278 +     * completedFuture returns a completed CompletableFuture with given value
279 +     */
280 +    public void testCompletedFuture() {
281 +        CompletableFuture<String> f = CompletableFuture.completedFuture("test");
282 +        checkCompletedNormally(f, "test");
283 +    }
284 +
285      static final Supplier<Integer> supplyOne =
286          () -> Integer.valueOf(1);
287      static final Function<Integer, Integer> inc =
# Line 394 | Line 405 | public class CompletableFutureTest exten
405          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
406          assertNull(f.join());
407          assertTrue(r.ran);
408 +        checkCompletedNormally(f, null);
409      }
410  
411      /**
# Line 404 | Line 416 | public class CompletableFutureTest exten
416          CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
417          assertNull(f.join());
418          assertTrue(r.ran);
419 +        checkCompletedNormally(f, null);
420      }
421  
422      /**
# Line 2344 | Line 2357 | public class CompletableFutureTest exten
2357              CompletableFuture[] fs = new CompletableFuture[k];
2358              for (int i = 0; i < k; ++i)
2359                  fs[i] = new CompletableFuture<Integer>();
2360 <            CompletableFuture<?> f = CompletableFuture.allOf(fs);
2360 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2361              for (int i = 0; i < k; ++i) {
2362                  checkIncomplete(f);
2363                  fs[i].complete(one);
2364              }
2365 <            assertTrue(f.isDone());
2353 <            assertFalse(f.isCancelled());
2365 >            checkCompletedNormally(f, null);
2366          }
2367      }
2368  
# Line 2363 | Line 2375 | public class CompletableFutureTest exten
2375      }
2376  
2377      /**
2378 <     * allOf returns a future completed when any components complete
2378 >     * anyOf returns a future completed when any components complete
2379       */
2380      public void testAnyOf() throws Exception {
2381          for (int k = 1; k < 20; ++k) {
2382              CompletableFuture[] fs = new CompletableFuture[k];
2383              for (int i = 0; i < k; ++i)
2384                  fs[i] = new CompletableFuture<Integer>();
2385 <            CompletableFuture<?> f = CompletableFuture.anyOf(fs);
2385 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2386              checkIncomplete(f);
2387              for (int i = 0; i < k; ++i) {
2388                  fs[i].complete(one);
2389 <                assertTrue(f.isDone());
2389 >                checkCompletedNormally(f, one);
2390              }
2391          }
2392      }
# Line 2385 | Line 2397 | public class CompletableFutureTest exten
2397      public void testNPE() {
2398          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2399          CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2400 <        CompletableFuture h;
2401 <        try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2402 <        try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2403 <        try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2404 <        try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2405 <        try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2406 <        try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2407 <        try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2408 <        try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2409 <        try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2410 <        try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2411 <        try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2412 <        try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2413 <        try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2414 <        try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2415 <        try { h = f.handle(null); } catch (NullPointerException ok) {}
2416 <        try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2417 <
2418 <        try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2419 <        try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2420 <        try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2421 <        try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2422 <        try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2423 <        try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2424 <        try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2425 <        try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2426 <        try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2427 <        try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2428 <        try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2429 <        try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2430 <        try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2400 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2401 >        CompletableFuture<?> h;
2402 >        Executor exec = new ThreadExecutor();
2403 >
2404 >        Runnable[] throwingActions = {
2405 >            () -> { CompletableFuture.supplyAsync(null); },
2406 >            () -> { CompletableFuture.supplyAsync(null, exec); },
2407 >            () -> { CompletableFuture.supplyAsync(() -> one, null); },
2408 >
2409 >            () -> { CompletableFuture.runAsync(null); },
2410 >            () -> { CompletableFuture.runAsync(null, exec); },
2411 >            () -> { CompletableFuture.runAsync(() -> {}, null); },
2412 >
2413 >            () -> { f.completeExceptionally(null); },
2414 >
2415 >            () -> { f.thenApply(null); },
2416 >            () -> { f.thenApplyAsync(null); },
2417 >            () -> { f.thenApplyAsync((x) -> x, null); },
2418 >            () -> { f.thenApplyAsync(null, exec); },
2419 >
2420 >            () -> { f.thenAccept(null); },
2421 >            () -> { f.thenAcceptAsync(null); },
2422 >            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2423 >            () -> { f.thenAcceptAsync(null, exec); },
2424 >
2425 >            () -> { f.thenRun(null); },
2426 >            () -> { f.thenRunAsync(null); },
2427 >            () -> { f.thenRunAsync(() -> { ; }, null); },
2428 >            () -> { f.thenRunAsync(null, exec); },
2429 >
2430 >            () -> { f.thenCombine(g, null); },
2431 >            () -> { f.thenCombineAsync(g, null); },
2432 >            () -> { f.thenCombineAsync(g, null, exec); },
2433 >            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2434 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2435 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2436 >            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2437 >
2438 >            () -> { f.thenAcceptBoth(g, null); },
2439 >            () -> { f.thenAcceptBothAsync(g, null); },
2440 >            () -> { f.thenAcceptBothAsync(g, null, exec); },
2441 >            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2442 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2443 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2444 >            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2445 >
2446 >            () -> { f.runAfterBoth(g, null); },
2447 >            () -> { f.runAfterBothAsync(g, null); },
2448 >            () -> { f.runAfterBothAsync(g, null, exec); },
2449 >            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2450 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2451 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2452 >            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2453 >
2454 >            () -> { f.applyToEither(g, null); },
2455 >            () -> { f.applyToEitherAsync(g, null); },
2456 >            () -> { f.applyToEitherAsync(g, null, exec); },
2457 >            () -> { f.applyToEither(nullFuture, (x) -> x); },
2458 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2459 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2460 >            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2461 >
2462 >            () -> { f.acceptEither(g, null); },
2463 >            () -> { f.acceptEitherAsync(g, null); },
2464 >            () -> { f.acceptEitherAsync(g, null, exec); },
2465 >            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2466 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2467 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2468 >            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2469 >
2470 >            () -> { f.runAfterEither(g, null); },
2471 >            () -> { f.runAfterEitherAsync(g, null); },
2472 >            () -> { f.runAfterEitherAsync(g, null, exec); },
2473 >            () -> { f.runAfterEither(nullFuture, () -> {}); },
2474 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2475 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2476 >            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2477 >
2478 >            () -> { f.thenCompose(null); },
2479 >            () -> { f.thenComposeAsync(null); },
2480 >            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2481 >            () -> { f.thenComposeAsync(null, exec); },
2482 >
2483 >            () -> { f.exceptionally(null); },
2484 >
2485 >            () -> { f.handle(null); },
2486 >
2487 >            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2488 >            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2489 >            () -> { CompletableFuture.allOf(f, null); },
2490 >            () -> { CompletableFuture.allOf(null, f); },
2491 >
2492 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2493 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2494 >            () -> { CompletableFuture.anyOf(f, null); },
2495 >            () -> { CompletableFuture.anyOf(null, f); },
2496 >
2497 >            // TODO: Crashes javac with lambda-8-2013-03-31...
2498 >            //() -> { CompletableFuture<?> x = f.thenAccept(null); },
2499 >            //() -> { CompletableFuture<Void> x = f.thenRun(null); },
2500 >            //() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); },
2501 >        };
2502  
2503 +        assertThrows(NullPointerException.class, throwingActions);
2504      }
2505  
2422
2506   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines