nodejs/test/parallel/test-http-agent-timeout-option.js
Luigi Pinca 988482e234 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>
2019-02-01 22:44:33 -08:00

30 lines
691 B
JavaScript

'use strict';
const { expectsError, mustCall } = require('../common');
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 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();
}));
});