Skip to content

Commit d615757

Browse files
cjihrigMyles Borins
authored and
Myles Borins
committed
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: #4476 Refs: #4644 PR-URL: #4650 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 40c8e6d commit d615757

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

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

+13-19
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,29 @@ if (common.inFreeBSDJail) {
1212
var conns = 0;
1313
var clientLocalPorts = [];
1414
var serverRemotePorts = [];
15-
16-
const server = net.createServer(function(socket) {
15+
const client = new net.Socket();
16+
const server = net.createServer(socket => {
1717
serverRemotePorts.push(socket.remotePort);
18-
testConnect();
18+
socket.end();
1919
});
2020

21-
const client = new net.Socket();
22-
23-
server.on('close', common.mustCall(function() {
21+
server.on('close', common.mustCall(() => {
2422
assert.deepEqual(clientLocalPorts, serverRemotePorts,
2523
'client and server should agree on the ports used');
26-
assert.equal(2, conns);
24+
assert.strictEqual(2, conns);
2725
}));
2826

29-
server.listen(common.PORT, common.localhostIPv4, testConnect);
27+
server.listen(common.PORT, common.localhostIPv4, connect);
3028

31-
function testConnect() {
32-
if (conns > serverRemotePorts.length || conns > clientLocalPorts.length) {
33-
// We're waiting for a callback to fire.
29+
function connect() {
30+
if (conns === 2) {
31+
server.close();
3432
return;
3533
}
3634

37-
if (conns === 2) {
38-
return server.close();
39-
}
40-
client.connect(common.PORT, common.localhostIPv4, function() {
41-
clientLocalPorts.push(this.localPort);
42-
this.once('close', testConnect);
43-
this.destroy();
44-
});
4535
conns++;
36+
client.once('close', connect);
37+
client.connect(common.PORT, common.localhostIPv4, () => {
38+
clientLocalPorts.push(client.localPort);
39+
});
4640
}

0 commit comments

Comments
 (0)