Skip to content

Commit

Permalink
test: add coverage for vm's breakOnSigint option
Browse files Browse the repository at this point in the history
The breakOnSigint option follows different code paths, depending
on the number of listeners for SIGINT. This commit updates the
existing test to vary the number of SIGINT handlers.

PR-URL: #12512
Reviewed-By: David Cai <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
cjihrig authored and DavidCai1111 committed Apr 21, 2017
1 parent 14c6ae8 commit 4bcbefc
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions test/parallel/test-vm-sigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ const vm = require('vm');

const spawn = require('child_process').spawn;

const methods = [
'runInThisContext',
'runInContext'
];

if (common.isWindows) {
// No way to send CTRL_C_EVENT to processes from JS right now.
common.skip('platform not supported');
Expand All @@ -18,31 +13,38 @@ if (common.isWindows) {

if (process.argv[2] === 'child') {
const method = process.argv[3];
const listeners = +process.argv[4];
assert.ok(method);
assert.ok(typeof listeners, 'number');

const script = `process.send('${method}'); while(true) {}`;
const args = method === 'runInContext' ?
[vm.createContext({ process })] :
[];
const options = { breakOnSigint: true };

for (let i = 0; i < listeners; i++)
process.on('SIGINT', common.noop);

assert.throws(() => { vm[method](script, ...args, options); },
/^Error: Script execution interrupted\.$/);

return;
}

for (const method of methods) {
const child = spawn(process.execPath, [__filename, 'child', method], {
stdio: [null, 'pipe', 'inherit', 'ipc']
});

child.on('message', common.mustCall(() => {
process.kill(child.pid, 'SIGINT');
}));

child.on('close', common.mustCall((code, signal) => {
assert.strictEqual(signal, null);
assert.strictEqual(code, 0);
}));
for (const method of ['runInThisContext', 'runInContext']) {
for (const listeners of [0, 1, 2]) {
const args = [__filename, 'child', method, listeners];
const child = spawn(process.execPath, args, {
stdio: [null, 'pipe', 'inherit', 'ipc']
});

child.on('message', common.mustCall(() => {
process.kill(child.pid, 'SIGINT');
}));

child.on('close', common.mustCall((code, signal) => {
assert.strictEqual(signal, null);
assert.strictEqual(code, 0);
}));
}
}

0 comments on commit 4bcbefc

Please sign in to comment.