test: refactor parallel/test-tls-async-cb-after-socket-end

PR-URL: https://github.com/nodejs/node/pull/18985
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
juggernaut451 2018-03-18 00:33:26 +05:30 committed by Ruben Bridgewater
parent cbc7eb7eec
commit d1156da815
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -1,5 +1,4 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
if (!common.hasCrypto) if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
@ -7,60 +6,60 @@ const fixtures = require('../common/fixtures');
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET; const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
const tls = require('tls'); const tls = require('tls');
// Check tls async callback after socket ends
const options = { const options = {
secureOptions: SSL_OP_NO_TICKET, secureOptions: SSL_OP_NO_TICKET,
key: fixtures.readSync('test_key.pem'), key: fixtures.readSync('test_key.pem'),
cert: fixtures.readSync('test_cert.pem') cert: fixtures.readSync('test_cert.pem')
}; };
const server = tls.createServer(options, function(c) { const server = tls.createServer(options, common.mustCall());
});
let sessionCb = null; let sessionCb = null;
let client = null; let client = null;
server.on('newSession', function(key, session, done) { server.on('newSession', common.mustCall((key, session, done) => {
done(); done();
}); }));
server.on('resumeSession', function(id, cb) { server.on('resumeSession', common.mustCall((id, cb) => {
sessionCb = cb; sessionCb = cb;
next(); next();
}); }));
server.listen(0, function() { server.listen(0, common.mustCall(() => {
const clientOpts = { const clientOpts = {
port: this.address().port, port: server.address().port,
rejectUnauthorized: false, rejectUnauthorized: false,
session: false session: false
}; };
const s1 = tls.connect(clientOpts, function() { const s1 = tls.connect(clientOpts, common.mustCall(() => {
clientOpts.session = s1.getSession(); clientOpts.session = s1.getSession();
console.log('1st secure'); console.log('1st secure');
s1.destroy(); s1.destroy();
const s2 = tls.connect(clientOpts, function(s) { const s2 = tls.connect(clientOpts, (s) => {
console.log('2nd secure'); console.log('2nd secure');
s2.destroy(); s2.destroy();
}).on('connect', function() { }).on('connect', common.mustCall(() => {
console.log('2nd connected'); console.log('2nd connected');
client = s2; client = s2;
next(); next();
}); }));
}); }));
}); }));
function next() { function next() {
if (!client || !sessionCb) if (!client || !sessionCb)
return; return;
client.destroy(); client.destroy();
setTimeout(function() { setTimeout(common.mustCall(() => {
sessionCb(); sessionCb();
server.close(); server.close();
}, 100); }), 100);
} }