2011-08-29 15:35:46 +08:00
|
|
|
var helper = require(__dirname + "/../test-helper");
|
|
|
|
var pg = require(__dirname + "/../../../lib");
|
2013-03-08 05:57:00 +08:00
|
|
|
pg = pg;
|
2011-08-29 15:35:46 +08:00
|
|
|
|
|
|
|
//first make pool hold 2 clients
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.defaults.poolSize = 2;
|
2011-08-29 15:35:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
//get first client
|
2013-03-08 05:57:00 +08:00
|
|
|
pg.connect(helper.config, assert.success(function(client, done) {
|
2011-08-29 15:35:46 +08:00
|
|
|
client.id = 1;
|
2013-04-17 22:26:31 +08:00
|
|
|
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) {
|
2013-07-23 22:51:48 +08:00
|
|
|
console.log(isGreater)
|
2013-04-17 22:54:16 +08:00
|
|
|
var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1';
|
|
|
|
var params = ['idle'];
|
|
|
|
if(!isGreater) {
|
|
|
|
killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1';
|
|
|
|
params = ['%IDLE%']
|
2013-04-17 22:26:31 +08:00
|
|
|
}
|
2013-07-23 22:51:48 +08:00
|
|
|
|
2013-04-17 22:26:31 +08:00
|
|
|
//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);
|
|
|
|
});
|
2013-07-23 22:51:48 +08:00
|
|
|
|
2013-04-17 22:26:31 +08:00
|
|
|
//kill the connection from client
|
2013-04-17 22:54:16 +08:00
|
|
|
client2.query(killIdleQuery, params, assert.success(function(res) {
|
2013-04-17 22:26:31 +08:00
|
|
|
//check to make sure client connection actually was killed
|
2013-07-23 22:51:48 +08:00
|
|
|
//return client2 to the pool
|
|
|
|
done2();
|
2013-04-17 22:26:31 +08:00
|
|
|
pg.end();
|
|
|
|
}));
|
|
|
|
}));
|
2011-08-29 15:35:46 +08:00
|
|
|
}));
|
|
|
|
}));
|