node/test/sequential/test-fs-watch-system-limit.js
Anna Henningsen bea41bc945
test: fix flaky sequential/test-fs-watch-system-limit
This test has at least once locally received `EMFILE` rather
than `ENOSPC`, which also seems to provide a reasonable error
message (which is what the test ultimately checks).

PR-URL: https://github.com/nodejs/node/pull/23038
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-09-23 22:42:43 +02:00

53 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const stream = require('stream');
if (!common.isLinux)
common.skip('The fs watch limit is OS-dependent');
if (!common.enoughTestCpu)
common.skip('This test is resource-intensive');
const processes = [];
const gatherStderr = new stream.PassThrough();
gatherStderr.setEncoding('utf8');
gatherStderr.setMaxListeners(Infinity);
let finished = false;
function spawnProcesses() {
for (let i = 0; i < 10; ++i) {
const proc = child_process.spawn(
process.execPath,
[ '-e',
`process.chdir(${JSON.stringify(__dirname)});
for (const file of fs.readdirSync('.'))
fs.watch(file, () => {});`
], { stdio: ['inherit', 'inherit', 'pipe'] });
proc.stderr.pipe(gatherStderr);
processes.push(proc);
}
setTimeout(() => {
if (!finished && processes.length < 200)
spawnProcesses();
}, 100);
}
spawnProcesses();
let accumulated = '';
gatherStderr.on('data', common.mustCallAtLeast((chunk) => {
accumulated += chunk;
if (accumulated.includes('Error:') && !finished) {
assert(
accumulated.includes('ENOSPC: System limit for number ' +
'of file watchers reached') ||
accumulated.includes('EMFILE: '),
accumulated);
console.log(`done after ${processes.length} processes, cleaning up`);
finished = true;
processes.forEach((proc) => proc.kill());
}
}, 1));