From 6148f4fffb8c7dc4e757d3fa3ea7ae1fb96b57d2 Mon Sep 17 00:00:00 2001 From: Sebastian Plesciuc Date: Sat, 22 Apr 2017 13:07:30 +0300 Subject: [PATCH] test: dynamic port in parallel cluster tests Removed common.PORT from test-cluster-message, test-cluster-server-restart-none, test-cluster-server-restart-rr and test-cluster-shared-handle-bind-error to eliminate the possibility that a dynamic port used in another test will collide with common.PORT. Refs: https://github.com/nodejs/node/issues/12376 --- test/parallel/test-cluster-message.js | 8 ++++---- test/parallel/test-cluster-server-restart-none.js | 4 ++-- test/parallel/test-cluster-server-restart-rr.js | 4 ++-- test/parallel/test-cluster-shared-handle-bind-error.js | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-cluster-message.js b/test/parallel/test-cluster-message.js index 7d1cef780f3306..ee7100185a5788 100644 --- a/test/parallel/test-cluster-message.js +++ b/test/parallel/test-cluster-message.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -const common = require('../common'); +require('../common'); const assert = require('assert'); const cluster = require('cluster'); const net = require('net'); @@ -60,7 +60,7 @@ if (cluster.isWorker) { maybeReply(); }); - server.listen(common.PORT, '127.0.0.1'); + server.listen(0, '127.0.0.1'); } else if (cluster.isMaster) { const checks = { @@ -109,9 +109,9 @@ if (cluster.isWorker) { }); // When a TCP server is listening in the worker connect to it - worker.on('listening', function() { + worker.on('listening', function(address) { - client = net.connect(common.PORT, function() { + client = net.connect(address.port, function() { // Send message to worker. worker.send('message from master'); }); diff --git a/test/parallel/test-cluster-server-restart-none.js b/test/parallel/test-cluster-server-restart-none.js index dc44bafd66317b..c89ce8b67a3405 100644 --- a/test/parallel/test-cluster-server-restart-none.js +++ b/test/parallel/test-cluster-server-restart-none.js @@ -23,10 +23,10 @@ if (cluster.isMaster) { } else { const net = require('net'); const server = net.createServer(); - server.listen(common.PORT, common.mustCall(() => { + server.listen(0, common.mustCall(() => { if (cluster.worker.id === 2) { server.close(() => { - server.listen(common.PORT, common.mustCall(() => { + server.listen(0, common.mustCall(() => { server.close(() => { process.disconnect(); }); diff --git a/test/parallel/test-cluster-server-restart-rr.js b/test/parallel/test-cluster-server-restart-rr.js index 724a77b71a4fa9..3b26562c5dfa01 100644 --- a/test/parallel/test-cluster-server-restart-rr.js +++ b/test/parallel/test-cluster-server-restart-rr.js @@ -23,10 +23,10 @@ if (cluster.isMaster) { } else { const net = require('net'); const server = net.createServer(); - server.listen(common.PORT, common.mustCall(() => { + server.listen(0, common.mustCall(() => { if (cluster.worker.id === 2) { server.close(() => { - server.listen(common.PORT, common.mustCall(() => { + server.listen(0, common.mustCall(() => { server.close(() => { process.disconnect(); }); diff --git a/test/parallel/test-cluster-shared-handle-bind-error.js b/test/parallel/test-cluster-shared-handle-bind-error.js index 3c1faaaa486d14..c58b6ed7d95001 100644 --- a/test/parallel/test-cluster-shared-handle-bind-error.js +++ b/test/parallel/test-cluster-shared-handle-bind-error.js @@ -31,16 +31,17 @@ if (cluster.isMaster) { // Hog the TCP port so that when the worker tries to bind, it'll fail. const server = net.createServer(common.mustNotCall()); - server.listen(common.PORT, common.mustCall(() => { - const worker = cluster.fork(); + server.listen(0, common.mustCall(() => { + const worker = cluster.fork({PORT: server.address().port}); worker.on('exit', common.mustCall((exitCode) => { assert.strictEqual(exitCode, 0); server.close(); })); })); } else { + assert(process.env.PORT); const s = net.createServer(common.mustNotCall()); - s.listen(common.PORT, common.mustNotCall('listen should have failed')); + s.listen(process.env.PORT, common.mustNotCall('listen should have failed')); s.on('error', common.mustCall((err) => { assert.strictEqual(err.code, 'EADDRINUSE'); process.disconnect();