mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 17:10:40 +00:00

The test was sometimes timing out due to a race condition. In OS X, events for `fs.watch()` might only start showing up after a delay. This is a limitation of the operating system. To work around that, there was a timer in the test that delayed the writing of the file by 100ms. However, sometimes that was not enough, and so the event never fired, and the test timed out. Change the timer to an interval so that it fires repeatedly until it is picked up. This change only affects OS X. Fixes: https://github.com/nodejs/node/issues/8511 PR-URL: https://github.com/nodejs/node/pull/9303 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
if (!(common.isOSX || common.isWindows)) {
|
|
common.skip('recursive option is darwin/windows specific');
|
|
return;
|
|
}
|
|
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
const testDir = common.tmpDir;
|
|
const filenameOne = 'watch.txt';
|
|
|
|
common.refreshTmpDir();
|
|
|
|
const testsubdir = fs.mkdtempSync(testDir + path.sep);
|
|
const relativePathOne = path.join(path.basename(testsubdir), filenameOne);
|
|
const filepathOne = path.join(testsubdir, filenameOne);
|
|
|
|
const watcher = fs.watch(testDir, {recursive: true});
|
|
|
|
var watcherClosed = false;
|
|
watcher.on('change', function(event, filename) {
|
|
assert.ok('change' === event || 'rename' === event);
|
|
|
|
// Ignore stale events generated by mkdir and other tests
|
|
if (filename !== relativePathOne)
|
|
return;
|
|
|
|
if (common.isOSX) {
|
|
clearInterval(interval);
|
|
}
|
|
watcher.close();
|
|
watcherClosed = true;
|
|
});
|
|
|
|
if (common.isOSX) {
|
|
var interval = setInterval(function() {
|
|
fs.writeFileSync(filepathOne, 'world');
|
|
}, 10);
|
|
} else {
|
|
fs.writeFileSync(filepathOne, 'world');
|
|
}
|
|
|
|
process.on('exit', function() {
|
|
assert(watcherClosed, 'watcher Object was not closed');
|
|
});
|