-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: use 'connect' event only if socket is connecting
Fixes a bug that prevented `ClientRequest.prototype.setTimeout()` from working properly when the socket was reused for multiple requests. Fixes: #16716 Refs: #8895 PR-URL: #16725 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
- Loading branch information
Showing
2 changed files
with
48 additions
and
2 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
42 changes: 42 additions & 0 deletions
42
test/parallel/test-http-client-timeout-connect-listener.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,42 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// This test ensures that `ClientRequest.prototype.setTimeout()` does | ||
// not add a listener for the `'connect'` event to the socket if the | ||
// socket is already connected. | ||
|
||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
// Maximum allowed value for timeouts. | ||
const timeout = 2 ** 31 - 1; | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end(); | ||
}); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const agent = new http.Agent({ keepAlive: true, maxSockets: 1 }); | ||
const options = { port: server.address().port, agent: agent }; | ||
|
||
doRequest(options, common.mustCall(() => { | ||
const req = doRequest(options, common.mustCall(() => { | ||
agent.destroy(); | ||
server.close(); | ||
})); | ||
|
||
req.on('socket', common.mustCall((socket) => { | ||
assert.strictEqual(socket.listenerCount('connect'), 0); | ||
})); | ||
})); | ||
})); | ||
|
||
function doRequest(options, callback) { | ||
const req = http.get(options, (res) => { | ||
res.on('end', callback); | ||
res.resume(); | ||
}); | ||
|
||
req.setTimeout(timeout); | ||
return req; | ||
} |