Fix flaky process output reading for dotnet-watch tests (#7173) (#7403)

This commit is contained in:
dotnet-maestro-bot 2019-02-11 16:47:58 -08:00 committed by BrennanConroy
parent c5d8f8c34e
commit 98be5a3ca5
4 changed files with 34 additions and 30 deletions

View File

@ -37,17 +37,31 @@ namespace Microsoft.DotNet.Watcher.Internal
{ {
cancellationToken.Register(() => processState.TryKill()); cancellationToken.Register(() => processState.TryKill());
process.OutputDataReceived += (_, a) =>
{
if (!string.IsNullOrEmpty(a.Data))
{
processSpec.OutputCapture.AddLine(a.Data);
}
};
process.ErrorDataReceived += (_, a) =>
{
if (!string.IsNullOrEmpty(a.Data))
{
processSpec.OutputCapture.AddLine(a.Data);
}
};
stopwatch.Start(); stopwatch.Start();
process.Start(); process.Start();
_reporter.Verbose($"Started '{processSpec.Executable}' with process id {process.Id}"); _reporter.Verbose($"Started '{processSpec.Executable}' with process id {process.Id}");
if (processSpec.IsOutputCaptured) if (processSpec.IsOutputCaptured)
{ {
await Task.WhenAll( process.BeginErrorReadLine();
processState.Task, process.BeginOutputReadLine();
ConsumeStreamAsync(process.StandardOutput, processSpec.OutputCapture.AddLine), await processState.Task;
ConsumeStreamAsync(process.StandardError, processSpec.OutputCapture.AddLine)
);
} }
else else
{ {
@ -86,15 +100,6 @@ namespace Microsoft.DotNet.Watcher.Internal
return process; return process;
} }
private static async Task ConsumeStreamAsync(StreamReader reader, Action<string> consume)
{
string line;
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
{
consume?.Invoke(line);
}
}
private class ProcessState : IDisposable private class ProcessState : IDisposable
{ {
private readonly IReporter _reporter; private readonly IReporter _reporter;

View File

@ -49,19 +49,8 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
await _app.IsWaitingForFileChange(); await _app.IsWaitingForFileChange();
try File.SetLastWriteTime(source, DateTime.Now);
{ await _app.HasRestarted(TimeSpan.FromMinutes(1));
File.SetLastWriteTime(source, DateTime.Now);
await _app.HasRestarted();
}
catch (Exception ex)
{
_logger.WriteLine("Retrying. First attempt to restart app failed: " + ex.Message);
// retry
File.SetLastWriteTime(source, DateTime.Now);
await _app.HasRestarted();
}
} }
} }

View File

@ -0,0 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]

View File

@ -42,7 +42,10 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
public string SourceDirectory { get; } public string SourceDirectory { get; }
public Task HasRestarted() public Task HasRestarted()
=> Process.GetOutputLineAsync(StartedMessage, DefaultMessageTimeOut); => HasRestarted(DefaultMessageTimeOut);
public Task HasRestarted(TimeSpan timeout)
=> Process.GetOutputLineAsync(StartedMessage, timeout);
public async Task HasExited() public async Task HasExited()
{ {
@ -50,9 +53,9 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
await Process.GetOutputLineStartsWithAsync(WatchExitedMessage, DefaultMessageTimeOut); await Process.GetOutputLineStartsWithAsync(WatchExitedMessage, DefaultMessageTimeOut);
} }
public async Task IsWaitingForFileChange() public Task IsWaitingForFileChange()
{ {
await Process.GetOutputLineStartsWithAsync(WaitingForFileChangeMessage, DefaultMessageTimeOut); return Process.GetOutputLineStartsWithAsync(WaitingForFileChangeMessage, DefaultMessageTimeOut);
} }
public bool UsePollingWatcher { get; set; } public bool UsePollingWatcher { get; set; }