Fix the tests that hang

This commit is contained in:
Victor Hurdugaci 2016-03-04 10:37:50 -08:00
parent 618e41fa1e
commit bc382be632
6 changed files with 80 additions and 47 deletions

View File

@ -96,8 +96,17 @@ namespace Microsoft.DotNet.Watcher.Core.Internal
private void DisposeWatcher(string directory) private void DisposeWatcher(string directory)
{ {
_watchers[directory].Dispose(); var watcher = _watchers[directory];
_watchers.Remove(directory); _watchers.Remove(directory);
watcher.EnableRaisingEvents = false;
watcher.Changed -= WatcherChangedHandler;
watcher.Created -= WatcherChangedHandler;
watcher.Deleted -= WatcherChangedHandler;
watcher.Renamed -= WatcherRenamedHandler;
watcher.Dispose();
} }
private void EnsureNotDisposed() private void EnsureNotDisposed()

View File

@ -13,19 +13,22 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30); private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
// Change a file included in compilation // Change a file included in compilation
[Fact(Skip = "Disabled temporary")] [Fact]
public void ChangeFileInDependency() public void ChangeFileInDependency()
{ {
using (var scenario = new AppWithDepsScenario()) using (var scenario = new AppWithDepsScenario())
using (var wait = new WaitForFileToChange(scenario.StartedFile))
{ {
var fileToChange = Path.Combine(scenario.DependencyFolder, "Foo.cs"); scenario.Start();
var programCs = File.ReadAllText(fileToChange); using (var wait = new WaitForFileToChange(scenario.StartedFile))
File.WriteAllText(fileToChange, programCs); {
var fileToChange = Path.Combine(scenario.DependencyFolder, "Foo.cs");
var programCs = File.ReadAllText(fileToChange);
File.WriteAllText(fileToChange, programCs);
wait.Wait(_defaultTimeout, wait.Wait(_defaultTimeout,
expectedToChange: true, expectedToChange: true,
errorMessage: $"Process did not restart because {scenario.StartedFile} was not changed"); errorMessage: $"Process did not restart because {scenario.StartedFile} was not changed");
}
} }
} }
@ -51,7 +54,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
AppWithDepsFolder = Path.Combine(_scenario.WorkFolder, AppWithDeps); AppWithDepsFolder = Path.Combine(_scenario.WorkFolder, AppWithDeps);
DependencyFolder = Path.Combine(_scenario.WorkFolder, Dependency); DependencyFolder = Path.Combine(_scenario.WorkFolder, Dependency);
}
public void Start()
{
// Wait for the process to start // Wait for the process to start
using (var wait = new WaitForFileToChange(StatusFile)) using (var wait = new WaitForFileToChange(StatusFile))
{ {

View File

@ -16,12 +16,14 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
private static readonly TimeSpan _negativeTestWaitTime = TimeSpan.FromSeconds(10); private static readonly TimeSpan _negativeTestWaitTime = TimeSpan.FromSeconds(10);
// Change a file included in compilation // Change a file included in compilation
[Fact(Skip = "Disabled temporary")] [Fact]
public void ChangeCompiledFile() public void ChangeCompiledFile()
{ {
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
using (var wait = new WaitForFileToChange(scenario.StartedFile)) using (var wait = new WaitForFileToChange(scenario.StartedFile))
{ {
scenario.Start();
var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs"); var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs");
var programCs = File.ReadAllText(fileToChange); var programCs = File.ReadAllText(fileToChange);
File.WriteAllText(fileToChange, programCs); File.WriteAllText(fileToChange, programCs);
@ -34,13 +36,15 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
// Add a file to a folder included in compilation // Add a file to a folder included in compilation
[Fact(Skip = "Disabled temporary")] [Fact]
public void AddCompiledFile() public void AddCompiledFile()
{ {
// Add a file in a folder that's included in compilation // Add a file in a folder that's included in compilation
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
using (var wait = new WaitForFileToChange(scenario.StartedFile)) using (var wait = new WaitForFileToChange(scenario.StartedFile))
{ {
scenario.Start();
var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Bar.cs"); var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Bar.cs");
File.WriteAllText(fileToChange, ""); File.WriteAllText(fileToChange, "");
@ -51,12 +55,14 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
// Delete a file included in compilation // Delete a file included in compilation
[Fact(Skip = "Disabled temporary")] [Fact]
public void DeleteCompiledFile() public void DeleteCompiledFile()
{ {
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
using (var wait = new WaitForFileToChange(scenario.StartedFile)) using (var wait = new WaitForFileToChange(scenario.StartedFile))
{ {
scenario.Start();
var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs"); var fileToChange = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs");
File.Delete(fileToChange); File.Delete(fileToChange);
@ -67,13 +73,14 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
// Rename a file included in compilation // Rename a file included in compilation
[Fact(Skip = "Disabled temporary")] [Fact]
public void RenameCompiledFile() public void RenameCompiledFile()
{ {
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
using (var wait = new WaitForFileToChange(scenario.StatusFile)) using (var wait = new WaitForFileToChange(scenario.StatusFile))
{ {
scenario.Start();
var oldFile = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs"); var oldFile = Path.Combine(scenario.TestAppFolder, "include", "Foo.cs");
var newFile = Path.Combine(scenario.TestAppFolder, "include", "Foo_new.cs"); var newFile = Path.Combine(scenario.TestAppFolder, "include", "Foo_new.cs");
File.Move(oldFile, newFile); File.Move(oldFile, newFile);
@ -85,19 +92,20 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
// Add a file that's in a included folder but not matching the globbing pattern // Add a file that's in a included folder but not matching the globbing pattern
[Fact(Skip = "Disabled temporary")] [Fact]
public void ChangeNonCompiledFile() public void ChangeNonCompiledFile()
{ {
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
using (var wait = new WaitForFileToChange(scenario.StartedFile))
{ {
scenario.Start();
var ids = File.ReadAllLines(scenario.StatusFile); var ids = File.ReadAllLines(scenario.StatusFile);
var procId = int.Parse(ids[0]); var procId = int.Parse(ids[0]);
var changedFile = Path.Combine(scenario.TestAppFolder, "include", "not_compiled.css"); var changedFile = Path.Combine(scenario.TestAppFolder, "include", "not_compiled.css");
File.WriteAllText(changedFile, ""); File.WriteAllText(changedFile, "");
Console.WriteLine($"Waiting {_negativeTestWaitTime} seconds to see if the app restarts"); Console.WriteLine($"Waiting {_negativeTestWaitTime.TotalSeconds} seconds to see if the app restarts");
Waiters.WaitForProcessToStop( Waiters.WaitForProcessToStop(
procId, procId,
_negativeTestWaitTime, _negativeTestWaitTime,
@ -107,27 +115,25 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
// Change a file that's in an excluded folder // Change a file that's in an excluded folder
[Fact(Skip = "Disabled temporary")] [Fact]
public void ChangeExcludedFile() public void ChangeExcludedFile()
{ {
using (var scenario = new GlobbingAppScenario()) using (var scenario = new GlobbingAppScenario())
{ {
// Then wait for it to restart when we change a file that's included in the compilation files scenario.Start();
using (var wait = new WaitForFileToChange(scenario.StartedFile))
{
var ids = File.ReadAllLines(scenario.StatusFile);
var procId = int.Parse(ids[0]);
var changedFile = Path.Combine(scenario.TestAppFolder, "exclude", "Baz.cs"); var ids = File.ReadAllLines(scenario.StatusFile);
File.WriteAllText(changedFile, ""); var procId = int.Parse(ids[0]);
Console.WriteLine($"Waiting {_negativeTestWaitTime} seconds to see if the app restarts"); var changedFile = Path.Combine(scenario.TestAppFolder, "exclude", "Baz.cs");
Waiters.WaitForProcessToStop( File.WriteAllText(changedFile, "");
procId,
_negativeTestWaitTime, Console.WriteLine($"Waiting {_negativeTestWaitTime.TotalSeconds} seconds to see if the app restarts");
expectedToStop: false, Waiters.WaitForProcessToStop(
errorMessage: "Test app restarted"); procId,
} _negativeTestWaitTime,
expectedToStop: false,
errorMessage: "Test app restarted");
} }
} }
@ -146,7 +152,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
_scenario.Restore(); _scenario.Restore();
TestAppFolder = Path.Combine(_scenario.WorkFolder, TestAppName); TestAppFolder = Path.Combine(_scenario.WorkFolder, TestAppName);
}
public void Start()
{
// Wait for the process to start // Wait for the process to start
using (var wait = new WaitForFileToChange(StartedFile)) using (var wait = new WaitForFileToChange(StartedFile))
{ {

View File

@ -12,7 +12,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
{ {
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30); private static readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(30);
[Fact(Skip = "Disabled temporary")] [Fact]
public void RestartProcessOnFileChange() public void RestartProcessOnFileChange()
{ {
using (var scenario = new NoDepsAppScenario()) using (var scenario = new NoDepsAppScenario())
@ -51,7 +51,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
} }
[Fact(Skip = "Disabled temporary")] [Fact]
public void RestartProcessThatTerminatesAfterFileChange() public void RestartProcessThatTerminatesAfterFileChange()
{ {
using (var scenario = new NoDepsAppScenario()) using (var scenario = new NoDepsAppScenario())
@ -91,7 +91,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
[Fact(Skip = "Disabled temporary")] [Fact]
public void ExitOnChange() public void ExitOnChange()
{ {
using (var scenario = new NoDepsAppScenario()) using (var scenario = new NoDepsAppScenario())

View File

@ -20,17 +20,14 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
Console.WriteLine($"The temporary test folder is {TempFolder}"); Console.WriteLine($"The temporary test folder is {TempFolder}");
WorkFolder = Path.Combine(TempFolder, "work"); WorkFolder = Path.Combine(TempFolder, "work");
PackagesFolder = Path.Combine(TempFolder, "packages");
CreateTestDirectory(); CreateTestDirectory();
} }
public string TempFolder { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); public string TempFolder { get; } = Path.Combine(Path.GetDirectoryName(FindNugetConfig()), "testWorkDir" , Guid.NewGuid().ToString());
public string WorkFolder { get; } public string WorkFolder { get; }
public string PackagesFolder { get; }
public void AddProject(string projectFolder) public void AddProject(string projectFolder)
{ {
var destinationFolder = Path.Combine(WorkFolder, Path.GetFileName(projectFolder)); var destinationFolder = Path.Combine(WorkFolder, Path.GetFileName(projectFolder));
@ -95,6 +92,8 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
private void CreateTestDirectory() private void CreateTestDirectory()
{ {
Directory.CreateDirectory(WorkFolder); Directory.CreateDirectory(WorkFolder);
File.WriteAllText(Path.Combine(WorkFolder, "global.json"), "{}");
var nugetConfigFilePath = FindNugetConfig(); var nugetConfigFilePath = FindNugetConfig();
var tempNugetConfigFile = Path.Combine(WorkFolder, NugetConfigFileName); var tempNugetConfigFile = Path.Combine(WorkFolder, NugetConfigFileName);
@ -114,7 +113,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
return Process.Start(psi); return Process.Start(psi);
} }
private string FindNugetConfig() private static string FindNugetConfig()
{ {
var currentDirPath = Directory.GetCurrentDirectory(); var currentDirPath = Directory.GetCurrentDirectory();

View File

@ -12,7 +12,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
private readonly FileSystemWatcher _watcher; private readonly FileSystemWatcher _watcher;
private readonly string _expectedFile; private readonly string _expectedFile;
private readonly ManualResetEvent _changed = new ManualResetEvent(false); private ManualResetEvent _changed = new ManualResetEvent(false);
public WaitForFileToChange(string file) public WaitForFileToChange(string file)
{ {
@ -30,23 +30,33 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
if (e.FullPath.Equals(_expectedFile, StringComparison.Ordinal)) if (e.FullPath.Equals(_expectedFile, StringComparison.Ordinal))
{ {
Waiters.WaitForFileToBeReadable(_expectedFile, TimeSpan.FromSeconds(10)); Waiters.WaitForFileToBeReadable(_expectedFile, TimeSpan.FromSeconds(10));
_changed.Set(); _changed?.Set();
} }
} }
public void Wait(TimeSpan timeout, bool expectedToChange, string errorMessage) public void Wait(TimeSpan timeout, bool expectedToChange, string errorMessage)
{ {
var changed = _changed.WaitOne(timeout); if (_changed != null)
if (changed != expectedToChange)
{ {
throw new Exception(errorMessage); var changed = _changed.WaitOne(timeout);
if (changed != expectedToChange)
{
throw new Exception(errorMessage);
}
} }
} }
public void Dispose() public void Dispose()
{ {
_watcher.EnableRaisingEvents = false;
_watcher.Changed -= WatcherEvent;
_watcher.Created -= WatcherEvent;
_watcher.Dispose(); _watcher.Dispose();
_changed.Dispose(); _changed.Dispose();
_changed = null;
} }
} }
} }