node/test/parallel/test-net-socket-local-address.js
cjihrig 6cfd0b5a32 test: fix flaky test-net-socket-local-address
Prior to this commit, the test was flaky because it was
executing the majority of its logic in a function called from
the client and multiple events on the server. This commit
simplifies the test by separating the server's connection and
listening events, and isolating the client logic.

Refs: https://github.com/nodejs/node/pull/4476
Refs: https://github.com/nodejs/node/pull/4644
PR-URL: https://github.com/nodejs/node/pull/4650
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2016-01-13 11:43:46 -05:00

41 lines
955 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
// skip test in FreeBSD jails
if (common.inFreeBSDJail) {
console.log('1..0 # Skipped: 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.deepEqual(clientLocalPorts, serverRemotePorts,
'client and server should agree on the ports used');
assert.strictEqual(2, conns);
}));
server.listen(common.PORT, common.localhostIPv4, connect);
function connect() {
if (conns === 2) {
server.close();
return;
}
conns++;
client.once('close', connect);
client.connect(common.PORT, common.localhostIPv4, () => {
clientLocalPorts.push(client.localPort);
});
}