From 5cc75540225445d6b57fbd36daf7aef90c5941f8 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sun, 24 Jul 2022 20:42:25 +0800 Subject: [PATCH] test: fix test http upload timeout PR-URL: https://github.com/nodejs/node/pull/43935 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel --- test/pummel/test-http-upload-timeout.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/pummel/test-http-upload-timeout.js b/test/pummel/test-http-upload-timeout.js index c3ec3ad19d5d37..6c071f8d7f550d 100644 --- a/test/pummel/test-http-upload-timeout.js +++ b/test/pummel/test-http-upload-timeout.js @@ -23,19 +23,20 @@ // This tests setTimeout() by having multiple clients connecting and sending // data in random intervals. Clients are also randomly disconnecting until there // are no more clients left. If no false timeout occurs, this test has passed. -require('../common'); +const common = require('../common'); const http = require('http'); const server = http.createServer(); let connections = 0; +const ontimeout = common.mustNotCall('Unexpected timeout'); + server.on('request', function(req, res) { req.socket.setTimeout(1000); - req.socket.on('timeout', function() { - throw new Error('Unexpected timeout'); - }); + req.socket.on('timeout', ontimeout); req.on('end', function() { connections--; res.writeHead(200); + req.socket.off('timeout', ontimeout); res.end('done\n'); if (connections === 0) { server.close(); @@ -47,7 +48,7 @@ server.on('request', function(req, res) { server.listen(0, '127.0.0.1', function() { for (let i = 0; i < 10; i++) { connections++; - + let count = 0; setTimeout(function() { const request = http.request({ port: server.address().port, @@ -56,13 +57,12 @@ server.listen(0, '127.0.0.1', function() { }); function ping() { - const nextPing = (Math.random() * 900).toFixed(); - if (nextPing > 600) { + if (++count === 10) { request.end(); return; } request.write('ping'); - setTimeout(ping, nextPing); + setTimeout(ping, 300); } ping(); }, i * 50);