Capture gRPC interop test client output (#22163)

This commit is contained in:
James Newton-King 2020-05-23 11:22:15 +12:00 committed by GitHub
parent 6e91ff9e23
commit d58947f1ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 11 deletions

View File

@ -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 @@ namespace InteropTests.Helpers
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,6 +33,7 @@ namespace InteropTests.Helpers
};
_process.EnableRaisingEvents = true;
_process.OutputDataReceived += Process_OutputDataReceived;
_process.ErrorDataReceived += Process_ErrorDataReceived;
_process.Start();
_processEx = new ProcessEx(output, _process, timeout: Timeout.InfiniteTimeSpan);
@ -35,13 +41,17 @@ namespace InteropTests.Helpers
_startTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
}
public Task WaitForReady()
{
return _startTcs.Task;
}
public Task WaitForReadyAsync() => _startTcs.Task;
public Task WaitForExitAsync() => _processEx.Exited;
public int ExitCode => _process.ExitCode;
public Task Exited => _processEx.Exited;
public string GetOutput()
{
lock (_outputLock)
{
return _output.ToString();
}
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
@ -52,6 +62,23 @@ namespace InteropTests.Helpers
{
_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);
}
}
}

View File

@ -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;
@ -95,11 +93,22 @@ namespace InteropTests
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);
}
}
}
}