From 1cd41e7a56587c0c4a05fa74719ac983e9293c6e Mon Sep 17 00:00:00 2001 From: Sebastian Plesciuc Date: Thu, 4 May 2017 10:54:22 +0300 Subject: [PATCH] test: dynamic port in cluster disconnect Removed common.PORT from test-cluster-disconnect to eliminate the possibility that a port used in another test will collide with common.PORT. PR-URL: https://github.com/nodejs/node/pull/12545 Refs: https://github.com/nodejs/node/issues/12376 Reviewed-By: Luigi Pinca Reviewed-By: Refael Ackermann Reviewed-By: Alexey Orlenko Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Gibson Fahnestock --- test/parallel/test-cluster-disconnect.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-cluster-disconnect.js b/test/parallel/test-cluster-disconnect.js index 78447f689a8f45..491cae9b1e6c20 100644 --- a/test/parallel/test-cluster-disconnect.js +++ b/test/parallel/test-cluster-disconnect.js @@ -7,14 +7,14 @@ const net = require('net'); if (cluster.isWorker) { net.createServer((socket) => { socket.end('echo'); - }).listen(common.PORT, '127.0.0.1'); + }).listen(0, '127.0.0.1'); net.createServer((socket) => { socket.end('echo'); - }).listen(common.PORT + 1, '127.0.0.1'); - + }).listen(0, '127.0.0.1'); } else if (cluster.isMaster) { const servers = 2; + const serverPorts = new Set(); // test a single TCP server const testConnection = (port, cb) => { @@ -26,6 +26,7 @@ if (cluster.isWorker) { // check result socket.on('end', common.mustCall(() => { cb(result === 'echo'); + serverPorts.delete(port); })); }); }; @@ -33,9 +34,10 @@ if (cluster.isWorker) { // test both servers created in the cluster const testCluster = (cb) => { let done = 0; + const portsArray = Array.from(serverPorts); for (let i = 0; i < servers; i++) { - testConnection(common.PORT + i, (success) => { + testConnection(portsArray[i], (success) => { assert.ok(success); done += 1; if (done === servers) { @@ -51,7 +53,9 @@ if (cluster.isWorker) { let online = 0; for (let i = 0, l = workers; i < l; i++) { - cluster.fork().on('listening', common.mustCall(() => { + cluster.fork().on('listening', common.mustCall((address) => { + serverPorts.add(address.port); + online += 1; if (online === workers * servers) { cb();