59 |
|
void checkIncomplete(CompletableFuture<?> f) { |
60 |
|
assertFalse(f.isDone()); |
61 |
|
assertFalse(f.isCancelled()); |
62 |
< |
assertTrue(f.toString().contains("Not completed")); |
62 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
63 |
|
try { |
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()); |
83 |
|
assertFalse(f.isCancelled()); |
84 |
|
assertFalse(f.isCompletedExceptionally()); |
85 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
85 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
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, |
138 |
|
assertFalse(f.isCancelled()); |
139 |
|
assertTrue(f.isDone()); |
140 |
|
assertTrue(f.isCompletedExceptionally()); |
141 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
141 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
142 |
|
} |
143 |
|
|
144 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
193 |
|
assertTrue(f.isDone()); |
194 |
|
assertTrue(f.isCompletedExceptionally()); |
195 |
|
assertTrue(f.isCancelled()); |
196 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
196 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
197 |
|
} |
198 |
|
|
199 |
|
/** |
329 |
|
/** |
330 |
|
* toString indicates current completion state |
331 |
|
*/ |
332 |
< |
public void testToString() { |
333 |
< |
CompletableFuture<String> f; |
334 |
< |
|
335 |
< |
f = new CompletableFuture<String>(); |
336 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
332 |
> |
public void testToString_incomplete() { |
333 |
> |
CompletableFuture<String> f = new CompletableFuture<String>(); |
334 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
335 |
> |
if (testImplementationDetails) |
336 |
> |
assertEquals(identityString(f) + "[Not completed]", |
337 |
> |
f.toString()); |
338 |
> |
} |
339 |
|
|
340 |
+ |
public void testToString_normal() { |
341 |
+ |
CompletableFuture<String> f = new CompletableFuture<String>(); |
342 |
|
assertTrue(f.complete("foo")); |
343 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
343 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
344 |
> |
if (testImplementationDetails) |
345 |
> |
assertEquals(identityString(f) + "[Completed normally]", |
346 |
> |
f.toString()); |
347 |
> |
} |
348 |
|
|
349 |
< |
f = new CompletableFuture<String>(); |
349 |
> |
public void testToString_exception() { |
350 |
> |
CompletableFuture<String> f = new CompletableFuture<String>(); |
351 |
|
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
352 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
352 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
353 |
> |
if (testImplementationDetails) |
354 |
> |
assertTrue(f.toString().startsWith( |
355 |
> |
identityString(f) + "[Completed exceptionally: ")); |
356 |
> |
} |
357 |
|
|
358 |
+ |
public void testToString_cancelled() { |
359 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
360 |
< |
f = new CompletableFuture<String>(); |
360 |
> |
CompletableFuture<String> f = new CompletableFuture<String>(); |
361 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
362 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
362 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
363 |
> |
if (testImplementationDetails) |
364 |
> |
assertTrue(f.toString().startsWith( |
365 |
> |
identityString(f) + "[Completed exceptionally: ")); |
366 |
|
} |
367 |
|
} |
368 |
|
|
1255 |
|
r.assertInvoked(); |
1256 |
|
}} |
1257 |
|
|
1258 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1259 |
|
public void testRunAsync_rejectingExecutor() { |
1260 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1261 |
|
try { |
1302 |
|
r.assertInvoked(); |
1303 |
|
}} |
1304 |
|
|
1305 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1306 |
|
public void testSupplyAsync_rejectingExecutor() { |
1307 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1308 |
|
try { |
2577 |
|
|
2578 |
|
// unspecified behavior - both source completions available |
2579 |
|
try { |
2580 |
< |
assertEquals(null, h0.join()); |
2580 |
> |
assertNull(h0.join()); |
2581 |
|
rs[0].assertValue(v1); |
2582 |
|
} catch (CompletionException ok) { |
2583 |
|
checkCompletedWithWrappedException(h0, ex); |
2584 |
|
rs[0].assertNotInvoked(); |
2585 |
|
} |
2586 |
|
try { |
2587 |
< |
assertEquals(null, h1.join()); |
2587 |
> |
assertNull(h1.join()); |
2588 |
|
rs[1].assertValue(v1); |
2589 |
|
} catch (CompletionException ok) { |
2590 |
|
checkCompletedWithWrappedException(h1, ex); |
2591 |
|
rs[1].assertNotInvoked(); |
2592 |
|
} |
2593 |
|
try { |
2594 |
< |
assertEquals(null, h2.join()); |
2594 |
> |
assertNull(h2.join()); |
2595 |
|
rs[2].assertValue(v1); |
2596 |
|
} catch (CompletionException ok) { |
2597 |
|
checkCompletedWithWrappedException(h2, ex); |
2598 |
|
rs[2].assertNotInvoked(); |
2599 |
|
} |
2600 |
|
try { |
2601 |
< |
assertEquals(null, h3.join()); |
2601 |
> |
assertNull(h3.join()); |
2602 |
|
rs[3].assertValue(v1); |
2603 |
|
} catch (CompletionException ok) { |
2604 |
|
checkCompletedWithWrappedException(h3, ex); |
2837 |
|
|
2838 |
|
// unspecified behavior - both source completions available |
2839 |
|
try { |
2840 |
< |
assertEquals(null, h0.join()); |
2840 |
> |
assertNull(h0.join()); |
2841 |
|
rs[0].assertInvoked(); |
2842 |
|
} catch (CompletionException ok) { |
2843 |
|
checkCompletedWithWrappedException(h0, ex); |
2844 |
|
rs[0].assertNotInvoked(); |
2845 |
|
} |
2846 |
|
try { |
2847 |
< |
assertEquals(null, h1.join()); |
2847 |
> |
assertNull(h1.join()); |
2848 |
|
rs[1].assertInvoked(); |
2849 |
|
} catch (CompletionException ok) { |
2850 |
|
checkCompletedWithWrappedException(h1, ex); |
2851 |
|
rs[1].assertNotInvoked(); |
2852 |
|
} |
2853 |
|
try { |
2854 |
< |
assertEquals(null, h2.join()); |
2854 |
> |
assertNull(h2.join()); |
2855 |
|
rs[2].assertInvoked(); |
2856 |
|
} catch (CompletionException ok) { |
2857 |
|
checkCompletedWithWrappedException(h2, ex); |
2858 |
|
rs[2].assertNotInvoked(); |
2859 |
|
} |
2860 |
|
try { |
2861 |
< |
assertEquals(null, h3.join()); |
2861 |
> |
assertNull(h3.join()); |
2862 |
|
rs[3].assertInvoked(); |
2863 |
|
} catch (CompletionException ok) { |
2864 |
|
checkCompletedWithWrappedException(h3, ex); |
3253 |
|
/** |
3254 |
|
* Completion methods throw NullPointerException with null arguments |
3255 |
|
*/ |
3256 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3257 |
|
public void testNPE() { |
3258 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3259 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3548 |
|
*/ |
3549 |
|
public void testCompletedStage() { |
3550 |
|
AtomicInteger x = new AtomicInteger(0); |
3551 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3551 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3552 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3553 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3554 |
|
assertEquals(x.get(), 1); |
3650 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3651 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3652 |
|
AtomicInteger x = new AtomicInteger(0); |
3653 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3653 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3654 |
|
checkIncomplete(f); |
3655 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3656 |
|
f.complete(1); |
3667 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3668 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3669 |
|
AtomicInteger x = new AtomicInteger(0); |
3670 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3670 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3671 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3672 |
|
checkIncomplete(f); |
3673 |
|
CFException ex = new CFException(); |
3685 |
|
CFException ex = new CFException(); |
3686 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3687 |
|
AtomicInteger x = new AtomicInteger(0); |
3688 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3688 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3689 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3690 |
|
assertEquals(x.get(), 0); |
3691 |
|
assertEquals(r.get(), ex); |
3709 |
|
public void testCompleteAsync2() { |
3710 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3711 |
|
CFException ex = new CFException(); |
3712 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3712 |
> |
f.completeAsync(() -> { throw ex; }); |
3713 |
|
try { |
3714 |
|
f.join(); |
3715 |
|
shouldThrow(); |
3739 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3740 |
|
CFException ex = new CFException(); |
3741 |
|
ThreadExecutor executor = new ThreadExecutor(); |
3742 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3742 |
> |
f.completeAsync(() -> { throw ex; }, executor); |
3743 |
|
try { |
3744 |
|
f.join(); |
3745 |
|
shouldThrow(); |
4315 |
|
} |
4316 |
|
|
4317 |
|
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4318 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4319 |
|
public void testRecursiveChains() throws Throwable { |
4320 |
|
for (ExecutionMode m : ExecutionMode.values()) |
4321 |
|
for (boolean addDeadEnds : new boolean[] { true, false }) |
4340 |
|
* A single CompletableFuture with many dependents. |
4341 |
|
* A demo of scalability - runtime is O(n). |
4342 |
|
*/ |
4343 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4344 |
|
public void testManyDependents() throws Throwable { |
4345 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4346 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4370 |
|
} |
4371 |
|
|
4372 |
|
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4373 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4374 |
|
public void testCoCompletionGarbageRetention() throws Throwable { |
4375 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4376 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |