Skip to content

Commit cf68280

Browse files
Sebastian PlesciucTrott
authored andcommitted
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: #12487 Ref: #12376 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent bd97e48 commit cf68280

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function master() {
4949
cluster.fork();
5050

5151
// Wait until all workers are listening.
52-
cluster.on('listening', common.mustCall(() => {
52+
cluster.on('listening', common.mustCall((worker, address) => {
5353
if (++listening < NUM_WORKERS)
5454
return;
5555

@@ -60,7 +60,7 @@ function master() {
6060
doSend();
6161

6262
function doSend() {
63-
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1', afterSend);
63+
socket.send(buf, 0, buf.length, address.port, address.address, afterSend);
6464
}
6565

6666
function afterSend() {
@@ -111,5 +111,5 @@ function worker() {
111111
}
112112
}, PACKETS_PER_WORKER));
113113

114-
socket.bind(common.PORT);
114+
socket.bind(0);
115115
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PACKETS_PER_WORKER = 10;
2626

2727
const cluster = require('cluster');
2828
const dgram = require('dgram');
29+
const assert = require('assert');
2930

3031

3132
if (common.isWindows) {
@@ -45,7 +46,14 @@ function master() {
4546

4647
// Start listening on a socket.
4748
const socket = dgram.createSocket('udp4');
48-
socket.bind(common.PORT);
49+
socket.bind({ port: 0 }, common.mustCall(() => {
50+
51+
// Fork workers.
52+
for (let i = 0; i < NUM_WORKERS; i++) {
53+
const worker = cluster.fork();
54+
worker.send({ port: socket.address().port });
55+
}
56+
}));
4957

5058
// Disconnect workers when the expected number of messages have been
5159
// received.
@@ -61,10 +69,6 @@ function master() {
6169
cluster.disconnect();
6270
}
6371
}, NUM_WORKERS * PACKETS_PER_WORKER));
64-
65-
// Fork workers.
66-
for (let i = 0; i < NUM_WORKERS; i++)
67-
cluster.fork();
6872
}
6973

7074

@@ -78,13 +82,17 @@ function worker() {
7882
// send(), explicitly bind them to an ephemeral port.
7983
socket.bind(0);
8084

81-
// There is no guarantee that a sent dgram packet will be received so keep
82-
// sending until disconnect.
83-
const interval = setInterval(() => {
84-
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1');
85-
}, 1);
85+
process.on('message', common.mustCall((msg) => {
86+
assert(msg.port);
87+
88+
// There is no guarantee that a sent dgram packet will be received so keep
89+
// sending until disconnect.
90+
const interval = setInterval(() => {
91+
socket.send(buf, 0, buf.length, msg.port, '127.0.0.1');
92+
}, 1);
8693

87-
cluster.worker.on('disconnect', () => {
88-
clearInterval(interval);
89-
});
94+
cluster.worker.on('disconnect', () => {
95+
clearInterval(interval);
96+
});
97+
}));
9098
}

0 commit comments

Comments
 (0)