Skip to content
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

Add test run serialization feature #4126

Merged
merged 10 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<FileToCopy Include="$(SourcePath)testhost\bin\$(Configuration)\net48\win7-x64\**\*.*" SubFolder="TestHostNetFramework\" />

<!-- copy datacollectors" -->
<FileToCopy Include="$(SourcePath)datacollector\bin\$(Configuration)\net472\**\*.*" SubFolder="vstest.console\" />
<FileToCopy Include="$(SourcePath)datacollector\bin\$(Configuration)\net472\**\*.*" SubFolder="" />
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
<FileToCopy Include="$(SourcePath)Microsoft.TestPlatform.Extensions.BlameDataCollector\bin\$(Configuration)\net472\**\*.*" SubFolder="Extensions\" />
<FileToCopy Include="$(SourcePath)DataCollectors\Microsoft.TestPlatform.Extensions.EventLogCollector\bin\$(Configuration)\$(NetFrameworkMinimum)\**\*.*" SubFolder="Extensions\" />
<FileToCopy Include="$(SourcePath)DataCollectors\DumpMinitool\bin\$(Configuration)\$(NetFrameworkMinimum)\win7-x64\**\*.*" SubFolder="Extensions\blame\" />
Expand Down
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_SERIALIZETESTRUN_DECORATOR)
? originalTestExecutor
: new SerializeTestRunDecorator(originalTestExecutor);
}
}
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 SerializeTestRunDecorator : ITestExecutor, ITestExecutor2, IDisposable
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly SemaphoreSlim _runSequentialEvent = new(1);

public ITestExecutor OriginalTestExecutor { get; private set; }
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved

public SerializeTestRunDecorator(ITestExecutor originalTestExecutor)
{
OriginalTestExecutor = originalTestExecutor;
}

public void RunTests(IEnumerable<TestCase>? tests, IRunContext? runContext, IFrameworkHandle? frameworkHandle)
{
if (IsSerializeTestRunEnabled(runContext))
{
EqtTrace.Info($"SerializeTestRunDecorator: Test cases will run sequentially");
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
if (tests is null)
{
return;
}

foreach (TestCase testToRun in tests)
{
_runSequentialEvent.Wait();
OriginalTestExecutor.RunTests(new List<TestCase> { testToRun }, runContext, new SerializeTestRunDecoratorFrameworkHandle(frameworkHandle!, _runSequentialEvent));
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
}
}
else
{
OriginalTestExecutor.RunTests(tests, runContext, frameworkHandle);
}
}

public void RunTests(IEnumerable<string>? sources, IRunContext? runContext, IFrameworkHandle? frameworkHandle)
{
if (IsSerializeTestRunEnabled(runContext))
{
EqtTrace.Error(Resources.Resources.SerializeTestRunInvalidScenario);
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
frameworkHandle?.SendMessage(TestMessageLevel.Error, Resources.Resources.SerializeTestRunInvalidScenario);
}
else
{
OriginalTestExecutor.RunTests(sources, runContext, frameworkHandle);
}
}

public bool ShouldAttachToTestHost(IEnumerable<string>? sources, IRunContext runContext)
{
if (OriginalTestExecutor is ITestExecutor2 executor)
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
{
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 IsSerializeTestRunEnabled(IRunContext? runContext)
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
{
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
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using System;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionDecorators;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.Utilities;

namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities;

/// <summary>
Expand All @@ -16,6 +20,7 @@ public class LazyExtension<TExtension, TMetadata>
private static readonly object Synclock = new();
private readonly Type? _metadataType;
private readonly Func<TExtension>? _extensionCreator;
private readonly ExtensionDecoratorFactory _extensionDecoratorFactory = new(FeatureFlag.Instance);
private TExtension? _extension;
private TMetadata? _metadata;

Expand Down Expand Up @@ -96,7 +101,15 @@ public TExtension Value
TPDebug.Assert(TestPluginInfo.AssemblyQualifiedName is not null, "TestPluginInfo.AssemblyQualifiedName is null");
var pluginType = TestPluginManager.GetTestExtensionType(TestPluginInfo.AssemblyQualifiedName);
TPDebug.Assert(pluginType is not null, "pluginType is null");
_extension = TestPluginManager.CreateTestExtension<TExtension>(pluginType);

// If the extension is a test executor we decorate the adapter to augment the test platform capabilities.
var extension = TestPluginManager.CreateTestExtension<TExtension>(pluginType);
if (typeof(ITestExecutor).IsAssignableFrom(typeof(TExtension)))
{
extension = (TExtension)_extensionDecoratorFactory.Decorate((ITestExecutor)extension!);
}

_extension = extension;
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions 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.

5 changes: 4 additions & 1 deletion src/Microsoft.TestPlatform.Common/Resources/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
<data name="RunSettingsParseError" xml:space="preserve">
<value>An error occurred while loading the run settings. Error: {0}</value>
</data>
<data name="SerializeTestRunInvalidScenario" xml:space="preserve">
<value>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</value>
MarcoRossignoli marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="SettingsNodeInvalidName" xml:space="preserve">
<value>Invalid settings node specified. The name property of the settings node must be non-empty.</value>
</data>
Expand All @@ -231,4 +234,4 @@
<data name="WrongDataCollectionContextType" xml:space="preserve">
<value>Types deriving from the data collection context cannot be used for sending data and messages. The DataCollectionContext used for sending data and messages must come from one of the events raised to the data collector.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Trasování zásobníku:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Stapelüberwachung:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Seguimiento de la pila:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Arborescence des appels de procédure :</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Analisi dello stack:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">スタック トレース:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">스택 추적:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Ślad stosu:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Rastreamento de pilha:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
<target state="translated">Трассировка стека:</target>
<note></note>
</trans-unit>
<trans-unit id="SerializeTestRunInvalidScenario">
<source>&lt;ForceOneTestAtTimePerTestHost&gt;true&lt;/ForceOneTestAtTimePerTestHost&gt; is not supported for sources test run</source>
<target state="new">&lt;SerializeTestRun&gt;true&lt;/SerializeTestRun&gt; is not supported for sources test run</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Loading