Skip to content

Commit 2ab1237

Browse files
TrottMyles Borins
authored andcommitted
test: fix flaky test-net-socket-timeout-unref
Throw immediately on socket timeout rather than checking boolean in exit handler. PR-URL: #6003Fixes: #5128 Reviewed-By: Myles Borins <myles.borins@gmail.com>
1 parent dd25984 commit 2ab1237

1 file changed

Lines changed: 16 additions & 20 deletions

File tree

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
'use strict';
2-
varcommon=require('../common');
3-
varassert=require('assert');
4-
varnet=require('net');
52

6-
varserver=net.createServer(function(c){
3+
// Test that unref'ed sockets with timeouts do not prevent exit.
4+
5+
constcommon=require('../common');
6+
constnet=require('net');
7+
8+
constserver=net.createServer(function(c){
79
c.write('hello');
810
c.unref();
911
});
1012
server.listen(common.PORT);
1113
server.unref();
1214

13-
vartimedout=false;
1415
varconnections=0;
15-
varsockets=[];
16-
vardelays=[8,5,3,6,2,4];
16+
constsockets=[];
17+
constdelays=[8,5,3,6,2,4];
1718

1819
delays.forEach(function(T){
19-
varsocket=net.createConnection(common.PORT,'localhost');
20-
socket.on('connect',function(){
20+
constsocket=net.createConnection(common.PORT,'localhost');
21+
socket.on('connect',common.mustCall(function(){
2122
if(++connections===delays.length){
2223
sockets.forEach(function(s){
23-
s[0].setTimeout(s[1]*1000,function(){
24-
timedout=true;
25-
s[0].destroy();
24+
s.socket.setTimeout(s.timeout,function(){
25+
s.socket.destroy();
26+
thrownewError('socket timed out unexpectedly');
2627
});
2728

28-
s[0].unref();
29+
s.socket.unref();
2930
});
3031
}
31-
});
32-
33-
sockets.push([socket,T]);
34-
});
32+
}));
3533

36-
process.on('exit',function(){
37-
assert.strictEqual(timedout,false,
38-
'Socket timeout should not hold loop open');
34+
sockets.push({socket: socket,timeout: T*1000});
3935
});

0 commit comments

Comments
 (0)