| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,15 +5,20 @@ const vm = require('vm'); | |||
| 5 | 5 | ||
| 6 | 6 | const spawn = require('child_process').spawn; | |
| 7 | 7 | ||
| 8 | + const methods = [ | ||
| 9 | + 'runInThisContext', | ||
| 10 | + 'runInContext' | ||
| 11 | + ]; | ||
| 12 | + | ||
| 8 | 13 | if (common.isWindows) { | |
| 9 | 14 | // No way to send CTRL_C_EVENT to processes from JS right now. | |
| 10 | 15 | common.skip('platform not supported'); | |
| 11 | 16 | return; | |
| 12 | 17 | } | |
| 13 | 18 | ||
| 14 | 19 | 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); | ||
| 17 | 22 | ||
| 18 | 23 | let firstHandlerCalled = 0; | |
| 19 | 24 | process.on('SIGINT', common.mustCall(() => { | |
@@ -27,12 +32,14 @@ if (process.argv[2] === 'child') { | |||
| 27 | 32 | // Handler attached _before_ execution. | |
| 28 | 33 | })); | |
| 29 | 34 | ||
| 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 }; | ||
| 35 | 40 | ||
| 41 | + assert.throws(() => { vm[method](script, ...args, options); }, | ||
| 42 | + /^Error: Script execution interrupted\.$/); | ||
| 36 | 43 | assert.strictEqual(firstHandlerCalled, 0); | |
| 37 | 44 | assert.strictEqual(onceHandlerCalled, 0); | |
| 38 | 45 | ||
@@ -46,7 +53,9 @@ if (process.argv[2] === 'child') { | |||
| 46 | 53 | if (afterHandlerCalled++ === 0) { | |
| 47 | 54 | // The first time it just bounces back to check that the `once()` | |
| 48 | 55 | // 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); | ||
| 50 | 59 | return; | |
| 51 | 60 | } | |
| 52 | 61 | ||
@@ -55,26 +64,24 @@ if (process.argv[2] === 'child') { | |||
| 55 | 64 | timeout.unref(); | |
| 56 | 65 | }, 2)); | |
| 57 | 66 | ||
| 58 | - process.kill(parent, 'SIGUSR2'); | ||
| 67 | + process.send(method); | ||
| 59 | 68 | ||
| 60 | 69 | return; | |
| 61 | 70 | } | |
| 62 | 71 | ||
| 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 | + }); | ||
| 72 | 76 | ||
| 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)); | ||
| 76 | 82 | ||
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,38 +5,44 @@ const vm = require('vm'); | |||
| 5 | 5 | ||
| 6 | 6 | const spawn = require('child_process').spawn; | |
| 7 | 7 | ||
| 8 | + const methods = [ | ||
| 9 | + 'runInThisContext', | ||
| 10 | + 'runInContext' | ||
| 11 | + ]; | ||
| 12 | + | ||
| 8 | 13 | if (common.isWindows) { | |
| 9 | 14 | // No way to send CTRL_C_EVENT to processes from JS right now. | |
| 10 | 15 | common.skip('platform not supported'); | |
| 11 | 16 | return; | |
| 12 | 17 | } | |
| 13 | 18 | ||
| 14 | 19 | 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 }; | ||
| 17 | 28 | ||
| 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\.$/); | ||
| 23 | 31 | ||
| 24 | 32 | return; | |
| 25 | 33 | } | |
| 26 | 34 | ||
| 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 | + }); | ||
| 28 | 39 | ||
| 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 | + })); | ||
| 34 | 43 | ||
| 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments