Fix some intermittently failing Desktop FSW tests (dotnet/corefx#15007)

* Fix some intermittently failing Desktop FSW tests

They're failing for misc reasons but mostly are plagued by timeouts. I excluded some actual failures and increased timeouts for the other ones so desktop FSW runs should be clean going forward.

* Make FSW_NotifyFilter test catch any ArgException

This is so it still runs on desktop and isn't just flat out excluded.

* Fix FSW StopCalledOnBackgroundThread test

* fix FSW test


Commit migrated from https://github.com/dotnet/corefx/commit/f548a49b0d40b90b45ef43ea27bd99e16c50676c
This commit is contained in:
Ian Hays authored and GitHub committed 2017-01-11 13:40:01 -08:00
1 parent 7cd94ac449
commit e3733e9157
4 files changed
+38 -36

No files matched your search

@@ -16,20 +16,24 @@ namespace System.IO.Tests
public static void RenamedEventArgs_ctor(WatcherChangeTypes changeType, string directory, string name, string oldName)
{
RenamedEventArgs args = new RenamedEventArgs(changeType, directory, name, oldName);
if (!directory.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
{
directory += Path.DirectorySeparatorChar;
}
Assert.Equal(changeType, args.ChangeType);
Assert.Equal(directory + name, args.FullPath);
Assert.Equal(directory + Path.DirectorySeparatorChar + name, args.FullPath);
Assert.Equal(name, args.Name);
Assert.Equal(directory + oldName, args.OldFullPath);
Assert.Equal(oldName, args.OldName);
}
[Theory]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "OldFullPath on Desktop demands FileIOPermissions which causes failures for invalid paths.")]
[InlineData(WatcherChangeTypes.Changed, "C:", "foo.txt", "bar.txt")]
[InlineData(WatcherChangeTypes.All, "C:", "foo.txt", "bar.txt")]
[InlineData(0, "", "", "")]
[InlineData(0, "", null, null)]
public static void RenamedEventArgs_ctor_OldFullPath(WatcherChangeTypes changeType, string directory, string name, string oldName)
{
RenamedEventArgs args = new RenamedEventArgs(changeType, directory, name, oldName);
Assert.Equal(directory + Path.DirectorySeparatorChar + oldName, args.OldFullPath);
}
[Fact]
public static void RenamedEventArgs_ctor_Invalid()
{
@@ -24,6 +24,7 @@ namespace System.IO.Tests
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Desktop FSW disallows modification of the watched folder.")]
public void FileSystemWatcher_Directory_Changed_WatchedFolder()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
@@ -92,7 +92,7 @@ namespace System.IO.Tests
}
[Fact]
public void FileSystemWatcher_ctor_InvalidStrings()
public void FileSystemWatcher_ctor_NullStrings()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
{
@@ -102,7 +102,15 @@ namespace System.IO.Tests
// Null path
Assert.Throws<ArgumentNullException>("path", () => new FileSystemWatcher(null));
Assert.Throws<ArgumentNullException>("path", () => new FileSystemWatcher(null, "*"));
}
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "On Desktop, these exceptions don't have a parameter")]
public void FileSystemWatcher_ctor_InvalidStrings()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
{
// Empty path
Assert.Throws<ArgumentException>("path", () => new FileSystemWatcher(string.Empty));
Assert.Throws<ArgumentException>("path", () => new FileSystemWatcher(string.Empty, "*"));
@@ -305,13 +313,13 @@ namespace System.IO.Tests
Assert.Equal((NotifyFilters)0, watcher.NotifyFilter);
// These throw InvalidEnumException on desktop, but ArgumentException on K
Assert.Throws<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)(-1));
Assert.Throws<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)int.MinValue);
Assert.Throws<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)int.MaxValue);
Assert.Throws<ArgumentException>(() => watcher.NotifyFilter = allFilters + 1);
Assert.ThrowsAny<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)(-1));
Assert.ThrowsAny<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)int.MinValue);
Assert.ThrowsAny<ArgumentException>(() => watcher.NotifyFilter = (NotifyFilters)int.MaxValue);
Assert.ThrowsAny<ArgumentException>(() => watcher.NotifyFilter = allFilters + 1);
// Simulate a bit added to the flags
Assert.Throws<ArgumentException>(() => watcher.NotifyFilter = allFilters | (NotifyFilters)((int)notifyFilters.Max() << 1));
Assert.ThrowsAny<ArgumentException>(() => watcher.NotifyFilter = allFilters | (NotifyFilters)((int)notifyFilters.Max() << 1));
}
[Fact]
@@ -584,32 +592,21 @@ namespace System.IO.Tests
// a FSW event callback and make sure we don't Thread.Join to deadlock
using (var dir = new TempDirectory(GetTestFilePath()))
{
FileSystemWatcher watcher = new FileSystemWatcher();
string filePath = Path.Combine(dir.Path, "testfile.txt");
File.Create(filePath).Dispose();
AutoResetEvent are = new AutoResetEvent(false);
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetFullPath(dir.Path), "*");
FileSystemEventHandler callback = (sender, arg) =>
{
watcher.Dispose();
are.Set();
};
// Attach the FSW to the existing structure
watcher.Path = Path.GetFullPath(dir.Path);
watcher.Filter = "*";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
watcher.Changed += callback;
using (var file = File.Create(Path.Combine(dir.Path, "testfile.txt")))
{
watcher.EnableRaisingEvents = true;
// Change the nested file and verify we get the changed event
byte[] bt = new byte[4096];
file.Write(bt, 0, bt.Length);
file.Flush();
}
ExpectEvent(are, "deleted");
watcher.EnableRaisingEvents = true;
File.SetLastWriteTime(filePath, File.GetLastWriteTime(filePath).AddDays(1));
Assert.True(are.WaitOne(10000));
Assert.Throws<ObjectDisposedException>(() => watcher.EnableRaisingEvents = true);
}
}
@@ -20,7 +20,7 @@ namespace System.IO.Tests
// to keep the timeout short, as in a successful run we'll end up waiting for
// the entire timeout specified.
public const int WaitForExpectedEventTimeout = 500; // ms to wait for an event to happen
public const int LongWaitTimeout = 10000; // ms to wait for an event that takes a longer time than the average operation
public const int LongWaitTimeout = 50000; // ms to wait for an event that takes a longer time than the average operation
public const int SubsequentExpectedWait = 10; // ms to wait for checks that occur after the first.
public const int WaitForExpectedEventTimeout_NoRetry = 3000;// ms to wait for an event that isn't surrounded by a retry.
public const int DefaultAttemptsForExpectedEvent = 3; // Number of times an expected event should be retried if failing.
@@ -196,7 +196,7 @@ namespace System.IO.Tests
/// <param name="assertExpected">True if results should be asserted. Used if there is no retry.</param>
/// <param name="expectedPath"> Adds path verification to all expected events.</param>
/// <returns>True if the events raised correctly; else, false.</returns>
private static bool ExecuteAndVerifyEvents(FileSystemWatcher watcher, WatcherChangeTypes expectedEvents, Action action, bool assertExpected, string[] expectedPaths, int timeout)
public static bool ExecuteAndVerifyEvents(FileSystemWatcher watcher, WatcherChangeTypes expectedEvents, Action action, bool assertExpected, string[] expectedPaths, int timeout)
{
bool result = true, verifyChanged = true, verifyCreated = true, verifyDeleted = true, verifyRenamed = true;
AutoResetEvent changed = null, created = null, deleted = null, renamed = null;