Skip to content

Commit bdb3b9f

Browse files
author
Rafa de la Torre
authored
Merge pull request #522 from CartoDB/521-test-robustness-pgbouncer
521 test robustness pgbouncer
2 parents de11573 + 942b007 commit bdb3b9f

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
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/prepare_db.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ if test x"$PREPARE_PGSQL" = xyes; then
7070

7171
echo "preparing postgres..."
7272
echo "PostgreSQL server version: `psql -A -t -c 'select version()'`"
73+
echo "PAUSE; RESUME;" | psql -p 6432 pgbouncer # make sure there are no connections pgbouncer -> test_db
7374
dropdb ${TEST_DB} # 2> /dev/null # error expected if doesn't exist, but not otherwise
7475
createdb -Ttemplate_postgis -EUTF8 ${TEST_DB} || die "Could not create test database"
7576
psql -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' ${TEST_DB}

test/run_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# To make output dates deterministic
44
export TZ='Europe/Rome'

test/support/db_utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 to reset internal pool connections of PgBouncer
26+
client.connect();
27+
client.query('PAUSE', err => {
28+
if (err) {
29+
return callback(err);
30+
}
31+
client.query('RESUME', err => {
32+
client.end();
33+
return callback(err);
34+
});
35+
});
36+
};

0 commit comments

Comments
 (0)