Attempt to address flaky dotnet-watch tests

This commit is contained in:
Nate McMaster 2018-05-03 18:01:56 -07:00
parent e0e9eb0b36
commit e5da690702
No known key found for this signature in database
GPG Key ID: A778D9601BD78810
3 changed files with 29 additions and 7 deletions

View File

@ -8,12 +8,19 @@ using System.Linq;
using System.Threading;
using Microsoft.DotNet.Watcher.Internal;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{
public class FileWatcherTests
{
public FileWatcherTests(ITestOutputHelper output)
{
_output = output;
}
private const int DefaultTimeout = 10 * 1000; // 10 sec
private readonly ITestOutputHelper _output;
[Theory]
[InlineData(true)]
@ -277,16 +284,24 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
[InlineData(false)]
public void MultipleTriggers(bool usePolling)
{
var filesChanged = new HashSet<string>();
void Clear()
{
_output.WriteLine("Clear files changed list");
filesChanged.Clear();
}
UsingTempDirectory(dir =>
{
using (var changedEv = new AutoResetEvent(false))
using (var watcher = FileWatcherFactory.CreateWatcher(dir, usePolling))
{
var filesChanged = new HashSet<string>();
EventHandler<string> handler = null;
handler = (_, f) =>
{
_output.WriteLine("File changed: " + f);
filesChanged.Add(f);
try
{
@ -318,7 +333,7 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
Assert.True(changedEv.WaitOne(DefaultTimeout));
var fileChanged = Assert.Single(filesChanged);
Assert.Equal(testFileFullPath, fileChanged);
filesChanged.Clear();
Clear();
changedEv.Reset();
// On Unix the file write time is in 1s increments;
@ -331,7 +346,7 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
Assert.True(changedEv.WaitOne(DefaultTimeout));
fileChanged = Assert.Single(filesChanged);
Assert.Equal(testFileFullPath, fileChanged);
filesChanged.Clear();
Clear();
changedEv.Reset();
// On Unix the file write time is in 1s increments;
@ -342,8 +357,9 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
testFileFullPath = Path.Combine(dir, "foo3");
File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single());
filesChanged.Clear();
fileChanged = Assert.Single(filesChanged);
Assert.Equal(testFileFullPath, fileChanged);
Clear();
changedEv.Reset();
// On Unix the file write time is in 1s increments;

View File

@ -1,11 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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 System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing;
using Xunit;
using Xunit.Abstractions;
@ -47,6 +46,7 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
await _app.StartWatcherAsync();
var pid = await _app.GetProcessId();
await _app.HasExited(); // process should exit after run
await _app.IsWaitingForFileChange();
var fileToChange = Path.Combine(_app.SourceDirectory, "Program.cs");
var programCs = File.ReadAllText(fileToChange);

View File

@ -19,6 +19,7 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
private const string StartedMessage = "Started";
private const string ExitingMessage = "Exiting";
private const string WatchExitedMessage = "watch : Exited";
private const string WaitingForFileChangeMessage = "watch : Waiting for a file to change";
private readonly ITestOutputHelper _logger;
private string _appName;
@ -49,6 +50,11 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
await Process.GetOutputLineAsync(WatchExitedMessage, DefaultMessageTimeOut);
}
public async Task IsWaitingForFileChange()
{
await Process.GetOutputLineStartsWithAsync(WaitingForFileChangeMessage, DefaultMessageTimeOut);
}
public bool UsePollingWatcher { get; set; }
public async Task<int> GetProcessId()