64 |
|
assertNull(f.getNow(null)); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
|
try { |
67 |
< |
f.get(0L, SECONDS); |
67 |
> |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
68 |
|
shouldThrow(); |
69 |
|
} |
70 |
|
catch (TimeoutException success) {} |
76 |
|
|
77 |
|
try { |
78 |
|
assertEquals(value, f.join()); |
79 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
80 |
– |
try { |
79 |
|
assertEquals(value, f.getNow(null)); |
82 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
– |
try { |
80 |
|
assertEquals(value, f.get()); |
81 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
82 |
|
assertTrue(f.isDone()); |
89 |
|
* Returns the "raw" internal exceptional completion of f, |
90 |
|
* without any additional wrapping with CompletionException. |
91 |
|
*/ |
92 |
< |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
93 |
< |
// handle (and whenComplete) can distinguish between "direct" |
94 |
< |
// and "wrapped" exceptional completion |
95 |
< |
return f.handle((U u, Throwable t) -> t).join(); |
92 |
> |
Throwable exceptionalCompletion(CompletableFuture<?> f) { |
93 |
> |
// handle (and whenComplete and exceptionally) can distinguish |
94 |
> |
// between "direct" and "wrapped" exceptional completion |
95 |
> |
return f.handle((u, t) -> t).join(); |
96 |
|
} |
97 |
|
|
98 |
|
void checkCompletedExceptionally(CompletableFuture<?> f, |
2558 |
|
|
2559 |
|
// unspecified behavior - both source completions available |
2560 |
|
try { |
2561 |
< |
assertEquals(null, h0.join()); |
2561 |
> |
assertNull(h0.join()); |
2562 |
|
rs[0].assertValue(v1); |
2563 |
|
} catch (CompletionException ok) { |
2564 |
|
checkCompletedWithWrappedException(h0, ex); |
2565 |
|
rs[0].assertNotInvoked(); |
2566 |
|
} |
2567 |
|
try { |
2568 |
< |
assertEquals(null, h1.join()); |
2568 |
> |
assertNull(h1.join()); |
2569 |
|
rs[1].assertValue(v1); |
2570 |
|
} catch (CompletionException ok) { |
2571 |
|
checkCompletedWithWrappedException(h1, ex); |
2572 |
|
rs[1].assertNotInvoked(); |
2573 |
|
} |
2574 |
|
try { |
2575 |
< |
assertEquals(null, h2.join()); |
2575 |
> |
assertNull(h2.join()); |
2576 |
|
rs[2].assertValue(v1); |
2577 |
|
} catch (CompletionException ok) { |
2578 |
|
checkCompletedWithWrappedException(h2, ex); |
2579 |
|
rs[2].assertNotInvoked(); |
2580 |
|
} |
2581 |
|
try { |
2582 |
< |
assertEquals(null, h3.join()); |
2582 |
> |
assertNull(h3.join()); |
2583 |
|
rs[3].assertValue(v1); |
2584 |
|
} catch (CompletionException ok) { |
2585 |
|
checkCompletedWithWrappedException(h3, ex); |
2818 |
|
|
2819 |
|
// unspecified behavior - both source completions available |
2820 |
|
try { |
2821 |
< |
assertEquals(null, h0.join()); |
2821 |
> |
assertNull(h0.join()); |
2822 |
|
rs[0].assertInvoked(); |
2823 |
|
} catch (CompletionException ok) { |
2824 |
|
checkCompletedWithWrappedException(h0, ex); |
2825 |
|
rs[0].assertNotInvoked(); |
2826 |
|
} |
2827 |
|
try { |
2828 |
< |
assertEquals(null, h1.join()); |
2828 |
> |
assertNull(h1.join()); |
2829 |
|
rs[1].assertInvoked(); |
2830 |
|
} catch (CompletionException ok) { |
2831 |
|
checkCompletedWithWrappedException(h1, ex); |
2832 |
|
rs[1].assertNotInvoked(); |
2833 |
|
} |
2834 |
|
try { |
2835 |
< |
assertEquals(null, h2.join()); |
2835 |
> |
assertNull(h2.join()); |
2836 |
|
rs[2].assertInvoked(); |
2837 |
|
} catch (CompletionException ok) { |
2838 |
|
checkCompletedWithWrappedException(h2, ex); |
2839 |
|
rs[2].assertNotInvoked(); |
2840 |
|
} |
2841 |
|
try { |
2842 |
< |
assertEquals(null, h3.join()); |
2842 |
> |
assertNull(h3.join()); |
2843 |
|
rs[3].assertInvoked(); |
2844 |
|
} catch (CompletionException ok) { |
2845 |
|
checkCompletedWithWrappedException(h3, ex); |
3234 |
|
/** |
3235 |
|
* Completion methods throw NullPointerException with null arguments |
3236 |
|
*/ |
3237 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3238 |
|
public void testNPE() { |
3239 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3240 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3529 |
|
*/ |
3530 |
|
public void testCompletedStage() { |
3531 |
|
AtomicInteger x = new AtomicInteger(0); |
3532 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3532 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3533 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3534 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3535 |
|
assertEquals(x.get(), 1); |
3631 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3632 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3633 |
|
AtomicInteger x = new AtomicInteger(0); |
3634 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3634 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3635 |
|
checkIncomplete(f); |
3636 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3637 |
|
f.complete(1); |
3648 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3649 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3650 |
|
AtomicInteger x = new AtomicInteger(0); |
3651 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3651 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3652 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3653 |
|
checkIncomplete(f); |
3654 |
|
CFException ex = new CFException(); |
3666 |
|
CFException ex = new CFException(); |
3667 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3668 |
|
AtomicInteger x = new AtomicInteger(0); |
3669 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3669 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3670 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3671 |
|
assertEquals(x.get(), 0); |
3672 |
|
assertEquals(r.get(), ex); |
3690 |
|
public void testCompleteAsync2() { |
3691 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3692 |
|
CFException ex = new CFException(); |
3693 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3693 |
> |
f.completeAsync(() -> { throw ex; }); |
3694 |
|
try { |
3695 |
|
f.join(); |
3696 |
|
shouldThrow(); |
3720 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3721 |
|
CFException ex = new CFException(); |
3722 |
|
ThreadExecutor executor = new ThreadExecutor(); |
3723 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3723 |
> |
f.completeAsync(() -> { throw ex; }, executor); |
3724 |
|
try { |
3725 |
|
f.join(); |
3726 |
|
shouldThrow(); |