-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Capture gRPC interop test client output #22163
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Internal; | ||
|
@@ -15,9 +16,13 @@ public class ClientProcess : IDisposable | |
private readonly Process _process; | ||
private readonly ProcessEx _processEx; | ||
private readonly TaskCompletionSource<object> _startTcs; | ||
private readonly StringBuilder _output; | ||
private readonly object _outputLock = new object(); | ||
|
||
public ClientProcess(ITestOutputHelper output, string path, string serverPort, string testCase) | ||
{ | ||
_output = new StringBuilder(); | ||
|
||
_process = new Process(); | ||
_process.StartInfo = new ProcessStartInfo | ||
{ | ||
|
@@ -28,21 +33,26 @@ public ClientProcess(ITestOutputHelper output, string path, string serverPort, s | |
}; | ||
_process.EnableRaisingEvents = true; | ||
_process.OutputDataReceived += Process_OutputDataReceived; | ||
_process.ErrorDataReceived += Process_ErrorDataReceived; | ||
_process.Start(); | ||
|
||
_processEx = new ProcessEx(output, _process, timeout: Timeout.InfiniteTimeSpan); | ||
|
||
_startTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
} | ||
|
||
public Task WaitForReady() | ||
public Task WaitForReadyAsync() => _startTcs.Task; | ||
public Task WaitForExitAsync() => _processEx.Exited; | ||
public int ExitCode => _process.ExitCode; | ||
|
||
public string GetOutput() | ||
{ | ||
return _startTcs.Task; | ||
lock (_outputLock) | ||
{ | ||
return _output.ToString(); | ||
} | ||
} | ||
|
||
public int ExitCode => _process.ExitCode; | ||
public Task Exited => _processEx.Exited; | ||
|
||
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) | ||
{ | ||
var data = e.Data; | ||
|
@@ -52,6 +62,23 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) | |
{ | ||
_startTcs.TrySetResult(null); | ||
} | ||
|
||
lock (_outputLock) | ||
{ | ||
_output.AppendLine(data); | ||
} | ||
} | ||
} | ||
|
||
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) | ||
{ | ||
var data = e.Data; | ||
if (data != null) | ||
{ | ||
lock (_outputLock) | ||
{ | ||
_output.AppendLine(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should error and regular output be distinguished in the buffer e.g. with an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only error output would be if an exception is thrown, and it should be obvious. Will change in the future if it is confusing. |
||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest cleaning this object up in
Dispose()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will get GCed with ClientProcess so I'm not worried about it