30 |
|
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
34 |
– |
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); } |
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) { |
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 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
475 |
> |
final CFException ex; |
476 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
477 |
|
public Integer get() { |
478 |
|
invoked(); |
479 |
< |
throw new CFException(); |
479 |
> |
throw ex; |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
< |
class FailingConsumer extends CheckedIntegerAction |
483 |
> |
static class FailingConsumer extends CheckedIntegerAction |
484 |
|
implements Consumer<Integer> |
485 |
|
{ |
486 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
486 |
> |
final CFException ex; |
487 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
488 |
|
public void accept(Integer x) { |
489 |
|
invoked(); |
490 |
|
value = x; |
491 |
< |
throw new CFException(); |
491 |
> |
throw ex; |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
< |
class FailingBiConsumer extends CheckedIntegerAction |
495 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
496 |
|
implements BiConsumer<Integer, Integer> |
497 |
|
{ |
498 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
498 |
> |
final CFException ex; |
499 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
500 |
|
public void accept(Integer x, Integer y) { |
501 |
|
invoked(); |
502 |
|
value = subtract(x, y); |
503 |
< |
throw new CFException(); |
503 |
> |
throw ex; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
< |
class FailingFunction extends CheckedIntegerAction |
507 |
> |
static class FailingFunction extends CheckedIntegerAction |
508 |
|
implements Function<Integer, Integer> |
509 |
|
{ |
510 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
510 |
> |
final CFException ex; |
511 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
512 |
|
public Integer apply(Integer x) { |
513 |
|
invoked(); |
514 |
|
value = x; |
515 |
< |
throw new CFException(); |
515 |
> |
throw ex; |
516 |
|
} |
517 |
|
} |
518 |
|
|
519 |
< |
class FailingBiFunction extends CheckedIntegerAction |
519 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
520 |
|
implements BiFunction<Integer, Integer, Integer> |
521 |
|
{ |
522 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
522 |
> |
final CFException ex; |
523 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
524 |
|
public Integer apply(Integer x, Integer y) { |
525 |
|
invoked(); |
526 |
|
value = subtract(x, y); |
527 |
< |
throw new CFException(); |
527 |
> |
throw ex; |
528 |
|
} |
529 |
|
} |
530 |
|
|
531 |
< |
class FailingRunnable extends CheckedAction implements Runnable { |
532 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
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() { |
535 |
|
invoked(); |
536 |
< |
throw new CFException(); |
536 |
> |
throw ex; |
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 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
556 |
> |
final CFException ex; |
557 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
558 |
|
public CompletableFuture<Integer> apply(Integer x) { |
559 |
|
invoked(); |
560 |
|
value = x; |
561 |
< |
throw new CFException(); |
561 |
> |
throw ex; |
562 |
> |
} |
563 |
> |
} |
564 |
> |
|
565 |
> |
static class CountingRejectingExecutor implements Executor { |
566 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
567 |
> |
final AtomicInteger count = new AtomicInteger(0); |
568 |
> |
public void execute(Runnable r) { |
569 |
> |
count.getAndIncrement(); |
570 |
> |
throw ex; |
571 |
|
} |
572 |
|
} |
573 |
|
|
1251 |
|
{ |
1252 |
|
final FailingRunnable r = new FailingRunnable(m); |
1253 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1254 |
< |
checkCompletedWithWrappedCFException(f); |
1254 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1255 |
|
r.assertInvoked(); |
1256 |
|
}} |
1257 |
|
|
1258 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1259 |
+ |
public void testRunAsync_rejectingExecutor() { |
1260 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1261 |
+ |
try { |
1262 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1263 |
+ |
shouldThrow(); |
1264 |
+ |
} catch (Throwable t) { |
1265 |
+ |
assertSame(e.ex, t); |
1266 |
+ |
} |
1267 |
+ |
|
1268 |
+ |
assertEquals(1, e.count.get()); |
1269 |
+ |
} |
1270 |
+ |
|
1271 |
|
/** |
1272 |
|
* supplyAsync completes with result of supplier |
1273 |
|
*/ |
1298 |
|
{ |
1299 |
|
FailingSupplier r = new FailingSupplier(m); |
1300 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1301 |
< |
checkCompletedWithWrappedCFException(f); |
1301 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1302 |
|
r.assertInvoked(); |
1303 |
|
}} |
1304 |
|
|
1305 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1306 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1307 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1308 |
+ |
try { |
1309 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1310 |
+ |
shouldThrow(); |
1311 |
+ |
} catch (Throwable t) { |
1312 |
+ |
assertSame(e.ex, t); |
1313 |
+ |
} |
1314 |
+ |
|
1315 |
+ |
assertEquals(1, e.count.get()); |
1316 |
+ |
} |
1317 |
+ |
|
1318 |
|
// seq completion methods |
1319 |
|
|
1320 |
|
/** |
1433 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1434 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1435 |
|
|
1436 |
< |
checkCompletedWithWrappedCFException(h0); |
1437 |
< |
checkCompletedWithWrappedCFException(h1); |
1438 |
< |
checkCompletedWithWrappedCFException(h2); |
1439 |
< |
checkCompletedWithWrappedCFException(h3); |
1440 |
< |
checkCompletedWithWrappedCFException(h4); |
1441 |
< |
checkCompletedWithWrappedCFException(h5); |
1436 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1437 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1438 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1439 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1440 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1441 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1442 |
|
checkCompletedNormally(f, v1); |
1443 |
|
}} |
1444 |
|
|
1537 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1538 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1539 |
|
|
1540 |
< |
checkCompletedWithWrappedCFException(h0); |
1541 |
< |
checkCompletedWithWrappedCFException(h1); |
1542 |
< |
checkCompletedWithWrappedCFException(h2); |
1543 |
< |
checkCompletedWithWrappedCFException(h3); |
1540 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1541 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1542 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1543 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1544 |
|
checkCompletedNormally(f, v1); |
1545 |
|
}} |
1546 |
|
|
1639 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1640 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1641 |
|
|
1642 |
< |
checkCompletedWithWrappedCFException(h0); |
1643 |
< |
checkCompletedWithWrappedCFException(h1); |
1644 |
< |
checkCompletedWithWrappedCFException(h2); |
1645 |
< |
checkCompletedWithWrappedCFException(h3); |
1642 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1643 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1644 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1645 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1646 |
|
checkCompletedNormally(f, v1); |
1647 |
|
}} |
1648 |
|
|
1804 |
|
assertTrue(snd.complete(w2)); |
1805 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1806 |
|
|
1807 |
< |
checkCompletedWithWrappedCFException(h1); |
1808 |
< |
checkCompletedWithWrappedCFException(h2); |
1809 |
< |
checkCompletedWithWrappedCFException(h3); |
1807 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1808 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1809 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1810 |
|
r1.assertInvoked(); |
1811 |
|
r2.assertInvoked(); |
1812 |
|
r3.assertInvoked(); |
1968 |
|
assertTrue(snd.complete(w2)); |
1969 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1970 |
|
|
1971 |
< |
checkCompletedWithWrappedCFException(h1); |
1972 |
< |
checkCompletedWithWrappedCFException(h2); |
1973 |
< |
checkCompletedWithWrappedCFException(h3); |
1971 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1972 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1973 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1974 |
|
r1.assertInvoked(); |
1975 |
|
r2.assertInvoked(); |
1976 |
|
r3.assertInvoked(); |
2132 |
|
assertTrue(snd.complete(w2)); |
2133 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2134 |
|
|
2135 |
< |
checkCompletedWithWrappedCFException(h1); |
2136 |
< |
checkCompletedWithWrappedCFException(h2); |
2137 |
< |
checkCompletedWithWrappedCFException(h3); |
2135 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2136 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2137 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2138 |
|
r1.assertInvoked(); |
2139 |
|
r2.assertInvoked(); |
2140 |
|
r3.assertInvoked(); |
2424 |
|
f.complete(v1); |
2425 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2426 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2427 |
< |
checkCompletedWithWrappedCFException(h0); |
2428 |
< |
checkCompletedWithWrappedCFException(h1); |
2429 |
< |
checkCompletedWithWrappedCFException(h2); |
2430 |
< |
checkCompletedWithWrappedCFException(h3); |
2427 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2428 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2429 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2430 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2431 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2432 |
|
|
2433 |
|
g.complete(v2); |
2436 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2437 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2438 |
|
|
2439 |
< |
checkCompletedWithWrappedCFException(h4); |
2439 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2440 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2441 |
|
Objects.equals(v2, rs[4].value)); |
2442 |
< |
checkCompletedWithWrappedCFException(h5); |
2442 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2443 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2444 |
|
Objects.equals(v2, rs[5].value)); |
2445 |
|
|
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); |
2683 |
|
f.complete(v1); |
2684 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2685 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2686 |
< |
checkCompletedWithWrappedCFException(h0); |
2687 |
< |
checkCompletedWithWrappedCFException(h1); |
2688 |
< |
checkCompletedWithWrappedCFException(h2); |
2689 |
< |
checkCompletedWithWrappedCFException(h3); |
2686 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2687 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2688 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2689 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2690 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2691 |
|
|
2692 |
|
g.complete(v2); |
2695 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2696 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2697 |
|
|
2698 |
< |
checkCompletedWithWrappedCFException(h4); |
2698 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2699 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2700 |
|
Objects.equals(v2, rs[4].value)); |
2701 |
< |
checkCompletedWithWrappedCFException(h5); |
2701 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2702 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2703 |
|
Objects.equals(v2, rs[5].value)); |
2704 |
|
|
2714 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2715 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2716 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2717 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2718 |
|
{ |
2719 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2720 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2727 |
|
checkIncomplete(h1); |
2728 |
|
rs[0].assertNotInvoked(); |
2729 |
|
rs[1].assertNotInvoked(); |
2730 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2731 |
+ |
m.thenRun(f, () -> {}); |
2732 |
+ |
m.thenRun(g, () -> {}); |
2733 |
+ |
} |
2734 |
|
f.complete(v1); |
2735 |
|
checkCompletedNormally(h0, null); |
2736 |
|
checkCompletedNormally(h1, null); |
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); |
2943 |
|
assertTrue(f.complete(v1)); |
2944 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2945 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2946 |
< |
checkCompletedWithWrappedCFException(h0); |
2947 |
< |
checkCompletedWithWrappedCFException(h1); |
2948 |
< |
checkCompletedWithWrappedCFException(h2); |
2949 |
< |
checkCompletedWithWrappedCFException(h3); |
2946 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2947 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2948 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2949 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2950 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2951 |
|
assertTrue(g.complete(v2)); |
2952 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2953 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2954 |
< |
checkCompletedWithWrappedCFException(h4); |
2955 |
< |
checkCompletedWithWrappedCFException(h5); |
2954 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2955 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2956 |
|
|
2957 |
|
checkCompletedNormally(f, v1); |
2958 |
|
checkCompletedNormally(g, v2); |
3013 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3014 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
3015 |
|
|
3016 |
< |
checkCompletedWithWrappedCFException(g); |
3016 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3017 |
|
checkCompletedNormally(f, v1); |
3018 |
|
}} |
3019 |
|
|
3122 |
|
} |
3123 |
|
} |
3124 |
|
|
3125 |
< |
public void testAllOf_backwards() throws Exception { |
3125 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3126 |
|
for (int k = 1; k < 10; k++) { |
3127 |
|
CompletableFuture<Integer>[] fs |
3128 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
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), |
3371 |
|
} |
3372 |
|
|
3373 |
|
/** |
3374 |
+ |
* Test submissions to an executor that rejects all tasks. |
3375 |
+ |
*/ |
3376 |
+ |
public void testRejectingExecutor() { |
3377 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3378 |
+ |
{ |
3379 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3380 |
+ |
|
3381 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3382 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3383 |
+ |
|
3384 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3385 |
+ |
|
3386 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3387 |
+ |
srcs.add(complete); |
3388 |
+ |
srcs.add(incomplete); |
3389 |
+ |
|
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)); |
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)); |
3402 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3403 |
+ |
|
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 |
+ |
|
3408 |
+ |
for (CompletableFuture<?> future : fs) { |
3409 |
+ |
if (src.isDone()) |
3410 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3411 |
+ |
else |
3412 |
+ |
checkIncomplete(future); |
3413 |
+ |
} |
3414 |
+ |
futures.addAll(fs); |
3415 |
+ |
} |
3416 |
+ |
|
3417 |
+ |
{ |
3418 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3419 |
+ |
|
3420 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3421 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3422 |
+ |
|
3423 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3424 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3425 |
+ |
|
3426 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3427 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3428 |
+ |
|
3429 |
+ |
for (CompletableFuture<?> future : fs) |
3430 |
+ |
checkIncomplete(future); |
3431 |
+ |
futures.addAll(fs); |
3432 |
+ |
} |
3433 |
+ |
|
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)); |
3439 |
+ |
|
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)); |
3445 |
+ |
|
3446 |
+ |
for (CompletableFuture<?> future : fs) |
3447 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3448 |
+ |
futures.addAll(fs); |
3449 |
+ |
} |
3450 |
+ |
|
3451 |
+ |
incomplete.complete(v); |
3452 |
+ |
|
3453 |
+ |
for (CompletableFuture<?> future : futures) |
3454 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3455 |
+ |
|
3456 |
+ |
assertEquals(futures.size(), e.count.get()); |
3457 |
+ |
}} |
3458 |
+ |
|
3459 |
+ |
/** |
3460 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3461 |
+ |
* should never be invoked because the dependent future is |
3462 |
+ |
* explicitly completed. |
3463 |
+ |
*/ |
3464 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3465 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3466 |
+ |
{ |
3467 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3468 |
+ |
|
3469 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3470 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3471 |
+ |
|
3472 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3473 |
+ |
|
3474 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3475 |
+ |
srcs.add(complete); |
3476 |
+ |
srcs.add(incomplete); |
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)); |
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)); |
3489 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3490 |
+ |
|
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 |
+ |
|
3495 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3496 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3497 |
+ |
|
3498 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3499 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3500 |
+ |
|
3501 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3502 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3503 |
+ |
|
3504 |
+ |
for (CompletableFuture<?> future : fs) |
3505 |
+ |
checkIncomplete(future); |
3506 |
+ |
|
3507 |
+ |
for (CompletableFuture<?> future : fs) |
3508 |
+ |
future.complete(null); |
3509 |
+ |
|
3510 |
+ |
incomplete.complete(v); |
3511 |
+ |
|
3512 |
+ |
for (CompletableFuture<?> future : fs) |
3513 |
+ |
checkCompletedNormally(future, null); |
3514 |
+ |
|
3515 |
+ |
assertEquals(0, e.count.get()); |
3516 |
+ |
}} |
3517 |
+ |
|
3518 |
+ |
/** |
3519 |
|
* toCompletableFuture returns this CompletableFuture. |
3520 |
|
*/ |
3521 |
|
public void testToCompletableFuture() { |
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(); |
3891 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3892 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3893 |
|
|
3894 |
+ |
final Runnable noopRunnable = new Noop(m); |
3895 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3896 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3897 |
+ |
|
3898 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3899 |
|
= new ArrayList<>(); |
3900 |
|
|
3901 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3902 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3903 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3904 |
< |
|
3905 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3906 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3907 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3908 |
< |
|
3909 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3910 |
< |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
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(new CompletableFuture<?>[] {y})); |
3921 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3922 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3923 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3924 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3925 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {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) { |
3929 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3930 |
|
f.completeExceptionally(ex); |
3931 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3931 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3932 |
|
checkCompletedWithWrappedException(src, ex); |
3933 |
|
CompletableFuture<?> dep = fun.apply(src); |
3934 |
|
checkCompletedWithWrappedException(dep, ex); |
3938 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3939 |
|
fun : funs) { |
3940 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3941 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3941 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3942 |
|
CompletableFuture<?> dep = fun.apply(src); |
3943 |
|
f.completeExceptionally(ex); |
3944 |
|
checkCompletedWithWrappedException(src, ex); |
3952 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3953 |
|
f.cancel(mayInterruptIfRunning); |
3954 |
|
checkCancelled(f); |
3955 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3955 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3956 |
|
checkCompletedWithWrappedCancellationException(src); |
3957 |
|
CompletableFuture<?> dep = fun.apply(src); |
3958 |
|
checkCompletedWithWrappedCancellationException(dep); |
3963 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3964 |
|
fun : funs) { |
3965 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3966 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3966 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3967 |
|
CompletableFuture<?> dep = fun.apply(src); |
3968 |
|
f.cancel(mayInterruptIfRunning); |
3969 |
|
checkCancelled(f); |
3974 |
|
}} |
3975 |
|
|
3976 |
|
/** |
3977 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3977 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3978 |
|
*/ |
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 |
< |
CompletionStage<Integer> minimalStage = |
4006 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
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 |
|
|
4014 |
|
List<Method> bugs = new ArrayList<>(); |
4015 |
|
for (Method method : allMethods) { |
4025 |
|
else if (parameterTypes[i] == long.class) |
4026 |
|
args[i] = 0L; |
4027 |
|
} |
4028 |
< |
try { |
4029 |
< |
method.invoke(minimalStage, args); |
4030 |
< |
bugs.add(method); |
3792 |
< |
} |
3793 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3794 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4028 |
> |
for (CompletionStage<Integer> stage : stages) { |
4029 |
> |
try { |
4030 |
> |
method.invoke(stage, args); |
4031 |
|
bugs.add(method); |
3796 |
– |
// expected.getCause().printStackTrace(); |
4032 |
|
} |
4033 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
4034 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4035 |
+ |
bugs.add(method); |
4036 |
+ |
// expected.getCause().printStackTrace(); |
4037 |
+ |
} |
4038 |
+ |
} |
4039 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4040 |
|
} |
3799 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4041 |
|
} |
4042 |
|
if (!bugs.isEmpty()) |
4043 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
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), |
4314 |
|
Monad.plus(godot, 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 }) |
4322 |
+ |
{ |
4323 |
+ |
final int val = 42; |
4324 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4325 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4326 |
+ |
CompletableFuture<Integer> tail = head; |
4327 |
+ |
for (int i = 0; i < n; i++) { |
4328 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4329 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4330 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4331 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4332 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4333 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4334 |
+ |
} |
4335 |
+ |
head.complete(val); |
4336 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4337 |
+ |
}} |
4338 |
+ |
|
4339 |
|
/** |
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 = 1_000; |
4345 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4346 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4347 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
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<>(); |
4377 |
+ |
CompletableFuture<Integer> f; |
4378 |
+ |
for (int i = 0; i < n; i++) { |
4379 |
+ |
f = new CompletableFuture<>(); |
4380 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4381 |
+ |
f.complete(null); |
4382 |
+ |
|
4383 |
+ |
f = new CompletableFuture<>(); |
4384 |
+ |
f.acceptEither(incomplete, x -> {}); |
4385 |
+ |
f.complete(null); |
4386 |
+ |
|
4387 |
+ |
f = new CompletableFuture<>(); |
4388 |
+ |
f.applyToEither(incomplete, x -> x); |
4389 |
+ |
f.complete(null); |
4390 |
+ |
|
4391 |
+ |
f = new CompletableFuture<>(); |
4392 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4393 |
+ |
f.complete(null); |
4394 |
+ |
} |
4395 |
+ |
|
4396 |
+ |
for (int i = 0; i < n; i++) { |
4397 |
+ |
f = new CompletableFuture<>(); |
4398 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4399 |
+ |
f.complete(null); |
4400 |
+ |
|
4401 |
+ |
f = new CompletableFuture<>(); |
4402 |
+ |
incomplete.acceptEither(f, x -> {}); |
4403 |
+ |
f.complete(null); |
4404 |
+ |
|
4405 |
+ |
f = new CompletableFuture<>(); |
4406 |
+ |
incomplete.applyToEither(f, x -> x); |
4407 |
+ |
f.complete(null); |
4408 |
+ |
|
4409 |
+ |
f = new CompletableFuture<>(); |
4410 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4411 |
+ |
f.complete(null); |
4412 |
+ |
} |
4413 |
+ |
} |
4414 |
+ |
|
4415 |
+ |
/** |
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 }) |
4422 |
+ |
{ |
4423 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4424 |
+ |
CompletableFuture<Integer>[] fs |
4425 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4426 |
+ |
for (int i = 0; i < fs.length; i++) |
4427 |
+ |
fs[i] = new CompletableFuture<>(); |
4428 |
+ |
fs[fs.length - 1].complete(v); |
4429 |
+ |
for (int i = 0; i < n; i++) |
4430 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4431 |
+ |
}} |
4432 |
+ |
|
4433 |
+ |
/** |
4434 |
+ |
* Checks for garbage retention with allOf. |
4435 |
+ |
* |
4436 |
+ |
* As of 2016-07, fails with OOME: |
4437 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4438 |
+ |
*/ |
4439 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4440 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4441 |
+ |
CompletableFuture<Integer>[] fs |
4442 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4443 |
+ |
for (int i = 0; i < fs.length; i++) |
4444 |
+ |
fs[i] = new CompletableFuture<>(); |
4445 |
+ |
for (int i = 0; i < n; i++) |
4446 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4447 |
+ |
} |
4448 |
+ |
|
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 |
4456 |
+ |
*/ |
4457 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4458 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4459 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4460 |
+ |
for (int i = 0; i < n; i++) |
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) -> { |