diff --git a/src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs b/src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs index b8ca602..9dfb4c7 100644 --- a/src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs @@ -25,18 +25,19 @@ public void TestWatcher() var fs = GetCommonAggregateFileSystem(out var fs1, out var fs2, out _); var watcher = fs.Watch("/"); - var gotChange1 = false; - var gotChange2 = false; + var change1WaitHandle = new ManualResetEvent(false); + var change2WaitHandle = new ManualResetEvent(false); + watcher.Created += (sender, args) => { if (args.FullPath == "/b/watched.txt") { - gotChange1 = true; + change1WaitHandle.Set(); } if (args.FullPath == "/C/watched.txt") { - gotChange2 = true; + change2WaitHandle.Set(); } }; @@ -46,10 +47,8 @@ public void TestWatcher() fs1.WriteAllText("/b/watched.txt", "test"); fs2.WriteAllText("/C/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - - Assert.True(gotChange1); - Assert.True(gotChange2); + Assert.True(change1WaitHandle.WaitOne(100)); + Assert.True(change2WaitHandle.WaitOne(100)); } [Fact] diff --git a/src/Zio.Tests/FileSystems/TestFileSystemBase.cs b/src/Zio.Tests/FileSystems/TestFileSystemBase.cs index c0622d8..c89313e 100644 --- a/src/Zio.Tests/FileSystems/TestFileSystemBase.cs +++ b/src/Zio.Tests/FileSystems/TestFileSystemBase.cs @@ -421,4 +421,25 @@ void CreateFolderStructure(UPath root) CreateFolderStructure(UPath.Root); CreateFolderStructure(UPath.Root / "a"); } + + protected void AssertFileCreatedEventDispatched(IFileSystem fs, UPath watchPath, UPath filePath) + { + var watcher = fs.Watch(watchPath); + var waitHandle = new ManualResetEvent(false); + + watcher.Created += (_, args) => + { + if (args.FullPath == filePath) + { + waitHandle.Set(); + } + }; + + watcher.IncludeSubdirectories = true; + watcher.EnableRaisingEvents = true; + + fs.WriteAllText(filePath, "test"); + + Assert.True(waitHandle.WaitOne(100)); + } } \ No newline at end of file diff --git a/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs b/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs index 93ca47b..8ec0028 100644 --- a/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs @@ -45,26 +45,9 @@ public void TestCopyFileSystemSubFolder() public void TestWatcher() { var fs = GetCommonMemoryFileSystem(); - var watcher = fs.Watch("/a"); - - var gotChange = false; - watcher.Created += (sender, args) => - { - if (args.FullPath == "/a/watched.txt") - { - gotChange = true; - } - }; - - watcher.IncludeSubdirectories = true; - watcher.EnableRaisingEvents = true; - - fs.WriteAllText("/a/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + AssertFileCreatedEventDispatched(fs, "/a", "/a/watched.txt"); } - [Fact] public void TestCreatingTopFile() { diff --git a/src/Zio.Tests/FileSystems/TestMountFileSystem.cs b/src/Zio.Tests/FileSystems/TestMountFileSystem.cs index 78676f0..f2c8baf 100644 --- a/src/Zio.Tests/FileSystems/TestMountFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestMountFileSystem.cs @@ -31,69 +31,21 @@ public void TestCommonReadWithMounts() public void TestWatcherOnRoot() { var fs = GetCommonMountFileSystemWithMounts(); - var watcher = fs.Watch("/"); - - var gotChange = false; - watcher.Created += (sender, args) => - { - if (args.FullPath == "/b/watched.txt") - { - gotChange = true; - } - }; - - watcher.IncludeSubdirectories = true; - watcher.EnableRaisingEvents = true; - - fs.WriteAllText("/b/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + AssertFileCreatedEventDispatched(fs, "/", "/b/watched.txt"); } [Fact] public void TestWatcherOnMount() { var fs = GetCommonMountFileSystemWithMounts(); - var watcher = fs.Watch("/b"); - - var gotChange = false; - watcher.Created += (sender, args) => - { - if (args.FullPath == "/b/watched.txt") - { - gotChange = true; - } - }; - - watcher.IncludeSubdirectories = true; - watcher.EnableRaisingEvents = true; - - fs.WriteAllText("/b/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + AssertFileCreatedEventDispatched(fs, "/b", "/b/watched.txt"); } [Fact] public void TestWatcherWithBackupOnRoot() { var fs = GetCommonMountFileSystemWithOnlyBackup(); - var watcher = fs.Watch("/"); - - var gotChange = false; - watcher.Created += (sender, args) => - { - if (args.FullPath == "/b/watched.txt") - { - gotChange = true; - } - }; - - watcher.IncludeSubdirectories = true; - watcher.EnableRaisingEvents = true; - - fs.WriteAllText("/b/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + AssertFileCreatedEventDispatched(fs, "/", "/b/watched.txt"); } [Fact] diff --git a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs index 5f12f9d..abd40fd 100644 --- a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs @@ -35,23 +35,7 @@ public void TestFileSystemInvalidDriveLetterOnWindows() public void TestWatcher() { var fs = GetCommonPhysicalFileSystem(); - var watcher = fs.Watch("/a"); - - var gotChange = false; - watcher.Created += (sender, args) => - { - if (args.FullPath == "/a/watched.txt") - { - gotChange = true; - } - }; - - watcher.IncludeSubdirectories = true; - watcher.EnableRaisingEvents = true; - - fs.WriteAllText("/a/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + AssertFileCreatedEventDispatched(fs, "/a", "/a/watched.txt"); } [Fact] diff --git a/src/Zio.Tests/FileSystems/TestSubFileSystem.cs b/src/Zio.Tests/FileSystems/TestSubFileSystem.cs index 620789f..2a31557 100644 --- a/src/Zio.Tests/FileSystems/TestSubFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestSubFileSystem.cs @@ -57,12 +57,12 @@ public void TestWatcher() var subFs = fs.GetOrCreateSubFileSystem("/a/b"); var watcher = subFs.Watch("/"); - var gotChange = false; + var waitHandle = new ManualResetEvent(false); watcher.Created += (sender, args) => { if (args.FullPath == "/watched.txt") { - gotChange = true; + waitHandle.Set(); } }; @@ -70,8 +70,7 @@ public void TestWatcher() watcher.EnableRaisingEvents = true; fs.WriteAllText("/a/b/watched.txt", "test"); - System.Threading.Thread.Sleep(100); - Assert.True(gotChange); + Assert.True(waitHandle.WaitOne(100)); } [SkippableFact]