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.136 by jsr166, Sun Nov 15 20:17:11 2015 UTC

# Line 876 | Line 876 | public class CompletableFutureTest exten
876          assertEquals(1, a.get());
877      }}
878  
879 +    /**
880 +     * If an "exceptionally action" throws an exception, it completes
881 +     * exceptionally with that exception
882 +     */
883      public void testExceptionally_exceptionalCompletionActionFailed() {
884          for (boolean createIncomplete : new boolean[] { true, false })
885      {
# Line 894 | Line 898 | public class CompletableFutureTest exten
898          if (createIncomplete) f.completeExceptionally(ex1);
899  
900          checkCompletedWithWrappedException(g, ex2);
901 +        checkCompletedExceptionally(f, ex1);
902          assertEquals(1, a.get());
903      }}
904  
# Line 911 | Line 916 | public class CompletableFutureTest exten
916          if (!createIncomplete) assertTrue(f.complete(v1));
917          final CompletableFuture<Integer> g = m.whenComplete
918              (f,
919 <             (Integer x, Throwable t) -> {
919 >             (Integer result, Throwable t) -> {
920                  m.checkExecutionMode();
921 <                threadAssertSame(x, v1);
921 >                threadAssertSame(result, v1);
922                  threadAssertNull(t);
923                  a.getAndIncrement();
924              });
# Line 938 | Line 943 | public class CompletableFutureTest exten
943          if (!createIncomplete) f.completeExceptionally(ex);
944          final CompletableFuture<Integer> g = m.whenComplete
945              (f,
946 <             (Integer x, Throwable t) -> {
946 >             (Integer result, Throwable t) -> {
947                  m.checkExecutionMode();
948 <                threadAssertNull(x);
948 >                threadAssertNull(result);
949                  threadAssertSame(t, ex);
950                  a.getAndIncrement();
951              });
# Line 965 | Line 970 | public class CompletableFutureTest exten
970          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971          final CompletableFuture<Integer> g = m.whenComplete
972              (f,
973 <             (Integer x, Throwable t) -> {
973 >             (Integer result, Throwable t) -> {
974                  m.checkExecutionMode();
975 <                threadAssertNull(x);
975 >                threadAssertNull(result);
976                  threadAssertTrue(t instanceof CancellationException);
977                  a.getAndIncrement();
978              });
# Line 982 | Line 987 | public class CompletableFutureTest exten
987       * If a whenComplete action throws an exception when triggered by
988       * a normal completion, it completes exceptionally
989       */
990 <    public void testWhenComplete_actionFailed() {
990 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
991          for (boolean createIncomplete : new boolean[] { true, false })
992          for (ExecutionMode m : ExecutionMode.values())
993          for (Integer v1 : new Integer[] { 1, null })
# Line 993 | Line 998 | public class CompletableFutureTest exten
998          if (!createIncomplete) assertTrue(f.complete(v1));
999          final CompletableFuture<Integer> g = m.whenComplete
1000              (f,
1001 <             (Integer x, Throwable t) -> {
1001 >             (Integer result, Throwable t) -> {
1002                  m.checkExecutionMode();
1003 <                threadAssertSame(x, v1);
1003 >                threadAssertSame(result, v1);
1004                  threadAssertNull(t);
1005                  a.getAndIncrement();
1006                  throw ex;
# Line 1010 | Line 1015 | public class CompletableFutureTest exten
1015      /**
1016       * If a whenComplete action throws an exception when triggered by
1017       * a source completion that also throws an exception, the source
1018 <     * exception takes precedence.
1018 >     * exception takes precedence (unlike handle)
1019       */
1020 <    public void testWhenComplete_actionFailedSourceFailed() {
1020 >    public void testWhenComplete_sourceFailedActionFailed() {
1021          for (boolean createIncomplete : new boolean[] { true, false })
1022          for (ExecutionMode m : ExecutionMode.values())
1023      {
# Line 1024 | Line 1029 | public class CompletableFutureTest exten
1029          if (!createIncomplete) f.completeExceptionally(ex1);
1030          final CompletableFuture<Integer> g = m.whenComplete
1031              (f,
1032 <             (Integer x, Throwable t) -> {
1032 >             (Integer result, Throwable t) -> {
1033                  m.checkExecutionMode();
1034                  threadAssertSame(t, ex1);
1035 <                threadAssertNull(x);
1035 >                threadAssertNull(result);
1036                  a.getAndIncrement();
1037                  throw ex2;
1038              });
# Line 1052 | Line 1057 | public class CompletableFutureTest exten
1057          if (!createIncomplete) assertTrue(f.complete(v1));
1058          final CompletableFuture<Integer> g = m.handle
1059              (f,
1060 <             (Integer x, Throwable t) -> {
1060 >             (Integer result, Throwable t) -> {
1061                  m.checkExecutionMode();
1062 <                threadAssertSame(x, v1);
1062 >                threadAssertSame(result, v1);
1063                  threadAssertNull(t);
1064                  a.getAndIncrement();
1065                  return inc(v1);
# Line 1081 | Line 1086 | public class CompletableFutureTest exten
1086          if (!createIncomplete) f.completeExceptionally(ex);
1087          final CompletableFuture<Integer> g = m.handle
1088              (f,
1089 <             (Integer x, Throwable t) -> {
1089 >             (Integer result, Throwable t) -> {
1090                  m.checkExecutionMode();
1091 <                threadAssertNull(x);
1091 >                threadAssertNull(result);
1092                  threadAssertSame(t, ex);
1093                  a.getAndIncrement();
1094                  return v1;
# Line 1110 | Line 1115 | public class CompletableFutureTest exten
1115          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1116          final CompletableFuture<Integer> g = m.handle
1117              (f,
1118 <             (Integer x, Throwable t) -> {
1118 >             (Integer result, Throwable t) -> {
1119                  m.checkExecutionMode();
1120 <                threadAssertNull(x);
1120 >                threadAssertNull(result);
1121                  threadAssertTrue(t instanceof CancellationException);
1122                  a.getAndIncrement();
1123                  return v1;
# Line 1125 | Line 1130 | public class CompletableFutureTest exten
1130      }}
1131  
1132      /**
1133 <     * handle result completes exceptionally if action does
1133 >     * If a "handle action" throws an exception when triggered by
1134 >     * a normal completion, it completes exceptionally
1135       */
1136 <    public void testHandle_sourceFailedActionFailed() {
1136 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1137          for (ExecutionMode m : ExecutionMode.values())
1138          for (boolean createIncomplete : new boolean[] { true, false })
1139 +        for (Integer v1 : new Integer[] { 1, null })
1140      {
1141          final CompletableFuture<Integer> f = new CompletableFuture<>();
1142          final AtomicInteger a = new AtomicInteger(0);
1143 <        final CFException ex1 = new CFException();
1144 <        final CFException ex2 = new CFException();
1138 <        if (!createIncomplete) f.completeExceptionally(ex1);
1143 >        final CFException ex = new CFException();
1144 >        if (!createIncomplete) assertTrue(f.complete(v1));
1145          final CompletableFuture<Integer> g = m.handle
1146              (f,
1147 <             (Integer x, Throwable t) -> {
1147 >             (Integer result, Throwable t) -> {
1148                  m.checkExecutionMode();
1149 <                threadAssertNull(x);
1150 <                threadAssertSame(ex1, t);
1149 >                threadAssertSame(result, v1);
1150 >                threadAssertNull(t);
1151                  a.getAndIncrement();
1152 <                throw ex2;
1152 >                throw ex;
1153              });
1154 <        if (createIncomplete) f.completeExceptionally(ex1);
1154 >        if (createIncomplete) assertTrue(f.complete(v1));
1155  
1156 <        checkCompletedWithWrappedException(g, ex2);
1157 <        checkCompletedExceptionally(f, ex1);
1156 >        checkCompletedWithWrappedException(g, ex);
1157 >        checkCompletedNormally(f, v1);
1158          assertEquals(1, a.get());
1159      }}
1160  
1161 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1162 <        for (ExecutionMode m : ExecutionMode.values())
1161 >    /**
1162 >     * If a "handle action" throws an exception when triggered by
1163 >     * a source completion that also throws an exception, the action
1164 >     * exception takes precedence (unlike whenComplete)
1165 >     */
1166 >    public void testHandle_sourceFailedActionFailed() {
1167          for (boolean createIncomplete : new boolean[] { true, false })
1168 <        for (Integer v1 : new Integer[] { 1, null })
1168 >        for (ExecutionMode m : ExecutionMode.values())
1169      {
1160        final CompletableFuture<Integer> f = new CompletableFuture<>();
1170          final AtomicInteger a = new AtomicInteger(0);
1171 <        final CFException ex = new CFException();
1172 <        if (!createIncomplete) assertTrue(f.complete(v1));
1171 >        final CFException ex1 = new CFException();
1172 >        final CFException ex2 = new CFException();
1173 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174 >
1175 >        if (!createIncomplete) f.completeExceptionally(ex1);
1176          final CompletableFuture<Integer> g = m.handle
1177              (f,
1178 <             (Integer x, Throwable t) -> {
1178 >             (Integer result, Throwable t) -> {
1179                  m.checkExecutionMode();
1180 <                threadAssertSame(x, v1);
1181 <                threadAssertNull(t);
1180 >                threadAssertNull(result);
1181 >                threadAssertSame(ex1, t);
1182                  a.getAndIncrement();
1183 <                throw ex;
1183 >                throw ex2;
1184              });
1185 <        if (createIncomplete) assertTrue(f.complete(v1));
1185 >        if (createIncomplete) f.completeExceptionally(ex1);
1186  
1187 <        checkCompletedWithWrappedException(g, ex);
1188 <        checkCompletedNormally(f, v1);
1187 >        checkCompletedWithWrappedException(g, ex2);
1188 >        checkCompletedExceptionally(f, ex1);
1189          assertEquals(1, a.get());
1190      }}
1191  
# Line 3644 | Line 3656 | public class CompletableFutureTest exten
3656          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3657          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3658  
3659 <        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3659 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3660  
3661          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3662  
# Line 3764 | Line 3776 | public class CompletableFutureTest exten
3776      }
3777  
3778      static class Monad {
3779 <        static class MonadError extends Error {
3780 <            public MonadError() { super("monadic zero"); }
3779 >        static class ZeroException extends RuntimeException {
3780 >            public ZeroException() { super("monadic zero"); }
3781          }
3782          // "return", "unit"
3783          static <T> CompletableFuture<T> unit(T value) {
3784              return completedFuture(value);
3785          }
3786          // monadic zero ?
3787 <        static <T> CompletableFuture<T> zero(T value) {
3788 <            return failedFuture(new MonadError());
3787 >        static <T> CompletableFuture<T> zero() {
3788 >            return failedFuture(new ZeroException());
3789          }
3790          // >=>
3791          static <T,U,V> Function<T, CompletableFuture<V>> compose
# Line 3787 | Line 3799 | public class CompletableFutureTest exten
3799                  f.getNow(null);
3800                  throw new AssertionFailedError("should throw");
3801              } catch (CompletionException success) {
3802 <                assertTrue(success.getCause() instanceof MonadError);
3802 >                assertTrue(success.getCause() instanceof ZeroException);
3803              }
3804          }
3805  
# Line 3818 | Line 3830 | public class CompletableFutureTest exten
3830          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3831                                               CompletableFuture<? extends T> g) {
3832              PlusFuture<T> plus = new PlusFuture<T>();
3833 <            BiConsumer<T, Throwable> action = (T result, Throwable fail) -> {
3834 <                if (result != null) {
3833 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3834 >                if (ex == null) {
3835                      if (plus.complete(result))
3836                          if (plus.firstFailure.get() != null)
3837                              plus.firstFailure.set(null);
3838                  }
3839 <                else if (plus.firstFailure.compareAndSet(null, fail)) {
3839 >                else if (plus.firstFailure.compareAndSet(null, ex)) {
3840                      if (plus.isDone())
3841                          plus.firstFailure.set(null);
3842 <                } else {
3843 <                    if (plus.completeExceptionally(fail))
3844 <                        plus.firstFailure.set(null);
3842 >                }
3843 >                else {
3844 >                    // first failure has precedence
3845 >                    Throwable first = plus.firstFailure.getAndSet(null);
3846 >
3847 >                    // may fail with "Self-suppression not permitted"
3848 >                    try { first.addSuppressed(ex); }
3849 >                    catch (Exception ignored) {}
3850 >
3851 >                    plus.completeExceptionally(first);
3852                  }
3853              };
3854              f.whenComplete(action);
# Line 3843 | Line 3862 | public class CompletableFutureTest exten
3862       * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3863       */
3864      public void testAdditiveMonad() throws Throwable {
3865 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3866 +        CompletableFuture<Long> zero = Monad.zero();
3867 +
3868          // Some mutually non-commutative functions
3869          Function<Long, CompletableFuture<Long>> triple
3870 <            = (x) -> completedFuture(3 * x);
3870 >            = (x) -> Monad.unit(3 * x);
3871          Function<Long, CompletableFuture<Long>> inc
3872 <            = (x) -> completedFuture(x + 1);
3851 <
3852 <        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3853 <        Function<Long, CompletableFuture<Long>> zero = Monad::zero;
3872 >            = (x) -> Monad.unit(x + 1);
3873  
3874          // unit is a right identity: m >>= unit === m
3875 <        Monad.assertFutureEquals(
3876 <            inc.apply(5L).thenCompose(unit),
3858 <            inc.apply(5L));
3875 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3876 >                                 inc.apply(5L));
3877          // unit is a left identity: (unit x) >>= f === f x
3878 <        Monad.assertFutureEquals(
3879 <            unit.apply(5L).thenCompose(inc),
3880 <            inc.apply(5L));
3878 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3879 >                                 inc.apply(5L));
3880 >
3881          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3882          Monad.assertFutureEquals(
3883              unit.apply(5L).thenCompose(inc).thenCompose(triple),
3884              unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3885  
3886 +        // The case for CompletableFuture as an additive monad is weaker...
3887 +
3888          // zero is a monadic zero
3889 <        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));
3889 >        Monad.assertZero(zero);
3890  
3891 <        // inc plus zero === inc
3892 <        Monad.assertFutureEquals(
3893 <            inc.apply(5L),
3894 <            Monad.plus(inc.apply(5L), zero.apply(5L)));
3895 <        // zero plus inc === inc
3896 <        Monad.assertFutureEquals(
3897 <            inc.apply(5L),
3898 <            Monad.plus(zero.apply(5L), inc.apply(5L)));
3891 >        // left zero: zero >>= f === zero
3892 >        Monad.assertZero(zero.thenCompose(inc));
3893 >        // right zero: f >>= (\x -> zero) === zero
3894 >        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3895 >
3896 >        // f plus zero === f
3897 >        Monad.assertFutureEquals(Monad.unit(5L),
3898 >                                 Monad.plus(Monad.unit(5L), zero));
3899 >        // zero plus f === f
3900 >        Monad.assertFutureEquals(Monad.unit(5L),
3901 >                                 Monad.plus(zero, Monad.unit(5L)));
3902          // zero plus zero === zero
3903 <        Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L)));
3903 >        Monad.assertZero(Monad.plus(zero, zero));
3904          {
3905 <            CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L));
3906 <            assertTrue(f.get() == 6L || f.get() == 9L);
3905 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3906 >                                                   Monad.unit(8L));
3907 >            // non-determinism
3908 >            assertTrue(f.get() == 5L || f.get() == 8L);
3909          }
3910  
3911          CompletableFuture<Long> godot = new CompletableFuture<>();
3912 <        // inc plus godot === inc (doesn't wait for godot)
3913 <        Monad.assertFutureEquals(
3914 <            inc.apply(5L),
3915 <            Monad.plus(inc.apply(5L), godot));
3916 <        // godot plus inc === inc (doesn't wait for godot)
3917 <        Monad.assertFutureEquals(
3897 <            inc.apply(5L),
3898 <            Monad.plus(godot, inc.apply(5L)));
3912 >        // f plus godot === f (doesn't wait for godot)
3913 >        Monad.assertFutureEquals(Monad.unit(5L),
3914 >                                 Monad.plus(Monad.unit(5L), godot));
3915 >        // godot plus f === f (doesn't wait for godot)
3916 >        Monad.assertFutureEquals(Monad.unit(5L),
3917 >                                 Monad.plus(godot, Monad.unit(5L)));
3918      }
3919  
3920   //     static <U> U join(CompletionStage<U> stage) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines