Browse Source

test: fix flaky test-child-process-emfile

Require the test setup to obtain an EMFILE error and not ENFILE as
ENFILE means there is a race condition with other processes that may
close files before `spawn()` is called by the test.

Fixes: https://github.com/nodejs/node/issues/2666
PR-URL: https://github.com/nodejs/node/pull/3430
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
v4.x
Rich Trott 9 years ago
committed by James M Snell
parent
commit
a5d968b8a2
  1. 32
      test/sequential/test-child-process-emfile.js

32
test/sequential/test-child-process-emfile.js

@ -1,30 +1,46 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var spawn = require('child_process').spawn; const child_process = require('child_process');
var fs = require('fs'); const fs = require('fs');
if (common.isWindows) { if (common.isWindows) {
console.log('1..0 # Skipped: no RLIMIT_NOFILE on Windows'); console.log('1..0 # Skipped: no RLIMIT_NOFILE on Windows');
return; return;
} }
var openFds = []; 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 (;;) { for (;;) {
try { try {
openFds.push(fs.openSync(__filename, 'r')); openFds.push(fs.openSync(__filename, 'r'));
} catch (err) { } catch (err) {
assert(err.code === 'EMFILE' || err.code === 'ENFILE'); assert(err.code === 'EMFILE');
break; break;
} }
} }
// Should emit an error, not throw. // Should emit an error, not throw.
var proc = spawn(process.execPath, ['-e', '0']); const proc = child_process.spawn(process.execPath, ['-e', '0']);
proc.on('error', common.mustCall(function(err) { proc.on('error', common.mustCall(function(err) {
assert(err.code === 'EMFILE' || err.code === 'ENFILE'); assert.strictEqual(err.code, 'EMFILE');
})); }));
proc.on('exit', function() { proc.on('exit', function() {

Loading…
Cancel
Save