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); } |
65 |
> |
} catch (Exception 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); } |
81 |
> |
} catch (Exception 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) { |
145 |
|
checkCompletedExceptionally(f, true, |
146 |
< |
(t) -> assertTrue(t instanceof CFException)); |
146 |
> |
t -> assertTrue(t instanceof CFException)); |
147 |
|
} |
148 |
|
|
149 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
150 |
|
checkCompletedExceptionally(f, true, |
151 |
< |
(t) -> assertTrue(t instanceof CancellationException)); |
151 |
> |
t -> assertTrue(t instanceof CancellationException)); |
152 |
|
} |
153 |
|
|
154 |
|
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
155 |
|
checkCompletedExceptionally(f, false, |
156 |
< |
(t) -> assertTrue(t instanceof TimeoutException)); |
156 |
> |
t -> assertTrue(t instanceof TimeoutException)); |
157 |
|
} |
158 |
|
|
159 |
|
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
160 |
|
Throwable ex) { |
161 |
< |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
161 |
> |
checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); |
162 |
|
} |
163 |
|
|
164 |
|
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
165 |
< |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
165 |
> |
checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); |
166 |
|
} |
167 |
|
|
168 |
|
void checkCancelled(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<>(); |
3273 |
|
|
3274 |
|
() -> f.thenApply(null), |
3275 |
|
() -> f.thenApplyAsync(null), |
3276 |
< |
() -> f.thenApplyAsync((x) -> x, null), |
3276 |
> |
() -> f.thenApplyAsync(x -> x, null), |
3277 |
|
() -> f.thenApplyAsync(null, exec), |
3278 |
|
|
3279 |
|
() -> f.thenAccept(null), |
3280 |
|
() -> f.thenAcceptAsync(null), |
3281 |
< |
() -> f.thenAcceptAsync((x) -> {} , null), |
3281 |
> |
() -> f.thenAcceptAsync(x -> {} , null), |
3282 |
|
() -> f.thenAcceptAsync(null, exec), |
3283 |
|
|
3284 |
|
() -> f.thenRun(null), |
3313 |
|
() -> f.applyToEither(g, null), |
3314 |
|
() -> f.applyToEitherAsync(g, null), |
3315 |
|
() -> f.applyToEitherAsync(g, null, exec), |
3316 |
< |
() -> f.applyToEither(nullFuture, (x) -> x), |
3317 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
3318 |
< |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
3319 |
< |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
3316 |
> |
() -> f.applyToEither(nullFuture, x -> x), |
3317 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x), |
3318 |
> |
() -> f.applyToEitherAsync(nullFuture, x -> x, exec), |
3319 |
> |
() -> f.applyToEitherAsync(g, x -> x, null), |
3320 |
|
|
3321 |
|
() -> f.acceptEither(g, null), |
3322 |
|
() -> f.acceptEitherAsync(g, null), |
3323 |
|
() -> f.acceptEitherAsync(g, null, exec), |
3324 |
< |
() -> f.acceptEither(nullFuture, (x) -> {}), |
3325 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
3326 |
< |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
3327 |
< |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
3324 |
> |
() -> f.acceptEither(nullFuture, x -> {}), |
3325 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}), |
3326 |
> |
() -> f.acceptEitherAsync(nullFuture, x -> {}, exec), |
3327 |
> |
() -> f.acceptEitherAsync(g, x -> {}, null), |
3328 |
|
|
3329 |
|
() -> f.runAfterEither(g, null), |
3330 |
|
() -> f.runAfterEitherAsync(g, null), |
3390 |
|
for (CompletableFuture<Integer> src : srcs) { |
3391 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3392 |
|
fs.add(src.thenRunAsync(() -> {}, e)); |
3393 |
< |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3394 |
< |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3393 |
> |
fs.add(src.thenAcceptAsync(z -> {}, e)); |
3394 |
> |
fs.add(src.thenApplyAsync(z -> z, e)); |
3395 |
|
|
3396 |
|
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3397 |
|
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3398 |
|
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3399 |
|
|
3400 |
< |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3401 |
< |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3400 |
> |
fs.add(src.applyToEitherAsync(src, z -> z, e)); |
3401 |
> |
fs.add(src.acceptEitherAsync(src, z -> {}, e)); |
3402 |
|
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3403 |
|
|
3404 |
< |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3404 |
> |
fs.add(src.thenComposeAsync(z -> null, e)); |
3405 |
|
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3406 |
|
fs.add(src.handleAsync((z, t) -> null, e)); |
3407 |
|
|
3434 |
|
{ |
3435 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3436 |
|
|
3437 |
< |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3438 |
< |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3437 |
> |
fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); |
3438 |
> |
fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); |
3439 |
|
|
3440 |
< |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3441 |
< |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3440 |
> |
fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); |
3441 |
> |
fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); |
3442 |
|
|
3443 |
|
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3444 |
|
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3477 |
|
|
3478 |
|
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3479 |
|
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3480 |
< |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3481 |
< |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3480 |
> |
fs.add(incomplete.thenAcceptAsync(z -> {}, e)); |
3481 |
> |
fs.add(incomplete.thenApplyAsync(z -> z, e)); |
3482 |
|
|
3483 |
|
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3484 |
|
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3485 |
|
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3486 |
|
|
3487 |
< |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3488 |
< |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3487 |
> |
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); |
3488 |
> |
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); |
3489 |
|
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3490 |
|
|
3491 |
< |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3491 |
> |
fs.add(incomplete.thenComposeAsync(z -> null, e)); |
3492 |
|
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3493 |
|
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3494 |
|
|
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(); |
3898 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3899 |
|
= new ArrayList<>(); |
3900 |
|
|
3901 |
< |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3902 |
< |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3903 |
< |
funs.add((y) -> m.thenApply(y, incFunction)); |
3904 |
< |
|
3905 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3906 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3907 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3908 |
< |
|
3909 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3910 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3911 |
< |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3912 |
< |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3913 |
< |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3914 |
< |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3915 |
< |
|
3916 |
< |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3917 |
< |
|
3918 |
< |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3919 |
< |
|
3920 |
< |
funs.add((y) -> CompletableFuture.allOf(y)); |
3921 |
< |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3922 |
< |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3923 |
< |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3924 |
< |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3925 |
< |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3901 |
> |
funs.add(y -> m.thenRun(y, noopRunnable)); |
3902 |
> |
funs.add(y -> m.thenAccept(y, noopConsumer)); |
3903 |
> |
funs.add(y -> m.thenApply(y, incFunction)); |
3904 |
> |
|
3905 |
> |
funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); |
3906 |
> |
funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); |
3907 |
> |
funs.add(y -> m.applyToEither(y, incomplete, incFunction)); |
3908 |
> |
|
3909 |
> |
funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); |
3910 |
> |
funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); |
3911 |
> |
funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3912 |
> |
funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3913 |
> |
funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3914 |
> |
funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3915 |
> |
|
3916 |
> |
funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3917 |
> |
|
3918 |
> |
funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); |
3919 |
> |
|
3920 |
> |
funs.add(y -> CompletableFuture.allOf(y)); |
3921 |
> |
funs.add(y -> CompletableFuture.allOf(y, v42)); |
3922 |
> |
funs.add(y -> CompletableFuture.allOf(v42, y)); |
3923 |
> |
funs.add(y -> CompletableFuture.anyOf(y)); |
3924 |
> |
funs.add(y -> CompletableFuture.anyOf(y, incomplete)); |
3925 |
> |
funs.add(y -> CompletableFuture.anyOf(incomplete, y)); |
3926 |
|
|
3927 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3928 |
|
fun : funs) { |
3979 |
|
public void testMinimalCompletionStage_minimality() { |
3980 |
|
if (!testImplementationDetails) return; |
3981 |
|
Function<Method, String> toSignature = |
3982 |
< |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3982 |
> |
method -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3983 |
|
Predicate<Method> isNotStatic = |
3984 |
< |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3984 |
> |
method -> (method.getModifiers() & Modifier.STATIC) == 0; |
3985 |
|
List<Method> minimalMethods = |
3986 |
|
Stream.of(Object.class, CompletionStage.class) |
3987 |
< |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3987 |
> |
.flatMap(klazz -> Stream.of(klazz.getMethods())) |
3988 |
|
.filter(isNotStatic) |
3989 |
|
.collect(Collectors.toList()); |
3990 |
|
// Methods from CompletableFuture permitted NOT to throw UOE |
4000 |
|
.collect(Collectors.toSet()); |
4001 |
|
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
4002 |
|
.filter(isNotStatic) |
4003 |
< |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
4003 |
> |
.filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
4004 |
|
.collect(Collectors.toList()); |
4005 |
|
|
4006 |
|
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
4160 |
|
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4161 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4162 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
4163 |
< |
minimal.thenAccept((x) -> g.complete(x)); |
4163 |
> |
minimal.thenAccept(x -> g.complete(x)); |
4164 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
4165 |
|
g.join(); |
4166 |
|
checkCompletedNormally(g, v1); |
4184 |
|
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4185 |
|
(Function<T, CompletableFuture<U>> f, |
4186 |
|
Function<U, CompletableFuture<V>> g) { |
4187 |
< |
return (x) -> f.apply(x).thenCompose(g); |
4187 |
> |
return x -> f.apply(x).thenCompose(g); |
4188 |
|
} |
4189 |
|
|
4190 |
|
static void assertZero(CompletableFuture<?> f) { |
4264 |
|
|
4265 |
|
// Some mutually non-commutative functions |
4266 |
|
Function<Long, CompletableFuture<Long>> triple |
4267 |
< |
= (x) -> Monad.unit(3 * x); |
4267 |
> |
= x -> Monad.unit(3 * x); |
4268 |
|
Function<Long, CompletableFuture<Long>> inc |
4269 |
< |
= (x) -> Monad.unit(x + 1); |
4269 |
> |
= x -> Monad.unit(x + 1); |
4270 |
|
|
4271 |
|
// unit is a right identity: m >>= unit === m |
4272 |
|
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4278 |
|
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4279 |
|
Monad.assertFutureEquals( |
4280 |
|
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4281 |
< |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4281 |
> |
unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); |
4282 |
|
|
4283 |
|
// The case for CompletableFuture as an additive monad is weaker... |
4284 |
|
|
4288 |
|
// left zero: zero >>= f === zero |
4289 |
|
Monad.assertZero(zero.thenCompose(inc)); |
4290 |
|
// right zero: f >>= (\x -> zero) === zero |
4291 |
< |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4291 |
> |
Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); |
4292 |
|
|
4293 |
|
// f plus zero === f |
4294 |
|
Monad.assertFutureEquals(Monad.unit(5L), |
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<>(); |
4348 |
|
final AtomicInteger count = new AtomicInteger(0); |
4349 |
|
for (int i = 0; i < n; i++) { |
4350 |
|
head.thenRun(() -> count.getAndIncrement()); |
4351 |
< |
head.thenAccept((x) -> count.getAndIncrement()); |
4352 |
< |
head.thenApply((x) -> count.getAndIncrement()); |
4351 |
> |
head.thenAccept(x -> count.getAndIncrement()); |
4352 |
> |
head.thenApply(x -> count.getAndIncrement()); |
4353 |
|
|
4354 |
|
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4355 |
|
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4359 |
|
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4360 |
|
|
4361 |
|
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4362 |
< |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4363 |
< |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4362 |
> |
head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4363 |
> |
head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
4364 |
|
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4365 |
< |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4366 |
< |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4365 |
> |
new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); |
4366 |
> |
new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); |
4367 |
|
} |
4368 |
|
head.complete(null); |
4369 |
|
assertEquals(5 * 3 * n, count.get()); |
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<>(); |
4381 |
|
f.complete(null); |
4382 |
|
|
4383 |
|
f = new CompletableFuture<>(); |
4384 |
< |
f.acceptEither(incomplete, (x) -> {}); |
4384 |
> |
f.acceptEither(incomplete, x -> {}); |
4385 |
|
f.complete(null); |
4386 |
|
|
4387 |
|
f = new CompletableFuture<>(); |
4388 |
< |
f.applyToEither(incomplete, (x) -> x); |
4388 |
> |
f.applyToEither(incomplete, x -> x); |
4389 |
|
f.complete(null); |
4390 |
|
|
4391 |
|
f = new CompletableFuture<>(); |
4399 |
|
f.complete(null); |
4400 |
|
|
4401 |
|
f = new CompletableFuture<>(); |
4402 |
< |
incomplete.acceptEither(f, (x) -> {}); |
4402 |
> |
incomplete.acceptEither(f, x -> {}); |
4403 |
|
f.complete(null); |
4404 |
|
|
4405 |
|
f = new CompletableFuture<>(); |
4406 |
< |
incomplete.applyToEither(f, (x) -> x); |
4406 |
> |
incomplete.applyToEither(f, x -> x); |
4407 |
|
f.complete(null); |
4408 |
|
|
4409 |
|
f = new CompletableFuture<>(); |
4461 |
|
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4462 |
|
} |
4463 |
|
|
4464 |
+ |
/** |
4465 |
+ |
* Checks for garbage retention when MinimalStage.toCompletableFuture() |
4466 |
+ |
* is invoked many times. |
4467 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4468 |
+ |
* |
4469 |
+ |
* As of 2016-07, fails with OOME: |
4470 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck |
4471 |
+ |
*/ |
4472 |
+ |
public void testToCompletableFutureGarbageRetention() throws Throwable { |
4473 |
+ |
final int n = expensiveTests ? 900_000 : 10; |
4474 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4475 |
+ |
CompletionStage minimal = neverCompleted.minimalCompletionStage(); |
4476 |
+ |
for (int i = 0; i < n; i++) |
4477 |
+ |
assertTrue(minimal.toCompletableFuture().cancel(true)); |
4478 |
+ |
} |
4479 |
+ |
|
4480 |
|
// static <U> U join(CompletionStage<U> stage) { |
4481 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4482 |
|
// stage.whenComplete((v, ex) -> { |