From 697c761b715ec133f5699b2b05a53e7c8dead9a9 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Mon, 17 Feb 2014 01:11:53 -0700 Subject: [PATCH] fixed the force closing of the forever agent, which was removing items from the same array it was itterating. --- src/lib/connectors/http.js | 25 +++++++++++++------------ test/unit/specs/http_connector.js | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/lib/connectors/http.js b/src/lib/connectors/http.js index 8a0d65f7d..477f824fe 100644 --- a/src/lib/connectors/http.js +++ b/src/lib/connectors/http.js @@ -58,21 +58,22 @@ _.inherits(HttpConnector, ConnectionAbstract); HttpConnector.prototype.onStatusSet = _.handler(function (status) { if (status === 'closed') { var agent = this.agent; + var toRemove = []; + var collectSockets = function (sockets, host) { + _.each(sockets, function (s) { + s && toRemove.push([host, s]); + }); + }; + agent.minSockets = agent.maxSockets = 0; agent.requests = {}; - _.each(agent.sockets, function (sockets, group) { - _.each(sockets, function (s) { - s && agent.removeSocket(s, group); - s && s.destroy(); - }); - }); - - _.each(agent.freeSockets, function (sockets, group) { - _.each(sockets, function (s) { - s && agent.removeSocket(s, group); - s && s.destroy(); - }); + _.each(agent.sockets, collectSockets); + _.each(agent.freeSockets, collectSockets); + _.each(toRemove, function (args) { + var host = args[0], socket = args[1]; + agent.removeSocket(socket, host); + socket.destroy(); }); } }); diff --git a/test/unit/specs/http_connector.js b/test/unit/specs/http_connector.js index 54b9c36c2..7181f1c2c 100644 --- a/test/unit/specs/http_connector.js +++ b/test/unit/specs/http_connector.js @@ -389,6 +389,25 @@ describe('Http Connector', function () { done(); })); }); + + it('properly removes all elements from the socket', function () { + var con = new HttpConnection(new Host('localhost')); + var sockets = [ + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} }, + { destroy: function () {} } + ]; + con.agent.sockets['http://localhost/'] = sockets; + con.setStatus('closed'); + expect(sockets).to.eql([]); + }); }); });