Skip to content

Commit

Permalink
Test a couple of fixes for apartment state issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kouvel committed Aug 17, 2018
1 parent 7ca2f1c commit 9fe6711
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;

namespace DefaultApartmentStateMain
{
internal static class DefaultApartmentStateMain
{
private const int Success = 0;
private const int SuccessOnUnix = 2;
private const int Failure = 1;

private static Thread s_mainThread;

static int Main(string[] args)
{
string testName = args[0];
s_mainThread = Thread.CurrentThread;

switch (testName)
{
case "GetApartmentStateTest":
return GetApartmentStateTest();
case "SetApartmentStateTest":
return SetApartmentStateTest();
default:
return Failure;
}
}

private static int GetApartmentStateTest()
{
if (s_mainThread.GetApartmentState() == ApartmentState.MTA)
{
s_mainThread.SetApartmentState(ApartmentState.MTA);
return Success;
}
return SuccessOnUnix;
}

private static int SetApartmentStateTest()
{
try
{
s_mainThread.SetApartmentState(ApartmentState.STA);
}
catch (InvalidOperationException)
{
return Success;
}
catch (PlatformNotSupportedException)
{
return SuccessOnUnix;
}
return Failure;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<ProjectGuid>{32432E07-5CA4-41F3-9855-22AB1F1E69B3}</ProjectGuid>
<Configurations>netstandard-Debug;netstandard-Release</Configurations>
</PropertyGroup>
<ItemGroup>
<Compile Include="DefaultApartmentStateMain.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="DefaultApartmentStateMain.runtimeconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="DefaultApartmentStateMain.exe.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<developmentMode developerInstallation="true" />
</runtime>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"runtimeOptions": {
"framework": {
"name": "Microsoft.NETCore.App",
"version": "9.9.9"
}
}
}
70 changes: 39 additions & 31 deletions src/System.Threading.Thread/tests/MTAMain/MTAMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,54 @@ namespace MTAMain
{
internal static class MTAMain
{
private const int Success = 0;
private const int SuccessOnUnix = 2;
private const int Failure = 1;

private static Thread s_mainThread;

[MTAThread]
static int Main(string[] args)
{
const int Success = 0;
const int SuccessOnUnix = 2;
const int Failure = 1;
string testName = args[0];
s_mainThread = Thread.CurrentThread;

string mode = args[0];
int retValue = Failure;
Thread curThread = Thread.CurrentThread;

if (mode == "GetApartmentState")
switch (testName)
{
if (curThread.GetApartmentState() == ApartmentState.MTA)
{
curThread.SetApartmentState(ApartmentState.MTA);
retValue = Success;
}
else
{
retValue = SuccessOnUnix;
}
case "GetApartmentStateTest":
return GetApartmentStateTest();
case "SetApartmentStateTest":
return SetApartmentStateTest();
default:
return Failure;
}
else
}

private static int GetApartmentStateTest()
{
if (s_mainThread.GetApartmentState() == ApartmentState.MTA)
{
try
{
curThread.SetApartmentState(ApartmentState.STA);
}
catch (InvalidOperationException)
{
retValue = Success;
}
catch (PlatformNotSupportedException)
{
retValue = SuccessOnUnix;
}
s_mainThread.SetApartmentState(ApartmentState.MTA);
return Success;
}
return SuccessOnUnix;
}

return retValue;
private static int SetApartmentStateTest()
{
try
{
s_mainThread.SetApartmentState(ApartmentState.STA);
}
catch (InvalidOperationException)
{
return Success;
}
catch (PlatformNotSupportedException)
{
return SuccessOnUnix;
}
return Failure;
}
}
}
70 changes: 39 additions & 31 deletions src/System.Threading.Thread/tests/STAMain/STAMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,54 @@ namespace STAMain
{
internal static class STAMain
{
private const int Success = 0;
private const int SuccessOnUnix = 2;
private const int Failure = 1;

private static Thread s_mainThread;

[STAThread]
static int Main(string[] args)
{
const int Success = 0;
const int SuccessOnUnix = 2;
const int Failure = 1;
string testName = args[0];
s_mainThread = Thread.CurrentThread;

string mode = args[0];
int retValue = Failure;
Thread curThread = Thread.CurrentThread;

if (mode == "GetApartmentState")
switch (testName)
{
if (curThread.GetApartmentState() == ApartmentState.STA)
{
curThread.SetApartmentState(ApartmentState.STA);
retValue = Success;
}
else
{
retValue = SuccessOnUnix;
}
case "GetApartmentStateTest":
return GetApartmentStateTest();
case "SetApartmentStateTest":
return SetApartmentStateTest();
default:
return Failure;
}
else
}

private static int GetApartmentStateTest()
{
if (s_mainThread.GetApartmentState() == ApartmentState.STA)
{
try
{
curThread.SetApartmentState(ApartmentState.MTA);
}
catch (InvalidOperationException)
{
retValue = Success;
}
catch (PlatformNotSupportedException)
{
retValue = SuccessOnUnix;
}
s_mainThread.SetApartmentState(ApartmentState.STA);
return Success;
}
return SuccessOnUnix;
}

return retValue;
private static int SetApartmentStateTest()
{
try
{
s_mainThread.SetApartmentState(ApartmentState.MTA);
}
catch (InvalidOperationException)
{
return Success;
}
catch (PlatformNotSupportedException)
{
return SuccessOnUnix;
}
return Failure;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<ProjectReference Include="MTAMain\MTAMain.csproj">
<Name>MTAMain</Name>
</ProjectReference>
<ProjectReference Include="DefaultApartmentStateMain\DefaultApartmentStateMain.csproj">
<Name>DefaultApartmentStateMain</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
18 changes: 10 additions & 8 deletions src/System.Threading.Thread/tests/ThreadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,25 @@ private static IEnumerable<object[]> ApartmentStateTest_MemberData()
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[InlineData("STAMain.exe", "GetApartmentState")]
[InlineData("STAMain.exe", "SetApartmentState")]
[InlineData("MTAMain.exe", "GetApartmentState")]
[InlineData("MTAMain.exe", "SetApartmentState")]
[InlineData("STAMain.exe", "GetApartmentStateTest")]
[InlineData("STAMain.exe", "SetApartmentStateTest")]
[InlineData("MTAMain.exe", "GetApartmentStateTest")]
[InlineData("MTAMain.exe", "SetApartmentStateTest")]
[InlineData("DefaultApartmentStateMain.exe", "GetApartmentStateTest")]
[InlineData("DefaultApartmentStateMain.exe", "SetApartmentStateTest")]
[ActiveIssue(20766, TargetFrameworkMonikers.Uap)]
public static void ApartmentState_AttributePresent(string AppName, string mode)
public static void ApartmentState_AttributePresent(string appName, string testName)
{
var psi = new ProcessStartInfo();
if (PlatformDetection.IsFullFramework || PlatformDetection.IsNetNative)
{
psi.FileName = AppName;
psi.Arguments = $"{mode}";
psi.FileName = appName;
psi.Arguments = $"{testName}";
}
else
{
psi.FileName = DummyClass.HostRunnerTest;
psi.Arguments = $"{AppName} {mode}";
psi.Arguments = $"{appName} {testName}";
}
using (Process p = Process.Start(psi))
{
Expand Down

0 comments on commit 9fe6711

Please sign in to comment.