Skip to content

Commit 5dca9d6

Browse files
authored
Merge pull request #93 from Facepunch/fix/watcher-test-wait-handles
Refactor FileSystemWatcher tests to use ManualResetEvent
2 parents a60abe4 + 68a447b commit 5dca9d6

6 files changed

+36
-98
lines changed

src/Zio.Tests/FileSystems/TestAggregateFileSystem.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ public void TestWatcher()
2525
var fs = GetCommonAggregateFileSystem(out var fs1, out var fs2, out _);
2626
var watcher = fs.Watch("/");
2727

28-
var gotChange1 = false;
29-
var gotChange2 = false;
28+
var change1WaitHandle = new ManualResetEvent(false);
29+
var change2WaitHandle = new ManualResetEvent(false);
30+
3031
watcher.Created += (sender, args) =>
3132
{
3233
if (args.FullPath == "/b/watched.txt")
3334
{
34-
gotChange1 = true;
35+
change1WaitHandle.Set();
3536
}
3637

3738
if (args.FullPath == "/C/watched.txt")
3839
{
39-
gotChange2 = true;
40+
change2WaitHandle.Set();
4041
}
4142
};
4243

@@ -46,10 +47,8 @@ public void TestWatcher()
4647
fs1.WriteAllText("/b/watched.txt", "test");
4748
fs2.WriteAllText("/C/watched.txt", "test");
4849

49-
System.Threading.Thread.Sleep(100);
50-
51-
Assert.True(gotChange1);
52-
Assert.True(gotChange2);
50+
Assert.True(change1WaitHandle.WaitOne(100));
51+
Assert.True(change2WaitHandle.WaitOne(100));
5352
}
5453

5554
[Fact]

src/Zio.Tests/FileSystems/TestFileSystemBase.cs

+21
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,25 @@ void CreateFolderStructure(UPath root)
421421
CreateFolderStructure(UPath.Root);
422422
CreateFolderStructure(UPath.Root / "a");
423423
}
424+
425+
protected void AssertFileCreatedEventDispatched(IFileSystem fs, UPath watchPath, UPath filePath)
426+
{
427+
var watcher = fs.Watch(watchPath);
428+
var waitHandle = new ManualResetEvent(false);
429+
430+
watcher.Created += (_, args) =>
431+
{
432+
if (args.FullPath == filePath)
433+
{
434+
waitHandle.Set();
435+
}
436+
};
437+
438+
watcher.IncludeSubdirectories = true;
439+
watcher.EnableRaisingEvents = true;
440+
441+
fs.WriteAllText(filePath, "test");
442+
443+
Assert.True(waitHandle.WaitOne(100));
444+
}
424445
}

src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs

+1-18
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,9 @@ public void TestCopyFileSystemSubFolder()
4545
public void TestWatcher()
4646
{
4747
var fs = GetCommonMemoryFileSystem();
48-
var watcher = fs.Watch("/a");
49-
50-
var gotChange = false;
51-
watcher.Created += (sender, args) =>
52-
{
53-
if (args.FullPath == "/a/watched.txt")
54-
{
55-
gotChange = true;
56-
}
57-
};
58-
59-
watcher.IncludeSubdirectories = true;
60-
watcher.EnableRaisingEvents = true;
61-
62-
fs.WriteAllText("/a/watched.txt", "test");
63-
System.Threading.Thread.Sleep(100);
64-
Assert.True(gotChange);
48+
AssertFileCreatedEventDispatched(fs, "/a", "/a/watched.txt");
6549
}
6650

67-
6851
[Fact]
6952
public void TestCreatingTopFile()
7053
{

src/Zio.Tests/FileSystems/TestMountFileSystem.cs

+3-51
Original file line numberDiff line numberDiff line change
@@ -31,69 +31,21 @@ public void TestCommonReadWithMounts()
3131
public void TestWatcherOnRoot()
3232
{
3333
var fs = GetCommonMountFileSystemWithMounts();
34-
var watcher = fs.Watch("/");
35-
36-
var gotChange = false;
37-
watcher.Created += (sender, args) =>
38-
{
39-
if (args.FullPath == "/b/watched.txt")
40-
{
41-
gotChange = true;
42-
}
43-
};
44-
45-
watcher.IncludeSubdirectories = true;
46-
watcher.EnableRaisingEvents = true;
47-
48-
fs.WriteAllText("/b/watched.txt", "test");
49-
System.Threading.Thread.Sleep(100);
50-
Assert.True(gotChange);
34+
AssertFileCreatedEventDispatched(fs, "/", "/b/watched.txt");
5135
}
5236

5337
[Fact]
5438
public void TestWatcherOnMount()
5539
{
5640
var fs = GetCommonMountFileSystemWithMounts();
57-
var watcher = fs.Watch("/b");
58-
59-
var gotChange = false;
60-
watcher.Created += (sender, args) =>
61-
{
62-
if (args.FullPath == "/b/watched.txt")
63-
{
64-
gotChange = true;
65-
}
66-
};
67-
68-
watcher.IncludeSubdirectories = true;
69-
watcher.EnableRaisingEvents = true;
70-
71-
fs.WriteAllText("/b/watched.txt", "test");
72-
System.Threading.Thread.Sleep(100);
73-
Assert.True(gotChange);
41+
AssertFileCreatedEventDispatched(fs, "/b", "/b/watched.txt");
7442
}
7543

7644
[Fact]
7745
public void TestWatcherWithBackupOnRoot()
7846
{
7947
var fs = GetCommonMountFileSystemWithOnlyBackup();
80-
var watcher = fs.Watch("/");
81-
82-
var gotChange = false;
83-
watcher.Created += (sender, args) =>
84-
{
85-
if (args.FullPath == "/b/watched.txt")
86-
{
87-
gotChange = true;
88-
}
89-
};
90-
91-
watcher.IncludeSubdirectories = true;
92-
watcher.EnableRaisingEvents = true;
93-
94-
fs.WriteAllText("/b/watched.txt", "test");
95-
System.Threading.Thread.Sleep(100);
96-
Assert.True(gotChange);
48+
AssertFileCreatedEventDispatched(fs, "/", "/b/watched.txt");
9749
}
9850

9951
[Fact]

src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs

+1-17
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,7 @@ public void TestFileSystemInvalidDriveLetterOnWindows()
3535
public void TestWatcher()
3636
{
3737
var fs = GetCommonPhysicalFileSystem();
38-
var watcher = fs.Watch("/a");
39-
40-
var gotChange = false;
41-
watcher.Created += (sender, args) =>
42-
{
43-
if (args.FullPath == "/a/watched.txt")
44-
{
45-
gotChange = true;
46-
}
47-
};
48-
49-
watcher.IncludeSubdirectories = true;
50-
watcher.EnableRaisingEvents = true;
51-
52-
fs.WriteAllText("/a/watched.txt", "test");
53-
System.Threading.Thread.Sleep(100);
54-
Assert.True(gotChange);
38+
AssertFileCreatedEventDispatched(fs, "/a", "/a/watched.txt");
5539
}
5640

5741
[Fact]

src/Zio.Tests/FileSystems/TestSubFileSystem.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,20 @@ public void TestWatcher()
5757
var subFs = fs.GetOrCreateSubFileSystem("/a/b");
5858
var watcher = subFs.Watch("/");
5959

60-
var gotChange = false;
60+
var waitHandle = new ManualResetEvent(false);
6161
watcher.Created += (sender, args) =>
6262
{
6363
if (args.FullPath == "/watched.txt")
6464
{
65-
gotChange = true;
65+
waitHandle.Set();
6666
}
6767
};
6868

6969
watcher.IncludeSubdirectories = true;
7070
watcher.EnableRaisingEvents = true;
7171

7272
fs.WriteAllText("/a/b/watched.txt", "test");
73-
System.Threading.Thread.Sleep(100);
74-
Assert.True(gotChange);
73+
Assert.True(waitHandle.WaitOne(100));
7574
}
7675

7776
[SkippableTheory]

0 commit comments

Comments
 (0)