mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 20:06:58 +00:00

When request with both timeout and agent, timeout not work. This patch will fix it, socket timeout will set to request timeout before socket is connected, and socket timeout will reset to agent timeout after response end. Fixes: https://github.com/nodejs/node/issues/21185 PR-URL: https://github.com/nodejs/node/pull/21204 Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
47 lines
1000 B
JavaScript
47 lines
1000 B
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
// Test that `req.setTimeout` will fired exactly once.
|
|
|
|
const assert = require('assert');
|
|
const http = require('http');
|
|
|
|
const HTTP_CLIENT_TIMEOUT = 2000;
|
|
|
|
const options = {
|
|
method: 'GET',
|
|
port: undefined,
|
|
host: '127.0.0.1',
|
|
path: '/',
|
|
timeout: HTTP_CLIENT_TIMEOUT,
|
|
};
|
|
|
|
const server = http.createServer(() => {
|
|
// Never respond.
|
|
});
|
|
|
|
server.listen(0, options.host, function() {
|
|
doRequest();
|
|
});
|
|
|
|
function doRequest() {
|
|
options.port = server.address().port;
|
|
const req = http.request(options);
|
|
req.setTimeout(HTTP_CLIENT_TIMEOUT / 2);
|
|
req.on('error', () => {
|
|
// This space is intentionally left blank.
|
|
});
|
|
req.on('close', common.mustCall(() => server.close()));
|
|
|
|
let timeout_events = 0;
|
|
req.on('timeout', common.mustCall(() => {
|
|
timeout_events += 1;
|
|
}));
|
|
req.end();
|
|
|
|
setTimeout(function() {
|
|
req.destroy();
|
|
assert.strictEqual(timeout_events, 1);
|
|
}, common.platformTimeout(HTTP_CLIENT_TIMEOUT));
|
|
}
|