0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-tls-socket-close.js
Rich Trott dde4f0f1bf test: fix flaky test-tls-socket-close
Add error listener to ignore `ECONNRESET`. Makes test reliable while it
still segfaults (as expected) on Node.js 7.7.3. It might not be possible
to eliminate the probable race causing `ECONNRESET` without also
eliminating the required segfault-inducing part of the test. (Or maybe
it's totally possible. If you figure it out, hey cool, submit a pull
request.)

PR-URL: https://github.com/nodejs/node/pull/13529
Fixes: https://github.com/nodejs/node/issues/13184
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-06-09 22:30:43 -07:00

70 lines
2.0 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const assert = require('assert');
const tls = require('tls');
const fs = require('fs');
const net = require('net');
const key = fs.readFileSync(`${common.fixturesDir}/keys/agent2-key.pem`);
const cert = fs.readFileSync(`${common.fixturesDir}/keys/agent2-cert.pem`);
let tlsSocket;
// tls server
const tlsServer = tls.createServer({ cert, key }, (socket) => {
tlsSocket = socket;
socket.on('error', common.mustCall((error) => {
assert.strictEqual(error.code, 'EINVAL');
tlsServer.close();
netServer.close();
}));
});
let netSocket;
// plain tcp server
const netServer = net.createServer((socket) => {
// if client wants to use tls
tlsServer.emit('connection', socket);
netSocket = socket;
}).listen(0, common.mustCall(function() {
connectClient(netServer);
}));
function connectClient(server) {
const tlsConnection = tls.connect({
host: 'localhost',
port: server.address().port,
rejectUnauthorized: false
});
tlsConnection.write('foo', 'utf8', common.mustCall(() => {
assert(netSocket);
netSocket.setTimeout(1, common.mustCall(() => {
assert(tlsSocket);
// this breaks if TLSSocket is already managing the socket:
netSocket.destroy();
const interval = setInterval(() => {
// Checking this way allows us to do the write at a time that causes a
// segmentation fault (not always, but often) in Node.js 7.7.3 and
// earlier. If we instead, for example, wait on the `close` event, then
// it will not segmentation fault, which is what this test is all about.
if (tlsSocket._handle._parent.bytesRead === 0) {
tlsSocket.write('bar');
clearInterval(interval);
}
}, 1);
}));
}));
tlsConnection.on('error', (e) => {
// Tolerate the occasional ECONNRESET.
// Ref: https://github.com/nodejs/node/issues/13184
if (e.code !== 'ECONNRESET')
throw e;
});
}