Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompletableFutureTask, fix spurious error logging when cancelling #3135

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public void run() {
future.run();
try {
complete(future.get());
} catch (ExecutionException exc) {
completeExceptionally(exc.getCause());
} catch (InterruptedException exc) {
Thread.currentThread().interrupt();
completeExceptionally(exc);
} catch (Exception exc) {
completeExceptionally(exc.getCause());
completeExceptionally(exc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,39 @@ private static List<Executor> executors() {
Executors.newSingleThreadExecutor(),
Executors.newCachedThreadPool(),
Executors.newWorkStealingPool(),
new MyTestExecutorWithException(),
ForkJoinPool.commonPool()
);
}

// Very basic executor that spawns a new thread
// and allows to wait for the end of the command.
// It just keeps an exception to be able to assert it.
// You should use it to launch only one command
// because it has just one latch and one exception
private static class MyTestExecutorWithException implements Executor {

Exception exception = null;
CountDownLatch waitForDone;

@Override
public void execute(Runnable command) {
(new Thread() {
@Override
public void run() {
waitForDone = new CountDownLatch(1);
try {
command.run();
} catch (Exception e) {
MyTestExecutorWithException.this.exception = e;
} finally {
waitForDone.countDown();
}
}
}).start();
}
}

@ParameterizedTest
@MethodSource("parameters")
void whenSupplyObjectThenReturnIt(Executor executor) throws Exception {
Expand Down Expand Up @@ -125,6 +154,10 @@ private void testEffectiveInterrupt(boolean addDependant, Executor executor) thr
//Second call to cancel should return false
cancelled = task.cancel(true);
assertFalse(cancelled);
if (executor instanceof MyTestExecutorWithException myTestExecutor) {
myTestExecutor.waitForDone.await();
assertNull(myTestExecutor.exception);
}
}

@ParameterizedTest
Expand Down