-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathFileSystemWatcher.File.Create.cs
145 lines (124 loc) · 6.16 KB
/
FileSystemWatcher.File.Create.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/34583", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public class File_Create_Tests : FileSystemWatcherTest
{
[Fact]
public void FileSystemWatcher_File_Create()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var watcher = new FileSystemWatcher(testDirectory.Path))
{
string fileName = Path.Combine(testDirectory.Path, "file");
watcher.Filter = Path.GetFileName(fileName);
Action action = () => File.Create(fileName).Dispose();
Action cleanup = () => File.Delete(fileName);
ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, fileName);
}
}
[Fact]
[OuterLoop]
public void FileSystemWatcher_File_Create_EnablingDisablingNotAffectRaisingEvent()
{
ExecuteWithRetry(() =>
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var watcher = new FileSystemWatcher(testDirectory.Path))
{
string fileName = Path.Combine(testDirectory.Path, "file");
watcher.Filter = Path.GetFileName(fileName);
int numberOfRaisedEvents = 0;
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
FileSystemEventHandler handler = (o, e) =>
{
Interlocked.Increment(ref numberOfRaisedEvents);
autoResetEvent.Set();
};
watcher.Created += handler;
for (int i = 0; i < 100; i++)
{
watcher.EnableRaisingEvents = true;
watcher.EnableRaisingEvents = false;
}
watcher.EnableRaisingEvents = true;
// this should raise one and only one event
File.Create(fileName).Dispose();
Assert.True(autoResetEvent.WaitOne(WaitForExpectedEventTimeout_NoRetry));
Assert.False(autoResetEvent.WaitOne(SubsequentExpectedWait));
Assert.True(numberOfRaisedEvents == 1);
}
});
}
[Fact]
public void FileSystemWatcher_File_Create_ForcedRestart()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var watcher = new FileSystemWatcher(testDirectory.Path))
{
string fileName = Path.Combine(testDirectory.Path, "file");
watcher.Filter = Path.GetFileName(fileName);
Action action = () =>
{
watcher.NotifyFilter = NotifyFilters.FileName; // change filter to force restart
File.Create(fileName).Dispose();
};
Action cleanup = () => File.Delete(fileName);
ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, fileName);
}
}
[Fact]
public void FileSystemWatcher_File_Create_InNestedDirectory()
{
using (var dir = new TempDirectory(GetTestFilePath()))
using (var firstDir = new TempDirectory(Path.Combine(dir.Path, "dir1")))
using (var nestedDir = new TempDirectory(Path.Combine(firstDir.Path, "nested")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.FileName;
string fileName = Path.Combine(nestedDir.Path, "file");
Action action = () => File.Create(fileName).Dispose();
Action cleanup = () => File.Delete(fileName);
ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, fileName);
}
}
[Fact]
[OuterLoop("This test has a longer than average timeout and may fail intermittently")]
public void FileSystemWatcher_File_Create_DeepDirectoryStructure()
{
using (var dir = new TempDirectory(GetTestFilePath()))
using (var deepDir = new TempDirectory(Path.Combine(dir.Path, "dir", "dir", "dir", "dir", "dir", "dir", "dir")))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.FileName;
// Put a file at the very bottom and expect it to raise an event
string fileName = Path.Combine(deepDir.Path, "file");
Action action = () => File.Create(fileName).Dispose();
Action cleanup = () => File.Delete(fileName);
ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, fileName, LongWaitTimeout);
}
}
[ConditionalFact(nameof(CanCreateSymbolicLinks))]
public void FileSystemWatcher_File_Create_SymLink()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, "dir")))
using (var temp = new TempFile(GetTestFilePath()))
using (var watcher = new FileSystemWatcher(dir.Path, "*"))
{
// Make the symlink in our path (to the temp file) and make sure an event is raised
string symLinkPath = Path.Combine(dir.Path, Path.GetFileName(temp.Path));
Action action = () => Assert.True(CreateSymLink(temp.Path, symLinkPath, false));
Action cleanup = () => File.Delete(symLinkPath);
ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, symLinkPath);
}
}
}
}