diff --git a/index.js b/index.js index 3a2c5cf..54683b9 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,9 @@ function ConnectionPool(poolConfig, connectionConfig) { var connection = new PooledConnection(pool, connectionConfig); pool.inUseConnections.push(connection); - callback(connection); + process.nextTick(function() { + callback(connection); + }); }; return { diff --git a/package.json b/package.json index eb4629e..47c623b 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "license": "MIT", "devDependencies": { "tedious": "0.0.7", - "mocha": "1.6.0" + "mocha": "1.6.0", + "async": "0.1.22" }, "licenses": "MIT" } diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index a25004f..541b867 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -1,5 +1,7 @@ var assert = require('assert') +var async = require('async') var ConnectionPool = require('../index'); +var Request = require('tedious').Request; var connectionConfig = { userName: 'test', @@ -9,18 +11,93 @@ var connectionConfig = { describe('ConnectionPool', function() { describe('one connection', function() { - it('should connect and end', function(done) { - var cp = new ConnectionPool({maxSize: 2}, connectionConfig); + var poolSize = 1; - cp.requestConnection(function (connection) { - connection.on('connect', function(err) { - connection.close(); + it('should connect, and end', function(done) { + var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); + + requestConnectionAndClose(pool, function() { + done(); + }); + }); + + it('should connect, select, and end', function(done) { + var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); + + requestConnectionSelectAndClose(pool, function() { + done(); + }); + }); + }); + + describe('multiple connections within pool maxSize', function() { + var poolSize = 5; + + it('should connect, and end', function(done) { + var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); + + function doIt(done) { + requestConnectionAndClose(pool, function() { + done(); }); + } + + var functions = []; + for (var f = 0; f < poolSize; f++) { + functions.push(doIt); + } + + async.parallel(functions, done); + }); - connection.on('end', function(err) { + it('should connect, select, and end', function(done) { + var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); + + function doIt(done) { + requestConnectionSelectAndClose(pool, function() { done(); }); - }); - }) - }) -}) + } + + var functions = []; + for (var f = 0; f < poolSize; f++) { + functions.push(doIt); + } + + async.parallel(functions, done); + }); + }); +}); + +function requestConnectionAndClose(pool, done) { + pool.requestConnection(function (connection) { + connection.on('connect', function(err) { + connection.close(); + }); + + connection.on('end', function(err) { + done(); + }); + }); +} + +function requestConnectionSelectAndClose(pool, done) { + pool.requestConnection(function (connection) { + var request = new Request('select 42', function(err, rowCount) { + assert.strictEqual(rowCount, 1) + connection.close() + }); + + request.on('row', function(columns) { + assert.strictEqual(columns[0].value, 42) + }); + + connection.on('connect', function(err) { + connection.execSql(request) + }); + + connection.on('end', function(err) { + done(); + }); + }); +}