From 9e7ce6d8b95274164a6f7a2458aa5d3868654e11 Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 21 Apr 2020 17:20:35 -0700 Subject: [PATCH 1/2] Fix race in gRPC interop test logging --- .../InteropTests/Helpers/WebsiteProcess.cs | 18 +++++++++++++++--- src/Grpc/test/InteropTests/InteropTests.cs | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs b/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs index 77ac62242dff..59ee8fee86b5 100644 --- a/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs +++ b/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs @@ -20,6 +20,7 @@ public class WebsiteProcess : IDisposable private readonly ProcessEx _processEx; private readonly TaskCompletionSource _startTcs; private readonly StringBuilder _consoleOut = new StringBuilder(); + private object _consoleLock = new object(); private static readonly Regex NowListeningRegex = new Regex(@"^\s*Now listening on: .*:(?\d*)$"); public string ServerPort { get; private set; } @@ -58,7 +59,10 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) var data = e.Data; if (data != null) { - _consoleOut.AppendLine(data); + lock (_consoleLock) + { + _consoleOut.AppendLine(data); + } var m = NowListeningRegex.Match(data); if (m.Success) { @@ -74,18 +78,26 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) public void Dispose() { + _process.OutputDataReceived -= Process_OutputDataReceived; + + string consoleOut; + lock (_consoleLock) + { + consoleOut = _consoleOut.ToString(); + } + var attributes = Assembly.GetExecutingAssembly() .GetCustomAttributes(); var serverLogPath = attributes.SingleOrDefault(a => a.Key == "ServerLogPath")?.Value; if (!string.IsNullOrEmpty(serverLogPath)) { - File.WriteAllText(serverLogPath, _consoleOut.ToString()); + File.WriteAllText(serverLogPath, consoleOut); } else { var logDir = Path.Combine(Directory.GetCurrentDirectory(), "artifacts", "logs"); Directory.CreateDirectory(logDir); - File.WriteAllText(Path.Combine(logDir, "InteropServer.log"), _consoleOut.ToString()); + File.WriteAllText(Path.Combine(logDir, "InteropServer.log"), consoleOut); } _processEx.Dispose(); } diff --git a/src/Grpc/test/InteropTests/InteropTests.cs b/src/Grpc/test/InteropTests/InteropTests.cs index 42b4bafe8a06..8e0e7a931033 100644 --- a/src/Grpc/test/InteropTests/InteropTests.cs +++ b/src/Grpc/test/InteropTests/InteropTests.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Threading.Tasks; using InteropTests.Helpers; using Microsoft.AspNetCore.Testing; @@ -27,6 +26,7 @@ public InteropTests(ITestOutputHelper output) } [Theory] + [QuarantinedTest] [MemberData(nameof(TestCaseData))] public async Task InteropTestCase(string name) { From 8dbf33fef0da8cf0c3dc5bcaeea84f5a89f35be1 Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 21 Apr 2020 17:32:08 -0700 Subject: [PATCH 2/2] Remove server-side logging --- .../InteropTests/Helpers/WebsiteProcess.cs | 31 ------------------- src/Grpc/test/InteropTests/InteropTests.cs | 1 - 2 files changed, 32 deletions(-) diff --git a/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs b/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs index 59ee8fee86b5..76d565b8edf2 100644 --- a/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs +++ b/src/Grpc/test/InteropTests/Helpers/WebsiteProcess.cs @@ -3,10 +3,6 @@ using System; using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.AspNetCore.Internal; @@ -19,8 +15,6 @@ public class WebsiteProcess : IDisposable private readonly Process _process; private readonly ProcessEx _processEx; private readonly TaskCompletionSource _startTcs; - private readonly StringBuilder _consoleOut = new StringBuilder(); - private object _consoleLock = new object(); private static readonly Regex NowListeningRegex = new Regex(@"^\s*Now listening on: .*:(?\d*)$"); public string ServerPort { get; private set; } @@ -59,10 +53,6 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) var data = e.Data; if (data != null) { - lock (_consoleLock) - { - _consoleOut.AppendLine(data); - } var m = NowListeningRegex.Match(data); if (m.Success) { @@ -78,27 +68,6 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) public void Dispose() { - _process.OutputDataReceived -= Process_OutputDataReceived; - - string consoleOut; - lock (_consoleLock) - { - consoleOut = _consoleOut.ToString(); - } - - var attributes = Assembly.GetExecutingAssembly() - .GetCustomAttributes(); - var serverLogPath = attributes.SingleOrDefault(a => a.Key == "ServerLogPath")?.Value; - if (!string.IsNullOrEmpty(serverLogPath)) - { - File.WriteAllText(serverLogPath, consoleOut); - } - else - { - var logDir = Path.Combine(Directory.GetCurrentDirectory(), "artifacts", "logs"); - Directory.CreateDirectory(logDir); - File.WriteAllText(Path.Combine(logDir, "InteropServer.log"), consoleOut); - } _processEx.Dispose(); } } diff --git a/src/Grpc/test/InteropTests/InteropTests.cs b/src/Grpc/test/InteropTests/InteropTests.cs index 8e0e7a931033..b08f491e27a9 100644 --- a/src/Grpc/test/InteropTests/InteropTests.cs +++ b/src/Grpc/test/InteropTests/InteropTests.cs @@ -26,7 +26,6 @@ public InteropTests(ITestOutputHelper output) } [Theory] - [QuarantinedTest] [MemberData(nameof(TestCaseData))] public async Task InteropTestCase(string name) {