Skip to content

Commit 20d1db7

Browse files
author
Rafa de la Torre
committed
Helper to reset pgbouncer connections #521
This sends a PAUSE and RESUME to pgbouncer (in case there's one) before and after executing tests, to make sure new connections are established in the tests. This may be especially important when role or session settings are modified in the DB (same happens in prod, BTW).
1 parent 6eeb949 commit 20d1db7

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

test/acceptance/batch/batch-limits.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var BatchTestClient = require('../../support/batch-test-client');
55
var JobStatus = require('../../../batch/job_status');
66
var redisUtils = require('../../support/redis_utils');
77
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
8+
const db_utils = require('../../support/db_utils');
89

910
describe('batch query statement_timeout limit', function() {
1011

@@ -14,13 +15,14 @@ describe('batch query statement_timeout limit', function() {
1415
global.settings.batch_query_timeout = 15000;
1516
metadataBackend.redisCmd(5, 'HMSET', ['limits:batch:vizzuality', 'timeout', 100], done);
1617
});
17-
18+
before(db_utils.resetPgBouncerConnections);
1819
after(function(done) {
1920
global.settings.batch_query_timeout = this.batchQueryTimeout;
2021
redisUtils.clean('limits:batch:*', function() {
2122
this.batchTestClient.drain(done);
2223
}.bind(this));
2324
});
25+
after(db_utils.resetPgBouncerConnections);
2426

2527
function jobPayload(query) {
2628
return {

test/acceptance/export/timeout.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ require('../../support/assert');
44

55
var assert = require('assert');
66
var querystring = require('querystring');
7+
const db_utils = require('../../support/db_utils');
78

89
describe('timeout', function () {
910
describe('export database', function () {
11+
before(db_utils.resetPgBouncerConnections);
12+
after(db_utils.resetPgBouncerConnections);
13+
1014
const databaseTimeoutQuery = `
1115
select
1216
ST_SetSRID(ST_Point(0, 0), 4326) as the_geom,

test/support/db_utils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const { Client } = require('pg');
4+
5+
const dbConfig = {
6+
db_user: process.env.PGUSER || 'postgres',
7+
db_host: global.settings.db_host,
8+
db_port: global.settings.db_port,
9+
db_batch_port: global.settings.db_batch_port
10+
};
11+
12+
module.exports.resetPgBouncerConnections = function (callback) {
13+
// We assume there's no pgbouncer if db_port === db_batch_port
14+
if (dbConfig.db_port === dbConfig.db_batch_port) {
15+
return callback();
16+
}
17+
18+
const client = new Client({
19+
database: 'pgbouncer',
20+
user: dbConfig.db_user,
21+
host: dbConfig.db_host,
22+
port: dbConfig.db_port
23+
});
24+
25+
// We just chain a PAUSE followed by a RESUME
26+
client.connect();
27+
client.query('PAUSE', (err, res) => {
28+
if (err) {
29+
return callback(err);
30+
} else {
31+
client.query('RESUME', (err, res) => {
32+
client.end();
33+
return callback(err);
34+
});
35+
}
36+
});
37+
}

0 commit comments

Comments
 (0)