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.133 by jsr166, Sun Nov 15 19:39:25 2015 UTC

# Line 982 | Line 982 | public class CompletableFutureTest exten
982       * If a whenComplete action throws an exception when triggered by
983       * a normal completion, it completes exceptionally
984       */
985 <    public void testWhenComplete_actionFailed() {
985 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
986          for (boolean createIncomplete : new boolean[] { true, false })
987          for (ExecutionMode m : ExecutionMode.values())
988          for (Integer v1 : new Integer[] { 1, null })
# Line 1152 | Line 1152 | public class CompletableFutureTest exten
1152          assertEquals(1, a.get());
1153      }}
1154  
1155 +    /**
1156 +     * If a "handle action" throws an exception when triggered by
1157 +     * a normal completion, it completes exceptionally
1158 +     */
1159      public void testHandle_sourceCompletedNormallyActionFailed() {
1160          for (ExecutionMode m : ExecutionMode.values())
1161          for (boolean createIncomplete : new boolean[] { true, false })
# Line 3764 | Line 3768 | public class CompletableFutureTest exten
3768      }
3769  
3770      static class Monad {
3771 <        static class MonadError extends Error {
3772 <            public MonadError() { super("monadic zero"); }
3771 >        static class ZeroException extends RuntimeException {
3772 >            public ZeroException() { super("monadic zero"); }
3773          }
3774          // "return", "unit"
3775          static <T> CompletableFuture<T> unit(T value) {
3776              return completedFuture(value);
3777          }
3778          // monadic zero ?
3779 <        static <T> CompletableFuture<T> zero(T value) {
3780 <            return failedFuture(new MonadError());
3779 >        static <T> CompletableFuture<T> zero() {
3780 >            return failedFuture(new ZeroException());
3781          }
3782          // >=>
3783          static <T,U,V> Function<T, CompletableFuture<V>> compose
# Line 3787 | Line 3791 | public class CompletableFutureTest exten
3791                  f.getNow(null);
3792                  throw new AssertionFailedError("should throw");
3793              } catch (CompletionException success) {
3794 <                assertTrue(success.getCause() instanceof MonadError);
3794 >                assertTrue(success.getCause() instanceof ZeroException);
3795              }
3796          }
3797  
# Line 3818 | Line 3822 | public class CompletableFutureTest exten
3822          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3823                                               CompletableFuture<? extends T> g) {
3824              PlusFuture<T> plus = new PlusFuture<T>();
3825 <            BiConsumer<T, Throwable> action = (T result, Throwable fail) -> {
3826 <                if (result != null) {
3825 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3826 >                if (ex == null) {
3827                      if (plus.complete(result))
3828                          if (plus.firstFailure.get() != null)
3829                              plus.firstFailure.set(null);
3830                  }
3831 <                else if (plus.firstFailure.compareAndSet(null, fail)) {
3831 >                else if (plus.firstFailure.compareAndSet(null, ex)) {
3832                      if (plus.isDone())
3833                          plus.firstFailure.set(null);
3834 <                } else {
3835 <                    if (plus.completeExceptionally(fail))
3836 <                        plus.firstFailure.set(null);
3834 >                }
3835 >                else {
3836 >                    // first failure has precedence
3837 >                    Throwable first = plus.firstFailure.getAndSet(null);
3838 >
3839 >                    // may fail with "Self-suppression not permitted"
3840 >                    try { first.addSuppressed(ex); }
3841 >                    catch (Exception ignored) {}
3842 >
3843 >                    plus.completeExceptionally(first);
3844                  }
3845              };
3846              f.whenComplete(action);
# Line 3843 | Line 3854 | public class CompletableFutureTest exten
3854       * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3855       */
3856      public void testAdditiveMonad() throws Throwable {
3857 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3858 +        CompletableFuture<Long> zero = Monad.zero();
3859 +
3860          // Some mutually non-commutative functions
3861          Function<Long, CompletableFuture<Long>> triple
3862 <            = (x) -> completedFuture(3 * x);
3862 >            = (x) -> Monad.unit(3 * x);
3863          Function<Long, CompletableFuture<Long>> inc
3864 <            = (x) -> completedFuture(x + 1);
3851 <
3852 <        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3853 <        Function<Long, CompletableFuture<Long>> zero = Monad::zero;
3864 >            = (x) -> Monad.unit(x + 1);
3865  
3866          // unit is a right identity: m >>= unit === m
3867 <        Monad.assertFutureEquals(
3868 <            inc.apply(5L).thenCompose(unit),
3858 <            inc.apply(5L));
3867 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3868 >                                 inc.apply(5L));
3869          // unit is a left identity: (unit x) >>= f === f x
3870 <        Monad.assertFutureEquals(
3871 <            unit.apply(5L).thenCompose(inc),
3872 <            inc.apply(5L));
3870 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3871 >                                 inc.apply(5L));
3872 >
3873          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3874          Monad.assertFutureEquals(
3875              unit.apply(5L).thenCompose(inc).thenCompose(triple),
3876              unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3877  
3878 +        // The case for CompletableFuture as an additive monad is weaker...
3879 +
3880          // zero is a monadic zero
3881 <        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));
3881 >        Monad.assertZero(zero);
3882  
3883 <        // inc plus zero === inc
3884 <        Monad.assertFutureEquals(
3885 <            inc.apply(5L),
3886 <            Monad.plus(inc.apply(5L), zero.apply(5L)));
3887 <        // zero plus inc === inc
3888 <        Monad.assertFutureEquals(
3889 <            inc.apply(5L),
3890 <            Monad.plus(zero.apply(5L), inc.apply(5L)));
3883 >        // left zero: zero >>= f === zero
3884 >        Monad.assertZero(zero.thenCompose(inc));
3885 >        // right zero: f >>= (\x -> zero) === zero
3886 >        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3887 >
3888 >        // f plus zero === f
3889 >        Monad.assertFutureEquals(Monad.unit(5L),
3890 >                                 Monad.plus(Monad.unit(5L), zero));
3891 >        // zero plus f === f
3892 >        Monad.assertFutureEquals(Monad.unit(5L),
3893 >                                 Monad.plus(zero, Monad.unit(5L)));
3894          // zero plus zero === zero
3895 <        Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L)));
3895 >        Monad.assertZero(Monad.plus(zero, zero));
3896          {
3897 <            CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L));
3898 <            assertTrue(f.get() == 6L || f.get() == 9L);
3897 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3898 >                                                   Monad.unit(8L));
3899 >            // non-determinism
3900 >            assertTrue(f.get() == 5L || f.get() == 8L);
3901          }
3902  
3903          CompletableFuture<Long> godot = new CompletableFuture<>();
3904 <        // inc plus godot === inc (doesn't wait for godot)
3905 <        Monad.assertFutureEquals(
3906 <            inc.apply(5L),
3907 <            Monad.plus(inc.apply(5L), godot));
3908 <        // godot plus inc === inc (doesn't wait for godot)
3909 <        Monad.assertFutureEquals(
3897 <            inc.apply(5L),
3898 <            Monad.plus(godot, inc.apply(5L)));
3904 >        // f plus godot === f (doesn't wait for godot)
3905 >        Monad.assertFutureEquals(Monad.unit(5L),
3906 >                                 Monad.plus(Monad.unit(5L), godot));
3907 >        // godot plus f === f (doesn't wait for godot)
3908 >        Monad.assertFutureEquals(Monad.unit(5L),
3909 >                                 Monad.plus(godot, Monad.unit(5L)));
3910      }
3911  
3912   //     static <U> U join(CompletionStage<U> stage) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines