Skip to content

Commit f0b5afe

Browse files
Sebastian Plesciucevanlucas
Sebastian Plesciuc
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 47b3992 commit f0b5afe

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function master() {
2828
cluster.fork();
2929

3030
// Wait until all workers are listening.
31-
cluster.on('listening', common.mustCall(() => {
31+
cluster.on('listening', common.mustCall((worker, address) => {
3232
if (++listening < NUM_WORKERS)
3333
return;
3434

@@ -39,7 +39,7 @@ function master() {
3939
doSend();
4040

4141
function doSend() {
42-
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1', afterSend);
42+
socket.send(buf, 0, buf.length, address.port, address.address, afterSend);
4343
}
4444

4545
function afterSend() {
@@ -90,5 +90,5 @@ function worker() {
9090
}
9191
}, PACKETS_PER_WORKER));
9292

93-
socket.bind(common.PORT);
93+
socket.bind(0);
9494
}

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const PACKETS_PER_WORKER = 10;
55

66
const cluster = require('cluster');
77
const dgram = require('dgram');
8+
const assert = require('assert');
89

910

1011
if (common.isWindows) {
@@ -24,7 +25,14 @@ function master() {
2425

2526
// Start listening on a socket.
2627
const socket = dgram.createSocket('udp4');
27-
socket.bind(common.PORT);
28+
socket.bind({ port: 0 }, common.mustCall(() => {
29+
30+
// Fork workers.
31+
for (let i = 0; i < NUM_WORKERS; i++) {
32+
const worker = cluster.fork();
33+
worker.send({ port: socket.address().port });
34+
}
35+
}));
2836

2937
// Disconnect workers when the expected number of messages have been
3038
// received.
@@ -40,10 +48,6 @@ function master() {
4048
cluster.disconnect();
4149
}
4250
}, NUM_WORKERS * PACKETS_PER_WORKER));
43-
44-
// Fork workers.
45-
for (let i = 0; i < NUM_WORKERS; i++)
46-
cluster.fork();
4751
}
4852

4953

@@ -57,13 +61,17 @@ function worker() {
5761
// send(), explicitly bind them to an ephemeral port.
5862
socket.bind(0);
5963

60-
// There is no guarantee that a sent dgram packet will be received so keep
61-
// sending until disconnect.
62-
const interval = setInterval(() => {
63-
socket.send(buf, 0, buf.length, common.PORT, '127.0.0.1');
64-
}, 1);
64+
process.on('message', common.mustCall((msg) => {
65+
assert(msg.port);
66+
67+
// There is no guarantee that a sent dgram packet will be received so keep
68+
// sending until disconnect.
69+
const interval = setInterval(() => {
70+
socket.send(buf, 0, buf.length, msg.port, '127.0.0.1');
71+
}, 1);
6572

66-
cluster.worker.on('disconnect', () => {
67-
clearInterval(interval);
68-
});
73+
cluster.worker.on('disconnect', () => {
74+
clearInterval(interval);
75+
});
76+
}));
6977
}

0 commit comments

Comments
 (0)