Skip to content

Commit

Permalink
test: refactor test-http-agent-timeout-option
Browse files Browse the repository at this point in the history
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: #25886
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
lpinca authored and addaleax committed Feb 7, 2019
1 parent f5d5034 commit 1b9a608
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions test/parallel/test-http-agent-timeout-option.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
'use strict';

const { expectsError, mustCall } = require('../common');
const { Agent, get, createServer } = require('http');
const { mustCall } = require('../common');
const { strictEqual } = require('assert');
const { Agent, get } = 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.
// Test that the listener that forwards the `'timeout'` event from the socket to
// the `ClientRequest` instance is added to the socket when the `timeout` option
// of the `Agent` is set.

const server = createServer(mustCall(() => {
// Never respond.
}));
const request = get({
agent: new Agent({ timeout: 50 }),
lookup: () => {}
});

server.listen(() => {
const request = get({
agent: new Agent({ timeout: 500 }),
port: server.address().port
});
request.on('socket', mustCall((socket) => {
strictEqual(socket.timeout, 50);

request.on('error', expectsError({
type: Error,
code: 'ECONNRESET',
message: 'socket hang up'
}));
const listeners = socket.listeners('timeout');

request.on('timeout', mustCall(() => {
request.abort();
server.close();
}));
});
strictEqual(listeners.length, 1);
strictEqual(listeners[0], request.timeoutCb);
}));

0 comments on commit 1b9a608

Please sign in to comment.