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.127 by jsr166, Sun Oct 25 02:58:25 2015 UTC vs.
Revision 1.128 by jsr166, Sun Nov 15 00:37:50 2015 UTC

# Line 7 | Line 7
7  
8   import static java.util.concurrent.TimeUnit.MILLISECONDS;
9   import static java.util.concurrent.TimeUnit.SECONDS;
10 + import static java.util.concurrent.CompletableFuture.completedFuture;
11 + import static java.util.concurrent.CompletableFuture.failedFuture;
12  
13   import java.lang.reflect.Method;
14   import java.lang.reflect.Modifier;
# Line 39 | Line 41 | import java.util.function.Function;
41   import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
44 + import junit.framework.AssertionFailedError;
45   import junit.framework.Test;
46   import junit.framework.TestSuite;
47  
# Line 3760 | Line 3763 | public class CompletableFutureTest exten
3763              throw new Error("Methods did not throw UOE: " + bugs.toString());
3764      }
3765  
3766 +    static class Monad {
3767 +        static class MonadError extends Error {
3768 +            public MonadError() { super("monadic zero"); }
3769 +        }
3770 +        // "return", "unit"
3771 +        static <T> CompletableFuture<T> unit(T value) {
3772 +            return completedFuture(value);
3773 +        }
3774 +        // monadic zero ?
3775 +        static <T> CompletableFuture<T> zero(T value) {
3776 +            return failedFuture(new MonadError());
3777 +        }
3778 +        // >=>
3779 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3780 +            (Function<T, CompletableFuture<U>> f,
3781 +             Function<U, CompletableFuture<V>> g) {
3782 +            return (x) -> f.apply(x).thenCompose(g);
3783 +        }
3784 +
3785 +        static void assertZero(CompletableFuture<?> f) {
3786 +            try {
3787 +                f.getNow(null);
3788 +                throw new AssertionFailedError("should throw");
3789 +            } catch (CompletionException success) {
3790 +                assertTrue(success.getCause() instanceof MonadError);
3791 +            }
3792 +        }
3793 +
3794 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3795 +                                           CompletableFuture<T> g) {
3796 +            T fval = null, gval = null;
3797 +            Throwable fex = null, gex = null;
3798 +
3799 +            try { fval = f.get(); }
3800 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3801 +            catch (Throwable ex) { fex = ex; }
3802 +
3803 +            try { gval = g.get(); }
3804 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3805 +            catch (Throwable ex) { gex = ex; }
3806 +
3807 +            if (fex != null || gex != null)
3808 +                assertSame(fex.getClass(), gex.getClass());
3809 +            else
3810 +                assertEquals(fval, gval);
3811 +        }
3812 +
3813 +        static class PlusFuture<T> extends CompletableFuture<T> {
3814 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3815 +        }
3816 +
3817 +        // Monadic "plus"
3818 +        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3819 +                                             CompletableFuture<? extends T> g) {
3820 +            PlusFuture<T> plus = new PlusFuture<T>();
3821 +            BiConsumer<T, Throwable> action = (T result, Throwable fail) -> {
3822 +                if (result != null) {
3823 +                    if (plus.complete(result))
3824 +                        if (plus.firstFailure.get() != null)
3825 +                            plus.firstFailure.set(null);
3826 +                }
3827 +                else if (plus.firstFailure.compareAndSet(null, fail)) {
3828 +                    if (plus.isDone())
3829 +                        plus.firstFailure.set(null);
3830 +                } else {
3831 +                    if (plus.completeExceptionally(fail))
3832 +                        plus.firstFailure.set(null);
3833 +                }
3834 +            };
3835 +            f.whenComplete(action);
3836 +            g.whenComplete(action);
3837 +            return plus;
3838 +        }
3839 +    }
3840 +
3841 +    /**
3842 +     * CompletableFuture is an additive monad - sort of.
3843 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3844 +     */
3845 +    public void testAdditiveMonad() throws Throwable {
3846 +        // Some mutually non-commutative functions
3847 +        Function<Long, CompletableFuture<Long>> triple
3848 +            = (x) -> completedFuture(3 * x);
3849 +        Function<Long, CompletableFuture<Long>> inc
3850 +            = (x) -> completedFuture(x + 1);
3851 +
3852 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3853 +        Function<Long, CompletableFuture<Long>> zero = Monad::zero;
3854 +
3855 +        // unit is a right identity: m >>= unit === m
3856 +        Monad.assertFutureEquals(
3857 +            inc.apply(5L).thenCompose(unit),
3858 +            inc.apply(5L));
3859 +        // unit is a left identity: (unit x) >>= f === f x
3860 +        Monad.assertFutureEquals(
3861 +            unit.apply(5L).thenCompose(inc),
3862 +            inc.apply(5L));
3863 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3864 +        Monad.assertFutureEquals(
3865 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3866 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3867 +
3868 +        // zero is a monadic zero
3869 +        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));
3874 +
3875 +        // inc plus zero === inc
3876 +        Monad.assertFutureEquals(
3877 +            inc.apply(5L),
3878 +            Monad.plus(inc.apply(5L), zero.apply(5L)));
3879 +        // zero plus inc === inc
3880 +        Monad.assertFutureEquals(
3881 +            inc.apply(5L),
3882 +            Monad.plus(zero.apply(5L), inc.apply(5L)));
3883 +        // zero plus zero === zero
3884 +        Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L)));
3885 +        {
3886 +            CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L));
3887 +            assertTrue(f.get() == 6L || f.get() == 9L);
3888 +        }
3889 +
3890 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3891 +        // inc plus godot === inc (doesn't wait for godot)
3892 +        Monad.assertFutureEquals(
3893 +            inc.apply(5L),
3894 +            Monad.plus(inc.apply(5L), godot));
3895 +        // godot plus inc === inc (doesn't wait for godot)
3896 +        Monad.assertFutureEquals(
3897 +            inc.apply(5L),
3898 +            Monad.plus(godot, inc.apply(5L)));
3899 +    }
3900 +
3901   //     static <U> U join(CompletionStage<U> stage) {
3902   //         CompletableFuture<U> f = new CompletableFuture<>();
3903   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines