Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: fix flaky test-fs-watch-encoding on OS X
  • Loading branch information
Trott committed Jun 22, 2016
commit 57e3e412569103616842827abe6ec95db981e660
49 changes: 32 additions & 17 deletions test/parallel/test-fs-watch-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,55 @@ common.refreshTmpDir();
const fn = '新建文夹件.txt';
const a = path.join(common.tmpDir, fn);

const watchers = new Set();

function registerWatcher(watcher) {
watchers.add(watcher);
}

function unregisterWatcher(watcher) {
watcher.close();
watchers.delete(watcher);
if (watchers.size === 0) {
clearInterval(interval);
}
}

const watcher1 = fs.watch(
common.tmpDir,
{encoding: 'hex'},
(event, filename) => {
if (filename)
assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874');
watcher1.close();
console.log(event);
assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874');
unregisterWatcher(watcher1);
}
);
registerWatcher(watcher1);

const watcher2 = fs.watch(
common.tmpDir,
(event, filename) => {
if (filename)
assert.equal(filename, fn);
watcher2.close();
assert.equal(filename, fn);
unregisterWatcher(watcher2);
}
);
registerWatcher(watcher2);

const watcher3 = fs.watch(
common.tmpDir,
{encoding: 'buffer'},
(event, filename) => {
if (filename) {
assert(filename instanceof Buffer);
assert.equal(filename.toString('utf8'), fn);
}
watcher3.close();
assert(filename instanceof Buffer);
assert.equal(filename.toString('utf8'), fn);
unregisterWatcher(watcher3);
}
);
registerWatcher(watcher3);

const fd = fs.openSync(a, 'w+');
fs.closeSync(fd);

process.on('exit', () => {
fs.unlink(a);
});
// OS X and perhaps other systems can have surprising race conditions with
// file events. So repeat the operation in case it is missed the first time.
const interval = setInterval(() => {
const fd = fs.openSync(a, 'w+');
fs.closeSync(fd);
fs.unlinkSync(a);
}, common.platformTimeout(100));