2017-09-07 14:02:19 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
2018-09-24 06:11:12 -07:00
|
|
|
const assert = require('assert');
|
2018-07-29 19:26:27 -07:00
|
|
|
const http2 = require('http2');
|
2023-01-01 20:07:47 +09:00
|
|
|
const hrtime = process.hrtime.bigint;
|
|
|
|
const NS_PER_MS = 1_000_000n;
|
2017-09-07 14:02:19 -04:00
|
|
|
|
2018-09-24 06:11:12 -07:00
|
|
|
let requests = 0;
|
|
|
|
const mustNotCall = () => {
|
|
|
|
assert.fail(`Timeout after ${requests} request(s)`);
|
|
|
|
};
|
2017-09-07 14:02:19 -04:00
|
|
|
|
2018-07-29 19:26:27 -07:00
|
|
|
const server = http2.createServer();
|
2018-12-06 10:21:05 -08:00
|
|
|
// Disable server timeout until first request. We will set the timeout based on
|
|
|
|
// how long the first request takes.
|
2023-01-01 20:07:47 +09:00
|
|
|
server.timeout = 0n;
|
2017-09-07 14:02:19 -04:00
|
|
|
|
|
|
|
server.on('request', (req, res) => res.end());
|
2017-09-16 08:56:42 -04:00
|
|
|
server.on('timeout', mustNotCall);
|
2017-09-07 14:02:19 -04:00
|
|
|
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const port = server.address().port;
|
|
|
|
|
|
|
|
const url = `http://localhost:${port}`;
|
2018-07-29 19:26:27 -07:00
|
|
|
const client = http2.connect(url);
|
2023-01-01 20:07:47 +09:00
|
|
|
let startTime = hrtime();
|
2018-05-10 20:45:44 -07:00
|
|
|
makeReq();
|
2017-09-07 14:02:19 -04:00
|
|
|
|
2018-05-10 20:45:44 -07:00
|
|
|
function makeReq() {
|
2017-09-07 14:02:19 -04:00
|
|
|
const request = client.request({
|
|
|
|
':path': '/foobar',
|
|
|
|
':method': 'GET',
|
|
|
|
':scheme': 'http',
|
|
|
|
':authority': `localhost:${port}`,
|
|
|
|
});
|
2017-09-16 08:56:42 -04:00
|
|
|
request.resume();
|
2017-09-07 14:02:19 -04:00
|
|
|
request.end();
|
|
|
|
|
2018-09-24 06:11:12 -07:00
|
|
|
requests += 1;
|
|
|
|
|
2017-09-16 08:56:42 -04:00
|
|
|
request.on('end', () => {
|
2023-01-01 20:07:47 +09:00
|
|
|
const diff = hrtime() - startTime;
|
|
|
|
const milliseconds = diff / NS_PER_MS;
|
|
|
|
if (server.timeout === 0n) {
|
2018-12-06 10:21:05 -08:00
|
|
|
// Set the timeout now. First connection will take significantly longer
|
|
|
|
// than subsequent connections, so using the duration of the first
|
|
|
|
// connection as the timeout should be robust. Double it anyway for good
|
|
|
|
// measure.
|
2023-01-01 20:07:47 +09:00
|
|
|
server.timeout = milliseconds * 2n;
|
|
|
|
startTime = hrtime();
|
2018-12-06 10:21:05 -08:00
|
|
|
makeReq();
|
2023-01-01 20:07:47 +09:00
|
|
|
} else if (milliseconds < server.timeout * 2n) {
|
2018-09-24 05:54:57 -07:00
|
|
|
makeReq();
|
2017-09-16 08:56:42 -04:00
|
|
|
} else {
|
|
|
|
server.removeListener('timeout', mustNotCall);
|
|
|
|
server.close();
|
2018-05-10 20:45:44 -07:00
|
|
|
client.close();
|
2017-09-16 08:56:42 -04:00
|
|
|
}
|
|
|
|
});
|
2017-09-07 14:02:19 -04:00
|
|
|
}
|
|
|
|
}));
|