11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst async_hooks = require('async_hooks'); 51cb0ef41Sopenharmony_ciconst http = require('http'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/19859. 81cb0ef41Sopenharmony_ci// Checks that matching destroys are emitted when creating new/reusing old http 91cb0ef41Sopenharmony_ci// parser instances. 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst N = 50; 121cb0ef41Sopenharmony_ciconst KEEP_ALIVE = 100; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst createdIdsIncomingMessage = []; 151cb0ef41Sopenharmony_ciconst createdIdsClientRequest = []; 161cb0ef41Sopenharmony_ciconst destroyedIdsIncomingMessage = []; 171cb0ef41Sopenharmony_ciconst destroyedIdsClientRequest = []; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciasync_hooks.createHook({ 201cb0ef41Sopenharmony_ci init: (asyncId, type) => { 211cb0ef41Sopenharmony_ci if (type === 'HTTPINCOMINGMESSAGE') { 221cb0ef41Sopenharmony_ci createdIdsIncomingMessage.push(asyncId); 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci if (type === 'HTTPCLIENTREQUEST') { 251cb0ef41Sopenharmony_ci createdIdsClientRequest.push(asyncId); 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci }, 281cb0ef41Sopenharmony_ci destroy: (asyncId) => { 291cb0ef41Sopenharmony_ci if (createdIdsIncomingMessage.includes(asyncId)) { 301cb0ef41Sopenharmony_ci destroyedIdsIncomingMessage.push(asyncId); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci if (createdIdsClientRequest.includes(asyncId)) { 331cb0ef41Sopenharmony_ci destroyedIdsClientRequest.push(asyncId); 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci if (destroyedIdsClientRequest.length === N && keepAliveAgent) { 371cb0ef41Sopenharmony_ci keepAliveAgent.destroy(); 381cb0ef41Sopenharmony_ci keepAliveAgent = undefined; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci if (destroyedIdsIncomingMessage.length === N && server.listening) { 421cb0ef41Sopenharmony_ci server.close(); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci}).enable(); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => { 481cb0ef41Sopenharmony_ci req.on('close', common.mustCall(() => { 491cb0ef41Sopenharmony_ci req.on('readable', common.mustNotCall()); 501cb0ef41Sopenharmony_ci })); 511cb0ef41Sopenharmony_ci res.end('Hello'); 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cilet keepAliveAgent = new http.Agent({ 551cb0ef41Sopenharmony_ci keepAlive: true, 561cb0ef41Sopenharmony_ci keepAliveMsecs: KEEP_ALIVE, 571cb0ef41Sopenharmony_ci}); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciserver.listen(0, () => { 601cb0ef41Sopenharmony_ci for (let i = 0; i < N; ++i) { 611cb0ef41Sopenharmony_ci (function makeRequest() { 621cb0ef41Sopenharmony_ci http.get({ 631cb0ef41Sopenharmony_ci port: server.address().port, 641cb0ef41Sopenharmony_ci agent: keepAliveAgent 651cb0ef41Sopenharmony_ci }, (res) => { 661cb0ef41Sopenharmony_ci res.resume(); 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci })(); 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci}); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_cifunction checkOnExit() { 731cb0ef41Sopenharmony_ci assert.strictEqual(createdIdsIncomingMessage.length, N); 741cb0ef41Sopenharmony_ci assert.strictEqual(createdIdsClientRequest.length, N); 751cb0ef41Sopenharmony_ci assert.strictEqual(destroyedIdsIncomingMessage.length, N); 761cb0ef41Sopenharmony_ci assert.strictEqual(destroyedIdsClientRequest.length, N); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci assert.deepStrictEqual(destroyedIdsIncomingMessage.sort(), 791cb0ef41Sopenharmony_ci createdIdsIncomingMessage.sort()); 801cb0ef41Sopenharmony_ci assert.deepStrictEqual(destroyedIdsClientRequest.sort(), 811cb0ef41Sopenharmony_ci createdIdsClientRequest.sort()); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciprocess.on('SIGTERM', () => { 851cb0ef41Sopenharmony_ci // Catching SIGTERM and calling `process.exit(1)` so that the `exit` event 861cb0ef41Sopenharmony_ci // is triggered and the assertions are checked. This can be useful for 871cb0ef41Sopenharmony_ci // troubleshooting this test if it times out. 881cb0ef41Sopenharmony_ci process.exit(1); 891cb0ef41Sopenharmony_ci}); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci// Ordinary exit. 921cb0ef41Sopenharmony_ciprocess.on('exit', checkOnExit); 93