fix tests for postgres >= v9.2.0
This commit is contained in:
parent
541832658e
commit
f55a0cd1b4
@ -22,7 +22,8 @@
|
||||
"buffer-writer": "1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jshint": "1.1.0"
|
||||
"jshint": "1.1.0",
|
||||
"semver": "~1.1.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "make test-all connectionString=pg://postgres@localhost:5432/postgres",
|
||||
|
@ -5,18 +5,26 @@ test('error during query execution', function() {
|
||||
var client = new Client(helper.args);
|
||||
client.connect(assert.success(function() {
|
||||
var sleepQuery = 'select pg_sleep(5)';
|
||||
client.query(sleepQuery, assert.calls(function(err, result) {
|
||||
assert(err);
|
||||
client.end();
|
||||
}));
|
||||
var client2 = new Client(helper.args);
|
||||
client2.connect(assert.success(function() {
|
||||
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) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.rowCount, 1);
|
||||
client2.end();
|
||||
assert.emits(client2, 'end');
|
||||
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) {
|
||||
assert(err);
|
||||
client.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');
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
|
@ -5,25 +5,31 @@ pg = pg;
|
||||
//first make pool hold 2 clients
|
||||
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
|
||||
pg.connect(helper.config, assert.success(function(client, done) {
|
||||
client.id = 1;
|
||||
pg.connect(helper.config, assert.success(function(client2, done2) {
|
||||
client2.id = 2;
|
||||
done2();
|
||||
//subscribe to the pg error event
|
||||
assert.emits(pg, 'error', function(error, brokenClient) {
|
||||
assert.ok(error);
|
||||
assert.ok(brokenClient);
|
||||
assert.equal(client.id, brokenClient.id);
|
||||
});
|
||||
//kill the connection from client
|
||||
client2.query(killIdleQuery, assert.success(function(res) {
|
||||
//check to make sure client connection actually was killed
|
||||
assert.lengthIs(res.rows, 1);
|
||||
pg.end();
|
||||
pg.connect(helper.config, assert.success(function(client2, done2) {
|
||||
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();
|
||||
//subscribe to the pg error event
|
||||
assert.emits(pg, 'error', function(error, brokenClient) {
|
||||
assert.ok(error);
|
||||
assert.ok(brokenClient);
|
||||
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();
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
}));
|
||||
|
@ -13,6 +13,15 @@ helper.client = function() {
|
||||
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
|
||||
module.exports = helper;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user