From 3ff0a6155ba41dc03f16bdaa0efc914e8c6d285e Mon Sep 17 00:00:00 2001 From: Lloyd Cotten Date: Tue, 17 Feb 2015 19:03:54 -0330 Subject: [PATCH] constrain min to <= max --- lib/connection-pool.js | 4 +-- test/connection-pool.test.js | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/lib/connection-pool.js b/lib/connection-pool.js index 2dde2c3..53f5705 100644 --- a/lib/connection-pool.js +++ b/lib/connection-pool.js @@ -19,7 +19,7 @@ function ConnectionPool(poolConfig, connectionConfig) { this.max = poolConfig.max || 50; - this.min = poolConfig.min >= 0 ? poolConfig.min : 10; + this.min = Math.min(this.max, poolConfig.min >= 0 ? poolConfig.min : 10); this.idleTimeout = !poolConfig.idleTimeout && poolConfig.idleTimeout !== false ? 300000 //5 min @@ -301,4 +301,4 @@ ConnectionPool.prototype.drain = function (callback) { this.connections = null; }; -module.exports = ConnectionPool; \ No newline at end of file +module.exports = ConnectionPool; diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index edddd6f..5ffbee6 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -116,6 +116,70 @@ describe('ConnectionPool', function () { } }); + it('min<=max, min specified > max specified', function (done) { + this.timeout(10000); + + var poolConfig = { min: 5, max: 1, idleTimeout: 10}; + var pool = new ConnectionPool(poolConfig, connectionConfig); + + setTimeout(function() { + assert.equal(pool.connections.length, 1); + }, 4); + + setTimeout(function() { + pool.acquire(function(err, connection) { + assert(!err); + + var request = new Request('select 42', function (err, rowCount) { + assert.strictEqual(rowCount, 1); + connection.release(); + setTimeout(function () { + assert.equal(pool.connections.length, 0); + pool.drain(done); + }, 200); + }); + + request.on('row', function (columns) { + assert.strictEqual(columns[0].value, 42); + }); + + connection.execSql(request); + }); + }, 2000); + }); + + it('min<=max, no min specified', function (done) { + this.timeout(10000); + + var poolConfig = {max: 1, idleTimeout: 10}; + var pool = new ConnectionPool(poolConfig, connectionConfig); + + setTimeout(function() { + assert.equal(pool.connections.length, 1); + }, 4); + + setTimeout(function() { + pool.acquire(function(err, connection) { + assert(!err); + + var request = new Request('select 42', function (err, rowCount) { + assert.strictEqual(rowCount, 1); + connection.release(); + setTimeout(function () { + assert.equal(pool.connections.length, 0); + pool.drain(done); + }, 200); + }); + + request.on('row', function (columns) { + assert.strictEqual(columns[0].value, 42); + }); + + connection.execSql(request); + }); + }, 2000); + }); + it('pool error event', function (done) { var poolConfig = {min: 2, max: 5}; var pool = new ConnectionPool(poolConfig, {});