Skip to content

Commit

Permalink
Merge pull request #6 from AbhitejJohn/in-assembly-parallel
Browse files Browse the repository at this point in the history
In assembly parallel
  • Loading branch information
AbhitejJohn authored Nov 20, 2017
2 parents 8c5b283 + 053969f commit 4b37cba
Show file tree
Hide file tree
Showing 25 changed files with 702 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ internal TestAssemblySettings GetSettings(string source)
// Load the source.
var testAssembly = PlatformServiceProvider.Instance.FileOperations.LoadAssembly(source, isReflectionOnly: false);

testAssemblySettings.ParallelLevel = this.reflectHelper.GetParallelizationLevel(testAssembly);
var parallelizeAttribute = this.reflectHelper.GetParallelizeAttribute(testAssembly);

if (testAssemblySettings.ParallelLevel == 0)
if (parallelizeAttribute != null)
{
testAssemblySettings.ParallelLevel = Environment.ProcessorCount;
}
testAssemblySettings.Workers = parallelizeAttribute.Workers;
testAssemblySettings.Scope = parallelizeAttribute.Scope;

testAssemblySettings.ParallelMode = this.reflectHelper.GetParallelizationMode(testAssembly);
if (testAssemblySettings.Workers == 0)
{
testAssemblySettings.Workers = Environment.ProcessorCount;
}
}

testAssemblySettings.CanParallelizeAssembly = !this.reflectHelper.IsDoNotParallelizeSet(testAssembly);

Expand Down
26 changes: 16 additions & 10 deletions src/Adapter/MSTest.CoreAdapter/Execution/TestExecutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,28 @@ private void ExecuteTestsInSource(IEnumerable<TestCase> tests, IRunContext runCo
null) as TestAssemblySettingsProvider;

var sourceSettings = sourceSettingsProvider.GetSettings(source);
var parallelLevel = sourceSettings.ParallelLevel;
var parallelMode = sourceSettings.ParallelMode;
var parallelWorkers = sourceSettings.Workers;
var parallelScope = sourceSettings.Scope;

if (MSTestSettings.CurrentSettings.TestParallelizationLevel > 0)
if (MSTestSettings.CurrentSettings.ParallelizationWorkers.HasValue)
{
// The runsettings value takes precedence over an assembly level setting. Reset the level.
parallelLevel = MSTestSettings.CurrentSettings.TestParallelizationLevel;
parallelWorkers = MSTestSettings.CurrentSettings.ParallelizationWorkers.Value;
}

if (parallelLevel > 0 && sourceSettings.CanParallelizeAssembly)
if (MSTestSettings.CurrentSettings.ParallelizationScope.HasValue)
{
// The runsettings value takes precedence over an assembly level setting. Reset the level.
parallelScope = MSTestSettings.CurrentSettings.ParallelizationScope.Value;
}

if (!MSTestSettings.CurrentSettings.DisableParallelization && sourceSettings.CanParallelizeAssembly && parallelWorkers > 0)
{
// Parallelization is enabled. Let's do further classification for sets.
var logger = (IMessageLogger)frameworkHandle;
logger.SendMessage(
TestMessageLevel.Informational,
string.Format(CultureInfo.CurrentCulture, Resource.TestParallelizationBanner, parallelLevel, parallelMode));
string.Format(CultureInfo.CurrentCulture, Resource.TestParallelizationBanner, parallelWorkers, parallelScope));

// Create test sets for execution, we can execute them in parallel based on parallel settings
IEnumerable<IGrouping<bool, TestCase>> testsets = Enumerable.Empty<IGrouping<bool, TestCase>>();
Expand All @@ -275,18 +281,18 @@ private void ExecuteTestsInSource(IEnumerable<TestCase> tests, IRunContext runCo
ConcurrentQueue<IEnumerable<TestCase>> queue = null;

// Chunk the sets into further groups based on parallel level
switch (parallelMode)
switch (parallelScope)
{
case TestParallelizationMode.MethodLevel:
case ExecutionScope.MethodLevel:
queue = new ConcurrentQueue<IEnumerable<TestCase>>(parallelizableTestSet.Select(t => new[] { t }));
break;
case TestParallelizationMode.ClassLevel:
case ExecutionScope.ClassLevel:
queue = new ConcurrentQueue<IEnumerable<TestCase>>(parallelizableTestSet.GroupBy(t => t.GetPropertyValue(TestAdapter.Constants.TestClassNameProperty) as string));
break;
}

var tasks = new List<Task>();
for (int i = 0; i < parallelLevel; i++)
for (int i = 0; i < parallelWorkers; i++)
{
tasks.Add(Task.Factory.StartNew(
() =>
Expand Down
29 changes: 2 additions & 27 deletions src/Adapter/MSTest.CoreAdapter/Helpers/ReflectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,34 +335,9 @@ internal virtual string[] GetCategories(MemberInfo categoryAttributeProvider)
/// </summary>
/// <param name="assembly"> The test asembly. </param>
/// <returns> The parallelization level if set. -1 otherwise. </returns>
internal int GetParallelizationLevel(Assembly assembly)
internal ParallelizeAttribute GetParallelizeAttribute(Assembly assembly)
{
var parallelizationLevelAttribute = PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributes(assembly, typeof(TestParallelizationLevelAttribute)).OfType<TestParallelizationLevelAttribute>().FirstOrDefault();

if (parallelizationLevelAttribute != null)
{
return parallelizationLevelAttribute.ParallelizationLevel;
}

return -1;
}

/// <summary>
/// Gets the parallelization mode set on an assembly.
/// </summary>
/// <param name="assembly"> The test assembly. </param>
/// <returns> The parallelization mode for this assembly if set. TestParallelizationMode.MethodLevel if not.</returns>
internal TestParallelizationMode GetParallelizationMode(Assembly assembly)
{
var parallelizationModeAttribute = PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributes(assembly, typeof(TestParallelizationModeAttribute)).OfType<TestParallelizationModeAttribute>().FirstOrDefault();

if (parallelizationModeAttribute != null)
{
return parallelizationModeAttribute.TestParallelizationMode;
}

// Default to highest degree of parallelization - Method Level.
return TestParallelizationMode.MethodLevel;
return PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributes(assembly, typeof(ParallelizeAttribute)).OfType<ParallelizeAttribute>().FirstOrDefault();
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Adapter/MSTest.CoreAdapter/MSTest.CoreAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Execution\TestAssemblyInfo.cs" />
<Compile Include="Execution\TestClassInfo.cs" />
<Compile Include="Execution\TestAssemblySettingsProvider.cs" />
<Compile Include="ObjectModel\AdapterSettingsException.cs" />
<Compile Include="ObjectModel\TestAssemblySettings.cs" />
<Compile Include="ObjectModel\TestFailedException.cs" />
<Compile Include="TestMethodFilter.cs" />
Expand Down
11 changes: 10 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/MSTestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
Expand Down Expand Up @@ -46,7 +47,15 @@ public void DiscoverTests(
}

// Populate the runsettings.
MSTestSettings.PopulateSettings(discoveryContext);
try
{
MSTestSettings.PopulateSettings(discoveryContext);
}
catch (AdapterSettingsException ex)
{
logger.SendMessage(TestMessageLevel.Error, ex.Message);
return;
}

// Scenarios that include testsettings or forcing a run via the legacy adapter are currently not supported in MSTestAdapter.
if (MSTestSettings.IsLegacyScenario(logger))
Expand Down
22 changes: 20 additions & 2 deletions src/Adapter/MSTest.CoreAdapter/MSTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

/// <summary>
/// Contains the execution logic for this adapter.
Expand Down Expand Up @@ -51,7 +53,15 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
}

// Populate the runsettings.
MSTestSettings.PopulateSettings(runContext);
try
{
MSTestSettings.PopulateSettings(runContext);
}
catch (AdapterSettingsException ex)
{
frameworkHandle.SendMessage(TestMessageLevel.Error, ex.Message);
return;
}

// Scenarios that include testsettings or forcing a run via the legacy adapter are currently not supported in MSTestAdapter.
if (MSTestSettings.IsLegacyScenario(frameworkHandle))
Expand All @@ -75,7 +85,15 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
}

// Populate the runsettings.
MSTestSettings.PopulateSettings(runContext);
try
{
MSTestSettings.PopulateSettings(runContext);
}
catch (AdapterSettingsException ex)
{
frameworkHandle.SendMessage(TestMessageLevel.Error, ex.Message);
return;
}

// Scenarios that include testsettings or forcing a run via the legacy adapter are currently not supported in MSTestAdapter.
if (MSTestSettings.IsLegacyScenario(frameworkHandle))
Expand Down
Loading

0 comments on commit 4b37cba

Please sign in to comment.