Skip to content

Commit a54e09b

Browse files
committedAug 27, 2020
check if watchers work as expected
1 parent 9ec3868 commit a54e09b

File tree

1 file changed

+145
-1
lines changed

1 file changed

+145
-1
lines changed
 

‎test/Assumption.js

+145-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var TestHelper = require("./helpers/TestHelper");
99
var fixtures = path.join(__dirname, "fixtures");
1010
var testHelper = new TestHelper(fixtures);
1111

12+
const IS_OSX = require("os").platform() === "darwin";
13+
const IS_WIN = require("os").platform() === "win32";
14+
const SUPPORTS_RECURSIVE_WATCHING = IS_OSX || IS_WIN;
15+
1216
describe("Assumption", function() {
1317
this.timeout(10000);
1418
var watcherToClose = null;
@@ -145,6 +149,76 @@ describe("Assumption", function() {
145149
}
146150
});
147151

152+
if (SUPPORTS_RECURSIVE_WATCHING) {
153+
it("should have a file system with correct mtime behavior (fs.watch recursive)", function(done) {
154+
this.timeout(20000);
155+
testHelper.file("a");
156+
var i = 60;
157+
var count = 60;
158+
var before;
159+
var after;
160+
var minDiffBefore = +Infinity;
161+
var maxDiffBefore = -Infinity;
162+
var sumDiffBefore = 0;
163+
var minDiffAfter = +Infinity;
164+
var maxDiffAfter = -Infinity;
165+
var sumDiffAfter = 0;
166+
var watcher = (watcherToClose = fs.watch(fixtures, { recursive: true }));
167+
testHelper.tick(100, function() {
168+
watcher.on("change", function(type, filename) {
169+
const s = fs.statSync(path.join(fixtures, filename));
170+
if (before && after) {
171+
var diffBefore = +s.mtime - before;
172+
if (diffBefore < minDiffBefore) minDiffBefore = diffBefore;
173+
if (diffBefore > maxDiffBefore) maxDiffBefore = diffBefore;
174+
sumDiffBefore += diffBefore;
175+
var diffAfter = +s.mtime - after;
176+
if (diffAfter < minDiffAfter) minDiffAfter = diffAfter;
177+
if (diffAfter > maxDiffAfter) maxDiffAfter = diffAfter;
178+
sumDiffAfter += diffAfter;
179+
before = after = undefined;
180+
if (i-- === 0) {
181+
afterMeasure();
182+
} else {
183+
testHelper.tick(100, checkMtime);
184+
}
185+
}
186+
});
187+
testHelper.tick(100, checkMtime);
188+
});
189+
190+
function checkMtime() {
191+
before = Date.now();
192+
testHelper.file("a");
193+
after = Date.now();
194+
}
195+
196+
function afterMeasure() {
197+
console.log(
198+
"mtime fs.watch({ recursive: true }) accuracy (before): [" +
199+
minDiffBefore +
200+
" ; " +
201+
maxDiffBefore +
202+
"] avg " +
203+
Math.round(sumDiffBefore / count)
204+
);
205+
console.log(
206+
"mtime fs.watch({ recursive: true }) accuracy (after): [" +
207+
minDiffAfter +
208+
" ; " +
209+
maxDiffAfter +
210+
"] avg " +
211+
Math.round(sumDiffAfter / count)
212+
);
213+
minDiffBefore.should.be.aboveOrEqual(-2000);
214+
maxDiffBefore.should.be.below(2000);
215+
minDiffAfter.should.be.aboveOrEqual(-2000);
216+
maxDiffAfter.should.be.below(2000);
217+
done();
218+
}
219+
});
220+
}
221+
148222
it("should not fire events in subdirectories", function(done) {
149223
testHelper.dir("watch-test-directory");
150224
testHelper.tick(500, () => {
@@ -166,7 +240,77 @@ describe("Assumption", function() {
166240
});
167241
});
168242

169-
if (require("os").platform() !== "darwin") {
243+
if (SUPPORTS_RECURSIVE_WATCHING) {
244+
it("should fire events in subdirectories (recursive)", function(done) {
245+
testHelper.dir("watch-test-directory");
246+
testHelper.file("watch-test-directory/watch-test-file");
247+
testHelper.file("watch-test-directory/existing-file");
248+
testHelper.tick(500, () => {
249+
var watcher = (watcherToClose = fs.watch(fixtures, {
250+
recursive: true
251+
}));
252+
const events = [];
253+
watcher.once("change", () => {
254+
testHelper.tick(1000, function() {
255+
events.should.matchAny(/watch-test-directory[/\\]watch-test-file/);
256+
done();
257+
});
258+
});
259+
watcher.on("change", function(type, filename) {
260+
events.push(filename);
261+
});
262+
watcher.on("error", function(err) {
263+
done(err);
264+
done = function() {};
265+
});
266+
testHelper.tick(500, function() {
267+
testHelper.file("watch-test-directory/watch-test-file");
268+
});
269+
});
270+
});
271+
272+
it("should allow to create/close/create recursive watchers", function(done) {
273+
testHelper.dir("watch-test-directory");
274+
testHelper.file("watch-test-directory/watch-test-file");
275+
testHelper.file("watch-test-directory/existing-file");
276+
testHelper.tick(500, () => {
277+
watcherToClose = fs.watch(fixtures, {
278+
recursive: true
279+
});
280+
watcherToClose.close();
281+
watcherToClose = fs.watch(fixtures, {
282+
recursive: true
283+
});
284+
watcherToClose.close();
285+
watcherToClose = fs.watch(fixtures, {
286+
recursive: true
287+
});
288+
watcherToClose.close();
289+
var watcher = (watcherToClose = fs.watch(fixtures, {
290+
recursive: true
291+
}));
292+
const events = [];
293+
watcher.once("change", () => {
294+
testHelper.tick(1000, function() {
295+
events.should.matchAny(/watch-test-directory[/\\]watch-test-file/);
296+
done();
297+
});
298+
});
299+
watcher.on("change", function(type, filename) {
300+
events.push(filename);
301+
});
302+
watcher.on("error", function(err) {
303+
done(err);
304+
done = function() {};
305+
});
306+
testHelper.tick(500, function() {
307+
testHelper.file("watch-test-directory/watch-test-file");
308+
});
309+
});
310+
});
311+
}
312+
313+
if (!IS_OSX) {
170314
it("should detect removed directory", function(done) {
171315
testHelper.dir("watch-test-dir");
172316
testHelper.tick(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.