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

Ensure that fixtures also support sta threading #2769

Merged
merged 4 commits into from
Apr 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/Adapter/MSTest.TestAdapter/Helpers/MethodRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;

Expand All @@ -15,6 +16,13 @@ internal static class MethodRunner
internal static TestFailedException? RunWithTimeoutAndCancellation(
Action action, CancellationTokenSource cancellationTokenSource, int? timeout, MethodInfo methodInfo,
string methodCancelledMessageFormat, string methodTimedOutMessageFormat)
=> RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Thread.CurrentThread.GetApartmentState() == ApartmentState.STA
? RunWithTimeoutAndCancellationWithSTAThread(action, cancellationTokenSource, timeout, methodInfo, methodCancelledMessageFormat, methodTimedOutMessageFormat)
: RunWithTimeoutAndCancellationWithThreadPool(action, cancellationTokenSource, timeout, methodInfo, methodCancelledMessageFormat, methodTimedOutMessageFormat);

private static TestFailedException? RunWithTimeoutAndCancellationWithThreadPool(
Action action, CancellationTokenSource cancellationTokenSource, int? timeout, MethodInfo methodInfo,
string methodCancelledMessageFormat, string methodTimedOutMessageFormat)
{
Exception? realException = null;
Task? executionTask = null;
Expand Down Expand Up @@ -83,6 +91,91 @@ internal static class MethodRunner
throw realException;
}

throw;
}
}

#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
private static TestFailedException? RunWithTimeoutAndCancellationWithSTAThread(
Action action, CancellationTokenSource cancellationTokenSource, int? timeout, MethodInfo methodInfo,
string methodCancelledMessageFormat, string methodTimedOutMessageFormat)
{
Exception? realException = null;
Thread executionThread = new(new ThreadStart(() =>
{
try
{
action();
}
catch (Exception ex)
{
realException = ex;
throw;
}
}))
{
IsBackground = true,
Name = "MSTest Fixture Execution Thread",
};

executionThread.SetApartmentState(ApartmentState.STA);
executionThread.Start();

try
{
// Creates a Task<bool> that represents the results of the execution thread.
var executionTask = Task.Run(
() =>
{
if (timeout.HasValue)
{
return executionThread.Join(timeout.Value);
}

executionThread.Join();
return true;
},
cancellationTokenSource.Token);
executionTask.Wait(cancellationTokenSource.Token);

// If the execution thread completes before the timeout, the task will return true, otherwise false.
if (executionTask.Result)
{
return realException is not null
? throw realException
: null;
}

// Timed out. For cancellation, either OCE or AggregateException with TCE will be thrown.
return new(
UnitTestOutcome.Timeout,
string.Format(
CultureInfo.InvariantCulture,
methodTimedOutMessageFormat,
methodInfo.DeclaringType!.FullName,
methodInfo.Name));
}
catch (Exception ex) when

// This exception occurs when the cancellation happens before the task is actually started.
((ex is TaskCanceledException tce && tce.CancellationToken == cancellationTokenSource.Token)
|| (ex is OperationCanceledException oce && oce.CancellationToken == cancellationTokenSource.Token)
|| (ex is AggregateException aggregateEx && aggregateEx.InnerExceptions.OfType<TaskCanceledException>().Any()))
{
return new(
UnitTestOutcome.Timeout,
string.Format(CultureInfo.InvariantCulture, methodCancelledMessageFormat, methodInfo.DeclaringType!.FullName, methodInfo.Name));
}
catch (Exception)
{
// We throw the real exception to have the original stack trace to elaborate up the chain.
if (realException is not null)
{
throw realException;
}

throw;
}
}
Expand Down