1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const http2 = require('http2'); 8const hrtime = process.hrtime.bigint; 9const NS_PER_MS = 1_000_000n; 10 11let requests = 0; 12const mustNotCall = () => { 13 assert.fail(`Timeout after ${requests} request(s)`); 14}; 15 16const server = http2.createServer(); 17// Disable server timeout until first request. We will set the timeout based on 18// how long the first request takes. 19server.timeout = 0n; 20 21server.on('request', (req, res) => res.end()); 22server.on('timeout', mustNotCall); 23 24server.listen(0, common.mustCall(() => { 25 const port = server.address().port; 26 27 const url = `http://localhost:${port}`; 28 const client = http2.connect(url); 29 let startTime = hrtime(); 30 makeReq(); 31 32 function makeReq() { 33 const request = client.request({ 34 ':path': '/foobar', 35 ':method': 'GET', 36 ':scheme': 'http', 37 ':authority': `localhost:${port}`, 38 }); 39 request.resume(); 40 request.end(); 41 42 requests += 1; 43 44 request.on('end', () => { 45 const diff = hrtime() - startTime; 46 const milliseconds = diff / NS_PER_MS; 47 if (server.timeout === 0n) { 48 // Set the timeout now. First connection will take significantly longer 49 // than subsequent connections, so using the duration of the first 50 // connection as the timeout should be robust. Double it anyway for good 51 // measure. 52 server.timeout = milliseconds * 2n; 53 startTime = hrtime(); 54 makeReq(); 55 } else if (milliseconds < server.timeout * 2n) { 56 makeReq(); 57 } else { 58 server.removeListener('timeout', mustNotCall); 59 server.close(); 60 client.close(); 61 } 62 }); 63 } 64})); 65