nodejs/test/parallel/test-http-agent-error-on-idle.js
José F. Romaniello 5a2541de81 http: handle errors on idle sockets
This change adds a new event handler to the `error` event of the socket
after it has been used by the http_client.

The purpose of this change is to catch errors on *keep alived*
connections from idle sockets that otherwise will cause an uncaugh error
event on the application.

Fix: #3595
PR-URL: https://github.com/nodejs/node/pull/4482
Reviewed-By: Fedor Indutny <fedor@indutny.com>
2016-01-04 13:35:53 -05:00

57 lines
1.2 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');
var Agent = http.Agent;
var agent = new Agent({
keepAlive: true,
});
var requestParams = {
host: 'localhost',
port: common.PORT,
agent: agent,
path: '/'
};
var socketKey = agent.getName(requestParams);
function get(callback) {
return http.get(requestParams, callback);
}
var destroy_queue = {};
var server = http.createServer(function(req, res) {
res.end('hello world');
});
server.listen(common.PORT, function() {
get(function(res) {
assert.equal(res.statusCode, 200);
res.resume();
res.on('end', function() {
process.nextTick(function() {
var freeSockets = agent.freeSockets[socketKey];
assert.equal(freeSockets.length, 1,
'expect a free socket on ' + socketKey);
//generate a random error on the free socket
var freeSocket = freeSockets[0];
freeSocket.emit('error', new Error('ECONNRESET: test'));
get(done);
});
});
});
});
function done() {
assert.equal(Object.keys(agent.freeSockets).length, 0,
'expect the freeSockets pool to be empty');
agent.destroy();
server.close();
process.exit(0);
}