From 988482e2341ba31996c4a44432fd9f4f35b89dbb Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 31 Jan 2019 19:46:18 +0100 Subject: [PATCH] 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 Reviewed-By: Gireesh Punathil --- .../test-http-agent-timeout-option.js | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/test/parallel/test-http-agent-timeout-option.js b/test/parallel/test-http-agent-timeout-option.js index 4fcfc1f1da54b1..3c694a0ef7d2d2 100644 --- a/test/parallel/test-http-agent-timeout-option.js +++ b/test/parallel/test-http-agent-timeout-option.js @@ -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(); + })); +});