Browse Source

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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
v6
Sebastian Plesciuc 8 years ago
committed by Rich Trott
parent
commit
cf68280ce1
  1. 6
      test/parallel/test-cluster-dgram-1.js
  2. 34
      test/parallel/test-cluster-dgram-2.js

6
test/parallel/test-cluster-dgram-1.js

@ -49,7 +49,7 @@ function master() {
cluster.fork(); cluster.fork();
// Wait until all workers are listening. // Wait until all workers are listening.
cluster.on('listening', common.mustCall(() => { cluster.on('listening', common.mustCall((worker, address) => {
if (++listening < NUM_WORKERS) if (++listening < NUM_WORKERS)
return; return;
@ -60,7 +60,7 @@ function master() {
doSend(); doSend();
function 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() { function afterSend() {
@ -111,5 +111,5 @@ function worker() {
} }
}, PACKETS_PER_WORKER)); }, PACKETS_PER_WORKER));
socket.bind(common.PORT); socket.bind(0);
} }

34
test/parallel/test-cluster-dgram-2.js

@ -26,6 +26,7 @@ const PACKETS_PER_WORKER = 10;
const cluster = require('cluster'); const cluster = require('cluster');
const dgram = require('dgram'); const dgram = require('dgram');
const assert = require('assert');
if (common.isWindows) { if (common.isWindows) {
@ -45,7 +46,14 @@ function master() {
// Start listening on a socket. // Start listening on a socket.
const socket = dgram.createSocket('udp4'); 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 // Disconnect workers when the expected number of messages have been
// received. // received.
@ -61,10 +69,6 @@ function master() {
cluster.disconnect(); cluster.disconnect();
} }
}, NUM_WORKERS * PACKETS_PER_WORKER)); }, NUM_WORKERS * PACKETS_PER_WORKER));
// Fork workers.
for (let i = 0; i < NUM_WORKERS; i++)
cluster.fork();
} }
@ -78,13 +82,17 @@ function worker() {
// send(), explicitly bind them to an ephemeral port. // send(), explicitly bind them to an ephemeral port.
socket.bind(0); socket.bind(0);
// There is no guarantee that a sent dgram packet will be received so keep process.on('message', common.mustCall((msg) => {
// sending until disconnect. assert(msg.port);
const interval = setInterval(() => {
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1'); // There is no guarantee that a sent dgram packet will be received so keep
}, 1); // sending until disconnect.
const interval = setInterval(() => {
socket.send(buf, 0, buf.length, msg.port, '127.0.0.1');
}, 1);
cluster.worker.on('disconnect', () => { cluster.worker.on('disconnect', () => {
clearInterval(interval); clearInterval(interval);
}); });
}));
} }

Loading…
Cancel
Save