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 }) |
3764 |
|
} |
3765 |
|
|
3766 |
|
static class Monad { |
3767 |
< |
static class MonadError extends Error { |
3768 |
< |
public MonadError() { super("monadic zero"); } |
3767 |
> |
static class ZeroException extends RuntimeException { |
3768 |
> |
public ZeroException() { 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()); |
3775 |
> |
static <T> CompletableFuture<T> zero() { |
3776 |
> |
return failedFuture(new ZeroException()); |
3777 |
|
} |
3778 |
|
// >=> |
3779 |
|
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3787 |
|
f.getNow(null); |
3788 |
|
throw new AssertionFailedError("should throw"); |
3789 |
|
} catch (CompletionException success) { |
3790 |
< |
assertTrue(success.getCause() instanceof MonadError); |
3790 |
> |
assertTrue(success.getCause() instanceof ZeroException); |
3791 |
|
} |
3792 |
|
} |
3793 |
|
|
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) { |
3821 |
> |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3822 |
> |
if (ex == 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)) { |
3827 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3828 |
|
if (plus.isDone()) |
3829 |
|
plus.firstFailure.set(null); |
3830 |
< |
} else { |
3831 |
< |
if (plus.completeExceptionally(fail)) |
3832 |
< |
plus.firstFailure.set(null); |
3830 |
> |
} |
3831 |
> |
else { |
3832 |
> |
// first failure has precedence |
3833 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3834 |
> |
|
3835 |
> |
// may fail with "Self-suppression not permitted" |
3836 |
> |
try { first.addSuppressed(ex); } |
3837 |
> |
catch (Exception ignored) {} |
3838 |
> |
|
3839 |
> |
plus.completeExceptionally(first); |
3840 |
|
} |
3841 |
|
}; |
3842 |
|
f.whenComplete(action); |
3850 |
|
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3851 |
|
*/ |
3852 |
|
public void testAdditiveMonad() throws Throwable { |
3853 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3854 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3855 |
+ |
|
3856 |
|
// Some mutually non-commutative functions |
3857 |
|
Function<Long, CompletableFuture<Long>> triple |
3858 |
< |
= (x) -> completedFuture(3 * x); |
3858 |
> |
= (x) -> Monad.unit(3 * x); |
3859 |
|
Function<Long, CompletableFuture<Long>> inc |
3860 |
< |
= (x) -> completedFuture(x + 1); |
3851 |
< |
|
3852 |
< |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3853 |
< |
Function<Long, CompletableFuture<Long>> zero = Monad::zero; |
3860 |
> |
= (x) -> Monad.unit(x + 1); |
3861 |
|
|
3862 |
|
// unit is a right identity: m >>= unit === m |
3863 |
< |
Monad.assertFutureEquals( |
3864 |
< |
inc.apply(5L).thenCompose(unit), |
3858 |
< |
inc.apply(5L)); |
3863 |
> |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3864 |
> |
inc.apply(5L)); |
3865 |
|
// unit is a left identity: (unit x) >>= f === f x |
3866 |
< |
Monad.assertFutureEquals( |
3867 |
< |
unit.apply(5L).thenCompose(inc), |
3868 |
< |
inc.apply(5L)); |
3866 |
> |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3867 |
> |
inc.apply(5L)); |
3868 |
> |
|
3869 |
|
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3870 |
|
Monad.assertFutureEquals( |
3871 |
|
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3872 |
|
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3873 |
|
|
3874 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3875 |
+ |
|
3876 |
|
// zero is a monadic zero |
3877 |
< |
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)); |
3877 |
> |
Monad.assertZero(zero); |
3878 |
|
|
3879 |
< |
// inc plus zero === inc |
3880 |
< |
Monad.assertFutureEquals( |
3881 |
< |
inc.apply(5L), |
3882 |
< |
Monad.plus(inc.apply(5L), zero.apply(5L))); |
3883 |
< |
// zero plus inc === inc |
3884 |
< |
Monad.assertFutureEquals( |
3885 |
< |
inc.apply(5L), |
3886 |
< |
Monad.plus(zero.apply(5L), inc.apply(5L))); |
3879 |
> |
// left zero: zero >>= f === zero |
3880 |
> |
Monad.assertZero(zero.thenCompose(inc)); |
3881 |
> |
// right zero: f >>= (\x -> zero) === zero |
3882 |
> |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3883 |
> |
|
3884 |
> |
// f plus zero === f |
3885 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3886 |
> |
Monad.plus(Monad.unit(5L), zero)); |
3887 |
> |
// zero plus f === f |
3888 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3889 |
> |
Monad.plus(zero, Monad.unit(5L))); |
3890 |
|
// zero plus zero === zero |
3891 |
< |
Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L))); |
3891 |
> |
Monad.assertZero(Monad.plus(zero, zero)); |
3892 |
|
{ |
3893 |
< |
CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L)); |
3894 |
< |
assertTrue(f.get() == 6L || f.get() == 9L); |
3893 |
> |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3894 |
> |
Monad.unit(8L)); |
3895 |
> |
// non-determinism |
3896 |
> |
assertTrue(f.get() == 5L || f.get() == 8L); |
3897 |
|
} |
3898 |
|
|
3899 |
|
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3900 |
< |
// inc plus godot === inc (doesn't wait for godot) |
3901 |
< |
Monad.assertFutureEquals( |
3902 |
< |
inc.apply(5L), |
3903 |
< |
Monad.plus(inc.apply(5L), godot)); |
3904 |
< |
// godot plus inc === inc (doesn't wait for godot) |
3905 |
< |
Monad.assertFutureEquals( |
3897 |
< |
inc.apply(5L), |
3898 |
< |
Monad.plus(godot, inc.apply(5L))); |
3900 |
> |
// f plus godot === f (doesn't wait for godot) |
3901 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3902 |
> |
Monad.plus(Monad.unit(5L), godot)); |
3903 |
> |
// godot plus f === f (doesn't wait for godot) |
3904 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3905 |
> |
Monad.plus(godot, Monad.unit(5L))); |
3906 |
|
} |
3907 |
|
|
3908 |
|
// static <U> U join(CompletionStage<U> stage) { |