Skip to content

Commit bcd21b7

Browse files
committed
Handle cancellation
1 parent 6608ec9 commit bcd21b7

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ The general recommendation is to migrate from xunit 2 to xunit.v3 which already
2424
- `TestMethodIdentifierProperty` is missing the parameter types for parameterized tests.
2525
- MTP's `--treenode-filter` is not yet supported.
2626
- MTP's `--maximum-failed-tests` is not yet supported.
27-
- Cancellation isn't yet supported.

src/YTest.MTP.XUnit2/MTPFramework/XUnit2MTPTestFramework.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
89
using Microsoft.Testing.Platform.CommandLine;
@@ -111,7 +112,7 @@ private async Task DiscoverTestsAsync(
111112

112113
using var frontController = GetFrontController(assemblyPath, configuration);
113114

114-
var testCases = await DiscoverAsync(frontController, configuration);
115+
var testCases = await DiscoverAsync(frontController, configuration, context.CancellationToken);
115116

116117
foreach (ITestCase test in testCases)
117118
{
@@ -182,7 +183,7 @@ private async Task RunTestsAsync(
182183
{
183184
var configuration = GetConfiguration(assemblyPath);
184185
using var frontController = GetFrontController(assemblyPath, configuration);
185-
var testCases = await DiscoverAsync(frontController, configuration);
186+
var testCases = await DiscoverAsync(frontController, configuration, context.CancellationToken);
186187

187188
var assemblyDisplayName = Path.GetFileNameWithoutExtension(assemblyPath);
188189
var executionSinkOptions = new ExecutionSinkOptions
@@ -200,9 +201,9 @@ private async Task RunTestsAsync(
200201
// TODO: SessionFileArtifact
201202
}
202203

203-
private static async Task<List<ITestCase>> DiscoverAsync(XunitFrontController frontController, TestAssemblyConfiguration configuration)
204+
private static async Task<List<ITestCase>> DiscoverAsync(XunitFrontController frontController, TestAssemblyConfiguration configuration, CancellationToken cancellationToken)
204205
{
205-
using var sink = new TestDiscoverySink();
206+
using var sink = new TestDiscoverySink(() => cancellationToken.IsCancellationRequested);
206207
frontController.Find(includeSourceInformation: true, sink, TestFrameworkOptions.ForDiscovery(configuration));
207208
await Task.Factory.StartNew(sink.Finished.WaitOne, TaskCreationOptions.LongRunning);
208209
return sink.TestCases;

src/YTest.MTP.XUnit2/XUnitSinks/MTPExecutionSink.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,67 @@ private void OnTestCaseStarting(MessageHandlerArgs<ITestCaseStarting> args)
3838
var testNode = CreateTestNode(args.Message);
3939
testNode.Properties.Add(InProgressTestNodeStateProperty.CachedInstance);
4040
PublishTestUpdate(testNode);
41+
StopIfCancellationIsRequested(args);
4142
}
4243

4344
private void OnTestFailed(MessageHandlerArgs<ITestFailed> args)
44-
=> OnFailure(null, args.Message, args.Message);
45+
{
46+
OnFailure(null, args.Message, args.Message);
47+
StopIfCancellationIsRequested(args);
48+
}
4549

4650
private void OnTestPassed(MessageHandlerArgs<ITestPassed> args)
4751
{
4852
var testNode = CreateTestNode(args.Message);
4953
testNode.Properties.Add(PassedTestNodeStateProperty.CachedInstance);
5054

5155
PublishTestUpdate(testNode);
56+
StopIfCancellationIsRequested(args);
5257
}
5358

5459
private void OnTestSkipped(MessageHandlerArgs<ITestSkipped> args)
5560
{
5661
var testNode = CreateTestNode(args.Message);
5762
testNode.Properties.Add(new SkippedTestNodeStateProperty(args.Message.Reason));
5863
PublishTestUpdate(testNode);
64+
StopIfCancellationIsRequested(args);
5965
}
6066

6167
private void OnTestClassCleanupFailure(MessageHandlerArgs<ITestClassCleanupFailure> args)
62-
=> OnCleanupFailure($"Test Class Cleanup Failure ({args.Message.TestClass.Class.Name})", args.Message);
68+
{
69+
OnCleanupFailure($"Test Class Cleanup Failure ({args.Message.TestClass.Class.Name})", args.Message);
70+
StopIfCancellationIsRequested(args);
71+
}
6372

6473
private void OnTestAssemblyCleanupFailure(MessageHandlerArgs<ITestAssemblyCleanupFailure> args)
65-
=> OnCleanupFailure($"Test Assembly Cleanup Failure ({args.Message.TestAssembly.Assembly.Name})", args.Message);
74+
{
75+
OnCleanupFailure($"Test Assembly Cleanup Failure ({args.Message.TestAssembly.Assembly.Name})", args.Message);
76+
StopIfCancellationIsRequested(args);
77+
}
6678

6779
private void OnTestCaseCleanupFailure(MessageHandlerArgs<ITestCaseCleanupFailure> args)
68-
=> OnCleanupFailure($"Test Case Cleanup Failure ({args.Message.TestCase.DisplayName})", args.Message);
80+
{
81+
OnCleanupFailure($"Test Case Cleanup Failure ({args.Message.TestCase.DisplayName})", args.Message);
82+
StopIfCancellationIsRequested(args);
83+
}
6984

7085
private void OnTestMethodCleanupFailure(MessageHandlerArgs<ITestMethodCleanupFailure> args)
71-
=> OnCleanupFailure($"Test Method Cleanup Failure ({args.Message.TestMethod.TestClass.Class.Name}.{args.Message.TestMethod.Method.Name})", args.Message);
86+
{
87+
OnCleanupFailure($"Test Method Cleanup Failure ({args.Message.TestMethod.TestClass.Class.Name}.{args.Message.TestMethod.Method.Name})", args.Message);
88+
StopIfCancellationIsRequested(args);
89+
}
7290

7391
private void OnTestCollectionCleanupFailure(MessageHandlerArgs<ITestCollectionCleanupFailure> args)
74-
=> OnCleanupFailure($"Test Collection Cleanup Failure ({args.Message.TestCollection.DisplayName})", args.Message);
92+
{
93+
OnCleanupFailure($"Test Collection Cleanup Failure ({args.Message.TestCollection.DisplayName})", args.Message);
94+
StopIfCancellationIsRequested(args);
95+
}
7596

7697
private void OnTestCleanupFailure(MessageHandlerArgs<ITestCleanupFailure> args)
77-
=> OnCleanupFailure($"Test Cleanup Failure ({args.Message.Test.DisplayName})", args.Message);
98+
{
99+
OnCleanupFailure($"Test Cleanup Failure ({args.Message.Test.DisplayName})", args.Message);
100+
StopIfCancellationIsRequested(args);
101+
}
78102

79103
private void OnCleanupFailure<T>(string failureName, T message) where T : IFailureInformation, IExecutionMessage
80104
{
@@ -156,4 +180,12 @@ private TestNode CreateTestNode(ITestCaseMessage testMessage)
156180

157181
return testNode;
158182
}
183+
184+
private void StopIfCancellationIsRequested(MessageHandlerArgs args)
185+
{
186+
if (_executeRequestContext.CancellationToken.IsCancellationRequested)
187+
{
188+
args.Stop();
189+
}
190+
}
159191
}

0 commit comments

Comments
 (0)