Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FileSystemWatcher tests to use ManualResetEvent #93

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}

[SkippableFact]
Expand Down
Loading