Skip to content

Commit 1b9a608

Browse files
lpincaaddaleax
authored andcommitted
test: refactor test-http-agent-timeout-option
There is no need to establish a TCP connection. It is sufficient to test that the listener that forwards the `'timeout'` event from the socket to the `ClientRequest` instance is added to the socket. PR-URL: #25886 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent f5d5034 commit 1b9a608

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
'use strict';
22

3-
const { expectsError, mustCall } = require('../common');
4-
const { Agent, get, createServer } = require('http');
3+
const { mustCall } = require('../common');
4+
const { strictEqual } = require('assert');
5+
const { Agent, get } = require('http');
56

6-
// Test that the `'timeout'` event is emitted on the `ClientRequest` instance
7-
// when the socket timeout set via the `timeout` option of the `Agent` expires.
7+
// Test that the listener that forwards the `'timeout'` event from the socket to
8+
// the `ClientRequest` instance is added to the socket when the `timeout` option
9+
// of the `Agent` is set.
810

9-
const server = createServer(mustCall(() => {
10-
// Never respond.
11-
}));
11+
const request = get({
12+
agent: new Agent({ timeout: 50 }),
13+
lookup: () => {}
14+
});
1215

13-
server.listen(() => {
14-
const request = get({
15-
agent: new Agent({ timeout: 500 }),
16-
port: server.address().port
17-
});
16+
request.on('socket', mustCall((socket) => {
17+
strictEqual(socket.timeout, 50);
1818

19-
request.on('error', expectsError({
20-
type: Error,
21-
code: 'ECONNRESET',
22-
message: 'socket hang up'
23-
}));
19+
const listeners = socket.listeners('timeout');
2420

25-
request.on('timeout', mustCall(() => {
26-
request.abort();
27-
server.close();
28-
}));
29-
});
21+
strictEqual(listeners.length, 1);
22+
strictEqual(listeners[0], request.timeoutCb);
23+
}));

0 commit comments

Comments
 (0)