0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/sequential/test-child-process-emfile.js
cjihrig 7dd82dd1c3 test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: https://github.com/nodejs/node/pull/11152
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
2017-02-06 14:07:55 -05:00

52 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const fs = require('fs');
if (common.isWindows) {
common.skip('no RLIMIT_NOFILE on Windows');
return;
}
const ulimit = Number(child_process.execSync('ulimit -Hn'));
if (ulimit > 64 || Number.isNaN(ulimit)) {
// Sorry about this nonsense. It can be replaced if
// https://github.com/nodejs/node-v0.x-archive/pull/2143#issuecomment-2847886
// ever happens.
const result = child_process.spawnSync(
'/bin/sh',
['-c', `ulimit -n 64 && '${process.execPath}' '${__filename}'`]
);
assert.strictEqual(result.stdout.toString(), '');
assert.strictEqual(result.stderr.toString(), '');
assert.strictEqual(result.status, 0);
assert.strictEqual(result.error, undefined);
return;
}
const openFds = [];
for (;;) {
try {
openFds.push(fs.openSync(__filename, 'r'));
} catch (err) {
assert.strictEqual(err.code, 'EMFILE');
break;
}
}
// Should emit an error, not throw.
const proc = child_process.spawn(process.execPath, ['-e', '0']);
proc.on('error', common.mustCall(function(err) {
assert.strictEqual(err.code, 'EMFILE');
}));
proc.on('exit', common.mustNotCall('"exit" event should not be emitted'));
// close one fd for LSan
if (openFds.length >= 1) {
fs.closeSync(openFds.pop());
}