Fix threading issue in ExecuteHelper (#13643)

This commit is contained in:
Michael Simons 2022-04-15 15:24:48 -05:00 committed by GitHub
parent 5609d7db88
commit 21c7bb3abb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 98 deletions

View file

@ -5,7 +5,6 @@
using System;
using System.Diagnostics;
using System.IO;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
@ -81,15 +80,6 @@ internal class DotNetHelper
public void ExecuteCmd(string args, string? workingDirectory = null, Action<Process>? additionalProcessConfigCallback = null, int expectedExitCode = 0, int millisecondTimeout = -1)
{
Action<Process, string?> configureProcess = (Process process, string? workingDirectory) => {
ConfigureProcess(process, workingDirectory);
if (additionalProcessConfigCallback != null)
{
additionalProcessConfigCallback(process);
}
};
(Process Process, string StdOut, string StdErr) executeResult = ExecuteHelper.ExecuteProcess(
DotNetPath,
args,
@ -98,6 +88,13 @@ internal class DotNetHelper
millisecondTimeout: millisecondTimeout);
ExecuteHelper.ValidateExitCode(executeResult, expectedExitCode);
void configureProcess(Process process, string? workingDirectory)
{
ConfigureProcess(process, workingDirectory);
additionalProcessConfigCallback?.Invoke(process);
}
}
public static void ConfigureProcess(Process process, string? workingDirectory, bool setPath = false)

View file

@ -44,10 +44,24 @@ internal static class ExecuteHelper
configure?.Invoke(process);
StringBuilder stdOutput = new();
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) => stdOutput.AppendLine(e.Data));
process.OutputDataReceived += new DataReceivedEventHandler(
(sender, e) =>
{
lock (stdOutput)
{
stdOutput.AppendLine(e.Data);
}
});
StringBuilder stdError = new();
process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => stdError.AppendLine(e.Data));
process.ErrorDataReceived += new DataReceivedEventHandler(
(sender, e) =>
{
lock (stdError)
{
stdError.AppendLine(e.Data);
}
});
process.Start();
process.BeginOutputReadLine();

View file

@ -5,13 +5,13 @@
using System;
using Xunit;
namespace Microsoft.DotNet.SourceBuild.SmokeTests
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
/// <summary>
/// A Fact that will be skipped based on the specified environment variable's value.
/// </summary>
internal class SkippableFactAttribute : FactAttribute
{
/// <summary>
/// A Fact that will be skipped based on the specified environment variable's value.
/// </summary>
internal class SkippableFactAttribute : FactAttribute
{
public SkippableFactAttribute(string envName, bool skipOnNullOrWhiteSpace = false, bool skipOnTrue = false) =>
CheckEnvs(skipOnNullOrWhiteSpace, skipOnTrue, (skip) => Skip = skip, envName);
@ -36,5 +36,4 @@ namespace Microsoft.DotNet.SourceBuild.SmokeTests
}
}
}
}
}

View file

@ -4,17 +4,16 @@
using Xunit;
namespace Microsoft.DotNet.SourceBuild.SmokeTests
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
/// <summary>
/// A Theory that will be skipped based on the specified environment variable's value.
/// </summary>
internal class SkippableTheoryAttribute : TheoryAttribute
{
/// <summary>
/// A Theory that will be skipped based on the specified environment variable's value.
/// </summary>
internal class SkippableTheoryAttribute : TheoryAttribute
{
public SkippableTheoryAttribute(string envName, bool skipOnNullOrWhiteSpace = false, bool skipOnTrue = false) =>
SkippableFactAttribute.CheckEnvs(skipOnNullOrWhiteSpace, skipOnTrue, (skip) => Skip = skip, envName);
public SkippableTheoryAttribute(string[] envNames, bool skipOnNullOrWhiteSpace = false, bool skipOnTrue = false) =>
SkippableFactAttribute.CheckEnvs(skipOnNullOrWhiteSpace, skipOnTrue, (skip) => Skip = skip, envNames);
}
}

View file

@ -2,12 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
namespace Microsoft.DotNet.SourceBuild.SmokeTests;
namespace Microsoft.DotNet.SourceBuild.SmokeTests
public class TestScenario
{
public class TestScenario
{
public DotNetActions Commands { get; }
public DotNetLanguage Language { get; }
public bool NoHttps { get; set; } = Config.TargetRid.Contains("osx");
@ -63,5 +61,4 @@ namespace Microsoft.DotNet.SourceBuild.SmokeTests
dotNetHelper.ExecuteTest(projectName);
}
}
}
}