Skip to content

Commit 9ba551f

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: #11921 Fixes: #11912 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 019a20a commit 9ba551f

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

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

+28-17
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,46 @@ const net = require('net');
1313
const key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem');
1414
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem');
1515

16-
const T = 100;
17-
16+
let tlsSocket;
1817
// tls server
1918
const tlsServer = tls.createServer({ cert, key }, (socket) => {
20-
setTimeout(() => {
21-
socket.on('error', (error) => {
22-
assert.strictEqual(error.code, 'EINVAL');
23-
tlsServer.close();
24-
netServer.close();
25-
});
26-
socket.write('bar');
27-
}, T * 2);
19+
tlsSocket = socket;
20+
socket.on('error', common.mustCall((error) => {
21+
assert.strictEqual(error.code, 'EINVAL');
22+
tlsServer.close();
23+
netServer.close();
24+
}));
2825
});
2926

27+
let netSocket;
3028
// plain tcp server
3129
const netServer = net.createServer((socket) => {
32-
// if client wants to use tls
30+
// if client wants to use tls
3331
tlsServer.emit('connection', socket);
3432

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

0 commit comments

Comments
 (0)