Skip to content

Commit

Permalink
added test for min=0
Browse files Browse the repository at this point in the history
fixed drain() callback not being triggered if there are no open connections or waiters
updated README.md and package.json
  • Loading branch information
ben-page committed Jan 21, 2015
1 parent b2d5eb3 commit f3f67ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```


Expand Down Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion lib/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
33 changes: 32 additions & 1 deletion test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f3f67ac

Please sign in to comment.