Skip to content

Commit

Permalink
Merge pull request #93 from Facepunch/fix/watcher-test-wait-handles
Browse files Browse the repository at this point in the history
Refactor FileSystemWatcher tests to use ManualResetEvent
  • Loading branch information
xoofx committed Jun 21, 2024
2 parents a60abe4 + 68a447b commit 5dca9d6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 98 deletions.
15 changes: 7 additions & 8 deletions src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand All @@ -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]
Expand Down
21 changes: 21 additions & 0 deletions src/Zio.Tests/FileSystems/TestFileSystemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
19 changes: 1 addition & 18 deletions src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
54 changes: 3 additions & 51 deletions src/Zio.Tests/FileSystems/TestMountFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 1 addition & 17 deletions src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 3 additions & 4 deletions src/Zio.Tests/FileSystems/TestSubFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,20 @@ 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();
}
};

watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;

fs.WriteAllText("/a/b/watched.txt", "test");
System.Threading.Thread.Sleep(100);
Assert.True(gotChange);
Assert.True(waitHandle.WaitOne(100));
}

[SkippableTheory]
Expand Down

0 comments on commit 5dca9d6

Please sign in to comment.