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); |
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 |
|
|
3299 |
|
() -> f.obtrudeException(null), |
3300 |
|
|
3301 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3302 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3302 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3303 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3304 |
|
|
3305 |
|
() -> f.orTimeout(1L, null), |
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 (ex == 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 |
< |
} |
3856 |
< |
else { |
3857 |
< |
// first failure has precedence |
3858 |
< |
Throwable first = plus.firstFailure.getAndSet(null); |
3859 |
< |
|
3860 |
< |
// may fail with "Self-suppression not permitted" |
3861 |
< |
try { first.addSuppressed(ex); } |
3862 |
< |
catch (Exception ignored) {} |
3863 |
< |
|
3851 |
< |
plus.completeExceptionally(first); |
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) -> { |