Skip to content

Commit b395ed9

Browse files
DavidCai1111addaleax
authored andcommitted
test: increase coverage of vm
PR-URL: #11377 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 0934a27 commit b395ed9

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

test/parallel/test-vm-sigint-existing-handler.js

+32-25
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ const vm = require('vm');
55

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

8+
const methods = [
9+
'runInThisContext',
10+
'runInContext'
11+
];
12+
813
if (common.isWindows) {
914
// No way to send CTRL_C_EVENT to processes from JS right now.
1015
common.skip('platform not supported');
1116
return;
1217
}
1318

1419
if (process.argv[2] === 'child') {
15-
const parent = +process.env.REPL_TEST_PPID;
16-
assert.ok(parent);
20+
const method = process.argv[3];
21+
assert.ok(method);
1722

1823
let firstHandlerCalled = 0;
1924
process.on('SIGINT', common.mustCall(() => {
@@ -27,12 +32,14 @@ if (process.argv[2] === 'child') {
2732
// Handler attached _before_ execution.
2833
}));
2934

30-
assert.throws(() => {
31-
vm.runInThisContext(`process.kill(${parent}, 'SIGUSR2'); while(true) {}`, {
32-
breakOnSigint: true
33-
});
34-
}, /Script execution interrupted/);
35+
const script = `process.send('${method}'); while(true) {}`;
36+
const args = method === 'runInContext' ?
37+
[vm.createContext({ process })] :
38+
[];
39+
const options = { breakOnSigint: true };
3540

41+
assert.throws(() => { vm[method](script, ...args, options); },
42+
/^Error: Script execution interrupted\.$/);
3643
assert.strictEqual(firstHandlerCalled, 0);
3744
assert.strictEqual(onceHandlerCalled, 0);
3845

@@ -46,7 +53,9 @@ if (process.argv[2] === 'child') {
4653
if (afterHandlerCalled++ === 0) {
4754
// The first time it just bounces back to check that the `once()`
4855
// handler is not called the second time.
49-
process.kill(parent, 'SIGUSR2');
56+
assert.strictEqual(firstHandlerCalled, 1);
57+
assert.strictEqual(onceHandlerCalled, 1);
58+
process.send(method);
5059
return;
5160
}
5261

@@ -55,26 +64,24 @@ if (process.argv[2] === 'child') {
5564
timeout.unref();
5665
}, 2));
5766

58-
process.kill(parent, 'SIGUSR2');
67+
process.send(method);
5968

6069
return;
6170
}
6271

63-
process.env.REPL_TEST_PPID = process.pid;
64-
65-
// Set the `SIGUSR2` handler before spawning the child process to make sure
66-
// the signal is always handled.
67-
process.on('SIGUSR2', common.mustCall(() => {
68-
// First kill() breaks the while(true) loop, second one invokes the real
69-
// signal handlers.
70-
process.kill(child.pid, 'SIGINT');
71-
}, 3));
72+
for (const method of methods) {
73+
const child = spawn(process.execPath, [__filename, 'child', method], {
74+
stdio: [null, 'inherit', 'inherit', 'ipc']
75+
});
7276

73-
const child = spawn(process.execPath, [__filename, 'child'], {
74-
stdio: [null, 'inherit', 'inherit']
75-
});
77+
child.on('message', common.mustCall(() => {
78+
// First kill() breaks the while(true) loop, second one invokes the real
79+
// signal handlers.
80+
process.kill(child.pid, 'SIGINT');
81+
}, 3));
7682

77-
child.on('close', function(code, signal) {
78-
assert.strictEqual(signal, null);
79-
assert.strictEqual(code, 0);
80-
});
83+
child.on('close', common.mustCall((code, signal) => {
84+
assert.strictEqual(signal, null);
85+
assert.strictEqual(code, 0);
86+
}));
87+
}

test/parallel/test-vm-sigint.js

+27-21
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,44 @@ const vm = require('vm');
55

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

8+
const methods = [
9+
'runInThisContext',
10+
'runInContext'
11+
];
12+
813
if (common.isWindows) {
914
// No way to send CTRL_C_EVENT to processes from JS right now.
1015
common.skip('platform not supported');
1116
return;
1217
}
1318

1419
if (process.argv[2] === 'child') {
15-
const parent = +process.env.REPL_TEST_PPID;
16-
assert.ok(parent);
20+
const method = process.argv[3];
21+
assert.ok(method);
22+
23+
const script = `process.send('${method}'); while(true) {}`;
24+
const args = method === 'runInContext' ?
25+
[vm.createContext({ process })] :
26+
[];
27+
const options = { breakOnSigint: true };
1728

18-
assert.throws(() => {
19-
vm.runInThisContext(`process.kill(${parent}, "SIGUSR2"); while(true) {}`, {
20-
breakOnSigint: true
21-
});
22-
}, /Script execution interrupted/);
29+
assert.throws(() => { vm[method](script, ...args, options); },
30+
/^Error: Script execution interrupted\.$/);
2331

2432
return;
2533
}
2634

27-
process.env.REPL_TEST_PPID = process.pid;
35+
for (const method of methods) {
36+
const child = spawn(process.execPath, [__filename, 'child', method], {
37+
stdio: [null, 'pipe', 'inherit', 'ipc']
38+
});
2839

29-
// Set the `SIGUSR2` handler before spawning the child process to make sure
30-
// the signal is always handled.
31-
process.on('SIGUSR2', common.mustCall(() => {
32-
process.kill(child.pid, 'SIGINT');
33-
}));
40+
child.on('message', common.mustCall(() => {
41+
process.kill(child.pid, 'SIGINT');
42+
}));
3443

35-
const child = spawn(process.execPath, [__filename, 'child'], {
36-
stdio: [null, 'pipe', 'inherit']
37-
});
38-
39-
child.on('close', common.mustCall((code, signal) => {
40-
assert.strictEqual(signal, null);
41-
assert.strictEqual(code, 0);
42-
}));
44+
child.on('close', common.mustCall((code, signal) => {
45+
assert.strictEqual(signal, null);
46+
assert.strictEqual(code, 0);
47+
}));
48+
}

0 commit comments

Comments
 (0)