Skip to content

Commit

Permalink
BatchSize Runsettings (#550)
Browse files Browse the repository at this point in the history
* Changes for making BatchSize configurable via runsettings.
* Merging the two test files for the same class and removing the redundant test.
  • Loading branch information
singhsarab authored Mar 1, 2017
1 parent cc11d0d commit d62c005
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 118 deletions.
5 changes: 5 additions & 0 deletions src/Microsoft.TestPlatform.ObjectModel/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public static class Constants
/// </summary>
public const int DefaultCpuCount = 1;

/// <summary>
/// The default batch size.
/// </summary>
public const long DefaultBatchSize = 10;

/// <summary>
/// Name of the results directory
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ public class RunConfiguration : TestRunSettings
/// Maximum number of cores that the engine can use to run tests in parallel
/// </summary>
private int maxCpuCount;

/// <summary>
/// .Net framework which rocksteady should use for discovery/execution
/// </summary>
private Framework framework;

/// <summary>
/// Specifies the frequency of the runStats/discoveredTests event
/// </summary>
private long batchSize;

/// <summary>
/// Directory in which rocksteady/adapter should keep their run specific data.
/// </summary>
Expand Down Expand Up @@ -71,6 +76,7 @@ public RunConfiguration() : base(Constants.RunConfigurationSettingsName)
this.BinariesRoot = null;
this.testAdaptersPaths = null;
this.maxCpuCount = Constants.DefaultCpuCount;
this.batchSize = Constants.DefaultBatchSize;
this.disableAppDomain = false;
this.disableParallelization = false;
}
Expand Down Expand Up @@ -121,6 +127,22 @@ public int MaxCpuCount
}
}

/// <summary>
/// Gets or sets the frequency of the runStats/discoveredTests event. Should be non-negative integer.
/// </summary>
public long BatchSize
{
get
{
return this.batchSize;
}
set
{
this.batchSize = value;
this.BatchSizeSet = true;
}
}

/// <summary>
/// Gets or sets a value indicating whether app domain creation should be disabled.
/// </summary>
Expand Down Expand Up @@ -171,7 +193,7 @@ public Architecture TargetPlatform
this.TargetPlatformSet = true;
}
}

/// <summary>
/// Gets or sets the target Framework this run is targeting. Possible values are Framework3.5|Framework4.0|Framework4.5
/// </summary>
Expand Down Expand Up @@ -237,6 +259,15 @@ public bool MaxCpuCountSet
private set;
}

/// <summary>
/// Gets a value indicating batch size is set
/// </summary>
public bool BatchSizeSet
{
get;
private set;
}

/// <summary>
/// Gets a value indicating whether app domain needs to be disabled by the adapters.
/// </summary>
Expand Down Expand Up @@ -289,7 +320,7 @@ public bool ResultsDirectorySet
public string BinariesRoot { get; private set; }

#endregion

/// <inheritdoc/>
[SuppressMessage("Microsoft.Security.Xml", "CA3053:UseXmlSecureResolver",
Justification = "XmlDocument.XmlResolver is not available in core. Suppress until fxcop issue is fixed.")]
Expand All @@ -311,6 +342,10 @@ public override XmlElement ToXml()
maxCpuCount.InnerXml = this.MaxCpuCount.ToString();
root.AppendChild(maxCpuCount);

XmlElement batchSize = doc.CreateElement("BatchSize");
batchSize.InnerXml = this.BatchSize.ToString();
root.AppendChild(batchSize);

XmlElement disableAppDomain = doc.CreateElement("DisableAppDomain");
disableAppDomain.InnerXml = this.DisableAppDomain.ToString();
root.AppendChild(disableAppDomain);
Expand Down Expand Up @@ -343,7 +378,7 @@ public override XmlElement ToXml()

return root;
}

/// <summary>
/// Loads RunConfiguration from XmlReader.
/// </summary>
Expand Down Expand Up @@ -390,6 +425,25 @@ public static RunConfiguration FromXml(XmlReader reader)
runConfiguration.MaxCpuCount = count;
break;

case "BatchSize":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

string batchSize = reader.ReadElementContentAsString();
long size;
if (!long.TryParse(batchSize, out size) || size < 0)
{
throw new SettingsException(
string.Format(
CultureInfo.CurrentCulture,
Resources.Resources.InvalidSettingsIncorrectValue,
Constants.RunConfigurationSettingsName,
batchSize,
elementName));
}

runConfiguration.BatchSize = size;
break;

case "DisableAppDomain":
XmlRunSettingsUtilities.ThrowOnHasAttributes(reader);

Expand Down
19 changes: 13 additions & 6 deletions src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.TestPlatformHelpers
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;

/// <summary>
/// Defines the TestRequestManger which can fire off discovery and test run requests
Expand Down Expand Up @@ -54,8 +55,8 @@ internal class TestRequestManager : ITestRequestManager
public TestRequestManager() :
this(CommandLineOptions.Instance,
TestPlatformFactory.GetTestPlatform(),
TestLoggerManager.Instance,
TestRunResultAggregator.Instance,
TestLoggerManager.Instance,
TestRunResultAggregator.Instance,
TestPlatformEventSource.Instance)
{
}
Expand All @@ -75,7 +76,7 @@ internal TestRequestManager(CommandLineOptions commandLineOptions, ITestPlatform
{
var consoleLogger = new ConsoleLogger();
this.testLoggerManager.AddLogger(consoleLogger, ConsoleLogger.ExtensionUri, null);
}
}
}

#endregion
Expand Down Expand Up @@ -126,8 +127,11 @@ public bool DiscoverTests(DiscoveryRequestPayload discoveryPayload, ITestDiscove

bool success = false;

var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(discoveryPayload.RunSettings);
var batchSize = runConfiguration.BatchSize;

// create discovery request
var criteria = new DiscoveryCriteria(discoveryPayload.Sources, this.commandLineOptions.BatchSize, this.commandLineOptions.TestStatsEventTimeout, discoveryPayload.RunSettings);
var criteria = new DiscoveryCriteria(discoveryPayload.Sources, batchSize, this.commandLineOptions.TestStatsEventTimeout, discoveryPayload.RunSettings);
using (IDiscoveryRequest discoveryRequest = this.testPlatform.CreateDiscoveryRequest(criteria))
{
try
Expand Down Expand Up @@ -182,12 +186,15 @@ public bool RunTests(TestRunRequestPayload testRunRequestPayload, ITestHostLaunc
{
EqtTrace.Info("TestRequestManager.RunTests: run tests started.");

var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(testRunRequestPayload.RunSettings);
var batchSize = runConfiguration.BatchSize;

TestRunCriteria runCriteria = null;
if (testRunRequestPayload.Sources != null && testRunRequestPayload.Sources.Any())
{
runCriteria = new TestRunCriteria(
testRunRequestPayload.Sources,
this.commandLineOptions.BatchSize,
batchSize,
testRunRequestPayload.KeepAlive,
testRunRequestPayload.RunSettings,
this.commandLineOptions.TestStatsEventTimeout,
Expand All @@ -198,7 +205,7 @@ public bool RunTests(TestRunRequestPayload testRunRequestPayload, ITestHostLaunc
{
runCriteria = new TestRunCriteria(
testRunRequestPayload.TestCases,
this.commandLineOptions.BatchSize,
batchSize,
testRunRequestPayload.KeepAlive,
testRunRequestPayload.RunSettings,
this.commandLineOptions.TestStatsEventTimeout,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// 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;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.TestPlatform.ObjectModel.UnitTests
{
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

[TestClass]
public class RunConfigurationTests
{
Expand All @@ -22,6 +19,7 @@ public void RunConfigurationDefaultValuesMustBeUsedOnCreation()
// Verify Default
Assert.AreEqual(Constants.DefaultPlatform, runConfiguration.TargetPlatform);
Assert.AreEqual(Framework.DefaultFramework, runConfiguration.TargetFrameworkVersion);
Assert.AreEqual(Constants.DefaultBatchSize, runConfiguration.BatchSize);
Assert.AreEqual(Constants.DefaultResultsDirectory, runConfiguration.ResultsDirectory);
Assert.AreEqual(null, runConfiguration.SolutionDirectory);
Assert.AreEqual(Constants.DefaultTreatTestAdapterErrorsAsWarnings, runConfiguration.TreatTestAdapterErrorsAsWarnings);
Expand All @@ -43,7 +41,6 @@ public void RunConfigurationThrowsExceptionOnUnknownElements()
</RunConfiguration>
</RunSettings>";


Assert.ThrowsException<SettingsException>(() =>
XmlRunSettingsUtilities.GetRunConfigurationNode(settingsXml));
}
Expand All @@ -63,6 +60,7 @@ public void RunConfigurationReadsValuesCorrectlyFromXml()
<DisableAppDomain>true</DisableAppDomain>
<DisableParallelization>true</DisableParallelization>
<MaxCpuCount>2</MaxCpuCount>
<BatchSize>5</BatchSize>
<TestAdaptersPaths>C:\a\b;D:\x\y</TestAdaptersPaths>
<BinariesRoot>E:\x\z</BinariesRoot>
</RunConfiguration>
Expand All @@ -87,8 +85,43 @@ public void RunConfigurationReadsValuesCorrectlyFromXml()
Assert.AreEqual(@"E:\x\z", runConfiguration.BinariesRoot);
Assert.AreEqual(@"C:\a\b;D:\x\y", runConfiguration.TestAdaptersPaths);
Assert.AreEqual(2, runConfiguration.MaxCpuCount);
Assert.AreEqual(5, runConfiguration.BatchSize);
Assert.AreEqual(true, runConfiguration.DisableAppDomain);
Assert.AreEqual(true, runConfiguration.DisableParallelization);
}

[TestMethod]
public void RunConfigurationFromXmlThrowsSettingsExceptionIfBatchSizeIsInvalid()
{
string settingsXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<BatchSize>Foo</BatchSize>
</RunConfiguration>
</RunSettings>";

var ex = Assert.ThrowsException<SettingsException>(
() => XmlRunSettingsUtilities.GetRunConfigurationNode(settingsXml)
);
Assert.AreEqual(ex.Message, "Invalid settings 'RunConfiguration'. Invalid value 'Foo' specified for 'BatchSize'.");
}

[TestMethod]
public void RunConfigurationFromXmlThrowsSettingsExceptionIfBatchSizeIsNegativeInteger()
{
string settingsXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<RunSettings>
<RunConfiguration>
<BatchSize>-10</BatchSize>
</RunConfiguration>
</RunSettings>";

var ex = Assert.ThrowsException<SettingsException>(
() => XmlRunSettingsUtilities.GetRunConfigurationNode(settingsXml)
);
Assert.AreEqual(ex.Message, "Invalid settings 'RunConfiguration'. Invalid value '-10' specified for 'BatchSize'.");
}
}
}
Loading

0 comments on commit d62c005

Please sign in to comment.