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 }) |
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 }) |
1010 |
|
/** |
1011 |
|
* If a whenComplete action throws an exception when triggered by |
1012 |
|
* a source completion that also throws an exception, the source |
1013 |
< |
* exception takes precedence. |
1013 |
> |
* exception takes precedence (unlike handle) |
1014 |
|
*/ |
1015 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1015 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1016 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1017 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1018 |
|
{ |
1125 |
|
}} |
1126 |
|
|
1127 |
|
/** |
1128 |
< |
* handle result completes exceptionally if action does |
1128 |
> |
* If a "handle action" throws an exception when triggered by |
1129 |
> |
* a normal completion, it completes exceptionally |
1130 |
|
*/ |
1131 |
< |
public void testHandle_sourceFailedActionFailed() { |
1131 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1132 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1133 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1134 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1135 |
|
{ |
1136 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1137 |
|
final AtomicInteger a = new AtomicInteger(0); |
1138 |
< |
final CFException ex1 = new CFException(); |
1139 |
< |
final CFException ex2 = new CFException(); |
1126 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1138 |
> |
final CFException ex = new CFException(); |
1139 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1140 |
|
final CompletableFuture<Integer> g = m.handle |
1141 |
|
(f, |
1142 |
|
(Integer x, Throwable t) -> { |
1143 |
|
m.checkExecutionMode(); |
1144 |
< |
threadAssertNull(x); |
1145 |
< |
threadAssertSame(ex1, t); |
1144 |
> |
threadAssertSame(x, v1); |
1145 |
> |
threadAssertNull(t); |
1146 |
|
a.getAndIncrement(); |
1147 |
< |
throw ex2; |
1147 |
> |
throw ex; |
1148 |
|
}); |
1149 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1149 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1150 |
|
|
1151 |
< |
checkCompletedWithWrappedException(g, ex2); |
1152 |
< |
checkCompletedExceptionally(f, ex1); |
1151 |
> |
checkCompletedWithWrappedException(g, ex); |
1152 |
> |
checkCompletedNormally(f, v1); |
1153 |
|
assertEquals(1, a.get()); |
1154 |
|
}} |
1155 |
|
|
1156 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1157 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1156 |
> |
/** |
1157 |
> |
* If a "handle action" throws an exception when triggered by |
1158 |
> |
* a source completion that also throws an exception, the action |
1159 |
> |
* exception takes precedence (unlike whenComplete) |
1160 |
> |
*/ |
1161 |
> |
public void testHandle_sourceFailedActionFailed() { |
1162 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1163 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1163 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1164 |
|
{ |
1148 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1165 |
|
final AtomicInteger a = new AtomicInteger(0); |
1166 |
< |
final CFException ex = new CFException(); |
1167 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1166 |
> |
final CFException ex1 = new CFException(); |
1167 |
> |
final CFException ex2 = new CFException(); |
1168 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1169 |
> |
|
1170 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1171 |
|
final CompletableFuture<Integer> g = m.handle |
1172 |
|
(f, |
1173 |
|
(Integer x, Throwable t) -> { |
1174 |
|
m.checkExecutionMode(); |
1175 |
< |
threadAssertSame(x, v1); |
1176 |
< |
threadAssertNull(t); |
1175 |
> |
threadAssertNull(x); |
1176 |
> |
threadAssertSame(ex1, t); |
1177 |
|
a.getAndIncrement(); |
1178 |
< |
throw ex; |
1178 |
> |
throw ex2; |
1179 |
|
}); |
1180 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1180 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1181 |
|
|
1182 |
< |
checkCompletedWithWrappedException(g, ex); |
1183 |
< |
checkCompletedNormally(f, v1); |
1182 |
> |
checkCompletedWithWrappedException(g, ex2); |
1183 |
> |
checkCompletedExceptionally(f, ex1); |
1184 |
|
assertEquals(1, a.get()); |
1185 |
|
}} |
1186 |
|
|
3586 |
|
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3587 |
|
} |
3588 |
|
|
3570 |
– |
/** |
3571 |
– |
* delayedExecutor returns an executor that delays submission |
3572 |
– |
*/ |
3589 |
|
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3590 |
|
long timeoutMillis = timeoutMillis(); |
3591 |
|
// Use an "unreasonably long" long timeout to catch lingering threads |
3707 |
|
} |
3708 |
|
}} |
3709 |
|
|
3710 |
+ |
/** |
3711 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3712 |
+ |
*/ |
3713 |
+ |
public void testMinimalCompletionStage_minimality() { |
3714 |
+ |
if (!testImplementationDetails) return; |
3715 |
+ |
Function<Method, String> toSignature = |
3716 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3717 |
+ |
Predicate<Method> isNotStatic = |
3718 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3719 |
+ |
List<Method> minimalMethods = |
3720 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3721 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3722 |
+ |
.filter(isNotStatic) |
3723 |
+ |
.collect(Collectors.toList()); |
3724 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3725 |
+ |
String[] signatureWhitelist = { |
3726 |
+ |
"newIncompleteFuture[]", |
3727 |
+ |
"defaultExecutor[]", |
3728 |
+ |
"minimalCompletionStage[]", |
3729 |
+ |
"copy[]", |
3730 |
+ |
}; |
3731 |
+ |
Set<String> permittedMethodSignatures = |
3732 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3733 |
+ |
Stream.of(signatureWhitelist)) |
3734 |
+ |
.collect(Collectors.toSet()); |
3735 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3736 |
+ |
.filter(isNotStatic) |
3737 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3738 |
+ |
.collect(Collectors.toList()); |
3739 |
+ |
|
3740 |
+ |
CompletionStage<Integer> minimalStage = |
3741 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3742 |
+ |
|
3743 |
+ |
List<Method> bugs = new ArrayList<>(); |
3744 |
+ |
for (Method method : allMethods) { |
3745 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3746 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3747 |
+ |
// Manufacture boxed primitives for primitive params |
3748 |
+ |
for (int i = 0; i < args.length; i++) { |
3749 |
+ |
Class<?> type = parameterTypes[i]; |
3750 |
+ |
if (parameterTypes[i] == boolean.class) |
3751 |
+ |
args[i] = false; |
3752 |
+ |
else if (parameterTypes[i] == int.class) |
3753 |
+ |
args[i] = 0; |
3754 |
+ |
else if (parameterTypes[i] == long.class) |
3755 |
+ |
args[i] = 0L; |
3756 |
+ |
} |
3757 |
+ |
try { |
3758 |
+ |
method.invoke(minimalStage, args); |
3759 |
+ |
bugs.add(method); |
3760 |
+ |
} |
3761 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3762 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3763 |
+ |
bugs.add(method); |
3764 |
+ |
// expected.getCause().printStackTrace(); |
3765 |
+ |
} |
3766 |
+ |
} |
3767 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3768 |
+ |
} |
3769 |
+ |
if (!bugs.isEmpty()) |
3770 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3771 |
+ |
} |
3772 |
+ |
|
3773 |
+ |
static class Monad { |
3774 |
+ |
static class ZeroException extends RuntimeException { |
3775 |
+ |
public ZeroException() { super("monadic zero"); } |
3776 |
+ |
} |
3777 |
+ |
// "return", "unit" |
3778 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3779 |
+ |
return completedFuture(value); |
3780 |
+ |
} |
3781 |
+ |
// monadic zero ? |
3782 |
+ |
static <T> CompletableFuture<T> zero() { |
3783 |
+ |
return failedFuture(new ZeroException()); |
3784 |
+ |
} |
3785 |
+ |
// >=> |
3786 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3787 |
+ |
(Function<T, CompletableFuture<U>> f, |
3788 |
+ |
Function<U, CompletableFuture<V>> g) { |
3789 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3790 |
+ |
} |
3791 |
+ |
|
3792 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3793 |
+ |
try { |
3794 |
+ |
f.getNow(null); |
3795 |
+ |
throw new AssertionFailedError("should throw"); |
3796 |
+ |
} catch (CompletionException success) { |
3797 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3798 |
+ |
} |
3799 |
+ |
} |
3800 |
+ |
|
3801 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3802 |
+ |
CompletableFuture<T> g) { |
3803 |
+ |
T fval = null, gval = null; |
3804 |
+ |
Throwable fex = null, gex = null; |
3805 |
+ |
|
3806 |
+ |
try { fval = f.get(); } |
3807 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3808 |
+ |
catch (Throwable ex) { fex = ex; } |
3809 |
+ |
|
3810 |
+ |
try { gval = g.get(); } |
3811 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3812 |
+ |
catch (Throwable ex) { gex = ex; } |
3813 |
+ |
|
3814 |
+ |
if (fex != null || gex != null) |
3815 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3816 |
+ |
else |
3817 |
+ |
assertEquals(fval, gval); |
3818 |
+ |
} |
3819 |
+ |
|
3820 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3821 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3822 |
+ |
} |
3823 |
+ |
|
3824 |
+ |
// Monadic "plus" |
3825 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3826 |
+ |
CompletableFuture<? extends T> g) { |
3827 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3828 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3829 |
+ |
if (ex == null) { |
3830 |
+ |
if (plus.complete(result)) |
3831 |
+ |
if (plus.firstFailure.get() != null) |
3832 |
+ |
plus.firstFailure.set(null); |
3833 |
+ |
} |
3834 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3835 |
+ |
if (plus.isDone()) |
3836 |
+ |
plus.firstFailure.set(null); |
3837 |
+ |
} |
3838 |
+ |
else { |
3839 |
+ |
// first failure has precedence |
3840 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
3841 |
+ |
|
3842 |
+ |
// may fail with "Self-suppression not permitted" |
3843 |
+ |
try { first.addSuppressed(ex); } |
3844 |
+ |
catch (Exception ignored) {} |
3845 |
+ |
|
3846 |
+ |
plus.completeExceptionally(first); |
3847 |
+ |
} |
3848 |
+ |
}; |
3849 |
+ |
f.whenComplete(action); |
3850 |
+ |
g.whenComplete(action); |
3851 |
+ |
return plus; |
3852 |
+ |
} |
3853 |
+ |
} |
3854 |
+ |
|
3855 |
+ |
/** |
3856 |
+ |
* CompletableFuture is an additive monad - sort of. |
3857 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3858 |
+ |
*/ |
3859 |
+ |
public void testAdditiveMonad() throws Throwable { |
3860 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3861 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3862 |
+ |
|
3863 |
+ |
// Some mutually non-commutative functions |
3864 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3865 |
+ |
= (x) -> Monad.unit(3 * x); |
3866 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3867 |
+ |
= (x) -> Monad.unit(x + 1); |
3868 |
+ |
|
3869 |
+ |
// unit is a right identity: m >>= unit === m |
3870 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3871 |
+ |
inc.apply(5L)); |
3872 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3873 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3874 |
+ |
inc.apply(5L)); |
3875 |
+ |
|
3876 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3877 |
+ |
Monad.assertFutureEquals( |
3878 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3879 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3880 |
+ |
|
3881 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3882 |
+ |
|
3883 |
+ |
// zero is a monadic zero |
3884 |
+ |
Monad.assertZero(zero); |
3885 |
+ |
|
3886 |
+ |
// left zero: zero >>= f === zero |
3887 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3888 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3889 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3890 |
+ |
|
3891 |
+ |
// f plus zero === f |
3892 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3893 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3894 |
+ |
// zero plus f === f |
3895 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3896 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3897 |
+ |
// zero plus zero === zero |
3898 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3899 |
+ |
{ |
3900 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3901 |
+ |
Monad.unit(8L)); |
3902 |
+ |
// non-determinism |
3903 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3904 |
+ |
} |
3905 |
+ |
|
3906 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3907 |
+ |
// f plus godot === f (doesn't wait for godot) |
3908 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3909 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3910 |
+ |
// godot plus f === f (doesn't wait for godot) |
3911 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3912 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3913 |
+ |
} |
3914 |
+ |
|
3915 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3916 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3917 |
+ |
// stage.whenComplete((v, ex) -> { |
3918 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3919 |
+ |
// }); |
3920 |
+ |
// return f.join(); |
3921 |
+ |
// } |
3922 |
+ |
|
3923 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3924 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3925 |
+ |
// stage.whenComplete((v, ex) -> { |
3926 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3927 |
+ |
// }); |
3928 |
+ |
// return f.isDone(); |
3929 |
+ |
// } |
3930 |
+ |
|
3931 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3932 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3933 |
+ |
// } |
3934 |
+ |
|
3935 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3936 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3937 |
+ |
// } |
3938 |
+ |
|
3939 |
|
} |