Skip to content

Commit

Permalink
Add some tests for multiple connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Oct 6, 2012
1 parent dcb945a commit dedda2c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function ConnectionPool(poolConfig, connectionConfig) {
var connection = new PooledConnection(pool, connectionConfig);
pool.inUseConnections.push(connection);

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

return {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"license": "MIT",
"devDependencies": {
"tedious": "0.0.7",
"mocha": "1.6.0"
"mocha": "1.6.0",
"async": "0.1.22"
},
"licenses": "MIT"
}
97 changes: 87 additions & 10 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var assert = require('assert')
var async = require('async')
var ConnectionPool = require('../index');
var Request = require('tedious').Request;

var connectionConfig = {
userName: 'test',
Expand All @@ -9,18 +11,93 @@ var connectionConfig = {

describe('ConnectionPool', function() {
describe('one connection', function() {
it('should connect and end', function(done) {
var cp = new ConnectionPool({maxSize: 2}, connectionConfig);
var poolSize = 1;

cp.requestConnection(function (connection) {
connection.on('connect', function(err) {
connection.close();
it('should connect, and end', function(done) {
var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig);

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

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

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

describe('multiple connections within pool maxSize', function() {
var poolSize = 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);
});

connection.on('end', function(err) {
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);
});
});
});

function requestConnectionAndClose(pool, done) {
pool.requestConnection(function (connection) {
connection.on('connect', function(err) {
connection.close();
});

connection.on('end', function(err) {
done();
});
});
}

function requestConnectionSelectAndClose(pool, done) {
pool.requestConnection(function (connection) {
var request = new Request('select 42', function(err, rowCount) {
assert.strictEqual(rowCount, 1)
connection.close()
});

request.on('row', function(columns) {
assert.strictEqual(columns[0].value, 42)
});

connection.on('connect', function(err) {
connection.execSql(request)
});

connection.on('end', function(err) {
done();
});
});
}

0 comments on commit dedda2c

Please sign in to comment.