-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: fix request with option timeout and agent
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: #21185 PR-URL: #21204 Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]>
- Loading branch information
Showing
6 changed files
with
169 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'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)); | ||
} |
55 changes: 55 additions & 0 deletions
55
test/parallel/test-http-client-timeout-option-with-agent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// Test that when http request uses both timeout and agent, | ||
// timeout will work as expected. | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const HTTP_AGENT_TIMEOUT = 1000; | ||
const HTTP_CLIENT_TIMEOUT = 3000; | ||
|
||
const agent = new http.Agent({ timeout: HTTP_AGENT_TIMEOUT }); | ||
const options = { | ||
method: 'GET', | ||
port: undefined, | ||
host: '127.0.0.1', | ||
path: '/', | ||
timeout: HTTP_CLIENT_TIMEOUT, | ||
agent, | ||
}; | ||
|
||
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); | ||
const start = Date.now(); | ||
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; | ||
const duration = Date.now() - start; | ||
// The timeout event cannot be precisely timed. It will delay | ||
// some number of milliseconds, so test it in second units. | ||
assert.strictEqual(duration / 1000 | 0, HTTP_CLIENT_TIMEOUT / 1000); | ||
})); | ||
req.end(); | ||
|
||
setTimeout(function() { | ||
req.destroy(); | ||
assert.strictEqual(timeout_events, 1); | ||
// Ensure the `timeout` event fired only once. | ||
}, common.platformTimeout(HTTP_CLIENT_TIMEOUT * 2)); | ||
} |