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.6 by jsr166, Fri Mar 22 16:10:19 2013 UTC vs.
Revision 1.18 by jsr166, Sun Apr 7 14:54:06 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 +    // Choose non-commutative actions for better coverage
286 +
287      static final Supplier<Integer> supplyOne =
288          () -> Integer.valueOf(1);
289      static final Function<Integer, Integer> inc =
290          (Integer x) -> Integer.valueOf(x.intValue() + 1);
291 <    static final BiFunction<Integer, Integer, Integer> add =
292 <        (Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue());
291 >    static final BiFunction<Integer, Integer, Integer> subtract =
292 >        (Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue());
293      static final class IncAction implements Consumer<Integer> {
294          int value;
295          public void accept(Integer x) { value = x.intValue() + 1; }
# Line 331 | Line 330 | public class CompletableFutureTest exten
330          public void run() { ran = true; throw new CFException(); }
331      }
332  
333 <    static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> {
333 >    static final class CompletableFutureInc
334 >        implements Function<Integer, CompletableFuture<Integer>> {
335          public CompletableFuture<Integer> apply(Integer x) {
336              CompletableFuture<Integer> f = new CompletableFuture<Integer>();
337              f.complete(Integer.valueOf(x.intValue() + 1));
# Line 339 | Line 339 | public class CompletableFutureTest exten
339          }
340      }
341  
342 <    static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
342 >    static final class FailingCompletableFutureFunction
343 >        implements Function<Integer, CompletableFuture<Integer>> {
344          boolean ran;
345          public CompletableFuture<Integer> apply(Integer x) {
346              ran = true; throw new CFException();
# Line 348 | Line 349 | public class CompletableFutureTest exten
349  
350      // Used for explicit executor tests
351      static final class ThreadExecutor implements Executor {
352 +        AtomicInteger count = new AtomicInteger(0);
353 +
354          public void execute(Runnable r) {
355 +            count.getAndIncrement();
356              new Thread(r).start();
357          }
358      }
# Line 358 | Line 362 | public class CompletableFutureTest exten
362      }
363  
364      static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
365 +        boolean ran;
366          public Integer apply(Integer x, Throwable t) {
367 +            ran = true;
368              return (t == null) ? two : three;
369          }
370      }
# Line 387 | Line 393 | public class CompletableFutureTest exten
393       * normal or exceptional completion of source
394       */
395      public void testHandle() {
396 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
397 <        IntegerHandler r = new IntegerHandler();
398 <        CompletableFuture<Integer> g = f.handle(r);
396 >        CompletableFuture<Integer> f, g;
397 >        IntegerHandler r;
398 >
399 >        f = new CompletableFuture<Integer>();
400          f.completeExceptionally(new CFException());
401 +        g = f.handle(r = new IntegerHandler());
402 +        assertTrue(r.ran);
403          checkCompletedNormally(g, three);
404  
405          f = new CompletableFuture<Integer>();
406 <        r = new IntegerHandler();
407 <        g = f.handle(r);
406 >        g = f.handle(r = new IntegerHandler());
407 >        assertFalse(r.ran);
408 >        f.completeExceptionally(new CFException());
409 >        checkCompletedNormally(g, three);
410 >        assertTrue(r.ran);
411 >
412 >        f = new CompletableFuture<Integer>();
413 >        f.complete(one);
414 >        g = f.handle(r = new IntegerHandler());
415 >        assertTrue(r.ran);
416 >        checkCompletedNormally(g, two);
417 >
418 >        f = new CompletableFuture<Integer>();
419 >        g = f.handle(r = new IntegerHandler());
420 >        assertFalse(r.ran);
421          f.complete(one);
422 +        assertTrue(r.ran);
423          checkCompletedNormally(g, two);
424      }
425  
# Line 408 | Line 431 | public class CompletableFutureTest exten
431          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
432          assertNull(f.join());
433          assertTrue(r.ran);
434 +        checkCompletedNormally(f, null);
435      }
436  
437      /**
# Line 415 | Line 439 | public class CompletableFutureTest exten
439       */
440      public void testRunAsync2() {
441          Noop r = new Noop();
442 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
442 >        ThreadExecutor exec = new ThreadExecutor();
443 >        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
444          assertNull(f.join());
445          assertTrue(r.ran);
446 +        checkCompletedNormally(f, null);
447 +        assertEquals(1, exec.count.get());
448      }
449  
450      /**
# Line 434 | Line 461 | public class CompletableFutureTest exten
461       * supplyAsync completes with result of supplier
462       */
463      public void testSupplyAsync() {
464 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
464 >        CompletableFuture<Integer> f;
465 >        f = CompletableFuture.supplyAsync(supplyOne);
466          assertEquals(f.join(), one);
467 +        checkCompletedNormally(f, one);
468      }
469  
470      /**
471       * supplyAsync with executor completes with result of supplier
472       */
473      public void testSupplyAsync2() {
474 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
474 >        CompletableFuture<Integer> f;
475 >        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
476          assertEquals(f.join(), one);
477 +        checkCompletedNormally(f, one);
478      }
479  
480      /**
# Line 456 | Line 487 | public class CompletableFutureTest exten
487          assertTrue(r.ran);
488      }
489  
490 <    // seq conmpletion methods
490 >    // seq completion methods
491  
492      /**
493       * thenRun result completes normally after normal completion of source
# Line 599 | Line 630 | public class CompletableFutureTest exten
630  
631  
632      /**
633 <     * thenCombine result completes normally after normal completion of sources
633 >     * thenCombine result completes normally after normal completion
634 >     * of sources
635       */
636      public void testThenCombine() {
637 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
606 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
607 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
608 <        f.complete(one);
609 <        checkIncomplete(g);
610 <        f2.complete(two);
611 <        checkCompletedNormally(g, three);
637 >        CompletableFuture<Integer> f, g, h;
638  
639          f = new CompletableFuture<Integer>();
640 <        f.complete(one);
641 <        f2 = new CompletableFuture<Integer>();
642 <        g = f.thenCombine(f2, add);
643 <        checkIncomplete(g);
644 <        f2.complete(two);
645 <        checkCompletedNormally(g, three);
640 >        g = new CompletableFuture<Integer>();
641 >        h = f.thenCombine(g, subtract);
642 >        f.complete(3);
643 >        checkIncomplete(h);
644 >        g.complete(1);
645 >        checkCompletedNormally(h, 2);
646 >
647 >        f = new CompletableFuture<Integer>();
648 >        g = new CompletableFuture<Integer>();
649 >        h = f.thenCombine(g, subtract);
650 >        g.complete(1);
651 >        checkIncomplete(h);
652 >        f.complete(3);
653 >        checkCompletedNormally(h, 2);
654 >
655 >        f = new CompletableFuture<Integer>();
656 >        g = new CompletableFuture<Integer>();
657 >        g.complete(1);
658 >        f.complete(3);
659 >        h = f.thenCombine(g, subtract);
660 >        checkCompletedNormally(h, 2);
661      }
662  
663      /**
# Line 624 | Line 665 | public class CompletableFutureTest exten
665       * completion of either source
666       */
667      public void testThenCombine2() {
668 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
669 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
670 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
668 >        CompletableFuture<Integer> f, g, h;
669 >
670 >        f = new CompletableFuture<Integer>();
671 >        g = new CompletableFuture<Integer>();
672 >        h = f.thenCombine(g, subtract);
673          f.completeExceptionally(new CFException());
674 <        f2.complete(two);
675 <        checkCompletedWithWrappedCFException(g);
674 >        checkIncomplete(h);
675 >        g.complete(1);
676 >        checkCompletedWithWrappedCFException(h);
677  
678          f = new CompletableFuture<Integer>();
679 <        f.complete(one);
680 <        f2 = new CompletableFuture<Integer>();
681 <        g = f.thenCombine(f2, add);
682 <        f2.completeExceptionally(new CFException());
683 <        checkCompletedWithWrappedCFException(g);
679 >        g = new CompletableFuture<Integer>();
680 >        h = f.thenCombine(g, subtract);
681 >        g.completeExceptionally(new CFException());
682 >        checkIncomplete(h);
683 >        f.complete(3);
684 >        checkCompletedWithWrappedCFException(h);
685 >
686 >        f = new CompletableFuture<Integer>();
687 >        g = new CompletableFuture<Integer>();
688 >        f.complete(3);
689 >        g.completeExceptionally(new CFException());
690 >        h = f.thenCombine(g, subtract);
691 >        checkCompletedWithWrappedCFException(h);
692 >
693 >        f = new CompletableFuture<Integer>();
694 >        g = new CompletableFuture<Integer>();
695 >        f.completeExceptionally(new CFException());
696 >        g.complete(3);
697 >        h = f.thenCombine(g, subtract);
698 >        checkCompletedWithWrappedCFException(h);
699      }
700  
701      /**
# Line 649 | Line 708 | public class CompletableFutureTest exten
708          CompletableFuture<Integer> g = f.thenCombine(f2, r);
709          f.complete(one);
710          checkIncomplete(g);
711 +        assertFalse(r.ran);
712          f2.complete(two);
713          checkCompletedWithWrappedCFException(g);
714 +        assertTrue(r.ran);
715      }
716  
717      /**
718       * thenCombine result completes exceptionally if either source cancelled
719       */
720      public void testThenCombine4() {
721 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
722 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
723 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
721 >        CompletableFuture<Integer> f, g, h;
722 >
723 >        f = new CompletableFuture<Integer>();
724 >        g = new CompletableFuture<Integer>();
725 >        h = f.thenCombine(g, subtract);
726          assertTrue(f.cancel(true));
727 <        f2.complete(two);
728 <        checkCompletedWithWrappedCancellationException(g);
727 >        checkIncomplete(h);
728 >        g.complete(1);
729 >        checkCompletedWithWrappedCancellationException(h);
730 >
731          f = new CompletableFuture<Integer>();
732 <        f2 = new CompletableFuture<Integer>();
733 <        g = f.thenCombine(f2, add);
734 <        f.complete(one);
735 <        assertTrue(f2.cancel(true));
736 <        checkCompletedWithWrappedCancellationException(g);
732 >        g = new CompletableFuture<Integer>();
733 >        h = f.thenCombine(g, subtract);
734 >        assertTrue(g.cancel(true));
735 >        checkIncomplete(h);
736 >        f.complete(3);
737 >        checkCompletedWithWrappedCancellationException(h);
738 >
739 >        f = new CompletableFuture<Integer>();
740 >        g = new CompletableFuture<Integer>();
741 >        assertTrue(f.cancel(true));
742 >        assertTrue(g.cancel(true));
743 >        h = f.thenCombine(g, subtract);
744 >        checkCompletedWithWrappedCancellationException(h);
745      }
746  
747      /**
# Line 1246 | Line 1319 | public class CompletableFutureTest exten
1319       * completion of sources
1320       */
1321      public void testThenCombineAsync() {
1322 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1323 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1324 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1325 <        f.complete(one);
1326 <        checkIncomplete(g);
1327 <        f2.complete(two);
1328 <        checkCompletedNormally(g, three);
1322 >        CompletableFuture<Integer> f, g, h;
1323 >
1324 >        f = new CompletableFuture<Integer>();
1325 >        g = new CompletableFuture<Integer>();
1326 >        h = f.thenCombineAsync(g, subtract);
1327 >        f.complete(3);
1328 >        checkIncomplete(h);
1329 >        g.complete(1);
1330 >        checkCompletedNormally(h, 2);
1331 >
1332 >        f = new CompletableFuture<Integer>();
1333 >        g = new CompletableFuture<Integer>();
1334 >        h = f.thenCombineAsync(g, subtract);
1335 >        g.complete(1);
1336 >        checkIncomplete(h);
1337 >        f.complete(3);
1338 >        checkCompletedNormally(h, 2);
1339 >
1340 >        f = new CompletableFuture<Integer>();
1341 >        g = new CompletableFuture<Integer>();
1342 >        g.complete(1);
1343 >        f.complete(3);
1344 >        h = f.thenCombineAsync(g, subtract);
1345 >        checkCompletedNormally(h, 2);
1346      }
1347  
1348      /**
1349       * thenCombineAsync result completes exceptionally after exceptional
1350 <     * completion of source
1350 >     * completion of either source
1351       */
1352      public void testThenCombineAsync2() {
1353 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1354 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1355 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1353 >        CompletableFuture<Integer> f, g, h;
1354 >
1355 >        f = new CompletableFuture<Integer>();
1356 >        g = new CompletableFuture<Integer>();
1357 >        h = f.thenCombineAsync(g, subtract);
1358          f.completeExceptionally(new CFException());
1359 <        f2.complete(two);
1360 <        checkCompletedWithWrappedCFException(g);
1359 >        checkIncomplete(h);
1360 >        g.complete(1);
1361 >        checkCompletedWithWrappedCFException(h);
1362  
1363          f = new CompletableFuture<Integer>();
1364 <        f2 = new CompletableFuture<Integer>();
1365 <        g = f.thenCombineAsync(f2, add);
1366 <        f.complete(one);
1367 <        f2.completeExceptionally(new CFException());
1368 <        checkCompletedWithWrappedCFException(g);
1364 >        g = new CompletableFuture<Integer>();
1365 >        h = f.thenCombineAsync(g, subtract);
1366 >        g.completeExceptionally(new CFException());
1367 >        checkIncomplete(h);
1368 >        f.complete(3);
1369 >        checkCompletedWithWrappedCFException(h);
1370 >
1371 >        f = new CompletableFuture<Integer>();
1372 >        g = new CompletableFuture<Integer>();
1373 >        g.completeExceptionally(new CFException());
1374 >        f.complete(3);
1375 >        h = f.thenCombineAsync(g, subtract);
1376 >        checkCompletedWithWrappedCFException(h);
1377      }
1378  
1379      /**
# Line 1285 | Line 1386 | public class CompletableFutureTest exten
1386          CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1387          f.complete(one);
1388          checkIncomplete(g);
1389 +        assertFalse(r.ran);
1390          f2.complete(two);
1391          checkCompletedWithWrappedCFException(g);
1392 +        assertTrue(r.ran);
1393      }
1394  
1395      /**
1396       * thenCombineAsync result completes exceptionally if either source cancelled
1397       */
1398      public void testThenCombineAsync4() {
1399 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1400 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1401 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1399 >        CompletableFuture<Integer> f, g, h;
1400 >
1401 >        f = new CompletableFuture<Integer>();
1402 >        g = new CompletableFuture<Integer>();
1403 >        h = f.thenCombineAsync(g, subtract);
1404          assertTrue(f.cancel(true));
1405 <        f2.complete(two);
1406 <        checkCompletedWithWrappedCancellationException(g);
1405 >        checkIncomplete(h);
1406 >        g.complete(1);
1407 >        checkCompletedWithWrappedCancellationException(h);
1408  
1409          f = new CompletableFuture<Integer>();
1410 <        f2 = new CompletableFuture<Integer>();
1411 <        g = f.thenCombineAsync(f2, add);
1412 <        f.complete(one);
1413 <        assertTrue(f2.cancel(true));
1414 <        checkCompletedWithWrappedCancellationException(g);
1410 >        g = new CompletableFuture<Integer>();
1411 >        h = f.thenCombineAsync(g, subtract);
1412 >        assertTrue(g.cancel(true));
1413 >        checkIncomplete(h);
1414 >        f.complete(3);
1415 >        checkCompletedWithWrappedCancellationException(h);
1416 >
1417 >        f = new CompletableFuture<Integer>();
1418 >        g = new CompletableFuture<Integer>();
1419 >        g.complete(3);
1420 >        assertTrue(f.cancel(true));
1421 >        h = f.thenCombineAsync(g, subtract);
1422 >        checkCompletedWithWrappedCancellationException(h);
1423 >
1424 >        f = new CompletableFuture<Integer>();
1425 >        g = new CompletableFuture<Integer>();
1426 >        f.complete(3);
1427 >        assertTrue(g.cancel(true));
1428 >        h = f.thenCombineAsync(g, subtract);
1429 >        checkCompletedWithWrappedCancellationException(h);
1430      }
1431  
1432      /**
# Line 1671 | Line 1792 | public class CompletableFutureTest exten
1792      }
1793  
1794      /**
1795 <     * thenCompse result completes normally after normal completion of source
1795 >     * thenComposeAsync result completes normally after normal
1796 >     * completion of source
1797       */
1798      public void testThenComposeAsync() {
1799          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
# Line 1682 | Line 1804 | public class CompletableFutureTest exten
1804      }
1805  
1806      /**
1807 <     * thenComposeAsync result completes exceptionally after exceptional
1808 <     * completion of source
1807 >     * thenComposeAsync result completes exceptionally after
1808 >     * exceptional completion of source
1809       */
1810      public void testThenComposeAsync2() {
1811          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
# Line 1716 | Line 1838 | public class CompletableFutureTest exten
1838      }
1839  
1840  
1841 <    // aaync with explicit executors
1841 >    // async with explicit executors
1842  
1843      /**
1844       * thenRunAsync result completes normally after normal completion of source
# Line 1863 | Line 1985 | public class CompletableFutureTest exten
1985          assertTrue(f.cancel(true));
1986          checkCompletedWithWrappedCancellationException(g);
1987      }
1988 +
1989      /**
1990       * thenCombineAsync result completes normally after normal
1991       * completion of sources
1992       */
1993      public void testThenCombineAsyncE() {
1994 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1995 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1996 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
1997 <        f.complete(one);
1998 <        checkIncomplete(g);
1999 <        f2.complete(two);
2000 <        checkCompletedNormally(g, three);
1994 >        CompletableFuture<Integer> f, g, h;
1995 >        ThreadExecutor e = new ThreadExecutor();
1996 >        int count = 0;
1997 >
1998 >        f = new CompletableFuture<Integer>();
1999 >        g = new CompletableFuture<Integer>();
2000 >        h = f.thenCombineAsync(g, subtract, e);
2001 >        f.complete(3);
2002 >        checkIncomplete(h);
2003 >        g.complete(1);
2004 >        checkCompletedNormally(h, 2);
2005 >        assertEquals(++count, e.count.get());
2006 >
2007 >        f = new CompletableFuture<Integer>();
2008 >        g = new CompletableFuture<Integer>();
2009 >        h = f.thenCombineAsync(g, subtract, e);
2010 >        g.complete(1);
2011 >        checkIncomplete(h);
2012 >        f.complete(3);
2013 >        checkCompletedNormally(h, 2);
2014 >        assertEquals(++count, e.count.get());
2015 >
2016 >        f = new CompletableFuture<Integer>();
2017 >        g = new CompletableFuture<Integer>();
2018 >        g.complete(1);
2019 >        f.complete(3);
2020 >        h = f.thenCombineAsync(g, subtract, e);
2021 >        checkCompletedNormally(h, 2);
2022 >        assertEquals(++count, e.count.get());
2023      }
2024  
2025      /**
2026       * thenCombineAsync result completes exceptionally after exceptional
2027 <     * completion of source
2027 >     * completion of either source
2028       */
2029      public void testThenCombineAsync2E() {
2030 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2031 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2032 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2030 >        CompletableFuture<Integer> f, g, h;
2031 >        ThreadExecutor e = new ThreadExecutor();
2032 >        int count = 0;
2033 >
2034 >        f = new CompletableFuture<Integer>();
2035 >        g = new CompletableFuture<Integer>();
2036 >        h = f.thenCombineAsync(g, subtract, e);
2037          f.completeExceptionally(new CFException());
2038 <        f2.complete(two);
2039 <        checkCompletedWithWrappedCFException(g);
2038 >        checkIncomplete(h);
2039 >        g.complete(1);
2040 >        checkCompletedWithWrappedCFException(h);
2041  
2042          f = new CompletableFuture<Integer>();
2043 <        f2 = new CompletableFuture<Integer>();
2044 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2045 <        f.complete(one);
2046 <        f2.completeExceptionally(new CFException());
2047 <        checkCompletedWithWrappedCFException(g);
2043 >        g = new CompletableFuture<Integer>();
2044 >        h = f.thenCombineAsync(g, subtract, e);
2045 >        g.completeExceptionally(new CFException());
2046 >        checkIncomplete(h);
2047 >        f.complete(3);
2048 >        checkCompletedWithWrappedCFException(h);
2049 >
2050 >        f = new CompletableFuture<Integer>();
2051 >        g = new CompletableFuture<Integer>();
2052 >        g.completeExceptionally(new CFException());
2053 >        h = f.thenCombineAsync(g, subtract, e);
2054 >        checkIncomplete(h);
2055 >        f.complete(3);
2056 >        checkCompletedWithWrappedCFException(h);
2057 >
2058 >        assertEquals(0, e.count.get());
2059      }
2060  
2061      /**
# Line 1907 | Line 2068 | public class CompletableFutureTest exten
2068          CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2069          f.complete(one);
2070          checkIncomplete(g);
2071 +        assertFalse(r.ran);
2072          f2.complete(two);
2073          checkCompletedWithWrappedCFException(g);
2074 +        assertTrue(r.ran);
2075      }
2076  
2077      /**
2078       * thenCombineAsync result completes exceptionally if either source cancelled
2079       */
2080      public void testThenCombineAsync4E() {
2081 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2082 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2083 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2081 >        CompletableFuture<Integer> f, g, h;
2082 >        ThreadExecutor e = new ThreadExecutor();
2083 >
2084 >        f = new CompletableFuture<Integer>();
2085 >        g = new CompletableFuture<Integer>();
2086 >        h = f.thenCombineAsync(g, subtract, e);
2087          assertTrue(f.cancel(true));
2088 <        f2.complete(two);
2089 <        checkCompletedWithWrappedCancellationException(g);
2088 >        checkIncomplete(h);
2089 >        g.complete(1);
2090 >        checkCompletedWithWrappedCancellationException(h);
2091  
2092          f = new CompletableFuture<Integer>();
2093 <        f2 = new CompletableFuture<Integer>();
2094 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2095 <        f.complete(one);
2096 <        assertTrue(f2.cancel(true));
2097 <        checkCompletedWithWrappedCancellationException(g);
2093 >        g = new CompletableFuture<Integer>();
2094 >        h = f.thenCombineAsync(g, subtract, e);
2095 >        assertTrue(g.cancel(true));
2096 >        checkIncomplete(h);
2097 >        f.complete(3);
2098 >        checkCompletedWithWrappedCancellationException(h);
2099 >
2100 >        f = new CompletableFuture<Integer>();
2101 >        g = new CompletableFuture<Integer>();
2102 >        assertTrue(g.cancel(true));
2103 >        h = f.thenCombineAsync(g, subtract, e);
2104 >        checkIncomplete(h);
2105 >        f.complete(3);
2106 >        checkCompletedWithWrappedCancellationException(h);
2107 >
2108 >        f = new CompletableFuture<Integer>();
2109 >        g = new CompletableFuture<Integer>();
2110 >        assertTrue(f.cancel(true));
2111 >        assertTrue(g.cancel(true));
2112 >        h = f.thenCombineAsync(g, subtract, e);
2113 >        checkCompletedWithWrappedCancellationException(h);
2114 >
2115 >        assertEquals(0, e.count.get());
2116      }
2117  
2118      /**
# Line 2293 | Line 2478 | public class CompletableFutureTest exten
2478      }
2479  
2480      /**
2481 <     * thenCompse result completes normally after normal completion of source
2481 >     * thenComposeAsync result completes normally after normal
2482 >     * completion of source
2483       */
2484      public void testThenComposeAsyncE() {
2485          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
# Line 2304 | Line 2490 | public class CompletableFutureTest exten
2490      }
2491  
2492      /**
2493 <     * thenComposeAsync result completes exceptionally after exceptional
2494 <     * completion of source
2493 >     * thenComposeAsync result completes exceptionally after
2494 >     * exceptional completion of source
2495       */
2496      public void testThenComposeAsync2E() {
2497          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
# Line 2356 | Line 2542 | public class CompletableFutureTest exten
2542              CompletableFuture[] fs = new CompletableFuture[k];
2543              for (int i = 0; i < k; ++i)
2544                  fs[i] = new CompletableFuture<Integer>();
2545 <            CompletableFuture<?> f = CompletableFuture.allOf(fs);
2545 >            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
2546              for (int i = 0; i < k; ++i) {
2547                  checkIncomplete(f);
2548                  fs[i].complete(one);
2549              }
2550 <            assertTrue(f.isDone());
2365 <            assertFalse(f.isCancelled());
2550 >            checkCompletedNormally(f, null);
2551          }
2552      }
2553  
# Line 2375 | Line 2560 | public class CompletableFutureTest exten
2560      }
2561  
2562      /**
2563 <     * allOf returns a future completed when any components complete
2563 >     * anyOf returns a future completed when any components complete
2564       */
2565      public void testAnyOf() throws Exception {
2566          for (int k = 1; k < 20; ++k) {
2567              CompletableFuture[] fs = new CompletableFuture[k];
2568              for (int i = 0; i < k; ++i)
2569                  fs[i] = new CompletableFuture<Integer>();
2570 <            CompletableFuture<?> f = CompletableFuture.anyOf(fs);
2570 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
2571              checkIncomplete(f);
2572              for (int i = 0; i < k; ++i) {
2573                  fs[i].complete(one);
2574 <                assertTrue(f.isDone());
2574 >                checkCompletedNormally(f, one);
2575              }
2576          }
2577      }
# Line 2397 | Line 2582 | public class CompletableFutureTest exten
2582      public void testNPE() {
2583          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2584          CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2585 <        CompletableFuture h;
2586 <        try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2587 <        try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2588 <        try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2589 <        try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2590 <        try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2591 <        try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2592 <        try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2593 <        try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2594 <        try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2595 <        try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2596 <        try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2597 <        try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2598 <        try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2599 <        try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2600 <        try { h = f.handle(null); } catch (NullPointerException ok) {}
2601 <        try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2602 <
2603 <        try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2604 <        try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2605 <        try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2606 <        try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2607 <        try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2608 <        try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2609 <        try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2610 <        try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2611 <        try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2612 <        try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2613 <        try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2614 <        try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2615 <        try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2585 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2586 >        CompletableFuture<?> h;
2587 >        ThreadExecutor exec = new ThreadExecutor();
2588 >
2589 >        Runnable[] throwingActions = {
2590 >            () -> { CompletableFuture.supplyAsync(null); },
2591 >            () -> { CompletableFuture.supplyAsync(null, exec); },
2592 >            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2593 >
2594 >            () -> { CompletableFuture.runAsync(null); },
2595 >            () -> { CompletableFuture.runAsync(null, exec); },
2596 >            () -> { CompletableFuture.runAsync(() -> {}, null); },
2597 >
2598 >            () -> { f.completeExceptionally(null); },
2599 >
2600 >            () -> { f.thenApply(null); },
2601 >            () -> { f.thenApplyAsync(null); },
2602 >            () -> { f.thenApplyAsync((x) -> x, null); },
2603 >            () -> { f.thenApplyAsync(null, exec); },
2604 >
2605 >            () -> { f.thenAccept(null); },
2606 >            () -> { f.thenAcceptAsync(null); },
2607 >            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2608 >            () -> { f.thenAcceptAsync(null, exec); },
2609 >
2610 >            () -> { f.thenRun(null); },
2611 >            () -> { f.thenRunAsync(null); },
2612 >            () -> { f.thenRunAsync(() -> { ; }, null); },
2613 >            () -> { f.thenRunAsync(null, exec); },
2614 >
2615 >            () -> { f.thenCombine(g, null); },
2616 >            () -> { f.thenCombineAsync(g, null); },
2617 >            () -> { f.thenCombineAsync(g, null, exec); },
2618 >            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2619 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2620 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2621 >            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2622 >
2623 >            () -> { f.thenAcceptBoth(g, null); },
2624 >            () -> { f.thenAcceptBothAsync(g, null); },
2625 >            () -> { f.thenAcceptBothAsync(g, null, exec); },
2626 >            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2627 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2628 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2629 >            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2630 >
2631 >            () -> { f.runAfterBoth(g, null); },
2632 >            () -> { f.runAfterBothAsync(g, null); },
2633 >            () -> { f.runAfterBothAsync(g, null, exec); },
2634 >            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2635 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2636 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2637 >            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2638 >
2639 >            () -> { f.applyToEither(g, null); },
2640 >            () -> { f.applyToEitherAsync(g, null); },
2641 >            () -> { f.applyToEitherAsync(g, null, exec); },
2642 >            () -> { f.applyToEither(nullFuture, (x) -> x); },
2643 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2644 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2645 >            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2646 >
2647 >            () -> { f.acceptEither(g, null); },
2648 >            () -> { f.acceptEitherAsync(g, null); },
2649 >            () -> { f.acceptEitherAsync(g, null, exec); },
2650 >            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2651 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2652 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2653 >            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2654 >
2655 >            () -> { f.runAfterEither(g, null); },
2656 >            () -> { f.runAfterEitherAsync(g, null); },
2657 >            () -> { f.runAfterEitherAsync(g, null, exec); },
2658 >            () -> { f.runAfterEither(nullFuture, () -> {}); },
2659 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2660 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2661 >            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2662 >
2663 >            () -> { f.thenCompose(null); },
2664 >            () -> { f.thenComposeAsync(null); },
2665 >            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2666 >            () -> { f.thenComposeAsync(null, exec); },
2667 >
2668 >            () -> { f.exceptionally(null); },
2669 >
2670 >            () -> { f.handle(null); },
2671 >
2672 >            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2673 >            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2674 >            () -> { CompletableFuture.allOf(f, null); },
2675 >            () -> { CompletableFuture.allOf(null, f); },
2676 >
2677 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2678 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2679 >            () -> { CompletableFuture.anyOf(f, null); },
2680 >            () -> { CompletableFuture.anyOf(null, f); },
2681 >        };
2682  
2683 +        assertThrows(NullPointerException.class, throwingActions);
2684 +        assertEquals(0, exec.count.get());
2685      }
2686  
2434
2687   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines