diff --git a/test/parallel/test-cluster-dgram-reuse.js b/test/parallel/test-cluster-dgram-reuse.js index aed565a3806e97..0196ffb5a9bccf 100644 --- a/test/parallel/test-cluster-dgram-reuse.js +++ b/test/parallel/test-cluster-dgram-reuse.js @@ -36,5 +36,13 @@ function close() { cluster.worker.disconnect(); } -for (let i = 0; i < 2; i++) - dgram.createSocket({ type: 'udp4', reuseAddr: true }).bind(common.PORT, next); +// Creates the first socket +const socket = dgram.createSocket({ type: 'udp4', reuseAddr: true }); +// Let the OS assign a random port to the first socket +socket.bind(0, common.mustCall(function() { + // Creates the second socket using the same port from the previous one + dgram.createSocket({ type: 'udp4', reuseAddr: true }) + .bind(socket.address().port, next); + // Call next for the first socket + next.call(this); +}));