11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst http2 = require('http2'); 81cb0ef41Sopenharmony_ciconst hrtime = process.hrtime.bigint; 91cb0ef41Sopenharmony_ciconst NS_PER_MS = 1_000_000n; 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cilet requests = 0; 121cb0ef41Sopenharmony_ciconst mustNotCall = () => { 131cb0ef41Sopenharmony_ci assert.fail(`Timeout after ${requests} request(s)`); 141cb0ef41Sopenharmony_ci}; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst server = http2.createServer(); 171cb0ef41Sopenharmony_ci// Disable server timeout until first request. We will set the timeout based on 181cb0ef41Sopenharmony_ci// how long the first request takes. 191cb0ef41Sopenharmony_ciserver.timeout = 0n; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciserver.on('request', (req, res) => res.end()); 221cb0ef41Sopenharmony_ciserver.on('timeout', mustNotCall); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => { 251cb0ef41Sopenharmony_ci const port = server.address().port; 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci const url = `http://localhost:${port}`; 281cb0ef41Sopenharmony_ci const client = http2.connect(url); 291cb0ef41Sopenharmony_ci let startTime = hrtime(); 301cb0ef41Sopenharmony_ci makeReq(); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci function makeReq() { 331cb0ef41Sopenharmony_ci const request = client.request({ 341cb0ef41Sopenharmony_ci ':path': '/foobar', 351cb0ef41Sopenharmony_ci ':method': 'GET', 361cb0ef41Sopenharmony_ci ':scheme': 'http', 371cb0ef41Sopenharmony_ci ':authority': `localhost:${port}`, 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci request.resume(); 401cb0ef41Sopenharmony_ci request.end(); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci requests += 1; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci request.on('end', () => { 451cb0ef41Sopenharmony_ci const diff = hrtime() - startTime; 461cb0ef41Sopenharmony_ci const milliseconds = diff / NS_PER_MS; 471cb0ef41Sopenharmony_ci if (server.timeout === 0n) { 481cb0ef41Sopenharmony_ci // Set the timeout now. First connection will take significantly longer 491cb0ef41Sopenharmony_ci // than subsequent connections, so using the duration of the first 501cb0ef41Sopenharmony_ci // connection as the timeout should be robust. Double it anyway for good 511cb0ef41Sopenharmony_ci // measure. 521cb0ef41Sopenharmony_ci server.timeout = milliseconds * 2n; 531cb0ef41Sopenharmony_ci startTime = hrtime(); 541cb0ef41Sopenharmony_ci makeReq(); 551cb0ef41Sopenharmony_ci } else if (milliseconds < server.timeout * 2n) { 561cb0ef41Sopenharmony_ci makeReq(); 571cb0ef41Sopenharmony_ci } else { 581cb0ef41Sopenharmony_ci server.removeListener('timeout', mustNotCall); 591cb0ef41Sopenharmony_ci server.close(); 601cb0ef41Sopenharmony_ci client.close(); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci }); 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci})); 65