Skip to content

Commit

Permalink
constrain min to <= max
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydcotten committed Feb 17, 2015
1 parent f3f67ac commit 3ff0a61
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 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 = Math.min(this.max, poolConfig.min >= 0 ? poolConfig.min : 10);

this.idleTimeout = !poolConfig.idleTimeout && poolConfig.idleTimeout !== false
? 300000 //5 min
Expand Down Expand Up @@ -301,4 +301,4 @@ ConnectionPool.prototype.drain = function (callback) {
this.connections = null;
};

module.exports = ConnectionPool;
module.exports = ConnectionPool;
64 changes: 64 additions & 0 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,70 @@ describe('ConnectionPool', function () {
}
});

it('min<=max, min specified > max specified', function (done) {
this.timeout(10000);

var poolConfig = { min: 5, max: 1, idleTimeout: 10};
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, 1);
}, 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('min<=max, no min specified', function (done) {
this.timeout(10000);

var poolConfig = {max: 1, idleTimeout: 10};
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, 1);
}, 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('pool error event', function (done) {
var poolConfig = {min: 2, max: 5};
var pool = new ConnectionPool(poolConfig, {});
Expand Down

0 comments on commit 3ff0a61

Please sign in to comment.