-
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.
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>
- Loading branch information
Showing
1 changed file
with
20 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
'use strict'; | ||
|
||
const { expectsError, mustCall } = require('../common'); | ||
const { Agent, get } = require('http'); | ||
const { Agent, get, createServer } = require('http'); | ||
|
||
// Test that the `'timeout'` event is emitted on the `ClientRequest` instance | ||
// when the socket timeout set via the `timeout` option of the `Agent` expires. | ||
|
||
const request = get({ | ||
agent: new Agent({ timeout: 500 }), | ||
// Non-routable IP address to prevent the connection from being established. | ||
host: '192.0.2.1' | ||
}); | ||
|
||
request.on('error', expectsError({ | ||
type: Error, | ||
code: 'ECONNRESET', | ||
message: 'socket hang up' | ||
const server = createServer(mustCall(() => { | ||
// Never respond. | ||
})); | ||
|
||
request.on('timeout', mustCall(() => { | ||
request.abort(); | ||
})); | ||
server.listen(() => { | ||
const request = get({ | ||
agent: new Agent({ timeout: 500 }), | ||
port: server.address().port | ||
}); | ||
|
||
request.on('error', expectsError({ | ||
type: Error, | ||
code: 'ECONNRESET', | ||
message: 'socket hang up' | ||
})); | ||
|
||
request.on('timeout', mustCall(() => { | ||
request.abort(); | ||
server.close(); | ||
})); | ||
}); |