diff options
author | Rich Trott <rtrott@gmail.com> | 2016-06-21 17:16:19 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2016-06-23 16:58:03 -0700 |
commit | 5267f29b3445d608e2458087a5724820aa996930 (patch) | |
tree | 4cfe7cc76952337abba6ff54ea826b7587a2f081 | |
parent | 6a9438343bb63e2c1fc028f2e9387e6ae41b9fc8 (diff) | |
download | android-node-v8-5267f29b3445d608e2458087a5724820aa996930.tar.gz android-node-v8-5267f29b3445d608e2458087a5724820aa996930.tar.bz2 android-node-v8-5267f29b3445d608e2458087a5724820aa996930.zip |
test: fix flaky test-fs-watch-encoding on OS X
PR-URL: https://github.com/nodejs/node/pull/7356
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
-rw-r--r-- | test/parallel/test-fs-watch-encoding.js | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/test/parallel/test-fs-watch-encoding.js b/test/parallel/test-fs-watch-encoding.js index 449d0c8bf4..f17ed7c30a 100644 --- a/test/parallel/test-fs-watch-encoding.js +++ b/test/parallel/test-fs-watch-encoding.js @@ -1,54 +1,75 @@ '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 fs = require('fs'); const path = require('path'); -const assert = require('assert'); - -if (common.isFreeBSD) { - common.skip('Test currently not working on FreeBSD'); - return; -} 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(); + if (['e696b0e5bbbae69687e5a4b9e4bbb62e747874', null].includes(filename)) + done(watcher1); } ); +registerWatcher(watcher1); const watcher2 = fs.watch( common.tmpDir, (event, filename) => { - if (filename) - assert.equal(filename, fn); - watcher2.close(); + if ([fn, null].includes(filename)) + done(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(); + if (filename instanceof Buffer && filename.toString('utf8') === fn) + done(watcher3); + else if (filename === null) + done(watcher3); } ); +registerWatcher(watcher3); -const fd = fs.openSync(a, 'w+'); -fs.closeSync(fd); +const done = common.mustCall(unregisterWatcher, watchers.size); -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)); |