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.134 by jsr166, Sun Nov 15 19:55:38 2015 UTC vs.
Revision 1.143 by jsr166, Sun Apr 3 17:09:46 2016 UTC

# Line 640 | Line 640 | public class CompletableFutureTest exten
640  
641          ASYNC {
642              public void checkExecutionMode() {
643 <                assertEquals(defaultExecutorIsCommonPool,
644 <                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
643 >                // If tests are added that may run across different
644 >                // pools, this needs to be weakened to no-op.
645 >                ForkJoinPool p = ForkJoinTask.getPool();
646 >                assertTrue(p == null ||
647 >                           (defaultExecutorIsCommonPool &&
648 >                            p == ForkJoinPool.commonPool()));
649              }
650              public CompletableFuture<Void> runAsync(Runnable a) {
651                  return CompletableFuture.runAsync(a);
# Line 876 | Line 880 | public class CompletableFutureTest exten
880          assertEquals(1, a.get());
881      }}
882  
883 +    /**
884 +     * If an "exceptionally action" throws an exception, it completes
885 +     * exceptionally with that exception
886 +     */
887      public void testExceptionally_exceptionalCompletionActionFailed() {
888          for (boolean createIncomplete : new boolean[] { true, false })
889      {
# Line 894 | Line 902 | public class CompletableFutureTest exten
902          if (createIncomplete) f.completeExceptionally(ex1);
903  
904          checkCompletedWithWrappedException(g, ex2);
905 +        checkCompletedExceptionally(f, ex1);
906          assertEquals(1, a.get());
907      }}
908  
# Line 911 | Line 920 | public class CompletableFutureTest exten
920          if (!createIncomplete) assertTrue(f.complete(v1));
921          final CompletableFuture<Integer> g = m.whenComplete
922              (f,
923 <             (Integer x, Throwable t) -> {
923 >             (Integer result, Throwable t) -> {
924                  m.checkExecutionMode();
925 <                threadAssertSame(x, v1);
925 >                threadAssertSame(result, v1);
926                  threadAssertNull(t);
927                  a.getAndIncrement();
928              });
# Line 938 | Line 947 | public class CompletableFutureTest exten
947          if (!createIncomplete) f.completeExceptionally(ex);
948          final CompletableFuture<Integer> g = m.whenComplete
949              (f,
950 <             (Integer x, Throwable t) -> {
950 >             (Integer result, Throwable t) -> {
951                  m.checkExecutionMode();
952 <                threadAssertNull(x);
952 >                threadAssertNull(result);
953                  threadAssertSame(t, ex);
954                  a.getAndIncrement();
955              });
# Line 965 | Line 974 | public class CompletableFutureTest exten
974          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
975          final CompletableFuture<Integer> g = m.whenComplete
976              (f,
977 <             (Integer x, Throwable t) -> {
977 >             (Integer result, Throwable t) -> {
978                  m.checkExecutionMode();
979 <                threadAssertNull(x);
979 >                threadAssertNull(result);
980                  threadAssertTrue(t instanceof CancellationException);
981                  a.getAndIncrement();
982              });
# Line 993 | Line 1002 | public class CompletableFutureTest exten
1002          if (!createIncomplete) assertTrue(f.complete(v1));
1003          final CompletableFuture<Integer> g = m.whenComplete
1004              (f,
1005 <             (Integer x, Throwable t) -> {
1005 >             (Integer result, Throwable t) -> {
1006                  m.checkExecutionMode();
1007 <                threadAssertSame(x, v1);
1007 >                threadAssertSame(result, v1);
1008                  threadAssertNull(t);
1009                  a.getAndIncrement();
1010                  throw ex;
# Line 1024 | Line 1033 | public class CompletableFutureTest exten
1033          if (!createIncomplete) f.completeExceptionally(ex1);
1034          final CompletableFuture<Integer> g = m.whenComplete
1035              (f,
1036 <             (Integer x, Throwable t) -> {
1036 >             (Integer result, Throwable t) -> {
1037                  m.checkExecutionMode();
1038                  threadAssertSame(t, ex1);
1039 <                threadAssertNull(x);
1039 >                threadAssertNull(result);
1040                  a.getAndIncrement();
1041                  throw ex2;
1042              });
# Line 1035 | Line 1044 | public class CompletableFutureTest exten
1044  
1045          checkCompletedWithWrappedException(g, ex1);
1046          checkCompletedExceptionally(f, ex1);
1047 +        if (testImplementationDetails) {
1048 +            assertEquals(1, ex1.getSuppressed().length);
1049 +            assertSame(ex2, ex1.getSuppressed()[0]);
1050 +        }
1051          assertEquals(1, a.get());
1052      }}
1053  
# Line 1052 | Line 1065 | public class CompletableFutureTest exten
1065          if (!createIncomplete) assertTrue(f.complete(v1));
1066          final CompletableFuture<Integer> g = m.handle
1067              (f,
1068 <             (Integer x, Throwable t) -> {
1068 >             (Integer result, Throwable t) -> {
1069                  m.checkExecutionMode();
1070 <                threadAssertSame(x, v1);
1070 >                threadAssertSame(result, v1);
1071                  threadAssertNull(t);
1072                  a.getAndIncrement();
1073                  return inc(v1);
# Line 1081 | Line 1094 | public class CompletableFutureTest exten
1094          if (!createIncomplete) f.completeExceptionally(ex);
1095          final CompletableFuture<Integer> g = m.handle
1096              (f,
1097 <             (Integer x, Throwable t) -> {
1097 >             (Integer result, Throwable t) -> {
1098                  m.checkExecutionMode();
1099 <                threadAssertNull(x);
1099 >                threadAssertNull(result);
1100                  threadAssertSame(t, ex);
1101                  a.getAndIncrement();
1102                  return v1;
# Line 1110 | Line 1123 | public class CompletableFutureTest exten
1123          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1124          final CompletableFuture<Integer> g = m.handle
1125              (f,
1126 <             (Integer x, Throwable t) -> {
1126 >             (Integer result, Throwable t) -> {
1127                  m.checkExecutionMode();
1128 <                threadAssertNull(x);
1128 >                threadAssertNull(result);
1129                  threadAssertTrue(t instanceof CancellationException);
1130                  a.getAndIncrement();
1131                  return v1;
# Line 1139 | Line 1152 | public class CompletableFutureTest exten
1152          if (!createIncomplete) assertTrue(f.complete(v1));
1153          final CompletableFuture<Integer> g = m.handle
1154              (f,
1155 <             (Integer x, Throwable t) -> {
1155 >             (Integer result, Throwable t) -> {
1156                  m.checkExecutionMode();
1157 <                threadAssertSame(x, v1);
1157 >                threadAssertSame(result, v1);
1158                  threadAssertNull(t);
1159                  a.getAndIncrement();
1160                  throw ex;
# Line 1170 | Line 1183 | public class CompletableFutureTest exten
1183          if (!createIncomplete) f.completeExceptionally(ex1);
1184          final CompletableFuture<Integer> g = m.handle
1185              (f,
1186 <             (Integer x, Throwable t) -> {
1186 >             (Integer result, Throwable t) -> {
1187                  m.checkExecutionMode();
1188 <                threadAssertNull(x);
1188 >                threadAssertNull(result);
1189                  threadAssertSame(ex1, t);
1190                  a.getAndIncrement();
1191                  throw ex2;
# Line 3286 | Line 3299 | public class CompletableFutureTest exten
3299              () -> f.obtrudeException(null),
3300  
3301              () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3302 <            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3302 >            () -> CompletableFuture.delayedExecutor(1L, null, exec),
3303              () -> CompletableFuture.delayedExecutor(1L, null),
3304  
3305              () -> f.orTimeout(1L, null),
# Line 3651 | Line 3664 | public class CompletableFutureTest exten
3664          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3665          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3666  
3667 <        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3667 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3668  
3669          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3670  
# Line 3821 | Line 3834 | public class CompletableFutureTest exten
3834              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3835          }
3836  
3837 <        // Monadic "plus"
3837 >        /** Implements "monadic plus". */
3838          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3839                                               CompletableFuture<? extends T> g) {
3840              PlusFuture<T> plus = new PlusFuture<T>();
3841              BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3842 <                if (ex == null) {
3843 <                    if (plus.complete(result))
3844 <                        if (plus.firstFailure.get() != null)
3842 >                try {
3843 >                    if (ex == null) {
3844 >                        if (plus.complete(result))
3845 >                            if (plus.firstFailure.get() != null)
3846 >                                plus.firstFailure.set(null);
3847 >                    }
3848 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3849 >                        if (plus.isDone())
3850                              plus.firstFailure.set(null);
3851 <                }
3852 <                else if (plus.firstFailure.compareAndSet(null, ex)) {
3853 <                    if (plus.isDone())
3854 <                        plus.firstFailure.set(null);
3855 <                }
3856 <                else {
3857 <                    // first failure has precedence
3858 <                    Throwable first = plus.firstFailure.getAndSet(null);
3859 <
3860 <                    // may fail with "Self-suppression not permitted"
3861 <                    try { first.addSuppressed(ex); }
3862 <                    catch (Exception ignored) {}
3863 <
3846 <                    plus.completeExceptionally(first);
3851 >                    }
3852 >                    else {
3853 >                        // first failure has precedence
3854 >                        Throwable first = plus.firstFailure.getAndSet(null);
3855 >
3856 >                        // may fail with "Self-suppression not permitted"
3857 >                        try { first.addSuppressed(ex); }
3858 >                        catch (Exception ignored) {}
3859 >
3860 >                        plus.completeExceptionally(first);
3861 >                    }
3862 >                } catch (Throwable unexpected) {
3863 >                    plus.completeExceptionally(unexpected);
3864                  }
3865              };
3866              f.whenComplete(action);
# Line 3912 | Line 3929 | public class CompletableFutureTest exten
3929                                   Monad.plus(godot, Monad.unit(5L)));
3930      }
3931  
3932 +    /**
3933 +     * A single CompletableFuture with many dependents.
3934 +     * A demo of scalability - runtime is O(n).
3935 +     */
3936 +    public void testManyDependents() throws Throwable {
3937 +        final int n = 1_000;
3938 +        final CompletableFuture<Void> head = new CompletableFuture<>();
3939 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3940 +        final AtomicInteger count = new AtomicInteger(0);
3941 +        for (int i = 0; i < n; i++) {
3942 +            head.thenRun(() -> count.getAndIncrement());
3943 +            head.thenAccept((x) -> count.getAndIncrement());
3944 +            head.thenApply((x) -> count.getAndIncrement());
3945 +
3946 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
3947 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3948 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3949 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
3950 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3951 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3952 +
3953 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3954 +            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3955 +            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3956 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3957 +            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3958 +            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3959 +        }
3960 +        head.complete(null);
3961 +        assertEquals(5 * 3 * n, count.get());
3962 +    }
3963 +
3964   //     static <U> U join(CompletionStage<U> stage) {
3965   //         CompletableFuture<U> f = new CompletableFuture<>();
3966   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines