-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Changes from 1 commit
33e62a1
4add8f6
7ab62fb
9773531
3b30906
1cf617e
2c5f0c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
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 response | ||
}); | ||
|
||
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 + 50)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const HTTP_AGENT_TIMEOUT = 1000; | ||
const HTTP_CLIENT_TIMEOUT = 2000; | ||
|
||
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 response | ||
}); | ||
|
||
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; | ||
assert((Math.abs(duration - HTTP_CLIENT_TIMEOUT) < 20)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding the request for a comment above, it would be useful in particular to know if what this is all about. |
||
})); | ||
req.end(); | ||
|
||
setTimeout(function() { | ||
req.destroy(); | ||
assert.strictEqual(timeout_events, 1); | ||
}, common.platformTimeout(HTTP_CLIENT_TIMEOUT + 50)); | ||
} |
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?