Skip to content

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

Merged
merged 1 commit into from
May 22, 2020
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
37 changes: 32 additions & 5 deletions src/Grpc/test/InteropTests/Helpers/ClientProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Internal;
Expand All @@ -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;
Copy link
Contributor

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()

Copy link
Member Author

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

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
{
Expand All @@ -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;
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ERROR: prefix?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

}
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/Grpc/test/InteropTests/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using InteropTests.Helpers;
using Microsoft.AspNetCore.Testing;
Expand Down Expand Up @@ -36,11 +34,22 @@ public async Task InteropTestCase(string name)

using (var clientProcess = new ClientProcess(_output, _clientPath, serverProcess.ServerPort, name))
{
await clientProcess.WaitForReady().TimeoutAfter(DefaultTimeout);
try
{
await clientProcess.WaitForReadyAsync().TimeoutAfter(DefaultTimeout);

await clientProcess.Exited.TimeoutAfter(DefaultTimeout);
await clientProcess.WaitForExitAsync().TimeoutAfter(DefaultTimeout);

Assert.Equal(0, clientProcess.ExitCode);
Assert.Equal(0, clientProcess.ExitCode);
}
catch (Exception ex)
{
var clientOutput = clientProcess.GetOutput();
var errorMessage = $@"Error while running client process. Process output:
======================================
{clientOutput}";
throw new InvalidOperationException(errorMessage, ex);
}
}
}
}
Expand Down