Skip to content

Commit

Permalink
Make the use of generic-pool work with tedious.
Browse files Browse the repository at this point in the history
All tests pass. More tests may be needed.

Issue tediousjs#1
  • Loading branch information
pekim committed Oct 9, 2012
1 parent 7ec1b5f commit 1af1a86
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
*.sublime-*
55 changes: 38 additions & 17 deletions lib/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,64 @@ 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;


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();
});
};
12 changes: 6 additions & 6 deletions lib/pooled-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 12 additions & 8 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -85,7 +89,7 @@ function requestConnectionAndClose(pool, done) {
connection.on('end', function(err) {
done();
});
} );
});
}

function requestConnectionSelectAndClose(pool, done) {
Expand Down

0 comments on commit 1af1a86

Please sign in to comment.