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 |
|
|
1040 |
|
|
1041 |
|
checkCompletedWithWrappedException(g, ex1); |
1042 |
|
checkCompletedExceptionally(f, ex1); |
1043 |
+ |
if (testImplementationDetails) { |
1044 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1045 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1046 |
+ |
} |
1047 |
|
assertEquals(1, a.get()); |
1048 |
|
}} |
1049 |
|
|
3830 |
|
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3831 |
|
} |
3832 |
|
|
3833 |
< |
// Monadic "plus" |
3833 |
> |
/** Implements "monadic plus". */ |
3834 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3835 |
|
CompletableFuture<? extends T> g) { |
3836 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3837 |
|
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3838 |
< |
if (ex == null) { |
3839 |
< |
if (plus.complete(result)) |
3840 |
< |
if (plus.firstFailure.get() != null) |
3838 |
> |
try { |
3839 |
> |
if (ex == null) { |
3840 |
> |
if (plus.complete(result)) |
3841 |
> |
if (plus.firstFailure.get() != null) |
3842 |
> |
plus.firstFailure.set(null); |
3843 |
> |
} |
3844 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3845 |
> |
if (plus.isDone()) |
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 { |
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 |
< |
|
3846 |
< |
plus.completeExceptionally(first); |
3847 |
> |
} |
3848 |
> |
else { |
3849 |
> |
// first failure has precedence |
3850 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3851 |
> |
|
3852 |
> |
// may fail with "Self-suppression not permitted" |
3853 |
> |
try { first.addSuppressed(ex); } |
3854 |
> |
catch (Exception ignored) {} |
3855 |
> |
|
3856 |
> |
plus.completeExceptionally(first); |
3857 |
> |
} |
3858 |
> |
} catch (Throwable unexpected) { |
3859 |
> |
plus.completeExceptionally(unexpected); |
3860 |
|
} |
3861 |
|
}; |
3862 |
|
f.whenComplete(action); |
3925 |
|
Monad.plus(godot, Monad.unit(5L))); |
3926 |
|
} |
3927 |
|
|
3928 |
+ |
/** |
3929 |
+ |
* A single CompletableFuture with many dependents. |
3930 |
+ |
* A demo of scalability - runtime is O(n). |
3931 |
+ |
*/ |
3932 |
+ |
public void testManyDependents() throws Throwable { |
3933 |
+ |
final int n = 1_000; |
3934 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3935 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3936 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3937 |
+ |
for (int i = 0; i < n; i++) { |
3938 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
3939 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
3940 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
3941 |
+ |
|
3942 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3943 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3944 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3945 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3946 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3947 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3948 |
+ |
|
3949 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3950 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3951 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3952 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3953 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3954 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3955 |
+ |
} |
3956 |
+ |
head.complete(null); |
3957 |
+ |
assertEquals(5 * 3 * n, count.get()); |
3958 |
+ |
} |
3959 |
+ |
|
3960 |
|
// static <U> U join(CompletionStage<U> stage) { |
3961 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
3962 |
|
// stage.whenComplete((v, ex) -> { |