Honor cancellation tokens linked by custom test executors - #6565
Conversation
Resolve the test body token when a custom executor invokes the action so tokens linked by that executor reach injected CancellationToken parameters.
Greptile SummaryThe PR changes custom test-executor invocation to resolve the test-body cancellation token when the executor invokes its action, allowing tokens linked by that executor to reach injected
Confidence Score: 5/5The PR appears safe to merge, with the cancellation-token propagation change covered by reflection and source-generated execution tests. The custom-executor path now reads the execution context’s current linked token at action invocation, while the context token retains the original engine cancellation source and the regression test verifies the intended observable behavior.
|
| Filename | Overview |
|---|---|
| src/TUnit.Engine/TestExecutor.cs | Defers cancellation-token resolution until the custom executor invokes the test action, preserving engine cancellation while incorporating newly linked tokens. |
| tests/TUnit.Engine.Tests/TestExecutorCancellationTokenTests.cs | Adds filtered engine-level coverage that requires exactly one cancellation regression test to execute and pass across configured execution modes. |
| tests/TUnit.TestProject/TestExecutorCancellationTokenTests.cs | Adds a custom executor that links and cancels a token before invocation and verifies that the injected test-body token observes cancellation. |
Sequence Diagram
sequenceDiagram
participant Engine as TUnit Engine
participant Executor as Custom ITestExecutor
participant Context as TestContext.Execution
participant Test as Test Method
Engine->>Executor: ExecuteTest(context, action)
Executor->>Context: AddLinkedCancellationToken(token)
Executor->>Executor: Cancel linked token
Executor->>Engine: Invoke action
Engine->>Context: Read current CancellationToken
Engine->>Test: InvokeTestAsync(current token)
Test-->>Engine: Observes cancellation
Reviews (1): Last reviewed commit: "fix(engine): honor executor-linked cance..." | Re-trigger Greptile
Code reviewReviewed Summary: Verification:
Non-blocking observation: No blocking issues found. The change is minimal, well-targeted, and the root-cause explanation in the PR description matches what's in the code. |
Summary
ITestExecutorinvokes its actionRoot cause
The executor action captured the engine cancellation token before the custom executor ran. Calling
context.Execution.AddLinkedCancellationToken(...)updated the context, but the test method still received the stale captured token.User impact
Custom test executors can now link and cancel a token before invoking the test body, and an injected
CancellationTokenobserves that cancellation. This enables the background-failure executor pattern discussed in discussion #3099.Validation
dotnet build tests/TUnit.TestProject/TUnit.TestProject.csproj -c Release --no-restore --nologonet8.0,net9.0, andnet10.0net10.0AOT runtime coverage remains enabled for CI; it is skipped locally by the existing test harness.