From 9cf40c5753d1a9afbe24cb8a7f7c87211d6983ff Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 10 May 2018 20:45:44 -0700 Subject: [PATCH] test: improve reliability of http2-session-timeout Check actual expired time rather than relying on a number of calls to setTimeout() in test-http2-session-timeout more robust. PR-URL: https://github.com/nodejs/node/pull/20692 Fixes: https://github.com/nodejs/node/issues/20628 Reviewed-By: Anatoli Papirovski Reviewed-By: Trivikram Kamat Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- test/sequential/test-http2-session-timeout.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/sequential/test-http2-session-timeout.js b/test/sequential/test-http2-session-timeout.js index fce4570563c584..48e98998c700b6 100644 --- a/test/sequential/test-http2-session-timeout.js +++ b/test/sequential/test-http2-session-timeout.js @@ -7,7 +7,6 @@ const h2 = require('http2'); const serverTimeout = common.platformTimeout(200); const callTimeout = common.platformTimeout(20); -const minRuns = Math.ceil(serverTimeout / callTimeout) * 2; const mustNotCall = common.mustNotCall(); const server = h2.createServer(); @@ -21,9 +20,10 @@ server.listen(0, common.mustCall(() => { const url = `http://localhost:${port}`; const client = h2.connect(url); - makeReq(minRuns); + const startTime = process.hrtime(); + makeReq(); - function makeReq(attempts) { + function makeReq() { const request = client.request({ ':path': '/foobar', ':method': 'GET', @@ -34,12 +34,14 @@ server.listen(0, common.mustCall(() => { request.end(); request.on('end', () => { - if (attempts) { - setTimeout(() => makeReq(attempts - 1), callTimeout); + const diff = process.hrtime(startTime); + const milliseconds = (diff[0] * 1e3 + diff[1] / 1e6); + if (milliseconds < serverTimeout * 2) { + setTimeout(makeReq, callTimeout); } else { server.removeListener('timeout', mustNotCall); - client.close(); server.close(); + client.close(); } }); }