From ecb902f33cc0854be728a3a718e614a3fdd551d7 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 26 Nov 2019 14:16:54 -0800 Subject: [PATCH] test: do not skip test-http-server-consumed-timeout test-http-server-consumed-timeout has code to that causes it to be skipped on busy machines. Instead, use an exponential backoff for the timeout if the machine is busy. PR-URL: https://github.com/nodejs/node/pull/30677 Reviewed-By: Gireesh Punathil Reviewed-By: James M Snell --- .../test-http-server-consumed-timeout.js | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/test/sequential/test-http-server-consumed-timeout.js b/test/sequential/test-http-server-consumed-timeout.js index 80b78b2202a016..231ec69ac3951d 100644 --- a/test/sequential/test-http-server-consumed-timeout.js +++ b/test/sequential/test-http-server-consumed-timeout.js @@ -5,47 +5,48 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -let time = Date.now(); let intervalWasInvoked = false; -const TIMEOUT = common.platformTimeout(200); - -const server = http.createServer((req, res) => { - server.close(); - - res.writeHead(200); - res.flushHeaders(); - - req.setTimeout(TIMEOUT, () => { - if (!intervalWasInvoked) - return common.skip('interval was not invoked quickly enough for test'); - assert.fail('Request timeout should not fire'); +const TIMEOUT = common.platformTimeout(50); + +runTest(TIMEOUT); + +function runTest(timeoutDuration) { + const server = http.createServer((req, res) => { + server.close(); + + res.writeHead(200); + res.flushHeaders(); + + req.setTimeout(timeoutDuration, () => { + if (!intervalWasInvoked) { + // Interval wasn't invoked, probably because the machine is busy with + // other things. Try again with a longer timeout. + console.error(`Retrying with timeout of ${timeoutDuration * 2}.`); + return setImmediate(() => { runTest(timeoutDuration * 2); }); + } + assert.fail('Request timeout should not fire'); + }); + + req.resume(); + req.once('end', () => { + res.end(); + }); }); - req.resume(); - req.once('end', () => { - res.end(); - }); -}); - -server.listen(0, common.mustCall(() => { - const req = http.request({ - port: server.address().port, - method: 'POST' - }, () => { - const interval = setInterval(() => { - intervalWasInvoked = true; - // If machine is busy enough that the interval takes more than TIMEOUT ms - // to be invoked, skip the test. - const now = Date.now(); - if (now - time > TIMEOUT) - return common.skip('interval is not invoked quickly enough for test'); - time = now; - req.write('a'); - }, common.platformTimeout(25)); - setTimeout(() => { - clearInterval(interval); - req.end(); - }, TIMEOUT); - }); - req.write('.'); -})); + server.listen(0, common.mustCall(() => { + const req = http.request({ + port: server.address().port, + method: 'POST' + }, () => { + const interval = setInterval(() => { + intervalWasInvoked = true; + req.write('a'); + }, common.platformTimeout(25)); + setTimeout(() => { + clearInterval(interval); + req.end(); + }, timeoutDuration); + }); + req.write('.'); + })); +}