Skip to content

Commit a208ab7

Browse files
authored
Bugfix for CancellationTokenSource (#593)
* Bugfix for CancellationTokenSource Added initialization that was missing in the NetCoreTestContext and added a test to cover the net core scenario as well.
1 parent 249cc70 commit a208ab7

File tree

7 files changed

+141
-8
lines changed

7 files changed

+141
-8
lines changed

TestFx.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeoutTestProject", "test\
184184
EndProject
185185
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeploymentTestProjectNetCore", "test\E2ETests\TestAssets\DeploymentTestProjectNetCore\DeploymentTestProjectNetCore.csproj", "{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}"
186186
EndProject
187+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeoutTestProjectNetCore", "test\E2ETests\TestAssets\TimeoutTestProjectNetCore\TimeoutTestProjectNetCore.csproj", "{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}"
188+
EndProject
187189
Global
188190
GlobalSection(SharedMSBuildProjectFiles) = preSolution
189191
src\TestFramework\Extension.Shared\Extension.Shared.projitems*{272ca5e1-8e81-4825-9e47-86cce02f700d}*SharedItemsImports = 13
@@ -1116,6 +1118,30 @@ Global
11161118
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x64.Build.0 = Release|Any CPU
11171119
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x86.ActiveCfg = Release|Any CPU
11181120
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x86.Build.0 = Release|Any CPU
1121+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|Any CPU.ActiveCfg = Debug|Any CPU
1122+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|Any CPU.Build.0 = Debug|Any CPU
1123+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|ARM.ActiveCfg = Debug|Any CPU
1124+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|ARM.Build.0 = Debug|Any CPU
1125+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x64.ActiveCfg = Debug|Any CPU
1126+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
1127+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
1128+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
1129+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1130+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|Any CPU.Build.0 = Debug|Any CPU
1131+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|ARM.ActiveCfg = Debug|Any CPU
1132+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|ARM.Build.0 = Debug|Any CPU
1133+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x64.ActiveCfg = Debug|Any CPU
1134+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x64.Build.0 = Debug|Any CPU
1135+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x86.ActiveCfg = Debug|Any CPU
1136+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x86.Build.0 = Debug|Any CPU
1137+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|Any CPU.ActiveCfg = Release|Any CPU
1138+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|Any CPU.Build.0 = Release|Any CPU
1139+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|ARM.ActiveCfg = Release|Any CPU
1140+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|ARM.Build.0 = Release|Any CPU
1141+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x64.ActiveCfg = Release|Any CPU
1142+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x64.Build.0 = Release|Any CPU
1143+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x86.ActiveCfg = Release|Any CPU
1144+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x86.Build.0 = Release|Any CPU
11191145
EndGlobalSection
11201146
GlobalSection(SolutionProperties) = preSolution
11211147
HideSolutionNode = FALSE
@@ -1178,6 +1204,7 @@ Global
11781204
{7FB80AAB-7123-4416-B6CD-8D3D69AA83F1} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
11791205
{4F0B2ACF-1341-42AF-918C-669A6D5CEA2B} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
11801206
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
1207+
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
11811208
EndGlobalSection
11821209
GlobalSection(ExtensibilityGlobals) = postSolution
11831210
SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9}

src/Adapter/PlatformServices.NetCore/Services/NetCoreTestContextImplementation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
1010
using System.Diagnostics.CodeAnalysis;
1111
using System.Globalization;
1212
using System.IO;
13-
13+
using System.Threading;
1414
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
1515
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel;
1616

@@ -67,6 +67,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID
6767
this.stringWriter = writer;
6868
this.InitializeProperties();
6969
this.testResultFiles = new List<string>();
70+
this.CancellationTokenSource = new CancellationTokenSource();
7071
}
7172

7273
#region TestContext impl

src/Adapter/PlatformServices.Shared/netstandard1.0/Services/ns10ThreadOperations.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ public class ThreadOperations : IThreadOperations
2626
public bool Execute(Action action, int timeout, CancellationToken cancelToken)
2727
{
2828
var executionTask = Task.Factory.StartNew(action);
29-
if (executionTask.Wait(timeout, cancelToken))
29+
try
3030
{
31-
return true;
31+
if (executionTask.Wait(timeout, cancelToken))
32+
{
33+
return true;
34+
}
35+
else
36+
{
37+
// Timed out.
38+
return false;
39+
}
3240
}
33-
else
41+
catch (OperationCanceledException)
3442
{
35-
// Timed out.
43+
// Task execution cancelled.
3644
return false;
3745
}
3846
}

test/E2ETests/Smoke.E2E.Tests/TimeoutTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,39 @@ namespace MSTestAdapter.Smoke.E2ETests
1111
public class TimeoutTests : CLITestBase
1212
{
1313
private const string TimeoutTestAssembly = "TimeoutTestProject.dll";
14+
private const string TimeoutTestAssemblyNetCore = "netcoreapp1.1\\TimeoutTestProjectNetCore.dll";
1415
private const int TestMethodWaitTimeInMs = 6000;
1516
private const int OverheadTimeInMs = 2500;
17+
private const string TimeoutFileToValidateNetCore = "netcoreapp1.1\\TimeoutTestOutputNetCore.txt";
18+
private const string TimeoutFileToValidate = "TimeoutTestOutput.txt";
1619

1720
[TestMethod]
1821
public void ValidateTimeoutTests()
1922
{
20-
this.InvokeVsTestForExecution(new string[] { TimeoutTestAssembly });
23+
this.Validate(TimeoutTestAssembly, TimeoutFileToValidate);
24+
}
25+
26+
[TestMethod]
27+
public void ValidateTimeoutTestsNetCore()
28+
{
29+
this.Validate(TimeoutTestAssemblyNetCore, TimeoutFileToValidateNetCore);
30+
}
31+
32+
private void Validate(string testAssembly, string fileToValidate)
33+
{
34+
this.InvokeVsTestForExecution(new string[] { testAssembly });
2135

2236
this.ValidateTestRunTime(TestMethodWaitTimeInMs + OverheadTimeInMs);
2337

2438
this.ValidateFailedTestsCount(2);
2539

2640
this.ValidateFailedTestsContain(
27-
TimeoutTestAssembly,
41+
testAssembly,
2842
false,
2943
"TimeoutTestProject.TerimnateLongRunningTasksUsingTokenTestClass.TerimnateLongRunningTasksUsingToken",
3044
"TimeoutTestProject.SelfTerminatingTestClass.SelfTerminatingTestMethod");
3145

32-
Assert.IsTrue(File.Exists(this.GetAssetFullPath("TimeoutTestOutput.txt")), "Unable to locate the TimeoutTestOutput.txt file");
46+
Assert.IsTrue(File.Exists(this.GetAssetFullPath(fileToValidate)), "Unable to locate the TimeoutTestOutput.txt file");
3347
}
3448
}
3549
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace TimeoutTestProject
9+
{
10+
[TestClass]
11+
public class SelfTerminatingTestClass
12+
{
13+
public TestContext TestContext { get; set; }
14+
15+
[TestMethod]
16+
[Timeout(60000)]
17+
public void SelfTerminatingTestMethod()
18+
{
19+
TestContext.CancellationTokenSource.Cancel();
20+
}
21+
}
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace TimeoutTestProject
11+
{
12+
[TestClass]
13+
public class TerimnateLongRunningTasksUsingTokenTestClass
14+
{
15+
public TestContext TestContext { get; set; }
16+
17+
[TestMethod]
18+
[Timeout(5000)]
19+
public void TerimnateLongRunningTasksUsingToken()
20+
{
21+
var longTask = new Thread(ExecuteLong);
22+
longTask.Start();
23+
longTask.Join();
24+
}
25+
26+
private void ExecuteLong()
27+
{
28+
try
29+
{
30+
File.Delete("TimeoutTestOutputNetCore.txt");
31+
Task.Delay(100000).Wait(TestContext.CancellationTokenSource.Token);
32+
}
33+
catch (OperationCanceledException)
34+
{
35+
File.WriteAllText("TimeoutTestOutputNetCore.txt", "Written from long running thread post termination");
36+
}
37+
}
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TestFxRoot Condition="$(TestFxRoot) == ''">..\..\..\..\</TestFxRoot>
4+
</PropertyGroup>
5+
6+
<PropertyGroup>
7+
<TargetFramework>netcoreapp1.1</TargetFramework>
8+
<FrameworkIdentifier>NetCore</FrameworkIdentifier>
9+
<IsPackable>false</IsPackable>
10+
<!--<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>-->
11+
<OutputPath>$(TestFxRoot)artifacts\TestAssets\</OutputPath>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="$(TestFxRoot)\src\TestFramework\Extension.Core\Extension.Core.csproj" />
20+
<ProjectReference Include="$(TestFxRoot)\src\TestFramework\MSTest.Core\MSTest.Core.csproj" />
21+
</ItemGroup>
22+
</Project>

0 commit comments

Comments
 (0)