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.7 by jsr166, Fri Mar 22 16:29:42 2013 UTC vs.
Revision 1.17 by jsr166, Thu Apr 4 04:16:02 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 75 | Line 75 | public class CompletableFutureTest exten
75          try {
76              f.join();
77              shouldThrow();
78 <        } catch (Throwable ex) {
79 <            assertTrue(ex instanceof CompletionException &&
80 <                       ((CompletionException)ex).getCause() instanceof CFException);
78 >        } catch (CompletionException success) {
79 >            assertTrue(success.getCause() instanceof CFException);
80          }
81          try {
82              f.getNow(null);
83              shouldThrow();
84 <        } catch (Throwable ex) {
85 <            assertTrue(ex instanceof CompletionException &&
87 <                       ((CompletionException)ex).getCause() instanceof CFException);
84 >        } catch (CompletionException success) {
85 >            assertTrue(success.getCause() instanceof CFException);
86          }
87          try {
88              f.get();
89              shouldThrow();
90 <        } catch (Throwable ex) {
91 <            assertTrue(ex instanceof ExecutionException &&
92 <                       ((ExecutionException)ex).getCause() instanceof CFException);
95 <        }
90 >        } catch (ExecutionException success) {
91 >            assertTrue(success.getCause() instanceof CFException);
92 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
93          try {
94              f.get(0L, SECONDS);
95              shouldThrow();
96 <        } catch (Throwable ex) {
97 <            assertTrue(ex instanceof ExecutionException &&
98 <                       ((ExecutionException)ex).getCause() instanceof CFException);
102 <        }
96 >        } catch (ExecutionException success) {
97 >            assertTrue(success.getCause() instanceof CFException);
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) {
105          try {
106              f.join();
107              shouldThrow();
108 <        } catch (Throwable ex) {
112 <            assertTrue(ex instanceof CancellationException);
113 <        }
108 >        } catch (CancellationException success) {}
109          try {
110              f.getNow(null);
111              shouldThrow();
112 <        } catch (Throwable ex) {
118 <            assertTrue(ex instanceof CancellationException);
119 <        }
112 >        } catch (CancellationException success) {}
113          try {
114              f.get();
115              shouldThrow();
116 <        } catch (Throwable ex) {
117 <            assertTrue(ex instanceof CancellationException);
125 <        }
116 >        } catch (CancellationException success) {
117 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
118          try {
119              f.get(0L, SECONDS);
120              shouldThrow();
121 <        } catch (Throwable ex) {
122 <            assertTrue(ex instanceof CancellationException);
131 <        }
121 >        } catch (CancellationException success) {
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) {
129          try {
130              f.join();
131              shouldThrow();
132 <        } catch (Throwable ex) {
133 <            assertTrue(ex instanceof CompletionException &&
142 <                       ((CompletionException)ex).getCause() instanceof CancellationException);
132 >        } catch (CompletionException success) {
133 >            assertTrue(success.getCause() instanceof CancellationException);
134          }
135          try {
136              f.getNow(null);
137              shouldThrow();
138 <        } catch (Throwable ex) {
139 <            assertTrue(ex instanceof CompletionException &&
149 <                       ((CompletionException)ex).getCause() instanceof CancellationException);
138 >        } catch (CompletionException success) {
139 >            assertTrue(success.getCause() instanceof CancellationException);
140          }
141          try {
142              f.get();
143              shouldThrow();
144 <        } catch (Throwable ex) {
145 <            assertTrue(ex instanceof ExecutionException &&
146 <                       ((ExecutionException)ex).getCause() instanceof CancellationException);
157 <        }
144 >        } catch (ExecutionException success) {
145 >            assertTrue(success.getCause() instanceof CancellationException);
146 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
147          try {
148              f.get(0L, SECONDS);
149              shouldThrow();
150 <        } catch (Throwable ex) {
151 <            assertTrue(ex instanceof ExecutionException &&
152 <                       ((ExecutionException)ex).getCause() instanceof CancellationException);
164 <        }
150 >        } catch (ExecutionException success) {
151 >            assertTrue(success.getCause() instanceof CancellationException);
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 285 | 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 348 | Line 345 | public class CompletableFutureTest exten
345  
346      // Used for explicit executor tests
347      static final class ThreadExecutor implements Executor {
348 +        AtomicInteger count = new AtomicInteger(0);
349 +
350          public void execute(Runnable r) {
351 +            count.getAndIncrement();
352              new Thread(r).start();
353          }
354      }
# Line 358 | Line 358 | public class CompletableFutureTest exten
358      }
359  
360      static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
361 +        boolean ran;
362          public Integer apply(Integer x, Throwable t) {
363 +            ran = true;
364              return (t == null) ? two : three;
365          }
366      }
# Line 387 | Line 389 | public class CompletableFutureTest exten
389       * normal or exceptional completion of source
390       */
391      public void testHandle() {
392 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
393 <        IntegerHandler r = new IntegerHandler();
394 <        CompletableFuture<Integer> g = f.handle(r);
392 >        CompletableFuture<Integer> f, g;
393 >        IntegerHandler r;
394 >
395 >        f = new CompletableFuture<Integer>();
396          f.completeExceptionally(new CFException());
397 +        g = f.handle(r = new IntegerHandler());
398 +        assertTrue(r.ran);
399          checkCompletedNormally(g, three);
400  
401          f = new CompletableFuture<Integer>();
402 <        r = new IntegerHandler();
403 <        g = f.handle(r);
402 >        g = f.handle(r = new IntegerHandler());
403 >        assertFalse(r.ran);
404 >        f.completeExceptionally(new CFException());
405 >        checkCompletedNormally(g, three);
406 >        assertTrue(r.ran);
407 >
408 >        f = new CompletableFuture<Integer>();
409          f.complete(one);
410 +        g = f.handle(r = new IntegerHandler());
411 +        assertTrue(r.ran);
412 +        checkCompletedNormally(g, two);
413 +
414 +        f = new CompletableFuture<Integer>();
415 +        g = f.handle(r = new IntegerHandler());
416 +        assertFalse(r.ran);
417 +        f.complete(one);
418 +        assertTrue(r.ran);
419          checkCompletedNormally(g, two);
420      }
421  
# Line 408 | Line 427 | public class CompletableFutureTest exten
427          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
428          assertNull(f.join());
429          assertTrue(r.ran);
430 +        checkCompletedNormally(f, null);
431      }
432  
433      /**
# Line 415 | Line 435 | public class CompletableFutureTest exten
435       */
436      public void testRunAsync2() {
437          Noop r = new Noop();
438 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
438 >        ThreadExecutor exec = new ThreadExecutor();
439 >        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
440          assertNull(f.join());
441          assertTrue(r.ran);
442 +        checkCompletedNormally(f, null);
443 +        assertEquals(1, exec.count.get());
444      }
445  
446      /**
# Line 434 | Line 457 | public class CompletableFutureTest exten
457       * supplyAsync completes with result of supplier
458       */
459      public void testSupplyAsync() {
460 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
460 >        CompletableFuture<Integer> f;
461 >        f = CompletableFuture.supplyAsync(supplyOne);
462          assertEquals(f.join(), one);
463 +        checkCompletedNormally(f, one);
464      }
465  
466      /**
467       * supplyAsync with executor completes with result of supplier
468       */
469      public void testSupplyAsync2() {
470 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
470 >        CompletableFuture<Integer> f;
471 >        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
472          assertEquals(f.join(), one);
473 +        checkCompletedNormally(f, one);
474      }
475  
476      /**
# Line 2358 | Line 2385 | public class CompletableFutureTest exten
2385              CompletableFuture[] fs = new CompletableFuture[k];
2386              for (int i = 0; i < k; ++i)
2387                  fs[i] = new CompletableFuture<Integer>();
2388 <            CompletableFuture<?> f = CompletableFuture.allOf(fs);
2388 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2389              for (int i = 0; i < k; ++i) {
2390                  checkIncomplete(f);
2391                  fs[i].complete(one);
2392              }
2393 <            assertTrue(f.isDone());
2367 <            assertFalse(f.isCancelled());
2393 >            checkCompletedNormally(f, null);
2394          }
2395      }
2396  
# Line 2377 | Line 2403 | public class CompletableFutureTest exten
2403      }
2404  
2405      /**
2406 <     * allOf returns a future completed when any components complete
2406 >     * anyOf returns a future completed when any components complete
2407       */
2408      public void testAnyOf() throws Exception {
2409          for (int k = 1; k < 20; ++k) {
2410              CompletableFuture[] fs = new CompletableFuture[k];
2411              for (int i = 0; i < k; ++i)
2412                  fs[i] = new CompletableFuture<Integer>();
2413 <            CompletableFuture<?> f = CompletableFuture.anyOf(fs);
2413 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2414              checkIncomplete(f);
2415              for (int i = 0; i < k; ++i) {
2416                  fs[i].complete(one);
2417 <                assertTrue(f.isDone());
2417 >                checkCompletedNormally(f, one);
2418              }
2419          }
2420      }
# Line 2399 | Line 2425 | public class CompletableFutureTest exten
2425      public void testNPE() {
2426          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2427          CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2428 <        CompletableFuture h;
2429 <        try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2430 <        try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2431 <        try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2432 <        try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2433 <        try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2434 <        try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2435 <        try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2436 <        try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2437 <        try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2438 <        try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2439 <        try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2440 <        try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2441 <        try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2442 <        try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2443 <        try { h = f.handle(null); } catch (NullPointerException ok) {}
2444 <        try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2445 <
2446 <        try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2447 <        try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2448 <        try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2449 <        try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2450 <        try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2451 <        try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2452 <        try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2453 <        try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2454 <        try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2455 <        try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2456 <        try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2457 <        try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2458 <        try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2428 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2429 >        CompletableFuture<?> h;
2430 >        ThreadExecutor exec = new ThreadExecutor();
2431 >
2432 >        Runnable[] throwingActions = {
2433 >            () -> { CompletableFuture.supplyAsync(null); },
2434 >            () -> { CompletableFuture.supplyAsync(null, exec); },
2435 >            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2436 >
2437 >            () -> { CompletableFuture.runAsync(null); },
2438 >            () -> { CompletableFuture.runAsync(null, exec); },
2439 >            () -> { CompletableFuture.runAsync(() -> {}, null); },
2440 >
2441 >            () -> { f.completeExceptionally(null); },
2442 >
2443 >            () -> { f.thenApply(null); },
2444 >            () -> { f.thenApplyAsync(null); },
2445 >            () -> { f.thenApplyAsync((x) -> x, null); },
2446 >            () -> { f.thenApplyAsync(null, exec); },
2447 >
2448 >            () -> { f.thenAccept(null); },
2449 >            () -> { f.thenAcceptAsync(null); },
2450 >            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2451 >            () -> { f.thenAcceptAsync(null, exec); },
2452 >
2453 >            () -> { f.thenRun(null); },
2454 >            () -> { f.thenRunAsync(null); },
2455 >            () -> { f.thenRunAsync(() -> { ; }, null); },
2456 >            () -> { f.thenRunAsync(null, exec); },
2457 >
2458 >            () -> { f.thenCombine(g, null); },
2459 >            () -> { f.thenCombineAsync(g, null); },
2460 >            () -> { f.thenCombineAsync(g, null, exec); },
2461 >            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2462 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2463 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2464 >            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2465 >
2466 >            () -> { f.thenAcceptBoth(g, null); },
2467 >            () -> { f.thenAcceptBothAsync(g, null); },
2468 >            () -> { f.thenAcceptBothAsync(g, null, exec); },
2469 >            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2470 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2471 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2472 >            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2473 >
2474 >            () -> { f.runAfterBoth(g, null); },
2475 >            () -> { f.runAfterBothAsync(g, null); },
2476 >            () -> { f.runAfterBothAsync(g, null, exec); },
2477 >            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2478 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2479 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2480 >            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2481 >
2482 >            () -> { f.applyToEither(g, null); },
2483 >            () -> { f.applyToEitherAsync(g, null); },
2484 >            () -> { f.applyToEitherAsync(g, null, exec); },
2485 >            () -> { f.applyToEither(nullFuture, (x) -> x); },
2486 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2487 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2488 >            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2489 >
2490 >            () -> { f.acceptEither(g, null); },
2491 >            () -> { f.acceptEitherAsync(g, null); },
2492 >            () -> { f.acceptEitherAsync(g, null, exec); },
2493 >            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2494 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2495 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2496 >            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2497 >
2498 >            () -> { f.runAfterEither(g, null); },
2499 >            () -> { f.runAfterEitherAsync(g, null); },
2500 >            () -> { f.runAfterEitherAsync(g, null, exec); },
2501 >            () -> { f.runAfterEither(nullFuture, () -> {}); },
2502 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2503 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2504 >            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2505 >
2506 >            () -> { f.thenCompose(null); },
2507 >            () -> { f.thenComposeAsync(null); },
2508 >            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2509 >            () -> { f.thenComposeAsync(null, exec); },
2510 >
2511 >            () -> { f.exceptionally(null); },
2512 >
2513 >            () -> { f.handle(null); },
2514 >
2515 >            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2516 >            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2517 >            () -> { CompletableFuture.allOf(f, null); },
2518 >            () -> { CompletableFuture.allOf(null, f); },
2519 >
2520 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2521 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2522 >            () -> { CompletableFuture.anyOf(f, null); },
2523 >            () -> { CompletableFuture.anyOf(null, f); },
2524 >        };
2525  
2526 +        assertThrows(NullPointerException.class, throwingActions);
2527 +        assertEquals(0, exec.count.get());
2528      }
2529  
2436
2530   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines