From d648ecc4889f04d4725f81d0242e8d6249055949 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 19 Apr 2019 15:06:23 -0700 Subject: [PATCH] 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 Reviewed-By: Joyee Cheung --- .../test-async-hooks-http-parser-destroy.js | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/test/parallel/test-async-hooks-http-parser-destroy.js b/test/parallel/test-async-hooks-http-parser-destroy.js index d69c474c1d371e..8e92f6359c6e72 100644 --- a/test/parallel/test-async-hooks-http-parser-destroy.js +++ b/test/parallel/test-async-hooks-http-parser-destroy.js @@ -1,6 +1,5 @@ 'use strict'; -const common = require('../common'); -const Countdown = require('../common/countdown'); +require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); const http = require('http'); @@ -15,13 +14,18 @@ const KEEP_ALIVE = 100; const createdIds = []; const destroyedIds = []; async_hooks.createHook({ - init: common.mustCallAtLeast((asyncId, type) => { + init: (asyncId, type) => { if (type === 'HTTPINCOMINGMESSAGE' || type === 'HTTPCLIENTREQUEST') { createdIds.push(asyncId); } - }, N), + }, destroy: (asyncId) => { - destroyedIds.push(asyncId); + if (createdIds.includes(asyncId)) { + destroyedIds.push(asyncId); + } + if (destroyedIds.length === 2 * N) { + server.close(); + } } }).enable(); @@ -34,19 +38,6 @@ const keepAliveAgent = new http.Agent({ keepAliveMsecs: KEEP_ALIVE, }); -const countdown = new Countdown(N, () => { - server.close(() => { - // Give the server sockets time to close (which will also free their - // associated parser objects) after the server has been closed. - setTimeout(() => { - assert.strictEqual(createdIds.length, 2 * N); - createdIds.forEach((createdAsyncId) => { - assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0); - }); - }, KEEP_ALIVE * 2); - }); -}); - server.listen(0, function() { for (let i = 0; i < N; ++i) { (function makeRequest() { @@ -54,9 +45,24 @@ server.listen(0, function() { port: server.address().port, agent: keepAliveAgent }, function(res) { - countdown.dec(); 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);