test: improve test-async-hooks-http-parser-destroy
Improve asserts to distinguish between reequest and response parsers. Change the assert sequence to first assert on the number of ids to easier identify if some operation is missing/incomplete. Destroy HTTP agent once expected number of events have been seen to avoid waiting on socket timeouts. Refs: https://github.com/nodejs/node/issues/28112 PR-URL: https://github.com/nodejs/node/pull/28253 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
42081580ac
commit
8007134189
@ -11,19 +11,34 @@ const http = require('http');
|
|||||||
const N = 50;
|
const N = 50;
|
||||||
const KEEP_ALIVE = 100;
|
const KEEP_ALIVE = 100;
|
||||||
|
|
||||||
const createdIds = [];
|
const createdIdsIncomingMessage = [];
|
||||||
const destroyedIds = [];
|
const createdIdsClientRequest = [];
|
||||||
|
const destroyedIdsIncomingMessage = [];
|
||||||
|
const destroyedIdsClientRequest = [];
|
||||||
|
|
||||||
async_hooks.createHook({
|
async_hooks.createHook({
|
||||||
init: (asyncId, type) => {
|
init: (asyncId, type) => {
|
||||||
if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') {
|
if (type === 'HTTPINCOMINGMESSAGE') {
|
||||||
createdIds.push(asyncId);
|
createdIdsIncomingMessage.push(asyncId);
|
||||||
|
}
|
||||||
|
if (type === 'HTTPCLIENTREQUEST') {
|
||||||
|
createdIdsClientRequest.push(asyncId);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
destroy: (asyncId) => {
|
destroy: (asyncId) => {
|
||||||
if (createdIds.includes(asyncId)) {
|
if (createdIdsIncomingMessage.includes(asyncId)) {
|
||||||
destroyedIds.push(asyncId);
|
destroyedIdsIncomingMessage.push(asyncId);
|
||||||
}
|
}
|
||||||
if (destroyedIds.length === 2 * N) {
|
if (createdIdsClientRequest.includes(asyncId)) {
|
||||||
|
destroyedIdsClientRequest.push(asyncId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destroyedIdsClientRequest.length === N && keepAliveAgent) {
|
||||||
|
keepAliveAgent.destroy();
|
||||||
|
keepAliveAgent = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destroyedIdsIncomingMessage.length === N && server.listening) {
|
||||||
server.close();
|
server.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,18 +48,18 @@ const server = http.createServer((req, res) => {
|
|||||||
res.end('Hello');
|
res.end('Hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
const keepAliveAgent = new http.Agent({
|
let keepAliveAgent = new http.Agent({
|
||||||
keepAlive: true,
|
keepAlive: true,
|
||||||
keepAliveMsecs: KEEP_ALIVE,
|
keepAliveMsecs: KEEP_ALIVE,
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(0, function() {
|
server.listen(0, () => {
|
||||||
for (let i = 0; i < N; ++i) {
|
for (let i = 0; i < N; ++i) {
|
||||||
(function makeRequest() {
|
(function makeRequest() {
|
||||||
http.get({
|
http.get({
|
||||||
port: server.address().port,
|
port: server.address().port,
|
||||||
agent: keepAliveAgent
|
agent: keepAliveAgent
|
||||||
}, function(res) {
|
}, (res) => {
|
||||||
res.resume();
|
res.resume();
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
@ -52,9 +67,15 @@ server.listen(0, function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function checkOnExit() {
|
function checkOnExit() {
|
||||||
assert.deepStrictEqual(destroyedIds.sort(), createdIds.sort());
|
assert.strictEqual(createdIdsIncomingMessage.length, N);
|
||||||
// There should be two IDs for each request.
|
assert.strictEqual(createdIdsClientRequest.length, N);
|
||||||
assert.strictEqual(createdIds.length, N * 2);
|
assert.strictEqual(destroyedIdsIncomingMessage.length, N);
|
||||||
|
assert.strictEqual(destroyedIdsClientRequest.length, N);
|
||||||
|
|
||||||
|
assert.deepStrictEqual(destroyedIdsIncomingMessage.sort(),
|
||||||
|
createdIdsIncomingMessage.sort());
|
||||||
|
assert.deepStrictEqual(destroyedIdsClientRequest.sort(),
|
||||||
|
createdIdsClientRequest.sort());
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on('SIGTERM', () => {
|
process.on('SIGTERM', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user