2017-06-11 21:01:27 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-02-08 00:37:04 +01:00
|
|
|
// Flags: --expose-internals
|
|
|
|
|
2017-06-11 21:01:27 +02:00
|
|
|
const common = require('../common');
|
|
|
|
|
|
|
|
common.skipIfInspectorDisabled();
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
|
|
|
const debuggerPort = common.PORT;
|
|
|
|
const childProcess = require('child_process');
|
|
|
|
|
|
|
|
let offset = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test suite checks that inspector port in cluster is incremented
|
|
|
|
* for different execArgv combinations
|
|
|
|
*/
|
|
|
|
|
|
|
|
function testRunnerMain() {
|
2017-07-10 21:24:49 +02:00
|
|
|
let defaultPortCase = spawnMaster({
|
2017-06-11 21:01:27 +02:00
|
|
|
execArgv: ['--inspect'],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: 9230 }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
2018-02-10 14:52:18 +01:00
|
|
|
spawnMaster({
|
|
|
|
execArgv: ['--inspect=65534'],
|
|
|
|
workers: [
|
|
|
|
{ expectedPort: 65535 },
|
|
|
|
{ expectedPort: 1024 },
|
|
|
|
{ expectedPort: 1025 },
|
|
|
|
{ expectedPort: 1026 }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
let port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedPort: port + 1 },
|
|
|
|
{ expectedPort: port + 2 },
|
|
|
|
{ expectedPort: port + 3 }
|
2017-06-11 21:01:27 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: ['--inspect', `--inspect-port=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1 }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: ['--inspect', `--debug-port=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1 }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=0.0.0.0:${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1, expectedHost: '0.0.0.0' }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=127.0.0.1:${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1, expectedHost: '127.0.0.1' }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (common.hasIPv6) {
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=[::]:${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1, expectedHost: '::' }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
|
2017-07-10 21:23:22 +02:00
|
|
|
port = debuggerPort + offset++ * 5;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=[::1]:${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
workers: [{ expectedPort: port + 1, expectedHost: '::1' }]
|
2017-06-11 21:01:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-10 21:24:49 +02:00
|
|
|
// These tests check that setting inspectPort in cluster.settings
|
|
|
|
// would take effect and override port incrementing behavior
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: port + 2 },
|
|
|
|
workers: [{ expectedPort: port + 2 }]
|
2017-07-10 21:24:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'addTwo' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedPort: port + 2 },
|
|
|
|
{ expectedPort: port + 4 }
|
2017-07-10 21:24:49 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'string' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'null' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'bignumber' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'negativenumber' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'bignumberfunc' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 'strfunc' },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [{}]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: port, execArgv: ['--inspect'] },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedPort: port }
|
2017-07-10 21:24:49 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [`--inspect=${port}`],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 0 },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedInitialPort: 0 },
|
|
|
|
{ expectedInitialPort: 0 },
|
|
|
|
{ expectedInitialPort: 0 }
|
2017-07-10 21:24:49 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
|
|
|
|
spawnMaster({
|
|
|
|
execArgv: [],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: 0 },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedInitialPort: 0 },
|
|
|
|
{ expectedInitialPort: 0 },
|
|
|
|
{ expectedInitialPort: 0 }
|
2017-07-10 21:24:49 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
defaultPortCase.then(() => {
|
|
|
|
port = debuggerPort + offset++ * 5;
|
|
|
|
defaultPortCase = spawnMaster({
|
|
|
|
execArgv: ['--inspect'],
|
2017-07-11 02:55:21 +02:00
|
|
|
clusterSettings: { inspectPort: port + 2 },
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: [
|
2017-07-11 02:55:21 +02:00
|
|
|
{ expectedInitialPort: port + 2 }
|
2017-07-10 21:24:49 +02:00
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-06-11 21:01:27 +02:00
|
|
|
function masterProcessMain() {
|
|
|
|
const workers = JSON.parse(process.env.workers);
|
2019-02-08 00:37:04 +01:00
|
|
|
const clusterSettings = JSON.parse(process.env.clusterSettings) || {};
|
2018-03-15 14:22:36 +01:00
|
|
|
const badPortError = { type: RangeError, code: 'ERR_SOCKET_BAD_PORT' };
|
2017-07-10 21:24:49 +02:00
|
|
|
let debugPort = process.debugPort;
|
2017-06-11 21:01:27 +02:00
|
|
|
|
|
|
|
for (const worker of workers) {
|
2017-07-10 21:24:49 +02:00
|
|
|
const params = {};
|
|
|
|
|
|
|
|
if (worker.expectedPort) {
|
|
|
|
params.expectedPort = worker.expectedPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker.expectedInitialPort) {
|
|
|
|
params.expectedInitialPort = worker.expectedInitialPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker.expectedHost) {
|
|
|
|
params.expectedHost = worker.expectedHost;
|
|
|
|
}
|
|
|
|
|
2019-02-08 00:37:04 +01:00
|
|
|
clusterSettings.execArgv = clusterSettings.execArgv ?
|
|
|
|
clusterSettings.execArgv.concat(['--expose-internals']) :
|
|
|
|
process.execArgv.concat(['--expose-internals']);
|
|
|
|
|
|
|
|
if (clusterSettings.inspectPort === 'addTwo') {
|
|
|
|
clusterSettings.inspectPort = common.mustCall(
|
|
|
|
() => { return debugPort += 2; },
|
|
|
|
workers.length
|
|
|
|
);
|
|
|
|
} else if (clusterSettings.inspectPort === 'string') {
|
|
|
|
clusterSettings.inspectPort = 'string';
|
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (clusterSettings.inspectPort === 'null') {
|
|
|
|
clusterSettings.inspectPort = null;
|
2017-07-10 21:24:49 +02:00
|
|
|
cluster.setupMaster(clusterSettings);
|
2019-02-08 00:37:04 +01:00
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (clusterSettings.inspectPort === 'bignumber') {
|
|
|
|
clusterSettings.inspectPort = 1293812;
|
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (clusterSettings.inspectPort === 'negativenumber') {
|
|
|
|
clusterSettings.inspectPort = -9776;
|
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (clusterSettings.inspectPort === 'bignumberfunc') {
|
|
|
|
clusterSettings.inspectPort = common.mustCall(
|
|
|
|
() => 123121,
|
|
|
|
workers.length
|
|
|
|
);
|
|
|
|
|
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (clusterSettings.inspectPort === 'strfunc') {
|
|
|
|
clusterSettings.inspectPort = common.mustCall(
|
|
|
|
() => 'invalidPort',
|
|
|
|
workers.length
|
|
|
|
);
|
|
|
|
|
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
|
|
|
common.expectsError(() => {
|
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
|
|
|
}, badPortError);
|
|
|
|
|
|
|
|
return;
|
2017-07-10 21:24:49 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 00:37:04 +01:00
|
|
|
cluster.setupMaster(clusterSettings);
|
|
|
|
|
2017-07-10 21:24:49 +02:00
|
|
|
cluster.fork(params).on('exit', common.mustCall(checkExitCode));
|
2017-06-11 21:01:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function workerProcessMain() {
|
2017-07-11 02:55:21 +02:00
|
|
|
const { expectedPort, expectedInitialPort, expectedHost } = process.env;
|
2019-02-08 00:37:04 +01:00
|
|
|
const debugOptions =
|
|
|
|
require('internal/options').getOptionValue('--inspect-port');
|
2017-07-10 21:24:49 +02:00
|
|
|
|
|
|
|
if ('expectedPort' in process.env) {
|
|
|
|
assert.strictEqual(process.debugPort, +expectedPort);
|
|
|
|
}
|
2017-06-11 21:01:27 +02:00
|
|
|
|
2017-07-10 21:24:49 +02:00
|
|
|
if ('expectedInitialPort' in process.env) {
|
|
|
|
assert.strictEqual(debugOptions.port, +expectedInitialPort);
|
|
|
|
}
|
2017-06-11 21:01:27 +02:00
|
|
|
|
2017-07-10 21:24:49 +02:00
|
|
|
if ('expectedHost' in process.env) {
|
|
|
|
assert.strictEqual(debugOptions.host, expectedHost);
|
2017-06-11 21:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2017-07-11 02:55:21 +02:00
|
|
|
function spawnMaster({ execArgv, workers, clusterSettings = {} }) {
|
2017-07-10 21:24:49 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
childProcess.fork(__filename, {
|
2017-10-23 09:47:39 +02:00
|
|
|
env: Object.assign({}, process.env, {
|
2017-07-10 21:24:49 +02:00
|
|
|
workers: JSON.stringify(workers),
|
|
|
|
clusterSettings: JSON.stringify(clusterSettings),
|
|
|
|
testProcess: true
|
2017-10-23 09:47:39 +02:00
|
|
|
}),
|
2019-02-08 00:37:04 +01:00
|
|
|
execArgv: execArgv.concat(['--expose-internals'])
|
2017-07-10 21:24:49 +02:00
|
|
|
}).on('exit', common.mustCall((code, signal) => {
|
|
|
|
checkExitCode(code, signal);
|
|
|
|
resolve();
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 21:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkExitCode(code, signal) {
|
|
|
|
assert.strictEqual(code, 0);
|
|
|
|
assert.strictEqual(signal, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process.env.testProcess) {
|
|
|
|
testRunnerMain();
|
|
|
|
} else if (cluster.isMaster) {
|
|
|
|
masterProcessMain();
|
|
|
|
} else {
|
|
|
|
workerProcessMain();
|
|
|
|
}
|