From dcb945ac5e472e316c9701a4902719f5f6049b8f Mon Sep 17 00:00:00 2001 From: Mike D Pilsbury Date: Sat, 6 Oct 2012 17:28:35 +0100 Subject: [PATCH] Connection made available from pool in a callback. --- index.js | 18 ++++++++++-------- test/connection-pool.test.js | 29 +++++++++-------------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index edb1dc0..3a2c5cf 100644 --- a/index.js +++ b/index.js @@ -21,20 +21,22 @@ function ConnectionPool(poolConfig, connectionConfig) { var pool = this; pool.config = poolConfig; - pool.activeConnections = []; + pool.inUseConnections = []; pool.availableConnections = []; pool.connectionAvailable = function(connection) { - this.activeConnections.splice(this.activeConnections.indexOf(connection), 1); + this.inUseConnections.splice(this.inUseConnections.indexOf(connection), 1); }; - return { - Connection: function() { - var connection = new PooledConnection(pool, connectionConfig); - pool.activeConnections.push(connection); + pool.requestConnection = function(callback) { + var connection = new PooledConnection(pool, connectionConfig); + pool.inUseConnections.push(connection); + + callback(connection); + }; - return connection; - } + return { + requestConnection: pool.requestConnection }; }; diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index d9e89aa..a25004f 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -9,28 +9,17 @@ var connectionConfig = { describe('ConnectionPool', function() { describe('one connection', function() { - it('should connect', function(done) { + it('should connect and end', function(done) { + var cp = new ConnectionPool({maxSize: 2}, connectionConfig); - // var connection = new Connection(connectionConfig); + cp.requestConnection(function (connection) { + connection.on('connect', function(err) { + connection.close(); + }); - // connection.on('connect', function(err) { - // done(); - // }); - - var cp = new ConnectionPool({size: 2}, connectionConfig); - var Connection = cp.Connection; - //var connection = cp.getConnection(); - var connection = new Connection(); - //console.log(connection.pool); - - connection.on('connect', function(err) { - connection.close(); - // done(); - }); - - connection.on('end', function(err) { - // console.log('end') - done(); + connection.on('end', function(err) { + done(); + }); }); }) })