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.128 by jsr166, Sun Nov 15 00:37:50 2015 UTC vs.
Revision 1.161 by jsr166, Tue Jun 28 14:49:48 2016 UTC

# Line 30 | Line 30 | import java.util.concurrent.ExecutionExc
30   import java.util.concurrent.Executor;
31   import java.util.concurrent.ForkJoinPool;
32   import java.util.concurrent.ForkJoinTask;
33 + import java.util.concurrent.RejectedExecutionException;
34   import java.util.concurrent.TimeoutException;
35   import java.util.concurrent.TimeUnit;
36   import java.util.concurrent.atomic.AtomicInteger;
# Line 459 | Line 460 | public class CompletableFutureTest exten
460      class FailingSupplier extends CheckedAction
461          implements Supplier<Integer>
462      {
463 <        FailingSupplier(ExecutionMode m) { super(m); }
463 >        final CFException ex;
464 >        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
465          public Integer get() {
466              invoked();
467 <            throw new CFException();
467 >            throw ex;
468          }
469      }
470  
471      class FailingConsumer extends CheckedIntegerAction
472          implements Consumer<Integer>
473      {
474 <        FailingConsumer(ExecutionMode m) { super(m); }
474 >        final CFException ex;
475 >        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
476          public void accept(Integer x) {
477              invoked();
478              value = x;
479 <            throw new CFException();
479 >            throw ex;
480          }
481      }
482  
483      class FailingBiConsumer extends CheckedIntegerAction
484          implements BiConsumer<Integer, Integer>
485      {
486 <        FailingBiConsumer(ExecutionMode m) { super(m); }
486 >        final CFException ex;
487 >        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
488          public void accept(Integer x, Integer y) {
489              invoked();
490              value = subtract(x, y);
491 <            throw new CFException();
491 >            throw ex;
492          }
493      }
494  
495      class FailingFunction extends CheckedIntegerAction
496          implements Function<Integer, Integer>
497      {
498 <        FailingFunction(ExecutionMode m) { super(m); }
498 >        final CFException ex;
499 >        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
500          public Integer apply(Integer x) {
501              invoked();
502              value = x;
503 <            throw new CFException();
503 >            throw ex;
504          }
505      }
506  
507      class FailingBiFunction extends CheckedIntegerAction
508          implements BiFunction<Integer, Integer, Integer>
509      {
510 <        FailingBiFunction(ExecutionMode m) { super(m); }
510 >        final CFException ex;
511 >        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
512          public Integer apply(Integer x, Integer y) {
513              invoked();
514              value = subtract(x, y);
515 <            throw new CFException();
515 >            throw ex;
516          }
517      }
518  
519      class FailingRunnable extends CheckedAction implements Runnable {
520 <        FailingRunnable(ExecutionMode m) { super(m); }
520 >        final CFException ex;
521 >        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
522          public void run() {
523              invoked();
524 <            throw new CFException();
524 >            throw ex;
525          }
526      }
527  
# Line 534 | Line 541 | public class CompletableFutureTest exten
541      class FailingCompletableFutureFunction extends CheckedIntegerAction
542          implements Function<Integer, CompletableFuture<Integer>>
543      {
544 <        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
544 >        final CFException ex;
545 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
546          public CompletableFuture<Integer> apply(Integer x) {
547              invoked();
548              value = x;
549 <            throw new CFException();
549 >            throw ex;
550 >        }
551 >    }
552 >
553 >    static class CountingRejectingExecutor implements Executor {
554 >        final RejectedExecutionException ex = new RejectedExecutionException();
555 >        final AtomicInteger count = new AtomicInteger(0);
556 >        public void execute(Runnable r) {
557 >            count.getAndIncrement();
558 >            throw ex;
559          }
560      }
561  
# Line 876 | Line 893 | public class CompletableFutureTest exten
893          assertEquals(1, a.get());
894      }}
895  
896 +    /**
897 +     * If an "exceptionally action" throws an exception, it completes
898 +     * exceptionally with that exception
899 +     */
900      public void testExceptionally_exceptionalCompletionActionFailed() {
901          for (boolean createIncomplete : new boolean[] { true, false })
902      {
# Line 894 | Line 915 | public class CompletableFutureTest exten
915          if (createIncomplete) f.completeExceptionally(ex1);
916  
917          checkCompletedWithWrappedException(g, ex2);
918 +        checkCompletedExceptionally(f, ex1);
919          assertEquals(1, a.get());
920      }}
921  
# Line 911 | Line 933 | public class CompletableFutureTest exten
933          if (!createIncomplete) assertTrue(f.complete(v1));
934          final CompletableFuture<Integer> g = m.whenComplete
935              (f,
936 <             (Integer x, Throwable t) -> {
936 >             (Integer result, Throwable t) -> {
937                  m.checkExecutionMode();
938 <                threadAssertSame(x, v1);
938 >                threadAssertSame(result, v1);
939                  threadAssertNull(t);
940                  a.getAndIncrement();
941              });
# Line 938 | Line 960 | public class CompletableFutureTest exten
960          if (!createIncomplete) f.completeExceptionally(ex);
961          final CompletableFuture<Integer> g = m.whenComplete
962              (f,
963 <             (Integer x, Throwable t) -> {
963 >             (Integer result, Throwable t) -> {
964                  m.checkExecutionMode();
965 <                threadAssertNull(x);
965 >                threadAssertNull(result);
966                  threadAssertSame(t, ex);
967                  a.getAndIncrement();
968              });
# Line 965 | Line 987 | public class CompletableFutureTest exten
987          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
988          final CompletableFuture<Integer> g = m.whenComplete
989              (f,
990 <             (Integer x, Throwable t) -> {
990 >             (Integer result, Throwable t) -> {
991                  m.checkExecutionMode();
992 <                threadAssertNull(x);
992 >                threadAssertNull(result);
993                  threadAssertTrue(t instanceof CancellationException);
994                  a.getAndIncrement();
995              });
# Line 982 | Line 1004 | public class CompletableFutureTest exten
1004       * If a whenComplete action throws an exception when triggered by
1005       * a normal completion, it completes exceptionally
1006       */
1007 <    public void testWhenComplete_actionFailed() {
1007 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
1008          for (boolean createIncomplete : new boolean[] { true, false })
1009          for (ExecutionMode m : ExecutionMode.values())
1010          for (Integer v1 : new Integer[] { 1, null })
# Line 993 | Line 1015 | public class CompletableFutureTest exten
1015          if (!createIncomplete) assertTrue(f.complete(v1));
1016          final CompletableFuture<Integer> g = m.whenComplete
1017              (f,
1018 <             (Integer x, Throwable t) -> {
1018 >             (Integer result, Throwable t) -> {
1019                  m.checkExecutionMode();
1020 <                threadAssertSame(x, v1);
1020 >                threadAssertSame(result, v1);
1021                  threadAssertNull(t);
1022                  a.getAndIncrement();
1023                  throw ex;
# Line 1010 | Line 1032 | public class CompletableFutureTest exten
1032      /**
1033       * If a whenComplete action throws an exception when triggered by
1034       * a source completion that also throws an exception, the source
1035 <     * exception takes precedence.
1035 >     * exception takes precedence (unlike handle)
1036       */
1037 <    public void testWhenComplete_actionFailedSourceFailed() {
1037 >    public void testWhenComplete_sourceFailedActionFailed() {
1038          for (boolean createIncomplete : new boolean[] { true, false })
1039          for (ExecutionMode m : ExecutionMode.values())
1040      {
# Line 1024 | Line 1046 | public class CompletableFutureTest exten
1046          if (!createIncomplete) f.completeExceptionally(ex1);
1047          final CompletableFuture<Integer> g = m.whenComplete
1048              (f,
1049 <             (Integer x, Throwable t) -> {
1049 >             (Integer result, Throwable t) -> {
1050                  m.checkExecutionMode();
1051                  threadAssertSame(t, ex1);
1052 <                threadAssertNull(x);
1052 >                threadAssertNull(result);
1053                  a.getAndIncrement();
1054                  throw ex2;
1055              });
# Line 1035 | Line 1057 | public class CompletableFutureTest exten
1057  
1058          checkCompletedWithWrappedException(g, ex1);
1059          checkCompletedExceptionally(f, ex1);
1060 +        if (testImplementationDetails) {
1061 +            assertEquals(1, ex1.getSuppressed().length);
1062 +            assertSame(ex2, ex1.getSuppressed()[0]);
1063 +        }
1064          assertEquals(1, a.get());
1065      }}
1066  
# Line 1052 | Line 1078 | public class CompletableFutureTest exten
1078          if (!createIncomplete) assertTrue(f.complete(v1));
1079          final CompletableFuture<Integer> g = m.handle
1080              (f,
1081 <             (Integer x, Throwable t) -> {
1081 >             (Integer result, Throwable t) -> {
1082                  m.checkExecutionMode();
1083 <                threadAssertSame(x, v1);
1083 >                threadAssertSame(result, v1);
1084                  threadAssertNull(t);
1085                  a.getAndIncrement();
1086                  return inc(v1);
# Line 1081 | Line 1107 | public class CompletableFutureTest exten
1107          if (!createIncomplete) f.completeExceptionally(ex);
1108          final CompletableFuture<Integer> g = m.handle
1109              (f,
1110 <             (Integer x, Throwable t) -> {
1110 >             (Integer result, Throwable t) -> {
1111                  m.checkExecutionMode();
1112 <                threadAssertNull(x);
1112 >                threadAssertNull(result);
1113                  threadAssertSame(t, ex);
1114                  a.getAndIncrement();
1115                  return v1;
# Line 1110 | Line 1136 | public class CompletableFutureTest exten
1136          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1137          final CompletableFuture<Integer> g = m.handle
1138              (f,
1139 <             (Integer x, Throwable t) -> {
1139 >             (Integer result, Throwable t) -> {
1140                  m.checkExecutionMode();
1141 <                threadAssertNull(x);
1141 >                threadAssertNull(result);
1142                  threadAssertTrue(t instanceof CancellationException);
1143                  a.getAndIncrement();
1144                  return v1;
# Line 1125 | Line 1151 | public class CompletableFutureTest exten
1151      }}
1152  
1153      /**
1154 <     * handle result completes exceptionally if action does
1154 >     * If a "handle action" throws an exception when triggered by
1155 >     * a normal completion, it completes exceptionally
1156       */
1157 <    public void testHandle_sourceFailedActionFailed() {
1157 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1158          for (ExecutionMode m : ExecutionMode.values())
1159          for (boolean createIncomplete : new boolean[] { true, false })
1160 +        for (Integer v1 : new Integer[] { 1, null })
1161      {
1162          final CompletableFuture<Integer> f = new CompletableFuture<>();
1163          final AtomicInteger a = new AtomicInteger(0);
1164 <        final CFException ex1 = new CFException();
1165 <        final CFException ex2 = new CFException();
1138 <        if (!createIncomplete) f.completeExceptionally(ex1);
1164 >        final CFException ex = new CFException();
1165 >        if (!createIncomplete) assertTrue(f.complete(v1));
1166          final CompletableFuture<Integer> g = m.handle
1167              (f,
1168 <             (Integer x, Throwable t) -> {
1168 >             (Integer result, Throwable t) -> {
1169                  m.checkExecutionMode();
1170 <                threadAssertNull(x);
1171 <                threadAssertSame(ex1, t);
1170 >                threadAssertSame(result, v1);
1171 >                threadAssertNull(t);
1172                  a.getAndIncrement();
1173 <                throw ex2;
1173 >                throw ex;
1174              });
1175 <        if (createIncomplete) f.completeExceptionally(ex1);
1175 >        if (createIncomplete) assertTrue(f.complete(v1));
1176  
1177 <        checkCompletedWithWrappedException(g, ex2);
1178 <        checkCompletedExceptionally(f, ex1);
1177 >        checkCompletedWithWrappedException(g, ex);
1178 >        checkCompletedNormally(f, v1);
1179          assertEquals(1, a.get());
1180      }}
1181  
1182 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1183 <        for (ExecutionMode m : ExecutionMode.values())
1182 >    /**
1183 >     * If a "handle action" throws an exception when triggered by
1184 >     * a source completion that also throws an exception, the action
1185 >     * exception takes precedence (unlike whenComplete)
1186 >     */
1187 >    public void testHandle_sourceFailedActionFailed() {
1188          for (boolean createIncomplete : new boolean[] { true, false })
1189 <        for (Integer v1 : new Integer[] { 1, null })
1189 >        for (ExecutionMode m : ExecutionMode.values())
1190      {
1160        final CompletableFuture<Integer> f = new CompletableFuture<>();
1191          final AtomicInteger a = new AtomicInteger(0);
1192 <        final CFException ex = new CFException();
1193 <        if (!createIncomplete) assertTrue(f.complete(v1));
1192 >        final CFException ex1 = new CFException();
1193 >        final CFException ex2 = new CFException();
1194 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1195 >
1196 >        if (!createIncomplete) f.completeExceptionally(ex1);
1197          final CompletableFuture<Integer> g = m.handle
1198              (f,
1199 <             (Integer x, Throwable t) -> {
1199 >             (Integer result, Throwable t) -> {
1200                  m.checkExecutionMode();
1201 <                threadAssertSame(x, v1);
1202 <                threadAssertNull(t);
1201 >                threadAssertNull(result);
1202 >                threadAssertSame(ex1, t);
1203                  a.getAndIncrement();
1204 <                throw ex;
1204 >                throw ex2;
1205              });
1206 <        if (createIncomplete) assertTrue(f.complete(v1));
1206 >        if (createIncomplete) f.completeExceptionally(ex1);
1207  
1208 <        checkCompletedWithWrappedException(g, ex);
1209 <        checkCompletedNormally(f, v1);
1208 >        checkCompletedWithWrappedException(g, ex2);
1209 >        checkCompletedExceptionally(f, ex1);
1210          assertEquals(1, a.get());
1211      }}
1212  
# Line 1206 | Line 1239 | public class CompletableFutureTest exten
1239      {
1240          final FailingRunnable r = new FailingRunnable(m);
1241          final CompletableFuture<Void> f = m.runAsync(r);
1242 <        checkCompletedWithWrappedCFException(f);
1242 >        checkCompletedWithWrappedException(f, r.ex);
1243          r.assertInvoked();
1244      }}
1245  
1246 +    public void testRunAsync_rejectingExecutor() {
1247 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1248 +        try {
1249 +            CompletableFuture.runAsync(() -> {}, e);
1250 +            shouldThrow();
1251 +        } catch (Throwable t) {
1252 +            assertSame(e.ex, t);
1253 +        }
1254 +
1255 +        assertEquals(1, e.count.get());
1256 +    }
1257 +
1258      /**
1259       * supplyAsync completes with result of supplier
1260       */
# Line 1240 | Line 1285 | public class CompletableFutureTest exten
1285      {
1286          FailingSupplier r = new FailingSupplier(m);
1287          CompletableFuture<Integer> f = m.supplyAsync(r);
1288 <        checkCompletedWithWrappedCFException(f);
1288 >        checkCompletedWithWrappedException(f, r.ex);
1289          r.assertInvoked();
1290      }}
1291  
1292 +    public void testSupplyAsync_rejectingExecutor() {
1293 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1294 +        try {
1295 +            CompletableFuture.supplyAsync(() -> null, e);
1296 +            shouldThrow();
1297 +        } catch (Throwable t) {
1298 +            assertSame(e.ex, t);
1299 +        }
1300 +
1301 +        assertEquals(1, e.count.get());
1302 +    }
1303 +
1304      // seq completion methods
1305  
1306      /**
# Line 1362 | Line 1419 | public class CompletableFutureTest exten
1419          final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1420          final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1421  
1422 <        checkCompletedWithWrappedCFException(h0);
1423 <        checkCompletedWithWrappedCFException(h1);
1424 <        checkCompletedWithWrappedCFException(h2);
1425 <        checkCompletedWithWrappedCFException(h3);
1426 <        checkCompletedWithWrappedCFException(h4);
1427 <        checkCompletedWithWrappedCFException(h5);
1422 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1423 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1424 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1425 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1426 >        checkCompletedWithWrappedException(h4, rs[4].ex);
1427 >        checkCompletedWithWrappedException(h5, rs[5].ex);
1428          checkCompletedNormally(f, v1);
1429      }}
1430  
# Line 1466 | Line 1523 | public class CompletableFutureTest exten
1523          final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1524          final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1525  
1526 <        checkCompletedWithWrappedCFException(h0);
1527 <        checkCompletedWithWrappedCFException(h1);
1528 <        checkCompletedWithWrappedCFException(h2);
1529 <        checkCompletedWithWrappedCFException(h3);
1526 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1527 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1528 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1529 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1530          checkCompletedNormally(f, v1);
1531      }}
1532  
# Line 1568 | Line 1625 | public class CompletableFutureTest exten
1625          final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1626          final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1627  
1628 <        checkCompletedWithWrappedCFException(h0);
1629 <        checkCompletedWithWrappedCFException(h1);
1630 <        checkCompletedWithWrappedCFException(h2);
1631 <        checkCompletedWithWrappedCFException(h3);
1628 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1629 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1630 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1631 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1632          checkCompletedNormally(f, v1);
1633      }}
1634  
# Line 1733 | Line 1790 | public class CompletableFutureTest exten
1790          assertTrue(snd.complete(w2));
1791          final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1792  
1793 <        checkCompletedWithWrappedCFException(h1);
1794 <        checkCompletedWithWrappedCFException(h2);
1795 <        checkCompletedWithWrappedCFException(h3);
1793 >        checkCompletedWithWrappedException(h1, r1.ex);
1794 >        checkCompletedWithWrappedException(h2, r2.ex);
1795 >        checkCompletedWithWrappedException(h3, r3.ex);
1796          r1.assertInvoked();
1797          r2.assertInvoked();
1798          r3.assertInvoked();
# Line 1897 | Line 1954 | public class CompletableFutureTest exten
1954          assertTrue(snd.complete(w2));
1955          final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1956  
1957 <        checkCompletedWithWrappedCFException(h1);
1958 <        checkCompletedWithWrappedCFException(h2);
1959 <        checkCompletedWithWrappedCFException(h3);
1957 >        checkCompletedWithWrappedException(h1, r1.ex);
1958 >        checkCompletedWithWrappedException(h2, r2.ex);
1959 >        checkCompletedWithWrappedException(h3, r3.ex);
1960          r1.assertInvoked();
1961          r2.assertInvoked();
1962          r3.assertInvoked();
# Line 2061 | Line 2118 | public class CompletableFutureTest exten
2118          assertTrue(snd.complete(w2));
2119          final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2120  
2121 <        checkCompletedWithWrappedCFException(h1);
2122 <        checkCompletedWithWrappedCFException(h2);
2123 <        checkCompletedWithWrappedCFException(h3);
2121 >        checkCompletedWithWrappedException(h1, r1.ex);
2122 >        checkCompletedWithWrappedException(h2, r2.ex);
2123 >        checkCompletedWithWrappedException(h3, r3.ex);
2124          r1.assertInvoked();
2125          r2.assertInvoked();
2126          r3.assertInvoked();
# Line 2353 | Line 2410 | public class CompletableFutureTest exten
2410          f.complete(v1);
2411          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2412          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2413 <        checkCompletedWithWrappedCFException(h0);
2414 <        checkCompletedWithWrappedCFException(h1);
2415 <        checkCompletedWithWrappedCFException(h2);
2416 <        checkCompletedWithWrappedCFException(h3);
2413 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2414 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2415 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2416 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2417          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2418  
2419          g.complete(v2);
# Line 2365 | Line 2422 | public class CompletableFutureTest exten
2422          final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2423          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2424  
2425 <        checkCompletedWithWrappedCFException(h4);
2425 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2426          assertTrue(Objects.equals(v1, rs[4].value) ||
2427                     Objects.equals(v2, rs[4].value));
2428 <        checkCompletedWithWrappedCFException(h5);
2428 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2429          assertTrue(Objects.equals(v1, rs[5].value) ||
2430                     Objects.equals(v2, rs[5].value));
2431  
# Line 2612 | Line 2669 | public class CompletableFutureTest exten
2669          f.complete(v1);
2670          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2671          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2672 <        checkCompletedWithWrappedCFException(h0);
2673 <        checkCompletedWithWrappedCFException(h1);
2674 <        checkCompletedWithWrappedCFException(h2);
2675 <        checkCompletedWithWrappedCFException(h3);
2672 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2673 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2674 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2675 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2676          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2677  
2678          g.complete(v2);
# Line 2624 | Line 2681 | public class CompletableFutureTest exten
2681          final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2682          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2683  
2684 <        checkCompletedWithWrappedCFException(h4);
2684 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2685          assertTrue(Objects.equals(v1, rs[4].value) ||
2686                     Objects.equals(v2, rs[4].value));
2687 <        checkCompletedWithWrappedCFException(h5);
2687 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2688          assertTrue(Objects.equals(v1, rs[5].value) ||
2689                     Objects.equals(v2, rs[5].value));
2690  
# Line 2867 | Line 2924 | public class CompletableFutureTest exten
2924          assertTrue(f.complete(v1));
2925          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2926          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2927 <        checkCompletedWithWrappedCFException(h0);
2928 <        checkCompletedWithWrappedCFException(h1);
2929 <        checkCompletedWithWrappedCFException(h2);
2930 <        checkCompletedWithWrappedCFException(h3);
2927 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2928 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2929 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2930 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2931          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2932          assertTrue(g.complete(v2));
2933          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2934          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2935 <        checkCompletedWithWrappedCFException(h4);
2936 <        checkCompletedWithWrappedCFException(h5);
2935 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2936 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2937  
2938          checkCompletedNormally(f, v1);
2939          checkCompletedNormally(g, v2);
# Line 2937 | Line 2994 | public class CompletableFutureTest exten
2994          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2995          if (createIncomplete) assertTrue(f.complete(v1));
2996  
2997 <        checkCompletedWithWrappedCFException(g);
2997 >        checkCompletedWithWrappedException(g, r.ex);
2998          checkCompletedNormally(f, v1);
2999      }}
3000  
# Line 3046 | Line 3103 | public class CompletableFutureTest exten
3103          }
3104      }
3105  
3106 <    public void testAllOf_backwards() throws Exception {
3106 >    public void testAllOf_normal_backwards() throws Exception {
3107          for (int k = 1; k < 10; k++) {
3108              CompletableFuture<Integer>[] fs
3109                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
# Line 3279 | Line 3336 | public class CompletableFutureTest exten
3336              () -> f.obtrudeException(null),
3337  
3338              () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3339 <            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3339 >            () -> CompletableFuture.delayedExecutor(1L, null, exec),
3340              () -> CompletableFuture.delayedExecutor(1L, null),
3341  
3342              () -> f.orTimeout(1L, null),
# Line 3294 | Line 3351 | public class CompletableFutureTest exten
3351      }
3352  
3353      /**
3354 +     * Test submissions to an executor that rejects all tasks.
3355 +     */
3356 +    public void testRejectingExecutor() {
3357 +        for (Integer v : new Integer[] { 1, null })
3358 +    {
3359 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3360 +
3361 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3362 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3363 +
3364 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3365 +
3366 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3367 +        srcs.add(complete);
3368 +        srcs.add(incomplete);
3369 +
3370 +        for (CompletableFuture<Integer> src : srcs) {
3371 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3372 +            fs.add(src.thenRunAsync(() -> {}, e));
3373 +            fs.add(src.thenAcceptAsync((z) -> {}, e));
3374 +            fs.add(src.thenApplyAsync((z) -> z, e));
3375 +
3376 +            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3377 +            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3378 +            fs.add(src.runAfterBothAsync(src, () -> {}, e));
3379 +
3380 +            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3381 +            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3382 +            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3383 +
3384 +            fs.add(src.thenComposeAsync((z) -> null, e));
3385 +            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3386 +            fs.add(src.handleAsync((z, t) -> null, e));
3387 +
3388 +            for (CompletableFuture<?> future : fs) {
3389 +                if (src.isDone())
3390 +                    checkCompletedWithWrappedException(future, e.ex);
3391 +                else
3392 +                    checkIncomplete(future);
3393 +            }
3394 +            futures.addAll(fs);
3395 +        }
3396 +
3397 +        {
3398 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3399 +
3400 +            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3401 +            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3402 +
3403 +            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3404 +            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3405 +
3406 +            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3407 +            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3408 +
3409 +            for (CompletableFuture<?> future : fs)
3410 +                checkIncomplete(future);
3411 +            futures.addAll(fs);
3412 +        }
3413 +
3414 +        {
3415 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3416 +
3417 +            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3418 +            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3419 +
3420 +            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3421 +            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3422 +
3423 +            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3424 +            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3425 +
3426 +            for (CompletableFuture<?> future : fs)
3427 +                checkCompletedWithWrappedException(future, e.ex);
3428 +            futures.addAll(fs);
3429 +        }
3430 +
3431 +        incomplete.complete(v);
3432 +
3433 +        for (CompletableFuture<?> future : futures)
3434 +            checkCompletedWithWrappedException(future, e.ex);
3435 +
3436 +        assertEquals(futures.size(), e.count.get());
3437 +    }}
3438 +
3439 +    /**
3440 +     * Test submissions to an executor that rejects all tasks, but
3441 +     * should never be invoked because the dependent future is
3442 +     * explicitly completed.
3443 +     */
3444 +    public void testRejectingExecutorNeverInvoked() {
3445 +        for (Integer v : new Integer[] { 1, null })
3446 +    {
3447 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3448 +
3449 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3450 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3451 +
3452 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3453 +
3454 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3455 +        srcs.add(complete);
3456 +        srcs.add(incomplete);
3457 +
3458 +        List<CompletableFuture<?>> fs = new ArrayList<>();
3459 +        fs.add(incomplete.thenRunAsync(() -> {}, e));
3460 +        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3461 +        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3462 +
3463 +        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3464 +        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3465 +        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3466 +
3467 +        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3468 +        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3469 +        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3470 +
3471 +        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3472 +        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3473 +        fs.add(incomplete.handleAsync((z, t) -> null, e));
3474 +
3475 +        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3476 +        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3477 +
3478 +        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3479 +        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3480 +
3481 +        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3482 +        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3483 +
3484 +        for (CompletableFuture<?> future : fs)
3485 +            checkIncomplete(future);
3486 +
3487 +        for (CompletableFuture<?> future : fs)
3488 +            future.complete(null);
3489 +
3490 +        incomplete.complete(v);
3491 +
3492 +        for (CompletableFuture<?> future : fs)
3493 +            checkCompletedNormally(future, null);
3494 +
3495 +        assertEquals(0, e.count.get());
3496 +    }}
3497 +
3498 +    /**
3499       * toCompletableFuture returns this CompletableFuture.
3500       */
3501      public void testToCompletableFuture() {
# Line 3509 | Line 3711 | public class CompletableFutureTest exten
3711          long timeoutMillis = timeoutMillis();
3712          CompletableFuture<Integer> f = new CompletableFuture<>();
3713          long startTime = System.nanoTime();
3714 <        f.orTimeout(timeoutMillis, MILLISECONDS);
3714 >        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
3715          checkCompletedWithTimeoutException(f);
3716          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3717      }
# Line 3524 | Line 3726 | public class CompletableFutureTest exten
3726          CompletableFuture<Integer> g = new CompletableFuture<>();
3727          long startTime = System.nanoTime();
3728          f.complete(v1);
3729 <        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3730 <        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3729 >        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3730 >        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3731          g.complete(v1);
3732          checkCompletedNormally(f, v1);
3733          checkCompletedNormally(g, v1);
# Line 3540 | Line 3742 | public class CompletableFutureTest exten
3742                         () -> testCompleteOnTimeout_timesOut(null));
3743      }
3744  
3745 +    /**
3746 +     * completeOnTimeout completes with given value if not complete
3747 +     */
3748      public void testCompleteOnTimeout_timesOut(Integer v) {
3749          long timeoutMillis = timeoutMillis();
3750          CompletableFuture<Integer> f = new CompletableFuture<>();
3751          long startTime = System.nanoTime();
3752 <        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3752 >        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
3753          assertSame(v, f.join());
3754          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3755          f.complete(99);         // should have no effect
# Line 3561 | Line 3766 | public class CompletableFutureTest exten
3766          CompletableFuture<Integer> g = new CompletableFuture<>();
3767          long startTime = System.nanoTime();
3768          f.complete(v1);
3769 <        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3770 <        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3769 >        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3770 >        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3771          g.complete(v1);
3772          checkCompletedNormally(f, v1);
3773          checkCompletedNormally(g, v1);
# Line 3613 | Line 3818 | public class CompletableFutureTest exten
3818      //--- tests of implementation details; not part of official tck ---
3819  
3820      Object resultOf(CompletableFuture<?> f) {
3821 +        SecurityManager sm = System.getSecurityManager();
3822 +        if (sm != null) {
3823 +            try {
3824 +                System.setSecurityManager(null);
3825 +            } catch (SecurityException giveUp) {
3826 +                return "Reflection not available";
3827 +            }
3828 +        }
3829 +
3830          try {
3831              java.lang.reflect.Field resultField
3832                  = CompletableFuture.class.getDeclaredField("result");
3833              resultField.setAccessible(true);
3834              return resultField.get(f);
3835 <        } catch (Throwable t) { throw new AssertionError(t); }
3835 >        } catch (Throwable t) {
3836 >            throw new AssertionError(t);
3837 >        } finally {
3838 >            if (sm != null) System.setSecurityManager(sm);
3839 >        }
3840      }
3841  
3842      public void testExceptionPropagationReusesResultObject() {
# Line 3641 | Line 3859 | public class CompletableFutureTest exten
3859          funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3860  
3861          funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3862 +        funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3863          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3864 +        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3865          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3866 +        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3867  
3868 <        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3868 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3869  
3870          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3871  
3872 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y}));
3873          funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3874 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3875 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y}));
3876          funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3877 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3878  
3879          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3880                   fun : funs) {
# Line 3764 | Line 3989 | public class CompletableFutureTest exten
3989      }
3990  
3991      static class Monad {
3992 <        static class MonadError extends Error {
3993 <            public MonadError() { super("monadic zero"); }
3992 >        static class ZeroException extends RuntimeException {
3993 >            public ZeroException() { super("monadic zero"); }
3994          }
3995          // "return", "unit"
3996          static <T> CompletableFuture<T> unit(T value) {
3997              return completedFuture(value);
3998          }
3999          // monadic zero ?
4000 <        static <T> CompletableFuture<T> zero(T value) {
4001 <            return failedFuture(new MonadError());
4000 >        static <T> CompletableFuture<T> zero() {
4001 >            return failedFuture(new ZeroException());
4002          }
4003          // >=>
4004          static <T,U,V> Function<T, CompletableFuture<V>> compose
# Line 3787 | Line 4012 | public class CompletableFutureTest exten
4012                  f.getNow(null);
4013                  throw new AssertionFailedError("should throw");
4014              } catch (CompletionException success) {
4015 <                assertTrue(success.getCause() instanceof MonadError);
4015 >                assertTrue(success.getCause() instanceof ZeroException);
4016              }
4017          }
4018  
# Line 3814 | Line 4039 | public class CompletableFutureTest exten
4039              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
4040          }
4041  
4042 <        // Monadic "plus"
4042 >        /** Implements "monadic plus". */
4043          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
4044                                               CompletableFuture<? extends T> g) {
4045              PlusFuture<T> plus = new PlusFuture<T>();
4046 <            BiConsumer<T, Throwable> action = (T result, Throwable fail) -> {
4047 <                if (result != null) {
4048 <                    if (plus.complete(result))
4049 <                        if (plus.firstFailure.get() != null)
4046 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
4047 >                try {
4048 >                    if (ex == null) {
4049 >                        if (plus.complete(result))
4050 >                            if (plus.firstFailure.get() != null)
4051 >                                plus.firstFailure.set(null);
4052 >                    }
4053 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
4054 >                        if (plus.isDone())
4055                              plus.firstFailure.set(null);
4056 <                }
4057 <                else if (plus.firstFailure.compareAndSet(null, fail)) {
4058 <                    if (plus.isDone())
4059 <                        plus.firstFailure.set(null);
4060 <                } else {
4061 <                    if (plus.completeExceptionally(fail))
4062 <                        plus.firstFailure.set(null);
4056 >                    }
4057 >                    else {
4058 >                        // first failure has precedence
4059 >                        Throwable first = plus.firstFailure.getAndSet(null);
4060 >
4061 >                        // may fail with "Self-suppression not permitted"
4062 >                        try { first.addSuppressed(ex); }
4063 >                        catch (Exception ignored) {}
4064 >
4065 >                        plus.completeExceptionally(first);
4066 >                    }
4067 >                } catch (Throwable unexpected) {
4068 >                    plus.completeExceptionally(unexpected);
4069                  }
4070              };
4071              f.whenComplete(action);
# Line 3843 | Line 4079 | public class CompletableFutureTest exten
4079       * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
4080       */
4081      public void testAdditiveMonad() throws Throwable {
4082 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
4083 +        CompletableFuture<Long> zero = Monad.zero();
4084 +
4085          // Some mutually non-commutative functions
4086          Function<Long, CompletableFuture<Long>> triple
4087 <            = (x) -> completedFuture(3 * x);
4087 >            = (x) -> Monad.unit(3 * x);
4088          Function<Long, CompletableFuture<Long>> inc
4089 <            = (x) -> completedFuture(x + 1);
3851 <
3852 <        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3853 <        Function<Long, CompletableFuture<Long>> zero = Monad::zero;
4089 >            = (x) -> Monad.unit(x + 1);
4090  
4091          // unit is a right identity: m >>= unit === m
4092 <        Monad.assertFutureEquals(
4093 <            inc.apply(5L).thenCompose(unit),
3858 <            inc.apply(5L));
4092 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
4093 >                                 inc.apply(5L));
4094          // unit is a left identity: (unit x) >>= f === f x
4095 <        Monad.assertFutureEquals(
4096 <            unit.apply(5L).thenCompose(inc),
4097 <            inc.apply(5L));
4095 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
4096 >                                 inc.apply(5L));
4097 >
4098          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4099          Monad.assertFutureEquals(
4100              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4101              unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4102  
4103 +        // The case for CompletableFuture as an additive monad is weaker...
4104 +
4105          // zero is a monadic zero
4106 <        Monad.assertZero(zero.apply(5L));
3870 <        // left zero: zero >>= f === zero
3871 <        Monad.assertZero(zero.apply(5L).thenCompose(inc));
3872 <        // right zero: f >>= zero === zero
3873 <        Monad.assertZero(inc.apply(5L).thenCompose(zero));
4106 >        Monad.assertZero(zero);
4107  
4108 <        // inc plus zero === inc
4109 <        Monad.assertFutureEquals(
4110 <            inc.apply(5L),
4111 <            Monad.plus(inc.apply(5L), zero.apply(5L)));
4112 <        // zero plus inc === inc
4113 <        Monad.assertFutureEquals(
4114 <            inc.apply(5L),
4115 <            Monad.plus(zero.apply(5L), inc.apply(5L)));
4108 >        // left zero: zero >>= f === zero
4109 >        Monad.assertZero(zero.thenCompose(inc));
4110 >        // right zero: f >>= (\x -> zero) === zero
4111 >        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4112 >
4113 >        // f plus zero === f
4114 >        Monad.assertFutureEquals(Monad.unit(5L),
4115 >                                 Monad.plus(Monad.unit(5L), zero));
4116 >        // zero plus f === f
4117 >        Monad.assertFutureEquals(Monad.unit(5L),
4118 >                                 Monad.plus(zero, Monad.unit(5L)));
4119          // zero plus zero === zero
4120 <        Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L)));
4120 >        Monad.assertZero(Monad.plus(zero, zero));
4121          {
4122 <            CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L));
4123 <            assertTrue(f.get() == 6L || f.get() == 9L);
4122 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
4123 >                                                   Monad.unit(8L));
4124 >            // non-determinism
4125 >            assertTrue(f.get() == 5L || f.get() == 8L);
4126          }
4127  
4128          CompletableFuture<Long> godot = new CompletableFuture<>();
4129 <        // inc plus godot === inc (doesn't wait for godot)
4130 <        Monad.assertFutureEquals(
4131 <            inc.apply(5L),
4132 <            Monad.plus(inc.apply(5L), godot));
4133 <        // godot plus inc === inc (doesn't wait for godot)
4134 <        Monad.assertFutureEquals(
4135 <            inc.apply(5L),
4136 <            Monad.plus(godot, inc.apply(5L)));
4129 >        // f plus godot === f (doesn't wait for godot)
4130 >        Monad.assertFutureEquals(Monad.unit(5L),
4131 >                                 Monad.plus(Monad.unit(5L), godot));
4132 >        // godot plus f === f (doesn't wait for godot)
4133 >        Monad.assertFutureEquals(Monad.unit(5L),
4134 >                                 Monad.plus(godot, Monad.unit(5L)));
4135 >    }
4136 >
4137 >    /**
4138 >     * A single CompletableFuture with many dependents.
4139 >     * A demo of scalability - runtime is O(n).
4140 >     */
4141 >    public void testManyDependents() throws Throwable {
4142 >        final int n = expensiveTests ? 1_000_000 : 10;
4143 >        final CompletableFuture<Void> head = new CompletableFuture<>();
4144 >        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4145 >        final AtomicInteger count = new AtomicInteger(0);
4146 >        for (int i = 0; i < n; i++) {
4147 >            head.thenRun(() -> count.getAndIncrement());
4148 >            head.thenAccept((x) -> count.getAndIncrement());
4149 >            head.thenApply((x) -> count.getAndIncrement());
4150 >
4151 >            head.runAfterBoth(complete, () -> count.getAndIncrement());
4152 >            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
4153 >            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
4154 >            complete.runAfterBoth(head, () -> count.getAndIncrement());
4155 >            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
4156 >            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4157 >
4158 >            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4159 >            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4160 >            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4161 >            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4162 >            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4163 >            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4164 >        }
4165 >        head.complete(null);
4166 >        assertEquals(5 * 3 * n, count.get());
4167 >    }
4168 >
4169 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4170 >    public void testCoCompletionGarbageRetention() throws Throwable {
4171 >        final int n = expensiveTests ? 1_000_000 : 10;
4172 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4173 >        CompletableFuture<Integer> f;
4174 >        for (int i = 0; i < n; i++) {
4175 >            f = new CompletableFuture<>();
4176 >            f.runAfterEither(incomplete, () -> {});
4177 >            f.complete(null);
4178 >
4179 >            f = new CompletableFuture<>();
4180 >            f.acceptEither(incomplete, (x) -> {});
4181 >            f.complete(null);
4182 >
4183 >            f = new CompletableFuture<>();
4184 >            f.applyToEither(incomplete, (x) -> x);
4185 >            f.complete(null);
4186 >
4187 >            f = new CompletableFuture<>();
4188 >            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4189 >            f.complete(null);
4190 >        }
4191 >
4192 >        for (int i = 0; i < n; i++) {
4193 >            f = new CompletableFuture<>();
4194 >            incomplete.runAfterEither(f, () -> {});
4195 >            f.complete(null);
4196 >
4197 >            f = new CompletableFuture<>();
4198 >            incomplete.acceptEither(f, (x) -> {});
4199 >            f.complete(null);
4200 >
4201 >            f = new CompletableFuture<>();
4202 >            incomplete.applyToEither(f, (x) -> x);
4203 >            f.complete(null);
4204 >
4205 >            f = new CompletableFuture<>();
4206 >            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4207 >            f.complete(null);
4208 >        }
4209 >    }
4210 >
4211 >    /*
4212 >     * Tests below currently fail in stress mode due to memory retention.
4213 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4214 >     */
4215 >
4216 >    /** Checks for garbage retention with anyOf. */
4217 >    public void testAnyOfGarbageRetention() throws Throwable {
4218 >        for (Integer v : new Integer[] { 1, null })
4219 >    {
4220 >        final int n = expensiveTests ? 100_000 : 10;
4221 >        CompletableFuture<Integer>[] fs
4222 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4223 >        for (int i = 0; i < fs.length; i++)
4224 >            fs[i] = new CompletableFuture<>();
4225 >        fs[fs.length - 1].complete(v);
4226 >        for (int i = 0; i < n; i++)
4227 >            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4228 >    }}
4229 >
4230 >    /** Checks for garbage retention with allOf. */
4231 >    public void testCancelledAllOfGarbageRetention() throws Throwable {
4232 >        final int n = expensiveTests ? 100_000 : 10;
4233 >        CompletableFuture<Integer>[] fs
4234 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4235 >        for (int i = 0; i < fs.length; i++)
4236 >            fs[i] = new CompletableFuture<>();
4237 >        for (int i = 0; i < n; i++)
4238 >            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4239      }
4240  
4241   //     static <U> U join(CompletionStage<U> stage) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines