2017-05-26 04:00:24 +02:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
2017-07-01 01:29:09 +02:00
|
|
|
common.skipIfInspectorDisabled();
|
2017-05-26 04:00:24 +02:00
|
|
|
|
|
|
|
// Test inspector open()/close()/url() API. It uses ephemeral ports so can be
|
|
|
|
// run safely in parallel.
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const fork = require('child_process').fork;
|
|
|
|
const net = require('net');
|
|
|
|
const url = require('url');
|
|
|
|
|
|
|
|
if (process.env.BE_CHILD)
|
|
|
|
return beChild();
|
|
|
|
|
2017-08-15 20:14:54 +02:00
|
|
|
const child = fork(__filename,
|
|
|
|
{ env: Object.assign({}, process.env, { BE_CHILD: 1 }) });
|
2017-05-26 04:00:24 +02:00
|
|
|
|
|
|
|
child.once('message', common.mustCall((msg) => {
|
|
|
|
assert.strictEqual(msg.cmd, 'started');
|
|
|
|
|
2017-07-11 02:55:21 +02:00
|
|
|
child.send({ cmd: 'open', args: [0] });
|
2017-05-26 04:00:24 +02:00
|
|
|
child.once('message', common.mustCall(firstOpen));
|
|
|
|
}));
|
|
|
|
|
|
|
|
let firstPort;
|
|
|
|
|
|
|
|
function firstOpen(msg) {
|
|
|
|
assert.strictEqual(msg.cmd, 'url');
|
|
|
|
const port = url.parse(msg.url).port;
|
|
|
|
ping(port, (err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
// Inspector is already open, and won't be reopened, so args don't matter.
|
2017-07-11 02:55:21 +02:00
|
|
|
child.send({ cmd: 'open', args: [] });
|
2017-05-26 04:00:24 +02:00
|
|
|
child.once('message', common.mustCall(tryToOpenWhenOpen));
|
|
|
|
firstPort = port;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryToOpenWhenOpen(msg) {
|
|
|
|
assert.strictEqual(msg.cmd, 'url');
|
|
|
|
const port = url.parse(msg.url).port;
|
|
|
|
// Reopen didn't do anything, the port was already open, and has not changed.
|
|
|
|
assert.strictEqual(port, firstPort);
|
|
|
|
ping(port, (err) => {
|
|
|
|
assert.ifError(err);
|
2017-07-11 02:55:21 +02:00
|
|
|
child.send({ cmd: 'close' });
|
2017-05-26 04:00:24 +02:00
|
|
|
child.once('message', common.mustCall(closeWhenOpen));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeWhenOpen(msg) {
|
|
|
|
assert.strictEqual(msg.cmd, 'url');
|
|
|
|
assert.strictEqual(msg.url, undefined);
|
|
|
|
ping(firstPort, (err) => {
|
|
|
|
assert(err);
|
2017-07-11 02:55:21 +02:00
|
|
|
child.send({ cmd: 'close' });
|
2017-05-26 04:00:24 +02:00
|
|
|
child.once('message', common.mustCall(tryToCloseWhenClosed));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryToCloseWhenClosed(msg) {
|
|
|
|
assert.strictEqual(msg.cmd, 'url');
|
|
|
|
assert.strictEqual(msg.url, undefined);
|
2017-07-11 02:55:21 +02:00
|
|
|
child.send({ cmd: 'open', args: [] });
|
2017-05-26 04:00:24 +02:00
|
|
|
child.once('message', common.mustCall(reopenAfterClose));
|
|
|
|
}
|
|
|
|
|
|
|
|
function reopenAfterClose(msg) {
|
|
|
|
assert.strictEqual(msg.cmd, 'url');
|
|
|
|
const port = url.parse(msg.url).port;
|
|
|
|
ping(port, (err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function ping(port, callback) {
|
|
|
|
net.connect(port)
|
|
|
|
.on('connect', function() { close(this); })
|
|
|
|
.on('error', function(err) { close(this, err); });
|
|
|
|
|
|
|
|
function close(self, err) {
|
|
|
|
self.end();
|
|
|
|
self.on('close', () => callback(err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function beChild() {
|
|
|
|
const inspector = require('inspector');
|
|
|
|
|
2017-07-11 02:55:21 +02:00
|
|
|
process.send({ cmd: 'started' });
|
2017-05-26 04:00:24 +02:00
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
if (msg.cmd === 'open') {
|
|
|
|
inspector.open(...msg.args);
|
|
|
|
}
|
|
|
|
if (msg.cmd === 'close') {
|
|
|
|
inspector.close();
|
|
|
|
}
|
2017-07-11 02:55:21 +02:00
|
|
|
process.send({ cmd: 'url', url: inspector.url() });
|
2017-05-26 04:00:24 +02:00
|
|
|
});
|
|
|
|
}
|