0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/async-hooks/test-writewrap.js
Trevor Norris 1e44fd960f
async_wrap: run destroy in uv_timer_t
Calling the destroy callbacks in a uv_idle_t causes a timing issue where
if a handle or request is closed then the class isn't deleted until
uv_close() callbacks are called (which happens after the poll phase).
This results in some destroy callbacks not being called just before the
application exits. So instead switch the destroy callbacks to be called
in a uv_timer_t with the timeout set to zero.

When uv_run() is called with UV_RUN_ONCE the final operation of the
event loop is to process all remaining timers. By setting the timeout to
zero it results in the destroy callbacks being processed after
uv_close() but before uv_run() returned. Processing the destroyed ids
that were previously missed.

Also, process the destroy_ids_list() in a do {} while() loop that makes
sure the vector is empty before returning. Which also makes running
clear() unnecessary.

Fixes: https://github.com/nodejs/node/issues/13262
PR-URL: https://github.com/nodejs/node/pull/13369
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2017-06-02 16:21:27 -06:00

99 lines
2.5 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const initHooks = require('./init-hooks');
const fs = require('fs');
const { checkInvocations } = require('./hook-checks');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const tls = require('tls');
const hooks = initHooks();
hooks.enable();
//
// Creating server and listening on port
//
const server = tls
.createServer({
cert: fs.readFileSync(common.fixturesDir + '/test_cert.pem'),
key: fs.readFileSync(common.fixturesDir + '/test_key.pem')
})
.on('listening', common.mustCall(onlistening))
.on('secureConnection', common.mustCall(onsecureConnection))
.listen(common.PORT);
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0,
'no WRITEWRAP when server created');
function onlistening() {
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0,
'no WRITEWRAP when server is listening');
//
// Creating client and connecting it to server
//
tls
.connect(common.PORT, { rejectUnauthorized: false })
.on('secureConnect', common.mustCall(onsecureConnect));
assert.strictEqual(hooks.activitiesOfTypes('WRITEWRAP').length, 0,
'no WRITEWRAP when client created');
}
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', 'write wrap');
assert.strictEqual(typeof w.uid, 'number', 'uid is a number');
assert.strictEqual(typeof w.triggerId, 'number', 'triggerId is a 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');
}