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 |
|
|
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 |
< |
|
3851 |
< |
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); |