Skip to content

Commit

Permalink
test: refactor test-http-agent-timeout-option
Browse files Browse the repository at this point in the history
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
lpinca authored and addaleax committed Feb 3, 2019
1 parent 9d2ea18 commit 7cb9439
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions test/parallel/test-http-agent-timeout-option.js
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();
}));
});

0 comments on commit 7cb9439

Please sign in to comment.