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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
require('../helper');
|
|
|
|
|
|
|
|
var app = require(global.settings.app_root + '/app/app')();
|
|
|
|
var assert = require('../support/assert');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var metadataBackend = require('cartodb-redis')({
|
|
|
|
host: global.settings.redis_host,
|
|
|
|
port: global.settings.redis_port,
|
|
|
|
max: global.settings.redisPool,
|
|
|
|
idleTimeoutMillis: global.settings.redisIdleTimeoutMillis,
|
|
|
|
reapIntervalMillis: global.settings.redisReapIntervalMillis
|
|
|
|
});
|
|
|
|
var batchFactory = require('../../batch');
|
|
|
|
|
|
|
|
describe('Use case 3: cancel a pending job', function() {
|
|
|
|
|
|
|
|
var batch = batchFactory(metadataBackend);
|
|
|
|
|
2016-01-14 21:08:34 +08:00
|
|
|
before(function () {
|
2016-01-13 23:25:25 +08:00
|
|
|
batch.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function (done) {
|
|
|
|
batch.stop(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
var runningJob = {};
|
|
|
|
var pendingJob = {};
|
|
|
|
|
|
|
|
it('Step 1, should create a job', function (done) {
|
|
|
|
assert.response(app, {
|
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
|
|
|
|
}, function (res) {
|
|
|
|
runningJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 2, should create a another job', function (done) {
|
|
|
|
assert.response(app, {
|
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
|
|
|
|
}, function(res) {
|
|
|
|
pendingJob = JSON.parse(res.body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 3, job should be pending', function (done){
|
|
|
|
var interval = setInterval(function () {
|
|
|
|
assert.response(app, {
|
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
|
|
|
|
}, function(res) {
|
|
|
|
var job = JSON.parse(res.body);
|
|
|
|
if (job.status === "pending") {
|
|
|
|
clearInterval(interval);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 50);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 4, cancel a job should be cancelled', function (done){
|
|
|
|
assert.response(app, {
|
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: 'DELETE'
|
|
|
|
}, {
|
|
|
|
status: 200
|
|
|
|
}, function(res) {
|
|
|
|
var jobGot = JSON.parse(res.body);
|
|
|
|
assert.equal(jobGot.job_id, pendingJob.job_id);
|
|
|
|
assert.equal(jobGot.status, "cancelled");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Step 5, running job should be cancelled', function (done){
|
|
|
|
assert.response(app, {
|
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
|
|
|
|
}, function(res) {
|
|
|
|
var cancelledJob = JSON.parse(res.body);
|
|
|
|
assert.equal(cancelledJob.status, "cancelled");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|