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; |
35 |
|
import java.util.concurrent.TimeUnit; |
36 |
|
import java.util.concurrent.atomic.AtomicInteger; |
460 |
|
class FailingSupplier extends CheckedAction |
461 |
|
implements Supplier<Integer> |
462 |
|
{ |
463 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
463 |
> |
final CFException ex; |
464 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
465 |
|
public Integer get() { |
466 |
|
invoked(); |
467 |
< |
throw new CFException(); |
467 |
> |
throw ex; |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
class FailingConsumer extends CheckedIntegerAction |
472 |
|
implements Consumer<Integer> |
473 |
|
{ |
474 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
474 |
> |
final CFException ex; |
475 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
476 |
|
public void accept(Integer x) { |
477 |
|
invoked(); |
478 |
|
value = x; |
479 |
< |
throw new CFException(); |
479 |
> |
throw ex; |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
|
class FailingBiConsumer extends CheckedIntegerAction |
484 |
|
implements BiConsumer<Integer, Integer> |
485 |
|
{ |
486 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
486 |
> |
final CFException ex; |
487 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
488 |
|
public void accept(Integer x, Integer y) { |
489 |
|
invoked(); |
490 |
|
value = subtract(x, y); |
491 |
< |
throw new CFException(); |
491 |
> |
throw ex; |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
|
class FailingFunction extends CheckedIntegerAction |
496 |
|
implements Function<Integer, Integer> |
497 |
|
{ |
498 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
498 |
> |
final CFException ex; |
499 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
500 |
|
public Integer apply(Integer x) { |
501 |
|
invoked(); |
502 |
|
value = x; |
503 |
< |
throw new CFException(); |
503 |
> |
throw ex; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
class FailingBiFunction extends CheckedIntegerAction |
508 |
|
implements BiFunction<Integer, Integer, Integer> |
509 |
|
{ |
510 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
510 |
> |
final CFException ex; |
511 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
512 |
|
public Integer apply(Integer x, Integer y) { |
513 |
|
invoked(); |
514 |
|
value = subtract(x, y); |
515 |
< |
throw new CFException(); |
515 |
> |
throw ex; |
516 |
|
} |
517 |
|
} |
518 |
|
|
519 |
|
class FailingRunnable extends CheckedAction implements Runnable { |
520 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
520 |
> |
final CFException ex; |
521 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
522 |
|
public void run() { |
523 |
|
invoked(); |
524 |
< |
throw new CFException(); |
524 |
> |
throw ex; |
525 |
|
} |
526 |
|
} |
527 |
|
|
541 |
|
class FailingCompletableFutureFunction extends CheckedIntegerAction |
542 |
|
implements Function<Integer, CompletableFuture<Integer>> |
543 |
|
{ |
544 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
544 |
> |
final CFException ex; |
545 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
546 |
|
public CompletableFuture<Integer> apply(Integer x) { |
547 |
|
invoked(); |
548 |
|
value = x; |
549 |
< |
throw new CFException(); |
549 |
> |
throw ex; |
550 |
> |
} |
551 |
> |
} |
552 |
> |
|
553 |
> |
static class CountingRejectingExecutor implements Executor { |
554 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
555 |
> |
final AtomicInteger count = new AtomicInteger(0); |
556 |
> |
public void execute(Runnable r) { |
557 |
> |
count.getAndIncrement(); |
558 |
> |
throw ex; |
559 |
|
} |
560 |
|
} |
561 |
|
|
1239 |
|
{ |
1240 |
|
final FailingRunnable r = new FailingRunnable(m); |
1241 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1242 |
< |
checkCompletedWithWrappedCFException(f); |
1242 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1243 |
|
r.assertInvoked(); |
1244 |
|
}} |
1245 |
|
|
1246 |
+ |
public void testRunAsync_rejectingExecutor() { |
1247 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1248 |
+ |
try { |
1249 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1250 |
+ |
shouldThrow(); |
1251 |
+ |
} catch (Throwable t) { |
1252 |
+ |
assertSame(e.ex, t); |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
assertEquals(1, e.count.get()); |
1256 |
+ |
} |
1257 |
+ |
|
1258 |
|
/** |
1259 |
|
* supplyAsync completes with result of supplier |
1260 |
|
*/ |
1285 |
|
{ |
1286 |
|
FailingSupplier r = new FailingSupplier(m); |
1287 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1288 |
< |
checkCompletedWithWrappedCFException(f); |
1288 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1289 |
|
r.assertInvoked(); |
1290 |
|
}} |
1291 |
|
|
1292 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1293 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1294 |
+ |
try { |
1295 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1296 |
+ |
shouldThrow(); |
1297 |
+ |
} catch (Throwable t) { |
1298 |
+ |
assertSame(e.ex, t); |
1299 |
+ |
} |
1300 |
+ |
|
1301 |
+ |
assertEquals(1, e.count.get()); |
1302 |
+ |
} |
1303 |
+ |
|
1304 |
|
// seq completion methods |
1305 |
|
|
1306 |
|
/** |
1419 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1420 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1421 |
|
|
1422 |
< |
checkCompletedWithWrappedCFException(h0); |
1423 |
< |
checkCompletedWithWrappedCFException(h1); |
1424 |
< |
checkCompletedWithWrappedCFException(h2); |
1425 |
< |
checkCompletedWithWrappedCFException(h3); |
1426 |
< |
checkCompletedWithWrappedCFException(h4); |
1427 |
< |
checkCompletedWithWrappedCFException(h5); |
1422 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1423 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1424 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1425 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1426 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1427 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1428 |
|
checkCompletedNormally(f, v1); |
1429 |
|
}} |
1430 |
|
|
1523 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1524 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1525 |
|
|
1526 |
< |
checkCompletedWithWrappedCFException(h0); |
1527 |
< |
checkCompletedWithWrappedCFException(h1); |
1528 |
< |
checkCompletedWithWrappedCFException(h2); |
1529 |
< |
checkCompletedWithWrappedCFException(h3); |
1526 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1527 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1528 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1529 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1530 |
|
checkCompletedNormally(f, v1); |
1531 |
|
}} |
1532 |
|
|
1625 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1626 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1627 |
|
|
1628 |
< |
checkCompletedWithWrappedCFException(h0); |
1629 |
< |
checkCompletedWithWrappedCFException(h1); |
1630 |
< |
checkCompletedWithWrappedCFException(h2); |
1631 |
< |
checkCompletedWithWrappedCFException(h3); |
1628 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1629 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1630 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1631 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1632 |
|
checkCompletedNormally(f, v1); |
1633 |
|
}} |
1634 |
|
|
1790 |
|
assertTrue(snd.complete(w2)); |
1791 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1792 |
|
|
1793 |
< |
checkCompletedWithWrappedCFException(h1); |
1794 |
< |
checkCompletedWithWrappedCFException(h2); |
1795 |
< |
checkCompletedWithWrappedCFException(h3); |
1793 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1794 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1795 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1796 |
|
r1.assertInvoked(); |
1797 |
|
r2.assertInvoked(); |
1798 |
|
r3.assertInvoked(); |
1954 |
|
assertTrue(snd.complete(w2)); |
1955 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1956 |
|
|
1957 |
< |
checkCompletedWithWrappedCFException(h1); |
1958 |
< |
checkCompletedWithWrappedCFException(h2); |
1959 |
< |
checkCompletedWithWrappedCFException(h3); |
1957 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1958 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1959 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1960 |
|
r1.assertInvoked(); |
1961 |
|
r2.assertInvoked(); |
1962 |
|
r3.assertInvoked(); |
2118 |
|
assertTrue(snd.complete(w2)); |
2119 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2120 |
|
|
2121 |
< |
checkCompletedWithWrappedCFException(h1); |
2122 |
< |
checkCompletedWithWrappedCFException(h2); |
2123 |
< |
checkCompletedWithWrappedCFException(h3); |
2121 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2122 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2123 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2124 |
|
r1.assertInvoked(); |
2125 |
|
r2.assertInvoked(); |
2126 |
|
r3.assertInvoked(); |
2410 |
|
f.complete(v1); |
2411 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2412 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2413 |
< |
checkCompletedWithWrappedCFException(h0); |
2414 |
< |
checkCompletedWithWrappedCFException(h1); |
2415 |
< |
checkCompletedWithWrappedCFException(h2); |
2416 |
< |
checkCompletedWithWrappedCFException(h3); |
2413 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2414 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2415 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2416 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2417 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2418 |
|
|
2419 |
|
g.complete(v2); |
2422 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2423 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2424 |
|
|
2425 |
< |
checkCompletedWithWrappedCFException(h4); |
2425 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2426 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2427 |
|
Objects.equals(v2, rs[4].value)); |
2428 |
< |
checkCompletedWithWrappedCFException(h5); |
2428 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2429 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2430 |
|
Objects.equals(v2, rs[5].value)); |
2431 |
|
|
2669 |
|
f.complete(v1); |
2670 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2671 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2672 |
< |
checkCompletedWithWrappedCFException(h0); |
2673 |
< |
checkCompletedWithWrappedCFException(h1); |
2674 |
< |
checkCompletedWithWrappedCFException(h2); |
2675 |
< |
checkCompletedWithWrappedCFException(h3); |
2672 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2673 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2674 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2675 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2676 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2677 |
|
|
2678 |
|
g.complete(v2); |
2681 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2682 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2683 |
|
|
2684 |
< |
checkCompletedWithWrappedCFException(h4); |
2684 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2685 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2686 |
|
Objects.equals(v2, rs[4].value)); |
2687 |
< |
checkCompletedWithWrappedCFException(h5); |
2687 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2688 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2689 |
|
Objects.equals(v2, rs[5].value)); |
2690 |
|
|
2700 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2701 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2702 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2703 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2704 |
|
{ |
2705 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2706 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2713 |
|
checkIncomplete(h1); |
2714 |
|
rs[0].assertNotInvoked(); |
2715 |
|
rs[1].assertNotInvoked(); |
2716 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2717 |
+ |
m.thenRun(f, () -> {}); |
2718 |
+ |
m.thenRun(g, () -> {}); |
2719 |
+ |
} |
2720 |
|
f.complete(v1); |
2721 |
|
checkCompletedNormally(h0, null); |
2722 |
|
checkCompletedNormally(h1, null); |
2929 |
|
assertTrue(f.complete(v1)); |
2930 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2931 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2932 |
< |
checkCompletedWithWrappedCFException(h0); |
2933 |
< |
checkCompletedWithWrappedCFException(h1); |
2934 |
< |
checkCompletedWithWrappedCFException(h2); |
2935 |
< |
checkCompletedWithWrappedCFException(h3); |
2932 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2933 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2934 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2935 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2936 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2937 |
|
assertTrue(g.complete(v2)); |
2938 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2939 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2940 |
< |
checkCompletedWithWrappedCFException(h4); |
2941 |
< |
checkCompletedWithWrappedCFException(h5); |
2940 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2941 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2942 |
|
|
2943 |
|
checkCompletedNormally(f, v1); |
2944 |
|
checkCompletedNormally(g, v2); |
2999 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3000 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
3001 |
|
|
3002 |
< |
checkCompletedWithWrappedCFException(g); |
3002 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3003 |
|
checkCompletedNormally(f, v1); |
3004 |
|
}} |
3005 |
|
|
3108 |
|
} |
3109 |
|
} |
3110 |
|
|
3111 |
< |
public void testAllOf_backwards() throws Exception { |
3111 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3112 |
|
for (int k = 1; k < 10; k++) { |
3113 |
|
CompletableFuture<Integer>[] fs |
3114 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3341 |
|
() -> f.obtrudeException(null), |
3342 |
|
|
3343 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3344 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3344 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3345 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3346 |
|
|
3347 |
|
() -> f.orTimeout(1L, null), |
3356 |
|
} |
3357 |
|
|
3358 |
|
/** |
3359 |
+ |
* Test submissions to an executor that rejects all tasks. |
3360 |
+ |
*/ |
3361 |
+ |
public void testRejectingExecutor() { |
3362 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3363 |
+ |
{ |
3364 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3365 |
+ |
|
3366 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3367 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3368 |
+ |
|
3369 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3370 |
+ |
|
3371 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3372 |
+ |
srcs.add(complete); |
3373 |
+ |
srcs.add(incomplete); |
3374 |
+ |
|
3375 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3376 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3377 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3378 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3379 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3380 |
+ |
|
3381 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3382 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3383 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3384 |
+ |
|
3385 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3386 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3387 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3388 |
+ |
|
3389 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3390 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3391 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3392 |
+ |
|
3393 |
+ |
for (CompletableFuture<?> future : fs) { |
3394 |
+ |
if (src.isDone()) |
3395 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3396 |
+ |
else |
3397 |
+ |
checkIncomplete(future); |
3398 |
+ |
} |
3399 |
+ |
futures.addAll(fs); |
3400 |
+ |
} |
3401 |
+ |
|
3402 |
+ |
{ |
3403 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3404 |
+ |
|
3405 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3406 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3407 |
+ |
|
3408 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3409 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3410 |
+ |
|
3411 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3412 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3413 |
+ |
|
3414 |
+ |
for (CompletableFuture<?> future : fs) |
3415 |
+ |
checkIncomplete(future); |
3416 |
+ |
futures.addAll(fs); |
3417 |
+ |
} |
3418 |
+ |
|
3419 |
+ |
{ |
3420 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3421 |
+ |
|
3422 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3423 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3424 |
+ |
|
3425 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3426 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3427 |
+ |
|
3428 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3429 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3430 |
+ |
|
3431 |
+ |
for (CompletableFuture<?> future : fs) |
3432 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3433 |
+ |
futures.addAll(fs); |
3434 |
+ |
} |
3435 |
+ |
|
3436 |
+ |
incomplete.complete(v); |
3437 |
+ |
|
3438 |
+ |
for (CompletableFuture<?> future : futures) |
3439 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3440 |
+ |
|
3441 |
+ |
assertEquals(futures.size(), e.count.get()); |
3442 |
+ |
}} |
3443 |
+ |
|
3444 |
+ |
/** |
3445 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3446 |
+ |
* should never be invoked because the dependent future is |
3447 |
+ |
* explicitly completed. |
3448 |
+ |
*/ |
3449 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3450 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3451 |
+ |
{ |
3452 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3453 |
+ |
|
3454 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3455 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3456 |
+ |
|
3457 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3458 |
+ |
|
3459 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3460 |
+ |
srcs.add(complete); |
3461 |
+ |
srcs.add(incomplete); |
3462 |
+ |
|
3463 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3464 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3465 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3466 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3467 |
+ |
|
3468 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3469 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3470 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3471 |
+ |
|
3472 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3473 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3474 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3475 |
+ |
|
3476 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3477 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3478 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3479 |
+ |
|
3480 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3481 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3482 |
+ |
|
3483 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3484 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3485 |
+ |
|
3486 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3487 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3488 |
+ |
|
3489 |
+ |
for (CompletableFuture<?> future : fs) |
3490 |
+ |
checkIncomplete(future); |
3491 |
+ |
|
3492 |
+ |
for (CompletableFuture<?> future : fs) |
3493 |
+ |
future.complete(null); |
3494 |
+ |
|
3495 |
+ |
incomplete.complete(v); |
3496 |
+ |
|
3497 |
+ |
for (CompletableFuture<?> future : fs) |
3498 |
+ |
checkCompletedNormally(future, null); |
3499 |
+ |
|
3500 |
+ |
assertEquals(0, e.count.get()); |
3501 |
+ |
}} |
3502 |
+ |
|
3503 |
+ |
/** |
3504 |
|
* toCompletableFuture returns this CompletableFuture. |
3505 |
|
*/ |
3506 |
|
public void testToCompletableFuture() { |
3716 |
|
long timeoutMillis = timeoutMillis(); |
3717 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3718 |
|
long startTime = System.nanoTime(); |
3719 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3719 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3720 |
|
checkCompletedWithTimeoutException(f); |
3721 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3722 |
|
} |
3731 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3732 |
|
long startTime = System.nanoTime(); |
3733 |
|
f.complete(v1); |
3734 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3735 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3734 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3735 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3736 |
|
g.complete(v1); |
3737 |
|
checkCompletedNormally(f, v1); |
3738 |
|
checkCompletedNormally(g, v1); |
3747 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3748 |
|
} |
3749 |
|
|
3750 |
+ |
/** |
3751 |
+ |
* completeOnTimeout completes with given value if not complete |
3752 |
+ |
*/ |
3753 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3754 |
|
long timeoutMillis = timeoutMillis(); |
3755 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3756 |
|
long startTime = System.nanoTime(); |
3757 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3757 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3758 |
|
assertSame(v, f.join()); |
3759 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3760 |
|
f.complete(99); // should have no effect |
3771 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3772 |
|
long startTime = System.nanoTime(); |
3773 |
|
f.complete(v1); |
3774 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3775 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3774 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3775 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3776 |
|
g.complete(v1); |
3777 |
|
checkCompletedNormally(f, v1); |
3778 |
|
checkCompletedNormally(g, v1); |
3823 |
|
//--- tests of implementation details; not part of official tck --- |
3824 |
|
|
3825 |
|
Object resultOf(CompletableFuture<?> f) { |
3826 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3827 |
+ |
if (sm != null) { |
3828 |
+ |
try { |
3829 |
+ |
System.setSecurityManager(null); |
3830 |
+ |
} catch (SecurityException giveUp) { |
3831 |
+ |
return "Reflection not available"; |
3832 |
+ |
} |
3833 |
+ |
} |
3834 |
+ |
|
3835 |
|
try { |
3836 |
|
java.lang.reflect.Field resultField |
3837 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3838 |
|
resultField.setAccessible(true); |
3839 |
|
return resultField.get(f); |
3840 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3840 |
> |
} catch (Throwable t) { |
3841 |
> |
throw new AssertionError(t); |
3842 |
> |
} finally { |
3843 |
> |
if (sm != null) System.setSecurityManager(sm); |
3844 |
> |
} |
3845 |
|
} |
3846 |
|
|
3847 |
|
public void testExceptionPropagationReusesResultObject() { |
3852 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3853 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3854 |
|
|
3855 |
+ |
final Runnable noopRunnable = new Noop(m); |
3856 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3857 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3858 |
+ |
|
3859 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3860 |
|
= new ArrayList<>(); |
3861 |
|
|
3862 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3863 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3864 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3865 |
< |
|
3866 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3867 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3868 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3862 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3863 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3864 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3865 |
> |
|
3866 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3867 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3868 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3869 |
|
|
3870 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3870 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3871 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3872 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3873 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3874 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3875 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3876 |
|
|
3877 |
|
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3878 |
|
|
3879 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3880 |
|
|
3881 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3882 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3881 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3882 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3883 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3884 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3885 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3886 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3887 |
|
|
3888 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3889 |
|
fun : funs) { |
3890 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3891 |
|
f.completeExceptionally(ex); |
3892 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3892 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3893 |
|
checkCompletedWithWrappedException(src, ex); |
3894 |
|
CompletableFuture<?> dep = fun.apply(src); |
3895 |
|
checkCompletedWithWrappedException(dep, ex); |
3899 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3900 |
|
fun : funs) { |
3901 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3902 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3902 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3903 |
|
CompletableFuture<?> dep = fun.apply(src); |
3904 |
|
f.completeExceptionally(ex); |
3905 |
|
checkCompletedWithWrappedException(src, ex); |
3913 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3914 |
|
f.cancel(mayInterruptIfRunning); |
3915 |
|
checkCancelled(f); |
3916 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3916 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3917 |
|
checkCompletedWithWrappedCancellationException(src); |
3918 |
|
CompletableFuture<?> dep = fun.apply(src); |
3919 |
|
checkCompletedWithWrappedCancellationException(dep); |
3924 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3925 |
|
fun : funs) { |
3926 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3927 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3927 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3928 |
|
CompletableFuture<?> dep = fun.apply(src); |
3929 |
|
f.cancel(mayInterruptIfRunning); |
3930 |
|
checkCancelled(f); |
3935 |
|
}} |
3936 |
|
|
3937 |
|
/** |
3938 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3938 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3939 |
|
*/ |
3940 |
|
public void testMinimalCompletionStage_minimality() { |
3941 |
|
if (!testImplementationDetails) return; |
3964 |
|
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3965 |
|
.collect(Collectors.toList()); |
3966 |
|
|
3967 |
< |
CompletionStage<Integer> minimalStage = |
3968 |
< |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3967 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3968 |
> |
stages.add(new CompletableFuture<Integer>().minimalCompletionStage()); |
3969 |
> |
stages.add(CompletableFuture.completedStage(1)); |
3970 |
> |
stages.add(CompletableFuture.failedStage(new CFException())); |
3971 |
|
|
3972 |
|
List<Method> bugs = new ArrayList<>(); |
3973 |
|
for (Method method : allMethods) { |
3983 |
|
else if (parameterTypes[i] == long.class) |
3984 |
|
args[i] = 0L; |
3985 |
|
} |
3986 |
< |
try { |
3987 |
< |
method.invoke(minimalStage, args); |
3988 |
< |
bugs.add(method); |
3769 |
< |
} |
3770 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3771 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3986 |
> |
for (CompletionStage<Integer> stage : stages) { |
3987 |
> |
try { |
3988 |
> |
method.invoke(stage, args); |
3989 |
|
bugs.add(method); |
3773 |
– |
// expected.getCause().printStackTrace(); |
3990 |
|
} |
3991 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3992 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3993 |
+ |
bugs.add(method); |
3994 |
+ |
// expected.getCause().printStackTrace(); |
3995 |
+ |
} |
3996 |
+ |
} |
3997 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3998 |
|
} |
3776 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3999 |
|
} |
4000 |
|
if (!bugs.isEmpty()) |
4001 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
4001 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4002 |
|
} |
4003 |
|
|
4004 |
|
static class Monad { |
4147 |
|
Monad.plus(godot, Monad.unit(5L))); |
4148 |
|
} |
4149 |
|
|
4150 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4151 |
+ |
public void testRecursiveChains() throws Throwable { |
4152 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4153 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4154 |
+ |
{ |
4155 |
+ |
final int val = 42; |
4156 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4157 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4158 |
+ |
CompletableFuture<Integer> tail = head; |
4159 |
+ |
for (int i = 0; i < n; i++) { |
4160 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4161 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4162 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4163 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4164 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4165 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4166 |
+ |
} |
4167 |
+ |
head.complete(val); |
4168 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4169 |
+ |
}} |
4170 |
+ |
|
4171 |
|
/** |
4172 |
|
* A single CompletableFuture with many dependents. |
4173 |
+ |
* A demo of scalability - runtime is O(n). |
4174 |
|
*/ |
4175 |
|
public void testManyDependents() throws Throwable { |
4176 |
< |
final int n = 10_000; |
4176 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4177 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3934 |
– |
final CompletableFuture<Void> incomplete = new CompletableFuture<>(); |
4178 |
|
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4179 |
|
final AtomicInteger count = new AtomicInteger(0); |
4180 |
|
for (int i = 0; i < n; i++) { |
4181 |
|
head.thenRun(() -> count.getAndIncrement()); |
4182 |
|
head.thenAccept((x) -> count.getAndIncrement()); |
4183 |
|
head.thenApply((x) -> count.getAndIncrement()); |
4184 |
+ |
|
4185 |
|
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4186 |
|
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4187 |
|
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4188 |
< |
head.runAfterEither(incomplete, () -> count.getAndIncrement()); |
4189 |
< |
head.acceptEither(incomplete, (x) -> count.getAndIncrement()); |
4190 |
< |
head.applyToEither(incomplete, (x) -> count.getAndIncrement()); |
4188 |
> |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4189 |
> |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4190 |
> |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4191 |
> |
|
4192 |
> |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4193 |
> |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4194 |
> |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4195 |
> |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4196 |
> |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4197 |
> |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4198 |
|
} |
4199 |
|
head.complete(null); |
4200 |
< |
assertEquals(9 * n, count.get()); |
4200 |
> |
assertEquals(5 * 3 * n, count.get()); |
4201 |
> |
} |
4202 |
> |
|
4203 |
> |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4204 |
> |
public void testCoCompletionGarbageRetention() throws Throwable { |
4205 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4206 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4207 |
> |
CompletableFuture<Integer> f; |
4208 |
> |
for (int i = 0; i < n; i++) { |
4209 |
> |
f = new CompletableFuture<>(); |
4210 |
> |
f.runAfterEither(incomplete, () -> {}); |
4211 |
> |
f.complete(null); |
4212 |
> |
|
4213 |
> |
f = new CompletableFuture<>(); |
4214 |
> |
f.acceptEither(incomplete, (x) -> {}); |
4215 |
> |
f.complete(null); |
4216 |
> |
|
4217 |
> |
f = new CompletableFuture<>(); |
4218 |
> |
f.applyToEither(incomplete, (x) -> x); |
4219 |
> |
f.complete(null); |
4220 |
> |
|
4221 |
> |
f = new CompletableFuture<>(); |
4222 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4223 |
> |
f.complete(null); |
4224 |
> |
} |
4225 |
> |
|
4226 |
> |
for (int i = 0; i < n; i++) { |
4227 |
> |
f = new CompletableFuture<>(); |
4228 |
> |
incomplete.runAfterEither(f, () -> {}); |
4229 |
> |
f.complete(null); |
4230 |
> |
|
4231 |
> |
f = new CompletableFuture<>(); |
4232 |
> |
incomplete.acceptEither(f, (x) -> {}); |
4233 |
> |
f.complete(null); |
4234 |
> |
|
4235 |
> |
f = new CompletableFuture<>(); |
4236 |
> |
incomplete.applyToEither(f, (x) -> x); |
4237 |
> |
f.complete(null); |
4238 |
> |
|
4239 |
> |
f = new CompletableFuture<>(); |
4240 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4241 |
> |
f.complete(null); |
4242 |
> |
} |
4243 |
> |
} |
4244 |
> |
|
4245 |
> |
/** |
4246 |
> |
* Reproduction recipe for: |
4247 |
> |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4248 |
> |
* 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 |
4249 |
> |
*/ |
4250 |
> |
public void testAnyOfGarbageRetention() throws Throwable { |
4251 |
> |
for (Integer v : new Integer[] { 1, null }) |
4252 |
> |
{ |
4253 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4254 |
> |
CompletableFuture<Integer>[] fs |
4255 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4256 |
> |
for (int i = 0; i < fs.length; i++) |
4257 |
> |
fs[i] = new CompletableFuture<>(); |
4258 |
> |
fs[fs.length - 1].complete(v); |
4259 |
> |
for (int i = 0; i < n; i++) |
4260 |
> |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4261 |
> |
}} |
4262 |
> |
|
4263 |
> |
/** |
4264 |
> |
* Checks for garbage retention with allOf. |
4265 |
> |
* |
4266 |
> |
* As of 2016-07, fails with OOME: |
4267 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4268 |
> |
*/ |
4269 |
> |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4270 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4271 |
> |
CompletableFuture<Integer>[] fs |
4272 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4273 |
> |
for (int i = 0; i < fs.length; i++) |
4274 |
> |
fs[i] = new CompletableFuture<>(); |
4275 |
> |
for (int i = 0; i < n; i++) |
4276 |
> |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4277 |
> |
} |
4278 |
> |
|
4279 |
> |
/** |
4280 |
> |
* Checks for garbage retention when a dependent future is |
4281 |
> |
* cancelled and garbage-collected. |
4282 |
> |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4283 |
> |
* |
4284 |
> |
* As of 2016-07, fails with OOME: |
4285 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4286 |
> |
*/ |
4287 |
> |
public void testCancelledGarbageRetention() throws Throwable { |
4288 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4289 |
> |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4290 |
> |
for (int i = 0; i < n; i++) |
4291 |
> |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4292 |
|
} |
4293 |
|
|
4294 |
|
// static <U> U join(CompletionStage<U> stage) { |