fix tests for postgres >= v9.2.0

This commit is contained in:
bmc 2013-04-17 09:26:31 -05:00
parent 541832658e
commit f55a0cd1b4
4 changed files with 53 additions and 29 deletions

View File

@ -22,7 +22,8 @@
"buffer-writer": "1.0.0" "buffer-writer": "1.0.0"
}, },
"devDependencies": { "devDependencies": {
"jshint": "1.1.0" "jshint": "1.1.0",
"semver": "~1.1.4"
}, },
"scripts": { "scripts": {
"test": "make test-all connectionString=pg://postgres@localhost:5432/postgres", "test": "make test-all connectionString=pg://postgres@localhost:5432/postgres",

View File

@ -5,13 +5,20 @@ test('error during query execution', function() {
var client = new Client(helper.args); var client = new Client(helper.args);
client.connect(assert.success(function() { client.connect(assert.success(function() {
var sleepQuery = 'select pg_sleep(5)'; var sleepQuery = 'select pg_sleep(5)';
var pidColName = 'procpid'
var queryColName = 'current_query';
helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) {
if(isGreater) {
pidColName = 'pid';
queryColName = 'query';
}
client.query(sleepQuery, assert.calls(function(err, result) { client.query(sleepQuery, assert.calls(function(err, result) {
assert(err); assert(err);
client.end(); client.end();
})); }));
var client2 = new Client(helper.args); var client2 = new Client(helper.args);
client2.connect(assert.success(function() { client2.connect(assert.success(function() {
var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query = $1"; var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1";
client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) {
assert.ifError(err); assert.ifError(err);
assert.equal(res.rowCount, 1); assert.equal(res.rowCount, 1);
@ -20,4 +27,5 @@ var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS k
})); }));
})); }));
})); }));
}));
}); });

View File

@ -5,13 +5,18 @@ pg = pg;
//first make pool hold 2 clients //first make pool hold 2 clients
pg.defaults.poolSize = 2; pg.defaults.poolSize = 2;
var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'<IDLE>\'';
//get first client //get first client
pg.connect(helper.config, assert.success(function(client, done) { pg.connect(helper.config, assert.success(function(client, done) {
client.id = 1; client.id = 1;
pg.connect(helper.config, assert.success(function(client2, done2) { pg.connect(helper.config, assert.success(function(client2, done2) {
client2.id = 2; client2.id = 2;
var pidColName = 'procpid'
helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) {
if(isGreater) {
pidColName = 'pid';
}
var killIdleQuery = 'SELECT ' + pidColName + ', (SELECT pg_terminate_backend(' + pidColName + ')) AS killed FROM pg_stat_activity WHERE state = $1';
done2(); done2();
//subscribe to the pg error event //subscribe to the pg error event
assert.emits(pg, 'error', function(error, brokenClient) { assert.emits(pg, 'error', function(error, brokenClient) {
@ -20,10 +25,11 @@ pg.connect(helper.config, assert.success(function(client, done) {
assert.equal(client.id, brokenClient.id); assert.equal(client.id, brokenClient.id);
}); });
//kill the connection from client //kill the connection from client
client2.query(killIdleQuery, assert.success(function(res) { client2.query(killIdleQuery, ['idle'], assert.success(function(res) {
//check to make sure client connection actually was killed //check to make sure client connection actually was killed
assert.lengthIs(res.rows, 1); assert.lengthIs(res.rows, 1);
pg.end(); pg.end();
})); }));
})); }));
})); }));
}));

View File

@ -13,6 +13,15 @@ helper.client = function() {
return client; return client;
}; };
var semver = require('semver');
helper.versionGTE = function(client, versionString, callback) {
client.query('SELECT version()', assert.calls(function(err, result) {
if(err) return callback(err);
var version = result.rows[0].version.split(' ')[1];
return callback(null, semver.gte(version, versionString));
}));
};
//export parent helper stuffs //export parent helper stuffs
module.exports = helper; module.exports = helper;