From 1af1a8635e8f62d3ccd16216b65771afd09d2399 Mon Sep 17 00:00:00 2001 From: Mike D Pilsbury Date: Tue, 9 Oct 2012 21:09:33 +0100 Subject: [PATCH] Make the use of generic-pool work with tedious. All tests pass. More tests may be needed. Issue #1 --- .gitignore | 1 + lib/connection-pool.js | 55 +++++++++++++++++++++++++----------- lib/pooled-connection.js | 12 ++++---- test/connection-pool.test.js | 20 +++++++------ 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index c2658d7..ab8d2d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +*.sublime-* diff --git a/lib/connection-pool.js b/lib/connection-pool.js index 1d7de2d..62ad38b 100644 --- a/lib/connection-pool.js +++ b/lib/connection-pool.js @@ -17,30 +17,43 @@ function ConnectionPool(poolConfig, connectionConfig) { var self = this, param = { name: poolConfig.name || "", - log: true, + log: poolConfig.log, create: function(callback) { - connection = new PooledConnection(connectionConfig); - connection.on('connect', function(err) { + var connection = new PooledConnection(connectionConfig); + + connection.on('connect', function(err) { + if (connection.__poolConnected) { + return; + } + if (err) { - callback(err,null); - } - else { - connection.on('release', function() { + callback(err, null); + } else { + connection.__poolConnected = true; + + connection.on('release', function() { + connectionEventNames.forEach(function removeAllListeners(eventName) { + connection.removeAllListeners(eventName); + }); + self.pool.release(connection); }); - callback(null, connection); - } - }); - }, + + callback(null, connection); + } + }); + }, destroy: function(connection) { connection._close(); - }, - max: poolConfig.max || 10, - min: poolConfig.min || 0, - idleTimeoutMillis: poolConfig.idleTimeoutMillis || 30000 + }, + max: poolConfig.max || 10, + min: poolConfig.min || 0, + idleTimeoutMillis: poolConfig.idleTimeoutMillis || 30000 }; + this.pool = PoolModule.Pool(param); } + module.exports = ConnectionPool; @@ -48,12 +61,20 @@ ConnectionPool.prototype.requestConnection = function(callback) { var self = this; this.pool.acquire(function(err, connection) { if(err) { - callback(err, null); + callback(err, null); } else { callback(null, connection); connection.emit('connect'); } }); -} +}; +ConnectionPool.prototype.drain = function(callback) { + var self = this; + + self.pool.drain(function() { + self.pool.destroyAllNow(); + callback(); + }); +}; diff --git a/lib/pooled-connection.js b/lib/pooled-connection.js index cd1fc67..6d29ff9 100644 --- a/lib/pooled-connection.js +++ b/lib/pooled-connection.js @@ -2,18 +2,18 @@ var Connection = require('tedious').Connection; var util = require('util'); function PooledConnection(config) { - Connection.call(this, config); + Connection.call(this, config); } util.inherits(PooledConnection, Connection); PooledConnection.prototype.close = function() { - this.emit('release');//used by the pool - this.emit('end'); -} + this.emit('end'); + this.emit('release'); //used by the pool +}; PooledConnection.prototype._close = function() { - return Connection.prototype.close.call(this); -} + return Connection.prototype.close.call(this); +}; module.exports = PooledConnection; diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index df2e03b..b5ceaed 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -19,20 +19,20 @@ var connectionConfig = { describe('ConnectionPool', function() { describe('one connection', function() { - var poolConfig = {maxSize: 1}; + var poolConfig = {max: 1, log: false}; it('should connect, and end', function(done) { - testPool(poolConfig, poolConfig.maxSize, requestConnectionAndClose, done); + testPool(poolConfig, poolConfig.max, requestConnectionAndClose, done); }); it('should connect, select, and end', function(done) { - testPool(poolConfig, poolConfig.maxSize, requestConnectionSelectAndClose, done); + testPool(poolConfig, poolConfig.max, requestConnectionSelectAndClose, done); }); }); describe('multiple connections within pool maxSize', function() { - var poolConfig = {maxSize: 5}; - var numberOfConnectionsToUse = poolConfig.maxSize; + var poolConfig = {max: 5, log: false}; + var numberOfConnectionsToUse = poolConfig.max; it('should connect, and end', function(done) { testPool(poolConfig, numberOfConnectionsToUse, requestConnectionAndClose, done); @@ -44,7 +44,7 @@ describe('ConnectionPool', function() { }); describe('connections exceed pool maxSize', function() { - var poolConfig = {maxSize: 5}; + var poolConfig = {max: 5, log: false}; var numberOfConnectionsToUse = 20; it('should connect, and end', function(done) { @@ -71,7 +71,11 @@ function testPool(poolConfig, numberOfConnectionsToUse, useConnectionFunction, d functions.push(doIt); } - async.parallel(functions, done); + async.parallel(functions, function() { + pool.drain(function() { + done(); + }); + }); } function requestConnectionAndClose(pool, done) { @@ -85,7 +89,7 @@ function requestConnectionAndClose(pool, done) { connection.on('end', function(err) { done(); }); - } ); + }); } function requestConnectionSelectAndClose(pool, done) {