forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemWatcher.cs
More file actions
251 lines (235 loc) · 10.3 KB
/
FileSystemWatcher.cs
File metadata and controls
251 lines (235 loc) · 10.3 KB
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
251
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using Xunit;
using Xunit.Sdk;
namespace System.IO.Tests
{
public class FileSystemWatcherTests_netstandard17 : FileSystemWatcherTest
{
public class TestSite : ISite
{
public bool designMode;
public bool DesignMode => designMode;
public IComponent Component => null;
public IContainer Container => null;
public string Name { get; set; }
public object GetService(Type serviceType) => null;
}
[Fact]
public void Site_GetSetRoundtrips()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
TestSite site = new TestSite();
Assert.Null(watcher.Site);
watcher.Site = site;
Assert.Equal(site, watcher.Site);
watcher.Site = null;
Assert.Null(watcher.Site);
Assert.False(watcher.EnableRaisingEvents);
}
}
/// <summary>
/// When the FSW Site is set to a nonnull Site with DesignMode enabled, Event raising will be set to true
/// </summary>
[Fact]
public void Site_NonNullSetEnablesRaisingEvents()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
TestSite site = new TestSite() { designMode = true };
watcher.Site = site;
Assert.True(watcher.EnableRaisingEvents);
}
}
[Fact]
public void SynchronizingObject_GetSetRoundtrips()
{
TestISynchronizeInvoke invoker = new TestISynchronizeInvoke() { };
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
Assert.Null(watcher.SynchronizingObject);
watcher.SynchronizingObject = invoker;
Assert.Equal(invoker, watcher.SynchronizingObject);
watcher.SynchronizingObject = null;
Assert.Null(watcher.SynchronizingObject);
}
}
/// <summary>
/// Ensure that the SynchronizingObject is invoked when an event occurs
/// </summary>
[Theory]
[InlineData(WatcherChangeTypes.Changed)]
[InlineData(WatcherChangeTypes.Deleted)]
[InlineData(WatcherChangeTypes.Created)]
public void SynchronizingObject_CalledOnEvent(WatcherChangeTypes expectedChangeType)
{
FileSystemEventHandler dele = (sender, e) => { Assert.Equal(expectedChangeType, e.ChangeType); };
TestISynchronizeInvoke invoker = new TestISynchronizeInvoke() { ExpectedDelegate = dele };
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.SynchronizingObject = invoker;
if (expectedChangeType == WatcherChangeTypes.Created)
{
watcher.Created += dele;
watcher.CallOnCreated(new FileSystemEventArgs(WatcherChangeTypes.Created, "test", "name"));
}
else if (expectedChangeType == WatcherChangeTypes.Deleted)
{
watcher.Deleted += dele;
watcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, "test", "name"));
}
else if (expectedChangeType == WatcherChangeTypes.Changed)
{
watcher.Changed += dele;
watcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, "test", "name"));
}
Assert.True(invoker.BeginInvoke_Called);
}
}
/// <summary>
/// Ensure that the SynchronizingObject is invoked when a Renamed event occurs
/// </summary>
[Fact]
public void SynchronizingObject_CalledOnRenamed()
{
RenamedEventHandler dele = (sender, e) => { Assert.Equal(WatcherChangeTypes.Renamed, e.ChangeType); };
TestISynchronizeInvoke invoker = new TestISynchronizeInvoke() { ExpectedDelegate = dele };
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.SynchronizingObject = invoker;
watcher.Renamed += dele;
watcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, "test", "name", "oldname"));
Assert.True(invoker.BeginInvoke_Called);
}
}
/// <summary>
/// Ensure that the SynchronizingObject is invoked when an Error event occurs
/// </summary>
[Fact]
public void SynchronizingObject_CalledOnError()
{
ErrorEventHandler dele = (sender, e) => { Assert.IsType<FileNotFoundException>(e.GetException()); };
TestISynchronizeInvoke invoker = new TestISynchronizeInvoke() { ExpectedDelegate = dele };
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.SynchronizingObject = invoker;
watcher.Error += dele;
watcher.CallOnError(new ErrorEventArgs(new FileNotFoundException()));
Assert.True(invoker.BeginInvoke_Called);
}
}
/// <summary>
/// Calling BeginInit and EndInit in a loop is fine. If events are enabled, they will start and stop in the loop as well.
/// </summary>
[Fact]
public void BeginEndInit_Repeated()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.BeginInit();
watcher.EndInit();
watcher.BeginInit();
watcher.EndInit();
Assert.False(watcher.EnableRaisingEvents);
}
}
/// <summary>
/// BeginInit followed by a EnableRaisingEvents=true does not cause the watcher to begin.
/// </summary>
[Fact]
public void BeginInit_PausesEnableRaisingEvents()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.Created += (obj, e) => { Assert.Fail("Created event should not occur"); };
watcher.Deleted += (obj, e) => { Assert.Fail("Deleted event should not occur"); };
watcher.BeginInit();
watcher.EnableRaisingEvents = true;
new TempFile(Path.Combine(TestDirectory, GetTestFileName())).Dispose();
Thread.Sleep(WaitForExpectedEventTimeout);
}
}
/// <summary>
/// EndInit will begin EnableRaisingEvents if we previously set EnableRaisingEvents=true
/// </summary>
[Theory]
[InlineData(true)]
[InlineData(false)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/103584", TestPlatforms.Windows)]
public void EndInit_ResumesPausedEnableRaisingEvents(bool setBeforeBeginInit)
{
FileSystemWatcherTest.Execute(() =>
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
if (setBeforeBeginInit)
watcher.EnableRaisingEvents = true;
watcher.BeginInit();
if (!setBeforeBeginInit)
watcher.EnableRaisingEvents = true;
watcher.EndInit();
ExpectEvent(watcher, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted, () => new TempFile(Path.Combine(TestDirectory, GetTestFileName())).Dispose(), null);
}
}, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
}
/// <summary>
/// Stopping events during the initialization period will prevent the watcher from restarting after EndInit()
/// </summary>
[Fact]
public void EndRaisingEventsDuringPause()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.EnableRaisingEvents = true;
watcher.BeginInit();
watcher.EnableRaisingEvents = false;
watcher.EndInit();
new TempFile(Path.Combine(TestDirectory, GetTestFileName())).Dispose();
Thread.Sleep(WaitForExpectedEventTimeout);
}
}
/// <summary>
/// EndInit will not start event raising unless EnableRaisingEvents was set to true.
/// </summary>
[Fact]
public void EndInit_DoesNotEnableEventRaisedEvents()
{
using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
{
watcher.Created += (obj, e) => { Assert.Fail("Created event should not occur"); };
watcher.Deleted += (obj, e) => { Assert.Fail("Deleted event should not occur"); };
watcher.BeginInit();
watcher.EndInit();
new TempFile(Path.Combine(TestDirectory, GetTestFileName())).Dispose();
Thread.Sleep(WaitForExpectedEventTimeout);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
public void DroppedWatcher_Collectible()
{
WeakReference watcher = CreateEnabledWatcher(TestDirectory);
File.Create(GetTestFilePath()).Dispose();
Assert.True(SpinWait.SpinUntil(() =>
{
GC.Collect();
return !watcher.IsAlive;
}, LongWaitTimeout));
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static WeakReference CreateEnabledWatcher(string path)
{
var fsw = new FileSystemWatcher(path);
fsw.Created += (s, e) => { };
fsw.Deleted += (s, e) => { };
fsw.Changed += (s, e) => { };
fsw.Renamed += (s, e) => { };
fsw.Error += (s, e) => { };
fsw.EnableRaisingEvents = true;
return new WeakReference(fsw);
}
}
}