nodejs/test/parallel/test-fs-promises-watch-iterator.js
Philipp Dunkel 5f7dbf45a3
fs: allow correct handling of burst in fs-events with AsyncIterator
PR-URL: https://github.com/nodejs/node/pull/58490
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
2025-06-10 16:27:17 +00:00

58 lines
1.7 KiB
JavaScript

'use strict';
// This tests that when there is a burst of fs watch events, the events
// emitted after the consumer receives the initial event and before the
// control returns back to fs.watch() can be queued up and show up
// in the next iteration.
const common = require('../common');
const { watch, writeFile } = require('fs/promises');
const fs = require('fs');
const assert = require('assert');
const { join } = require('path');
const { setTimeout } = require('timers/promises');
const tmpdir = require('../common/tmpdir');
class WatchTestCase {
constructor(dirName, files) {
this.dirName = dirName;
this.files = files;
}
get dirPath() { return tmpdir.resolve(this.dirName); }
filePath(fileName) { return join(this.dirPath, fileName); }
async run() {
await Promise.all([this.watchFiles(), this.writeFiles()]);
assert(!this.files.length);
}
async watchFiles() {
const watcher = watch(this.dirPath);
for await (const evt of watcher) {
const idx = this.files.indexOf(evt.filename);
if (idx < 0) continue;
this.files.splice(idx, 1);
await setTimeout(common.platformTimeout(100));
if (!this.files.length) break;
}
}
async writeFiles() {
for (const fileName of [...this.files]) {
await writeFile(this.filePath(fileName), Date.now() + fileName.repeat(1e4));
}
await setTimeout(common.platformTimeout(100));
}
}
const kCases = [
// Watch on a directory should callback with a filename on supported systems
new WatchTestCase(
'watch1',
['foo', 'bar', 'baz']
),
];
tmpdir.refresh();
for (const testCase of kCases) {
fs.mkdirSync(testCase.dirPath);
testCase.run().then(common.mustCall());
}