Skip to content

Commit

Permalink
Merge pull request #23 from dotnet-campus/t/lindexi/STA
Browse files Browse the repository at this point in the history
支持设置强行 STA 线程
  • Loading branch information
walterlv authored Aug 9, 2023
2 parents cad253b + cb5eebf commit 1affc9f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/MSTest.Extensions/Contracts/ContractTestConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MSTest.Extensions.Contracts
{
/// <summary>
/// 测试的配置
/// </summary>
public static class ContractTestConfiguration
{
/// <summary>
/// 强制采用 STA 线程执行单元测试
/// </summary>
public static bool MustSTAThread { set; get; }
}
}
32 changes: 29 additions & 3 deletions src/MSTest.Extensions/Core/ContractTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSTest.Extensions.Contracts;

namespace MSTest.Extensions.Core
{
Expand Down Expand Up @@ -106,7 +108,7 @@ internal async Task<TestResult> ExecuteAsync()
TimeSpan duration;
string output;
string error;
Exception exception;
Exception exception = null;

using (var outputWriter = new ThreadSafeStringWriter(CultureInfo.InvariantCulture))
{
Expand All @@ -124,8 +126,32 @@ internal async Task<TestResult> ExecuteAsync()
// Execute the test case.
try
{
await _testCase().ConfigureAwait(false);
exception = null;
if (ContractTestConfiguration.MustSTAThread &&
Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
// 强行要求 STA 且当前运行非 STA 线程的情况,就需要进行切换线程
// 这里是单元测试,就可以无视性能问题哈
var thread = new Thread(() =>
{
try
{
_testCase().Wait();
}
catch (Exception e)
{
// 不能抛到后台线程去
exception = e;
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
else
{
await _testCase().ConfigureAwait(false);
exception = null;
}
}
catch (AggregateException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MSTest.Extensions/MSTest.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/dotnet-campus/CUnit</PackageProjectUrl>
<Copyright>Copyright (c) 2018-2021 dotnet职业技术学院</Copyright>
<Copyright>Copyright (c) 2018-2023 dotnet职业技术学院</Copyright>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Description>MSTestEnhancer helps you to write unit tests without naming any method. You can write method contract descriptions instead of writing confusing test method name when writing unit tests.</Description>
<PackageReleaseNotes>Add some assersion extensions.</PackageReleaseNotes>
Expand Down

0 comments on commit 1affc9f

Please sign in to comment.