Skip to content

Commit

Permalink
Bugfix for CancellationTokenSource (#593)
Browse files Browse the repository at this point in the history
* Bugfix for CancellationTokenSource

Added initialization that was missing in the NetCoreTestContext and added a test to cover the net core scenario as well.
  • Loading branch information
cltshivash authored Apr 8, 2019
1 parent 249cc70 commit a208ab7
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 8 deletions.
27 changes: 27 additions & 0 deletions TestFx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeoutTestProject", "test\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeploymentTestProjectNetCore", "test\E2ETests\TestAssets\DeploymentTestProjectNetCore\DeploymentTestProjectNetCore.csproj", "{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeoutTestProjectNetCore", "test\E2ETests\TestAssets\TimeoutTestProjectNetCore\TimeoutTestProjectNetCore.csproj", "{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\TestFramework\Extension.Shared\Extension.Shared.projitems*{272ca5e1-8e81-4825-9e47-86cce02f700d}*SharedItemsImports = 13
Expand Down Expand Up @@ -1116,6 +1118,30 @@ Global
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x64.Build.0 = Release|Any CPU
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x86.ActiveCfg = Release|Any CPU
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE}.Release|x86.Build.0 = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|Any CPU.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|ARM.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|ARM.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x64.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|ARM.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|ARM.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x64.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x64.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x86.ActiveCfg = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Debug|x86.Build.0 = Debug|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|Any CPU.Build.0 = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|ARM.ActiveCfg = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|ARM.Build.0 = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x64.ActiveCfg = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x64.Build.0 = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x86.ActiveCfg = Release|Any CPU
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1178,6 +1204,7 @@ Global
{7FB80AAB-7123-4416-B6CD-8D3D69AA83F1} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
{4F0B2ACF-1341-42AF-918C-669A6D5CEA2B} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
{26F0B8EF-92D4-4A23-ACB4-D1B662F0EEBE} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
{ED27A844-6870-4FE6-8FEF-3ABDD1ED6564} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;

using System.Threading;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel;

Expand Down Expand Up @@ -67,6 +67,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter writer, ID
this.stringWriter = writer;
this.InitializeProperties();
this.testResultFiles = new List<string>();
this.CancellationTokenSource = new CancellationTokenSource();
}

#region TestContext impl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ public class ThreadOperations : IThreadOperations
public bool Execute(Action action, int timeout, CancellationToken cancelToken)
{
var executionTask = Task.Factory.StartNew(action);
if (executionTask.Wait(timeout, cancelToken))
try
{
return true;
if (executionTask.Wait(timeout, cancelToken))
{
return true;
}
else
{
// Timed out.
return false;
}
}
else
catch (OperationCanceledException)
{
// Timed out.
// Task execution cancelled.
return false;
}
}
Expand Down
20 changes: 17 additions & 3 deletions test/E2ETests/Smoke.E2E.Tests/TimeoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,39 @@ namespace MSTestAdapter.Smoke.E2ETests
public class TimeoutTests : CLITestBase
{
private const string TimeoutTestAssembly = "TimeoutTestProject.dll";
private const string TimeoutTestAssemblyNetCore = "netcoreapp1.1\\TimeoutTestProjectNetCore.dll";
private const int TestMethodWaitTimeInMs = 6000;
private const int OverheadTimeInMs = 2500;
private const string TimeoutFileToValidateNetCore = "netcoreapp1.1\\TimeoutTestOutputNetCore.txt";
private const string TimeoutFileToValidate = "TimeoutTestOutput.txt";

[TestMethod]
public void ValidateTimeoutTests()
{
this.InvokeVsTestForExecution(new string[] { TimeoutTestAssembly });
this.Validate(TimeoutTestAssembly, TimeoutFileToValidate);
}

[TestMethod]
public void ValidateTimeoutTestsNetCore()
{
this.Validate(TimeoutTestAssemblyNetCore, TimeoutFileToValidateNetCore);
}

private void Validate(string testAssembly, string fileToValidate)
{
this.InvokeVsTestForExecution(new string[] { testAssembly });

this.ValidateTestRunTime(TestMethodWaitTimeInMs + OverheadTimeInMs);

this.ValidateFailedTestsCount(2);

this.ValidateFailedTestsContain(
TimeoutTestAssembly,
testAssembly,
false,
"TimeoutTestProject.TerimnateLongRunningTasksUsingTokenTestClass.TerimnateLongRunningTasksUsingToken",
"TimeoutTestProject.SelfTerminatingTestClass.SelfTerminatingTestMethod");

Assert.IsTrue(File.Exists(this.GetAssetFullPath("TimeoutTestOutput.txt")), "Unable to locate the TimeoutTestOutput.txt file");
Assert.IsTrue(File.Exists(this.GetAssetFullPath(fileToValidate)), "Unable to locate the TimeoutTestOutput.txt file");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TimeoutTestProject
{
[TestClass]
public class SelfTerminatingTestClass
{
public TestContext TestContext { get; set; }

[TestMethod]
[Timeout(60000)]
public void SelfTerminatingTestMethod()
{
TestContext.CancellationTokenSource.Cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TimeoutTestProject
{
[TestClass]
public class TerimnateLongRunningTasksUsingTokenTestClass
{
public TestContext TestContext { get; set; }

[TestMethod]
[Timeout(5000)]
public void TerimnateLongRunningTasksUsingToken()
{
var longTask = new Thread(ExecuteLong);
longTask.Start();
longTask.Join();
}

private void ExecuteLong()
{
try
{
File.Delete("TimeoutTestOutputNetCore.txt");
Task.Delay(100000).Wait(TestContext.CancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
File.WriteAllText("TimeoutTestOutputNetCore.txt", "Written from long running thread post termination");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TestFxRoot Condition="$(TestFxRoot) == ''">..\..\..\..\</TestFxRoot>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<FrameworkIdentifier>NetCore</FrameworkIdentifier>
<IsPackable>false</IsPackable>
<!--<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>-->
<OutputPath>$(TestFxRoot)artifacts\TestAssets\</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(TestFxRoot)\src\TestFramework\Extension.Core\Extension.Core.csproj" />
<ProjectReference Include="$(TestFxRoot)\src\TestFramework\MSTest.Core\MSTest.Core.csproj" />
</ItemGroup>
</Project>

0 comments on commit a208ab7

Please sign in to comment.