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 |
|
|
2924 |
|
assertTrue(f.complete(v1)); |
2925 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2926 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2927 |
< |
checkCompletedWithWrappedCFException(h0); |
2928 |
< |
checkCompletedWithWrappedCFException(h1); |
2929 |
< |
checkCompletedWithWrappedCFException(h2); |
2930 |
< |
checkCompletedWithWrappedCFException(h3); |
2927 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2928 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2929 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2930 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2931 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2932 |
|
assertTrue(g.complete(v2)); |
2933 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2934 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2935 |
< |
checkCompletedWithWrappedCFException(h4); |
2936 |
< |
checkCompletedWithWrappedCFException(h5); |
2935 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2936 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2937 |
|
|
2938 |
|
checkCompletedNormally(f, v1); |
2939 |
|
checkCompletedNormally(g, v2); |
2994 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2995 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
2996 |
|
|
2997 |
< |
checkCompletedWithWrappedCFException(g); |
2997 |
> |
checkCompletedWithWrappedException(g, r.ex); |
2998 |
|
checkCompletedNormally(f, v1); |
2999 |
|
}} |
3000 |
|
|
3103 |
|
} |
3104 |
|
} |
3105 |
|
|
3106 |
< |
public void testAllOf_backwards() throws Exception { |
3106 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3107 |
|
for (int k = 1; k < 10; k++) { |
3108 |
|
CompletableFuture<Integer>[] fs |
3109 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3336 |
|
() -> f.obtrudeException(null), |
3337 |
|
|
3338 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3339 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3339 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3340 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3341 |
|
|
3342 |
|
() -> f.orTimeout(1L, null), |
3351 |
|
} |
3352 |
|
|
3353 |
|
/** |
3354 |
+ |
* Test submissions to an executor that rejects all tasks. |
3355 |
+ |
*/ |
3356 |
+ |
public void testRejectingExecutor() { |
3357 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3358 |
+ |
|
3359 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3360 |
+ |
|
3361 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3362 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3363 |
+ |
|
3364 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3365 |
+ |
|
3366 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3367 |
+ |
srcs.add(complete); |
3368 |
+ |
srcs.add(incomplete); |
3369 |
+ |
|
3370 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3371 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3372 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3373 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3374 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3375 |
+ |
|
3376 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3377 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3378 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3379 |
+ |
|
3380 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3381 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3382 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3383 |
+ |
|
3384 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3385 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3386 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3387 |
+ |
|
3388 |
+ |
for (CompletableFuture<?> future : fs) { |
3389 |
+ |
if (src.isDone()) |
3390 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3391 |
+ |
else |
3392 |
+ |
checkIncomplete(future); |
3393 |
+ |
} |
3394 |
+ |
futures.addAll(fs); |
3395 |
+ |
} |
3396 |
+ |
|
3397 |
+ |
{ |
3398 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3399 |
+ |
|
3400 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3401 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3402 |
+ |
|
3403 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3404 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3405 |
+ |
|
3406 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3407 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3408 |
+ |
|
3409 |
+ |
for (CompletableFuture<?> future : fs) |
3410 |
+ |
checkIncomplete(future); |
3411 |
+ |
futures.addAll(fs); |
3412 |
+ |
} |
3413 |
+ |
|
3414 |
+ |
{ |
3415 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3416 |
+ |
|
3417 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3418 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3419 |
+ |
|
3420 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3421 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3422 |
+ |
|
3423 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3424 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3425 |
+ |
|
3426 |
+ |
for (CompletableFuture<?> future : fs) |
3427 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3428 |
+ |
futures.addAll(fs); |
3429 |
+ |
} |
3430 |
+ |
|
3431 |
+ |
incomplete.complete(v); |
3432 |
+ |
|
3433 |
+ |
for (CompletableFuture<?> future : futures) |
3434 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3435 |
+ |
|
3436 |
+ |
assertEquals(futures.size(), e.count.get()); |
3437 |
+ |
|
3438 |
+ |
} |
3439 |
+ |
} |
3440 |
+ |
|
3441 |
+ |
/** |
3442 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3443 |
+ |
* should never be invoked because the dependent future is |
3444 |
+ |
* explicitly completed. |
3445 |
+ |
*/ |
3446 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3447 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3448 |
+ |
|
3449 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3450 |
+ |
|
3451 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3452 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3453 |
+ |
|
3454 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3455 |
+ |
|
3456 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3457 |
+ |
srcs.add(complete); |
3458 |
+ |
srcs.add(incomplete); |
3459 |
+ |
|
3460 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3461 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3462 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3463 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3464 |
+ |
|
3465 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3466 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3467 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3468 |
+ |
|
3469 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3470 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3471 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3472 |
+ |
|
3473 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3474 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3475 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3476 |
+ |
|
3477 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3478 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3479 |
+ |
|
3480 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3481 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3482 |
+ |
|
3483 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3484 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3485 |
+ |
|
3486 |
+ |
for (CompletableFuture<?> future : fs) |
3487 |
+ |
checkIncomplete(future); |
3488 |
+ |
|
3489 |
+ |
for (CompletableFuture<?> future : fs) |
3490 |
+ |
future.complete(null); |
3491 |
+ |
|
3492 |
+ |
incomplete.complete(v); |
3493 |
+ |
|
3494 |
+ |
for (CompletableFuture<?> future : fs) |
3495 |
+ |
checkCompletedNormally(future, null); |
3496 |
+ |
|
3497 |
+ |
assertEquals(0, e.count.get()); |
3498 |
+ |
|
3499 |
+ |
} |
3500 |
+ |
} |
3501 |
+ |
|
3502 |
+ |
/** |
3503 |
|
* toCompletableFuture returns this CompletableFuture. |
3504 |
|
*/ |
3505 |
|
public void testToCompletableFuture() { |
3715 |
|
long timeoutMillis = timeoutMillis(); |
3716 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3717 |
|
long startTime = System.nanoTime(); |
3718 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3718 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3719 |
|
checkCompletedWithTimeoutException(f); |
3720 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3721 |
|
} |
3730 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3731 |
|
long startTime = System.nanoTime(); |
3732 |
|
f.complete(v1); |
3733 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3734 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3733 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3734 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3735 |
|
g.complete(v1); |
3736 |
|
checkCompletedNormally(f, v1); |
3737 |
|
checkCompletedNormally(g, v1); |
3746 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3747 |
|
} |
3748 |
|
|
3749 |
+ |
/** |
3750 |
+ |
* completeOnTimeout completes with given value if not complete |
3751 |
+ |
*/ |
3752 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3753 |
|
long timeoutMillis = timeoutMillis(); |
3754 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3755 |
|
long startTime = System.nanoTime(); |
3756 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3756 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3757 |
|
assertSame(v, f.join()); |
3758 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3759 |
|
f.complete(99); // should have no effect |
3770 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3771 |
|
long startTime = System.nanoTime(); |
3772 |
|
f.complete(v1); |
3773 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3774 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3773 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3774 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3775 |
|
g.complete(v1); |
3776 |
|
checkCompletedNormally(f, v1); |
3777 |
|
checkCompletedNormally(g, v1); |
3822 |
|
//--- tests of implementation details; not part of official tck --- |
3823 |
|
|
3824 |
|
Object resultOf(CompletableFuture<?> f) { |
3825 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3826 |
+ |
if (sm != null) { |
3827 |
+ |
try { |
3828 |
+ |
System.setSecurityManager(null); |
3829 |
+ |
} catch (SecurityException giveUp) { |
3830 |
+ |
return "Reflection not available"; |
3831 |
+ |
} |
3832 |
+ |
} |
3833 |
+ |
|
3834 |
|
try { |
3835 |
|
java.lang.reflect.Field resultField |
3836 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3837 |
|
resultField.setAccessible(true); |
3838 |
|
return resultField.get(f); |
3839 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3839 |
> |
} catch (Throwable t) { |
3840 |
> |
throw new AssertionError(t); |
3841 |
> |
} finally { |
3842 |
> |
if (sm != null) System.setSecurityManager(sm); |
3843 |
> |
} |
3844 |
|
} |
3845 |
|
|
3846 |
|
public void testExceptionPropagationReusesResultObject() { |
3863 |
|
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3864 |
|
|
3865 |
|
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3866 |
+ |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3867 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3868 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3869 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3870 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3871 |
|
|
3872 |
|
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3873 |
|
|
3874 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3875 |
|
|
3876 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3877 |
|
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3878 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3879 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3880 |
|
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3881 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3882 |
|
|
3883 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3884 |
|
fun : funs) { |
4138 |
|
Monad.plus(godot, Monad.unit(5L))); |
4139 |
|
} |
4140 |
|
|
4141 |
+ |
/** |
4142 |
+ |
* A single CompletableFuture with many dependents. |
4143 |
+ |
* A demo of scalability - runtime is O(n). |
4144 |
+ |
*/ |
4145 |
+ |
public void testManyDependents() throws Throwable { |
4146 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4147 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4148 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4149 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4150 |
+ |
for (int i = 0; i < n; i++) { |
4151 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4152 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
4153 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
4154 |
+ |
|
4155 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4156 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4157 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4158 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4159 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4160 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4161 |
+ |
|
4162 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4163 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4164 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4165 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4166 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4167 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4168 |
+ |
} |
4169 |
+ |
head.complete(null); |
4170 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4171 |
+ |
} |
4172 |
+ |
|
4173 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4174 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4175 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4176 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4177 |
+ |
CompletableFuture<Integer> f; |
4178 |
+ |
for (int i = 0; i < n; i++) { |
4179 |
+ |
f = new CompletableFuture<>(); |
4180 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4181 |
+ |
f.complete(null); |
4182 |
+ |
|
4183 |
+ |
f = new CompletableFuture<>(); |
4184 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4185 |
+ |
f.complete(null); |
4186 |
+ |
|
4187 |
+ |
f = new CompletableFuture<>(); |
4188 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4189 |
+ |
f.complete(null); |
4190 |
+ |
|
4191 |
+ |
f = new CompletableFuture<>(); |
4192 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4193 |
+ |
f.complete(null); |
4194 |
+ |
} |
4195 |
+ |
|
4196 |
+ |
for (int i = 0; i < n; i++) { |
4197 |
+ |
f = new CompletableFuture<>(); |
4198 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4199 |
+ |
f.complete(null); |
4200 |
+ |
|
4201 |
+ |
f = new CompletableFuture<>(); |
4202 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4203 |
+ |
f.complete(null); |
4204 |
+ |
|
4205 |
+ |
f = new CompletableFuture<>(); |
4206 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4207 |
+ |
f.complete(null); |
4208 |
+ |
|
4209 |
+ |
f = new CompletableFuture<>(); |
4210 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4211 |
+ |
f.complete(null); |
4212 |
+ |
} |
4213 |
+ |
} |
4214 |
+ |
|
4215 |
+ |
/* |
4216 |
+ |
* Tests below currently fail in stress mode due to memory retention. |
4217 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck |
4218 |
+ |
*/ |
4219 |
+ |
|
4220 |
+ |
/** Checks for garbage retention with anyOf. */ |
4221 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4222 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4223 |
+ |
{ |
4224 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4225 |
+ |
CompletableFuture<Integer>[] fs |
4226 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4227 |
+ |
for (int i = 0; i < fs.length; i++) |
4228 |
+ |
fs[i] = new CompletableFuture<>(); |
4229 |
+ |
fs[fs.length - 1].complete(v); |
4230 |
+ |
for (int i = 0; i < n; i++) |
4231 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4232 |
+ |
}} |
4233 |
+ |
|
4234 |
+ |
/** Checks for garbage retention with allOf. */ |
4235 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4236 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4237 |
+ |
CompletableFuture<Integer>[] fs |
4238 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4239 |
+ |
for (int i = 0; i < fs.length; i++) |
4240 |
+ |
fs[i] = new CompletableFuture<>(); |
4241 |
+ |
for (int i = 0; i < n; i++) |
4242 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4243 |
+ |
} |
4244 |
+ |
|
4245 |
|
// static <U> U join(CompletionStage<U> stage) { |
4246 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4247 |
|
// stage.whenComplete((v, ex) -> { |