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); |
3930 |
|
|
3931 |
|
/** |
3932 |
|
* A single CompletableFuture with many dependents. |
3933 |
+ |
* A demo of scalability - runtime is O(n). |
3934 |
|
*/ |
3935 |
|
public void testManyDependents() throws Throwable { |
3936 |
< |
final int n = 10_000; |
3936 |
> |
final int n = 1_000; |
3937 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3934 |
– |
final CompletableFuture<Void> incomplete = new CompletableFuture<>(); |
3938 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3939 |
|
final AtomicInteger count = new AtomicInteger(0); |
3940 |
|
for (int i = 0; i < n; i++) { |
3941 |
|
head.thenRun(() -> count.getAndIncrement()); |
3942 |
|
head.thenAccept((x) -> count.getAndIncrement()); |
3943 |
|
head.thenApply((x) -> count.getAndIncrement()); |
3944 |
+ |
|
3945 |
|
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3946 |
|
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3947 |
|
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3948 |
< |
head.runAfterEither(incomplete, () -> count.getAndIncrement()); |
3949 |
< |
head.acceptEither(incomplete, (x) -> count.getAndIncrement()); |
3950 |
< |
head.applyToEither(incomplete, (x) -> count.getAndIncrement()); |
3948 |
> |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3949 |
> |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3950 |
> |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3951 |
> |
|
3952 |
> |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3953 |
> |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3954 |
> |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3955 |
> |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3956 |
> |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3957 |
> |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3958 |
|
} |
3959 |
|
head.complete(null); |
3960 |
< |
assertEquals(9 * n, count.get()); |
3960 |
> |
assertEquals(5 * 3 * n, count.get()); |
3961 |
|
} |
3962 |
|
|
3963 |
|
// static <U> U join(CompletionStage<U> stage) { |