2016-01-13 23:25:25 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Requires the database and tables setup in config/environments/test.js to exist
|
|
|
|
* Ensure the user is present in the pgbouncer auth file too
|
|
|
|
* TODO: Add OAuth tests.
|
|
|
|
*
|
|
|
|
* To run this test, ensure that cartodb_test_user_1_db metadata exists
|
|
|
|
* in Redis for the vizzuality.cartodb.com domain
|
|
|
|
*
|
|
|
|
* SELECT 5
|
|
|
|
* HSET rails:users:vizzuality id 1
|
|
|
|
* HSET rails:users:vizzuality database_name cartodb_test_user_1_db
|
|
|
|
*
|
|
|
|
*/
|
2016-10-06 17:53:56 +08:00
|
|
|
require('../../helper');
|
2016-01-13 23:25:25 +08:00
|
|
|
|
2016-10-06 17:53:56 +08:00
|
|
|
var server = require('../../../app/server')();
|
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var redisUtils = require('../../support/redis_utils');
|
2016-01-13 23:25:25 +08:00
|
|
|
var querystring = require('querystring');
|
2016-08-01 17:48:11 +08:00
|
|
|
var metadataBackend = require('cartodb-redis')(redisUtils.getConfig());
|
2016-10-06 17:53:56 +08:00
|
|
|
var batchFactory = require('../../../batch');
|
2016-01-13 23:25:25 +08:00
|
|
|
|
|
|
|
describe('Use case 4: modify a pending job', function() {
|
2016-08-01 17:48:11 +08:00
|
|
|
var batch = batchFactory(metadataBackend, redisUtils.getConfig());
|
2016-01-13 23:25:25 +08:00
|
|
|
|
2016-07-22 20:27:18 +08:00
|
|
|
before(function (done) {
|
2016-01-13 23:25:25 +08:00
|
|
|
batch.start();
|
2016-07-22 20:27:18 +08:00
|
|
|
batch.on('ready', done);
|
2016-01-13 23:25:25 +08:00
|
|
|
});
|
|
|
|
|
2016-01-26 00:30:07 +08:00
|
|
|
after(function (done) {
|
2016-01-25 21:51:37 +08:00
|
|
|
batch.stop();
|
2016-07-22 23:05:01 +08:00
|
|
|
redisUtils.clean('batch:*', done);
|
2016-01-13 23:25:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var runningJob = {};
|
|
|
|
var pendingJob = {};
|
|
|
|
|
|
|
|
it('Step 1, should create a job', function (done) {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-01-19 03:12:44 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2016-01-13 23:25:25 +08:00
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: "SELECT * FROM untitle_table_4; select pg_sleep(3)"
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 201
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function(err, res) {
|
2016-01-13 23:25:25 +08:00
|
|
|
runningJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 2, should create another job', function (done) {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-01-19 03:12:44 +08:00
|
|
|
url: '/api/v2/sql/job?api_key=1234',
|
2016-01-13 23:25:25 +08:00
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST',
|
|
|
|
data: querystring.stringify({
|
|
|
|
query: "SELECT * FROM untitle_table_4"
|
|
|
|
})
|
|
|
|
}, {
|
|
|
|
status: 201
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function(err, res) {
|
2016-01-13 23:25:25 +08:00
|
|
|
pendingJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 3, job should be pending', function (done){
|
|
|
|
var interval = setInterval(function () {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-01-19 03:12:44 +08:00
|
|
|
url: '/api/v2/sql/job/' + pendingJob.job_id + '?api_key=1234',
|
2016-01-13 23:25:25 +08:00
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'GET'
|
|
|
|
}, {
|
|
|
|
status: 200
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function(err, res) {
|
2016-01-13 23:25:25 +08:00
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === "pending") {
|
|
|
|
clearInterval(interval);
|
|
|
|
done();
|
2016-01-25 23:44:00 +08:00
|
|
|
} else {
|
|
|
|
clearInterval(interval);
|
|
|
|
done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be pending'));
|
2016-01-13 23:25:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 5, running job should be cancelled', function (done){
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2016-01-19 03:12:44 +08:00
|
|
|
url: '/api/v2/sql/job/' + runningJob.job_id + '?api_key=1234',
|
2016-01-13 23:25:25 +08:00
|
|
|
headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'DELETE'
|
|
|
|
}, {
|
|
|
|
status: 200
|
2016-09-26 20:37:40 +08:00
|
|
|
}, function(err, res) {
|
2016-01-13 23:25:25 +08:00
|
|
|
var cancelledJob = JSON.parse(res.body);
|
|
|
|
assert.equal(cancelledJob.status, "cancelled");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|