public static void FileSystemWatcher_Deleted_FileDeletedInNestedDirectory() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks AutoResetEvent eventOccurred = 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(createOccurred, "create"); using (var secondDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir2"))) { // Wait for the created event Utility.ExpectEvent(createOccurred, "create"); using (var nestedDir = new TemporaryTestFile(Path.Combine(secondDir.Path, "nestedFile"))) { } Utility.ExpectEvent(eventOccurred, "deleted"); } Utility.ExpectEvent(eventOccurred, "deleted"); } Utility.ExpectEvent(eventOccurred, "deleted"); } }
public static void FileSystemWatcher_Created_MoveDirectory() { // create two test directories using (TemporaryTestDirectory dir = Utility.CreateTestDirectory(), targetDir = new TemporaryTestDirectory(dir.Path + "_target")) using (var watcher = new FileSystemWatcher(".")) { string testFileName = "testFile.txt"; // watch the target dir watcher.Path = Path.GetFullPath(targetDir.Path); watcher.Filter = testFileName; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); string sourceFile = Path.Combine(dir.Path, testFileName); string targetFile = Path.Combine(targetDir.Path, testFileName); // create a test file in source File.WriteAllText(sourceFile, "test content"); watcher.EnableRaisingEvents = true; // move the test file from source to target directory File.Move(sourceFile, targetFile); Utility.ExpectEvent(eventOccured, "created"); } }
public static void TestNestedDirectoriesHelper( WatcherChangeTypes change, Action <AutoResetEvent, TemporaryTestDirectory> action, NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks 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"); using (var nestedDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "nested"))) { Utility.ExpectEvent(createdOccured, "nested created"); action(eventOccured, nestedDir); } } } }
static IToposProducer CreateProducer(TemporaryTestDirectory temporaryTestDirectory) { return(Configure .Producer(p => p.UseFileSystem(temporaryTestDirectory)) .Serialization(s => s.UseNewtonsoftJson()) .Create()); }
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); } } } }
public static void FileSystemWatcher_Renamed_Negative() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Renamed); watcher.EnableRaisingEvents = true; // run all scenarios together to avoid unnecessary waits, // assert information is verbose enough to trace to failure cause // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"))) { // change a file testFile.WriteByte(0xFF); testFile.Flush(); // deleting a file & directory by leaving the using block } Utility.ExpectNoEvent(eventOccured, "created"); } }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test checks for when the file being moved has a destination directory that is outside of /// the path of the FileSystemWatcher. /// </summary> private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName())) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) { watcher.EnableRaisingEvents = true; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, eventType); // Move the testFile to a different name in the same directory testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString())); // Test which events are thrown if (moveRaisesEvent) { Utility.ExpectEvent(eventOccured, eventType.ToString()); } else { Utility.ExpectNoEvent(eventOccured, eventType.ToString()); } } } }
static IDisposable StartConsumer(TemporaryTestDirectory testDirectory, ManualResetEvent gotTheEvent) { return(Configure .Consumer("whatever", c => c.UseFileSystem(testDirectory)) .Serialization(s => s.UseNewtonsoftJson()) .Topics(t => t.Subscribe("test-topic")) .Positions(p => p.StoreInMemory()) .Handle(async(messages, context, token) => gotTheEvent.Set()) .Start()); }
static IToposProducer CreateProducer(TemporaryTestDirectory temporaryTestDirectory) { return(Configure .Producer(p => { p.UseFileSystem(temporaryTestDirectory).SetMaxAge("test-topic", TimeSpan.FromSeconds(5)); ChangeCompactionIntervalTo(p, TimeSpan.FromSeconds(1)); }) .Serialization(s => s.UseNewtonsoftJson()) .Create()); }
static IDisposable StartConsumer(TemporaryTestDirectory testDirectory, Action <Timestamp> handle) { return(Configure .Consumer("whatever", c => c.UseFileSystem(testDirectory)) .Serialization(s => s.UseNewtonsoftJson()) .Topics(t => t.Subscribe("test-topic")) .Positions(p => p.StoreInMemory()) .Handle(async(messages, context, token) => { foreach (var message in messages.Select(m => m.Body).OfType <Timestamp>()) { handle(message); } }) .Start()); }
public static void FileSystemWatcher_Created_Directory() { using (var watcher = new FileSystemWatcher(".")) { string dirName = Guid.NewGuid().ToString(); watcher.Filter = dirName; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); watcher.EnableRaisingEvents = true; using (var dir = new TemporaryTestDirectory(dirName)) { Utility.ExpectEvent(eventOccured, "created"); } } }
public static void FileSystemWatcher_Deleted_Directory() { using (var watcher = new FileSystemWatcher(".")) { string dirName = Guid.NewGuid().ToString(); watcher.Filter = dirName; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted); using (var dir = new TemporaryTestDirectory(dirName)) { watcher.EnableRaisingEvents = true; } Utility.ExpectEvent(eventOccured, "deleted"); } }
public static void FileSystemWatcher_Created_DeepDirectoryStructure() { // List of created directories List <TemporaryTestDirectory> lst = new List <TemporaryTestDirectory>(); try { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; // Priming directory TemporaryTestDirectory priming = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir")); lst.Add(priming); Utility.ExpectEvent(eventOccurred, "priming create"); // Create a deep directory structure and expect things to work for (int i = 1; i < 20; i++) { lst.Add(new TemporaryTestDirectory(Path.Combine(lst[i - 1].Path, String.Format("dir{0}", i)))); Utility.ExpectEvent(eventOccurred, lst[i].Path + " create"); } // Put a file at the very bottom and expect it to raise an event using (var file = new TemporaryTestFile(Path.Combine(lst[lst.Count - 1].Path, "temp file"))) { Utility.ExpectEvent(eventOccurred, "temp file create"); } } } finally { // Cleanup foreach (TemporaryTestDirectory d in lst) { d.Dispose(); } } }
// Note: Can't use the TestNestedDirectoriesHelper since we need access to the root public static void FileSystemWatcher_Moved_NestedDirectoryTreeMoveFileAndFolder() { using (var root = Utility.CreateTestDirectory()) using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root"))) using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp"))) using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1"))) using (var watcher = new FileSystemWatcher()) { AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted | WatcherChangeTypes.Changed); watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; string filePath = Path.Combine(dir1.Path, "test_file"); using (var file = File.Create(filePath)) { // Wait for the file to be created then make a change to validate that we get a change Utility.ExpectEvent(eventOccured, "test file created"); byte[] buffer = new byte[4096]; file.Write(buffer, 0, buffer.Length); file.Flush(); } Utility.ExpectEvent(eventOccured, "test file changed"); // Move the nested dir out of scope and validate that we get a single deleted event string original = dir1.Path; string target = Path.Combine(temp.Path, "dir1"); dir1.Move(target); Utility.ExpectEvent(eventOccured, "nested dir deleted"); // Move the dir (and child file) back into scope and validate that we get a created event dir1.Move(original); Utility.ExpectEvent(eventOccured, "nested dir created"); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write)) { byte[] buffer = new byte[4096]; fs.Write(buffer, 0, buffer.Length); fs.Flush(); } Utility.ExpectEvent(eventOccured, "test file changed"); } }
// Note: Can't use the TestNestedDirectoriesHelper since we need access to the root public static void FileSystemWatcher_Moved_NestedDirectoryRoot() { // Create a test root with our watch dir and a temp directory since, on the default Ubuntu install, the system // temp directory is on a different mount point and Directory.Move does not work across mount points. using (var root = Utility.CreateTestDirectory()) using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root"))) using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp"))) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks AutoResetEvent deletedOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted); watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1"))) { Utility.ExpectEvent(createdOccured, "dir1 created"); using (var dir2 = new TemporaryTestDirectory(Path.Combine(dir1.Path, "dir2"))) { Utility.ExpectEvent(createdOccured, "dir2 created"); using (var file = Utility.CreateTestFile(Path.Combine(dir2.Path, "test file"))) { }; // Move the directory out of the watched folder and expect that we get a deleted event string original = dir1.Path; string target = Path.Combine(temp.Path, Path.GetFileName(dir1.Path)); dir1.Move(target); Utility.ExpectEvent(deletedOccured, "dir1 moved out"); // Move the directory back and expect a created event dir1.Move(original); Utility.ExpectEvent(createdOccured, "dir1 moved back"); } } } }
public static void FileSystemWatcher_Created_Negative() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // run all scenarios together to avoid unnecessary waits, // assert information is verbose enough to trace to failure cause using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"))) { // start listening after we've created these watcher.EnableRaisingEvents = true; // change a file testFile.WriteByte(0xFF); testFile.Flush(); // renaming a directory // // We don't do this on Linux because depending on the timing of MOVED_FROM and MOVED_TO events, // a rename can trigger delete + create as a deliberate handling of an edge case, and this // test is checking that no create events are raised. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { testDir.Move(testDir.Path + "_rename"); } // deleting a file & directory by leaving the using block } Utility.ExpectNoEvent(eventOccurred, "created"); } }
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"); } }
public static void FileSystemWatcher_Deleted_Negative() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted); // run all scenarios together to avoid unnecessary waits, // assert information is verbose enough to trace to failure cause watcher.EnableRaisingEvents = true; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) using (var testDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir"))) { // change a file testFile.WriteByte(0xFF); testFile.Flush(); // renaming a directory // // We don't do this on Linux because depending on the timing of MOVED_FROM and MOVED_TO events, // a rename can trigger delete + create as a deliberate handling of an edge case, and this // test is checking that no delete events are raised. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { testDir.Move(testDir.Path + "_rename"); } Utility.ExpectNoEvent(eventOccured, "deleted"); } } }
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); } } } }
/// <summary> /// Sets up watchers for the type given before performing a File.Move operation and checking for /// events. If moveRaisesEvent is true, we make sure that the given event type is observed. If false, /// we ensure that it is not observed. /// /// This test checks for when the file being moved has a destination directory that is outside of /// the path of the FileSystemWatcher. /// </summary> private static void MoveAndCheck_DifferentDirectory(WatcherChangeTypes eventType, bool moveRaisesEvent) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var dir_unwatched = new TemporaryTestDirectory(Path.GetRandomFileName())) using (var watcher = new FileSystemWatcher()) { // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; // create a file using (var testFile = new TemporaryTestFile(Path.Combine(dir.Path, "file"))) { watcher.EnableRaisingEvents = true; AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, eventType); // Move the testFile to a different name in the same directory testFile.Move(Path.Combine(dir_unwatched.Path, testFile.Name + "_" + eventType.ToString())); // Test which events are thrown if (moveRaisesEvent) Utility.ExpectEvent(eventOccurred, eventType.ToString()); else Utility.ExpectNoEvent(eventOccurred, eventType.ToString()); } } }
// Note: Can't use the TestNestedDirectoriesHelper since we need access to the root public static void FileSystemWatcher_Moved_NestedDirectoryRoot() { // Create a test root with our watch dir and a temp directory since, on the default Ubuntu install, the system // temp directory is on a different mount point and Directory.Move does not work across mount points. using (var root = Utility.CreateTestDirectory()) using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root"))) using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp"))) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createdOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks AutoResetEvent deletedOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted); watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1"))) { Utility.ExpectEvent(createdOccurred, "dir1 created"); using (var dir2 = new TemporaryTestDirectory(Path.Combine(dir1.Path, "dir2"))) { Utility.ExpectEvent(createdOccurred, "dir2 created"); using (var file = Utility.CreateTestFile(Path.Combine(dir2.Path, "test file"))) { }; // Move the directory out of the watched folder and expect that we get a deleted event string original = dir1.Path; string target = Path.Combine(temp.Path, Path.GetFileName(dir1.Path)); dir1.Move(target); Utility.ExpectEvent(deletedOccurred, "dir1 moved out"); // Move the directory back and expect a created event dir1.Move(original); Utility.ExpectEvent(createdOccurred, "dir1 moved back"); } } } }
// Note: Can't use the TestNestedDirectoriesHelper since we need access to the root public static void FileSystemWatcher_Moved_NestedDirectoryTreeMoveFileAndFolder() { using (var root = Utility.CreateTestDirectory()) using (var dir = Utility.CreateTestDirectory(Path.Combine(root.Path, "test_root"))) using (var temp = Utility.CreateTestDirectory(Path.Combine(root.Path, "temp"))) using (var dir1 = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1"))) using (var watcher = new FileSystemWatcher()) { AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted | WatcherChangeTypes.Changed); watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; string filePath = Path.Combine(dir1.Path, "test_file"); using (var file = File.Create(filePath)) { // Wait for the file to be created then make a change to validate that we get a change Utility.ExpectEvent(eventOccurred, "test file created"); byte[] buffer = new byte[4096]; file.Write(buffer, 0, buffer.Length); file.Flush(); } Utility.ExpectEvent(eventOccurred, "test file changed"); // Move the nested dir out of scope and validate that we get a single deleted event string original = dir1.Path; string target = Path.Combine(temp.Path, "dir1"); dir1.Move(target); Utility.ExpectEvent(eventOccurred, "nested dir deleted"); // Move the dir (and child file) back into scope and validate that we get a created event dir1.Move(original); Utility.ExpectEvent(eventOccurred, "nested dir created"); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write)) { byte[] buffer = new byte[4096]; fs.Write(buffer, 0, buffer.Length); fs.Flush(); } Utility.ExpectEvent(eventOccurred, "test file changed"); } }
public static void FileSystemWatcher_Deleted_NestedDirectories() { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks 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"); } }
public static void TestNestedDirectoriesHelper( WatcherChangeTypes change, Action<AutoResetEvent, TemporaryTestDirectory> action, NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName) { using (var dir = Utility.CreateTestDirectory(Guid.NewGuid().ToString())) using (var watcher = new FileSystemWatcher()) { AutoResetEvent createdOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // not "using" to avoid race conditions with FSW callbacks AutoResetEvent eventOccurred = 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(createdOccurred, "dir1 created"); using (var nestedDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "nested"))) { Utility.ExpectEvent(createdOccurred, "nested created"); action(eventOccurred, nestedDir); } } } }
public static void FileSystemWatcher_Created_DeepDirectoryStructure() { // List of created directories List<TemporaryTestDirectory> lst = new List<TemporaryTestDirectory>(); try { using (var dir = Utility.CreateTestDirectory()) using (var watcher = new FileSystemWatcher()) { AutoResetEvent eventOccurred = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created); // put everything in our own directory to avoid collisions watcher.Path = Path.GetFullPath(dir.Path); watcher.Filter = "*.*"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; // Priming directory TemporaryTestDirectory priming = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir")); lst.Add(priming); Utility.ExpectEvent(eventOccurred, "priming create"); // Create a deep directory structure and expect things to work for (int i = 1; i < 20; i++) { lst.Add(new TemporaryTestDirectory(Path.Combine(lst[i - 1].Path, String.Format("dir{0}", i)))); Utility.ExpectEvent(eventOccurred, lst[i].Path + " create"); } // Put a file at the very bottom and expect it to raise an event using (var file = new TemporaryTestFile(Path.Combine(lst[lst.Count - 1].Path, "temp file"))) { Utility.ExpectEvent(eventOccurred, "temp file create"); } } } finally { // Cleanup foreach (TemporaryTestDirectory d in lst) d.Dispose(); } }
static void PrintDirectoryDetails(TemporaryTestDirectory testDirectory) {