node/test/parallel/test-net-socket-local-address.js
Brian White 2bc7841d0f
test: use random ports where possible
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: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

41 lines
953 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
// skip test in FreeBSD jails
if (common.inFreeBSDJail) {
common.skip('In a FreeBSD jail');
return;
}
var conns = 0;
var clientLocalPorts = [];
var serverRemotePorts = [];
const client = new net.Socket();
const server = net.createServer((socket) => {
serverRemotePorts.push(socket.remotePort);
socket.end();
});
server.on('close', common.mustCall(() => {
assert.deepStrictEqual(clientLocalPorts, serverRemotePorts,
'client and server should agree on the ports used');
assert.strictEqual(2, conns);
}));
server.listen(0, common.localhostIPv4, connect);
function connect() {
if (conns === 2) {
server.close();
return;
}
conns++;
client.once('close', connect);
client.connect(server.address().port, common.localhostIPv4, () => {
clientLocalPorts.push(client.localPort);
});
}