forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inspector: bind to random port with --inspect=0
Allow binding to a randomly assigned port number with `--inspect=0` or `--inspect-brk=0`. PR-URL: nodejs#5025 Refs: nodejs#4419 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
- Loading branch information
1 parent
c420cd1
commit 399cb25
Showing
6 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |