Skip to content

Commit

Permalink
test: Worker initialization failure test case
Browse files Browse the repository at this point in the history
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
HarshithaKP committed Feb 24, 2020
1 parent 9c70292 commit f69276b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/parallel/test-worker-init-failed.js
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());
})
}
43 changes: 43 additions & 0 deletions test/parallel/test-worker-init-failure.js
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());
});
}

0 comments on commit f69276b

Please sign in to comment.