From f3f67ac56ff28c4284ba36a82736a4f2179ed857 Mon Sep 17 00:00:00 2001 From: Ben Page Date: Tue, 20 Jan 2015 18:15:01 -0600 Subject: [PATCH] added test for min=0 fixed drain() callback not being triggered if there are no open connections or waiters updated README.md and package.json --- README.md | 5 ++++- lib/connection-pool.js | 8 +++++++- package.json | 2 +- test/connection-pool.test.js | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 421527a..7aebe9e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ pool.on('error', function(err) { When you are finished with the pool, you can drain it (close all connections). ```javascript -pool.drain(); //drain the pool when finished using it +pool.drain(); ``` @@ -100,6 +100,9 @@ The following method is added to the Tedious [Connection](http://pekim.github.co ### Connection.release() Release the connect back to the pool to be used again +## Version 0.3.4 Changes +* `poolConfig` option `min` supports being set to 0 + ## Version 0.3.3 Changes * Ignore calls to connection.release() on a connection that has been closed or not part of the connection pool. diff --git a/lib/connection-pool.js b/lib/connection-pool.js index 4fbe2fd..2dde2c3 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 = poolConfig.min >= 0 ? poolConfig.min : 10; this.idleTimeout = !poolConfig.idleTimeout && poolConfig.idleTimeout !== false ? 300000 //5 min @@ -267,6 +267,12 @@ ConnectionPool.prototype.drain = function (callback) { var eventTotal = this.connections.length + this.waiting.length; var eventCount = 0; + if (eventTotal === 0) { + if (callback) + callback(); + return; + } + var ended = function() { if (++eventCount === eventTotal) if (callback) diff --git a/package.json b/package.json index c96d2db..b3b0061 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tedious-connection-pool", - "version": "0.3.3", + "version": "0.3.4", "description": "Connection Pool for tedious.", "main": "lib/connection-pool.js", "scripts": { diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index 6affb79..edddd6f 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -45,6 +45,38 @@ describe('ConnectionPool', function () { }, 4); }); + it('min=0', function (done) { + this.timeout(10000); + + var poolConfig = {min: 0, idleTimeout: 10}; + var pool = new ConnectionPool(poolConfig, connectionConfig); + + setTimeout(function() { + assert.equal(pool.connections.length, 0); + }, 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('max', function (done) { this.timeout(10000); @@ -208,7 +240,6 @@ describe('ConnectionPool', function () { var poolConfig = {max: 1}; var pool = new ConnectionPool(poolConfig, connectionConfig); - var createRequest = function(query, value, callback) { var request = new Request(query, function (err, rowCount) { assert.strictEqual(rowCount, 1);