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 |
|
|
3295 |
|
() -> f.obtrudeException(null), |
3296 |
|
|
3297 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3298 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3298 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3299 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3300 |
|
|
3301 |
|
() -> f.orTimeout(1L, null), |
3525 |
|
long timeoutMillis = timeoutMillis(); |
3526 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3527 |
|
long startTime = System.nanoTime(); |
3528 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3528 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3529 |
|
checkCompletedWithTimeoutException(f); |
3530 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3531 |
|
} |
3540 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3541 |
|
long startTime = System.nanoTime(); |
3542 |
|
f.complete(v1); |
3543 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3544 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3543 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3544 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3545 |
|
g.complete(v1); |
3546 |
|
checkCompletedNormally(f, v1); |
3547 |
|
checkCompletedNormally(g, v1); |
3556 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3557 |
|
} |
3558 |
|
|
3559 |
+ |
/** |
3560 |
+ |
* completeOnTimeout completes with given value if not complete |
3561 |
+ |
*/ |
3562 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3563 |
|
long timeoutMillis = timeoutMillis(); |
3564 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3565 |
|
long startTime = System.nanoTime(); |
3566 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3566 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3567 |
|
assertSame(v, f.join()); |
3568 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3569 |
|
f.complete(99); // should have no effect |
3580 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3581 |
|
long startTime = System.nanoTime(); |
3582 |
|
f.complete(v1); |
3583 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3584 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3583 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3584 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3585 |
|
g.complete(v1); |
3586 |
|
checkCompletedNormally(f, v1); |
3587 |
|
checkCompletedNormally(g, v1); |
3632 |
|
//--- tests of implementation details; not part of official tck --- |
3633 |
|
|
3634 |
|
Object resultOf(CompletableFuture<?> f) { |
3635 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3636 |
+ |
if (sm != null) { |
3637 |
+ |
try { |
3638 |
+ |
System.setSecurityManager(null); |
3639 |
+ |
} catch (SecurityException giveUp) { |
3640 |
+ |
return "Reflection not available"; |
3641 |
+ |
} |
3642 |
+ |
} |
3643 |
+ |
|
3644 |
|
try { |
3645 |
|
java.lang.reflect.Field resultField |
3646 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3647 |
|
resultField.setAccessible(true); |
3648 |
|
return resultField.get(f); |
3649 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3649 |
> |
} catch (Throwable t) { |
3650 |
> |
throw new AssertionError(t); |
3651 |
> |
} finally { |
3652 |
> |
if (sm != null) System.setSecurityManager(sm); |
3653 |
> |
} |
3654 |
|
} |
3655 |
|
|
3656 |
|
public void testExceptionPropagationReusesResultObject() { |
3846 |
|
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3847 |
|
} |
3848 |
|
|
3849 |
< |
// Monadic "plus" |
3849 |
> |
/** Implements "monadic plus". */ |
3850 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3851 |
|
CompletableFuture<? extends T> g) { |
3852 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3853 |
|
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3854 |
< |
if (ex == null) { |
3855 |
< |
if (plus.complete(result)) |
3856 |
< |
if (plus.firstFailure.get() != null) |
3854 |
> |
try { |
3855 |
> |
if (ex == null) { |
3856 |
> |
if (plus.complete(result)) |
3857 |
> |
if (plus.firstFailure.get() != null) |
3858 |
> |
plus.firstFailure.set(null); |
3859 |
> |
} |
3860 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3861 |
> |
if (plus.isDone()) |
3862 |
|
plus.firstFailure.set(null); |
3863 |
< |
} |
3864 |
< |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3865 |
< |
if (plus.isDone()) |
3866 |
< |
plus.firstFailure.set(null); |
3867 |
< |
} |
3868 |
< |
else { |
3869 |
< |
// first failure has precedence |
3870 |
< |
Throwable first = plus.firstFailure.getAndSet(null); |
3871 |
< |
|
3872 |
< |
// may fail with "Self-suppression not permitted" |
3873 |
< |
try { first.addSuppressed(ex); } |
3874 |
< |
catch (Exception ignored) {} |
3875 |
< |
|
3851 |
< |
plus.completeExceptionally(first); |
3863 |
> |
} |
3864 |
> |
else { |
3865 |
> |
// first failure has precedence |
3866 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
3867 |
> |
|
3868 |
> |
// may fail with "Self-suppression not permitted" |
3869 |
> |
try { first.addSuppressed(ex); } |
3870 |
> |
catch (Exception ignored) {} |
3871 |
> |
|
3872 |
> |
plus.completeExceptionally(first); |
3873 |
> |
} |
3874 |
> |
} catch (Throwable unexpected) { |
3875 |
> |
plus.completeExceptionally(unexpected); |
3876 |
|
} |
3877 |
|
}; |
3878 |
|
f.whenComplete(action); |
3941 |
|
Monad.plus(godot, Monad.unit(5L))); |
3942 |
|
} |
3943 |
|
|
3944 |
+ |
/** |
3945 |
+ |
* A single CompletableFuture with many dependents. |
3946 |
+ |
* A demo of scalability - runtime is O(n). |
3947 |
+ |
*/ |
3948 |
+ |
public void testManyDependents() throws Throwable { |
3949 |
+ |
final int n = 1_000; |
3950 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3951 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3952 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3953 |
+ |
for (int i = 0; i < n; i++) { |
3954 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
3955 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
3956 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
3957 |
+ |
|
3958 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3959 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3960 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3961 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3962 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3963 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3964 |
+ |
|
3965 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3966 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3967 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3968 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3969 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3970 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3971 |
+ |
} |
3972 |
+ |
head.complete(null); |
3973 |
+ |
assertEquals(5 * 3 * n, count.get()); |
3974 |
+ |
} |
3975 |
+ |
|
3976 |
|
// static <U> U join(CompletionStage<U> stage) { |
3977 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
3978 |
|
// stage.whenComplete((v, ex) -> { |