Skip to content

Commit 7cb9439

Browse files
lpincaaddaleax
authored andcommitted
test: refactor test-http-agent-timeout-option
Fix flakyness caused by usage of a non-routable IP address. Refs: #25488 (comment) PR-URL: #25854 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent 9d2ea18 commit 7cb9439

File tree

1 file changed

+20
-14
lines changed

1 file changed

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

33
const { expectsError, mustCall } = require('../common');
4-
const { Agent, get } = require('http');
4+
const { Agent, get, createServer } = require('http');
55

66
// Test that the `'timeout'` event is emitted on the `ClientRequest` instance
77
// when the socket timeout set via the `timeout` option of the `Agent` expires.
88

9-
const request = get({
10-
agent: new Agent({ timeout: 500 }),
11-
// Non-routable IP address to prevent the connection from being established.
12-
host: '192.0.2.1'
13-
});
14-
15-
request.on('error', expectsError({
16-
type: Error,
17-
code: 'ECONNRESET',
18-
message: 'socket hang up'
9+
const server = createServer(mustCall(() => {
10+
// Never respond.
1911
}));
2012

21-
request.on('timeout', mustCall(() => {
22-
request.abort();
23-
}));
13+
server.listen(() => {
14+
const request = get({
15+
agent: new Agent({ timeout: 500 }),
16+
port: server.address().port
17+
});
18+
19+
request.on('error', expectsError({
20+
type: Error,
21+
code: 'ECONNRESET',
22+
message: 'socket hang up'
23+
}));
24+
25+
request.on('timeout', mustCall(() => {
26+
request.abort();
27+
server.close();
28+
}));
29+
});

0 commit comments

Comments
 (0)