640 |
|
|
641 |
|
ASYNC { |
642 |
|
public void checkExecutionMode() { |
643 |
< |
assertEquals(defaultExecutorIsCommonPool, |
644 |
< |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
643 |
> |
// If tests are added that may run across different |
644 |
> |
// pools, this needs to be weakened to no-op. |
645 |
> |
ForkJoinPool p = ForkJoinTask.getPool(); |
646 |
> |
assertTrue(p == null || |
647 |
> |
(defaultExecutorIsCommonPool && |
648 |
> |
p == ForkJoinPool.commonPool())); |
649 |
|
} |
650 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
651 |
|
return CompletableFuture.runAsync(a); |
880 |
|
assertEquals(1, a.get()); |
881 |
|
}} |
882 |
|
|
883 |
+ |
/** |
884 |
+ |
* If an "exceptionally action" throws an exception, it completes |
885 |
+ |
* exceptionally with that exception |
886 |
+ |
*/ |
887 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
888 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
889 |
|
{ |
902 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
903 |
|
|
904 |
|
checkCompletedWithWrappedException(g, ex2); |
905 |
+ |
checkCompletedExceptionally(f, ex1); |
906 |
|
assertEquals(1, a.get()); |
907 |
|
}} |
908 |
|
|
920 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
921 |
|
final CompletableFuture<Integer> g = m.whenComplete |
922 |
|
(f, |
923 |
< |
(Integer x, Throwable t) -> { |
923 |
> |
(Integer result, Throwable t) -> { |
924 |
|
m.checkExecutionMode(); |
925 |
< |
threadAssertSame(x, v1); |
925 |
> |
threadAssertSame(result, v1); |
926 |
|
threadAssertNull(t); |
927 |
|
a.getAndIncrement(); |
928 |
|
}); |
947 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
948 |
|
final CompletableFuture<Integer> g = m.whenComplete |
949 |
|
(f, |
950 |
< |
(Integer x, Throwable t) -> { |
950 |
> |
(Integer result, Throwable t) -> { |
951 |
|
m.checkExecutionMode(); |
952 |
< |
threadAssertNull(x); |
952 |
> |
threadAssertNull(result); |
953 |
|
threadAssertSame(t, ex); |
954 |
|
a.getAndIncrement(); |
955 |
|
}); |
974 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
975 |
|
final CompletableFuture<Integer> g = m.whenComplete |
976 |
|
(f, |
977 |
< |
(Integer x, Throwable t) -> { |
977 |
> |
(Integer result, Throwable t) -> { |
978 |
|
m.checkExecutionMode(); |
979 |
< |
threadAssertNull(x); |
979 |
> |
threadAssertNull(result); |
980 |
|
threadAssertTrue(t instanceof CancellationException); |
981 |
|
a.getAndIncrement(); |
982 |
|
}); |
991 |
|
* If a whenComplete action throws an exception when triggered by |
992 |
|
* a normal completion, it completes exceptionally |
993 |
|
*/ |
994 |
< |
public void testWhenComplete_actionFailed() { |
994 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
995 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
996 |
|
for (ExecutionMode m : ExecutionMode.values()) |
997 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1002 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1003 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1004 |
|
(f, |
1005 |
< |
(Integer x, Throwable t) -> { |
1005 |
> |
(Integer result, Throwable t) -> { |
1006 |
|
m.checkExecutionMode(); |
1007 |
< |
threadAssertSame(x, v1); |
1007 |
> |
threadAssertSame(result, v1); |
1008 |
|
threadAssertNull(t); |
1009 |
|
a.getAndIncrement(); |
1010 |
|
throw ex; |
1019 |
|
/** |
1020 |
|
* If a whenComplete action throws an exception when triggered by |
1021 |
|
* a source completion that also throws an exception, the source |
1022 |
< |
* exception takes precedence. |
1022 |
> |
* exception takes precedence (unlike handle) |
1023 |
|
*/ |
1024 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1024 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1025 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1026 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1027 |
|
{ |
1033 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1034 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1035 |
|
(f, |
1036 |
< |
(Integer x, Throwable t) -> { |
1036 |
> |
(Integer result, Throwable t) -> { |
1037 |
|
m.checkExecutionMode(); |
1038 |
|
threadAssertSame(t, ex1); |
1039 |
< |
threadAssertNull(x); |
1039 |
> |
threadAssertNull(result); |
1040 |
|
a.getAndIncrement(); |
1041 |
|
throw ex2; |
1042 |
|
}); |
1044 |
|
|
1045 |
|
checkCompletedWithWrappedException(g, ex1); |
1046 |
|
checkCompletedExceptionally(f, ex1); |
1047 |
+ |
if (testImplementationDetails) { |
1048 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1049 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1050 |
+ |
} |
1051 |
|
assertEquals(1, a.get()); |
1052 |
|
}} |
1053 |
|
|
1065 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1066 |
|
final CompletableFuture<Integer> g = m.handle |
1067 |
|
(f, |
1068 |
< |
(Integer x, Throwable t) -> { |
1068 |
> |
(Integer result, Throwable t) -> { |
1069 |
|
m.checkExecutionMode(); |
1070 |
< |
threadAssertSame(x, v1); |
1070 |
> |
threadAssertSame(result, v1); |
1071 |
|
threadAssertNull(t); |
1072 |
|
a.getAndIncrement(); |
1073 |
|
return inc(v1); |
1094 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1095 |
|
final CompletableFuture<Integer> g = m.handle |
1096 |
|
(f, |
1097 |
< |
(Integer x, Throwable t) -> { |
1097 |
> |
(Integer result, Throwable t) -> { |
1098 |
|
m.checkExecutionMode(); |
1099 |
< |
threadAssertNull(x); |
1099 |
> |
threadAssertNull(result); |
1100 |
|
threadAssertSame(t, ex); |
1101 |
|
a.getAndIncrement(); |
1102 |
|
return v1; |
1123 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1124 |
|
final CompletableFuture<Integer> g = m.handle |
1125 |
|
(f, |
1126 |
< |
(Integer x, Throwable t) -> { |
1126 |
> |
(Integer result, Throwable t) -> { |
1127 |
|
m.checkExecutionMode(); |
1128 |
< |
threadAssertNull(x); |
1128 |
> |
threadAssertNull(result); |
1129 |
|
threadAssertTrue(t instanceof CancellationException); |
1130 |
|
a.getAndIncrement(); |
1131 |
|
return v1; |
1138 |
|
}} |
1139 |
|
|
1140 |
|
/** |
1141 |
< |
* handle result completes exceptionally if action does |
1141 |
> |
* If a "handle action" throws an exception when triggered by |
1142 |
> |
* a normal completion, it completes exceptionally |
1143 |
|
*/ |
1144 |
< |
public void testHandle_sourceFailedActionFailed() { |
1144 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1145 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1146 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1147 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1148 |
|
{ |
1149 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1150 |
|
final AtomicInteger a = new AtomicInteger(0); |
1151 |
< |
final CFException ex1 = new CFException(); |
1152 |
< |
final CFException ex2 = new CFException(); |
1138 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1151 |
> |
final CFException ex = new CFException(); |
1152 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1153 |
|
final CompletableFuture<Integer> g = m.handle |
1154 |
|
(f, |
1155 |
< |
(Integer x, Throwable t) -> { |
1155 |
> |
(Integer result, Throwable t) -> { |
1156 |
|
m.checkExecutionMode(); |
1157 |
< |
threadAssertNull(x); |
1158 |
< |
threadAssertSame(ex1, t); |
1157 |
> |
threadAssertSame(result, v1); |
1158 |
> |
threadAssertNull(t); |
1159 |
|
a.getAndIncrement(); |
1160 |
< |
throw ex2; |
1160 |
> |
throw ex; |
1161 |
|
}); |
1162 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1162 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1163 |
|
|
1164 |
< |
checkCompletedWithWrappedException(g, ex2); |
1165 |
< |
checkCompletedExceptionally(f, ex1); |
1164 |
> |
checkCompletedWithWrappedException(g, ex); |
1165 |
> |
checkCompletedNormally(f, v1); |
1166 |
|
assertEquals(1, a.get()); |
1167 |
|
}} |
1168 |
|
|
1169 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1170 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1169 |
> |
/** |
1170 |
> |
* If a "handle action" throws an exception when triggered by |
1171 |
> |
* a source completion that also throws an exception, the action |
1172 |
> |
* exception takes precedence (unlike whenComplete) |
1173 |
> |
*/ |
1174 |
> |
public void testHandle_sourceFailedActionFailed() { |
1175 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1176 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1176 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1177 |
|
{ |
1160 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1178 |
|
final AtomicInteger a = new AtomicInteger(0); |
1179 |
< |
final CFException ex = new CFException(); |
1180 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1179 |
> |
final CFException ex1 = new CFException(); |
1180 |
> |
final CFException ex2 = new CFException(); |
1181 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1182 |
> |
|
1183 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1184 |
|
final CompletableFuture<Integer> g = m.handle |
1185 |
|
(f, |
1186 |
< |
(Integer x, Throwable t) -> { |
1186 |
> |
(Integer result, Throwable t) -> { |
1187 |
|
m.checkExecutionMode(); |
1188 |
< |
threadAssertSame(x, v1); |
1189 |
< |
threadAssertNull(t); |
1188 |
> |
threadAssertNull(result); |
1189 |
> |
threadAssertSame(ex1, t); |
1190 |
|
a.getAndIncrement(); |
1191 |
< |
throw ex; |
1191 |
> |
throw ex2; |
1192 |
|
}); |
1193 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1193 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1194 |
|
|
1195 |
< |
checkCompletedWithWrappedException(g, ex); |
1196 |
< |
checkCompletedNormally(f, v1); |
1195 |
> |
checkCompletedWithWrappedException(g, ex2); |
1196 |
> |
checkCompletedExceptionally(f, ex1); |
1197 |
|
assertEquals(1, a.get()); |
1198 |
|
}} |
1199 |
|
|
3664 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3665 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3666 |
|
|
3667 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3667 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3668 |
|
|
3669 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3670 |
|
|
3784 |
|
} |
3785 |
|
|
3786 |
|
static class Monad { |
3787 |
< |
static class MonadError extends Error { |
3788 |
< |
public MonadError() { super("monadic zero"); } |
3787 |
> |
static class ZeroException extends RuntimeException { |
3788 |
> |
public ZeroException() { super("monadic zero"); } |
3789 |
|
} |
3790 |
|
// "return", "unit" |
3791 |
|
static <T> CompletableFuture<T> unit(T value) { |
3793 |
|
} |
3794 |
|
// monadic zero ? |
3795 |
|
static <T> CompletableFuture<T> zero() { |
3796 |
< |
return failedFuture(new MonadError()); |
3796 |
> |
return failedFuture(new ZeroException()); |
3797 |
|
} |
3798 |
|
// >=> |
3799 |
|
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3807 |
|
f.getNow(null); |
3808 |
|
throw new AssertionFailedError("should throw"); |
3809 |
|
} catch (CompletionException success) { |
3810 |
< |
assertTrue(success.getCause() instanceof MonadError); |
3810 |
> |
assertTrue(success.getCause() instanceof ZeroException); |
3811 |
|
} |
3812 |
|
} |
3813 |
|
|
3834 |
|
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3835 |
|
} |
3836 |
|
|
3837 |
< |
// Monadic "plus" |
3837 |
> |
/** Implements "monadic plus". */ |
3838 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3839 |
|
CompletableFuture<? extends T> g) { |
3840 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3841 |
|
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3842 |
< |
if (result != null) { |
3843 |
< |
if (plus.complete(result)) |
3844 |
< |
if (plus.firstFailure.get() != null) |
3842 |
> |
try { |
3843 |
> |
if (ex == null) { |
3844 |
> |
if (plus.complete(result)) |
3845 |
> |
if (plus.firstFailure.get() != null) |
3846 |
> |
plus.firstFailure.set(null); |
3847 |
> |
} |
3848 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3849 |
> |
if (plus.isDone()) |
3850 |
|
plus.firstFailure.set(null); |
3851 |
< |
} |
3852 |
< |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3853 |
< |
if (plus.isDone()) |
3854 |
< |
plus.firstFailure.set(null); |
3855 |
< |
} else { |
3856 |
< |
if (plus.completeExceptionally(ex)) |
3857 |
< |
plus.firstFailure.set(null); |
3851 |
> |
} |
3852 |
> |
else { |
3853 |
> |
// first failure has precedence |
3854 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3855 |
> |
|
3856 |
> |
// may fail with "Self-suppression not permitted" |
3857 |
> |
try { first.addSuppressed(ex); } |
3858 |
> |
catch (Exception ignored) {} |
3859 |
> |
|
3860 |
> |
plus.completeExceptionally(first); |
3861 |
> |
} |
3862 |
> |
} catch (Throwable unexpected) { |
3863 |
> |
plus.completeExceptionally(unexpected); |
3864 |
|
} |
3865 |
|
}; |
3866 |
|
f.whenComplete(action); |
3929 |
|
Monad.plus(godot, Monad.unit(5L))); |
3930 |
|
} |
3931 |
|
|
3932 |
+ |
/** |
3933 |
+ |
* A single CompletableFuture with many dependents. |
3934 |
+ |
* A demo of scalability - runtime is O(n). |
3935 |
+ |
*/ |
3936 |
+ |
public void testManyDependents() throws Throwable { |
3937 |
+ |
final int n = 1_000; |
3938 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3939 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3940 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3941 |
+ |
for (int i = 0; i < n; i++) { |
3942 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
3943 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
3944 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
3945 |
+ |
|
3946 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3947 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3948 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3949 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3950 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3951 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3952 |
+ |
|
3953 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3954 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3955 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3956 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3957 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3958 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3959 |
+ |
} |
3960 |
+ |
head.complete(null); |
3961 |
+ |
assertEquals(5 * 3 * n, count.get()); |
3962 |
+ |
} |
3963 |
+ |
|
3964 |
|
// static <U> U join(CompletionStage<U> stage) { |
3965 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
3966 |
|
// stage.whenComplete((v, ex) -> { |