98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
assertTrue(f.isDone()); |
100 |
|
assertFalse(f.isCancelled()); |
101 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
102 |
|
} |
103 |
|
|
104 |
|
void checkCancelled(CompletableFuture<?> f) { |
122 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
|
assertTrue(f.isDone()); |
124 |
|
assertTrue(f.isCancelled()); |
125 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
126 |
|
} |
127 |
|
|
128 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
152 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
assertTrue(f.isDone()); |
154 |
|
assertFalse(f.isCancelled()); |
155 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
345 |
|
|
346 |
|
// Used for explicit executor tests |
347 |
|
static final class ThreadExecutor implements Executor { |
348 |
+ |
AtomicInteger count = new AtomicInteger(0); |
349 |
+ |
|
350 |
|
public void execute(Runnable r) { |
351 |
+ |
count.getAndIncrement(); |
352 |
|
new Thread(r).start(); |
353 |
|
} |
354 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
361 |
+ |
boolean ran; |
362 |
|
public Integer apply(Integer x, Throwable t) { |
363 |
+ |
ran = true; |
364 |
|
return (t == null) ? two : three; |
365 |
|
} |
366 |
|
} |
389 |
|
* normal or exceptional completion of source |
390 |
|
*/ |
391 |
|
public void testHandle() { |
392 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
393 |
< |
IntegerHandler r = new IntegerHandler(); |
394 |
< |
CompletableFuture<Integer> g = f.handle(r); |
392 |
> |
CompletableFuture<Integer> f, g; |
393 |
> |
IntegerHandler r; |
394 |
> |
|
395 |
> |
f = new CompletableFuture<Integer>(); |
396 |
|
f.completeExceptionally(new CFException()); |
397 |
+ |
g = f.handle(r = new IntegerHandler()); |
398 |
+ |
assertTrue(r.ran); |
399 |
|
checkCompletedNormally(g, three); |
400 |
|
|
401 |
|
f = new CompletableFuture<Integer>(); |
402 |
< |
r = new IntegerHandler(); |
403 |
< |
g = f.handle(r); |
402 |
> |
g = f.handle(r = new IntegerHandler()); |
403 |
> |
assertFalse(r.ran); |
404 |
> |
f.completeExceptionally(new CFException()); |
405 |
> |
checkCompletedNormally(g, three); |
406 |
> |
assertTrue(r.ran); |
407 |
> |
|
408 |
> |
f = new CompletableFuture<Integer>(); |
409 |
> |
f.complete(one); |
410 |
> |
g = f.handle(r = new IntegerHandler()); |
411 |
> |
assertTrue(r.ran); |
412 |
> |
checkCompletedNormally(g, two); |
413 |
> |
|
414 |
> |
f = new CompletableFuture<Integer>(); |
415 |
> |
g = f.handle(r = new IntegerHandler()); |
416 |
> |
assertFalse(r.ran); |
417 |
|
f.complete(one); |
418 |
+ |
assertTrue(r.ran); |
419 |
|
checkCompletedNormally(g, two); |
420 |
|
} |
421 |
|
|
427 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
428 |
|
assertNull(f.join()); |
429 |
|
assertTrue(r.ran); |
430 |
+ |
checkCompletedNormally(f, null); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
435 |
|
*/ |
436 |
|
public void testRunAsync2() { |
437 |
|
Noop r = new Noop(); |
438 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
438 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
439 |
> |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
440 |
|
assertNull(f.join()); |
441 |
|
assertTrue(r.ran); |
442 |
+ |
checkCompletedNormally(f, null); |
443 |
+ |
assertEquals(1, exec.count.get()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
457 |
|
* supplyAsync completes with result of supplier |
458 |
|
*/ |
459 |
|
public void testSupplyAsync() { |
460 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
460 |
> |
CompletableFuture<Integer> f; |
461 |
> |
f = CompletableFuture.supplyAsync(supplyOne); |
462 |
|
assertEquals(f.join(), one); |
463 |
+ |
checkCompletedNormally(f, one); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* supplyAsync with executor completes with result of supplier |
468 |
|
*/ |
469 |
|
public void testSupplyAsync2() { |
470 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
470 |
> |
CompletableFuture<Integer> f; |
471 |
> |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
472 |
|
assertEquals(f.join(), one); |
473 |
+ |
checkCompletedNormally(f, one); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
2403 |
|
} |
2404 |
|
|
2405 |
|
/** |
2406 |
< |
* allOf returns a future completed when any components complete |
2406 |
> |
* anyOf returns a future completed when any components complete |
2407 |
|
*/ |
2408 |
|
public void testAnyOf() throws Exception { |
2409 |
|
for (int k = 1; k < 20; ++k) { |
2425 |
|
public void testNPE() { |
2426 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2427 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2428 |
< |
CompletableFuture h; |
2429 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2430 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2431 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2432 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2433 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2434 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2435 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2436 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2437 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2438 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2439 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2440 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2441 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2442 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2443 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2444 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2445 |
< |
|
2446 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2447 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2448 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2449 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2450 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2451 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2452 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2453 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2454 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2455 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2456 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2457 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2458 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2428 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2429 |
> |
CompletableFuture<?> h; |
2430 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
2431 |
> |
|
2432 |
> |
Runnable[] throwingActions = { |
2433 |
> |
() -> { CompletableFuture.supplyAsync(null); }, |
2434 |
> |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2435 |
> |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2436 |
> |
|
2437 |
> |
() -> { CompletableFuture.runAsync(null); }, |
2438 |
> |
() -> { CompletableFuture.runAsync(null, exec); }, |
2439 |
> |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2440 |
> |
|
2441 |
> |
() -> { f.completeExceptionally(null); }, |
2442 |
> |
|
2443 |
> |
() -> { f.thenApply(null); }, |
2444 |
> |
() -> { f.thenApplyAsync(null); }, |
2445 |
> |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2446 |
> |
() -> { f.thenApplyAsync(null, exec); }, |
2447 |
> |
|
2448 |
> |
() -> { f.thenAccept(null); }, |
2449 |
> |
() -> { f.thenAcceptAsync(null); }, |
2450 |
> |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2451 |
> |
() -> { f.thenAcceptAsync(null, exec); }, |
2452 |
> |
|
2453 |
> |
() -> { f.thenRun(null); }, |
2454 |
> |
() -> { f.thenRunAsync(null); }, |
2455 |
> |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2456 |
> |
() -> { f.thenRunAsync(null, exec); }, |
2457 |
> |
|
2458 |
> |
() -> { f.thenCombine(g, null); }, |
2459 |
> |
() -> { f.thenCombineAsync(g, null); }, |
2460 |
> |
() -> { f.thenCombineAsync(g, null, exec); }, |
2461 |
> |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2462 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2463 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2464 |
> |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2465 |
> |
|
2466 |
> |
() -> { f.thenAcceptBoth(g, null); }, |
2467 |
> |
() -> { f.thenAcceptBothAsync(g, null); }, |
2468 |
> |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2469 |
> |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2470 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2471 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2472 |
> |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2473 |
> |
|
2474 |
> |
() -> { f.runAfterBoth(g, null); }, |
2475 |
> |
() -> { f.runAfterBothAsync(g, null); }, |
2476 |
> |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2477 |
> |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2478 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2479 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2480 |
> |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2481 |
> |
|
2482 |
> |
() -> { f.applyToEither(g, null); }, |
2483 |
> |
() -> { f.applyToEitherAsync(g, null); }, |
2484 |
> |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2485 |
> |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2486 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2487 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2488 |
> |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2489 |
> |
|
2490 |
> |
() -> { f.acceptEither(g, null); }, |
2491 |
> |
() -> { f.acceptEitherAsync(g, null); }, |
2492 |
> |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2493 |
> |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2494 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2495 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2496 |
> |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2497 |
> |
|
2498 |
> |
() -> { f.runAfterEither(g, null); }, |
2499 |
> |
() -> { f.runAfterEitherAsync(g, null); }, |
2500 |
> |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
2501 |
> |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
2502 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
2503 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
2504 |
> |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
2505 |
> |
|
2506 |
> |
() -> { f.thenCompose(null); }, |
2507 |
> |
() -> { f.thenComposeAsync(null); }, |
2508 |
> |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
2509 |
> |
() -> { f.thenComposeAsync(null, exec); }, |
2510 |
> |
|
2511 |
> |
() -> { f.exceptionally(null); }, |
2512 |
> |
|
2513 |
> |
() -> { f.handle(null); }, |
2514 |
> |
|
2515 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2516 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2517 |
> |
() -> { CompletableFuture.allOf(f, null); }, |
2518 |
> |
() -> { CompletableFuture.allOf(null, f); }, |
2519 |
> |
|
2520 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2521 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2522 |
> |
() -> { CompletableFuture.anyOf(f, null); }, |
2523 |
> |
() -> { CompletableFuture.anyOf(null, f); }, |
2524 |
> |
}; |
2525 |
|
|
2526 |
+ |
assertThrows(NullPointerException.class, throwingActions); |
2527 |
+ |
assertEquals(0, exec.count.get()); |
2528 |
|
} |
2529 |
|
|
2429 |
– |
|
2530 |
|
} |