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; |
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 |
|
|
840 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
841 |
|
final CompletableFuture<Integer> g = f.exceptionally |
842 |
|
((Throwable t) -> { |
840 |
– |
// Should not be called |
843 |
|
a.getAndIncrement(); |
844 |
< |
throw new AssertionError(); |
844 |
> |
threadFail("should not be called"); |
845 |
> |
return null; // unreached |
846 |
|
}); |
847 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
848 |
|
|
901 |
|
* whenComplete action executes on normal completion, propagating |
902 |
|
* source result. |
903 |
|
*/ |
904 |
< |
public void testWhenComplete_normalCompletion1() { |
904 |
> |
public void testWhenComplete_normalCompletion() { |
905 |
|
for (ExecutionMode m : ExecutionMode.values()) |
906 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
907 |
|
for (Integer v1 : new Integer[] { 1, null }) |
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 }) |
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 }) |
3715 |
|
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3716 |
|
List<Method> minimalMethods = |
3717 |
|
Stream.of(Object.class, CompletionStage.class) |
3718 |
< |
.map((klazz) -> Stream.of(klazz.getMethods())) |
3712 |
< |
.reduce(Stream::concat) |
3713 |
< |
.orElseGet(Stream::empty) |
3718 |
> |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3719 |
|
.filter(isNotStatic) |
3720 |
|
.collect(Collectors.toList()); |
3721 |
|
// Methods from CompletableFuture permitted NOT to throw UOE |
3767 |
|
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3768 |
|
} |
3769 |
|
|
3770 |
+ |
static class Monad { |
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() { |
3780 |
+ |
return failedFuture(new ZeroException()); |
3781 |
+ |
} |
3782 |
+ |
// >=> |
3783 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3784 |
+ |
(Function<T, CompletableFuture<U>> f, |
3785 |
+ |
Function<U, CompletableFuture<V>> g) { |
3786 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3787 |
+ |
} |
3788 |
+ |
|
3789 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3790 |
+ |
try { |
3791 |
+ |
f.getNow(null); |
3792 |
+ |
throw new AssertionFailedError("should throw"); |
3793 |
+ |
} catch (CompletionException success) { |
3794 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3795 |
+ |
} |
3796 |
+ |
} |
3797 |
+ |
|
3798 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3799 |
+ |
CompletableFuture<T> g) { |
3800 |
+ |
T fval = null, gval = null; |
3801 |
+ |
Throwable fex = null, gex = null; |
3802 |
+ |
|
3803 |
+ |
try { fval = f.get(); } |
3804 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3805 |
+ |
catch (Throwable ex) { fex = ex; } |
3806 |
+ |
|
3807 |
+ |
try { gval = g.get(); } |
3808 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3809 |
+ |
catch (Throwable ex) { gex = ex; } |
3810 |
+ |
|
3811 |
+ |
if (fex != null || gex != null) |
3812 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3813 |
+ |
else |
3814 |
+ |
assertEquals(fval, gval); |
3815 |
+ |
} |
3816 |
+ |
|
3817 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3818 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3819 |
+ |
} |
3820 |
+ |
|
3821 |
+ |
// Monadic "plus" |
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 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, ex)) { |
3832 |
+ |
if (plus.isDone()) |
3833 |
+ |
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); |
3847 |
+ |
g.whenComplete(action); |
3848 |
+ |
return plus; |
3849 |
+ |
} |
3850 |
+ |
} |
3851 |
+ |
|
3852 |
+ |
/** |
3853 |
+ |
* CompletableFuture is an additive monad - sort of. |
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) -> Monad.unit(3 * x); |
3863 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3864 |
+ |
= (x) -> Monad.unit(x + 1); |
3865 |
+ |
|
3866 |
+ |
// unit is a right identity: m >>= unit === m |
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(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); |
3882 |
+ |
|
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, zero)); |
3896 |
+ |
{ |
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 |
+ |
// 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) { |
3913 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
3914 |
|
// stage.whenComplete((v, ex) -> { |