Skip to content

Commit

Permalink
Connection made available from pool in a callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Oct 6, 2012
1 parent 1919496 commit dcb945a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

Expand Down
29 changes: 9 additions & 20 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
})
})
Expand Down

0 comments on commit dcb945a

Please sign in to comment.