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; |
36 |
|
public class CompletableFutureTest extends JSR166TestCase { |
37 |
|
|
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(CompletableFutureTest.class); |
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); |
312 |
|
{ |
313 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
314 |
|
checkIncomplete(f); |
315 |
< |
assertTrue(f.cancel(true)); |
316 |
< |
assertTrue(f.cancel(true)); |
315 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
316 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
317 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
318 |
|
checkCancelled(f); |
319 |
|
}} |
320 |
|
|
586 |
|
} |
587 |
|
} |
588 |
|
|
549 |
– |
|
589 |
|
class CompletableFutureInc extends CheckedIntegerAction |
590 |
|
implements Function<Integer, CompletableFuture<Integer>> |
591 |
|
{ |
946 |
|
|
947 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
948 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
910 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
949 |
|
{ |
950 |
|
final AtomicInteger a = new AtomicInteger(0); |
951 |
|
final CFException ex1 = new CFException(); |
999 |
|
public void testWhenComplete_exceptionalCompletion() { |
1000 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1001 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
964 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1002 |
|
{ |
1003 |
|
final AtomicInteger a = new AtomicInteger(0); |
1004 |
|
final CFException ex = new CFException(); |
1083 |
|
public void testWhenComplete_actionFailedSourceFailed() { |
1084 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1085 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1049 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1086 |
|
{ |
1087 |
|
final AtomicInteger a = new AtomicInteger(0); |
1088 |
|
final CFException ex1 = new CFException(); |
3249 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3250 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3251 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3216 |
– |
CompletableFuture<?> h; |
3252 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3253 |
|
|
3254 |
|
Runnable[] throwingActions = { |
3359 |
|
assertSame(f, f.toCompletableFuture()); |
3360 |
|
} |
3361 |
|
|
3362 |
+ |
// jdk9 |
3363 |
+ |
|
3364 |
+ |
/** |
3365 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3366 |
+ |
*/ |
3367 |
+ |
public void testNewIncompleteFuture() { |
3368 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3369 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3370 |
+ |
checkIncomplete(f); |
3371 |
+ |
checkIncomplete(g); |
3372 |
+ |
} |
3373 |
+ |
|
3374 |
+ |
/** |
3375 |
+ |
* completedStage returns a completed CompletionStage |
3376 |
+ |
*/ |
3377 |
+ |
public void testCompletedStage() { |
3378 |
+ |
AtomicInteger x = new AtomicInteger(); |
3379 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3380 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3381 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3382 |
+ |
assertEquals(x.get(), 1); |
3383 |
+ |
assertNull(r.get()); |
3384 |
+ |
} |
3385 |
+ |
|
3386 |
+ |
/** |
3387 |
+ |
* defaultExecutor by default returns the commonPool if |
3388 |
+ |
* it supports at least one thread. |
3389 |
+ |
*/ |
3390 |
+ |
public void testDefaultExecutor() { |
3391 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3392 |
+ |
Executor e = f.defaultExecutor(); |
3393 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3394 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3395 |
+ |
assertSame(e, c); |
3396 |
+ |
} |
3397 |
+ |
|
3398 |
+ |
/** |
3399 |
+ |
* failedFuture returns a CompletableFuture completed |
3400 |
+ |
* exceptionally with the given Exception |
3401 |
+ |
*/ |
3402 |
+ |
public void testFailedFuture() { |
3403 |
+ |
CFException ex = new CFException(); |
3404 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3405 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
3406 |
+ |
} |
3407 |
+ |
|
3408 |
+ |
/** |
3409 |
+ |
* failedFuture(null) throws NPE |
3410 |
+ |
*/ |
3411 |
+ |
public void testFailedFuture2() { |
3412 |
+ |
try { |
3413 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3414 |
+ |
shouldThrow(); |
3415 |
+ |
} catch (NullPointerException success) {} |
3416 |
+ |
} |
3417 |
+ |
|
3418 |
+ |
/** |
3419 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3420 |
+ |
* with the same value, when source is. |
3421 |
+ |
*/ |
3422 |
+ |
public void testCopy() { |
3423 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3424 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3425 |
+ |
checkIncomplete(f); |
3426 |
+ |
checkIncomplete(g); |
3427 |
+ |
f.complete(1); |
3428 |
+ |
checkCompletedNormally(f, 1); |
3429 |
+ |
checkCompletedNormally(g, 1); |
3430 |
+ |
} |
3431 |
+ |
|
3432 |
+ |
/** |
3433 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3434 |
+ |
* when source is. |
3435 |
+ |
*/ |
3436 |
+ |
public void testCopy2() { |
3437 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3438 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3439 |
+ |
checkIncomplete(f); |
3440 |
+ |
checkIncomplete(g); |
3441 |
+ |
CFException ex = new CFException(); |
3442 |
+ |
f.completeExceptionally(ex); |
3443 |
+ |
checkCompletedExceptionally(f, ex); |
3444 |
+ |
checkCompletedWithWrappedCFException(g); |
3445 |
+ |
} |
3446 |
+ |
|
3447 |
+ |
/** |
3448 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3449 |
+ |
* completed normally, with the same value, when source is. |
3450 |
+ |
*/ |
3451 |
+ |
public void testMinimalCompletionStage() { |
3452 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3453 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3454 |
+ |
AtomicInteger x = new AtomicInteger(); |
3455 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3456 |
+ |
checkIncomplete(f); |
3457 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3458 |
+ |
f.complete(1); |
3459 |
+ |
checkCompletedNormally(f, 1); |
3460 |
+ |
assertEquals(x.get(), 1); |
3461 |
+ |
assertNull(r.get()); |
3462 |
+ |
} |
3463 |
+ |
|
3464 |
+ |
/** |
3465 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3466 |
+ |
* completed exceptionally when source is. |
3467 |
+ |
*/ |
3468 |
+ |
public void testMinimalCompletionStage2() { |
3469 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3470 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3471 |
+ |
AtomicInteger x = new AtomicInteger(); |
3472 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3473 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3474 |
+ |
checkIncomplete(f); |
3475 |
+ |
CFException ex = new CFException(); |
3476 |
+ |
f.completeExceptionally(ex); |
3477 |
+ |
checkCompletedExceptionally(f, ex); |
3478 |
+ |
assertEquals(x.get(), 0); |
3479 |
+ |
assertEquals(r.get().getCause(), ex); |
3480 |
+ |
} |
3481 |
+ |
|
3482 |
+ |
/** |
3483 |
+ |
* failedStage returns a Completionstage completed |
3484 |
+ |
* exceptionally with the given Exception |
3485 |
+ |
*/ |
3486 |
+ |
public void testFailedStage() { |
3487 |
+ |
CFException ex = new CFException(); |
3488 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3489 |
+ |
AtomicInteger x = new AtomicInteger(); |
3490 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3491 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3492 |
+ |
assertEquals(x.get(), 0); |
3493 |
+ |
assertEquals(r.get().getCause(), ex); |
3494 |
+ |
} |
3495 |
+ |
|
3496 |
+ |
/** |
3497 |
+ |
* completeAsync completes with value of given supplier |
3498 |
+ |
*/ |
3499 |
+ |
public void testCompleteAsync() { |
3500 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3501 |
+ |
f.completeAsync(() -> 1); |
3502 |
+ |
f.join(); |
3503 |
+ |
checkCompletedNormally(f, 1); |
3504 |
+ |
} |
3505 |
+ |
|
3506 |
+ |
/** |
3507 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3508 |
+ |
*/ |
3509 |
+ |
public void testCompleteAsync2() { |
3510 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3511 |
+ |
CFException ex = new CFException(); |
3512 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3513 |
+ |
try { |
3514 |
+ |
f.join(); |
3515 |
+ |
shouldThrow(); |
3516 |
+ |
} catch (Exception success) {} |
3517 |
+ |
checkCompletedWithWrappedCFException(f); |
3518 |
+ |
} |
3519 |
+ |
|
3520 |
+ |
/** |
3521 |
+ |
* completeAsync with given executor completes with value of given supplier |
3522 |
+ |
*/ |
3523 |
+ |
public void testCompleteAsync3() { |
3524 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3525 |
+ |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3526 |
+ |
f.join(); |
3527 |
+ |
checkCompletedNormally(f, 1); |
3528 |
+ |
} |
3529 |
+ |
|
3530 |
+ |
/** |
3531 |
+ |
* completeAsync with given executor completes exceptionally if |
3532 |
+ |
* given supplier throws |
3533 |
+ |
*/ |
3534 |
+ |
public void testCompleteAsync4() { |
3535 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3536 |
+ |
CFException ex = new CFException(); |
3537 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3538 |
+ |
try { |
3539 |
+ |
f.join(); |
3540 |
+ |
shouldThrow(); |
3541 |
+ |
} catch (Exception success) {} |
3542 |
+ |
checkCompletedWithWrappedCFException(f); |
3543 |
+ |
} |
3544 |
+ |
|
3545 |
+ |
/** |
3546 |
+ |
* orTimeout completes with TimeoutException if not complete |
3547 |
+ |
*/ |
3548 |
+ |
public void testOrTimeout() { |
3549 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3550 |
+ |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3551 |
+ |
checkCompletedExceptionallyWithTimeout(f); |
3552 |
+ |
} |
3553 |
+ |
|
3554 |
+ |
/** |
3555 |
+ |
* orTimeout completes normally if completed before timeout |
3556 |
+ |
*/ |
3557 |
+ |
public void testOrTimeout2() { |
3558 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3559 |
+ |
f.complete(1); |
3560 |
+ |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3561 |
+ |
checkCompletedNormally(f, 1); |
3562 |
+ |
} |
3563 |
+ |
|
3564 |
+ |
/** |
3565 |
+ |
* completeOnTimeout completes with given value if not complete |
3566 |
+ |
*/ |
3567 |
+ |
public void testCompleteOnTimeout() { |
3568 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3569 |
+ |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3570 |
+ |
f.join(); |
3571 |
+ |
checkCompletedNormally(f, -1); |
3572 |
+ |
} |
3573 |
+ |
|
3574 |
+ |
/** |
3575 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3576 |
+ |
*/ |
3577 |
+ |
public void testCompleteOnTimeout2() { |
3578 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3579 |
+ |
f.complete(1); |
3580 |
+ |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3581 |
+ |
checkCompletedNormally(f, 1); |
3582 |
+ |
} |
3583 |
+ |
|
3584 |
+ |
/** |
3585 |
+ |
* delayedExecutor returns an executor that delays submission |
3586 |
+ |
*/ |
3587 |
+ |
public void testDelayedExecutor() { |
3588 |
+ |
long timeoutMillis = SMALL_DELAY_MS; |
3589 |
+ |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3590 |
+ |
MILLISECONDS); |
3591 |
+ |
long startTime = System.nanoTime(); |
3592 |
+ |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3593 |
+ |
assertNull(f.getNow(null)); |
3594 |
+ |
try { |
3595 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3596 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3597 |
+ |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3598 |
+ |
checkCompletedNormally(f, 1); |
3599 |
+ |
} |
3600 |
+ |
|
3601 |
+ |
/** |
3602 |
+ |
* delayedExecutor for a given executor returns an executor that |
3603 |
+ |
* delays submission |
3604 |
+ |
*/ |
3605 |
+ |
public void testDelayedExecutor2() { |
3606 |
+ |
long timeoutMillis = SMALL_DELAY_MS; |
3607 |
+ |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3608 |
+ |
MILLISECONDS, |
3609 |
+ |
new ThreadExecutor()); |
3610 |
+ |
long startTime = System.nanoTime(); |
3611 |
+ |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3612 |
+ |
assertNull(f.getNow(null)); |
3613 |
+ |
try { |
3614 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3615 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3616 |
+ |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3617 |
+ |
checkCompletedNormally(f, 1); |
3618 |
+ |
} |
3619 |
+ |
|
3620 |
|
//--- tests of implementation details; not part of official tck --- |
3621 |
|
|
3622 |
|
Object resultOf(CompletableFuture<?> f) { |