From 2142b6d3d192a857f6ba8425962b4ea29c57a7c8 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Sun, 16 Jun 2019 22:00:54 +0200 Subject: [PATCH] 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 Reviewed-By: Daijiro Wachi Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott Reviewed-By: Ruben Bridgewater --- .../test-async-hooks-http-parser-destroy.js | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-async-hooks-http-parser-destroy.js b/test/parallel/test-async-hooks-http-parser-destroy.js index 8e92f6359c6e72..22b5a0ff11b889 100644 --- a/test/parallel/test-async-hooks-http-parser-destroy.js +++ b/test/parallel/test-async-hooks-http-parser-destroy.js @@ -11,19 +11,34 @@ const http = require('http'); const N = 50; const KEEP_ALIVE = 100; -const createdIds = []; -const destroyedIds = []; +const createdIdsIncomingMessage = []; +const createdIdsClientRequest = []; +const destroyedIdsIncomingMessage = []; +const destroyedIdsClientRequest = []; + async_hooks.createHook({ init: (asyncId, type) => { - if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') { - createdIds.push(asyncId); + if (type === 'HTTPINCOMINGMESSAGE') { + createdIdsIncomingMessage.push(asyncId); + } + if (type === 'HTTPCLIENTREQUEST') { + createdIdsClientRequest.push(asyncId); } }, destroy: (asyncId) => { - if (createdIds.includes(asyncId)) { - destroyedIds.push(asyncId); + if (createdIdsIncomingMessage.includes(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(); } } @@ -33,18 +48,18 @@ const server = http.createServer((req, res) => { res.end('Hello'); }); -const keepAliveAgent = new http.Agent({ +let keepAliveAgent = new http.Agent({ keepAlive: true, keepAliveMsecs: KEEP_ALIVE, }); -server.listen(0, function() { +server.listen(0, () => { for (let i = 0; i < N; ++i) { (function makeRequest() { http.get({ port: server.address().port, agent: keepAliveAgent - }, function(res) { + }, (res) => { res.resume(); }); })(); @@ -52,9 +67,15 @@ server.listen(0, function() { }); function checkOnExit() { - assert.deepStrictEqual(destroyedIds.sort(), createdIds.sort()); - // There should be two IDs for each request. - assert.strictEqual(createdIds.length, N * 2); + assert.strictEqual(createdIdsIncomingMessage.length, N); + assert.strictEqual(createdIdsClientRequest.length, N); + 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', () => {