-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
FileSystemWatcher.WaitForChanged.cs
250 lines (230 loc) · 10.3 KB
/
FileSystemWatcher.WaitForChanged.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
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 partial class WaitForChangedTests : FileSystemWatcherTest
{
private const int BetweenOperationsDelayMilliseconds = 100;
[Fact]
public static void WaitForChangedResult_DefaultValues()
{
var result = new WaitForChangedResult();
Assert.Equal((WatcherChangeTypes)0, result.ChangeType);
Assert.Null(result.Name);
Assert.Null(result.OldName);
Assert.False(result.TimedOut);
}
[Theory]
[InlineData(WatcherChangeTypes.All)]
[InlineData(WatcherChangeTypes.Changed)]
[InlineData(WatcherChangeTypes.Created)]
[InlineData(WatcherChangeTypes.Deleted)]
[InlineData(WatcherChangeTypes.Renamed)]
[InlineData(WatcherChangeTypes.Changed | WatcherChangeTypes.Created)]
[InlineData(WatcherChangeTypes.Deleted | WatcherChangeTypes.Renamed)]
[InlineData((WatcherChangeTypes)0)]
[InlineData((WatcherChangeTypes)int.MinValue)]
[InlineData((WatcherChangeTypes)int.MaxValue)]
public static void WaitForChangedResult_ChangeType_Roundtrip(WatcherChangeTypes changeType)
{
var result = new WaitForChangedResult();
result.ChangeType = changeType;
Assert.Equal(changeType, result.ChangeType);
}
[Theory]
[InlineData("")]
[InlineData("myfile.txt")]
[InlineData(" ")]
[InlineData(" myfile.txt ")]
public static void WaitForChangedResult_Name_Roundtrip(string name)
{
var result = new WaitForChangedResult();
result.Name = name;
Assert.Equal(name, result.Name);
}
[Theory]
[InlineData("")]
[InlineData("myfile.txt")]
[InlineData(" ")]
[InlineData(" myfile.txt ")]
public static void WaitForChangedResult_OldName_Roundtrip(string name)
{
var result = new WaitForChangedResult();
result.OldName = name;
Assert.Equal(name, result.OldName);
}
[Fact]
public static void WaitForChangedResult_TimedOut_Roundtrip()
{
var result = new WaitForChangedResult();
result.TimedOut = true;
Assert.True(result.TimedOut);
result.TimedOut = false;
Assert.False(result.TimedOut);
result.TimedOut = true;
Assert.True(result.TimedOut);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void ZeroTimeout_TimesOut(bool enabledBeforeWait)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
if (enabledBeforeWait) fsw.EnableRaisingEvents = true;
AssertTimedOut(fsw.WaitForChanged(WatcherChangeTypes.All, 0));
Assert.Equal(enabledBeforeWait, fsw.EnableRaisingEvents);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void NonZeroTimeout_NoEvents_TimesOut(bool enabledBeforeWait)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
if (enabledBeforeWait) fsw.EnableRaisingEvents = true;
AssertTimedOut(fsw.WaitForChanged(0, 1));
Assert.Equal(enabledBeforeWait, fsw.EnableRaisingEvents);
}
}
[Theory]
[InlineData(WatcherChangeTypes.Deleted, false)]
[InlineData(WatcherChangeTypes.Created, true)]
[InlineData(WatcherChangeTypes.Changed, false)]
[InlineData(WatcherChangeTypes.Renamed, true)]
[InlineData(WatcherChangeTypes.All, true)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/58418", typeof(PlatformDetection), nameof(PlatformDetection.IsMacCatalyst), nameof(PlatformDetection.IsArm64Process))]
public void NonZeroTimeout_NoActivity_TimesOut(WatcherChangeTypes changeType, bool enabledBeforeWait)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var dir = new TempDirectory(Path.Combine(testDirectory.Path, GetTestFileName())))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
if (enabledBeforeWait) fsw.EnableRaisingEvents = true;
AssertTimedOut(fsw.WaitForChanged(changeType, 1));
Assert.Equal(enabledBeforeWait, fsw.EnableRaisingEvents);
}
}
[Theory]
[OuterLoop("This test has a longer than average timeout and may fail intermittently")]
[InlineData(WatcherChangeTypes.Created)]
[InlineData(WatcherChangeTypes.Deleted)]
public void CreatedDeleted_Success(WatcherChangeTypes changeType)
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
for (int i = 1; i <= DefaultAttemptsForExpectedEvent; i++)
{
Task<WaitForChangedResult> t = Task.Run(() => fsw.WaitForChanged(changeType, LongWaitTimeout));
while (!t.IsCompleted)
{
string path = Path.Combine(testDirectory.Path, Path.GetRandomFileName());
File.WriteAllText(path, "text");
Task.Delay(BetweenOperationsDelayMilliseconds).Wait();
if ((changeType & WatcherChangeTypes.Deleted) != 0)
{
File.Delete(path);
}
}
try
{
Assert.Equal(TaskStatus.RanToCompletion, t.Status);
Assert.Equal(changeType, t.Result.ChangeType);
Assert.NotNull(t.Result.Name);
Assert.Null(t.Result.OldName);
Assert.False(t.Result.TimedOut);
}
catch when (i < DefaultAttemptsForExpectedEvent)
{
continue;
}
return;
}
}
}
[Fact]
[OuterLoop("This test has a longer than average timeout and may fail intermittently")]
public void Changed_Success()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
for (int i = 1; i <= DefaultAttemptsForExpectedEvent; i++)
{
string name = Path.Combine(testDirectory.Path, Path.GetRandomFileName());
File.Create(name).Dispose();
Task<WaitForChangedResult> t = Task.Run(() => fsw.WaitForChanged(WatcherChangeTypes.Changed, LongWaitTimeout));
while (!t.IsCompleted)
{
File.AppendAllText(name, "text");
Task.Delay(BetweenOperationsDelayMilliseconds).Wait();
}
try
{
Assert.Equal(TaskStatus.RanToCompletion, t.Status);
Assert.Equal(WatcherChangeTypes.Changed, t.Result.ChangeType);
Assert.NotNull(t.Result.Name);
Assert.Null(t.Result.OldName);
Assert.False(t.Result.TimedOut);
}
catch when (i < DefaultAttemptsForExpectedEvent)
{
continue;
}
return;
}
}
}
[Fact]
[OuterLoop("This test has a longer than average timeout and may fail intermittently")]
public void Renamed_Success()
{
using (var testDirectory = new TempDirectory(GetTestFilePath()))
using (var fsw = new FileSystemWatcher(testDirectory.Path))
{
for (int i = 1; i <= DefaultAttemptsForExpectedEvent; i++)
{
Task<WaitForChangedResult> t = Task.Run(() =>
fsw.WaitForChanged(WatcherChangeTypes.Renamed | WatcherChangeTypes.Created, LongWaitTimeout)); // on some OSes, the renamed might come through as Deleted/Created
string name = Path.Combine(testDirectory.Path, Path.GetRandomFileName());
File.Create(name).Dispose();
while (!t.IsCompleted)
{
string newName = Path.Combine(testDirectory.Path, Path.GetRandomFileName());
File.Move(name, newName);
name = newName;
Task.Delay(BetweenOperationsDelayMilliseconds).Wait();
}
try
{
Assert.Equal(TaskStatus.RanToCompletion, t.Status);
Assert.True(t.Result.ChangeType == WatcherChangeTypes.Created || t.Result.ChangeType == WatcherChangeTypes.Renamed);
Assert.NotNull(t.Result.Name);
Assert.False(t.Result.TimedOut);
}
catch when (i < DefaultAttemptsForExpectedEvent)
{
continue;
}
return;
}
}
}
private static void AssertTimedOut(WaitForChangedResult result)
{
Assert.Equal(0, (int)result.ChangeType);
Assert.Null(result.Name);
Assert.Null(result.OldName);
Assert.True(result.TimedOut);
}
}
}