forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
9c70292
commit f69276b
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
|
||
// Test that workers fail with meaningful error message | ||
// when their initialization fails. | ||
|
||
if (process.argv[2] === 'child') { | ||
const {Worker} = require('worker_threads'); | ||
for (let i = 0; i < 256; ++i) { | ||
const worker = new Worker( | ||
'require(\'worker_threads\').parentPort.postMessage(2 + 2)', | ||
{ eval: true }); | ||
worker.on('message', (result) => { | ||
assert.strictEqual(result, 4); | ||
}) | ||
worker.on('error', (e) => { | ||
assert(e.message.match(/Worker initialization failure: EMFILE/)); | ||
assert.strictEqual(e.code, 'ERR_WORKER_INIT_FAILED'); | ||
}) | ||
} | ||
|
||
} else { | ||
|
||
if (common.isWindows) { | ||
common.skip('ulimit does not work on Windows.'); | ||
} | ||
|
||
// limit the number of open files, to force workers to fail | ||
let testCmd = 'ulimit -n 128 && '; | ||
|
||
testCmd += `${process.argv[0]} ${process.argv[1]} child`; | ||
const cp = child_process.exec(testCmd); | ||
|
||
// turn on the child streams for debugging purpose | ||
cp.stderr.on('data', (d) => { | ||
console.log(d.toString()); | ||
}) | ||
cp.stdout.on('data', (d) => { | ||
console.log(d.toString()); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
|
||
// Test that workers fail with meaningful error message | ||
// when their initialization fails. | ||
|
||
if (process.argv[2] === 'child') { | ||
const { Worker } = require('worker_threads'); | ||
for (let i = 0; i < 256; ++i) { | ||
const worker = new Worker( | ||
'require(\'worker_threads\').parentPort.postMessage(2 + 2)', | ||
{ eval: true }); | ||
worker.on('message', (result) => { | ||
assert.strictEqual(result, 4); | ||
}); | ||
worker.on('error', (e) => { | ||
assert(e.message.match(/Worker initialization failure: EMFILE/)); | ||
assert.strictEqual(e.code, 'ERR_WORKER_INIT_FAILED'); | ||
}); | ||
} | ||
|
||
} else { | ||
|
||
if (common.isWindows) { | ||
common.skip('ulimit does not work on Windows.'); | ||
} | ||
|
||
// Limit the number of open files, to force workers to fail | ||
let testCmd = 'ulimit -n 128 && '; | ||
|
||
testCmd += `${process.argv[0]} ${process.argv[1]} child`; | ||
const cp = child_process.exec(testCmd); | ||
|
||
// Turn on the child streams for debugging purpose | ||
cp.stderr.on('data', (d) => { | ||
console.log(d.toString()); | ||
}); | ||
cp.stdout.on('data', (d) => { | ||
console.log(d.toString()); | ||
}); | ||
} |