Skip to content

Commit

Permalink
adding tests for pool relating to #90
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Oct 14, 2013
1 parent f23e24c commit 44b96f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
19 changes: 10 additions & 9 deletions clients/pool.js
Expand Up @@ -24,32 +24,33 @@ define(function(require, exports) {
_.bindAll(this, 'acquire', 'release');
this.config = config;
this.client = client;
if (!config || !client) {
throw new Error('The config and client are required to use the pool module.');
}
this.init();
};

Pool.prototype = {

// Some basic defaults for the pool... generally you don't really want to keep
// mutable objects on the prototype, but in this case we're not supposed to be
// messing around with them, so it should be alright.
// Some basic defaults for the pool...
defaults: function() {
var poolInstance = this;
var pool = this;
return {
min: 2,
max: 10,
create: function(callback) {
var promise = poolInstance.client.getRawConnection()
var promise = pool.client.getRawConnection()
.tap(function(connection) {
connection.__cid = _.uniqueId('__cid');
if (poolInstance.config.afterCreate) {
return nodefn.call(poolInstance.config.afterCreate, connection);
if (pool.config.afterCreate) {
return nodefn.call(pool.config.afterCreate, connection);
}
});
return nodefn.bindCallback(promise, callback);
},
destroy: function(connection) {
if (poolInstance.config.beforeDestroy) {
return poolInstance.config.beforeDestroy(connection, function() {
if (pool.config.beforeDestroy) {
return pool.config.beforeDestroy(connection, function() {
connection.end();
});
}
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -27,6 +27,7 @@ describe('Unit Tests', function() {
require('./unit/builder/joinclause');
require('./unit/raw');
require('./unit/transaction');
require('./unit/clients/pool');
require('./unit/clients/base');
require('./unit/clients/base/grammar');
require('./unit/clients/base/schemagrammar');
Expand Down
60 changes: 60 additions & 0 deletions test/unit/clients/pool.js
@@ -0,0 +1,60 @@
var _ = require('underscore');
var when = require('when');
var Pool = require('../../../clients/pool').Pool;
var GenericPool = require('generic-pool-redux').Pool;

describe('Pool', function () {

var connStub = function() {
return when.resolve({end: function() {}});
};

describe('constructor', function() {

it('creates an instance of the pool in the `poolInstance` property', function() {

var pool = new Pool({}, {getRawConnection: function() { return connStub(); }});

expect(pool.poolInstance).to.be.an.instanceOf(GenericPool);

});

});

describe('destroy', function() {

it('removes the poolInstance', function() {

var pool = new Pool({}, {getRawConnection: function() { return connStub(); }});

pool.destroy();

expect(pool.poolInstance).to.be.undefined;

});

it('calls a callback on success', function(ok) {

var pool = new Pool({}, {getRawConnection: function() { return connStub(); }});

pool.destroy(function() {
ok();
});

});

it('does not fail with subsequent destroy calls', function(ok) {

var pool = new Pool({}, {getRawConnection: function() { return connStub(); }});

pool.destroy();

pool.destroy(function() {
ok();
});

});

});

});

0 comments on commit 44b96f2

Please sign in to comment.