Skip to content

Commit

Permalink
test: fix flaky test-fs-watch-encoding on OS X
Browse files Browse the repository at this point in the history
PR-URL: #7356
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
Trott committed Jun 23, 2016
1 parent 6a94383 commit 5267f29
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions test/parallel/test-fs-watch-encoding.js
@@ -1,54 +1,75 @@
'use strict'; 'use strict';


// This test is a bit more complicated than it ideally needs to be to work
// around issues on OS X and SmartOS.
//
// On OS X, watch events are subject to peculiar timing oddities such that an
// event might fire out of order. The synchronous refreshing of the tmp
// directory might trigger an event on the watchers that are instantiated after
// it!
//
// On SmartOS, the watch events fire but the filename is null.

const common = require('../common'); const common = require('../common');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const assert = require('assert');

if (common.isFreeBSD) {
common.skip('Test currently not working on FreeBSD');
return;
}


common.refreshTmpDir(); common.refreshTmpDir();


const fn = '新建文夹件.txt'; const fn = '新建文夹件.txt';
const a = path.join(common.tmpDir, fn); 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( const watcher1 = fs.watch(
common.tmpDir, common.tmpDir,
{encoding: 'hex'}, {encoding: 'hex'},
(event, filename) => { (event, filename) => {
if (filename) if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename))
assert.equal(filename, 'e696b0e5bbbae69687e5a4b9e4bbb62e747874'); done(watcher1);
watcher1.close();
} }
); );
registerWatcher(watcher1);


const watcher2 = fs.watch( const watcher2 = fs.watch(
common.tmpDir, common.tmpDir,
(event, filename) => { (event, filename) => {
if (filename) if ([fn, null].includes(filename))
assert.equal(filename, fn); done(watcher2);
watcher2.close();
} }
); );
registerWatcher(watcher2);


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


const fd = fs.openSync(a, 'w+'); const done = common.mustCall(unregisterWatcher, watchers.size);
fs.closeSync(fd);


process.on('exit', () => { // OS X and perhaps other systems can have surprising race conditions with
fs.unlink(a); // 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));

0 comments on commit 5267f29

Please sign in to comment.