Skip to content

Commit d615757

Browse files
cjihrigMyles Borins
authored andcommitted
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 <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 40c8e6d commit d615757

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,29 @@ if (common.inFreeBSDJail) {
1212
varconns=0;
1313
varclientLocalPorts=[];
1414
varserverRemotePorts=[];
15-
16-
constserver=net.createServer(function(socket){
15+
constclient=newnet.Socket();
16+
constserver=net.createServer(socket=>{
1717
serverRemotePorts.push(socket.remotePort);
18-
testConnect();
18+
socket.end();
1919
});
2020

21-
constclient=newnet.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-
functiontestConnect(){
32-
if(conns>serverRemotePorts.length||conns>clientLocalPorts.length){
33-
// We're waiting for a callback to fire.
29+
functionconnect(){
30+
if(conns===2){
31+
server.close();
3432
return;
3533
}
3634

37-
if(conns===2){
38-
returnserver.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)