2013-03-29 02:24:33 +08:00
|
|
|
var helper = require(__dirname + '/test-helper');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
test('error during query execution', function() {
|
2013-03-29 04:06:34 +08:00
|
|
|
var client = new Client(helper.args);
|
2013-03-29 02:24:33 +08:00
|
|
|
client.connect(assert.success(function() {
|
|
|
|
var sleepQuery = 'select pg_sleep(5)';
|
2013-04-17 22:26:31 +08:00
|
|
|
var pidColName = 'procpid'
|
|
|
|
var queryColName = 'current_query';
|
|
|
|
helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) {
|
|
|
|
if(isGreater) {
|
|
|
|
pidColName = 'pid';
|
|
|
|
queryColName = 'query';
|
|
|
|
}
|
2014-04-07 00:53:47 +08:00
|
|
|
var query1 = client.query(sleepQuery, assert.calls(function(err, result) {
|
2013-04-17 22:26:31 +08:00
|
|
|
assert(err);
|
|
|
|
client.end();
|
|
|
|
}));
|
2014-04-07 00:53:47 +08:00
|
|
|
//ensure query1 does not emit an 'end' event
|
|
|
|
//because it was killed and received an error
|
|
|
|
//https://github.com/brianc/node-postgres/issues/547
|
|
|
|
query1.on('end', function() {
|
2014-04-07 01:55:26 +08:00
|
|
|
assert.fail('Query with an error should not emit "end" event')
|
2014-04-07 00:53:47 +08:00
|
|
|
})
|
2013-04-17 22:26:31 +08:00
|
|
|
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);
|
2013-04-17 23:29:21 +08:00
|
|
|
assert.equal(res.rows.length, 1);
|
2013-04-17 22:26:31 +08:00
|
|
|
client2.end();
|
|
|
|
assert.emits(client2, 'end');
|
|
|
|
}));
|
2013-03-29 02:24:33 +08:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|