-
Notifications
You must be signed in to change notification settings - Fork 344
Add test run serialization feature #4126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarcoRossignoli
merged 10 commits into
microsoft:main
from
MarcoRossignoli:pertestcoverage
Nov 28, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
86e329a
add test run serialization feature
5de5500
address PR feedback
e9ad115
add comment
5755ab0
Update src/Microsoft.TestPlatform.Common/ExtensionDecorators/Serializ…
MarcoRossignoli c0b3ac1
Update src/Microsoft.TestPlatform.Common/ExtensionDecorators/Serializ…
MarcoRossignoli 931aa49
Update src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTe…
MarcoRossignoli 51a5c09
address PR feedback
b06cdfd
address PR feedback
acd2360
address PR feedback
f5e96a8
address PR feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Microsoft.TestPlatform.Common/ExtensionDecorators/ExtensionDecoratorFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
| using Microsoft.VisualStudio.TestPlatform.Utilities; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionDecorators; | ||
| internal class ExtensionDecoratorFactory | ||
| { | ||
| private readonly IFeatureFlag _featureFlag; | ||
|
|
||
| public ExtensionDecoratorFactory(IFeatureFlag featureFlag) | ||
| { | ||
| _featureFlag = featureFlag; | ||
| } | ||
|
|
||
| public ITestExecutor Decorate(ITestExecutor originalTestExecutor) | ||
| { | ||
| return _featureFlag.IsSet(FeatureFlag.DISABLE_SERIALTESTRUN_DECORATOR) | ||
| ? originalTestExecutor | ||
| : new SerialTestRunDecorator(originalTestExecutor); | ||
| } | ||
| } |
137 changes: 137 additions & 0 deletions
137
src/Microsoft.TestPlatform.Common/ExtensionDecorators/SerialTestRunDecorator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Xml.Linq; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionDecorators; | ||
|
|
||
| internal class SerialTestRunDecorator : ITestExecutor, ITestExecutor2, IDisposable | ||
| { | ||
| private readonly SemaphoreSlim _runSequentialEvent = new(1); | ||
|
|
||
| public ITestExecutor OriginalTestExecutor { get; } | ||
|
|
||
| public SerialTestRunDecorator(ITestExecutor originalTestExecutor) | ||
| { | ||
| OriginalTestExecutor = originalTestExecutor; | ||
| } | ||
|
|
||
| public void RunTests(IEnumerable<TestCase>? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle) | ||
| { | ||
| if (IsSerialTestRunEnabled(runContext)) | ||
| { | ||
| EqtTrace.Info("SerializeTestRunDecorator.RunTests: Test cases will run sequentially"); | ||
| if (tests is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (TestCase testToRun in tests) | ||
| { | ||
| _runSequentialEvent.Wait(); | ||
| OriginalTestExecutor.RunTests(new List<TestCase> { testToRun }, runContext, new SerializeTestRunDecoratorFrameworkHandle(frameworkHandle!, _runSequentialEvent)); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| OriginalTestExecutor.RunTests(tests, runContext, frameworkHandle); | ||
| } | ||
| } | ||
|
|
||
| public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle) | ||
| { | ||
| if (IsSerialTestRunEnabled(runContext)) | ||
| { | ||
| EqtTrace.Error("<ForceOneTestAtTimePerTestHost>true</ForceOneTestAtTimePerTestHost> is not supported for sources test run."); | ||
| frameworkHandle?.SendMessage(TestMessageLevel.Error, Resources.Resources.SerialTestRunInvalidScenario); | ||
| } | ||
| else | ||
| { | ||
| OriginalTestExecutor.RunTests(sources, runContext, frameworkHandle); | ||
| } | ||
| } | ||
|
|
||
| public bool ShouldAttachToTestHost(IEnumerable<string>? sources, IRunContext runContext) | ||
| { | ||
| if (OriginalTestExecutor is ITestExecutor2 executor) | ||
| { | ||
| return executor.ShouldAttachToTestHost(sources, runContext); | ||
| } | ||
|
|
||
| // If the adapter doesn't implement the new test executor interface we should attach to | ||
| // the default test host by default to preserve old behavior. | ||
| return true; | ||
| } | ||
|
|
||
| public bool ShouldAttachToTestHost(IEnumerable<TestCase>? tests, IRunContext runContext) | ||
| { | ||
| if (OriginalTestExecutor is ITestExecutor2 executor) | ||
| { | ||
| return executor.ShouldAttachToTestHost(tests, runContext); | ||
| } | ||
|
|
||
| // If the adapter doesn't implement the new test executor interface we should attach to | ||
| // the default test host by default to preserve old behavior. | ||
| return true; | ||
| } | ||
|
|
||
| public void Cancel() | ||
| => OriginalTestExecutor.Cancel(); | ||
|
|
||
| private static bool IsSerialTestRunEnabled(IRunContext? runContext) | ||
| { | ||
| if (runContext is null || runContext.RunSettings is null || runContext.RunSettings.SettingsXml is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| XElement runSettings = XElement.Parse(runContext.RunSettings.SettingsXml); | ||
| XElement? serializeTestRun = runSettings.Element("RunConfiguration")?.Element("ForceOneTestAtTimePerTestHost"); | ||
| return serializeTestRun is not null && bool.TryParse(serializeTestRun.Value, out bool enabled) && enabled; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| => _runSequentialEvent.Dispose(); | ||
| } | ||
|
|
||
| internal class SerializeTestRunDecoratorFrameworkHandle : IFrameworkHandle | ||
| { | ||
| private readonly IFrameworkHandle _frameworkHandle; | ||
| private readonly SemaphoreSlim _testEnd; | ||
|
|
||
| public SerializeTestRunDecoratorFrameworkHandle(IFrameworkHandle frameworkHandle, SemaphoreSlim testEnd) | ||
| { | ||
| _frameworkHandle = frameworkHandle; | ||
| _testEnd = testEnd; | ||
| } | ||
|
|
||
| public bool EnableShutdownAfterTestRun { get => _frameworkHandle.EnableShutdownAfterTestRun; set => _frameworkHandle.EnableShutdownAfterTestRun = value; } | ||
|
|
||
| public int LaunchProcessWithDebuggerAttached(string filePath, string? workingDirectory, string? arguments, IDictionary<string, string?>? environmentVariables) | ||
| => _frameworkHandle.LaunchProcessWithDebuggerAttached(filePath, workingDirectory, arguments, environmentVariables); | ||
|
|
||
| public void RecordAttachments(IList<AttachmentSet> attachmentSets) | ||
| => _frameworkHandle.RecordAttachments(attachmentSets); | ||
|
|
||
| public void RecordEnd(TestCase testCase, TestOutcome outcome) | ||
| { | ||
| _frameworkHandle.RecordEnd(testCase, outcome); | ||
| _testEnd.Release(); | ||
| } | ||
|
|
||
| public void RecordResult(TestResult testResult) | ||
| => _frameworkHandle.RecordResult(testResult); | ||
|
|
||
| public void RecordStart(TestCase testCase) | ||
| => _frameworkHandle.RecordStart(testCase); | ||
|
|
||
| public void SendMessage(TestMessageLevel testMessageLevel, string message) | ||
| => _frameworkHandle.SendMessage(testMessageLevel, message); | ||
| } |
46 changes: 46 additions & 0 deletions
46
...icrosoft.TestPlatform.Common/ExtensionDecorators/SerialTestRunDecoratorFrameworkHandle.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; | ||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionDecorators; | ||
|
|
||
| internal class SerialTestRunDecoratorFrameworkHandle : IFrameworkHandle | ||
| { | ||
| private readonly IFrameworkHandle _frameworkHandle; | ||
| private readonly SemaphoreSlim _testEnd; | ||
|
|
||
| public SerialTestRunDecoratorFrameworkHandle(IFrameworkHandle frameworkHandle, SemaphoreSlim testEnd) | ||
| { | ||
| _frameworkHandle = frameworkHandle; | ||
| _testEnd = testEnd; | ||
| } | ||
|
|
||
| public bool EnableShutdownAfterTestRun { get => _frameworkHandle.EnableShutdownAfterTestRun; set => _frameworkHandle.EnableShutdownAfterTestRun = value; } | ||
|
|
||
| public int LaunchProcessWithDebuggerAttached(string filePath, string? workingDirectory, string? arguments, IDictionary<string, string?>? environmentVariables) | ||
| => _frameworkHandle.LaunchProcessWithDebuggerAttached(filePath, workingDirectory, arguments, environmentVariables); | ||
|
|
||
| public void RecordAttachments(IList<AttachmentSet> attachmentSets) | ||
| => _frameworkHandle.RecordAttachments(attachmentSets); | ||
|
|
||
| public void RecordEnd(TestCase testCase, TestOutcome outcome) | ||
| { | ||
| _frameworkHandle.RecordEnd(testCase, outcome); | ||
| _testEnd.Release(); | ||
| } | ||
|
|
||
| public void RecordResult(TestResult testResult) | ||
| => _frameworkHandle.RecordResult(testResult); | ||
|
|
||
| public void RecordStart(TestCase testCase) | ||
| => _frameworkHandle.RecordStart(testCase); | ||
|
|
||
| public void SendMessage(TestMessageLevel testMessageLevel, string message) | ||
| => _frameworkHandle.SendMessage(testMessageLevel, message); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
src/Microsoft.TestPlatform.Common/Resources/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.