Skip to content

Introduce support for cancelling a running execution to the Launcher API #4728

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

Merged
merged 2 commits into from
Jul 7, 2025

Conversation

marcphilipp
Copy link
Member

@marcphilipp marcphilipp commented Jul 7, 2025

Overview

This commit introduces CancellationToken that can be created and
passed to the Launcher as part of a LauncherExecutionRequest. The
Launcher checks whether cancellation has been requested on the token
prior to asking each test engine to execute tests. If cancellation has
been requested, the Launcher reports all direct children of engine
descriptors as skipped. Moreover, it passes the CancellationToken to
each engine so they can check and respond to cancellation as well which
will be implemented separately as indicated by TODO comments.

EngineTestKit also supports passing a CancellationToken to the
engine under test.

The documentation now contains an example for implementing a fail-fast
listener and documents the additional requirement for test engines that
wish to support cancellation.

Resolves #1880.


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

This commit introduces `CancellationToken` that can be created and
passed to the `Launcher` as part of a `LauncherExecutionRequest`. The
`Launcher` checks whether cancellation has been requested on the token
prior to asking each test engine to execute tests. If cancellation has
been requested, the `Launcher` reports all direct children of engine
descriptors as skipped. Moreover, it passes the `CancellationToken` to
each engine so they can check and respond to cancellation as well which
will be implemented separately as indicated by TODO comments.

`EngineTestKit` also supports passing a `CancellationToken` to the
engine under test.

The documentation now contains an example for implementing a fail-fast
listener and documents the additional requirement for test engines that
wish to support cancellation.

Resolves #1880.
@marcphilipp marcphilipp force-pushed the marc/1880-cancellation-token branch from 4b6c2df to eec8df0 Compare July 7, 2025 10:35
@marcphilipp marcphilipp marked this pull request as ready for review July 7, 2025 10:35
@marcphilipp marcphilipp merged commit 6153c35 into main Jul 7, 2025
14 checks passed
@marcphilipp marcphilipp deleted the marc/1880-cancellation-token branch July 7, 2025 10:50
@iloveeclipse
Copy link

iloveeclipse commented Jul 7, 2025

@marcphilipp : could you please give a hint for IDE providers:

  1. Which API is suggested
  2. Starting from which JUnit version
  3. Any side effects/behavior changes/incompatibilities related to that API that need to be considered by IDE providers
  4. Give a link to the final document describing the usage of the new API?

Thanks.

@marcphilipp
Copy link
Member Author

Sure!

Which API is suggested

The Launcher API will get a new execute(LauncherExecutionRequest) method and LauncherExecutionRequest.getCancellationToken() is checked by the JUnit Platform and registered test engines to determine whether cancellation has been requests.

Starting from which JUnit version

6.0.0-M2 (not released yet)

Any side effects/behavior changes/incompatibilities related to that API that need to be considered by IDE providers

To effectively cancel test execution, registered test engines will have to implement support cancellation. As you can see in the umbrella issue #4725, all engines provided by the JUnit team (Jupiter, Suites, Vintage, and TestNG) will do so and some third-party engines (most notably, Cucumber and Spock) will automatically receive support since they extend HierarchicalTestEngine. Other engines may be slower to adopt. Therefore, I think IDE providers should first try to cancel execution gracefully but have an option or timeout in place after which the forked JVM is terminated without waiting in case a user has requested test execution to be cancelled.

Give a link to the final document describing the usage of the new API?

I added a full example to the docs: https://docs.junit.org/snapshot/user-guide/#launcher-api-launcher-cancellation

Please let me know if you have any concerns or questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce support for cancelling a running execution to the Launcher API
3 participants