41 |
|
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
– |
import junit.framework.AssertionFailedError; |
44 |
|
import junit.framework.Test; |
45 |
|
import junit.framework.TestSuite; |
46 |
|
|
58 |
|
void checkIncomplete(CompletableFuture<?> f) { |
59 |
|
assertFalse(f.isDone()); |
60 |
|
assertFalse(f.isCancelled()); |
61 |
< |
assertTrue(f.toString().contains("Not completed")); |
61 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
62 |
> |
|
63 |
> |
Object result = null; |
64 |
|
try { |
65 |
< |
assertNull(f.getNow(null)); |
65 |
> |
result = f.getNow(null); |
66 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
67 |
+ |
assertNull(result); |
68 |
+ |
|
69 |
|
try { |
70 |
< |
f.get(0L, SECONDS); |
70 |
> |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
71 |
|
shouldThrow(); |
72 |
|
} |
73 |
|
catch (TimeoutException success) {} |
79 |
|
|
80 |
|
try { |
81 |
|
assertEquals(value, f.join()); |
79 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
80 |
– |
try { |
82 |
|
assertEquals(value, f.getNow(null)); |
82 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
– |
try { |
83 |
|
assertEquals(value, f.get()); |
84 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
|
assertTrue(f.isDone()); |
86 |
|
assertFalse(f.isCancelled()); |
87 |
|
assertFalse(f.isCompletedExceptionally()); |
88 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
88 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
141 |
|
assertFalse(f.isCancelled()); |
142 |
|
assertTrue(f.isDone()); |
143 |
|
assertTrue(f.isCompletedExceptionally()); |
144 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
144 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
145 |
|
} |
146 |
|
|
147 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
196 |
|
assertTrue(f.isDone()); |
197 |
|
assertTrue(f.isCompletedExceptionally()); |
198 |
|
assertTrue(f.isCancelled()); |
199 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
199 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
295 |
|
} |
296 |
|
|
297 |
|
f = new CompletableFuture<>(); |
298 |
< |
f.completeExceptionally(ex = new CFException()); |
298 |
> |
f.completeExceptionally(new CFException()); |
299 |
|
f.obtrudeValue(v1); |
300 |
|
checkCompletedNormally(f, v1); |
301 |
|
f.obtrudeException(ex = new CFException()); |
332 |
|
/** |
333 |
|
* toString indicates current completion state |
334 |
|
*/ |
335 |
< |
public void testToString() { |
336 |
< |
CompletableFuture<String> f; |
337 |
< |
|
338 |
< |
f = new CompletableFuture<String>(); |
339 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
335 |
> |
public void testToString_incomplete() { |
336 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
337 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
338 |
> |
if (testImplementationDetails) |
339 |
> |
assertEquals(identityString(f) + "[Not completed]", |
340 |
> |
f.toString()); |
341 |
> |
} |
342 |
|
|
343 |
+ |
public void testToString_normal() { |
344 |
+ |
CompletableFuture<String> f = new CompletableFuture<>(); |
345 |
|
assertTrue(f.complete("foo")); |
346 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
346 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
347 |
> |
if (testImplementationDetails) |
348 |
> |
assertEquals(identityString(f) + "[Completed normally]", |
349 |
> |
f.toString()); |
350 |
> |
} |
351 |
|
|
352 |
< |
f = new CompletableFuture<String>(); |
352 |
> |
public void testToString_exception() { |
353 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
354 |
|
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
355 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
355 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
356 |
> |
if (testImplementationDetails) |
357 |
> |
assertTrue(f.toString().startsWith( |
358 |
> |
identityString(f) + "[Completed exceptionally: ")); |
359 |
> |
} |
360 |
|
|
361 |
+ |
public void testToString_cancelled() { |
362 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
363 |
< |
f = new CompletableFuture<String>(); |
363 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
364 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
365 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
365 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
366 |
> |
if (testImplementationDetails) |
367 |
> |
assertTrue(f.toString().startsWith( |
368 |
> |
identityString(f) + "[Completed exceptionally: ")); |
369 |
|
} |
370 |
|
} |
371 |
|
|
1258 |
|
r.assertInvoked(); |
1259 |
|
}} |
1260 |
|
|
1261 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1262 |
|
public void testRunAsync_rejectingExecutor() { |
1263 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1264 |
|
try { |
1305 |
|
r.assertInvoked(); |
1306 |
|
}} |
1307 |
|
|
1308 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1309 |
|
public void testSupplyAsync_rejectingExecutor() { |
1310 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1311 |
|
try { |
2580 |
|
|
2581 |
|
// unspecified behavior - both source completions available |
2582 |
|
try { |
2583 |
< |
assertEquals(null, h0.join()); |
2583 |
> |
assertNull(h0.join()); |
2584 |
|
rs[0].assertValue(v1); |
2585 |
|
} catch (CompletionException ok) { |
2586 |
|
checkCompletedWithWrappedException(h0, ex); |
2587 |
|
rs[0].assertNotInvoked(); |
2588 |
|
} |
2589 |
|
try { |
2590 |
< |
assertEquals(null, h1.join()); |
2590 |
> |
assertNull(h1.join()); |
2591 |
|
rs[1].assertValue(v1); |
2592 |
|
} catch (CompletionException ok) { |
2593 |
|
checkCompletedWithWrappedException(h1, ex); |
2594 |
|
rs[1].assertNotInvoked(); |
2595 |
|
} |
2596 |
|
try { |
2597 |
< |
assertEquals(null, h2.join()); |
2597 |
> |
assertNull(h2.join()); |
2598 |
|
rs[2].assertValue(v1); |
2599 |
|
} catch (CompletionException ok) { |
2600 |
|
checkCompletedWithWrappedException(h2, ex); |
2601 |
|
rs[2].assertNotInvoked(); |
2602 |
|
} |
2603 |
|
try { |
2604 |
< |
assertEquals(null, h3.join()); |
2604 |
> |
assertNull(h3.join()); |
2605 |
|
rs[3].assertValue(v1); |
2606 |
|
} catch (CompletionException ok) { |
2607 |
|
checkCompletedWithWrappedException(h3, ex); |
2840 |
|
|
2841 |
|
// unspecified behavior - both source completions available |
2842 |
|
try { |
2843 |
< |
assertEquals(null, h0.join()); |
2843 |
> |
assertNull(h0.join()); |
2844 |
|
rs[0].assertInvoked(); |
2845 |
|
} catch (CompletionException ok) { |
2846 |
|
checkCompletedWithWrappedException(h0, ex); |
2847 |
|
rs[0].assertNotInvoked(); |
2848 |
|
} |
2849 |
|
try { |
2850 |
< |
assertEquals(null, h1.join()); |
2850 |
> |
assertNull(h1.join()); |
2851 |
|
rs[1].assertInvoked(); |
2852 |
|
} catch (CompletionException ok) { |
2853 |
|
checkCompletedWithWrappedException(h1, ex); |
2854 |
|
rs[1].assertNotInvoked(); |
2855 |
|
} |
2856 |
|
try { |
2857 |
< |
assertEquals(null, h2.join()); |
2857 |
> |
assertNull(h2.join()); |
2858 |
|
rs[2].assertInvoked(); |
2859 |
|
} catch (CompletionException ok) { |
2860 |
|
checkCompletedWithWrappedException(h2, ex); |
2861 |
|
rs[2].assertNotInvoked(); |
2862 |
|
} |
2863 |
|
try { |
2864 |
< |
assertEquals(null, h3.join()); |
2864 |
> |
assertNull(h3.join()); |
2865 |
|
rs[3].assertInvoked(); |
2866 |
|
} catch (CompletionException ok) { |
2867 |
|
checkCompletedWithWrappedException(h3, ex); |
3256 |
|
/** |
3257 |
|
* Completion methods throw NullPointerException with null arguments |
3258 |
|
*/ |
3259 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3260 |
|
public void testNPE() { |
3261 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3262 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3551 |
|
*/ |
3552 |
|
public void testCompletedStage() { |
3553 |
|
AtomicInteger x = new AtomicInteger(0); |
3554 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3554 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3555 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3556 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3557 |
|
assertEquals(x.get(), 1); |
3653 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3654 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3655 |
|
AtomicInteger x = new AtomicInteger(0); |
3656 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3656 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3657 |
|
checkIncomplete(f); |
3658 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3659 |
|
f.complete(1); |
3670 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3671 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3672 |
|
AtomicInteger x = new AtomicInteger(0); |
3673 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3673 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3674 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3675 |
|
checkIncomplete(f); |
3676 |
|
CFException ex = new CFException(); |
3688 |
|
CFException ex = new CFException(); |
3689 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3690 |
|
AtomicInteger x = new AtomicInteger(0); |
3691 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3691 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3692 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3693 |
|
assertEquals(x.get(), 0); |
3694 |
|
assertEquals(r.get(), ex); |
4193 |
|
static void assertZero(CompletableFuture<?> f) { |
4194 |
|
try { |
4195 |
|
f.getNow(null); |
4196 |
< |
throw new AssertionFailedError("should throw"); |
4196 |
> |
throw new AssertionError("should throw"); |
4197 |
|
} catch (CompletionException success) { |
4198 |
|
assertTrue(success.getCause() instanceof ZeroException); |
4199 |
|
} |
4318 |
|
} |
4319 |
|
|
4320 |
|
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4321 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4322 |
|
public void testRecursiveChains() throws Throwable { |
4323 |
|
for (ExecutionMode m : ExecutionMode.values()) |
4324 |
|
for (boolean addDeadEnds : new boolean[] { true, false }) |
4343 |
|
* A single CompletableFuture with many dependents. |
4344 |
|
* A demo of scalability - runtime is O(n). |
4345 |
|
*/ |
4346 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4347 |
|
public void testManyDependents() throws Throwable { |
4348 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4349 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4373 |
|
} |
4374 |
|
|
4375 |
|
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4376 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4377 |
|
public void testCoCompletionGarbageRetention() throws Throwable { |
4378 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4379 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4392 |
|
f.complete(null); |
4393 |
|
|
4394 |
|
f = new CompletableFuture<>(); |
4395 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4395 |
> |
CompletableFuture.anyOf(f, incomplete); |
4396 |
|
f.complete(null); |
4397 |
|
} |
4398 |
|
|
4410 |
|
f.complete(null); |
4411 |
|
|
4412 |
|
f = new CompletableFuture<>(); |
4413 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4413 |
> |
CompletableFuture.anyOf(incomplete, f); |
4414 |
|
f.complete(null); |
4415 |
|
} |
4416 |
|
} |