Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
test: refactor two http client timeout tests
Refactor test-http-client-set-timeout and test-http-client-timeout-option-with-agent. PR-URL: #25473 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
with
51 additions
and 84 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
| @@ -1,46 +1,48 @@ | ||
| 'use strict'; | ||
|
|
||
| // Test that the `'timeout'` event is emitted exactly once if the `timeout` | ||
| // option and `request.setTimeout()` are used together. | ||
|
|
||
| const { expectsError, mustCall } = require('../common'); | ||
| const { strictEqual } = require('assert'); | ||
| const { createServer, get } = require('http'); | ||
|
|
||
| const server = createServer(() => { | ||
| // Never respond. | ||
| }); | ||
|
|
||
| server.listen(0, mustCall(() => { | ||
| const req = get({ | ||
| port: server.address().port, | ||
| timeout: 2000, | ||
| }); | ||
|
|
||
| req.setTimeout(1000); | ||
|
|
||
| req.on('socket', mustCall((socket) => { | ||
| strictEqual(socket.timeout, 2000); | ||
|
|
||
| socket.on('connect', mustCall(() => { | ||
| strictEqual(socket.timeout, 1000); | ||
|
|
||
| // Reschedule the timer to not wait 1 sec and make the test finish faster. | ||
| socket.setTimeout(10); | ||
| strictEqual(socket.timeout, 10); | ||
| })); | ||
| })); | ||
|
|
||
| req.on('error', expectsError({ | ||
| type: Error, | ||
| code: 'ECONNRESET', | ||
| message: 'socket hang up' | ||
| })); | ||
|
|
||
| req.on('close', mustCall(() => { | ||
| server.close(); | ||
| })); | ||
|
|
||
| req.on('timeout', mustCall(() => { | ||
| strictEqual(req.socket.listenerCount('timeout'), 0); | ||
| req.destroy(); | ||
| })); | ||
| })); |
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
| @@ -1,58 +1,23 @@ | ||
| 'use strict'; | ||
|
|
||
| // Test that the request `timeout` option has precedence over the agent | ||
| // `timeout` option. | ||
|
|
||
| const { mustCall } = require('../common'); | ||
| const { Agent, get } = require('http'); | ||
| const { strictEqual } = require('assert'); | ||
|
|
||
| const request = get({ | ||
| agent: new Agent({ timeout: 50 }), | ||
| lookup: () => {}, | ||
| timeout: 100 | ||
| }); | ||
|
|
||
| request.on('socket', mustCall((socket) => { | ||
| strictEqual(socket.timeout, 100); | ||
|
|
||
| const listeners = socket.listeners('timeout'); | ||
|
|
||
| strictEqual(listeners.length, 1); | ||
| strictEqual(listeners[0], request.timeoutCb); | ||
| })); |