Skip to content

Commit

Permalink
Add multiplier format support to RunSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Apr 27, 2024
1 parent 3c2e493 commit b8be108
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/xunit.runner.visualstudio/Utility/RunSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Xunit.Runner.VisualStudio;
Expand Down Expand Up @@ -94,8 +95,28 @@ public static RunSettings Parse(string? settingsXml)
result.LongRunningTestSeconds = longRunningTestSeconds;

var maxParallelThreadsString = xunitElement.Element(Constants.Xunit.MaxParallelThreads)?.Value;
if (int.TryParse(maxParallelThreadsString, out var maxParallelThreads) && maxParallelThreads >= -1)
result.MaxParallelThreads = maxParallelThreads;
if (maxParallelThreadsString is not null)
switch (maxParallelThreadsString)
{
case "default":
result.MaxParallelThreads = 0;
break;

case "unlimited":
result.MaxParallelThreads = -1;
break;

default:
var match = ConfigUtility.MultiplierStyleMaxParallelThreadsRegex.Match(maxParallelThreadsString);
// Use invariant format and convert ',' to '.' so we can always support both formats, regardless of locale
// If we stick to locale-only parsing, we could break people when moving from one locale to another (for example,
// from people running tests on their desktop in a comma locale vs. running them in CI with a decimal locale).
if (match.Success && decimal.TryParse(match.Groups[1].Value.Replace(',', '.'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var maxThreadMultiplier))
result.MaxParallelThreads = (int)(maxThreadMultiplier * Environment.ProcessorCount);
else if (int.TryParse(maxParallelThreadsString, out var threadValue) && threadValue >= -1)
result.MaxParallelThreads = threadValue;
break;
}

var methodDisplayString = xunitElement.Element(Constants.Xunit.MethodDisplay)?.Value;
if (Enum.TryParse<TestMethodDisplay>(methodDisplayString, ignoreCase: true, out var methodDisplay))
Expand Down
20 changes: 16 additions & 4 deletions test/test.xunit.runner.visualstudio/RunSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Xunit;
using Xunit.Runner.VisualStudio;

Expand All @@ -16,6 +17,7 @@ void AssertDefaultValues(RunSettings runSettings)
Assert.Null(runSettings.MethodDisplay);
Assert.Null(runSettings.MethodDisplayOptions);
Assert.Null(runSettings.NoAutoReporters);
Assert.Null(runSettings.ParallelAlgorithm);
Assert.Null(runSettings.ParallelizeAssembly);
Assert.Null(runSettings.ParallelizeTestCollections);
Assert.Null(runSettings.PreEnumerateTheories);
Expand Down Expand Up @@ -182,11 +184,21 @@ public void LongRunningTestSeconds(int value, int? expected)
Assert.Equal(expected, runSettings.LongRunningTestSeconds);
}

public static readonly TheoryData<string, int?> MaxParallelThreadData = new()
{
{ "blarch", null },
{ "-2", null },
{ "-1", -1 },
{ "0", 0 },
{ "42", 42 },
{ "unlimited", -1 },
{ "default", 0 },
{ "2.0x", Environment.ProcessorCount * 2 },
};

[Theory]
[InlineData(-2, null)]
[InlineData(-1, -1)]
[InlineData(42, 42)]
public void MaxParallelThreads(int value, int? expected)
[MemberData(nameof(MaxParallelThreadData))]
public void MaxParallelThreads(string value, int? expected)
{
string settingsXml =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
Expand Down

0 comments on commit b8be108

Please sign in to comment.