diff options
author | Keting Yang <ketyang@microsoft.com> | 2019-08-29 20:37:26 +0300 |
---|---|---|
committer | Keting Yang <ketyang@microsoft.com> | 2019-08-29 21:22:41 +0300 |
commit | 6e7acc339a25faf7a20dce0599aaf18cd26448a8 (patch) | |
tree | deff6a7a5ff143eaf790f1c299c2759fbcedddac /main/src/addins/MonoDevelop.UnitTesting | |
parent | 2592c46cd590ae23dc2fc87d817558bfd468ca60 (diff) |
Threading
Diffstat (limited to 'main/src/addins/MonoDevelop.UnitTesting')
2 files changed, 39 insertions, 6 deletions
diff --git a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestTestClass.cs b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestTestClass.cs index d7a81fff23..8bdd518b48 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestTestClass.cs +++ b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestTestClass.cs @@ -26,6 +26,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using MonoDevelop.Core.Execution; using MonoDevelop.Ide; @@ -40,16 +42,20 @@ namespace MonoDevelop.UnitTesting.VsTest public Project Project { get; private set; } IVsTestTestRunner testRunner; SourceCodeLocation sourceCodeLocation; + CancellationTokenSource cts; public VsTestTestClass (IVsTestTestRunner testRunner, Project project, VsTestUnitTest vsTestUnit) : base (vsTestUnit.FixtureTypeName) { - this.Project = project; + Project = project; this.testRunner = testRunner; FixtureTypeName = vsTestUnit.FixtureTypeName; TestSourceCodeDocumentId = string.IsNullOrEmpty (vsTestUnit.FixtureTypeNamespace) ? FixtureTypeName : vsTestUnit.FixtureTypeNamespace + "." + FixtureTypeName; - - IdeApp.TypeSystemService.GetCompilationAsync (Project).ContinueWith ((t) => { + cts = new CancellationTokenSource (); + var token = cts.Token; + IdeApp.TypeSystemService.GetCompilationAsync (Project, token).ContinueWith ((t) => { + if (token.IsCancellationRequested) + return; var className = TestSourceCodeDocumentId; var compilation = t.Result; if (compilation == null) @@ -62,7 +68,7 @@ namespace MonoDevelop.UnitTesting.VsTest return; var line = source.GetLineSpan (); sourceCodeLocation = new SourceCodeLocation (source.SourceTree.FilePath, line.StartLinePosition.Line, line.StartLinePosition.Character); - }).Ignore(); + }, token, TaskContinuationOptions.NotOnFaulted, TaskScheduler.Default).Ignore (); } protected override UnitTestResult OnRun (TestContext testContext) @@ -94,5 +100,15 @@ namespace MonoDevelop.UnitTesting.VsTest } } + public override void Dispose () + { + if (cts != null) { + cts.Cancel (); + cts.Dispose (); + cts = null; + } + base.Dispose (); + } + } } diff --git a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestUnitTest.cs b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestUnitTest.cs index 899b262ed6..11105abdac 100644 --- a/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestUnitTest.cs +++ b/main/src/addins/MonoDevelop.UnitTesting/MonoDevelop.UnitTesting.VsTest/VsTestUnitTest.cs @@ -26,6 +26,8 @@ using System; using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using MonoDevelop.Core.Execution; using MonoDevelop.Projects; @@ -44,6 +46,7 @@ namespace MonoDevelop.UnitTesting.VsTest IVsTestTestRunner testRunner; string name; SourceCodeLocation sourceCodeLocation; + CancellationTokenSource cts; protected VsTestUnitTest(string displayName) : base (displayName) { } @@ -62,10 +65,14 @@ namespace MonoDevelop.UnitTesting.VsTest { TestId = test.Id.ToString (); TestSourceCodeDocumentId = test.FullyQualifiedName; + cts = new CancellationTokenSource (); + var token = cts.Token; if (!string.IsNullOrEmpty (test.CodeFilePath)) sourceCodeLocation = new SourceCodeLocation (test.CodeFilePath, test.LineNumber, 0); else { - IdeApp.TypeSystemService.GetCompilationAsync (Project).ContinueWith ((t) => { + IdeApp.TypeSystemService.GetCompilationAsync (Project, token).ContinueWith ((t) => { + if (token.IsCancellationRequested) + return; var dotIndex = test.FullyQualifiedName.LastIndexOf (".", StringComparison.Ordinal); var className = test.FullyQualifiedName.Remove (dotIndex); var methodName = test.FullyQualifiedName.Substring (dotIndex + 1); @@ -91,7 +98,7 @@ namespace MonoDevelop.UnitTesting.VsTest return; var line = source.GetLineSpan (); sourceCodeLocation = new SourceCodeLocation (source.SourceTree.FilePath, line.StartLinePosition.Line, line.StartLinePosition.Character); - }).Ignore (); + }, token, TaskContinuationOptions.NotOnFaulted, TaskScheduler.Default).Ignore (); } int index = test.FullyQualifiedName.LastIndexOf ('.'); if (index > 0) { @@ -155,5 +162,15 @@ namespace MonoDevelop.UnitTesting.VsTest { yield return test; } + + public override void Dispose () + { + if (cts != null) { + cts.Cancel (); + cts.Dispose (); + cts = null; + } + base.Dispose (); + } } } |