test: refactor test-debug-signal-cluster

Notable changes include removing one (but not all) hard-coded ports,
using `common.fail()`, and tidying conditionals and assertions.

PR-URL: https://github.com/nodejs/node/pull/8289
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott 2016-08-26 13:43:09 -07:00
parent 2cc7fa5e7d
commit e192825de0
2 changed files with 25 additions and 24 deletions

View File

@ -1,12 +1,13 @@
var http = require('http'); 'use strict';
var cluster = require('cluster');
var common = require('../../common'); const http = require('http');
const cluster = require('cluster');
function handleRequest(request, response) { function handleRequest(request, response) {
response.end('hello world\n'); response.end('hello world\n');
} }
var NUMBER_OF_WORKERS = 2; const NUMBER_OF_WORKERS = 2;
var workersOnline = 0; var workersOnline = 0;
if (cluster.isMaster) { if (cluster.isMaster) {
@ -18,7 +19,7 @@ if (cluster.isMaster) {
process.on('message', function(msg) { process.on('message', function(msg) {
if (msg.type === 'getpids') { if (msg.type === 'getpids') {
var pids = []; const pids = [];
pids.push(process.pid); pids.push(process.pid);
for (var key in cluster.workers) for (var key in cluster.workers)
pids.push(cluster.workers[key].process.pid); pids.push(cluster.workers[key].process.pid);
@ -30,6 +31,6 @@ if (cluster.isMaster) {
cluster.fork(); cluster.fork();
} }
} else { } else {
var server = http.createServer(handleRequest); const server = http.createServer(handleRequest);
server.listen(common.PORT); server.listen(0);
} }

View File

@ -1,21 +1,23 @@
'use strict'; 'use strict';
var common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;
var port = common.PORT + 1; // The fixture uses common.PORT. const common = require('../common');
var args = ['--debug-port=' + port, const assert = require('assert');
common.fixturesDir + '/clustered-server/app.js']; const spawn = require('child_process').spawn;
var options = { stdio: ['inherit', 'inherit', 'pipe', 'ipc'] }; const path = require('path');
var child = spawn(process.execPath, args, options);
var outputLines = []; const port = common.PORT;
const serverPath = path.join(common.fixturesDir, 'clustered-server', 'app.js');
const args = [`--debug-port=${port}`, serverPath];
const options = { stdio: ['inherit', 'inherit', 'pipe', 'ipc'] };
const child = spawn(process.execPath, args, options);
const outputLines = [];
var waitingForDebuggers = false; var waitingForDebuggers = false;
var pids = null; var pids;
child.stderr.on('data', function(data) { child.stderr.on('data', function(data) {
var lines = data.toString().replace(/\r/g, '').trim().split('\n'); const lines = data.toString().replace(/\r/g, '').trim().split('\n');
lines.forEach(function(line) { lines.forEach(function(line) {
console.log('> ' + line); console.log('> ' + line);
@ -40,7 +42,7 @@ child.stderr.on('data', function(data) {
} }
}); });
if (outputLines.length >= expectedLines.length) if (outputLines.length === expectedLines.length)
onNoMoreLines(); onNoMoreLines();
}); });
@ -50,7 +52,7 @@ function onNoMoreLines() {
} }
setTimeout(function testTimedOut() { setTimeout(function testTimedOut() {
assert(false, 'test timed out.'); common.fail('test timed out');
}, common.platformTimeout(4000)).unref(); }, common.platformTimeout(4000)).unref();
process.on('exit', function onExit() { process.on('exit', function onExit() {
@ -61,7 +63,7 @@ process.on('exit', function onExit() {
}); });
}); });
var expectedLines = [ const expectedLines = [
'Starting debugger agent.', 'Starting debugger agent.',
'Debugger listening on 127.0.0.1:' + (port + 0), 'Debugger listening on 127.0.0.1:' + (port + 0),
'Starting debugger agent.', 'Starting debugger agent.',
@ -77,7 +79,5 @@ function assertOutputLines() {
outputLines.sort(); outputLines.sort();
expectedLines.sort(); expectedLines.sort();
assert.equal(outputLines.length, expectedLines.length); assert.deepStrictEqual(outputLines, expectedLines);
for (var i = 0; i < expectedLines.length; i++)
assert(expectedLines[i].includes(outputLines[i]));
} }