Skip to content

Commit

Permalink
test: use random ports where possible
Browse files Browse the repository at this point in the history
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: nodejs#7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
  • Loading branch information
mscdex committed Jun 11, 2016
1 parent 624734e commit 2bc7841
Show file tree
Hide file tree
Showing 358 changed files with 1,554 additions and 1,516 deletions.
13 changes: 7 additions & 6 deletions test/parallel/test-async-wrap-check-providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ net.createServer(function(c) {
net.createServer(function(c) {
c.end();
this.close(checkTLS);
}).listen(common.PORT, function() {
net.connect(common.PORT, noop);
}).listen(0, function() {
net.connect(this.address().port, noop);
});

dgram.createSocket('udp4').bind(common.PORT, function() {
this.send(Buffer.allocUnsafe(2), 0, 2, common.PORT, '::', () => {
dgram.createSocket('udp4').bind(0, function() {
this.send(Buffer.allocUnsafe(2), 0, 2, this.address().port, '::', () => {
this.close();
});
});
Expand All @@ -100,8 +100,9 @@ function checkTLS() {
cert: fs.readFileSync(common.fixturesDir + '/keys/ec-cert.pem')
};
const server = tls.createServer(options, noop)
.listen(common.PORT, function() {
tls.connect(common.PORT, { rejectUnauthorized: false }, function() {
.listen(0, function() {
const connectOpts = { rejectUnauthorized: false };
tls.connect(this.address().port, connectOpts, function() {
this.destroy();
server.close();
});
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-async-wrap-disabled-propagate-parent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
Expand Down Expand Up @@ -40,8 +40,8 @@ const server = net.createServer(function(c) {
c.end();
this.close();
});
}).listen(common.PORT, function() {
net.connect(common.PORT, noop);
}).listen(0, function() {
net.connect(this.address().port, noop);
});

async_wrap.disable();
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-async-wrap-propagate-parent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const net = require('net');
const async_wrap = process.binding('async_wrap');
Expand Down Expand Up @@ -40,8 +40,8 @@ const server = net.createServer(function(c) {
c.end();
this.close();
});
}).listen(common.PORT, function() {
net.connect(common.PORT, noop);
}).listen(0, function() {
net.connect(this.address().port, noop);
});


Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-beforeexit-event.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
require('../common');
var assert = require('assert');
var net = require('net');
var common = require('../common');
var revivals = 0;
var deaths = 0;

Expand Down Expand Up @@ -29,7 +29,7 @@ function tryTimer() {
function tryListen() {
console.log('create a server');
net.createServer()
.listen(common.PORT)
.listen(0)
.on('listening', function() {
revivals++;
this.close();
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-child-process-disconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ if (process.argv[2] === 'child') {

// when the server is ready tell parent
server.on('listening', function() {
process.send('ready');
process.send({ msg: 'ready', port: server.address().port });
});

server.listen(common.PORT);
server.listen(0);

} else {
// testcase
Expand All @@ -65,11 +65,11 @@ if (process.argv[2] === 'child') {
});

// when child is listening
child.on('message', function(msg) {
if (msg === 'ready') {
child.on('message', function(obj) {
if (obj && obj.msg === 'ready') {

// connect to child using TCP to know if disconnect was emitted
var socket = net.connect(common.PORT);
var socket = net.connect(obj.port);

socket.on('data', function(data) {
data = data.toString();
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-fork-dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if (process.argv[2] === 'child') {
msg,
0,
msg.length,
common.PORT,
server.address().port,
'127.0.0.1',
function(err) {
if (err) throw err;
Expand All @@ -98,7 +98,7 @@ if (process.argv[2] === 'child') {
client.close();
};

server.bind(common.PORT, '127.0.0.1');
server.bind(0, '127.0.0.1');

process.once('exit', function() {
assert(parentGotMessage);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-fork-net.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const assert = require('assert');
require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const net = require('net');

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-fork-net2.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if (process.argv[2] === 'child') {

var j = count, client;
while (j--) {
client = net.connect(common.PORT, '127.0.0.1');
client = net.connect(this.address().port, '127.0.0.1');
client.on('error', function() {
// This can happen if we kill the child too early.
// The client should still get a close event afterwards.
Expand All @@ -125,7 +125,7 @@ if (process.argv[2] === 'child') {
child3.kill();
}));

server.listen(common.PORT, '127.0.0.1');
server.listen(0, '127.0.0.1');

var closeServer = function() {
server.close();
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-fork-regr-gh-2847.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ var server = net.createServer(function(s) {
setTimeout(function() {
s.destroy();
}, 100);
}).listen(common.PORT, function() {
}).listen(0, function() {
var worker = cluster.fork();

function send(callback) {
var s = net.connect(common.PORT, function() {
var s = net.connect(server.address().port, function() {
worker.send({}, s, callback);
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-recv-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function master() {
});
proc.stdout.on('data', function(data) {
assert.equal(data, 'ok\r\n');
net.createServer(common.fail).listen(common.PORT, function() {
net.createServer(common.fail).listen(0, function() {
handle = this._handle;
proc.send('one');
proc.send('two', handle);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-send-keep-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ if (process.argv[2] !== 'child') {
}));
});

server.listen(common.PORT, () => {
const socket = net.connect(common.PORT, common.localhostIPv4);
server.listen(0, () => {
const socket = net.connect(server.address().port, common.localhostIPv4);
socket.setEncoding('utf8');
socket.on('data', (data) => result += data);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-send-returns-boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ s.on('exit', function() {
handle.close();
});

net.createServer(common.fail).listen(common.PORT, function() {
net.createServer(common.fail).listen(0, function() {
handle = this._handle;
assert.strictEqual(s.send('one', handle), true);
assert.strictEqual(s.send('two', handle), true);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-verify-failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function verify() {
.verify(certPem, 'asdfasdfas', 'base64');
}

server.listen(common.PORT, function() {
server.listen(0, function() {
tls.connect({
port: common.PORT,
port: this.address().port,
rejectUnauthorized: false
}, function() {
verify();
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-dgram-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var family_ipv4 = 'IPv4';
socket_ipv4.on('listening', function() {
var address_ipv4 = socket_ipv4.address();
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
assert.strictEqual(address_ipv4.port, common.PORT);
assert.strictEqual(typeof address_ipv4.port, 'number');
assert.ok(isFinite(address_ipv4.port));
assert.ok(address_ipv4.port > 0);
assert.strictEqual(address_ipv4.family, family_ipv4);
socket_ipv4.close();
});
Expand All @@ -20,7 +22,7 @@ socket_ipv4.on('error', function(e) {
socket_ipv4.close();
});

socket_ipv4.bind(common.PORT, common.localhostIPv4);
socket_ipv4.bind(0, common.localhostIPv4);

// IPv6 Test
var localhost_ipv6 = '::1';
Expand All @@ -30,7 +32,9 @@ var family_ipv6 = 'IPv6';
socket_ipv6.on('listening', function() {
var address_ipv6 = socket_ipv6.address();
assert.strictEqual(address_ipv6.address, localhost_ipv6);
assert.strictEqual(address_ipv6.port, common.PORT);
assert.strictEqual(typeof address_ipv6.port, 'number');
assert.ok(isFinite(address_ipv6.port));
assert.ok(address_ipv6.port > 0);
assert.strictEqual(address_ipv6.family, family_ipv6);
socket_ipv6.close();
});
Expand All @@ -40,4 +44,4 @@ socket_ipv6.on('error', function(e) {
socket_ipv6.close();
});

socket_ipv6.bind(common.PORT, localhost_ipv6);
socket_ipv6.bind(0, localhost_ipv6);
12 changes: 8 additions & 4 deletions test/parallel/test-dgram-bind-default-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ if (common.inFreeBSDJail) {
return;
}

dgram.createSocket('udp4').bind(common.PORT + 0, common.mustCall(function() {
assert.equal(this.address().port, common.PORT + 0);
dgram.createSocket('udp4').bind(0, common.mustCall(function() {
assert.strictEqual(typeof this.address().port, 'number');
assert.ok(isFinite(this.address().port));
assert.ok(this.address().port > 0);
assert.equal(this.address().address, '0.0.0.0');
this.close();
}));
Expand All @@ -20,8 +22,10 @@ if (!common.hasIPv6) {
return;
}

dgram.createSocket('udp6').bind(common.PORT + 1, common.mustCall(function() {
assert.equal(this.address().port, common.PORT + 1);
dgram.createSocket('udp6').bind(0, common.mustCall(function() {
assert.strictEqual(typeof this.address().port, 'number');
assert.ok(isFinite(this.address().port));
assert.ok(this.address().port > 0);
var address = this.address().address;
if (address === '::ffff:0.0.0.0')
address = '::';
Expand Down
39 changes: 20 additions & 19 deletions test/parallel/test-dgram-empty-packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ if (process.platform === 'darwin') {

client = dgram.createSocket('udp4');

client.bind(common.PORT);

function callback() {
callbacks++;
if (callbacks == 2) {
clearTimeout(timer);
client.close();
} else if (callbacks > 2) {
throw new Error('the callbacks should be called only two times');
client.bind(0, function() {
function callback() {
callbacks++;
if (callbacks == 2) {
clearTimeout(timer);
client.close();
} else if (callbacks > 2) {
throw new Error('the callbacks should be called only two times');
}
}
}

client.on('message', function(buffer, bytes) {
callback();
});

client.send(
Buffer.allocUnsafe(1), 0, 0, common.PORT, '127.0.0.1', (err, len) => {
client.on('message', function(buffer, bytes) {
callback();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
const port = this.address().port;
client.send(
Buffer.allocUnsafe(1), 0, 0, port, '127.0.0.1', (err, len) => {
callback();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
});
12 changes: 6 additions & 6 deletions test/parallel/test-dgram-error-message-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ var socket_ipv4 = dgram.createSocket('udp4');
socket_ipv4.on('listening', common.fail);

socket_ipv4.on('error', common.mustCall(function(e) {
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1:' + common.PORT);
assert.strictEqual(e.port, undefined);
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1');
assert.equal(e.address, '1.1.1.1');
assert.equal(e.port, common.PORT);
assert.equal(e.code, 'EADDRNOTAVAIL');
socket_ipv4.close();
}));

socket_ipv4.bind(common.PORT, '1.1.1.1');
socket_ipv4.bind(0, '1.1.1.1');

// IPv6 Test
var socket_ipv6 = dgram.createSocket('udp6');
Expand All @@ -27,10 +27,10 @@ socket_ipv6.on('error', common.mustCall(function(e) {
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.
var allowed = ['EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EPROTONOSUPPORT'];
assert.notEqual(allowed.indexOf(e.code), -1);
assert.equal(e.message, 'bind ' + e.code + ' 111::1:' + common.PORT);
assert.strictEqual(e.port, undefined);
assert.equal(e.message, 'bind ' + e.code + ' 111::1');
assert.equal(e.address, '111::1');
assert.equal(e.port, common.PORT);
socket_ipv6.close();
}));

socket_ipv6.bind(common.PORT, '111::1');
socket_ipv6.bind(0, '111::1');
9 changes: 5 additions & 4 deletions test/parallel/test-dgram-implicit-bind.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var common = require('../common');
require('../common');
var assert = require('assert');
var dgram = require('dgram');

Expand All @@ -22,8 +22,9 @@ target.on('message', function(buf) {

target.on('listening', function() {
// Second .send() call should not throw a bind error.
source.send(Buffer.from('abc'), 0, 3, common.PORT, '127.0.0.1');
source.send(Buffer.from('def'), 0, 3, common.PORT, '127.0.0.1');
const port = this.address().port;
source.send(Buffer.from('abc'), 0, 3, port, '127.0.0.1');
source.send(Buffer.from('def'), 0, 3, port, '127.0.0.1');
});

target.bind(common.PORT);
target.bind(0);
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-multicast-setTTL.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
let thrown = false;

socket.bind(common.PORT);
socket.bind(0);
socket.on('listening', function() {
socket.setMulticastTTL(16);

Expand Down
Loading

0 comments on commit 2bc7841

Please sign in to comment.