Skip to content

Commit f69276b

Browse files
committed
test: Worker initialization failure test case
Cover the scenario fixed through nodejs#31621 Unfortunately there is no easy way to test this, in a cross-platform manner. So the approach is: - open a child process with ulimit restriction on file descriptors - in the child process, start few workers - more than the fd limit - make sure some workers fail, with the expected error type. - skip the test in windows, as there is no ulimit there.
1 parent 9c70292 commit f69276b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const child_process = require('child_process');
5+
6+
// Test that workers fail with meaningful error message
7+
// when their initialization fails.
8+
9+
if (process.argv[2] === 'child') {
10+
const {Worker} = require('worker_threads');
11+
for (let i = 0; i < 256; ++i) {
12+
const worker = new Worker(
13+
'require(\'worker_threads\').parentPort.postMessage(2 + 2)',
14+
{ eval: true });
15+
worker.on('message', (result) => {
16+
assert.strictEqual(result, 4);
17+
})
18+
worker.on('error', (e) => {
19+
assert(e.message.match(/Worker initialization failure: EMFILE/));
20+
assert.strictEqual(e.code, 'ERR_WORKER_INIT_FAILED');
21+
})
22+
}
23+
24+
} else {
25+
26+
if (common.isWindows) {
27+
common.skip('ulimit does not work on Windows.');
28+
}
29+
30+
// limit the number of open files, to force workers to fail
31+
let testCmd = 'ulimit -n 128 && ';
32+
33+
testCmd += `${process.argv[0]} ${process.argv[1]} child`;
34+
const cp = child_process.exec(testCmd);
35+
36+
// turn on the child streams for debugging purpose
37+
cp.stderr.on('data', (d) => {
38+
console.log(d.toString());
39+
})
40+
cp.stdout.on('data', (d) => {
41+
console.log(d.toString());
42+
})
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const child_process = require('child_process');
5+
6+
// Test that workers fail with meaningful error message
7+
// when their initialization fails.
8+
9+
if (process.argv[2] === 'child') {
10+
const { Worker } = require('worker_threads');
11+
for (let i = 0; i < 256; ++i) {
12+
const worker = new Worker(
13+
'require(\'worker_threads\').parentPort.postMessage(2 + 2)',
14+
{ eval: true });
15+
worker.on('message', (result) => {
16+
assert.strictEqual(result, 4);
17+
});
18+
worker.on('error', (e) => {
19+
assert(e.message.match(/Worker initialization failure: EMFILE/));
20+
assert.strictEqual(e.code, 'ERR_WORKER_INIT_FAILED');
21+
});
22+
}
23+
24+
} else {
25+
26+
if (common.isWindows) {
27+
common.skip('ulimit does not work on Windows.');
28+
}
29+
30+
// Limit the number of open files, to force workers to fail
31+
let testCmd = 'ulimit -n 128 && ';
32+
33+
testCmd += `${process.argv[0]} ${process.argv[1]} child`;
34+
const cp = child_process.exec(testCmd);
35+
36+
// Turn on the child streams for debugging purpose
37+
cp.stderr.on('data', (d) => {
38+
console.log(d.toString());
39+
});
40+
cp.stdout.on('data', (d) => {
41+
console.log(d.toString());
42+
});
43+
}

0 commit comments

Comments
 (0)