Capture gRPC interop test client output (#22163)
This commit is contained in:
parent
6e91ff9e23
commit
d58947f1ba
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Internal;
|
using Microsoft.AspNetCore.Internal;
|
||||||
|
|
@ -15,9 +16,13 @@ namespace InteropTests.Helpers
|
||||||
private readonly Process _process;
|
private readonly Process _process;
|
||||||
private readonly ProcessEx _processEx;
|
private readonly ProcessEx _processEx;
|
||||||
private readonly TaskCompletionSource<object> _startTcs;
|
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)
|
public ClientProcess(ITestOutputHelper output, string path, string serverPort, string testCase)
|
||||||
{
|
{
|
||||||
|
_output = new StringBuilder();
|
||||||
|
|
||||||
_process = new Process();
|
_process = new Process();
|
||||||
_process.StartInfo = new ProcessStartInfo
|
_process.StartInfo = new ProcessStartInfo
|
||||||
{
|
{
|
||||||
|
|
@ -28,6 +33,7 @@ namespace InteropTests.Helpers
|
||||||
};
|
};
|
||||||
_process.EnableRaisingEvents = true;
|
_process.EnableRaisingEvents = true;
|
||||||
_process.OutputDataReceived += Process_OutputDataReceived;
|
_process.OutputDataReceived += Process_OutputDataReceived;
|
||||||
|
_process.ErrorDataReceived += Process_ErrorDataReceived;
|
||||||
_process.Start();
|
_process.Start();
|
||||||
|
|
||||||
_processEx = new ProcessEx(output, _process, timeout: Timeout.InfiniteTimeSpan);
|
_processEx = new ProcessEx(output, _process, timeout: Timeout.InfiniteTimeSpan);
|
||||||
|
|
@ -35,13 +41,17 @@ namespace InteropTests.Helpers
|
||||||
_startTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
|
_startTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task WaitForReady()
|
public Task WaitForReadyAsync() => _startTcs.Task;
|
||||||
{
|
public Task WaitForExitAsync() => _processEx.Exited;
|
||||||
return _startTcs.Task;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int ExitCode => _process.ExitCode;
|
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)
|
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
@ -52,6 +62,23 @@ namespace InteropTests.Helpers
|
||||||
{
|
{
|
||||||
_startTcs.TrySetResult(null);
|
_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using InteropTests.Helpers;
|
using InteropTests.Helpers;
|
||||||
using Microsoft.AspNetCore.Testing;
|
using Microsoft.AspNetCore.Testing;
|
||||||
|
|
@ -95,11 +93,22 @@ namespace InteropTests
|
||||||
|
|
||||||
using (var clientProcess = new ClientProcess(_output, _clientPath, serverProcess.ServerPort, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue