Fix tests on unix and make tests more reliable on all platforms

This commit is contained in:
Victor Hurdugaci 2016-04-13 12:07:54 -07:00
parent e054eac3bd
commit cf465b2001
5 changed files with 107 additions and 25 deletions

View File

@ -37,6 +37,9 @@ namespace Microsoft.DotNet.Watcher.Core.Internal
_pollingThread = new Thread(new ThreadStart(PollingLoop)); _pollingThread = new Thread(new ThreadStart(PollingLoop));
_pollingThread.IsBackground = true; _pollingThread.IsBackground = true;
_pollingThread.Name = nameof(PollingFileWatcher); _pollingThread.Name = nameof(PollingFileWatcher);
CreateKnownFilesSnapshot();
_pollingThread.Start(); _pollingThread.Start();
} }
@ -55,17 +58,6 @@ namespace Microsoft.DotNet.Watcher.Core.Internal
set set
{ {
EnsureNotDisposed(); 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; _raiseEvents = value;
} }
} }
@ -124,6 +116,7 @@ namespace Microsoft.DotNet.Watcher.Core.Internal
else else
{ {
var fileMeta = _knownEntities[fullFilePath]; var fileMeta = _knownEntities[fullFilePath];
if (fileMeta.FileInfo.LastWriteTime != f.LastWriteTime) if (fileMeta.FileInfo.LastWriteTime != f.LastWriteTime)
{ {
// File changed // File changed

View File

@ -4,20 +4,36 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading;
namespace ConsoleApplication namespace ConsoleApplication
{ {
public class Program public class Program
{ {
private static readonly int processId = Process.GetCurrentProcess().Id;
public static void Main(string[] args) 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.AppendAllLines(args[0], new string[] { $"{processId}" });
File.WriteAllText(args[0] + ".started", ""); 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);
}
} }
} }
} }

View File

@ -4,20 +4,36 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading;
namespace ConsoleApplication namespace ConsoleApplication
{ {
public class Program public class Program
{ {
private static readonly int processId = Process.GetCurrentProcess().Id;
public static void Main(string[] args) 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.AppendAllLines(args[0], new string[] { $"{processId}" });
File.WriteAllText(args[0] + ".started", ""); 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);
}
} }
} }
} }

View File

@ -4,23 +4,39 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading;
namespace ConsoleApplication namespace ConsoleApplication
{ {
public class Program public class Program
{ {
private static readonly int processId = Process.GetCurrentProcess().Id;
public static void Main(string[] args) 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.AppendAllLines(args[0], new string[] { $"{processId}" });
File.WriteAllText(args[0] + ".started", ""); File.WriteAllText(args[0] + ".started", "");
if (args.Length > 1 && args[1] == "--no-exit") 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);
} }
} }
} }

View File

@ -65,6 +65,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
}; };
watcher.EnableRaisingEvents = true; 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
@ -113,10 +117,8 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
}); });
} }
[Theory] [Fact]
[InlineData(true)] public void FileInSubdirectory()
[InlineData(false)]
public void FileInSubdirectory(bool usePolling)
{ {
UsingTempDirectory(dir => UsingTempDirectory(dir =>
{ {
@ -127,7 +129,7 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
File.WriteAllText(testFileFullPath, string.Empty); File.WriteAllText(testFileFullPath, string.Empty);
using (var changedEv = new ManualResetEvent(false)) using (var changedEv = new ManualResetEvent(false))
using (var watcher = FileWatcherFactory.CreateWatcher(dir, usePolling)) using (var watcher = FileWatcherFactory.CreateWatcher(dir, true))
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
@ -144,6 +146,10 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
}; };
watcher.EnableRaisingEvents = true; 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
@ -169,6 +175,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
watcher.EnableRaisingEvents = false; watcher.EnableRaisingEvents = false;
var testFileFullPath = Path.Combine(dir, "foo"); 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.False(changedEv.WaitOne(DefaultTimeout / 2)); Assert.False(changedEv.WaitOne(DefaultTimeout / 2));
@ -192,6 +203,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
} }
var testFileFullPath = Path.Combine(dir, "foo"); 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.False(changedEv.WaitOne(DefaultTimeout / 2)); Assert.False(changedEv.WaitOne(DefaultTimeout / 2));
@ -226,6 +242,11 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
}; };
watcher.EnableRaisingEvents = true; 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
@ -253,24 +274,44 @@ namespace Microsoft.DotNet.Watcher.FunctionalTests
}; };
watcher.EnableRaisingEvents = true; 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"); var testFileFullPath = Path.Combine(dir, "foo1");
File.WriteAllText(testFileFullPath, string.Empty); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single()); Assert.Equal(testFileFullPath, filesChanged.Single());
filesChanged.Clear(); 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"); testFileFullPath = Path.Combine(dir, "foo2");
File.WriteAllText(testFileFullPath, string.Empty); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single()); Assert.Equal(testFileFullPath, filesChanged.Single());
filesChanged.Clear(); 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"); testFileFullPath = Path.Combine(dir, "foo3");
File.WriteAllText(testFileFullPath, string.Empty); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single()); Assert.Equal(testFileFullPath, filesChanged.Single());
filesChanged.Clear(); 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); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single()); Assert.Equal(testFileFullPath, filesChanged.Single());