Skip to content

Commit

Permalink
Make tests simpler and easier to read and maintain.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Oct 6, 2012
1 parent dedda2c commit f468f02
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
28 changes: 22 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,34 @@ function ConnectionPool(poolConfig, connectionConfig) {
pool.config = poolConfig;
pool.inUseConnections = [];
pool.availableConnections = [];
pool.pendingConnectionRequests = [];

pool.connectionAvailable = function(connection) {
this.inUseConnections.splice(this.inUseConnections.indexOf(connection), 1);
};

pool.requestConnection = function(callback) {
var connection = new PooledConnection(pool, connectionConfig);
pool.inUseConnections.push(connection);

process.nextTick(function() {
callback(connection);
});
var connection;

if (pool.availableConnections.length > 1) {
connection = availableConnections.shift();
} else if (pool.inUseConnections.length < pool.config.maxSize) {
connection = new PooledConnection(pool, connectionConfig);
}

if (connection) {
useConnection(connection);
} else {
pool.pendingConnectionRequests.push(useConnection);
}

function useConnection(connection) {
pool.inUseConnections.push(connection);

process.nextTick(function() {
callback(connection);
});
}
};

return {
Expand Down
63 changes: 23 additions & 40 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,47 @@ var connectionConfig = {

describe('ConnectionPool', function() {
describe('one connection', function() {
var poolSize = 1;
var poolConfig = {maxSize: 1};

it('should connect, and end', function(done) {
var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig);

requestConnectionAndClose(pool, function() {
done();
});
testPool(poolConfig, poolConfig.maxSize, requestConnectionAndClose, done);
});

it('should connect, select, and end', function(done) {
var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig);

requestConnectionSelectAndClose(pool, function() {
done();
});
testPool(poolConfig, poolConfig.maxSize, requestConnectionSelectAndClose, done);
});
});

describe('multiple connections within pool maxSize', function() {
var poolSize = 5;
var poolConfig = {maxSize: 5};

it('should connect, and end', function(done) {
var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig);

function doIt(done) {
requestConnectionAndClose(pool, function() {
done();
});
}

var functions = [];
for (var f = 0; f < poolSize; f++) {
functions.push(doIt);
}

async.parallel(functions, done);
testPool(poolConfig, poolConfig.maxSize, requestConnectionAndClose, done);
});

it('should connect, select, and end', function(done) {
var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig);

function doIt(done) {
requestConnectionSelectAndClose(pool, function() {
done();
});
}

var functions = [];
for (var f = 0; f < poolSize; f++) {
functions.push(doIt);
}

async.parallel(functions, done);
testPool(poolConfig, poolConfig.maxSize, requestConnectionSelectAndClose, done);
});
});
});

function testPool(poolConfig, numberOfConnectionsToUse, useConnectionfunction, done) {
var pool = new ConnectionPool(poolConfig, connectionConfig);

function doIt(done) {
useConnectionfunction(pool, function() {
done();
});
}

var functions = [];
for (var f = 0; f < numberOfConnectionsToUse; f++) {
functions.push(doIt);
}

async.parallel(functions, done);
}

function requestConnectionAndClose(pool, done) {
pool.requestConnection(function (connection) {
connection.on('connect', function(err) {
Expand Down

0 comments on commit f468f02

Please sign in to comment.