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; |
15 |
+ |
|
16 |
+ |
import java.util.stream.Collectors; |
17 |
+ |
import java.util.stream.Stream; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
+ |
import java.util.Arrays; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Objects; |
23 |
+ |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
25 |
|
import java.util.concurrent.CancellationException; |
26 |
|
import java.util.concurrent.CompletableFuture; |
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) -> { |
831 |
– |
// 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 }) |
3700 |
|
} |
3701 |
|
}} |
3702 |
|
|
3703 |
+ |
/** |
3704 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3705 |
+ |
*/ |
3706 |
+ |
public void testMinimalCompletionStage_minimality() { |
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 |
+ |
.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 = { |
3719 |
+ |
"newIncompleteFuture[]", |
3720 |
+ |
"defaultExecutor[]", |
3721 |
+ |
"minimalCompletionStage[]", |
3722 |
+ |
"copy[]", |
3723 |
+ |
}; |
3724 |
+ |
Set<String> permittedMethodSignatures = |
3725 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3726 |
+ |
Stream.of(signatureWhitelist)) |
3727 |
+ |
.collect(Collectors.toSet()); |
3728 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3729 |
+ |
.filter(isNotStatic) |
3730 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3731 |
+ |
.collect(Collectors.toList()); |
3732 |
+ |
|
3733 |
+ |
CompletionStage<Integer> minimalStage = |
3734 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3735 |
+ |
|
3736 |
+ |
List<Method> bugs = new ArrayList<>(); |
3737 |
+ |
for (Method method : allMethods) { |
3738 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3739 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3740 |
+ |
// Manufacture boxed primitives for primitive params |
3741 |
+ |
for (int i = 0; i < args.length; i++) { |
3742 |
+ |
Class<?> type = parameterTypes[i]; |
3743 |
+ |
if (parameterTypes[i] == boolean.class) |
3744 |
+ |
args[i] = false; |
3745 |
+ |
else if (parameterTypes[i] == int.class) |
3746 |
+ |
args[i] = 0; |
3747 |
+ |
else if (parameterTypes[i] == long.class) |
3748 |
+ |
args[i] = 0L; |
3749 |
+ |
} |
3750 |
+ |
try { |
3751 |
+ |
method.invoke(minimalStage, args); |
3752 |
+ |
bugs.add(method); |
3753 |
+ |
} |
3754 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3755 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3756 |
+ |
bugs.add(method); |
3757 |
+ |
// expected.getCause().printStackTrace(); |
3758 |
+ |
} |
3759 |
+ |
} |
3760 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3761 |
+ |
} |
3762 |
+ |
if (!bugs.isEmpty()) |
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) -> { |
3904 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3905 |
+ |
// }); |
3906 |
+ |
// return f.join(); |
3907 |
+ |
// } |
3908 |
+ |
|
3909 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3910 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3911 |
+ |
// stage.whenComplete((v, ex) -> { |
3912 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3913 |
+ |
// }); |
3914 |
+ |
// return f.isDone(); |
3915 |
+ |
// } |
3916 |
+ |
|
3917 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3918 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3919 |
+ |
// } |
3920 |
+ |
|
3921 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3922 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3923 |
+ |
// } |
3924 |
+ |
|
3925 |
|
} |