Skip to content

Commit 4bcbefc

Browse files
cjihrigDavidCai1111
authored andcommitted
test: add coverage for vm's breakOnSigint option
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]>
1 parent 14c6ae8 commit 4bcbefc

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

test/parallel/test-vm-sigint.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ const vm = require('vm');
55

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

8-
const methods = [
9-
'runInThisContext',
10-
'runInContext'
11-
];
12-
138
if (common.isWindows) {
149
// No way to send CTRL_C_EVENT to processes from JS right now.
1510
common.skip('platform not supported');
@@ -18,31 +13,38 @@ if (common.isWindows) {
1813

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

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

26+
for (let i = 0; i < listeners; i++)
27+
process.on('SIGINT', common.noop);
28+
2929
assert.throws(() => { vm[method](script, ...args, options); },
3030
/^Error: Script execution interrupted\.$/);
31-
3231
return;
3332
}
3433

35-
for (const method of methods) {
36-
const child = spawn(process.execPath, [__filename, 'child', method], {
37-
stdio: [null, 'pipe', 'inherit', 'ipc']
38-
});
39-
40-
child.on('message', common.mustCall(() => {
41-
process.kill(child.pid, 'SIGINT');
42-
}));
43-
44-
child.on('close', common.mustCall((code, signal) => {
45-
assert.strictEqual(signal, null);
46-
assert.strictEqual(code, 0);
47-
}));
34+
for (const method of ['runInThisContext', 'runInContext']) {
35+
for (const listeners of [0, 1, 2]) {
36+
const args = [__filename, 'child', method, listeners];
37+
const child = spawn(process.execPath, args, {
38+
stdio: [null, 'pipe', 'inherit', 'ipc']
39+
});
40+
41+
child.on('message', common.mustCall(() => {
42+
process.kill(child.pid, 'SIGINT');
43+
}));
44+
45+
child.on('close', common.mustCall((code, signal) => {
46+
assert.strictEqual(signal, null);
47+
assert.strictEqual(code, 0);
48+
}));
49+
}
4850
}

0 commit comments

Comments
 (0)