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; |
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 |
+ |
|
562 |
|
// Used for explicit executor tests |
563 |
|
static final class ThreadExecutor implements Executor { |
564 |
|
final AtomicInteger count = new AtomicInteger(0); |
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 |
|
*/ |
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 |
|
/** |
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() { |