node/test/async-hooks/test-writewrap.js
Rich Trott e18390032e test: remove common.PORT from parallel tests
`common.PORT` should not be used in parallel tests because another test
may experience a collision with `common.PORT` when using port 0 to get
an open port. This has been observed to result in test failures in CI.

PR-URL: https://github.com/nodejs/node/pull/17410
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Lance Ball <lball@redhat.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-12-05 00:07:27 -08:00

94 lines
2.2 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const initHooks = require('./init-hooks');
const fixtures = require('../common/fixtures');
const { checkInvocations } = require('./hook-checks');
const tls = require('tls');
const hooks = initHooks();
hooks.enable();
//
// Creating server and listening on port
//
const server = tls
.createServer({
cert: fixtures.readSync('test_cert.pem'),
key: fixtures.readSync('test_key.pem')
})
.on('listening', common.mustCall(onlistening))
.on('secureConnection', common.mustCall(onsecureConnection))
.listen(0);
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
function onlistening() {
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
//
// Creating client and connecting it to server
//
tls
.connect(server.address().port, { rejectUnauthorized: false })
.on('secureConnect', common.mustCall(onsecureConnect));
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0);
}
function checkDestroyedWriteWraps(n, stage) {
const as = hooks.activitiesOfTypes('WRITEWRAP');
assert.strictEqual(as.length, n, `${n} WRITEWRAPs when ${stage}`);
function checkValidWriteWrap(w) {
assert.strictEqual(w.type, 'WRITEWRAP');
assert.strictEqual(typeof w.uid, 'number');
assert.strictEqual(typeof w.triggerAsyncId, 'number');
checkInvocations(w, { init: 1 }, `when ${stage}`);
}
as.forEach(checkValidWriteWrap);
}
function onsecureConnection() {
//
// Server received client connection
//
checkDestroyedWriteWraps(3, 'server got secure connection');
}
function onsecureConnect() {
//
// Client connected to server
//
checkDestroyedWriteWraps(4, 'client connected');
//
// Destroying client socket
//
this.destroy();
checkDestroyedWriteWraps(4, 'client destroyed');
//
// Closing server
//
server.close(common.mustCall(onserverClosed));
checkDestroyedWriteWraps(4, 'server closing');
}
function onserverClosed() {
checkDestroyedWriteWraps(4, 'server closed');
}
process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('WRITEWRAP');
checkDestroyedWriteWraps(4, 'process exits');
}