Skip to content

Commit 485bb1b

Browse files
TrottMylesBorins
authored andcommitted
test: fix flaky test-tls-socket-close
Replace timer/timeout race with event-based ordering, eliminating test flakiness. PR-URL: #11921Fixes: #11912 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent abbf6e3 commit 485bb1b

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

‎test/parallel/test-tls-socket-close.js‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,46 @@ const net = require('net');
99
constkey=fs.readFileSync(common.fixturesDir+'/keys/agent2-key.pem');
1010
constcert=fs.readFileSync(common.fixturesDir+'/keys/agent2-cert.pem');
1111

12-
constT=100;
13-
12+
lettlsSocket;
1413
// tls server
1514
consttlsServer=tls.createServer({ cert, key },(socket)=>{
16-
setTimeout(()=>{
17-
socket.on('error',(error)=>{
18-
assert.strictEqual(error.code,'EINVAL');
19-
tlsServer.close();
20-
netServer.close();
21-
});
22-
socket.write('bar');
23-
},T*2);
15+
tlsSocket=socket;
16+
socket.on('error',common.mustCall((error)=>{
17+
assert.strictEqual(error.code,'EINVAL');
18+
tlsServer.close();
19+
netServer.close();
20+
}));
2421
});
2522

23+
letnetSocket;
2624
// plain tcp server
2725
constnetServer=net.createServer((socket)=>{
28-
// if client wants to use tls
26+
// if client wants to use tls
2927
tlsServer.emit('connection',socket);
3028

31-
socket.setTimeout(T,()=>{
32-
// this breaks if TLSSocket is already managing the socket:
33-
socket.destroy();
34-
});
29+
netSocket=socket;
3530
}).listen(0,common.mustCall(function(){
36-
3731
// connect client
3832
tls.connect({
3933
host: 'localhost',
4034
port: this.address().port,
4135
rejectUnauthorized: false
42-
}).write('foo');
36+
}).write('foo','utf8',common.mustCall(()=>{
37+
assert(netSocket);
38+
netSocket.setTimeout(1,common.mustCall(()=>{
39+
assert(tlsSocket);
40+
// this breaks if TLSSocket is already managing the socket:
41+
netSocket.destroy();
42+
constinterval=setInterval(()=>{
43+
// Checking this way allows us to do the write at a time that causes a
44+
// segmentation fault (not always, but often) in Node.js 7.7.3 and
45+
// earlier. If we instead, for example, wait on the `close` event, then
46+
// it will not segmentation fault, which is what this test is all about.
47+
if(tlsSocket._handle._parent.bytesRead===0){
48+
tlsSocket.write('bar');
49+
clearInterval(interval);
50+
}
51+
},1);
52+
}));
53+
}));
4354
}));

0 commit comments

Comments
 (0)