diff --git a/src/lib/connectors/http.js b/src/lib/connectors/http.js index 2e7660cf5..8a0d65f7d 100644 --- a/src/lib/connectors/http.js +++ b/src/lib/connectors/http.js @@ -57,17 +57,21 @@ _.inherits(HttpConnector, ConnectionAbstract); HttpConnector.prototype.onStatusSet = _.handler(function (status) { if (status === 'closed') { - this.agent.minSockets = this.agent.maxSockets = 0; + var agent = this.agent; + agent.minSockets = agent.maxSockets = 0; + agent.requests = {}; - _.each(this.agent.sockets, function (sockets) { + _.each(agent.sockets, function (sockets, group) { _.each(sockets, function (s) { - s.destroy(); + s && agent.removeSocket(s, group); + s && s.destroy(); }); }); - _.each(this.agent.freeSockets, function (sockets) { + _.each(agent.freeSockets, function (sockets, group) { _.each(sockets, function (s) { - s.destroy(); + s && agent.removeSocket(s, group); + s && s.destroy(); }); }); } diff --git a/test/fixtures/keepalive.js b/test/fixtures/keepalive.js index 39affd68f..e9267784d 100644 --- a/test/fixtures/keepalive.js +++ b/test/fixtures/keepalive.js @@ -1,5 +1,7 @@ var elasticsearch = require('../../src/elasticsearch'); var _ = require('lodash'); +var clock = require('sinon').useFakeTimers(); + var es = elasticsearch.Client({ host: 'localhost:5555', log: false @@ -16,6 +18,12 @@ es.search({ }, function (err, resp) { var conn = _.union(es.transport.connectionPool._conns.dead, es.transport.connectionPool._conns.alive).pop(); es.close(); + + if (_.size(clock.timeouts)) { + console.log('Timeouts were left behind'); + console.log(clock); + } + var destroyedSockets = 0; function countDestroyed(sockets) { destroyedSockets += _.where(sockets, { destroyed: true}).length; @@ -23,4 +31,6 @@ es.search({ _.each(conn.agent.sockets, countDestroyed); _.each(conn.agent.freeSockets, countDestroyed); console.log(destroyedSockets); -}); \ No newline at end of file +}); + +clock.tick(1); \ No newline at end of file diff --git a/test/unit/specs/http_connector.js b/test/unit/specs/http_connector.js index ca9a0a202..54b9c36c2 100644 --- a/test/unit/specs/http_connector.js +++ b/test/unit/specs/http_connector.js @@ -371,6 +371,7 @@ describe('Http Connector', function () { describe('Connection cleanup', function () { it('destroys any connections created', function (done) { + this.timeout(4000); var cp = require('child_process'); var path = require('path'); var es = require('event-stream');