mirror of https://github.com/lukechilds/node.git
Browse Source
Allow binding to a randomly assigned port number with `--inspect=0` or `--inspect-brk=0`. PR-URL: https://github.com/nodejs/node/pull/5025 Refs: https://github.com/nodejs/node/issues/4419 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>v6
6 changed files with 99 additions and 4 deletions
@ -0,0 +1,32 @@ |
|||
// Flags: --inspect=0
|
|||
'use strict'; |
|||
|
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const cluster = require('cluster'); |
|||
|
|||
if (cluster.isMaster) { |
|||
const ports = []; |
|||
for (const worker of [cluster.fork(), |
|||
cluster.fork(), |
|||
cluster.fork()]) { |
|||
worker.on('message', common.mustCall((message) => { |
|||
ports.push(message.debugPort); |
|||
worker.kill(); |
|||
})); |
|||
worker.send('debugPort'); |
|||
} |
|||
process.on('exit', () => { |
|||
ports.sort(); |
|||
assert.strictEqual(ports.length, 3); |
|||
assert(ports.every((port) => port > 0)); |
|||
assert(ports.every((port) => port < 65536)); |
|||
assert.strictEqual(ports[0] + 1, ports[1]); // Ports should be consecutive.
|
|||
assert.strictEqual(ports[1] + 1, ports[2]); |
|||
}); |
|||
} else { |
|||
process.on('message', (message) => { |
|||
if (message === 'debugPort') |
|||
process.send({ debugPort: process.debugPort }); |
|||
}); |
|||
} |
@ -0,0 +1,46 @@ |
|||
'use strict'; |
|||
|
|||
const { mustCall } = require('../common'); |
|||
const assert = require('assert'); |
|||
const { URL } = require('url'); |
|||
const { spawn } = require('child_process'); |
|||
|
|||
function test(arg) { |
|||
const args = [arg, '-p', 'process.debugPort']; |
|||
const proc = spawn(process.execPath, args); |
|||
proc.stdout.setEncoding('utf8'); |
|||
proc.stderr.setEncoding('utf8'); |
|||
let stdout = ''; |
|||
let stderr = ''; |
|||
proc.stdout.on('data', (data) => stdout += data); |
|||
proc.stderr.on('data', (data) => stderr += data); |
|||
proc.stdout.on('close', assert.ifError); |
|||
proc.stderr.on('close', assert.ifError); |
|||
let port = ''; |
|||
proc.stderr.on('data', () => { |
|||
if (!stderr.includes('\n')) return; |
|||
assert(/Debugger listening on (.+)/.test(stderr)); |
|||
port = new URL(RegExp.$1).port; |
|||
assert(+port > 0); |
|||
}); |
|||
if (/inspect-brk/.test(arg)) { |
|||
proc.stderr.on('data', () => { |
|||
if (stderr.includes('\n') && !proc.killed) proc.kill(); |
|||
}); |
|||
} else { |
|||
let onclose = () => { |
|||
onclose = () => assert.strictEqual(port, stdout.trim()); |
|||
}; |
|||
proc.stdout.on('close', mustCall(() => onclose())); |
|||
proc.stderr.on('close', mustCall(() => onclose())); |
|||
proc.on('exit', mustCall((exitCode) => assert.strictEqual(exitCode, 0))); |
|||
} |
|||
} |
|||
|
|||
test('--inspect=0'); |
|||
test('--inspect=127.0.0.1:0'); |
|||
test('--inspect=localhost:0'); |
|||
|
|||
test('--inspect-brk=0'); |
|||
test('--inspect-brk=127.0.0.1:0'); |
|||
test('--inspect-brk=localhost:0'); |
Loading…
Reference in new issue