Skip to content

Commit

Permalink
fixed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-page committed Oct 27, 2015
1 parent c08726e commit 5d0ad40
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 92 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
],
"licenses": "MIT",
"dependencies": {
"tedious": "^1.8.0"
"tedious": "^1.13.0"
},
"devDependencies": {
"mocha": ">=1.6.0"
Expand Down
111 changes: 20 additions & 91 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
var assert = require('assert');
var Request = require('tedious').Request;
var fs = require('fs');
var ConnectionPool = require('../lib/connection-pool');

function getConfig() {
var config = JSON.parse(fs.readFileSync(process.env.HOME + '/.tedious/test-connection-pool.json', 'utf8')).config;
return config;
}
var connectionConfig = {
userName: 'test',
password: 'test',
server: 'dev1',
options: {
appName: 'pool-test',
database: 'test'
}
};

/* create a db user with the correct permissions:
CREATE DATABASE test
Expand All @@ -33,7 +37,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {min: 2};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, poolConfig.min);
Expand All @@ -45,7 +49,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {min: 0, idleTimeout: 10};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, 0);
Expand Down Expand Up @@ -77,7 +81,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {min: 2, max: 5};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

var count = 20;
var run = 0;
Expand Down Expand Up @@ -116,7 +120,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = { min: 5, max: 1, idleTimeout: 10};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, 1);
Expand Down Expand Up @@ -148,7 +152,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {max: 1, idleTimeout: 10};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
assert.equal(pool.connections.length, 1);
Expand Down Expand Up @@ -196,7 +200,7 @@ describe('ConnectionPool', function () {

pool.on('error', function(err) {
assert(!!err);
pool.connectionConfig = getConfig();
pool.connectionConfig = connectionConfig;
});

function testConnected() {
Expand All @@ -218,7 +222,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {min: 1, max: 1, acquireTimeout: 2000};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

pool.acquire(function(err, connection) {
assert(!err);
Expand All @@ -235,7 +239,7 @@ describe('ConnectionPool', function () {
it('idle timeout', function (done) {
this.timeout(10000);
var poolConfig = {min: 1, max: 5, idleTimeout: 100};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

setTimeout(function() {
pool.acquire(function (err, connection) {
Expand All @@ -256,13 +260,9 @@ describe('ConnectionPool', function () {
}, 300);
});

it('lost connection handling', function (done) {
it('connection error handling', function (done) {
this.timeout(10000);
var poolConfig = {min: 1, max: 5};
var connectionConfig = getConfig();

connectionConfig.options = connectionConfig.options || {};
connectionConfig.options.requestTimeout = 5000;

var pool = new ConnectionPool(poolConfig, connectionConfig);

Expand Down Expand Up @@ -292,7 +292,6 @@ describe('ConnectionPool', function () {

var request = new Request(command, function (err, rowCount) {
assert(err);
assert(err.code,'ETIMEOUT');
pool.drain(done);
});

Expand All @@ -308,7 +307,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {max: 1};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

var createRequest = function(query, value, callback) {
var request = new Request(query, function (err, rowCount) {
Expand Down Expand Up @@ -345,7 +344,7 @@ describe('ConnectionPool', function () {
this.timeout(10000);

var poolConfig = {min: 3};
var pool = new ConnectionPool(poolConfig, getConfig());
var pool = new ConnectionPool(poolConfig, connectionConfig);

pool.acquire(function() { });

Expand All @@ -354,74 +353,4 @@ describe('ConnectionPool', function () {
pool.drain(done);
}, 4);
});

it('request callback should be called on socket error', function (done) {
this.timeout(10000);

var poolConfig = {min: 1};

var connectionConfig = getConfig();
connectionConfig.options = connectionConfig.options || {};
connectionConfig.options.requestTimeout = 60000;

var pool = new ConnectionPool(poolConfig, connectionConfig);

pool.acquire(function(err, conn) {
var request = new Request("WAITFOR 00:00:30", function(err) {
assert(err);
assert.equal(err.message, 'socket error');
pool.drain(done);
});
conn.execSql(request);
conn.socket.emit('error', new Error('socket error'));
});
pool.on('error', function(err) {
assert.equal(err.code, 'ESOCKET');
});
});

it('connection should still work after socket error', function (done) {
this.timeout(10000);

var poolConfig = {min: 1, max: 1};

var connectionConfig = getConfig();
connectionConfig.options = connectionConfig.options || {};
connectionConfig.options.requestTimeout = 60000;

var pool = new ConnectionPool(poolConfig, connectionConfig);

var createRequest = function(query, value, callback) {
var request = new Request(query, function (err, rowCount) {
assert.strictEqual(rowCount, 1);
callback();
});

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

return request;
};

pool.acquire(function(err, conn) {
var request = new Request("WAITFOR 00:00:30", function(err) {
assert(err);
assert.equal(err.message, 'socket error');
conn.release();
pool.acquire(function(err, conn) {
assert(!err);
conn.execSql(createRequest('SELECT 42', 42, function () {
pool.drain(done);
}));
});
});
conn.execSql(request);
conn.socket.emit('error', new Error('socket error'));
});
pool.on('error', function(err) {
assert.equal(err.code, 'ESOCKET');
});
});

});

0 comments on commit 5d0ad40

Please sign in to comment.