CartoDB-SQL-API/test/acceptance/batch/queued-jobs-limit.test.js

84 lines
2.8 KiB
JavaScript
Raw Normal View History

require('../../helper');
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');
var batchFactory = require('../../../batch/index');
2016-10-17 21:02:34 +08:00
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
2016-10-17 18:28:01 +08:00
var TestClient = require('../../support/test-client');
describe('max queued jobs', function() {
2016-10-17 18:28:01 +08:00
before(function(done) {
this.batch_max_queued_jobs = global.settings.batch_max_queued_jobs;
global.settings.batch_max_queued_jobs = 1;
this.server = require('../../../app/server')();
2016-10-17 18:28:01 +08:00
this.testClient = new TestClient();
this.testClient.getResult(
'drop table if exists max_queued_jobs_inserts; create table max_queued_jobs_inserts (status numeric)',
done
);
});
after(function (done) {
2016-10-17 18:28:01 +08:00
var self = this;
global.settings.batch_max_queued_jobs = this.batch_max_queued_jobs;
2016-10-17 21:02:34 +08:00
var batch = batchFactory(metadataBackend, redisUtils.getPool());
batch.start();
batch.on('ready', function() {
2016-10-19 02:05:57 +08:00
// this is not ideal as the first job might not be committed yet
setTimeout(function() {
batch.stop(function() {
self.testClient.getResult('select count(*) from max_queued_jobs_inserts', function(err, rows) {
assert.ok(!err);
assert.equal(rows[0].count, 1);
2016-10-17 18:28:01 +08:00
2016-10-19 02:05:57 +08:00
redisUtils.clean('batch:*', done);
});
2016-10-17 18:28:01 +08:00
});
2016-10-19 02:05:57 +08:00
}, 100);
});
});
function createJob(server, status, callback) {
assert.response(
server,
{
url: '/api/v2/sql/job?api_key=1234',
headers: {
host: 'vizzuality.cartodb.com',
'Content-Type': 'application/json'
},
method: 'POST',
data: JSON.stringify({
2016-10-17 18:28:01 +08:00
query: "insert into max_queued_jobs_inserts values (1)"
})
},
{
status: status
},
function(err, res) {
if (err) {
return callback(err);
}
return callback(null, JSON.parse(res.body));
}
);
}
it('POST /api/v2/sql/job should respond with 200 and the created job', function (done) {
var self = this;
createJob(this.server, 201, function(err) {
assert.ok(!err);
createJob(self.server, 400, function(err, res) {
assert.ok(!err);
2016-10-17 21:23:53 +08:00
assert.equal(res.error[0], "Failed to create job. Max number of jobs (" +
global.settings.batch_max_queued_jobs + ") queued reached");
done();
});
});
});
});