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.13 by jsr166, Sun Mar 31 18:17:18 2013 UTC vs.
Revision 1.20 by jsr166, Mon Apr 8 16:45:15 2013 UTC

# Line 55 | Line 55 | public class CompletableFutureTest exten
55  
56      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
57          try {
58 +            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
59 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
60 +        try {
61              assertEquals(value, f.join());
62          } catch (Throwable fail) { threadUnexpectedException(fail); }
63          try {
# Line 63 | Line 66 | public class CompletableFutureTest exten
66          try {
67              assertEquals(value, f.get());
68          } catch (Throwable fail) { threadUnexpectedException(fail); }
66        try {
67            assertEquals(value, f.get(0L, SECONDS));
68        } catch (Throwable fail) { threadUnexpectedException(fail); }
69          assertTrue(f.isDone());
70          assertFalse(f.isCancelled());
71          assertTrue(f.toString().contains("[Completed normally]"));
# Line 73 | Line 73 | public class CompletableFutureTest exten
73  
74      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
75          try {
76 +            f.get(LONG_DELAY_MS, MILLISECONDS);
77 +            shouldThrow();
78 +        } catch (ExecutionException success) {
79 +            assertTrue(success.getCause() instanceof CFException);
80 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
81 +        try {
82              f.join();
83              shouldThrow();
84          } catch (CompletionException success) {
# Line 90 | Line 96 | public class CompletableFutureTest exten
96          } catch (ExecutionException success) {
97              assertTrue(success.getCause() instanceof CFException);
98          } catch (Throwable fail) { threadUnexpectedException(fail); }
93        try {
94            f.get(0L, SECONDS);
95            shouldThrow();
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.get(LONG_DELAY_MS, MILLISECONDS);
107 +            shouldThrow();
108 +        } catch (CancellationException success) {
109 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
110 +        try {
111              f.join();
112              shouldThrow();
113          } catch (CancellationException success) {}
# Line 114 | Line 120 | public class CompletableFutureTest exten
120              shouldThrow();
121          } catch (CancellationException success) {
122          } catch (Throwable fail) { threadUnexpectedException(fail); }
117        try {
118            f.get(0L, SECONDS);
119            shouldThrow();
120        } catch (CancellationException success) {
121        } 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.get(LONG_DELAY_MS, MILLISECONDS);
131 +            shouldThrow();
132 +        } catch (ExecutionException success) {
133 +            assertTrue(success.getCause() instanceof CancellationException);
134 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
135 +        try {
136              f.join();
137              shouldThrow();
138          } catch (CompletionException success) {
# Line 142 | Line 150 | public class CompletableFutureTest exten
150          } catch (ExecutionException success) {
151              assertTrue(success.getCause() instanceof CancellationException);
152          } catch (Throwable fail) { threadUnexpectedException(fail); }
145        try {
146            f.get(0L, SECONDS);
147            shouldThrow();
148        } catch (ExecutionException success) {
149            assertTrue(success.getCause() instanceof CancellationException);
150        } catch (Throwable fail) { threadUnexpectedException(fail); }
153          assertTrue(f.isDone());
154          assertFalse(f.isCancelled());
155 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
156      }
157  
158      /**
# Line 279 | Line 282 | public class CompletableFutureTest exten
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 325 | 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 >        boolean ran;
336          public CompletableFuture<Integer> apply(Integer x) {
337 +            ran = true;
338              CompletableFuture<Integer> f = new CompletableFuture<Integer>();
339              f.complete(Integer.valueOf(x.intValue() + 1));
340              return f;
341          }
342      }
343  
344 <    static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> {
344 >    static final class FailingCompletableFutureFunction
345 >        implements Function<Integer, CompletableFuture<Integer>> {
346          boolean ran;
347          public CompletableFuture<Integer> apply(Integer x) {
348              ran = true; throw new CFException();
# Line 342 | Line 351 | public class CompletableFutureTest exten
351  
352      // Used for explicit executor tests
353      static final class ThreadExecutor implements Executor {
354 +        AtomicInteger count = new AtomicInteger(0);
355 +
356          public void execute(Runnable r) {
357 +            count.getAndIncrement();
358              new Thread(r).start();
359          }
360      }
# Line 352 | Line 364 | public class CompletableFutureTest exten
364      }
365  
366      static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> {
367 +        boolean ran;
368          public Integer apply(Integer x, Throwable t) {
369 +            ran = true;
370              return (t == null) ? two : three;
371          }
372      }
# Line 381 | Line 395 | public class CompletableFutureTest exten
395       * normal or exceptional completion of source
396       */
397      public void testHandle() {
398 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
399 <        IntegerHandler r = new IntegerHandler();
400 <        CompletableFuture<Integer> g = f.handle(r);
398 >        CompletableFuture<Integer> f, g;
399 >        IntegerHandler r;
400 >
401 >        f = new CompletableFuture<Integer>();
402          f.completeExceptionally(new CFException());
403 +        g = f.handle(r = new IntegerHandler());
404 +        assertTrue(r.ran);
405          checkCompletedNormally(g, three);
406  
407          f = new CompletableFuture<Integer>();
408 <        r = new IntegerHandler();
409 <        g = f.handle(r);
408 >        g = f.handle(r = new IntegerHandler());
409 >        assertFalse(r.ran);
410 >        f.completeExceptionally(new CFException());
411 >        checkCompletedNormally(g, three);
412 >        assertTrue(r.ran);
413 >
414 >        f = new CompletableFuture<Integer>();
415 >        f.complete(one);
416 >        g = f.handle(r = new IntegerHandler());
417 >        assertTrue(r.ran);
418 >        checkCompletedNormally(g, two);
419 >
420 >        f = new CompletableFuture<Integer>();
421 >        g = f.handle(r = new IntegerHandler());
422 >        assertFalse(r.ran);
423          f.complete(one);
424 +        assertTrue(r.ran);
425          checkCompletedNormally(g, two);
426      }
427  
# Line 402 | Line 433 | public class CompletableFutureTest exten
433          CompletableFuture<Void> f = CompletableFuture.runAsync(r);
434          assertNull(f.join());
435          assertTrue(r.ran);
436 +        checkCompletedNormally(f, null);
437      }
438  
439      /**
# Line 409 | Line 441 | public class CompletableFutureTest exten
441       */
442      public void testRunAsync2() {
443          Noop r = new Noop();
444 <        CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor());
444 >        ThreadExecutor exec = new ThreadExecutor();
445 >        CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec);
446          assertNull(f.join());
447          assertTrue(r.ran);
448 +        checkCompletedNormally(f, null);
449 +        assertEquals(1, exec.count.get());
450      }
451  
452      /**
# Line 428 | Line 463 | public class CompletableFutureTest exten
463       * supplyAsync completes with result of supplier
464       */
465      public void testSupplyAsync() {
466 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne);
466 >        CompletableFuture<Integer> f;
467 >        f = CompletableFuture.supplyAsync(supplyOne);
468          assertEquals(f.join(), one);
469 +        checkCompletedNormally(f, one);
470      }
471  
472      /**
473       * supplyAsync with executor completes with result of supplier
474       */
475      public void testSupplyAsync2() {
476 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
476 >        CompletableFuture<Integer> f;
477 >        f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor());
478          assertEquals(f.join(), one);
479 +        checkCompletedNormally(f, one);
480      }
481  
482      /**
# Line 593 | Line 632 | public class CompletableFutureTest exten
632  
633  
634      /**
635 <     * thenCombine result completes normally after normal completion of sources
635 >     * thenCombine result completes normally after normal completion
636 >     * of sources
637       */
638      public void testThenCombine() {
639 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
600 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
601 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
602 <        f.complete(one);
603 <        checkIncomplete(g);
604 <        f2.complete(two);
605 <        checkCompletedNormally(g, three);
639 >        CompletableFuture<Integer> f, g, h;
640  
641          f = new CompletableFuture<Integer>();
642 <        f.complete(one);
643 <        f2 = new CompletableFuture<Integer>();
644 <        g = f.thenCombine(f2, add);
645 <        checkIncomplete(g);
646 <        f2.complete(two);
647 <        checkCompletedNormally(g, three);
642 >        g = new CompletableFuture<Integer>();
643 >        h = f.thenCombine(g, subtract);
644 >        f.complete(3);
645 >        checkIncomplete(h);
646 >        g.complete(1);
647 >        checkCompletedNormally(h, 2);
648 >
649 >        f = new CompletableFuture<Integer>();
650 >        g = new CompletableFuture<Integer>();
651 >        h = f.thenCombine(g, subtract);
652 >        g.complete(1);
653 >        checkIncomplete(h);
654 >        f.complete(3);
655 >        checkCompletedNormally(h, 2);
656 >
657 >        f = new CompletableFuture<Integer>();
658 >        g = new CompletableFuture<Integer>();
659 >        g.complete(1);
660 >        f.complete(3);
661 >        h = f.thenCombine(g, subtract);
662 >        checkCompletedNormally(h, 2);
663      }
664  
665      /**
# Line 618 | Line 667 | public class CompletableFutureTest exten
667       * completion of either source
668       */
669      public void testThenCombine2() {
670 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
671 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
672 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
670 >        CompletableFuture<Integer> f, g, h;
671 >
672 >        f = new CompletableFuture<Integer>();
673 >        g = new CompletableFuture<Integer>();
674 >        h = f.thenCombine(g, subtract);
675          f.completeExceptionally(new CFException());
676 <        f2.complete(two);
677 <        checkCompletedWithWrappedCFException(g);
676 >        checkIncomplete(h);
677 >        g.complete(1);
678 >        checkCompletedWithWrappedCFException(h);
679  
680          f = new CompletableFuture<Integer>();
681 <        f.complete(one);
682 <        f2 = new CompletableFuture<Integer>();
683 <        g = f.thenCombine(f2, add);
684 <        f2.completeExceptionally(new CFException());
685 <        checkCompletedWithWrappedCFException(g);
681 >        g = new CompletableFuture<Integer>();
682 >        h = f.thenCombine(g, subtract);
683 >        g.completeExceptionally(new CFException());
684 >        checkIncomplete(h);
685 >        f.complete(3);
686 >        checkCompletedWithWrappedCFException(h);
687 >
688 >        f = new CompletableFuture<Integer>();
689 >        g = new CompletableFuture<Integer>();
690 >        f.complete(3);
691 >        g.completeExceptionally(new CFException());
692 >        h = f.thenCombine(g, subtract);
693 >        checkCompletedWithWrappedCFException(h);
694 >
695 >        f = new CompletableFuture<Integer>();
696 >        g = new CompletableFuture<Integer>();
697 >        f.completeExceptionally(new CFException());
698 >        g.complete(3);
699 >        h = f.thenCombine(g, subtract);
700 >        checkCompletedWithWrappedCFException(h);
701      }
702  
703      /**
# Line 643 | Line 710 | public class CompletableFutureTest exten
710          CompletableFuture<Integer> g = f.thenCombine(f2, r);
711          f.complete(one);
712          checkIncomplete(g);
713 +        assertFalse(r.ran);
714          f2.complete(two);
715          checkCompletedWithWrappedCFException(g);
716 +        assertTrue(r.ran);
717      }
718  
719      /**
720       * thenCombine result completes exceptionally if either source cancelled
721       */
722      public void testThenCombine4() {
723 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
724 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
725 <        CompletableFuture<Integer> g = f.thenCombine(f2, add);
723 >        CompletableFuture<Integer> f, g, h;
724 >
725 >        f = new CompletableFuture<Integer>();
726 >        g = new CompletableFuture<Integer>();
727 >        h = f.thenCombine(g, subtract);
728          assertTrue(f.cancel(true));
729 <        f2.complete(two);
730 <        checkCompletedWithWrappedCancellationException(g);
729 >        checkIncomplete(h);
730 >        g.complete(1);
731 >        checkCompletedWithWrappedCancellationException(h);
732 >
733          f = new CompletableFuture<Integer>();
734 <        f2 = new CompletableFuture<Integer>();
735 <        g = f.thenCombine(f2, add);
736 <        f.complete(one);
737 <        assertTrue(f2.cancel(true));
738 <        checkCompletedWithWrappedCancellationException(g);
734 >        g = new CompletableFuture<Integer>();
735 >        h = f.thenCombine(g, subtract);
736 >        assertTrue(g.cancel(true));
737 >        checkIncomplete(h);
738 >        f.complete(3);
739 >        checkCompletedWithWrappedCancellationException(h);
740 >
741 >        f = new CompletableFuture<Integer>();
742 >        g = new CompletableFuture<Integer>();
743 >        assertTrue(f.cancel(true));
744 >        assertTrue(g.cancel(true));
745 >        h = f.thenCombine(g, subtract);
746 >        checkCompletedWithWrappedCancellationException(h);
747      }
748  
749      /**
# Line 1235 | Line 1316 | public class CompletableFutureTest exten
1316          assertTrue(f.cancel(true));
1317          checkCompletedWithWrappedCancellationException(g);
1318      }
1319 +
1320      /**
1321       * thenCombineAsync result completes normally after normal
1322       * completion of sources
1323       */
1324      public void testThenCombineAsync() {
1325 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1326 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1327 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1328 <        f.complete(one);
1329 <        checkIncomplete(g);
1330 <        f2.complete(two);
1331 <        checkCompletedNormally(g, three);
1325 >        CompletableFuture<Integer> f, g, h;
1326 >
1327 >        f = new CompletableFuture<Integer>();
1328 >        g = new CompletableFuture<Integer>();
1329 >        h = f.thenCombineAsync(g, subtract);
1330 >        f.complete(3);
1331 >        checkIncomplete(h);
1332 >        g.complete(1);
1333 >        checkCompletedNormally(h, 2);
1334 >
1335 >        f = new CompletableFuture<Integer>();
1336 >        g = new CompletableFuture<Integer>();
1337 >        h = f.thenCombineAsync(g, subtract);
1338 >        g.complete(1);
1339 >        checkIncomplete(h);
1340 >        f.complete(3);
1341 >        checkCompletedNormally(h, 2);
1342 >
1343 >        f = new CompletableFuture<Integer>();
1344 >        g = new CompletableFuture<Integer>();
1345 >        g.complete(1);
1346 >        f.complete(3);
1347 >        h = f.thenCombineAsync(g, subtract);
1348 >        checkCompletedNormally(h, 2);
1349      }
1350  
1351      /**
1352       * thenCombineAsync result completes exceptionally after exceptional
1353 <     * completion of source
1353 >     * completion of either source
1354       */
1355      public void testThenCombineAsync2() {
1356 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1357 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1358 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1356 >        CompletableFuture<Integer> f, g, h;
1357 >
1358 >        f = new CompletableFuture<Integer>();
1359 >        g = new CompletableFuture<Integer>();
1360 >        h = f.thenCombineAsync(g, subtract);
1361          f.completeExceptionally(new CFException());
1362 <        f2.complete(two);
1363 <        checkCompletedWithWrappedCFException(g);
1362 >        checkIncomplete(h);
1363 >        g.complete(1);
1364 >        checkCompletedWithWrappedCFException(h);
1365  
1366          f = new CompletableFuture<Integer>();
1367 <        f2 = new CompletableFuture<Integer>();
1368 <        g = f.thenCombineAsync(f2, add);
1369 <        f.complete(one);
1370 <        f2.completeExceptionally(new CFException());
1371 <        checkCompletedWithWrappedCFException(g);
1367 >        g = new CompletableFuture<Integer>();
1368 >        h = f.thenCombineAsync(g, subtract);
1369 >        g.completeExceptionally(new CFException());
1370 >        checkIncomplete(h);
1371 >        f.complete(3);
1372 >        checkCompletedWithWrappedCFException(h);
1373 >
1374 >        f = new CompletableFuture<Integer>();
1375 >        g = new CompletableFuture<Integer>();
1376 >        g.completeExceptionally(new CFException());
1377 >        f.complete(3);
1378 >        h = f.thenCombineAsync(g, subtract);
1379 >        checkCompletedWithWrappedCFException(h);
1380      }
1381  
1382      /**
# Line 1279 | Line 1389 | public class CompletableFutureTest exten
1389          CompletableFuture<Integer> g = f.thenCombineAsync(f2, r);
1390          f.complete(one);
1391          checkIncomplete(g);
1392 +        assertFalse(r.ran);
1393          f2.complete(two);
1394          checkCompletedWithWrappedCFException(g);
1395 +        assertTrue(r.ran);
1396      }
1397  
1398      /**
1399       * thenCombineAsync result completes exceptionally if either source cancelled
1400       */
1401      public void testThenCombineAsync4() {
1402 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1403 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
1404 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add);
1402 >        CompletableFuture<Integer> f, g, h;
1403 >
1404 >        f = new CompletableFuture<Integer>();
1405 >        g = new CompletableFuture<Integer>();
1406 >        h = f.thenCombineAsync(g, subtract);
1407          assertTrue(f.cancel(true));
1408 <        f2.complete(two);
1409 <        checkCompletedWithWrappedCancellationException(g);
1408 >        checkIncomplete(h);
1409 >        g.complete(1);
1410 >        checkCompletedWithWrappedCancellationException(h);
1411  
1412          f = new CompletableFuture<Integer>();
1413 <        f2 = new CompletableFuture<Integer>();
1414 <        g = f.thenCombineAsync(f2, add);
1415 <        f.complete(one);
1416 <        assertTrue(f2.cancel(true));
1417 <        checkCompletedWithWrappedCancellationException(g);
1413 >        g = new CompletableFuture<Integer>();
1414 >        h = f.thenCombineAsync(g, subtract);
1415 >        assertTrue(g.cancel(true));
1416 >        checkIncomplete(h);
1417 >        f.complete(3);
1418 >        checkCompletedWithWrappedCancellationException(h);
1419 >
1420 >        f = new CompletableFuture<Integer>();
1421 >        g = new CompletableFuture<Integer>();
1422 >        g.complete(3);
1423 >        assertTrue(f.cancel(true));
1424 >        h = f.thenCombineAsync(g, subtract);
1425 >        checkCompletedWithWrappedCancellationException(h);
1426 >
1427 >        f = new CompletableFuture<Integer>();
1428 >        g = new CompletableFuture<Integer>();
1429 >        f.complete(3);
1430 >        assertTrue(g.cancel(true));
1431 >        h = f.thenCombineAsync(g, subtract);
1432 >        checkCompletedWithWrappedCancellationException(h);
1433      }
1434  
1435      /**
# Line 1669 | Line 1799 | public class CompletableFutureTest exten
1799       * completion of source
1800       */
1801      public void testThenComposeAsync() {
1802 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1803 <        CompletableFutureInc r = new CompletableFutureInc();
1804 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1802 >        CompletableFuture<Integer> f, g;
1803 >        CompletableFutureInc r;
1804 >
1805 >        f = new CompletableFuture<Integer>();
1806 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
1807 >        f.complete(one);
1808 >        checkCompletedNormally(g, two);
1809 >
1810 >        f = new CompletableFuture<Integer>();
1811          f.complete(one);
1812 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
1813          checkCompletedNormally(g, two);
1814      }
1815  
# Line 1681 | Line 1818 | public class CompletableFutureTest exten
1818       * exceptional completion of source
1819       */
1820      public void testThenComposeAsync2() {
1821 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1822 <        CompletableFutureInc r = new CompletableFutureInc();
1823 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1821 >        CompletableFuture<Integer> f, g;
1822 >        CompletableFutureInc r;
1823 >
1824 >        f = new CompletableFuture<Integer>();
1825 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
1826          f.completeExceptionally(new CFException());
1827          checkCompletedWithWrappedCFException(g);
1828 +        assertFalse(r.ran);
1829 +
1830 +        f = new CompletableFuture<Integer>();
1831 +        f.completeExceptionally(new CFException());
1832 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
1833 +        checkCompletedWithWrappedCFException(g);
1834 +        assertFalse(r.ran);
1835      }
1836  
1837      /**
1838       * thenComposeAsync result completes exceptionally if action does
1839       */
1840      public void testThenComposeAsync3() {
1841 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1842 <        FailingCompletableFutureFunction r = new FailingCompletableFutureFunction();
1843 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1841 >        CompletableFuture<Integer> f, g;
1842 >        FailingCompletableFutureFunction r;
1843 >        
1844 >        f = new CompletableFuture<Integer>();
1845 >        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
1846 >        f.complete(one);
1847 >        checkCompletedWithWrappedCFException(g);
1848 >        
1849 >        f = new CompletableFuture<Integer>();
1850          f.complete(one);
1851 +        g = f.thenComposeAsync(r = new FailingCompletableFutureFunction());
1852          checkCompletedWithWrappedCFException(g);
1853      }
1854  
# Line 1703 | Line 1856 | public class CompletableFutureTest exten
1856       * thenComposeAsync result completes exceptionally if source cancelled
1857       */
1858      public void testThenComposeAsync4() {
1859 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
1860 <        CompletableFutureInc r = new CompletableFutureInc();
1861 <        CompletableFuture<Integer> g = f.thenComposeAsync(r);
1859 >        CompletableFuture<Integer> f, g;
1860 >        CompletableFutureInc r;
1861 >
1862 >        f = new CompletableFuture<Integer>();
1863 >        g = f.thenComposeAsync(r = new CompletableFutureInc());
1864 >        assertTrue(f.cancel(true));
1865 >        checkCompletedWithWrappedCancellationException(g);
1866 >
1867 >        f = new CompletableFuture<Integer>();
1868          assertTrue(f.cancel(true));
1869 +        g = f.thenComposeAsync(r = new CompletableFutureInc());
1870          checkCompletedWithWrappedCancellationException(g);
1871      }
1872  
# Line 1858 | Line 2018 | public class CompletableFutureTest exten
2018          assertTrue(f.cancel(true));
2019          checkCompletedWithWrappedCancellationException(g);
2020      }
2021 +
2022      /**
2023       * thenCombineAsync result completes normally after normal
2024       * completion of sources
2025       */
2026      public void testThenCombineAsyncE() {
2027 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2028 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2029 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2030 <        f.complete(one);
2031 <        checkIncomplete(g);
2032 <        f2.complete(two);
2033 <        checkCompletedNormally(g, three);
2027 >        CompletableFuture<Integer> f, g, h;
2028 >        ThreadExecutor e = new ThreadExecutor();
2029 >        int count = 0;
2030 >
2031 >        f = new CompletableFuture<Integer>();
2032 >        g = new CompletableFuture<Integer>();
2033 >        h = f.thenCombineAsync(g, subtract, e);
2034 >        f.complete(3);
2035 >        checkIncomplete(h);
2036 >        g.complete(1);
2037 >        checkCompletedNormally(h, 2);
2038 >        assertEquals(++count, e.count.get());
2039 >
2040 >        f = new CompletableFuture<Integer>();
2041 >        g = new CompletableFuture<Integer>();
2042 >        h = f.thenCombineAsync(g, subtract, e);
2043 >        g.complete(1);
2044 >        checkIncomplete(h);
2045 >        f.complete(3);
2046 >        checkCompletedNormally(h, 2);
2047 >        assertEquals(++count, e.count.get());
2048 >
2049 >        f = new CompletableFuture<Integer>();
2050 >        g = new CompletableFuture<Integer>();
2051 >        g.complete(1);
2052 >        f.complete(3);
2053 >        h = f.thenCombineAsync(g, subtract, e);
2054 >        checkCompletedNormally(h, 2);
2055 >        assertEquals(++count, e.count.get());
2056      }
2057  
2058      /**
2059       * thenCombineAsync result completes exceptionally after exceptional
2060 <     * completion of source
2060 >     * completion of either source
2061       */
2062      public void testThenCombineAsync2E() {
2063 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2064 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2065 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2063 >        CompletableFuture<Integer> f, g, h;
2064 >        ThreadExecutor e = new ThreadExecutor();
2065 >        int count = 0;
2066 >
2067 >        f = new CompletableFuture<Integer>();
2068 >        g = new CompletableFuture<Integer>();
2069 >        h = f.thenCombineAsync(g, subtract, e);
2070          f.completeExceptionally(new CFException());
2071 <        f2.complete(two);
2072 <        checkCompletedWithWrappedCFException(g);
2071 >        checkIncomplete(h);
2072 >        g.complete(1);
2073 >        checkCompletedWithWrappedCFException(h);
2074  
2075          f = new CompletableFuture<Integer>();
2076 <        f2 = new CompletableFuture<Integer>();
2077 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2078 <        f.complete(one);
2079 <        f2.completeExceptionally(new CFException());
2080 <        checkCompletedWithWrappedCFException(g);
2076 >        g = new CompletableFuture<Integer>();
2077 >        h = f.thenCombineAsync(g, subtract, e);
2078 >        g.completeExceptionally(new CFException());
2079 >        checkIncomplete(h);
2080 >        f.complete(3);
2081 >        checkCompletedWithWrappedCFException(h);
2082 >
2083 >        f = new CompletableFuture<Integer>();
2084 >        g = new CompletableFuture<Integer>();
2085 >        g.completeExceptionally(new CFException());
2086 >        h = f.thenCombineAsync(g, subtract, e);
2087 >        checkIncomplete(h);
2088 >        f.complete(3);
2089 >        checkCompletedWithWrappedCFException(h);
2090 >
2091 >        assertEquals(0, e.count.get());
2092      }
2093  
2094      /**
# Line 1902 | Line 2101 | public class CompletableFutureTest exten
2101          CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor());
2102          f.complete(one);
2103          checkIncomplete(g);
2104 +        assertFalse(r.ran);
2105          f2.complete(two);
2106          checkCompletedWithWrappedCFException(g);
2107 +        assertTrue(r.ran);
2108      }
2109  
2110      /**
2111       * thenCombineAsync result completes exceptionally if either source cancelled
2112       */
2113      public void testThenCombineAsync4E() {
2114 <        CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2115 <        CompletableFuture<Integer> f2 = new CompletableFuture<Integer>();
2116 <        CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2114 >        CompletableFuture<Integer> f, g, h;
2115 >        ThreadExecutor e = new ThreadExecutor();
2116 >
2117 >        f = new CompletableFuture<Integer>();
2118 >        g = new CompletableFuture<Integer>();
2119 >        h = f.thenCombineAsync(g, subtract, e);
2120          assertTrue(f.cancel(true));
2121 <        f2.complete(two);
2122 <        checkCompletedWithWrappedCancellationException(g);
2121 >        checkIncomplete(h);
2122 >        g.complete(1);
2123 >        checkCompletedWithWrappedCancellationException(h);
2124  
2125          f = new CompletableFuture<Integer>();
2126 <        f2 = new CompletableFuture<Integer>();
2127 <        g = f.thenCombineAsync(f2, add, new ThreadExecutor());
2128 <        f.complete(one);
2129 <        assertTrue(f2.cancel(true));
2130 <        checkCompletedWithWrappedCancellationException(g);
2126 >        g = new CompletableFuture<Integer>();
2127 >        h = f.thenCombineAsync(g, subtract, e);
2128 >        assertTrue(g.cancel(true));
2129 >        checkIncomplete(h);
2130 >        f.complete(3);
2131 >        checkCompletedWithWrappedCancellationException(h);
2132 >
2133 >        f = new CompletableFuture<Integer>();
2134 >        g = new CompletableFuture<Integer>();
2135 >        assertTrue(g.cancel(true));
2136 >        h = f.thenCombineAsync(g, subtract, e);
2137 >        checkIncomplete(h);
2138 >        f.complete(3);
2139 >        checkCompletedWithWrappedCancellationException(h);
2140 >
2141 >        f = new CompletableFuture<Integer>();
2142 >        g = new CompletableFuture<Integer>();
2143 >        assertTrue(f.cancel(true));
2144 >        assertTrue(g.cancel(true));
2145 >        h = f.thenCombineAsync(g, subtract, e);
2146 >        checkCompletedWithWrappedCancellationException(h);
2147 >
2148 >        assertEquals(0, e.count.get());
2149      }
2150  
2151      /**
# Line 2370 | Line 2593 | public class CompletableFutureTest exten
2593      }
2594  
2595      /**
2596 <     * allOf returns a future completed when any components complete
2596 >     * anyOf returns a future completed when any components complete
2597       */
2598      public void testAnyOf() throws Exception {
2599          for (int k = 1; k < 20; ++k) {
# Line 2392 | Line 2615 | public class CompletableFutureTest exten
2615      public void testNPE() {
2616          CompletableFuture<Integer> f = new CompletableFuture<Integer>();
2617          CompletableFuture<Integer> g = new CompletableFuture<Integer>();
2618 <        CompletableFuture h;
2619 <        try { h = f.thenApply(null); } catch (NullPointerException ok) {}
2620 <        try { h = f.thenAccept(null); } catch (NullPointerException ok) {}
2621 <        try { h = f.thenRun(null); } catch (NullPointerException ok) {}
2622 <        try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {}
2623 <        try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {}
2624 <        try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {}
2625 <        try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {}
2626 <        try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {}
2627 <        try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {}
2628 <        try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {}
2629 <        try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {}
2630 <        try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {}
2631 <        try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {}
2632 <        try { h = f.exceptionally(null); } catch (NullPointerException ok) {}
2633 <        try { h = f.handle(null); } catch (NullPointerException ok) {}
2634 <        try { h = f.thenCompose(null); } catch (NullPointerException ok) {}
2635 <
2636 <        try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {}
2637 <        try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {}
2638 <        try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {}
2639 <        try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {}
2640 <        try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {}
2641 <        try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {}
2642 <        try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {}
2643 <        try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {}
2644 <        try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {}
2645 <        try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {}
2646 <        try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {}
2647 <        try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {}
2648 <        try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {}
2618 >        CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2619 >        CompletableFuture<?> h;
2620 >        ThreadExecutor exec = new ThreadExecutor();
2621 >
2622 >        Runnable[] throwingActions = {
2623 >            () -> { CompletableFuture.supplyAsync(null); },
2624 >            () -> { CompletableFuture.supplyAsync(null, exec); },
2625 >            () -> { CompletableFuture.supplyAsync(supplyOne, null); },
2626 >
2627 >            () -> { CompletableFuture.runAsync(null); },
2628 >            () -> { CompletableFuture.runAsync(null, exec); },
2629 >            () -> { CompletableFuture.runAsync(() -> {}, null); },
2630 >
2631 >            () -> { f.completeExceptionally(null); },
2632 >
2633 >            () -> { f.thenApply(null); },
2634 >            () -> { f.thenApplyAsync(null); },
2635 >            () -> { f.thenApplyAsync((x) -> x, null); },
2636 >            () -> { f.thenApplyAsync(null, exec); },
2637 >
2638 >            () -> { f.thenAccept(null); },
2639 >            () -> { f.thenAcceptAsync(null); },
2640 >            () -> { f.thenAcceptAsync((x) -> { ; }, null); },
2641 >            () -> { f.thenAcceptAsync(null, exec); },
2642 >
2643 >            () -> { f.thenRun(null); },
2644 >            () -> { f.thenRunAsync(null); },
2645 >            () -> { f.thenRunAsync(() -> { ; }, null); },
2646 >            () -> { f.thenRunAsync(null, exec); },
2647 >
2648 >            () -> { f.thenCombine(g, null); },
2649 >            () -> { f.thenCombineAsync(g, null); },
2650 >            () -> { f.thenCombineAsync(g, null, exec); },
2651 >            () -> { f.thenCombine(nullFuture, (x, y) -> x); },
2652 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x); },
2653 >            () -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); },
2654 >            () -> { f.thenCombineAsync(g, (x, y) -> x, null); },
2655 >
2656 >            () -> { f.thenAcceptBoth(g, null); },
2657 >            () -> { f.thenAcceptBothAsync(g, null); },
2658 >            () -> { f.thenAcceptBothAsync(g, null, exec); },
2659 >            () -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); },
2660 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); },
2661 >            () -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); },
2662 >            () -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); },
2663 >
2664 >            () -> { f.runAfterBoth(g, null); },
2665 >            () -> { f.runAfterBothAsync(g, null); },
2666 >            () -> { f.runAfterBothAsync(g, null, exec); },
2667 >            () -> { f.runAfterBoth(nullFuture, () -> {}); },
2668 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}); },
2669 >            () -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); },
2670 >            () -> { f.runAfterBothAsync(g, () -> {}, null); },
2671 >
2672 >            () -> { f.applyToEither(g, null); },
2673 >            () -> { f.applyToEitherAsync(g, null); },
2674 >            () -> { f.applyToEitherAsync(g, null, exec); },
2675 >            () -> { f.applyToEither(nullFuture, (x) -> x); },
2676 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x); },
2677 >            () -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); },
2678 >            () -> { f.applyToEitherAsync(g, (x) -> x, null); },
2679 >
2680 >            () -> { f.acceptEither(g, null); },
2681 >            () -> { f.acceptEitherAsync(g, null); },
2682 >            () -> { f.acceptEitherAsync(g, null, exec); },
2683 >            () -> { f.acceptEither(nullFuture, (x) -> {}); },
2684 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}); },
2685 >            () -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); },
2686 >            () -> { f.acceptEitherAsync(g, (x) -> {}, null); },
2687 >
2688 >            () -> { f.runAfterEither(g, null); },
2689 >            () -> { f.runAfterEitherAsync(g, null); },
2690 >            () -> { f.runAfterEitherAsync(g, null, exec); },
2691 >            () -> { f.runAfterEither(nullFuture, () -> {}); },
2692 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}); },
2693 >            () -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); },
2694 >            () -> { f.runAfterEitherAsync(g, () -> {}, null); },
2695 >
2696 >            () -> { f.thenCompose(null); },
2697 >            () -> { f.thenComposeAsync(null); },
2698 >            () -> { f.thenComposeAsync(new CompletableFutureInc(), null); },
2699 >            () -> { f.thenComposeAsync(null, exec); },
2700 >
2701 >            () -> { f.exceptionally(null); },
2702 >
2703 >            () -> { f.handle(null); },
2704 >
2705 >            () -> { CompletableFuture.allOf((CompletableFuture<?>)null); },
2706 >            () -> { CompletableFuture.allOf((CompletableFuture<?>[])null); },
2707 >            () -> { CompletableFuture.allOf(f, null); },
2708 >            () -> { CompletableFuture.allOf(null, f); },
2709 >
2710 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>)null); },
2711 >            () -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); },
2712 >            () -> { CompletableFuture.anyOf(f, null); },
2713 >            () -> { CompletableFuture.anyOf(null, f); },
2714 >        };
2715  
2716 +        assertThrows(NullPointerException.class, throwingActions);
2717 +        assertEquals(0, exec.count.get());
2718      }
2719  
2429
2720   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines