Skip to content

test: fix global.PORT in dgram-cluster-test-1 #12491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions test/parallel/test-cluster-dgram-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,37 @@ const assert = require('assert');
const cluster = require('cluster');
const dgram = require('dgram');

function getPort(cb) {
const s = dgram.createSocket({ type: 'udp4', reuseAddr: true });
s.bind(0, () => {
const port = s.address().port;
s.close();
cb(null, port);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time this port is used by the test, there is a possibility that this would be assigned to someother process by the operating system.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or Am I missing something here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port assignment was relay on the suggestion of @benjamingr in
#12426 (comment)

Maybe the right solution is to close this port only in the end of the test.

});
}

if (common.isWindows) {
common.skip('dgram clustering is currently not supported ' +
'on windows.');
return;
}

if (cluster.isMaster)
master();
else
// We are creating unique port only for the in the master cluster
if (cluster.isMaster) {
getPort((info, port) => {
master(port);
});
} else {
worker();
}


function master() {
function master(port) {
let listening = 0;

// Fork 4 workers.
for (let i = 0; i < NUM_WORKERS; i++)
cluster.fork();
cluster.fork().send(port);

// Wait until all workers are listening.
cluster.on('listening', common.mustCall(() => {
Expand All @@ -60,7 +72,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, port, '127.0.0.1', afterSend);
}

function afterSend() {
Expand Down Expand Up @@ -106,10 +118,13 @@ function worker() {

// Every 10 messages, notify the master.
if (received === PACKETS_PER_WORKER) {
process.send({received: received});
process.send({ received: received });
socket.close();
}
}, PACKETS_PER_WORKER));

socket.bind(common.PORT);
// We are getting the PORT through the process
// message in the master process => cluster.fork().send(port);
process.on('message', common.mustCall((port, info) => {
socket.bind(port);
}));
}