21 |
|
import java.util.concurrent.ForkJoinPool; |
22 |
|
import java.util.concurrent.ForkJoinTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
+ |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import java.util.function.BiConsumer; |
28 |
|
import java.util.function.BiFunction; |
29 |
|
import java.util.function.Consumer; |
47 |
|
void checkIncomplete(CompletableFuture<?> f) { |
48 |
|
assertFalse(f.isDone()); |
49 |
|
assertFalse(f.isCancelled()); |
50 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
50 |
> |
assertTrue(f.toString().contains("Not completed")); |
51 |
|
try { |
52 |
|
assertNull(f.getNow(null)); |
53 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
148 |
|
} |
149 |
|
|
150 |
+ |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
151 |
+ |
long startTime = System.nanoTime(); |
152 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
153 |
+ |
try { |
154 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
155 |
+ |
shouldThrow(); |
156 |
+ |
} catch (ExecutionException ex) { |
157 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
158 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
160 |
+ |
|
161 |
+ |
try { |
162 |
+ |
f.join(); |
163 |
+ |
shouldThrow(); |
164 |
+ |
} catch (Throwable ex) { |
165 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
166 |
+ |
} |
167 |
+ |
|
168 |
+ |
try { |
169 |
+ |
f.getNow(null); |
170 |
+ |
shouldThrow(); |
171 |
+ |
} catch (Throwable ex) { |
172 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
try { |
176 |
+ |
f.get(); |
177 |
+ |
shouldThrow(); |
178 |
+ |
} catch (ExecutionException ex) { |
179 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
+ |
|
182 |
+ |
assertTrue(f.isDone()); |
183 |
+ |
assertFalse(f.isCancelled()); |
184 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
185 |
+ |
} |
186 |
+ |
|
187 |
|
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
188 |
|
Throwable ex) { |
189 |
|
checkCompletedExceptionallyWithRootCause(f, ex); |
3333 |
|
() -> f.exceptionally(null), |
3334 |
|
|
3335 |
|
() -> f.handle(null), |
3297 |
– |
|
3336 |
|
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3337 |
|
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3338 |
|
() -> CompletableFuture.allOf(f, null), |
3358 |
|
assertSame(f, f.toCompletableFuture()); |
3359 |
|
} |
3360 |
|
|
3361 |
+ |
// jdk9 |
3362 |
+ |
|
3363 |
+ |
/** |
3364 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3365 |
+ |
*/ |
3366 |
+ |
public void testNewIncompleteFuture() { |
3367 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3368 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3369 |
+ |
checkIncomplete(f); |
3370 |
+ |
checkIncomplete(g); |
3371 |
+ |
} |
3372 |
+ |
|
3373 |
+ |
/** |
3374 |
+ |
* completedStage returns a completed CompletionStage |
3375 |
+ |
*/ |
3376 |
+ |
public void testCompletedStage() { |
3377 |
+ |
AtomicInteger x = new AtomicInteger(); |
3378 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3379 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3380 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3381 |
+ |
assertEquals(x.get(), 1); |
3382 |
+ |
assertNull(r.get()); |
3383 |
+ |
} |
3384 |
+ |
|
3385 |
+ |
/** |
3386 |
+ |
* defaultExecutor by default returns the commonPool if |
3387 |
+ |
* it supports at least one thread. |
3388 |
+ |
*/ |
3389 |
+ |
public void testDefaultExecutor() { |
3390 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3391 |
+ |
Executor e = f.defaultExecutor(); |
3392 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3393 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 0) |
3394 |
+ |
assertSame(e, c); |
3395 |
+ |
} |
3396 |
+ |
|
3397 |
+ |
/** |
3398 |
+ |
* failedFuture returns a CompletableFuture completed |
3399 |
+ |
* exceptionally with the given Exception |
3400 |
+ |
*/ |
3401 |
+ |
public void testFailedFuture() { |
3402 |
+ |
CFException ex = new CFException(); |
3403 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3404 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
3405 |
+ |
} |
3406 |
+ |
|
3407 |
+ |
/** |
3408 |
+ |
* failedFuture(null) throws NPE |
3409 |
+ |
*/ |
3410 |
+ |
public void testFailedFuture2() { |
3411 |
+ |
try { |
3412 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3413 |
+ |
shouldThrow(); |
3414 |
+ |
} catch (NullPointerException success) {} |
3415 |
+ |
} |
3416 |
+ |
|
3417 |
+ |
/** |
3418 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3419 |
+ |
* with the same value, when source is. |
3420 |
+ |
*/ |
3421 |
+ |
public void testCopy() { |
3422 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3423 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3424 |
+ |
checkIncomplete(f); |
3425 |
+ |
checkIncomplete(g); |
3426 |
+ |
f.complete(1); |
3427 |
+ |
checkCompletedNormally(f, 1); |
3428 |
+ |
checkCompletedNormally(g, 1); |
3429 |
+ |
} |
3430 |
+ |
|
3431 |
+ |
/** |
3432 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3433 |
+ |
* when source is. |
3434 |
+ |
*/ |
3435 |
+ |
public void testCopy2() { |
3436 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3437 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3438 |
+ |
checkIncomplete(f); |
3439 |
+ |
checkIncomplete(g); |
3440 |
+ |
CFException ex = new CFException(); |
3441 |
+ |
f.completeExceptionally(ex); |
3442 |
+ |
checkCompletedExceptionally(f, ex); |
3443 |
+ |
checkCompletedWithWrappedCFException(g); |
3444 |
+ |
} |
3445 |
+ |
|
3446 |
+ |
/** |
3447 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3448 |
+ |
* completed normally, with the same value, when source is. |
3449 |
+ |
*/ |
3450 |
+ |
public void testMinimalCompletionStage() { |
3451 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3452 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3453 |
+ |
AtomicInteger x = new AtomicInteger(); |
3454 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3455 |
+ |
checkIncomplete(f); |
3456 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3457 |
+ |
f.complete(1); |
3458 |
+ |
checkCompletedNormally(f, 1); |
3459 |
+ |
assertEquals(x.get(), 1); |
3460 |
+ |
assertNull(r.get()); |
3461 |
+ |
} |
3462 |
+ |
|
3463 |
+ |
/** |
3464 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3465 |
+ |
* completed exceptionally when source is. |
3466 |
+ |
*/ |
3467 |
+ |
public void testMinimalCompletionStage2() { |
3468 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3469 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3470 |
+ |
AtomicInteger x = new AtomicInteger(); |
3471 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3472 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3473 |
+ |
checkIncomplete(f); |
3474 |
+ |
CFException ex = new CFException(); |
3475 |
+ |
f.completeExceptionally(ex); |
3476 |
+ |
checkCompletedExceptionally(f, ex); |
3477 |
+ |
assertEquals(x.get(), 0); |
3478 |
+ |
assertEquals(r.get().getCause(), ex); |
3479 |
+ |
} |
3480 |
+ |
|
3481 |
+ |
/** |
3482 |
+ |
* failedStage returns a Completionstage completed |
3483 |
+ |
* exceptionally with the given Exception |
3484 |
+ |
*/ |
3485 |
+ |
public void testFailedStage() { |
3486 |
+ |
CFException ex = new CFException(); |
3487 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3488 |
+ |
AtomicInteger x = new AtomicInteger(); |
3489 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3490 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3491 |
+ |
assertEquals(x.get(), 0); |
3492 |
+ |
assertEquals(r.get().getCause(), ex); |
3493 |
+ |
} |
3494 |
+ |
|
3495 |
+ |
/** |
3496 |
+ |
* completeAsync completes with value of given supplier |
3497 |
+ |
*/ |
3498 |
+ |
public void testCompleteAsync() { |
3499 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3500 |
+ |
f.completeAsync(() -> 1); |
3501 |
+ |
f.join(); |
3502 |
+ |
checkCompletedNormally(f, 1); |
3503 |
+ |
} |
3504 |
+ |
|
3505 |
+ |
/** |
3506 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3507 |
+ |
*/ |
3508 |
+ |
public void testCompleteAsync2() { |
3509 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3510 |
+ |
CFException ex = new CFException(); |
3511 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3512 |
+ |
try { |
3513 |
+ |
f.join(); |
3514 |
+ |
shouldThrow(); |
3515 |
+ |
} catch (Exception success) {} |
3516 |
+ |
checkCompletedWithWrappedCFException(f); |
3517 |
+ |
} |
3518 |
+ |
|
3519 |
+ |
/** |
3520 |
+ |
* completeAsync with given executor completes with value of given supplier |
3521 |
+ |
*/ |
3522 |
+ |
public void testCompleteAsync3() { |
3523 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3524 |
+ |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3525 |
+ |
f.join(); |
3526 |
+ |
checkCompletedNormally(f, 1); |
3527 |
+ |
} |
3528 |
+ |
|
3529 |
+ |
/** |
3530 |
+ |
* completeAsync with given executor completes exceptionally if |
3531 |
+ |
* given supplier throws |
3532 |
+ |
*/ |
3533 |
+ |
public void testCompleteAsync4() { |
3534 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3535 |
+ |
CFException ex = new CFException(); |
3536 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3537 |
+ |
try { |
3538 |
+ |
f.join(); |
3539 |
+ |
shouldThrow(); |
3540 |
+ |
} catch (Exception success) {} |
3541 |
+ |
checkCompletedWithWrappedCFException(f); |
3542 |
+ |
} |
3543 |
+ |
|
3544 |
+ |
/** |
3545 |
+ |
* orTimeout completes with TimeoutException if not complete |
3546 |
+ |
*/ |
3547 |
+ |
public void testOrTimeout() { |
3548 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3549 |
+ |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3550 |
+ |
checkCompletedExceptionallyWithTimeout(f); |
3551 |
+ |
} |
3552 |
+ |
|
3553 |
+ |
/** |
3554 |
+ |
* orTimeout completes normally if completed before timeout |
3555 |
+ |
*/ |
3556 |
+ |
public void testOrTimeout2() { |
3557 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3558 |
+ |
f.complete(1); |
3559 |
+ |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3560 |
+ |
checkCompletedNormally(f, 1); |
3561 |
+ |
} |
3562 |
+ |
|
3563 |
+ |
/** |
3564 |
+ |
* completeOnTimeout completes with given value if not complete |
3565 |
+ |
*/ |
3566 |
+ |
public void testCompleteOnTimeout() { |
3567 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3568 |
+ |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3569 |
+ |
f.join(); |
3570 |
+ |
checkCompletedNormally(f, -1); |
3571 |
+ |
} |
3572 |
+ |
|
3573 |
+ |
/** |
3574 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3575 |
+ |
*/ |
3576 |
+ |
public void testCompleteOnTimeout2() { |
3577 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3578 |
+ |
f.complete(1); |
3579 |
+ |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3580 |
+ |
checkCompletedNormally(f, 1); |
3581 |
+ |
} |
3582 |
+ |
|
3583 |
+ |
/** |
3584 |
+ |
* delayedExecutor returns an executor that delays submission |
3585 |
+ |
*/ |
3586 |
+ |
public void testDelayedExecutor() { |
3587 |
+ |
long timeoutMillis = SMALL_DELAY_MS; |
3588 |
+ |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3589 |
+ |
MILLISECONDS); |
3590 |
+ |
long startTime = System.nanoTime(); |
3591 |
+ |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3592 |
+ |
assertNull(f.getNow(null)); |
3593 |
+ |
try { |
3594 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3595 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3596 |
+ |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3597 |
+ |
checkCompletedNormally(f, 1); |
3598 |
+ |
} |
3599 |
+ |
|
3600 |
+ |
/** |
3601 |
+ |
* delayedExecutor for a given executor returns an executor that |
3602 |
+ |
* delays submission |
3603 |
+ |
*/ |
3604 |
+ |
public void testDelayedExecutor2() { |
3605 |
+ |
long timeoutMillis = SMALL_DELAY_MS; |
3606 |
+ |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3607 |
+ |
MILLISECONDS, |
3608 |
+ |
new ThreadExecutor()); |
3609 |
+ |
long startTime = System.nanoTime(); |
3610 |
+ |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3611 |
+ |
assertNull(f.getNow(null)); |
3612 |
+ |
try { |
3613 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3614 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3615 |
+ |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3616 |
+ |
checkCompletedNormally(f, 1); |
3617 |
+ |
} |
3618 |
+ |
|
3619 |
|
//--- tests of implementation details; not part of official tck --- |
3620 |
|
|
3621 |
|
Object resultOf(CompletableFuture<?> f) { |