diff --git a/test/parallel/test-dgram-close-in-listening.js b/test/parallel/test-dgram-close-in-listening.js index e181f40de67dcf..902384e7e082d0 100644 --- a/test/parallel/test-dgram-close-in-listening.js +++ b/test/parallel/test-dgram-close-in-listening.js @@ -13,6 +13,14 @@ socket.on('listening', function() { socket.close(); }); -// adds a listener to 'listening' to send the data when -// the socket is available -socket.send(buf, 0, buf.length, common.PORT, 'localhost'); +// get a random port for send +const portGetter = dgram.createSocket('udp4') + .bind(0, 'localhost', common.mustCall(() => { + // adds a listener to 'listening' to send the data when + // the socket is available + socket.send(buf, 0, buf.length, + portGetter.address().port, + portGetter.address().address); + + portGetter.close(); + })); diff --git a/test/parallel/test-dgram-close-is-not-callback.js b/test/parallel/test-dgram-close-is-not-callback.js index e61f8904ca8191..70700a16c6a798 100644 --- a/test/parallel/test-dgram-close-is-not-callback.js +++ b/test/parallel/test-dgram-close-is-not-callback.js @@ -6,9 +6,16 @@ const buf = Buffer.alloc(1024, 42); const socket = dgram.createSocket('udp4'); -socket.send(buf, 0, buf.length, common.PORT, 'localhost'); +// get a random port for send +const portGetter = dgram.createSocket('udp4') + .bind(0, 'localhost', common.mustCall(() => { + socket.send(buf, 0, buf.length, + portGetter.address().port, + portGetter.address().address); -// if close callback is not function, ignore the argument. -socket.close('bad argument'); + // if close callback is not function, ignore the argument. + socket.close('bad argument'); + portGetter.close(); -socket.on('close', common.mustCall()); + socket.on('close', common.mustCall()); + })); diff --git a/test/parallel/test-dgram-close.js b/test/parallel/test-dgram-close.js index 65c8a4d0df17aa..01aadf2aef1bef 100644 --- a/test/parallel/test-dgram-close.js +++ b/test/parallel/test-dgram-close.js @@ -32,14 +32,23 @@ const buf = Buffer.alloc(1024, 42); let socket = dgram.createSocket('udp4'); const handle = socket._handle; -socket.send(buf, 0, buf.length, common.PORT, 'localhost'); -assert.strictEqual(socket.close(common.mustCall()), socket); -socket.on('close', common.mustCall()); -socket = null; - -// Verify that accessing handle after closure doesn't throw -setImmediate(function() { - setImmediate(function() { - console.log('Handle fd is: ', handle.fd); - }); -}); +// get a random port for send +const portGetter = dgram.createSocket('udp4') + .bind(0, 'localhost', common.mustCall(() => { + socket.send(buf, 0, buf.length, + portGetter.address().port, + portGetter.address().address); + + assert.strictEqual(socket.close(common.mustCall()), socket); + socket.on('close', common.mustCall()); + socket = null; + + // Verify that accessing handle after closure doesn't throw + setImmediate(function() { + setImmediate(function() { + console.log('Handle fd is: ', handle.fd); + }); + }); + + portGetter.close(); + })); diff --git a/test/parallel/test-dgram-exclusive-implicit-bind.js b/test/parallel/test-dgram-exclusive-implicit-bind.js index 927c9f815fc046..de7137d964d932 100644 --- a/test/parallel/test-dgram-exclusive-implicit-bind.js +++ b/test/parallel/test-dgram-exclusive-implicit-bind.js @@ -70,15 +70,15 @@ if (cluster.isMaster) { }); target.on('listening', function() { - cluster.fork(); - cluster.fork(); + cluster.fork({PORT: target.address().port}); + cluster.fork({PORT: target.address().port}); if (!common.isWindows) { - cluster.fork({BOUND: 'y'}); - cluster.fork({BOUND: 'y'}); + cluster.fork({BOUND: 'y', PORT: target.address().port}); + cluster.fork({BOUND: 'y', PORT: target.address().port}); } }); - target.bind({port: common.PORT, exclusive: true}); + target.bind({port: 0, exclusive: true}); return; } @@ -98,7 +98,8 @@ if (process.env.BOUND === 'y') { source.unref(); } +assert(process.env.PORT); const buf = Buffer.from(process.pid.toString()); const interval = setInterval(() => { - source.send(buf, common.PORT, '127.0.0.1'); + source.send(buf, process.env.PORT, '127.0.0.1'); }, 1).unref(); diff --git a/test/parallel/test-dgram-oob-buffer.js b/test/parallel/test-dgram-oob-buffer.js index 3c85ec213d2a72..136d378bbb9a46 100644 --- a/test/parallel/test-dgram-oob-buffer.js +++ b/test/parallel/test-dgram-oob-buffer.js @@ -29,12 +29,17 @@ const dgram = require('dgram'); const socket = dgram.createSocket('udp4'); const buf = Buffer.from([1, 2, 3, 4]); +const portGetter = dgram.createSocket('udp4') + .bind(0, 'localhost', common.mustCall(() => { + const address = portGetter.address(); + portGetter.close(common.mustCall(() => { + socket.send(buf, 0, 0, address.port, address.address, common.noop); + socket.send(buf, 0, 4, address.port, address.address, common.noop); + socket.send(buf, 1, 3, address.port, address.address, common.noop); + socket.send(buf, 3, 1, address.port, address.address, common.noop); + // Since length of zero means nothing, don't error despite OOB. + socket.send(buf, 4, 0, address.port, address.address, common.noop); -socket.send(buf, 0, 0, common.PORT, '127.0.0.1', common.noop); // useful? no -socket.send(buf, 0, 4, common.PORT, '127.0.0.1', common.noop); -socket.send(buf, 1, 3, common.PORT, '127.0.0.1', common.noop); -socket.send(buf, 3, 1, common.PORT, '127.0.0.1', common.noop); -// Since length of zero means nothing, don't error despite OOB. -socket.send(buf, 4, 0, common.PORT, '127.0.0.1', common.noop); - -socket.close(); + socket.close(); + })); + }));