Skip to content

Commit

Permalink
Merge pull request #522 from CartoDB/521-test-robustness-pgbouncer
Browse files Browse the repository at this point in the history
521 test robustness pgbouncer
  • Loading branch information
Rafa de la Torre authored Jul 30, 2018
2 parents de11573 + 942b007 commit bdb3b9f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion test/acceptance/batch/batch-limits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var BatchTestClient = require('../../support/batch-test-client');
var JobStatus = require('../../../batch/job_status');
var redisUtils = require('../../support/redis_utils');
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
const db_utils = require('../../support/db_utils');

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

Expand All @@ -14,13 +15,14 @@ describe('batch query statement_timeout limit', function() {
global.settings.batch_query_timeout = 15000;
metadataBackend.redisCmd(5, 'HMSET', ['limits:batch:vizzuality', 'timeout', 100], done);
});

before(db_utils.resetPgBouncerConnections);
after(function(done) {
global.settings.batch_query_timeout = this.batchQueryTimeout;
redisUtils.clean('limits:batch:*', function() {
this.batchTestClient.drain(done);
}.bind(this));
});
after(db_utils.resetPgBouncerConnections);

function jobPayload(query) {
return {
Expand Down
4 changes: 4 additions & 0 deletions test/acceptance/export/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ require('../../support/assert');

var assert = require('assert');
var querystring = require('querystring');
const db_utils = require('../../support/db_utils');

describe('timeout', function () {
describe('export database', function () {
before(db_utils.resetPgBouncerConnections);
after(db_utils.resetPgBouncerConnections);

const databaseTimeoutQuery = `
select
ST_SetSRID(ST_Point(0, 0), 4326) as the_geom,
Expand Down
1 change: 1 addition & 0 deletions test/prepare_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ if test x"$PREPARE_PGSQL" = xyes; then

echo "preparing postgres..."
echo "PostgreSQL server version: `psql -A -t -c 'select version()'`"
echo "PAUSE; RESUME;" | psql -p 6432 pgbouncer # make sure there are no connections pgbouncer -> test_db
dropdb ${TEST_DB} # 2> /dev/null # error expected if doesn't exist, but not otherwise
createdb -Ttemplate_postgis -EUTF8 ${TEST_DB} || die "Could not create test database"
psql -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' ${TEST_DB}
Expand Down
2 changes: 1 addition & 1 deletion test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

# To make output dates deterministic
export TZ='Europe/Rome'
Expand Down
36 changes: 36 additions & 0 deletions test/support/db_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const { Client } = require('pg');

const dbConfig = {
db_user: process.env.PGUSER || 'postgres',
db_host: global.settings.db_host,
db_port: global.settings.db_port,
db_batch_port: global.settings.db_batch_port
};

module.exports.resetPgBouncerConnections = function (callback) {
// We assume there's no pgbouncer if db_port === db_batch_port
if (dbConfig.db_port === dbConfig.db_batch_port) {
return callback();
}

const client = new Client({
database: 'pgbouncer',
user: dbConfig.db_user,
host: dbConfig.db_host,
port: dbConfig.db_port
});

// We just chain a PAUSE followed by a RESUME to reset internal pool connections of PgBouncer
client.connect();
client.query('PAUSE', err => {
if (err) {
return callback(err);
}
client.query('RESUME', err => {
client.end();
return callback(err);
});
});
};

0 comments on commit bdb3b9f

Please sign in to comment.