mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Merge pull request dotnet/corefx#2347 from sokket/fsw
Adding new Unit Tests to the FileSystemWatcher code base Commit migrated from https://github.com/dotnet/corefx/commit/305974d03f0984e735a2ea0ce3b72139822397ba
This commit is contained in:
commit
83bfedb80a
7 files changed
+205
-17
No files matched your search
+13
-11
@@ -10,6 +10,9 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace System.IO
|
||||
{
|
||||
// Note: This class has an OS Limitation where the inotify API can miss events if a directory is created and immediately has
|
||||
// changes underneath. This is due to the inotify* APIs not being recursive and needing to call inotify_add_watch on
|
||||
// each subdirectory, causing a race between adding the watch and file system events happening.
|
||||
public partial class FileSystemWatcher
|
||||
{
|
||||
/// <summary>Starts a new watch operation if one is not currently running.</summary>
|
||||
@@ -166,7 +169,7 @@ namespace System.IO
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// State and processing associatd with an active watch operation. This state is kept separate from FileSystemWatcher to avoid
|
||||
/// State and processing associated with an active watch operation. This state is kept separate from FileSystemWatcher to avoid
|
||||
/// race conditions when a user starts/stops/starts/stops/etc. in quick succession, resulting in the potential for multiple
|
||||
/// active operations. It also helps with avoiding rooted cycles and enabling proper finalization.
|
||||
/// </summary>
|
||||
@@ -470,7 +473,6 @@ namespace System.IO
|
||||
}
|
||||
|
||||
uint mask = nextEvent.mask;
|
||||
bool addWatch = false;
|
||||
string expandedName = null;
|
||||
WatchedDirectory associatedDirectoryEntry = null;
|
||||
|
||||
@@ -531,6 +533,15 @@ namespace System.IO
|
||||
previousEventCookie = 0;
|
||||
}
|
||||
|
||||
// If the event signaled that there's a new subdirectory and if we're monitoring subdirectories,
|
||||
// add a watch for it.
|
||||
const Interop.libc.NotifyEvents AddMaskFilters = Interop.libc.NotifyEvents.IN_CREATE | Interop.libc.NotifyEvents.IN_MOVED_TO;
|
||||
bool addWatch = ((mask & (uint)AddMaskFilters) != 0);
|
||||
if (addWatch && isDir && _includeSubdirectories)
|
||||
{
|
||||
AddDirectoryWatch(associatedDirectoryEntry, nextEvent.name);
|
||||
}
|
||||
|
||||
const Interop.libc.NotifyEvents switchMask =
|
||||
Interop.libc.NotifyEvents.IN_Q_OVERFLOW | Interop.libc.NotifyEvents.IN_IGNORED |
|
||||
Interop.libc.NotifyEvents.IN_CREATE | Interop.libc.NotifyEvents.IN_DELETE |
|
||||
@@ -543,7 +554,6 @@ namespace System.IO
|
||||
break;
|
||||
case Interop.libc.NotifyEvents.IN_CREATE:
|
||||
watcher.NotifyFileSystemEventArgs(WatcherChangeTypes.Created, expandedName);
|
||||
addWatch = true;
|
||||
break;
|
||||
case Interop.libc.NotifyEvents.IN_IGNORED:
|
||||
// We're getting an IN_IGNORED because a directory watch was removed.
|
||||
@@ -582,17 +592,9 @@ namespace System.IO
|
||||
previousEventName = null;
|
||||
previousEventParent = null;
|
||||
previousEventCookie = 0;
|
||||
addWatch = true; // for either rename or creation, we need to update our state
|
||||
break;
|
||||
}
|
||||
|
||||
// If the event signaled that there's a new subdirectory and if we're monitoring subdirectories,
|
||||
// add a watch for it.
|
||||
if (addWatch && isDir && _includeSubdirectories)
|
||||
{
|
||||
AddDirectoryWatch(associatedDirectoryEntry, nextEvent.name);
|
||||
}
|
||||
|
||||
// Drop our strong reference to the watcher now that we're potentially going to block again for another read
|
||||
watcher = null;
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public partial class FileSystemWatcher_4000_Tests
|
||||
}
|
||||
|
||||
[Fact, ActiveIssue(2279)]
|
||||
public static void FileSystemWatcher_ChangeWatchedFolder()
|
||||
public static void FileSystemWatcher_Changed_WatchedFolder()
|
||||
{
|
||||
using (var dir = Utility.CreateTestDirectory())
|
||||
using (var watcher = new FileSystemWatcher())
|
||||
@@ -93,4 +93,29 @@ public partial class FileSystemWatcher_4000_Tests
|
||||
Utility.ExpectEvent(eventOccured, "changed");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Changed_NestedDirectories()
|
||||
{
|
||||
Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Changed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
|
||||
{
|
||||
Directory.SetLastAccessTime(ttd.Path, DateTime.Now);
|
||||
Utility.ExpectEvent(are, "changed");
|
||||
},
|
||||
NotifyFilters.DirectoryName | NotifyFilters.LastAccess);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Changed_FileInNestedDirectory()
|
||||
{
|
||||
Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Changed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
|
||||
{
|
||||
using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
|
||||
{
|
||||
Directory.SetLastAccessTime(nestedFile.Path, DateTime.Now);
|
||||
Utility.ExpectEvent(are, "changed");
|
||||
}
|
||||
},
|
||||
NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.FileName);
|
||||
}
|
||||
}
|
||||
@@ -111,4 +111,16 @@ public partial class FileSystemWatcher_4000_Tests
|
||||
Utility.ExpectNoEvent(eventOccured, "created");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Created_FileCreatedInNestedDirectory()
|
||||
{
|
||||
Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Created, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
|
||||
{
|
||||
using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
|
||||
{
|
||||
Utility.ExpectEvent(are, "nested file created", 1000 * 30);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -80,4 +80,78 @@ public partial class FileSystemWatcher_4000_Tests
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Deleted_NestedDirectories()
|
||||
{
|
||||
using (var dir = Utility.CreateTestDirectory())
|
||||
using (var watcher = new FileSystemWatcher())
|
||||
using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
|
||||
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
|
||||
{
|
||||
watcher.Path = Path.GetFullPath(dir.Path);
|
||||
watcher.Filter = "*";
|
||||
watcher.IncludeSubdirectories = true;
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
|
||||
{
|
||||
// Wait for the created event
|
||||
Utility.ExpectEvent(createOccured, "create", 1000 * 30);
|
||||
|
||||
using (var secondDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "dir2")))
|
||||
{
|
||||
// Wait for the created event
|
||||
Utility.ExpectEvent(createOccured, "create", 1000 * 30);
|
||||
|
||||
using (var nestedDir = new TemporaryTestDirectory(Path.Combine(secondDir.Path, "nested")))
|
||||
{
|
||||
// Wait for the created event
|
||||
Utility.ExpectEvent(createOccured, "create", 1000 * 30);
|
||||
}
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Deleted_FileDeletedInNestedDirectory()
|
||||
{
|
||||
using (var dir = Utility.CreateTestDirectory())
|
||||
using (var watcher = new FileSystemWatcher())
|
||||
using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
|
||||
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
|
||||
{
|
||||
watcher.Path = Path.GetFullPath(dir.Path);
|
||||
watcher.Filter = "*";
|
||||
watcher.IncludeSubdirectories = true;
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
|
||||
{
|
||||
// Wait for the created event
|
||||
Utility.ExpectEvent(createOccured, "create", 1000 * 30);
|
||||
|
||||
using (var secondDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir2")))
|
||||
{
|
||||
// Wait for the created event
|
||||
Utility.ExpectEvent(createOccured, "create", 1000 * 30);
|
||||
|
||||
using (var nestedDir = new TemporaryTestFile(Path.Combine(secondDir.Path, "nestedFile"))) { }
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
|
||||
Utility.ExpectEvent(eventOccured, "deleted");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Xunit;
|
||||
@@ -79,4 +78,27 @@ public partial class FileSystemWatcher_4000_Tests
|
||||
Utility.ExpectNoEvent(eventOccured, "created");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Renamed_NestedDirectory()
|
||||
{
|
||||
Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
|
||||
{
|
||||
ttd.Move(ttd.Path + "_2");
|
||||
Utility.ExpectEvent(are, "renamed");
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void FileSystemWatcher_Renamed_FileInNestedDirectory()
|
||||
{
|
||||
Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
|
||||
{
|
||||
using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
|
||||
{
|
||||
nestedFile.Move(nestedFile.Path + "_2");
|
||||
Utility.ExpectEvent(are, "renamed");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ public static class Utility
|
||||
// events are reported asynchronously by the OS, so allow an amount of time for
|
||||
// them to arrive before testing an assertion.
|
||||
public const int Timeout = 500;
|
||||
public const int WaitForCreationTimeoutInMs = 1000 * 30;
|
||||
|
||||
public static TemporaryTestFile CreateTestFile([CallerMemberName] string path = null)
|
||||
{
|
||||
@@ -91,12 +92,42 @@ public static class Utility
|
||||
public static void ExpectEvent(WaitHandle eventOccured, string eventName, int timeout = Utility.Timeout)
|
||||
{
|
||||
string message = String.Format("Didn't observe a {0} event within {1}ms", eventName, timeout);
|
||||
Assert.True(eventOccured.WaitOne(Utility.Timeout), message);
|
||||
Assert.True(eventOccured.WaitOne(timeout), message);
|
||||
}
|
||||
|
||||
public static void ExpectNoEvent(WaitHandle eventOccured, string eventName, int timeout = Utility.Timeout)
|
||||
{
|
||||
string message = String.Format("Should not observe a {0} event", eventName);
|
||||
Assert.False(eventOccured.WaitOne(Utility.Timeout), message);
|
||||
Assert.False(eventOccured.WaitOne(timeout), message);
|
||||
}
|
||||
|
||||
public static void TestNestedDirectoriesHelper(
|
||||
WatcherChangeTypes change,
|
||||
Action<AutoResetEvent, TemporaryTestDirectory> action,
|
||||
NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName)
|
||||
{
|
||||
using (var dir = Utility.CreateTestDirectory())
|
||||
using (var watcher = new FileSystemWatcher())
|
||||
using (AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
|
||||
using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, change))
|
||||
{
|
||||
watcher.Path = Path.GetFullPath(dir.Path);
|
||||
watcher.Filter = "*";
|
||||
watcher.NotifyFilter = changeFilers;
|
||||
watcher.IncludeSubdirectories = true;
|
||||
watcher.EnableRaisingEvents = true;
|
||||
|
||||
using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
|
||||
{
|
||||
Utility.ExpectEvent(createdOccured, "dir1 created", WaitForCreationTimeoutInMs);
|
||||
|
||||
using (var nestedDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "nested")))
|
||||
{
|
||||
Utility.ExpectEvent(createdOccured, "nested created", WaitForCreationTimeoutInMs);
|
||||
|
||||
action(eventOccured, nestedDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user