876 |
|
assertEquals(1, a.get()); |
877 |
|
}} |
878 |
|
|
879 |
+ |
/** |
880 |
+ |
* If an "exceptionally action" throws an exception, it completes |
881 |
+ |
* exceptionally with that exception |
882 |
+ |
*/ |
883 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
884 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
885 |
|
{ |
898 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
899 |
|
|
900 |
|
checkCompletedWithWrappedException(g, ex2); |
901 |
+ |
checkCompletedExceptionally(f, ex1); |
902 |
|
assertEquals(1, a.get()); |
903 |
|
}} |
904 |
|
|
916 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
917 |
|
final CompletableFuture<Integer> g = m.whenComplete |
918 |
|
(f, |
919 |
< |
(Integer x, Throwable t) -> { |
919 |
> |
(Integer result, Throwable t) -> { |
920 |
|
m.checkExecutionMode(); |
921 |
< |
threadAssertSame(x, v1); |
921 |
> |
threadAssertSame(result, v1); |
922 |
|
threadAssertNull(t); |
923 |
|
a.getAndIncrement(); |
924 |
|
}); |
943 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
|
final CompletableFuture<Integer> g = m.whenComplete |
945 |
|
(f, |
946 |
< |
(Integer x, Throwable t) -> { |
946 |
> |
(Integer result, Throwable t) -> { |
947 |
|
m.checkExecutionMode(); |
948 |
< |
threadAssertNull(x); |
948 |
> |
threadAssertNull(result); |
949 |
|
threadAssertSame(t, ex); |
950 |
|
a.getAndIncrement(); |
951 |
|
}); |
970 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
|
final CompletableFuture<Integer> g = m.whenComplete |
972 |
|
(f, |
973 |
< |
(Integer x, Throwable t) -> { |
973 |
> |
(Integer result, Throwable t) -> { |
974 |
|
m.checkExecutionMode(); |
975 |
< |
threadAssertNull(x); |
975 |
> |
threadAssertNull(result); |
976 |
|
threadAssertTrue(t instanceof CancellationException); |
977 |
|
a.getAndIncrement(); |
978 |
|
}); |
998 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
999 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
|
(f, |
1001 |
< |
(Integer x, Throwable t) -> { |
1001 |
> |
(Integer result, Throwable t) -> { |
1002 |
|
m.checkExecutionMode(); |
1003 |
< |
threadAssertSame(x, v1); |
1003 |
> |
threadAssertSame(result, v1); |
1004 |
|
threadAssertNull(t); |
1005 |
|
a.getAndIncrement(); |
1006 |
|
throw ex; |
1029 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1030 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1031 |
|
(f, |
1032 |
< |
(Integer x, Throwable t) -> { |
1032 |
> |
(Integer result, Throwable t) -> { |
1033 |
|
m.checkExecutionMode(); |
1034 |
|
threadAssertSame(t, ex1); |
1035 |
< |
threadAssertNull(x); |
1035 |
> |
threadAssertNull(result); |
1036 |
|
a.getAndIncrement(); |
1037 |
|
throw ex2; |
1038 |
|
}); |
1040 |
|
|
1041 |
|
checkCompletedWithWrappedException(g, ex1); |
1042 |
|
checkCompletedExceptionally(f, ex1); |
1043 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1044 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1045 |
|
assertEquals(1, a.get()); |
1046 |
|
}} |
1047 |
|
|
1059 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1060 |
|
final CompletableFuture<Integer> g = m.handle |
1061 |
|
(f, |
1062 |
< |
(Integer x, Throwable t) -> { |
1062 |
> |
(Integer result, Throwable t) -> { |
1063 |
|
m.checkExecutionMode(); |
1064 |
< |
threadAssertSame(x, v1); |
1064 |
> |
threadAssertSame(result, v1); |
1065 |
|
threadAssertNull(t); |
1066 |
|
a.getAndIncrement(); |
1067 |
|
return inc(v1); |
1088 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1089 |
|
final CompletableFuture<Integer> g = m.handle |
1090 |
|
(f, |
1091 |
< |
(Integer x, Throwable t) -> { |
1091 |
> |
(Integer result, Throwable t) -> { |
1092 |
|
m.checkExecutionMode(); |
1093 |
< |
threadAssertNull(x); |
1093 |
> |
threadAssertNull(result); |
1094 |
|
threadAssertSame(t, ex); |
1095 |
|
a.getAndIncrement(); |
1096 |
|
return v1; |
1117 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1118 |
|
final CompletableFuture<Integer> g = m.handle |
1119 |
|
(f, |
1120 |
< |
(Integer x, Throwable t) -> { |
1120 |
> |
(Integer result, Throwable t) -> { |
1121 |
|
m.checkExecutionMode(); |
1122 |
< |
threadAssertNull(x); |
1122 |
> |
threadAssertNull(result); |
1123 |
|
threadAssertTrue(t instanceof CancellationException); |
1124 |
|
a.getAndIncrement(); |
1125 |
|
return v1; |
1146 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1147 |
|
final CompletableFuture<Integer> g = m.handle |
1148 |
|
(f, |
1149 |
< |
(Integer x, Throwable t) -> { |
1149 |
> |
(Integer result, Throwable t) -> { |
1150 |
|
m.checkExecutionMode(); |
1151 |
< |
threadAssertSame(x, v1); |
1151 |
> |
threadAssertSame(result, v1); |
1152 |
|
threadAssertNull(t); |
1153 |
|
a.getAndIncrement(); |
1154 |
|
throw ex; |
1177 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1178 |
|
final CompletableFuture<Integer> g = m.handle |
1179 |
|
(f, |
1180 |
< |
(Integer x, Throwable t) -> { |
1180 |
> |
(Integer result, Throwable t) -> { |
1181 |
|
m.checkExecutionMode(); |
1182 |
< |
threadAssertNull(x); |
1182 |
> |
threadAssertNull(result); |
1183 |
|
threadAssertSame(ex1, t); |
1184 |
|
a.getAndIncrement(); |
1185 |
|
throw ex2; |
3658 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3659 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3660 |
|
|
3661 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3661 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3662 |
|
|
3663 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3664 |
|
|
3828 |
|
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3829 |
|
} |
3830 |
|
|
3831 |
< |
// Monadic "plus" |
3831 |
> |
/** Implements "monadic plus". */ |
3832 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3833 |
|
CompletableFuture<? extends T> g) { |
3834 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3835 |
|
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3836 |
< |
if (ex == null) { |
3837 |
< |
if (plus.complete(result)) |
3838 |
< |
if (plus.firstFailure.get() != null) |
3836 |
> |
try { |
3837 |
> |
if (ex == null) { |
3838 |
> |
if (plus.complete(result)) |
3839 |
> |
if (plus.firstFailure.get() != null) |
3840 |
> |
plus.firstFailure.set(null); |
3841 |
> |
} |
3842 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3843 |
> |
if (plus.isDone()) |
3844 |
|
plus.firstFailure.set(null); |
3845 |
< |
} |
3846 |
< |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3847 |
< |
if (plus.isDone()) |
3848 |
< |
plus.firstFailure.set(null); |
3849 |
< |
} |
3850 |
< |
else { |
3851 |
< |
// first failure has precedence |
3852 |
< |
Throwable first = plus.firstFailure.getAndSet(null); |
3853 |
< |
|
3854 |
< |
// may fail with "Self-suppression not permitted" |
3855 |
< |
try { first.addSuppressed(ex); } |
3856 |
< |
catch (Exception ignored) {} |
3857 |
< |
|
3846 |
< |
plus.completeExceptionally(first); |
3845 |
> |
} |
3846 |
> |
else { |
3847 |
> |
// first failure has precedence |
3848 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3849 |
> |
|
3850 |
> |
// may fail with "Self-suppression not permitted" |
3851 |
> |
try { first.addSuppressed(ex); } |
3852 |
> |
catch (Exception ignored) {} |
3853 |
> |
|
3854 |
> |
plus.completeExceptionally(first); |
3855 |
> |
} |
3856 |
> |
} catch (Throwable unexpected) { |
3857 |
> |
plus.completeExceptionally(unexpected); |
3858 |
|
} |
3859 |
|
}; |
3860 |
|
f.whenComplete(action); |