node/test/parallel/test-async-hooks-http-parser-destroy.js
Rich Trott 11e1e0050b test: improve test-async-hooks-http-parser-destroy
Improve reporting in test-async-hooks-http-parser-destroy when failing.

Before, failures looked like this (edited slightly to conform to our
commit message 72-char line length restriction):

    The expression evaluated to a falsy value:
    assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0)

Now, you get a slightly better idea of what's up. (Is it missing one ID?
More than one? All of them?):

    AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
    + actual - expected ... Lines skipped

      [
        156,
    ...
        757,
    -   761,
        765
      ]

PR-URL: https://github.com/nodejs/node/pull/27319
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2019-05-04 21:19:19 -07:00

69 lines
1.6 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const http = require('http');
// Regression test for https://github.com/nodejs/node/issues/19859.
// Checks that matching destroys are emitted when creating new/reusing old http
// parser instances.
const N = 50;
const KEEP_ALIVE = 100;
const createdIds = [];
const destroyedIds = [];
async_hooks.createHook({
init: (asyncId, type) => {
if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') {
createdIds.push(asyncId);
}
},
destroy: (asyncId) => {
if (createdIds.includes(asyncId)) {
destroyedIds.push(asyncId);
}
if (destroyedIds.length === 2 * N) {
server.close();
}
}
}).enable();
const server = http.createServer((req, res) => {
res.end('Hello');
});
const keepAliveAgent = new http.Agent({
keepAlive: true,
keepAliveMsecs: KEEP_ALIVE,
});
server.listen(0, function() {
for (let i = 0; i < N; ++i) {
(function makeRequest() {
http.get({
port: server.address().port,
agent: keepAliveAgent
}, function(res) {
res.resume();
});
})();
}
});
function checkOnExit() {
assert.deepStrictEqual(destroyedIds.sort(), createdIds.sort());
// There should be two IDs for each request.
assert.strictEqual(createdIds.length, N * 2);
}
process.on('SIGTERM', () => {
// Catching SIGTERM and calling `process.exit(1)` so that the `exit` event
// is triggered and the assertions are checked. This can be useful for
// troubleshooting this test if it times out.
process.exit(1);
});
// Ordinary exit.
process.on('exit', checkOnExit);