test: refactor test-http-agent-timeout-option

Fix flakyness caused by usage of a non-routable IP address.

Refs: https://github.com/nodejs/node/pull/25488#issuecomment-459385146

PR-URL: https://github.com/nodejs/node/pull/25854
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
Luigi Pinca
2019-01-31 19:46:18 +01:00
committed by Rich Trott
parent 0f8e8f7c6b
commit 988482e234

View File

@@ -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'
const server = createServer(mustCall(() => {
// Never respond.
}));
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();
}));
});
request.on('error', expectsError({
type: Error,
code: 'ECONNRESET',
message: 'socket hang up'
}));
request.on('timeout', mustCall(() => {
request.abort();
}));