From cf465b2001fbd7a81ddea8834a95356f7b3bdab4 Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Wed, 13 Apr 2016 12:07:54 -0700 Subject: [PATCH] Fix tests on unix and make tests more reliable on all platforms --- .../FileWatcher/PollingFileWatcher.cs | 15 ++---- test/TestApps/AppWithDeps/Program.cs | 22 ++++++-- test/TestApps/GlobbingApp/Program.cs | 22 ++++++-- test/TestApps/NoDepsApp/Program.cs | 22 ++++++-- .../FileWatcherTests.cs | 51 +++++++++++++++++-- 5 files changed, 107 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs index b7d6866f69..2bc335f76a 100644 --- a/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs +++ b/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs @@ -37,6 +37,9 @@ namespace Microsoft.DotNet.Watcher.Core.Internal _pollingThread = new Thread(new ThreadStart(PollingLoop)); _pollingThread.IsBackground = true; _pollingThread.Name = nameof(PollingFileWatcher); + + CreateKnownFilesSnapshot(); + _pollingThread.Start(); } @@ -55,17 +58,6 @@ namespace Microsoft.DotNet.Watcher.Core.Internal set { EnsureNotDisposed(); - - if (value == true) - { - CreateKnownFilesSnapshot(); - - if (_pollingThread.ThreadState == System.Threading.ThreadState.Unstarted) - { - // Start the loop the first time events are enabled - _pollingThread.Start(); - } - } _raiseEvents = value; } } @@ -124,6 +116,7 @@ namespace Microsoft.DotNet.Watcher.Core.Internal else { var fileMeta = _knownEntities[fullFilePath]; + if (fileMeta.FileInfo.LastWriteTime != f.LastWriteTime) { // File changed diff --git a/test/TestApps/AppWithDeps/Program.cs b/test/TestApps/AppWithDeps/Program.cs index 6c67ed0901..a5aaf90366 100644 --- a/test/TestApps/AppWithDeps/Program.cs +++ b/test/TestApps/AppWithDeps/Program.cs @@ -4,20 +4,36 @@ using System; using System.Diagnostics; using System.IO; +using System.Threading; namespace ConsoleApplication { public class Program { + private static readonly int processId = Process.GetCurrentProcess().Id; + public static void Main(string[] args) { - Console.WriteLine("AppWithDeps started."); + ConsoleWrite("AppWithDeps started."); - var processId = Process.GetCurrentProcess().Id; File.AppendAllLines(args[0], new string[] { $"{processId}" }); File.WriteAllText(args[0] + ".started", ""); - Console.ReadLine(); + Block(); + } + + private static void ConsoleWrite(string text) + { + Console.WriteLine($"[{processId}] {text}"); + } + + private static void Block() + { + while (true) + { + ConsoleWrite("Blocked..."); + Thread.Sleep(1000); + } } } } diff --git a/test/TestApps/GlobbingApp/Program.cs b/test/TestApps/GlobbingApp/Program.cs index ae996205d5..9e89b57a60 100644 --- a/test/TestApps/GlobbingApp/Program.cs +++ b/test/TestApps/GlobbingApp/Program.cs @@ -4,20 +4,36 @@ using System; using System.Diagnostics; using System.IO; +using System.Threading; namespace ConsoleApplication { public class Program { + private static readonly int processId = Process.GetCurrentProcess().Id; + public static void Main(string[] args) { - Console.WriteLine("GlobbingApp started."); + ConsoleWrite("GlobbingApp started."); - var processId = Process.GetCurrentProcess().Id; File.AppendAllLines(args[0], new string[] { $"{processId}" }); File.WriteAllText(args[0] + ".started", ""); - Console.ReadLine(); + Block(); + } + + private static void ConsoleWrite(string text) + { + Console.WriteLine($"[{processId}] {text}"); + } + + private static void Block() + { + while (true) + { + ConsoleWrite("Blocked..."); + Thread.Sleep(1000); + } } } } diff --git a/test/TestApps/NoDepsApp/Program.cs b/test/TestApps/NoDepsApp/Program.cs index 2309fabd26..cbff2063f8 100644 --- a/test/TestApps/NoDepsApp/Program.cs +++ b/test/TestApps/NoDepsApp/Program.cs @@ -4,23 +4,39 @@ using System; using System.Diagnostics; using System.IO; +using System.Threading; namespace ConsoleApplication { public class Program { + private static readonly int processId = Process.GetCurrentProcess().Id; + public static void Main(string[] args) { - Console.WriteLine("NoDepsApp started."); + ConsoleWrite("NoDepsApp started."); - var processId = Process.GetCurrentProcess().Id; File.AppendAllLines(args[0], new string[] { $"{processId}" }); File.WriteAllText(args[0] + ".started", ""); if (args.Length > 1 && args[1] == "--no-exit") { - Console.ReadLine(); + Block(); + } + } + + private static void ConsoleWrite(string text) + { + Console.WriteLine($"[{processId}] {text}"); + } + + private static void Block() + { + while (true) + { + ConsoleWrite("Blocked..."); + Thread.Sleep(1000); } } } diff --git a/test/dotnet-watch.FunctionalTests/FileWatcherTests.cs b/test/dotnet-watch.FunctionalTests/FileWatcherTests.cs index d31cce667d..9a652f4325 100644 --- a/test/dotnet-watch.FunctionalTests/FileWatcherTests.cs +++ b/test/dotnet-watch.FunctionalTests/FileWatcherTests.cs @@ -65,6 +65,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests }; watcher.EnableRaisingEvents = true; + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); @@ -113,10 +117,8 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests }); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public void FileInSubdirectory(bool usePolling) + [Fact] + public void FileInSubdirectory() { UsingTempDirectory(dir => { @@ -127,7 +129,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests File.WriteAllText(testFileFullPath, string.Empty); using (var changedEv = new ManualResetEvent(false)) - using (var watcher = FileWatcherFactory.CreateWatcher(dir, usePolling)) + using (var watcher = FileWatcherFactory.CreateWatcher(dir, true)) { var filesChanged = new HashSet(); @@ -144,6 +146,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests }; watcher.EnableRaisingEvents = true; + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); @@ -169,6 +175,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests watcher.EnableRaisingEvents = false; var testFileFullPath = Path.Combine(dir, "foo"); + + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); File.WriteAllText(testFileFullPath, string.Empty); Assert.False(changedEv.WaitOne(DefaultTimeout / 2)); @@ -192,6 +203,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests } var testFileFullPath = Path.Combine(dir, "foo"); + + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); File.WriteAllText(testFileFullPath, string.Empty); Assert.False(changedEv.WaitOne(DefaultTimeout / 2)); @@ -226,6 +242,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests }; watcher.EnableRaisingEvents = true; + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); + File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); @@ -253,24 +274,44 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests }; watcher.EnableRaisingEvents = true; + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); + var testFileFullPath = Path.Combine(dir, "foo1"); File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.Equal(testFileFullPath, filesChanged.Single()); filesChanged.Clear(); + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); + testFileFullPath = Path.Combine(dir, "foo2"); File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.Equal(testFileFullPath, filesChanged.Single()); filesChanged.Clear(); + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); + testFileFullPath = Path.Combine(dir, "foo3"); File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.Equal(testFileFullPath, filesChanged.Single()); filesChanged.Clear(); + // On Unix the file write time is in 1s increments; + // if we don't wait, there's a chance that the polling + // watcher will not detect the change + Thread.Sleep(1000); + File.WriteAllText(testFileFullPath, string.Empty); Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.Equal(testFileFullPath, filesChanged.Single());