947 |
|
* of sources |
948 |
|
*/ |
949 |
|
public void testThenCombine_normalCompletion1() { |
950 |
+ |
for (boolean createdIncomplete : new boolean[] { true, false }) |
951 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
952 |
|
for (ExecutionMode m : ExecutionMode.values()) |
953 |
|
for (Integer v1 : new Integer[] { 1, null }) |
954 |
|
for (Integer v2 : new Integer[] { 2, null }) { |
956 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
957 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
958 |
|
final SubtractFunction r = new SubtractFunction(); |
959 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
960 |
< |
|
959 |
< |
f.complete(v1); |
960 |
< |
checkIncomplete(h); |
961 |
< |
assertEquals(r.invocationCount, 0); |
962 |
< |
g.complete(v2); |
963 |
< |
|
964 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
965 |
< |
checkCompletedNormally(f, v1); |
966 |
< |
checkCompletedNormally(g, v2); |
967 |
< |
assertEquals(r.invocationCount, 1); |
968 |
< |
} |
969 |
< |
} |
970 |
< |
|
971 |
< |
public void testThenCombine_normalCompletion2() { |
972 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
973 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
974 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
975 |
< |
|
976 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
977 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
978 |
< |
final SubtractFunction r = new SubtractFunction(); |
979 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
959 |
> |
CompletableFuture<Integer> h = null; |
960 |
> |
if (createdIncomplete) h = m.thenCombine(f, g, r); |
961 |
|
|
962 |
< |
g.complete(v2); |
963 |
< |
checkIncomplete(h); |
964 |
< |
assertEquals(r.invocationCount, 0); |
965 |
< |
f.complete(v1); |
966 |
< |
|
967 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
968 |
< |
checkCompletedNormally(f, v1); |
969 |
< |
checkCompletedNormally(g, v2); |
970 |
< |
assertEquals(r.invocationCount, 1); |
971 |
< |
} |
972 |
< |
} |
992 |
< |
|
993 |
< |
public void testThenCombine_normalCompletion3() { |
994 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
995 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
996 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
997 |
< |
|
998 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
999 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1000 |
< |
final SubtractFunction r = new SubtractFunction(); |
1001 |
< |
|
1002 |
< |
g.complete(v2); |
1003 |
< |
f.complete(v1); |
1004 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1005 |
< |
|
1006 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1007 |
< |
checkCompletedNormally(f, v1); |
1008 |
< |
checkCompletedNormally(g, v2); |
1009 |
< |
assertEquals(r.invocationCount, 1); |
1010 |
< |
} |
1011 |
< |
} |
1012 |
< |
|
1013 |
< |
public void testThenCombine_normalCompletion4() { |
1014 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1015 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1016 |
< |
for (Integer v2 : new Integer[] { 2, null }) { |
1017 |
< |
|
1018 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1019 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1020 |
< |
final SubtractFunction r = new SubtractFunction(); |
1021 |
< |
|
1022 |
< |
f.complete(v1); |
1023 |
< |
g.complete(v2); |
1024 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
962 |
> |
if (fFirst) |
963 |
> |
f.complete(v1); |
964 |
> |
else |
965 |
> |
g.complete(v2); |
966 |
> |
if (createdIncomplete) checkIncomplete(h); |
967 |
> |
assertEquals(r.invocationCount, 0); |
968 |
> |
if (!fFirst) |
969 |
> |
f.complete(v1); |
970 |
> |
else |
971 |
> |
g.complete(v2); |
972 |
> |
if (!createdIncomplete) h = m.thenCombine(f, g, r); |
973 |
|
|
974 |
|
checkCompletedNormally(h, subtract(v1, v2)); |
975 |
|
checkCompletedNormally(f, v1); |
3416 |
|
* whenComplete action executes on normal completion, propagating |
3417 |
|
* source result. |
3418 |
|
*/ |
3419 |
< |
public void testWhenComplete1() { |
3419 |
> |
public void testWhenComplete_normalCompletion1() { |
3420 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3421 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
3422 |
> |
|
3423 |
|
final AtomicInteger a = new AtomicInteger(); |
3424 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3425 |
< |
CompletableFuture<Integer> g = |
3426 |
< |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3427 |
< |
f.complete(three); |
3428 |
< |
checkCompletedNormally(f, three); |
3429 |
< |
checkCompletedNormally(g, three); |
3424 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3425 |
> |
final CompletableFuture<Integer> g = |
3426 |
> |
m.whenComplete(f, |
3427 |
> |
(Integer x, Throwable t) -> { |
3428 |
> |
threadAssertSame(x, v1); |
3429 |
> |
threadAssertNull(t); |
3430 |
> |
a.getAndIncrement(); |
3431 |
> |
}); |
3432 |
> |
f.complete(v1); |
3433 |
> |
checkCompletedNormally(f, v1); |
3434 |
> |
checkCompletedNormally(g, v1); |
3435 |
|
assertEquals(a.get(), 1); |
3436 |
+ |
} |
3437 |
|
} |
3438 |
|
|
3439 |
|
/** |
3440 |
|
* whenComplete action executes on exceptional completion, propagating |
3441 |
|
* source result. |
3442 |
|
*/ |
3443 |
< |
public void testWhenComplete2() { |
3443 |
> |
public void testWhenComplete_exceptionalCompletion() { |
3444 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3445 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
3446 |
> |
|
3447 |
|
final AtomicInteger a = new AtomicInteger(); |
3448 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3449 |
< |
CompletableFuture<Integer> g = |
3450 |
< |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3451 |
< |
f.completeExceptionally(new CFException()); |
3452 |
< |
assertTrue(f.isCompletedExceptionally()); |
3453 |
< |
assertTrue(g.isCompletedExceptionally()); |
3448 |
> |
final CFException ex = new CFException(); |
3449 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3450 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
3451 |
> |
(f, |
3452 |
> |
(Integer x, Throwable t) -> { |
3453 |
> |
threadAssertNull(x); |
3454 |
> |
threadAssertSame(t, ex); |
3455 |
> |
a.getAndIncrement(); |
3456 |
> |
}); |
3457 |
> |
f.completeExceptionally(ex); |
3458 |
> |
checkCompletedWithWrappedCFException(f, ex); |
3459 |
> |
checkCompletedWithWrappedCFException(g, ex); |
3460 |
|
assertEquals(a.get(), 1); |
3461 |
+ |
} |
3462 |
|
} |
3463 |
|
|
3464 |
|
/** |
3465 |
|
* If a whenComplete action throws an exception when triggered by |
3466 |
|
* a normal completion, it completes exceptionally |
3467 |
|
*/ |
3468 |
< |
public void testWhenComplete3() { |
3469 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3470 |
< |
CompletableFuture<Integer> g = |
3504 |
< |
f.whenComplete((Integer x, Throwable t) -> |
3505 |
< |
{ throw new CFException(); } ); |
3506 |
< |
f.complete(three); |
3507 |
< |
checkCompletedNormally(f, three); |
3508 |
< |
assertTrue(g.isCompletedExceptionally()); |
3509 |
< |
checkCompletedWithWrappedCFException(g); |
3510 |
< |
} |
3511 |
< |
|
3512 |
< |
/** |
3513 |
< |
* whenCompleteAsync action executes on normal completion, propagating |
3514 |
< |
* source result. |
3515 |
< |
*/ |
3516 |
< |
public void testWhenCompleteAsync1() { |
3517 |
< |
final AtomicInteger a = new AtomicInteger(); |
3518 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3519 |
< |
CompletableFuture<Integer> g = |
3520 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3521 |
< |
f.complete(three); |
3522 |
< |
checkCompletedNormally(f, three); |
3523 |
< |
checkCompletedNormally(g, three); |
3524 |
< |
assertEquals(a.get(), 1); |
3525 |
< |
} |
3526 |
< |
|
3527 |
< |
/** |
3528 |
< |
* whenCompleteAsync action executes on exceptional completion, propagating |
3529 |
< |
* source result. |
3530 |
< |
*/ |
3531 |
< |
public void testWhenCompleteAsync2() { |
3532 |
< |
final AtomicInteger a = new AtomicInteger(); |
3533 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3534 |
< |
CompletableFuture<Integer> g = |
3535 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3536 |
< |
f.completeExceptionally(new CFException()); |
3537 |
< |
checkCompletedWithWrappedCFException(f); |
3538 |
< |
checkCompletedWithWrappedCFException(g); |
3539 |
< |
} |
3540 |
< |
|
3541 |
< |
/** |
3542 |
< |
* If a whenCompleteAsync action throws an exception when |
3543 |
< |
* triggered by a normal completion, it completes exceptionally |
3544 |
< |
*/ |
3545 |
< |
public void testWhenCompleteAsync3() { |
3546 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3547 |
< |
CompletableFuture<Integer> g = |
3548 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3549 |
< |
{ throw new CFException(); } ); |
3550 |
< |
f.complete(three); |
3551 |
< |
checkCompletedNormally(f, three); |
3552 |
< |
checkCompletedWithWrappedCFException(g); |
3553 |
< |
} |
3468 |
> |
public void testWhenComplete_actionFailed() { |
3469 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3470 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
3471 |
|
|
3472 |
< |
/** |
3473 |
< |
* whenCompleteAsync action executes on normal completion, propagating |
3474 |
< |
* source result. |
3475 |
< |
*/ |
3476 |
< |
public void testWhenCompleteAsync1e() { |
3477 |
< |
final AtomicInteger a = new AtomicInteger(); |
3478 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3479 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3480 |
< |
CompletableFuture<Integer> g = |
3481 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3482 |
< |
exec); |
3483 |
< |
f.complete(three); |
3484 |
< |
checkCompletedNormally(f, three); |
3568 |
< |
checkCompletedNormally(g, three); |
3569 |
< |
assertEquals(a.get(), 1); |
3472 |
> |
final CFException ex = new CFException(); |
3473 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3474 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
3475 |
> |
(f, |
3476 |
> |
(Integer x, Throwable t) -> { |
3477 |
> |
threadAssertSame(x, v1); |
3478 |
> |
threadAssertNull(t); |
3479 |
> |
throw ex; |
3480 |
> |
}); |
3481 |
> |
f.complete(v1); |
3482 |
> |
checkCompletedNormally(f, v1); |
3483 |
> |
checkCompletedWithWrappedCFException(g, ex); |
3484 |
> |
} |
3485 |
|
} |
3486 |
|
|
3487 |
|
/** |
3488 |
< |
* whenCompleteAsync action executes on exceptional completion, propagating |
3489 |
< |
* source result. |
3488 |
> |
* If a whenComplete action throws an exception when triggered by |
3489 |
> |
* a source completion that also throws an exception, the source |
3490 |
> |
* exception takes precedence. |
3491 |
|
*/ |
3492 |
< |
public void testWhenCompleteAsync2e() { |
3493 |
< |
final AtomicInteger a = new AtomicInteger(); |
3494 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3579 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3580 |
< |
CompletableFuture<Integer> g = |
3581 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3582 |
< |
exec); |
3583 |
< |
f.completeExceptionally(new CFException()); |
3584 |
< |
checkCompletedWithWrappedCFException(f); |
3585 |
< |
checkCompletedWithWrappedCFException(g); |
3586 |
< |
} |
3492 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
3493 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3494 |
> |
for (Integer v1 : new Integer[] { 1, null }) { |
3495 |
|
|
3496 |
< |
/** |
3497 |
< |
* If a whenCompleteAsync action throws an exception when triggered |
3498 |
< |
* by a normal completion, it completes exceptionally |
3499 |
< |
*/ |
3500 |
< |
public void testWhenCompleteAsync3e() { |
3501 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
3502 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3503 |
< |
CompletableFuture<Integer> g = |
3504 |
< |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3505 |
< |
{ throw new CFException(); }, |
3506 |
< |
exec); |
3507 |
< |
f.complete(three); |
3508 |
< |
checkCompletedNormally(f, three); |
3509 |
< |
checkCompletedWithWrappedCFException(g); |
3496 |
> |
final CFException ex1 = new CFException(); |
3497 |
> |
final CFException ex2 = new CFException(); |
3498 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3499 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
3500 |
> |
(f, |
3501 |
> |
(Integer x, Throwable t) -> { |
3502 |
> |
threadAssertSame(t, ex1); |
3503 |
> |
threadAssertNull(x); |
3504 |
> |
throw ex2; |
3505 |
> |
}); |
3506 |
> |
f.completeExceptionally(ex1); |
3507 |
> |
checkCompletedWithWrappedCFException(f, ex1); |
3508 |
> |
checkCompletedWithWrappedCFException(g, ex1); |
3509 |
> |
} |
3510 |
|
} |
3511 |
|
|
3512 |
|
/** |