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: nodejs#25488 (comment)

PR-URL: nodejs#25854
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
  • Loading branch information
lpinca authored and Trott committed Feb 2, 2019
1 parent 0f8e8f7 commit 988482e
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 988482e

Please sign in to comment.