Browse Source

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>
v5.x
cjihrig 9 years ago
committed by Rod Vagg
parent
commit
525afc1cb3
  1. 32
      test/parallel/test-net-socket-local-address.js

32
test/parallel/test-net-socket-local-address.js

@ -12,35 +12,29 @@ if (common.inFreeBSDJail) {
var conns = 0; var conns = 0;
var clientLocalPorts = []; var clientLocalPorts = [];
var serverRemotePorts = []; var serverRemotePorts = [];
const client = new net.Socket();
const server = net.createServer(function(socket) { const server = net.createServer(socket => {
serverRemotePorts.push(socket.remotePort); serverRemotePorts.push(socket.remotePort);
testConnect(); socket.end();
}); });
const client = new net.Socket(); server.on('close', common.mustCall(() => {
server.on('close', common.mustCall(function() {
assert.deepEqual(clientLocalPorts, serverRemotePorts, assert.deepEqual(clientLocalPorts, serverRemotePorts,
'client and server should agree on the ports used'); 'client and server should agree on the ports used');
assert.equal(2, conns); assert.strictEqual(2, conns);
})); }));
server.listen(common.PORT, common.localhostIPv4, testConnect); server.listen(common.PORT, common.localhostIPv4, connect);
function testConnect() { function connect() {
if (conns > serverRemotePorts.length || conns > clientLocalPorts.length) { if (conns === 2) {
// We're waiting for a callback to fire. server.close();
return; return;
} }
if (conns === 2) {
return server.close();
}
client.connect(common.PORT, common.localhostIPv4, function() {
clientLocalPorts.push(this.localPort);
this.once('close', testConnect);
this.destroy();
});
conns++; conns++;
client.once('close', connect);
client.connect(common.PORT, common.localhostIPv4, () => {
clientLocalPorts.push(client.localPort);
});
} }

Loading…
Cancel
Save