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

Set dotnet-watch timeout based on XUnitWorkItemTimeout #40028

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -24,11 +24,12 @@

<ItemGroup>
<ProjectReference Include="..\Microsoft.NET.Build.Extensions.Tasks\Microsoft.NET.Build.Extensions.Tasks.csproj" />
<ProjectReference Include="..\..\..\test\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<Compile Include="..\..\..\test\Common\Program.UnitTests.cs" />
<Compile Include="..\..\..\test\Common\Program.cs" />
<Compile Include="..\Microsoft.NET.Build.Tasks.UnitTests\Mocks\MockBuildEngine.cs" />
<Compile Include="..\Microsoft.NET.Build.Tasks.UnitTests\Mocks\MockTaskItem.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<ItemGroup>
<Compile Include="**\*.cs" />
<Compile Include="..\..\..\test\Common\Program.UnitTests.cs" />
<Compile Include="..\..\..\test\Common\Program.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 0 additions & 24 deletions test/Common/Program.UnitTests.cs

This file was deleted.

2 changes: 2 additions & 0 deletions test/Common/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Commands;

#pragma warning disable SA1205 // Partial elements should declare access
partial class Program
Expand Down
27 changes: 16 additions & 11 deletions test/HelixTasks/SDKCustomCreateXUnitWorkItemsWithTestExclusion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
xunitProject.TryGetMetadata("ExcludeAdditionalParameters", out string ExcludeAdditionalParameters);

xunitProject.TryGetMetadata("Arguments", out string arguments);
TimeSpan timeout = TimeSpan.FromMinutes(5);
if (!string.IsNullOrEmpty(XUnitWorkItemTimeout))
{
if (!TimeSpan.TryParse(XUnitWorkItemTimeout, out timeout))
{
Log.LogWarning($"Invalid value \"{XUnitWorkItemTimeout}\" provided for XUnitWorkItemTimeout; falling back to default value of \"00:05:00\" (5 minutes)");
}
}

string assemblyName = Path.GetFileName(targetPath);

Expand Down Expand Up @@ -142,24 +150,21 @@ private async Task<List<ITaskItem>> PrepareWorkItem(ITaskItem xunitProject)
var partitionedWorkItem = new List<ITaskItem>();
foreach (var assemblyPartitionInfo in assemblyPartitionInfos)
{
string command = $"{driver} exec {assemblyName} {testExecutionDirectory} {msbuildAdditionalSdkResolverFolder} {(XUnitArguments != null ? " " + XUnitArguments : "")} -xml testResults.xml {assemblyPartitionInfo.ClassListArgumentString} {arguments}";
string command;
if (netFramework)
{
var testFilter = string.IsNullOrEmpty(assemblyPartitionInfo.ClassListArgumentString) ? "" : $"--filter \"{assemblyPartitionInfo.ClassListArgumentString}\"";
command = $"{driver} test {assemblyName} {testExecutionDirectory} {msbuildAdditionalSdkResolverFolder} {(XUnitArguments != null ? " " + XUnitArguments : "")} --results-directory .\\ --logger trx {testFilter}";
command = $"{driver} test {assemblyName} -e HELIX_WORK_ITEM_TIMEOUT={timeout} {testExecutionDirectory} {msbuildAdditionalSdkResolverFolder} " +
$"{(XUnitArguments != null ? " " + XUnitArguments : "")} --results-directory .\\ --logger trx {testFilter}";
}

Log.LogMessage($"Creating work item with properties Identity: {assemblyName}, PayloadDirectory: {publishDirectory}, Command: {command}");

TimeSpan timeout = TimeSpan.FromMinutes(5);
if (!string.IsNullOrEmpty(XUnitWorkItemTimeout))
else
{
if (!TimeSpan.TryParse(XUnitWorkItemTimeout, out timeout))
{
Log.LogWarning($"Invalid value \"{XUnitWorkItemTimeout}\" provided for XUnitWorkItemTimeout; falling back to default value of \"00:05:00\" (5 minutes)");
}
command = $"{driver} exec {assemblyName} -e HELIX_WORK_ITEM_TIMEOUT={timeout} {testExecutionDirectory} {msbuildAdditionalSdkResolverFolder} " +
$"{(XUnitArguments != null ? " " + XUnitArguments : "")} -xml testResults.xml {assemblyPartitionInfo.ClassListArgumentString} {arguments}";
}

Log.LogMessage($"Creating work item with properties Identity: {assemblyName}, PayloadDirectory: {publishDirectory}, Command: {command}");

partitionedWorkItem.Add(new Microsoft.Build.Utilities.TaskItem(assemblyPartitionInfo.DisplayName + testIdentityDifferentiator, new Dictionary<string, string>()
{
{ "Identity", assemblyPartitionInfo.DisplayName + testIdentityDifferentiator},
Expand Down
19 changes: 18 additions & 1 deletion test/Microsoft.NET.TestFramework/TestCommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class TestCommandLine

public string MsbuildAdditionalSdkResolverFolder { get; set; }

public List<string> TestConfigFiles { get; private set; } = new List<string>();
public List<(string name, string value)> EnvironmentVariables { get; set; } = [];

public List<string> TestConfigFiles { get; private set; } = [];

public HashSet<string> TestListsToRun { get; private set; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

Expand Down Expand Up @@ -91,6 +93,10 @@ public static TestCommandLine Parse(string[] args)
{
ret.TestListsToRun.Add(argStack.Pop());
}
else if (arg.Equals("-e", StringComparison.CurrentCultureIgnoreCase))
{
ret.EnvironmentVariables.Add(ParseEnvironmentVariableArg(argStack.Pop()));
}
else if (arg.Equals("-showSdkInfo", StringComparison.CurrentCultureIgnoreCase))
{
ret.ShowSdkInfo = true;
Expand Down Expand Up @@ -125,6 +131,17 @@ public static TestCommandLine Parse(string[] args)
return ret;
}

private static (string name, string value) ParseEnvironmentVariableArg(string arg)
{
var i = arg.IndexOf('=');
if (i <= 0)
{
throw new ArgumentException($"Invalid environment variable specification (expected 'name=value'): '{arg}'");
}

return (arg.Substring(0, i), arg.Substring(i + 1));
}

public List<string> GetXunitArgsFromTestConfig()
{
List<TestSpecifier> testsToSkip = new();
Expand Down
5 changes: 5 additions & 0 deletions test/Microsoft.NET.TestFramework/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public static void Initialize(TestCommandLine commandLine)
CommandLoggingContext.SetVerbose(true);
Reporter.Reset();

foreach (var (name, value) in commandLine.EnvironmentVariables)
{
Environment.SetEnvironmentVariable(name, value);
}

Environment.SetEnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "0");

// Reset this environment variable so that if the dotnet under test is different than the
Expand Down
7 changes: 5 additions & 2 deletions test/dotnet-watch.Tests/Utilities/AwaitableProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Microsoft.DotNet.Watcher.Tools
{
internal class AwaitableProcess : IDisposable
{
// cancel just before we hit timeout used on CI (XUnitWorkItemTimeout value in sdk\test\UnitTests.proj)
private static readonly TimeSpan s_timeout = Environment.GetEnvironmentVariable("HELIX_WORK_ITEM_TIMEOUT") is { } value
? TimeSpan.Parse(value).Subtract(TimeSpan.FromSeconds(10)) : TimeSpan.FromMinutes(1);

private readonly object _testOutputLock = new();

private Process _process;
Expand Down Expand Up @@ -70,8 +74,7 @@ public async Task<string> GetOutputLineAsync(Predicate<string> success, Predicat
{
using var cancellationOnFailure = new CancellationTokenSource();

// cancel just before we hit 2 minute time out used on CI (sdk\test\UnitTests.proj)
cancellationOnFailure.CancelAfter(TimeSpan.FromSeconds(110));
cancellationOnFailure.CancelAfter(s_timeout);

var failedLineCount = 0;
while (!_source.Completion.IsCompleted && failedLineCount == 0)
Expand Down