test: increase coverage of vm
PR-URL: https://github.com/nodejs/node/pull/11377 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
5c1ec6ea67
commit
c7205ba941
@ -5,6 +5,11 @@ const vm = require('vm');
|
|||||||
|
|
||||||
const spawn = require('child_process').spawn;
|
const spawn = require('child_process').spawn;
|
||||||
|
|
||||||
|
const methods = [
|
||||||
|
'runInThisContext',
|
||||||
|
'runInContext'
|
||||||
|
];
|
||||||
|
|
||||||
if (common.isWindows) {
|
if (common.isWindows) {
|
||||||
// No way to send CTRL_C_EVENT to processes from JS right now.
|
// No way to send CTRL_C_EVENT to processes from JS right now.
|
||||||
common.skip('platform not supported');
|
common.skip('platform not supported');
|
||||||
@ -12,8 +17,8 @@ if (common.isWindows) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (process.argv[2] === 'child') {
|
if (process.argv[2] === 'child') {
|
||||||
const parent = +process.env.REPL_TEST_PPID;
|
const method = process.argv[3];
|
||||||
assert.ok(parent);
|
assert.ok(method);
|
||||||
|
|
||||||
let firstHandlerCalled = 0;
|
let firstHandlerCalled = 0;
|
||||||
process.on('SIGINT', common.mustCall(() => {
|
process.on('SIGINT', common.mustCall(() => {
|
||||||
@ -27,12 +32,14 @@ if (process.argv[2] === 'child') {
|
|||||||
// Handler attached _before_ execution.
|
// Handler attached _before_ execution.
|
||||||
}));
|
}));
|
||||||
|
|
||||||
assert.throws(() => {
|
const script = `process.send('${method}'); while(true) {}`;
|
||||||
vm.runInThisContext(`process.kill(${parent}, 'SIGUSR2'); while(true) {}`, {
|
const args = method === 'runInContext' ?
|
||||||
breakOnSigint: true
|
[vm.createContext({ process })] :
|
||||||
});
|
[];
|
||||||
}, /Script execution interrupted/);
|
const options = { breakOnSigint: true };
|
||||||
|
|
||||||
|
assert.throws(() => { vm[method](script, ...args, options); },
|
||||||
|
/^Error: Script execution interrupted\.$/);
|
||||||
assert.strictEqual(firstHandlerCalled, 0);
|
assert.strictEqual(firstHandlerCalled, 0);
|
||||||
assert.strictEqual(onceHandlerCalled, 0);
|
assert.strictEqual(onceHandlerCalled, 0);
|
||||||
|
|
||||||
@ -46,7 +53,9 @@ if (process.argv[2] === 'child') {
|
|||||||
if (afterHandlerCalled++ === 0) {
|
if (afterHandlerCalled++ === 0) {
|
||||||
// The first time it just bounces back to check that the `once()`
|
// The first time it just bounces back to check that the `once()`
|
||||||
// handler is not called the second time.
|
// handler is not called the second time.
|
||||||
process.kill(parent, 'SIGUSR2');
|
assert.strictEqual(firstHandlerCalled, 1);
|
||||||
|
assert.strictEqual(onceHandlerCalled, 1);
|
||||||
|
process.send(method);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,26 +64,24 @@ if (process.argv[2] === 'child') {
|
|||||||
timeout.unref();
|
timeout.unref();
|
||||||
}, 2));
|
}, 2));
|
||||||
|
|
||||||
process.kill(parent, 'SIGUSR2');
|
process.send(method);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.REPL_TEST_PPID = process.pid;
|
for (const method of methods) {
|
||||||
|
const child = spawn(process.execPath, [__filename, 'child', method], {
|
||||||
|
stdio: [null, 'inherit', 'inherit', 'ipc']
|
||||||
|
});
|
||||||
|
|
||||||
// Set the `SIGUSR2` handler before spawning the child process to make sure
|
child.on('message', common.mustCall(() => {
|
||||||
// the signal is always handled.
|
|
||||||
process.on('SIGUSR2', common.mustCall(() => {
|
|
||||||
// First kill() breaks the while(true) loop, second one invokes the real
|
// First kill() breaks the while(true) loop, second one invokes the real
|
||||||
// signal handlers.
|
// signal handlers.
|
||||||
process.kill(child.pid, 'SIGINT');
|
process.kill(child.pid, 'SIGINT');
|
||||||
}, 3));
|
}, 3));
|
||||||
|
|
||||||
const child = spawn(process.execPath, [__filename, 'child'], {
|
child.on('close', common.mustCall((code, signal) => {
|
||||||
stdio: [null, 'inherit', 'inherit']
|
|
||||||
});
|
|
||||||
|
|
||||||
child.on('close', function(code, signal) {
|
|
||||||
assert.strictEqual(signal, null);
|
assert.strictEqual(signal, null);
|
||||||
assert.strictEqual(code, 0);
|
assert.strictEqual(code, 0);
|
||||||
});
|
}));
|
||||||
|
}
|
||||||
|
@ -5,6 +5,11 @@ const vm = require('vm');
|
|||||||
|
|
||||||
const spawn = require('child_process').spawn;
|
const spawn = require('child_process').spawn;
|
||||||
|
|
||||||
|
const methods = [
|
||||||
|
'runInThisContext',
|
||||||
|
'runInContext'
|
||||||
|
];
|
||||||
|
|
||||||
if (common.isWindows) {
|
if (common.isWindows) {
|
||||||
// No way to send CTRL_C_EVENT to processes from JS right now.
|
// No way to send CTRL_C_EVENT to processes from JS right now.
|
||||||
common.skip('platform not supported');
|
common.skip('platform not supported');
|
||||||
@ -12,31 +17,32 @@ if (common.isWindows) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (process.argv[2] === 'child') {
|
if (process.argv[2] === 'child') {
|
||||||
const parent = +process.env.REPL_TEST_PPID;
|
const method = process.argv[3];
|
||||||
assert.ok(parent);
|
assert.ok(method);
|
||||||
|
|
||||||
assert.throws(() => {
|
const script = `process.send('${method}'); while(true) {}`;
|
||||||
vm.runInThisContext(`process.kill(${parent}, "SIGUSR2"); while(true) {}`, {
|
const args = method === 'runInContext' ?
|
||||||
breakOnSigint: true
|
[vm.createContext({ process })] :
|
||||||
});
|
[];
|
||||||
}, /Script execution interrupted/);
|
const options = { breakOnSigint: true };
|
||||||
|
|
||||||
|
assert.throws(() => { vm[method](script, ...args, options); },
|
||||||
|
/^Error: Script execution interrupted\.$/);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.REPL_TEST_PPID = process.pid;
|
for (const method of methods) {
|
||||||
|
const child = spawn(process.execPath, [__filename, 'child', method], {
|
||||||
|
stdio: [null, 'pipe', 'inherit', 'ipc']
|
||||||
|
});
|
||||||
|
|
||||||
// Set the `SIGUSR2` handler before spawning the child process to make sure
|
child.on('message', common.mustCall(() => {
|
||||||
// the signal is always handled.
|
|
||||||
process.on('SIGUSR2', common.mustCall(() => {
|
|
||||||
process.kill(child.pid, 'SIGINT');
|
process.kill(child.pid, 'SIGINT');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const child = spawn(process.execPath, [__filename, 'child'], {
|
|
||||||
stdio: [null, 'pipe', 'inherit']
|
|
||||||
});
|
|
||||||
|
|
||||||
child.on('close', common.mustCall((code, signal) => {
|
child.on('close', common.mustCall((code, signal) => {
|
||||||
assert.strictEqual(signal, null);
|
assert.strictEqual(signal, null);
|
||||||
assert.strictEqual(code, 0);
|
assert.strictEqual(code, 0);
|
||||||
}));
|
}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user