From daf65354756eec3ace9d935a351a401768cc162e Mon Sep 17 00:00:00 2001 From: Sebastian Plesciuc Date: Tue, 18 Apr 2017 18:20:31 +0300 Subject: [PATCH] test: dynamic port in cluster worker dgram Remove common.PORT from test-cluster-dgram-1 and test-cluster-dgram-2, in order to eliminate the possibility of port collision. PR-URL: https://github.com/nodejs/node/pull/12487 Ref: https://github.com/nodejs/node/issues/12376 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Santiago Gimeno --- test/parallel/test-cluster-dgram-1.js | 6 ++--- test/parallel/test-cluster-dgram-2.js | 34 +++++++++++++++++---------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/test/parallel/test-cluster-dgram-1.js b/test/parallel/test-cluster-dgram-1.js index 25391997755f6b..0d3d435c312916 100644 --- a/test/parallel/test-cluster-dgram-1.js +++ b/test/parallel/test-cluster-dgram-1.js @@ -28,7 +28,7 @@ function master() { cluster.fork(); // Wait until all workers are listening. - cluster.on('listening', common.mustCall(() => { + cluster.on('listening', common.mustCall((worker, address) => { if (++listening < NUM_WORKERS) return; @@ -39,7 +39,7 @@ function master() { doSend(); function doSend() { - socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1', afterSend); + socket.send(buf, 0, buf.length, address.port, address.address, afterSend); } function afterSend() { @@ -90,5 +90,5 @@ function worker() { } }, PACKETS_PER_WORKER)); - socket.bind(common.PORT); + socket.bind(0); } diff --git a/test/parallel/test-cluster-dgram-2.js b/test/parallel/test-cluster-dgram-2.js index 37aea29e1a4263..277434f79b7102 100644 --- a/test/parallel/test-cluster-dgram-2.js +++ b/test/parallel/test-cluster-dgram-2.js @@ -5,6 +5,7 @@ const PACKETS_PER_WORKER = 10; const cluster = require('cluster'); const dgram = require('dgram'); +const assert = require('assert'); if (common.isWindows) { @@ -24,7 +25,14 @@ function master() { // Start listening on a socket. const socket = dgram.createSocket('udp4'); - socket.bind(common.PORT); + socket.bind({ port: 0 }, common.mustCall(() => { + + // Fork workers. + for (let i = 0; i < NUM_WORKERS; i++) { + const worker = cluster.fork(); + worker.send({ port: socket.address().port }); + } + })); // Disconnect workers when the expected number of messages have been // received. @@ -40,10 +48,6 @@ function master() { cluster.disconnect(); } }, NUM_WORKERS * PACKETS_PER_WORKER)); - - // Fork workers. - for (let i = 0; i < NUM_WORKERS; i++) - cluster.fork(); } @@ -57,13 +61,17 @@ function worker() { // send(), explicitly bind them to an ephemeral port. socket.bind(0); - // There is no guarantee that a sent dgram packet will be received so keep - // sending until disconnect. - const interval = setInterval(() => { - socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1'); - }, 1); + process.on('message', common.mustCall((msg) => { + assert(msg.port); + + // There is no guarantee that a sent dgram packet will be received so keep + // sending until disconnect. + const interval = setInterval(() => { + socket.send(buf, 0, buf.length, msg.port, '127.0.0.1'); + }, 1); - cluster.worker.on('disconnect', () => { - clearInterval(interval); - }); + cluster.worker.on('disconnect', () => { + clearInterval(interval); + }); + })); }