Prevent pool for returning connections that are not yet opened NODEJS-75
diff --git a/lib/host.js b/lib/host.js index 43d6093..5abd665 100644 --- a/lib/host.js +++ b/lib/host.js
@@ -147,7 +147,7 @@ HostConnectionPool.prototype._maybeCreatePool = function (callback) { //The parameter this.coreConnectionsLength could change over time //It can result of a created pool being resized (setting the distance). - if (this.connections && this.connections.length >= this.coreConnectionsLength) { + if (!this.creating && this.connections && this.connections.length >= this.coreConnectionsLength) { return callback(); } this.once('creation', callback);
diff --git a/test/unit/host-tests.js b/test/unit/host-tests.js index d7eef41..d56a4fc 100644 --- a/test/unit/host-tests.js +++ b/test/unit/host-tests.js
@@ -9,16 +9,27 @@ var types = require('../../lib/types.js'); var utils = require('../../lib/utils.js'); var reconnection = require('../../lib/policies/reconnection.js'); +//Delay before connection.open callbacks +var openDelay = 10; before(function () { //inject a mock Connection class var connectionMock = function () {}; - connectionMock.prototype.open = function noop (cb) {cb()}; + connectionMock.prototype.open = function noop (cb) { + var self = this; + setTimeout(function () { + self.connected = true; + cb(); + }, openDelay); + }; hostModule.__set__("Connection", connectionMock); }); describe('HostConnectionPool', function () { describe('#_maybeCreatePool()', function () { + afterEach(function () { + openDelay = 10; + }); it('should create the pool once', function (done) { var hostPool = newHostConnectionPoolInstance(); hostPool.coreConnectionsLength = 10; @@ -32,6 +43,26 @@ }); }, done); }); + it('should never callback with unopened connections', function (done) { + openDelay = 800; + var hostPool = newHostConnectionPoolInstance(); + async.times(5, function(n, next) { + setTimeout(function () { + hostPool._maybeCreatePool(function (err) { + assert.ifError(err); + var closedConnections = hostPool.connections.filter(function (x) {return !x.connected}).length; + if (closedConnections) + { + return next(new Error('All connections should be opened: ' + closedConnections + ' closed')) + } + next(); + }); + }, n); + }, function (err) { + assert.ifError(err); + done(); + }); + }); }); describe('#borrowConnection()', function () {