From 8f885cfc7939ee2e35f65549235723411c29ff60 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Tue, 3 Nov 2026 19:52:24 +0000 Subject: [PATCH 1/2] Raise data-source lifecycle delegates (#7473) --- docs/docs/execution/retrying.md | 4 + src/TUnit.Core/Interfaces/ITestEvents.cs | 8 +- src/TUnit.Core/TestContextEvents.cs | 9 +- .../Services/EventReceiverOrchestrator.cs | 45 ++++-- .../Services/TestExecution/RetryHelper.cs | 21 +++ .../Services/TestFilterService.cs | 27 ++++ .../DelegateLifecycleEventTests.cs | 115 +++++++++++++++ .../PropertyRegistrationDelegateTests.cs | 57 ++++++++ .../EventReceiverRegistrationTests.cs | 136 ++++++++++++++++++ .../RetryDelegateCancellationTests.cs | 71 +++++++++ 10 files changed, 482 insertions(+), 11 deletions(-) create mode 100644 tests/TUnit.TestProject/DelegateLifecycleEventTests.cs create mode 100644 tests/TUnit.TestProject/PropertyRegistrationDelegateTests.cs create mode 100644 tests/TUnit.UnitTests/RetryDelegateCancellationTests.cs diff --git a/docs/docs/execution/retrying.md b/docs/docs/execution/retrying.md index 47386052542..9a038744813 100644 --- a/docs/docs/execution/retrying.md +++ b/docs/docs/execution/retrying.md @@ -94,3 +94,7 @@ So the order of precedence is: 1. Method 1. Class 1. Assembly + +## Retry event handlers + +Handlers subscribed through `TestBuilderContext.Events.OnTestRetry` receive the upcoming, one-based retry number after backoff and cancellation checks. If a handler throws, retry processing stops and the failure propagates. If cancellation was already accepted, the test remains cancelled instead. diff --git a/src/TUnit.Core/Interfaces/ITestEvents.cs b/src/TUnit.Core/Interfaces/ITestEvents.cs index 335351ece4c..c8247958c39 100644 --- a/src/TUnit.Core/Interfaces/ITestEvents.cs +++ b/src/TUnit.Core/Interfaces/ITestEvents.cs @@ -29,6 +29,7 @@ public interface ITestEvents /// /// Gets the event that is raised when the test has been registered with the test runner. /// + /// Handler failures propagate and stop registration. AsyncEvent? OnTestRegistered { get; } /// @@ -39,20 +40,25 @@ public interface ITestEvents /// /// Gets the event that is raised before the test method is invoked. /// + /// Handler failures propagate and prevent the test method from running. AsyncEvent? OnTestStart { get; } /// /// Gets the event that is raised after the test method has completed. /// + /// Handler failures join the teardown exception collection, alongside receiver failures. AsyncEvent? OnTestEnd { get; } /// /// Gets the event that is raised if the test was skipped. /// + /// Handler failures propagate. AsyncEvent? OnTestSkipped { get; } /// - /// Gets the event that is raised before a test is retried. + /// Gets the event raised before a retry, after backoff and cancellation checks. + /// The retry attempt is one-based. A handler failure aborts retry processing; + /// an accepted cancellation is preserved as a cancelled result. /// AsyncEvent<(TestContext TestContext, int RetryAttempt)>? OnTestRetry { get; } } diff --git a/src/TUnit.Core/TestContextEvents.cs b/src/TUnit.Core/TestContextEvents.cs index 1fa75485b6c..359df9a8d1b 100644 --- a/src/TUnit.Core/TestContextEvents.cs +++ b/src/TUnit.Core/TestContextEvents.cs @@ -3,7 +3,8 @@ namespace TUnit.Core; /// -/// Test context events container +/// Test context events container. Registration, start and skipped handler failures propagate +/// and stop the remaining handlers. End handler failures are collected with teardown failures. /// public class TestContextEvents : ITestEvents { @@ -80,8 +81,14 @@ internal DataRowScopeCollection? DataRowScopes public AsyncEvent? OnDispose { get; set; } public AsyncEvent? OnTestRegistered { get; set; } public AsyncEvent? OnInitialize { get; set; } + // Delegate subscribers run once in the late stage, after early-stage receivers. public AsyncEvent? OnTestStart { get; set; } public AsyncEvent? OnTestEnd { get; set; } public AsyncEvent? OnTestSkipped { get; set; } + /// + /// Gets or sets handlers invoked before a retry, after backoff and cancellation checks. + /// The retry attempt is one-based. A handler failure aborts retry processing; + /// an accepted cancellation is preserved as a cancelled result. + /// public AsyncEvent<(TestContext TestContext, int RetryAttempt)>? OnTestRetry { get; set; } } diff --git a/src/TUnit.Engine/Services/EventReceiverOrchestrator.cs b/src/TUnit.Engine/Services/EventReceiverOrchestrator.cs index 8bf554dd6e1..b64b4979c9d 100644 --- a/src/TUnit.Engine/Services/EventReceiverOrchestrator.cs +++ b/src/TUnit.Engine/Services/EventReceiverOrchestrator.cs @@ -154,7 +154,7 @@ public ValueTask InvokeTestStartEventReceiversAsync(TestContext context, Cancell } #endif // Fast path - no allocation if no receivers - if (!_registry.HasTestStartReceivers()) + if (!_registry.HasTestStartReceivers() && (stage == EventReceiverStage.Early || context.InternalEventsIfCreated?.OnTestStart is null)) { return ValueTask.CompletedTask; } @@ -196,6 +196,14 @@ private async ValueTask InvokeTestStartEventReceiversCore(TestContext context, C await receiver.OnTestStart(context); } #endif + // Delegate events have no stage; dispatch once, with the default (late) receivers. + if (stage != EventReceiverStage.Early && context.InternalEventsIfCreated?.OnTestStart is { } onStart) + { + foreach (var handler in onStart.InvocationList) + { + await handler.InvokeAsync(context, context); + } + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -207,7 +215,7 @@ public ValueTask> InvokeTestEndEventReceiversAsync(Test return new ValueTask>([]); } #endif - if (!_registry.HasTestEndReceivers()) + if (!_registry.HasTestEndReceivers() && (stage == EventReceiverStage.Early || context.InternalEventsIfCreated?.OnTestEnd is null)) { return new ValueTask>([]); } @@ -215,7 +223,7 @@ public ValueTask> InvokeTestEndEventReceiversAsync(Test if (stage.HasValue) { var receivers = context.GetTestEndReceivers(stage.Value); - if (receivers.Length == 0) + if (receivers.Length == 0 && (stage == EventReceiverStage.Early || context.InternalEventsIfCreated?.OnTestEnd is null)) { return new ValueTask>([]); } @@ -225,7 +233,7 @@ public ValueTask> InvokeTestEndEventReceiversAsync(Test var earlyReceivers = context.GetTestEndReceivers(EventReceiverStage.Early); var lateReceivers = context.GetTestEndReceivers(EventReceiverStage.Late); - if (earlyReceivers.Length == 0 && lateReceivers.Length == 0) + if (earlyReceivers.Length == 0 && lateReceivers.Length == 0 && context.InternalEventsIfCreated?.OnTestEnd is null) { return new ValueTask>([]); } @@ -309,13 +317,29 @@ private async ValueTask> InvokeTestEndEventReceiversCor } #endif + if (stage != EventReceiverStage.Early && context.InternalEventsIfCreated?.OnTestEnd is { } onEnd) + { + foreach (var handler in onEnd.InvocationList) + { + try + { + await handler.InvokeAsync(context, context); + } + catch (Exception ex) + { + await _logger.LogErrorAsync($"Error in test end event handler: {ex.Message}"); + (exceptions ??= []).Add(ex); + } + } + } + return exceptions == null ? Array.Empty() : exceptions; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeTestSkippedEventReceiversAsync(TestContext context, CancellationToken cancellationToken) { - if (!_registry.HasTestSkippedReceivers()) + if (!_registry.HasTestSkippedReceivers() && context.InternalEventsIfCreated?.OnTestSkipped is null) { return ValueTask.CompletedTask; } @@ -328,14 +352,17 @@ private async ValueTask InvokeTestSkippedEventReceiversCore(TestContext context, // Use pre-computed receivers (already filtered, sorted, and scoped-attribute filtered) var receivers = context.GetTestSkippedReceivers(); - if (receivers.Length == 0) + foreach (var receiver in receivers) { - return; + await receiver.OnTestSkipped(context); } - foreach (var receiver in receivers) + if (context.InternalEventsIfCreated?.OnTestSkipped is { } onSkipped) { - await receiver.OnTestSkipped(context); + foreach (var handler in onSkipped.InvocationList) + { + await handler.InvokeAsync(context, context); + } } } diff --git a/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs b/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs index 09af835a7ba..89d9833191f 100644 --- a/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs +++ b/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs @@ -109,6 +109,27 @@ public static async Task ExecuteWithRetry(TestContext testContext, Func.Invocation>? invokedDelegates = null; + await InvokeRegistrationDelegatesAsync().ConfigureAwait(false); + + async Task InvokeRegistrationDelegatesAsync() + { + if (test.Context.InternalEventsIfCreated?.OnTestRegistered is not { } onRegistered) + { + return; + } + + // Snapshot because a handler can subscribe another handler while it runs. + // Invocation identity preserves duplicate subscriptions while skipping the earlier pass. + foreach (var handler in onRegistered.InvocationList) + { + invokedDelegates ??= []; + if (invokedDelegates.Add(handler)) + { + await handler.InvokeAsync(test.Context, test.Context).ConfigureAwait(false); + } + } + } + // If the test was marked as skipped by an event receiver, skip argument registration entirely. // This avoids initializing expensive shared data sources (e.g., WebApplicationFactory) // for tests that will never execute. @@ -191,6 +213,11 @@ private async Task RegisterTest(AbstractExecutableTest test, bool isForExecution await InvokeTestRegisteredReceivers(test.Context.GetTestRegisteredReceivers(), registeredContext, invokedReceivers).ConfigureAwait(false); } + + if (argumentsRegistered) + { + await InvokeRegistrationDelegatesAsync().ConfigureAwait(false); + } } // Clear the cached display name after registration events diff --git a/tests/TUnit.TestProject/DelegateLifecycleEventTests.cs b/tests/TUnit.TestProject/DelegateLifecycleEventTests.cs new file mode 100644 index 00000000000..76f17c8bb80 --- /dev/null +++ b/tests/TUnit.TestProject/DelegateLifecycleEventTests.cs @@ -0,0 +1,115 @@ +using TUnit.TestProject.Attributes; + +namespace TUnit.TestProject; + +public class DelegateLifecycleEventTests +{ + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] + public sealed class SubscribeAttribute : DataSourceGeneratorAttribute + { + public int Rows { get; set; } = 1; + + protected override IEnumerable> GenerateDataSources(DataGeneratorMetadata metadata) + { + for (var i = 0; i < Rows; i++) + { + yield return () => + { + metadata.TestBuilderContext.Current.StateBag["factoryState"] = "preserved"; + var events = metadata.TestBuilderContext.Current.Events; + events.OnTestRegistered += Record("registered"); + events.OnTestStart += Record("start"); + events.OnTestEnd += Record("end"); + events.OnTestSkipped += Record("skipped"); + events.OnTestRetry += (_, args) => + { + args.TestContext.StateBag.Items["retry"] = args.RetryAttempt; + return default; + }; + return 1; + }; + } + } + + private static Func Record(string name) => (_, context) => + { + context.StateBag.Items.TryGetValue(name, out var count); + context.StateBag.Items[name] = (count as int? ?? 0) + 1; + return default; + }; + } + + [Test, Subscribe, EngineTest(ExpectedResult.Pass)] + public async Task Passing(int value) + { + await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); + await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); + } + + [Test, Subscribe, EngineTest(ExpectedResult.Pass)] + public async Task Generic(T value) + { + await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); + await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); + } + + [Test, Subscribe, Retry(1), EngineTest(ExpectedResult.Pass)] + public async Task Retried(int value) + { + var context = TestContext.Current!; + if (!context.StateBag.Items.ContainsKey("retry")) + { + throw new InvalidOperationException("Exercise retry notification"); + } + + await Assert.That(context.StateBag.Items["retry"]).IsEqualTo(1); + await Assert.That(context.StateBag.Items["start"]).IsEqualTo(2); + await Assert.That(context.StateBag.Items["end"]).IsEqualTo(1); + } + + [Test, Subscribe, Skip("Exercise skipped notification")] + public void Skipped(int value) => throw new InvalidOperationException("Must not execute"); + + [After(Class)] + public static async Task Verify(ClassHookContext context) + { + foreach (var test in context.Tests) + { + await Assert.That(test.StateBag.Items["registered"]).IsEqualTo(1); + if (test.Metadata.TestDetails.MethodName == nameof(Skipped)) + { + await Assert.That(test.StateBag.Items["skipped"]).IsEqualTo(1); + await Assert.That(test.StateBag.Items["end"]).IsEqualTo(1); + await Assert.That(test.StateBag.Items.ContainsKey("start")).IsFalse(); + } + else + { + var expected = test.Metadata.TestDetails.MethodName == nameof(Retried) ? 2 : 1; + await Assert.That(test.StateBag.Items["end"]).IsEqualTo(expected); + } + } + } +} + +[DelegateLifecycleEventTests.Subscribe(Rows = 2)] +public class ClassDelegateLifecycleEventTests(int value) +{ + [Test, Repeat(1), EngineTest(ExpectedResult.Pass)] + public async Task FirstRow() + { + await Assert.That(value).IsEqualTo(1); + await Assert.That(TestContext.Current!.StateBag.Items["factoryState"]).IsEqualTo("preserved"); + await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); + await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); + } + + [After(Class)] + public static async Task Verify(ClassHookContext context) + { + foreach (var test in context.Tests) + { + await Assert.That(test.StateBag.Items["end"]).IsEqualTo(1); + } + } +} + diff --git a/tests/TUnit.TestProject/PropertyRegistrationDelegateTests.cs b/tests/TUnit.TestProject/PropertyRegistrationDelegateTests.cs new file mode 100644 index 00000000000..6e06f0c2304 --- /dev/null +++ b/tests/TUnit.TestProject/PropertyRegistrationDelegateTests.cs @@ -0,0 +1,57 @@ +using TUnit.TestProject.Attributes; + +namespace TUnit.TestProject; + +[PropertyRegistrationDelegateTests.ConstructorData] +public class PropertyRegistrationDelegateTests(int value) +{ + [PropertyData] + public required Service Injected { get; init; } + + public sealed class Service; + + public sealed class ConstructorDataAttribute : DataSourceGeneratorAttribute + { + protected override IEnumerable> GenerateDataSources(DataGeneratorMetadata metadata) + { + yield return () => + { + var handler = Record("constructor"); + metadata.TestBuilderContext.Current.Events.OnTestRegistered += handler; + metadata.TestBuilderContext.Current.Events.OnTestRegistered += handler; + return 1; + }; + } + } + + public sealed class PropertyDataAttribute : DataSourceGeneratorAttribute + { + protected override IEnumerable> GenerateDataSources(DataGeneratorMetadata metadata) + { + yield return () => + { + var events = metadata.TestBuilderContext.Current.Events; + events.OnTestRegistered ??= new(); + // A newly inserted handler sorts ahead of the previously dispatched subscription. + events.OnTestRegistered.Add(Record("property"), order: 0); + return new Service(); + }; + } + } + + private static Func Record(string key) => (_, context) => + { + context.StateBag.Items.TryGetValue(key, out var count); + context.StateBag.Items[key] = (count as int? ?? 0) + 1; + return default; + }; + + [Test, Repeat(1), EngineTest(ExpectedResult.Pass)] + public async Task PropertyAndConstructorSubscriptionsRunOnce() + { + await Assert.That(value).IsEqualTo(1); + await Assert.That(Injected).IsNotNull(); + await Assert.That(TestContext.Current!.StateBag.Items["constructor"]).IsEqualTo(2); + await Assert.That(TestContext.Current.StateBag.Items["property"]).IsEqualTo(1); + } +} diff --git a/tests/TUnit.UnitTests/EventReceiverRegistrationTests.cs b/tests/TUnit.UnitTests/EventReceiverRegistrationTests.cs index dbef91b51f0..f4cd76542d1 100644 --- a/tests/TUnit.UnitTests/EventReceiverRegistrationTests.cs +++ b/tests/TUnit.UnitTests/EventReceiverRegistrationTests.cs @@ -1,3 +1,4 @@ +using NSubstitute; using TUnit.Core.Interfaces; using TUnit.Engine.Services; @@ -5,6 +6,141 @@ namespace TUnit.UnitTests; public class EventReceiverRegistrationTests { + [Test] + [Arguments("start")] + [Arguments("end")] + public async Task DuplicateDelegateSubscriptionsRunOnlyInLateStage(string eventName) + { + var context = CreateContext(new OrdinaryObject(), []); + var calls = 0; + Func handler = (_, _) => { calls++; return default; }; + var selected = eventName == "start" + ? context.InternalEvents.OnTestStart ??= new() + : context.InternalEvents.OnTestEnd ??= new(); + selected.Add(handler); + selected.Add(handler); + try + { + var orchestrator = new EventReceiverOrchestrator(null!); + async Task Dispatch(TUnit.Core.Enums.EventReceiverStage stage) + { + if (eventName == "start") + await orchestrator.InvokeTestStartEventReceiversAsync(context, CancellationToken.None, stage); + else + await orchestrator.InvokeTestEndEventReceiversAsync(context, CancellationToken.None, stage); + } + await Dispatch(TUnit.Core.Enums.EventReceiverStage.Early); + await Assert.That(calls).IsEqualTo(0); + await Dispatch(TUnit.Core.Enums.EventReceiverStage.Late); + await Assert.That(calls).IsEqualTo(2); + } + finally + { + context.RemoveFromRegistry(); + context.Dispose(); + } + } + + [Test] + [Arguments("start")] + [Arguments("end")] + [Arguments("skipped")] + public async Task DelegateSubscriptionsDuringDispatchApplyToNextDispatch(string eventName) + { + var context = CreateContext(new OrdinaryObject(), []); + var calls = new List(); + var events = context.InternalEvents; + var selected = eventName switch + { + "start" => events.OnTestStart ??= new(), + "end" => events.OnTestEnd ??= new(), + _ => events.OnTestSkipped ??= new() + }; + selected += (_, _) => + { + calls.Add("first"); + selected += (_, _) => { calls.Add("added"); return ValueTask.CompletedTask; }; + return ValueTask.CompletedTask; + }; + selected += (_, _) => { calls.Add("last"); return ValueTask.CompletedTask; }; + try + { + var orchestrator = new EventReceiverOrchestrator(null!); + async Task Dispatch() + { + if (eventName == "start") + await orchestrator.InvokeTestStartEventReceiversAsync(context, CancellationToken.None); + else if (eventName == "end") + await orchestrator.InvokeTestEndEventReceiversAsync(context, CancellationToken.None); + else + await orchestrator.InvokeTestSkippedEventReceiversAsync(context, CancellationToken.None); + } + + await Dispatch(); + await Assert.That(string.Join(",", calls)).IsEqualTo("first,last"); + calls.Clear(); + await Dispatch(); + await Assert.That(string.Join(",", calls)).IsEqualTo("first,last,added"); + } + finally + { + context.RemoveFromRegistry(); + context.Dispose(); + } + } + + [Test] + public async Task EndDelegateMutationPreservesRemainingHandlersAndCollectedFailures() + { + var context = CreateContext(new OrdinaryObject(), []); + var failure = new InvalidOperationException("End handler failed"); + var lastRan = false; + context.InternalEvents.OnTestEnd += (_, _) => + { + context.InternalEvents.OnTestEnd += (_, _) => ValueTask.CompletedTask; + throw failure; + }; + context.InternalEvents.OnTestEnd += (_, _) => { lastRan = true; return ValueTask.CompletedTask; }; + var options = NSubstitute.Substitute.For(); + options.TryGetOptionArgumentList(NSubstitute.Arg.Any(), out NSubstitute.Arg.Any()!) + .Returns(call => { call[1] = new[] { "None" }; return true; }); + var logger = new TUnit.Engine.Logging.TUnitFrameworkLogger(null!, null!, null!, new LogLevelProvider(options)); + try + { + var orchestrator = new EventReceiverOrchestrator(logger); + var exceptions = await orchestrator.InvokeTestEndEventReceiversAsync(context, CancellationToken.None); + await Assert.That(lastRan).IsTrue(); + await Assert.That(exceptions.Count).IsEqualTo(1); + await Assert.That(exceptions[0]).IsSameReferenceAs(failure); + } + finally + { + context.RemoveFromRegistry(); + context.Dispose(); + } + } + + [Test] + public async Task StartDelegateFailurePropagatesOriginalException() + { + var context = CreateContext(new OrdinaryObject(), []); + var failure = new InvalidOperationException("Start handler failed"); + context.InternalEvents.OnTestStart += (_, _) => throw failure; + try + { + var orchestrator = new EventReceiverOrchestrator(null!); + var thrown = await Assert.That(async () => + await orchestrator.InvokeTestStartEventReceiversAsync(context, CancellationToken.None)) + .Throws(); + await Assert.That(thrown).IsSameReferenceAs(failure); + } + finally + { + context.RemoveFromRegistry(); + context.Dispose(); + } + } + [Test] public void OrdinaryObjectsDoNotParticipateInReceiverDeduplication() { diff --git a/tests/TUnit.UnitTests/RetryDelegateCancellationTests.cs b/tests/TUnit.UnitTests/RetryDelegateCancellationTests.cs new file mode 100644 index 00000000000..e01d0f2584a --- /dev/null +++ b/tests/TUnit.UnitTests/RetryDelegateCancellationTests.cs @@ -0,0 +1,71 @@ +using TUnit.Core; +using TUnit.Engine.Services.TestExecution; + +namespace TUnit.UnitTests; + +public class RetryDelegateCancellationTests +{ + [Test] + [Arguments(true)] + [Arguments(false)] + public async Task FailingRetryHandlerCompletesCancellation(bool requestCancellation) + { + var current = TestContext.Current!; + var builder = new TestBuilderContext { TestMetadata = current.TestDetails.MethodMetadata }; + var context = new TestContext(nameof(RetryDelegateCancellationTests), current.ServiceProvider, + current.ClassContext, builder, CancellationToken.None); + context.Metadata.TestDetails = new TestDetails([]) + { + TestId = context.Id, TestName = "Retry", ClassType = typeof(RetryDelegateCancellationTests), + MethodName = "Retry", ClassInstance = this, TestMethodArguments = [], TestClassArguments = [], + MethodMetadata = current.TestDetails.MethodMetadata, ReturnType = typeof(void), RetryLimit = 1, + AttributesByType = new Dictionary>() + }; + var executable = new StubExecutableTest { TestId = context.Id, Arguments = [], Context = context }; + context.InitializeTestCancellation(CancellationToken.None); + context.CompleteTestCancellation(); + var failure = new InvalidOperationException("Retry handler failure"); + builder.Events.OnTestRetry += (_, _) => + { + if (requestCancellation) + { + context.Cancel(); + } + throw failure; + }; + var attempts = 0; + try + { + async Task Execute() => await RetryHelper.ExecuteWithRetry(context, () => + { + attempts++; + throw new InvalidOperationException("Attempt failure"); + }); + + if (requestCancellation) + { + await Execute(); + await Assert.That(executable.State).IsEqualTo(TestState.Cancelled); + } + else + { + var thrown = await Assert.That(Execute).Throws(); + await Assert.That(thrown).IsSameReferenceAs(failure); + context.Cancel(); + await Assert.That(context.IsTestCancellationRequested).IsFalse(); + } + await Assert.That(attempts).IsEqualTo(1); + } + finally + { + context.DisposeLinkedCancellationTokenSources(); + context.RemoveFromRegistry(); + } + } + + private sealed class StubExecutableTest : AbstractExecutableTest + { + public override Task CreateInstanceAsync() => throw new NotSupportedException(); + public override Task InvokeTestAsync(object instance, CancellationToken cancellationToken) => throw new NotSupportedException(); + } +} From 8ea91bd0a116c4d21925b191559120c071ed6cfc Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Tue, 3 Nov 2026 20:00:36 +0000 Subject: [PATCH 2/2] Stop retrying after successful handlers accept cancellation (#7473) --- .../Services/TestExecution/RetryHelper.cs | 6 ++++++ .../RetryDelegateCancellationTests.cs | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs b/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs index 89d9833191f..f265ec8ea88 100644 --- a/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs +++ b/src/TUnit.Engine/Services/TestExecution/RetryHelper.cs @@ -130,6 +130,12 @@ public static async Task ExecuteWithRetry(TestContext testContext, Func