32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
|
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
– |
import java.util.concurrent.TimeUnit; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
|
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
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()); |
80 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
81 |
– |
try { |
79 |
|
assertEquals(value, f.getNow(null)); |
83 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
84 |
– |
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 |
|
|
374 |
|
checkCompletedNormally(f, "test"); |
375 |
|
} |
376 |
|
|
377 |
< |
abstract class CheckedAction { |
377 |
> |
abstract static class CheckedAction { |
378 |
|
int invocationCount = 0; |
379 |
|
final ExecutionMode m; |
380 |
|
CheckedAction(ExecutionMode m) { this.m = m; } |
386 |
|
void assertInvoked() { assertEquals(1, invocationCount); } |
387 |
|
} |
388 |
|
|
389 |
< |
abstract class CheckedIntegerAction extends CheckedAction { |
389 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
390 |
|
Integer value; |
391 |
|
CheckedIntegerAction(ExecutionMode m) { super(m); } |
392 |
|
void assertValue(Integer expected) { |
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
< |
class IntegerSupplier extends CheckedAction |
398 |
> |
static class IntegerSupplier extends CheckedAction |
399 |
|
implements Supplier<Integer> |
400 |
|
{ |
401 |
|
final Integer value; |
414 |
|
return (x == null) ? null : x + 1; |
415 |
|
} |
416 |
|
|
417 |
< |
class NoopConsumer extends CheckedIntegerAction |
417 |
> |
static class NoopConsumer extends CheckedIntegerAction |
418 |
|
implements Consumer<Integer> |
419 |
|
{ |
420 |
|
NoopConsumer(ExecutionMode m) { super(m); } |
424 |
|
} |
425 |
|
} |
426 |
|
|
427 |
< |
class IncFunction extends CheckedIntegerAction |
427 |
> |
static class IncFunction extends CheckedIntegerAction |
428 |
|
implements Function<Integer,Integer> |
429 |
|
{ |
430 |
|
IncFunction(ExecutionMode m) { super(m); } |
442 |
|
- ((y == null) ? 99 : y.intValue()); |
443 |
|
} |
444 |
|
|
445 |
< |
class SubtractAction extends CheckedIntegerAction |
445 |
> |
static class SubtractAction extends CheckedIntegerAction |
446 |
|
implements BiConsumer<Integer, Integer> |
447 |
|
{ |
448 |
|
SubtractAction(ExecutionMode m) { super(m); } |
452 |
|
} |
453 |
|
} |
454 |
|
|
455 |
< |
class SubtractFunction extends CheckedIntegerAction |
455 |
> |
static class SubtractFunction extends CheckedIntegerAction |
456 |
|
implements BiFunction<Integer, Integer, Integer> |
457 |
|
{ |
458 |
|
SubtractFunction(ExecutionMode m) { super(m); } |
462 |
|
} |
463 |
|
} |
464 |
|
|
465 |
< |
class Noop extends CheckedAction implements Runnable { |
465 |
> |
static class Noop extends CheckedAction implements Runnable { |
466 |
|
Noop(ExecutionMode m) { super(m); } |
467 |
|
public void run() { |
468 |
|
invoked(); |
469 |
|
} |
470 |
|
} |
471 |
|
|
472 |
< |
class FailingSupplier extends CheckedAction |
472 |
> |
static class FailingSupplier extends CheckedAction |
473 |
|
implements Supplier<Integer> |
474 |
|
{ |
475 |
|
final CFException ex; |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
< |
class FailingConsumer extends CheckedIntegerAction |
483 |
> |
static class FailingConsumer extends CheckedIntegerAction |
484 |
|
implements Consumer<Integer> |
485 |
|
{ |
486 |
|
final CFException ex; |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
< |
class FailingBiConsumer extends CheckedIntegerAction |
495 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
496 |
|
implements BiConsumer<Integer, Integer> |
497 |
|
{ |
498 |
|
final CFException ex; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
< |
class FailingFunction extends CheckedIntegerAction |
507 |
> |
static class FailingFunction extends CheckedIntegerAction |
508 |
|
implements Function<Integer, Integer> |
509 |
|
{ |
510 |
|
final CFException ex; |
516 |
|
} |
517 |
|
} |
518 |
|
|
519 |
< |
class FailingBiFunction extends CheckedIntegerAction |
519 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
520 |
|
implements BiFunction<Integer, Integer, Integer> |
521 |
|
{ |
522 |
|
final CFException ex; |
528 |
|
} |
529 |
|
} |
530 |
|
|
531 |
< |
class FailingRunnable extends CheckedAction implements Runnable { |
531 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
532 |
|
final CFException ex; |
533 |
|
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
534 |
|
public void run() { |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
< |
class CompletableFutureInc extends CheckedIntegerAction |
540 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
541 |
|
implements Function<Integer, CompletableFuture<Integer>> |
542 |
|
{ |
543 |
|
CompletableFutureInc(ExecutionMode m) { super(m); } |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
< |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
553 |
> |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
554 |
|
implements Function<Integer, CompletableFuture<Integer>> |
555 |
|
{ |
556 |
|
final CFException ex; |
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); |
3593 |
|
* copy returns a CompletableFuture that is completed normally, |
3594 |
|
* with the same value, when source is. |
3595 |
|
*/ |
3596 |
< |
public void testCopy() { |
3596 |
> |
public void testCopy_normalCompletion() { |
3597 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3598 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3599 |
> |
{ |
3600 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3601 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3602 |
|
CompletableFuture<Integer> g = f.copy(); |
3603 |
< |
checkIncomplete(f); |
3604 |
< |
checkIncomplete(g); |
3605 |
< |
f.complete(1); |
3606 |
< |
checkCompletedNormally(f, 1); |
3607 |
< |
checkCompletedNormally(g, 1); |
3608 |
< |
} |
3603 |
> |
if (createIncomplete) { |
3604 |
> |
checkIncomplete(f); |
3605 |
> |
checkIncomplete(g); |
3606 |
> |
assertTrue(f.complete(v1)); |
3607 |
> |
} |
3608 |
> |
checkCompletedNormally(f, v1); |
3609 |
> |
checkCompletedNormally(g, v1); |
3610 |
> |
}} |
3611 |
|
|
3612 |
|
/** |
3613 |
|
* copy returns a CompletableFuture that is completed exceptionally |
3614 |
|
* when source is. |
3615 |
|
*/ |
3616 |
< |
public void testCopy2() { |
3616 |
> |
public void testCopy_exceptionalCompletion() { |
3617 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3618 |
> |
{ |
3619 |
> |
CFException ex = new CFException(); |
3620 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3621 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
3622 |
|
CompletableFuture<Integer> g = f.copy(); |
3623 |
< |
checkIncomplete(f); |
3624 |
< |
checkIncomplete(g); |
3625 |
< |
CFException ex = new CFException(); |
3626 |
< |
f.completeExceptionally(ex); |
3623 |
> |
if (createIncomplete) { |
3624 |
> |
checkIncomplete(f); |
3625 |
> |
checkIncomplete(g); |
3626 |
> |
f.completeExceptionally(ex); |
3627 |
> |
} |
3628 |
|
checkCompletedExceptionally(f, ex); |
3629 |
|
checkCompletedWithWrappedException(g, ex); |
3630 |
+ |
}} |
3631 |
+ |
|
3632 |
+ |
/** |
3633 |
+ |
* Completion of a copy does not complete its source. |
3634 |
+ |
*/ |
3635 |
+ |
public void testCopy_oneWayPropagation() { |
3636 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3637 |
+ |
assertTrue(f.copy().complete(1)); |
3638 |
+ |
assertTrue(f.copy().complete(null)); |
3639 |
+ |
assertTrue(f.copy().cancel(true)); |
3640 |
+ |
assertTrue(f.copy().cancel(false)); |
3641 |
+ |
assertTrue(f.copy().completeExceptionally(new CFException())); |
3642 |
+ |
checkIncomplete(f); |
3643 |
|
} |
3644 |
|
|
3645 |
|
/** |
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<>(); |
4007 |
< |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
4007 |
> |
CompletionStage<Integer> min = |
4008 |
> |
new CompletableFuture<Integer>().minimalCompletionStage(); |
4009 |
> |
stages.add(min); |
4010 |
> |
stages.add(min.thenApply(x -> x)); |
4011 |
|
stages.add(CompletableFuture.completedStage(1)); |
4012 |
|
stages.add(CompletableFuture.failedStage(new CFException())); |
4013 |
|
|
4043 |
|
throw new Error("Methods did not throw UOE: " + bugs); |
4044 |
|
} |
4045 |
|
|
4046 |
+ |
/** |
4047 |
+ |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4048 |
+ |
* is completed normally, with the same value, when source is. |
4049 |
+ |
*/ |
4050 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
4051 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4052 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4053 |
+ |
{ |
4054 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4055 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4056 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4057 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4058 |
+ |
if (createIncomplete) { |
4059 |
+ |
checkIncomplete(f); |
4060 |
+ |
checkIncomplete(g); |
4061 |
+ |
assertTrue(f.complete(v1)); |
4062 |
+ |
} |
4063 |
+ |
checkCompletedNormally(f, v1); |
4064 |
+ |
checkCompletedNormally(g, v1); |
4065 |
+ |
}} |
4066 |
+ |
|
4067 |
+ |
/** |
4068 |
+ |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4069 |
+ |
* is completed exceptionally when source is. |
4070 |
+ |
*/ |
4071 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
4072 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4073 |
+ |
{ |
4074 |
+ |
CFException ex = new CFException(); |
4075 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4076 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4077 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4078 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4079 |
+ |
if (createIncomplete) { |
4080 |
+ |
checkIncomplete(f); |
4081 |
+ |
checkIncomplete(g); |
4082 |
+ |
f.completeExceptionally(ex); |
4083 |
+ |
} |
4084 |
+ |
checkCompletedExceptionally(f, ex); |
4085 |
+ |
checkCompletedWithWrappedException(g, ex); |
4086 |
+ |
}} |
4087 |
+ |
|
4088 |
+ |
/** |
4089 |
+ |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4090 |
+ |
*/ |
4091 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4092 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4093 |
+ |
{ |
4094 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4095 |
+ |
CompletionStage minimal = f.minimalCompletionStage(); |
4096 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4097 |
+ |
assertTrue(g.complete(v1)); |
4098 |
+ |
checkCompletedNormally(g, v1); |
4099 |
+ |
checkIncomplete(f); |
4100 |
+ |
checkIncomplete(minimal.toCompletableFuture()); |
4101 |
+ |
}} |
4102 |
+ |
|
4103 |
+ |
/** |
4104 |
+ |
* minimalStage.toCompletableFuture().join() awaits completion |
4105 |
+ |
*/ |
4106 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4107 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4108 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4109 |
+ |
{ |
4110 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4111 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4112 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4113 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4114 |
+ |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4115 |
+ |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4116 |
+ |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4117 |
+ |
}} |
4118 |
+ |
|
4119 |
+ |
/** |
4120 |
+ |
* Completion of a toCompletableFuture copy of a minimal stage |
4121 |
+ |
* does not complete its source. |
4122 |
+ |
*/ |
4123 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
4124 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4125 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
4126 |
+ |
assertTrue(g.toCompletableFuture().complete(1)); |
4127 |
+ |
assertTrue(g.toCompletableFuture().complete(null)); |
4128 |
+ |
assertTrue(g.toCompletableFuture().cancel(true)); |
4129 |
+ |
assertTrue(g.toCompletableFuture().cancel(false)); |
4130 |
+ |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
4131 |
+ |
checkIncomplete(g.toCompletableFuture()); |
4132 |
+ |
f.complete(1); |
4133 |
+ |
checkCompletedNormally(g.toCompletableFuture(), 1); |
4134 |
+ |
} |
4135 |
+ |
|
4136 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4137 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4138 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4139 |
+ |
stage.handle((T t, Throwable ex) -> { |
4140 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4141 |
+ |
else f.complete(t); |
4142 |
+ |
return null; |
4143 |
+ |
}); |
4144 |
+ |
return f; |
4145 |
+ |
} |
4146 |
+ |
|
4147 |
+ |
/** Demo utility method to join a CompletionStage */ |
4148 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4149 |
+ |
return toCompletableFuture(stage).join(); |
4150 |
+ |
} |
4151 |
+ |
|
4152 |
+ |
/** |
4153 |
+ |
* Joining a minimal stage "by hand" works |
4154 |
+ |
*/ |
4155 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4156 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4157 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4158 |
+ |
{ |
4159 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
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)); |
4164 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4165 |
+ |
g.join(); |
4166 |
+ |
checkCompletedNormally(g, v1); |
4167 |
+ |
checkCompletedNormally(f, v1); |
4168 |
+ |
assertEquals(v1, join(minimal)); |
4169 |
+ |
}} |
4170 |
+ |
|
4171 |
|
static class Monad { |
4172 |
|
static class ZeroException extends RuntimeException { |
4173 |
|
public ZeroException() { super("monadic zero"); } |
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<>(); |
4413 |
|
} |
4414 |
|
|
4415 |
|
/** |
4416 |
< |
* Checks for garbage retention with anyOf. |
4417 |
< |
* Following used to fail with OOME: |
4418 |
< |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck |
4416 |
> |
* Reproduction recipe for: |
4417 |
> |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4418 |
> |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
4419 |
|
*/ |
4420 |
|
public void testAnyOfGarbageRetention() throws Throwable { |
4421 |
|
for (Integer v : new Integer[] { 1, null }) |
4449 |
|
/** |
4450 |
|
* Checks for garbage retention when a dependent future is |
4451 |
|
* cancelled and garbage-collected. |
4452 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4453 |
|
* |
4454 |
|
* As of 2016-07, fails with OOME: |
4455 |
|
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
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) -> { |