Skip to content

Commit fcc1641

Browse files
authored
Simplify reading process stdout/stderr for dotnet test [MTP] (#50808)
1 parent 7b8c60f commit fcc1641

File tree

1 file changed

+3
-39
lines changed

1 file changed

+3
-39
lines changed

src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,51 +49,15 @@ public async Task<int> RunAsync()
4949
Logger.LogTrace($"Starting test process with command '{processStartInfo.FileName}' and arguments '{processStartInfo.Arguments}'.");
5050

5151
using var process = Process.Start(processStartInfo)!;
52-
var standardOutput = process.StandardOutput;
53-
var standardError = process.StandardError;
54-
55-
var tcsStdOutput = new TaskCompletionSource<string>();
56-
var tcsStdError = new TaskCompletionSource<string>();
5752

5853
// Reading from process stdout/stderr is done on separate threads to avoid blocking IO on the threadpool.
5954
// Note: even with 'process.StandardOutput.ReadToEndAsync()' or 'process.BeginOutputReadLine()', we ended up with
6055
// many TP threads just doing synchronous IO, slowing down the progress of the test run.
6156
// We want to read requests coming through the pipe and sending responses back to the test app as fast as possible.
62-
var tStdOut = new Thread(() =>
63-
{
64-
StringBuilder? builder = null;
65-
while (true)
66-
{
67-
if (standardOutput.ReadLine() is not { } line)
68-
{
69-
tcsStdOutput.SetResult(builder?.ToString() ?? string.Empty);
70-
return;
71-
}
72-
73-
(builder ??= new()).AppendLine(line);
74-
}
75-
});
76-
tStdOut.Name = "TestApp StdOut read";
77-
tStdOut.Start();
78-
79-
var tStdErr = new Thread(() =>
80-
{
81-
StringBuilder? builder = null;
82-
while (true)
83-
{
84-
if (standardError.ReadLine() is not { } line)
85-
{
86-
tcsStdError.SetResult(builder?.ToString() ?? string.Empty);
87-
return;
88-
}
89-
90-
(builder ??= new()).AppendLine(line);
91-
}
92-
});
93-
tStdErr.Name = "TestApp StdErr read";
94-
tStdErr.Start();
57+
var stdOutTask = Task.Factory.StartNew(static standardOutput => ((StreamReader)standardOutput!).ReadToEnd(), process.StandardOutput, TaskCreationOptions.LongRunning);
58+
var stdErrTask = Task.Factory.StartNew(static standardError => ((StreamReader)standardError!).ReadToEnd(), process.StandardError, TaskCreationOptions.LongRunning);
9559

96-
var outputAndError = await Task.WhenAll(tcsStdOutput.Task, tcsStdError.Task);
60+
var outputAndError = await Task.WhenAll(stdOutTask, stdErrTask);
9761
await process.WaitForExitAsync();
9862

9963
_handler.OnTestProcessExited(process.ExitCode, outputAndError[0], outputAndError[1]);

0 commit comments

Comments
 (0)