-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http: fix request with option timeout and agent #21204
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33e62a1
http: fix request with option timeout and agent
killagu 4add8f6
update tests
killagu 7ab62fb
minor edit to doc text
Trott 9773531
fixup! minor edit to comment text
Trott 3b30906
squash! minor edit to comment
Trott 1cf617e
squash! minor edits to comments
Trott 2c5f0c0
squash! minor edits to comments
Trott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be willing to add a comment here explaining what this is testing?