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; |
3317 |
|
assertEquals(0, exec.count.get()); |
3318 |
|
} |
3319 |
|
|
3320 |
+ |
static class CountingRejectingExecutor implements Executor { |
3321 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3322 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3323 |
+ |
public void execute(Runnable r) { |
3324 |
+ |
count.getAndIncrement(); |
3325 |
+ |
throw ex; |
3326 |
+ |
} |
3327 |
+ |
} |
3328 |
+ |
|
3329 |
+ |
/** |
3330 |
+ |
* Test submissions to an executor that rejects all tasks. |
3331 |
+ |
*/ |
3332 |
+ |
public void testRejectingExecutor() { |
3333 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3334 |
+ |
|
3335 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3336 |
+ |
|
3337 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3338 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3339 |
+ |
|
3340 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3341 |
+ |
|
3342 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3343 |
+ |
srcs.add(complete); |
3344 |
+ |
srcs.add(incomplete); |
3345 |
+ |
|
3346 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3347 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3348 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3349 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3350 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3351 |
+ |
|
3352 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3353 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3354 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3355 |
+ |
|
3356 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3357 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3358 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3359 |
+ |
|
3360 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3361 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3362 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3363 |
+ |
|
3364 |
+ |
for (CompletableFuture<?> future : fs) { |
3365 |
+ |
if (src.isDone()) |
3366 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3367 |
+ |
else |
3368 |
+ |
checkIncomplete(future); |
3369 |
+ |
} |
3370 |
+ |
futures.addAll(fs); |
3371 |
+ |
} |
3372 |
+ |
|
3373 |
+ |
{ |
3374 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3375 |
+ |
|
3376 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3377 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3378 |
+ |
|
3379 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3380 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3381 |
+ |
|
3382 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3383 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3384 |
+ |
|
3385 |
+ |
for (CompletableFuture<?> future : fs) |
3386 |
+ |
checkIncomplete(future); |
3387 |
+ |
futures.addAll(fs); |
3388 |
+ |
} |
3389 |
+ |
|
3390 |
+ |
{ |
3391 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3392 |
+ |
|
3393 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3394 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3395 |
+ |
|
3396 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3397 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3398 |
+ |
|
3399 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3400 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3401 |
+ |
|
3402 |
+ |
for (CompletableFuture<?> future : fs) |
3403 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3404 |
+ |
futures.addAll(fs); |
3405 |
+ |
} |
3406 |
+ |
|
3407 |
+ |
incomplete.complete(v); |
3408 |
+ |
|
3409 |
+ |
for (CompletableFuture<?> future : futures) |
3410 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3411 |
+ |
|
3412 |
+ |
assertEquals(futures.size(), e.count.get()); |
3413 |
+ |
|
3414 |
+ |
} |
3415 |
+ |
} |
3416 |
+ |
|
3417 |
+ |
/** |
3418 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3419 |
+ |
* should never be invoked because the dependent future is |
3420 |
+ |
* explicitly completed. |
3421 |
+ |
*/ |
3422 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3423 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3424 |
+ |
|
3425 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3426 |
+ |
|
3427 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3428 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3429 |
+ |
|
3430 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3431 |
+ |
|
3432 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3433 |
+ |
srcs.add(complete); |
3434 |
+ |
srcs.add(incomplete); |
3435 |
+ |
|
3436 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3437 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3438 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3439 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3440 |
+ |
|
3441 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3442 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3443 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3444 |
+ |
|
3445 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3446 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3447 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3448 |
+ |
|
3449 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3450 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3451 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3452 |
+ |
|
3453 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3454 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3455 |
+ |
|
3456 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3457 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3458 |
+ |
|
3459 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3460 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3461 |
+ |
|
3462 |
+ |
for (CompletableFuture<?> future : fs) |
3463 |
+ |
checkIncomplete(future); |
3464 |
+ |
|
3465 |
+ |
for (CompletableFuture<?> future : fs) |
3466 |
+ |
future.complete(null); |
3467 |
+ |
|
3468 |
+ |
incomplete.complete(v); |
3469 |
+ |
|
3470 |
+ |
for (CompletableFuture<?> future : fs) |
3471 |
+ |
checkCompletedNormally(future, null); |
3472 |
+ |
|
3473 |
+ |
assertEquals(0, e.count.get()); |
3474 |
+ |
|
3475 |
+ |
} |
3476 |
+ |
} |
3477 |
+ |
|
3478 |
|
/** |
3479 |
|
* toCompletableFuture returns this CompletableFuture. |
3480 |
|
*/ |