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,18 +5,26 @@ 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)';
client.query(sleepQuery, assert.calls(function(err, result) { var pidColName = 'procpid'
assert(err); var queryColName = 'current_query';
client.end(); helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) {
})); if(isGreater) {
var client2 = new Client(helper.args); pidColName = 'pid';
client2.connect(assert.success(function() { queryColName = 'query';
var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query = $1"; }
client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { client.query(sleepQuery, assert.calls(function(err, result) {
assert.ifError(err); assert(err);
assert.equal(res.rowCount, 1); client.end();
client2.end(); }));
assert.emits(client2, 'end'); var client2 = new Client(helper.args);
client2.connect(assert.success(function() {
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) {
assert.ifError(err);
assert.equal(res.rowCount, 1);
client2.end();
assert.emits(client2, 'end');
}));
})); }));
})); }));
})); }));

View File

@ -5,25 +5,31 @@ 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;
done2(); var pidColName = 'procpid'
//subscribe to the pg error event helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) {
assert.emits(pg, 'error', function(error, brokenClient) { if(isGreater) {
assert.ok(error); pidColName = 'pid';
assert.ok(brokenClient); }
assert.equal(client.id, brokenClient.id); var killIdleQuery = 'SELECT ' + pidColName + ', (SELECT pg_terminate_backend(' + pidColName + ')) AS killed FROM pg_stat_activity WHERE state = $1';
}); done2();
//kill the connection from client //subscribe to the pg error event
client2.query(killIdleQuery, assert.success(function(res) { assert.emits(pg, 'error', function(error, brokenClient) {
//check to make sure client connection actually was killed assert.ok(error);
assert.lengthIs(res.rows, 1); assert.ok(brokenClient);
pg.end(); assert.equal(client.id, brokenClient.id);
});
//kill the connection from client
client2.query(killIdleQuery, ['idle'], assert.success(function(res) {
//check to make sure client connection actually was killed
assert.lengthIs(res.rows, 1);
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;