Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 485bb1b

Browse filesBrowse 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 <cjihrig@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent abbf6e3 commit 485bb1b
Copy full SHA for 485bb1b

File tree

Expand file treeCollapse file tree

1 file changed

+28
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+28
-17
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-tls-socket-close.js
+28-17Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,46 @@ const net = require('net');
99
const key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem');
1010
const cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem');
1111

12-
const T = 100;
13-
12+
let tlsSocket;
1413
// tls server
1514
const tlsServer = 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+
let netSocket;
2624
// plain tcp server
2725
const netServer = 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+
const interval = 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.