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