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

There is no need to establish a TCP connection. It is sufficient to
test that the listener that forwards the `'timeout'` event from the
socket to the `ClientRequest` instance is added to the socket.

PR-URL: https://github.com/nodejs/node/pull/25886
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Luigi Pinca 2019-02-02 08:10:28 +01:00
parent 0e54a0a213
commit b8d9d4ac68

View File

@ -1,29 +1,23 @@
'use strict'; 'use strict';
const { expectsError, mustCall } = require('../common'); const { mustCall } = require('../common');
const { Agent, get, createServer } = require('http'); const { strictEqual } = require('assert');
const { Agent, get } = require('http');
// Test that the `'timeout'` event is emitted on the `ClientRequest` instance // Test that the listener that forwards the `'timeout'` event from the socket to
// when the socket timeout set via the `timeout` option of the `Agent` expires. // the `ClientRequest` instance is added to the socket when the `timeout` option
// of the `Agent` is set.
const server = createServer(mustCall(() => {
// Never respond.
}));
server.listen(() => {
const request = get({ const request = get({
agent: new Agent({ timeout: 500 }), agent: new Agent({ timeout: 50 }),
port: server.address().port lookup: () => {}
}); });
request.on('error', expectsError({ request.on('socket', mustCall((socket) => {
type: Error, strictEqual(socket.timeout, 50);
code: 'ECONNRESET',
message: 'socket hang up'
}));
request.on('timeout', mustCall(() => { const listeners = socket.listeners('timeout');
request.abort();
server.close(); strictEqual(listeners.length, 1);
strictEqual(listeners[0], request.timeoutCb);
})); }));
});