Skip to content

Commit

Permalink
test: fix flaky test-net-socket-local-address
Browse files Browse the repository at this point in the history
test-net-socket-local-address had a race condition that resulted in
unreliability on FreeBSD and Windows. This changes fixes the issue.

Fixes: nodejs#2475
PR-URL: nodejs#4109
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
  • Loading branch information
Trott authored and Michael Scovetta committed Apr 2, 2016
1 parent 0ccfe4b commit 19c8af2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 0 additions & 1 deletion test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ test-child-process-exit-code : PASS,FLAKY
[$system==solaris] # Also applies to SmartOS

[$system==freebsd]
test-net-socket-local-address : PASS,FLAKY
10 changes: 8 additions & 2 deletions test/parallel/test-net-socket-local-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var serverRemotePorts = [];

const server = net.createServer(function(socket) {
serverRemotePorts.push(socket.remotePort);
conns++;
testConnect();
});

const client = new net.Socket();
Expand All @@ -29,12 +29,18 @@ server.on('close', common.mustCall(function() {
server.listen(common.PORT, common.localhostIPv4, testConnect);

function testConnect() {
if (conns == 2) {
if (conns > serverRemotePorts.length || conns > clientLocalPorts.length) {
// We're waiting for a callback to fire.
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++;
}

0 comments on commit 19c8af2

Please sign in to comment.