diff options
author | Rich Trott <rtrott@gmail.com> | 2018-08-19 16:53:45 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-08-20 18:59:08 -0700 |
commit | 4b71b01339a968c9e5a1d3750da46fa1d70fe03c (patch) | |
tree | 240047479a81be2d4ca279b12319318e64f66359 /test/parallel/test-http-client-timeout-option-with-agent.js | |
parent | fd76a3d482db94aa06c28f10b1572bb492451a5e (diff) | |
download | android-node-v8-4b71b01339a968c9e5a1d3750da46fa1d70fe03c.tar.gz android-node-v8-4b71b01339a968c9e5a1d3750da46fa1d70fe03c.tar.bz2 android-node-v8-4b71b01339a968c9e5a1d3750da46fa1d70fe03c.zip |
test: move http timeout test to parallel
test-http-client-timeout-option-with-agent no longer checks that the
timeout happens within a certain tolerance so it can be moved to the
parallel test suite.
PR-URL: https://github.com/nodejs/node/pull/22403
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'test/parallel/test-http-client-timeout-option-with-agent.js')
-rw-r--r-- | test/parallel/test-http-client-timeout-option-with-agent.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/test/parallel/test-http-client-timeout-option-with-agent.js b/test/parallel/test-http-client-timeout-option-with-agent.js new file mode 100644 index 0000000000..63f683975d --- /dev/null +++ b/test/parallel/test-http-client-timeout-option-with-agent.js @@ -0,0 +1,56 @@ +'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. + assert.ok(duration >= HTTP_CLIENT_TIMEOUT, + `${duration} < ${HTTP_CLIENT_TIMEOUT}`); + })); + req.end(); + + setTimeout(function() { + req.destroy(); + assert.strictEqual(timeout_events, 1); + // Ensure the `timeout` event fired only once. + }, common.platformTimeout(HTTP_CLIENT_TIMEOUT * 2)); +} |