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; |
38 |
|
import java.util.function.BiFunction; |
39 |
|
import java.util.function.Consumer; |
40 |
|
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 |
|
|
840 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
841 |
|
final CompletableFuture<Integer> g = f.exceptionally |
842 |
|
((Throwable t) -> { |
839 |
– |
// 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 }) |
3707 |
|
if (!testImplementationDetails) return; |
3708 |
|
Function<Method, String> toSignature = |
3709 |
|
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3710 |
+ |
Predicate<Method> isNotStatic = |
3711 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3712 |
|
List<Method> minimalMethods = |
3713 |
|
Stream.of(Object.class, CompletionStage.class) |
3714 |
< |
.map((klazz) -> Stream.of(klazz.getMethods())) |
3715 |
< |
.reduce(Stream::concat) |
3710 |
< |
.orElseGet(Stream::empty) |
3711 |
< |
.filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0) |
3714 |
> |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3715 |
> |
.filter(isNotStatic) |
3716 |
|
.collect(Collectors.toList()); |
3717 |
|
// Methods from CompletableFuture permitted NOT to throw UOE |
3718 |
|
String[] signatureWhitelist = { |
3726 |
|
Stream.of(signatureWhitelist)) |
3727 |
|
.collect(Collectors.toSet()); |
3728 |
|
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3729 |
< |
.filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0) |
3729 |
> |
.filter(isNotStatic) |
3730 |
|
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3731 |
|
.collect(Collectors.toList()); |
3732 |
|
|
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() { |
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 ex) -> { |
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, ex)) { |
3828 |
+ |
if (plus.isDone()) |
3829 |
+ |
plus.firstFailure.set(null); |
3830 |
+ |
} else { |
3831 |
+ |
if (plus.completeExceptionally(ex)) |
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 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3847 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3848 |
+ |
|
3849 |
+ |
// Some mutually non-commutative functions |
3850 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3851 |
+ |
= (x) -> Monad.unit(3 * x); |
3852 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3853 |
+ |
= (x) -> Monad.unit(x + 1); |
3854 |
+ |
|
3855 |
+ |
// unit is a right identity: m >>= unit === m |
3856 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3857 |
+ |
inc.apply(5L)); |
3858 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3859 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3860 |
+ |
inc.apply(5L)); |
3861 |
+ |
|
3862 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3863 |
+ |
Monad.assertFutureEquals( |
3864 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3865 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3866 |
+ |
|
3867 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3868 |
+ |
|
3869 |
+ |
// zero is a monadic zero |
3870 |
+ |
Monad.assertZero(zero); |
3871 |
+ |
|
3872 |
+ |
// left zero: zero >>= f === zero |
3873 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3874 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3875 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3876 |
+ |
|
3877 |
+ |
// f plus zero === f |
3878 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3879 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3880 |
+ |
// zero plus f === f |
3881 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3882 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3883 |
+ |
// zero plus zero === zero |
3884 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3885 |
+ |
{ |
3886 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3887 |
+ |
Monad.unit(8L)); |
3888 |
+ |
// non-determinism |
3889 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3890 |
+ |
} |
3891 |
+ |
|
3892 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3893 |
+ |
// f plus godot === f (doesn't wait for godot) |
3894 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3895 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3896 |
+ |
// godot plus f === f (doesn't wait for godot) |
3897 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3898 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3899 |
+ |
} |
3900 |
+ |
|
3901 |
|
// static <U> U join(CompletionStage<U> stage) { |
3902 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
3903 |
|
// stage.whenComplete((v, ex) -> { |