CartoDB-SQL-API/test/acceptance/batch/use-cases-test.js

193 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
2016-11-24 06:48:23 +08:00
require('../../helper');
var assert = require('../../support/assert');
var JobStatus = require('../../../lib/batch/job-status');
2016-11-24 06:48:23 +08:00
var BatchTestClient = require('../../support/batch-test-client');
describe('Use cases', function () {
2019-12-24 01:19:08 +08:00
before(function () {
2016-11-24 06:48:23 +08:00
this.batchTestClient = new BatchTestClient();
});
2019-12-24 01:19:08 +08:00
after(function (done) {
2016-11-24 06:48:23 +08:00
this.batchTestClient.drain(done);
});
it('cancel a done job should return an error', function (done) {
var payload = {
2019-12-24 01:19:08 +08:00
query: 'SELECT * FROM untitle_table_4'
2016-11-24 06:48:23 +08:00
};
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(JobStatus.DONE, function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.DONE);
2016-11-24 06:48:23 +08:00
jobResult.tryCancel(function (err, body) {
2019-12-26 23:57:24 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(body.error[0], 'Cannot set status from done to cancelled');
2016-11-24 06:48:23 +08:00
done();
});
});
});
});
it('cancel a running job', function (done) {
var payload = {
2019-12-24 01:19:08 +08:00
query: 'SELECT * FROM untitle_table_4; select pg_sleep(3)'
2016-11-24 06:48:23 +08:00
};
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(JobStatus.RUNNING, function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.RUNNING);
2016-11-24 06:48:23 +08:00
jobResult.cancel(function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.CANCELLED);
2016-11-24 06:48:23 +08:00
jobResult.tryCancel(function (err, body) {
2019-12-26 23:57:24 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(body.error[0], 'Cannot set status from cancelled to cancelled');
2016-11-24 06:48:23 +08:00
done();
});
});
});
});
});
it('cancel a pending job', function (done) {
var self = this;
var payload1 = {
2019-12-24 01:19:08 +08:00
query: 'SELECT * FROM untitle_table_4; select pg_sleep(3)'
2016-11-24 06:48:23 +08:00
};
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload1, function (err, jobResult1) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
var payload2 = {
2019-12-24 01:19:08 +08:00
query: 'SELECT * FROM untitle_table_4'
2016-11-24 06:48:23 +08:00
};
2019-12-24 01:19:08 +08:00
self.batchTestClient.createJob(payload2, function (err, jobResult2) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
jobResult2.getStatus(JobStatus.PENDING, function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.PENDING);
2016-11-24 06:48:23 +08:00
jobResult2.cancel(function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.CANCELLED);
2016-11-24 06:48:23 +08:00
jobResult1.cancel(function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.CANCELLED);
2016-11-24 06:48:23 +08:00
done();
});
});
});
});
});
});
it('cancel a job with quotes', function (done) {
var payload = {
query: "SELECT name FROM untitle_table_4 WHERE name = 'Hawai'; select pg_sleep(3)"
};
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(JobStatus.RUNNING, function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.RUNNING);
2016-11-24 06:48:23 +08:00
jobResult.cancel(function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.CANCELLED);
2016-11-24 06:48:23 +08:00
done();
});
});
});
});
it('cancel a running multiquery job', function (done) {
var payload = {
query: [
2019-12-24 01:19:08 +08:00
'select pg_sleep(1)',
'select pg_sleep(1)',
'select pg_sleep(1)'
2016-11-24 06:48:23 +08:00
]
};
2019-12-24 01:19:08 +08:00
this.batchTestClient.createJob(payload, function (err, jobResult) {
2016-11-24 06:48:23 +08:00
if (err) {
return done(err);
}
jobResult.getStatus(JobStatus.RUNNING, function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.RUNNING);
2016-11-24 06:48:23 +08:00
jobResult.cancel(function (err, job) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(job.status, JobStatus.CANCELLED);
2016-11-24 06:48:23 +08:00
jobResult.tryCancel(function (err, body) {
2019-12-26 23:57:24 +08:00
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(body.error[0], 'Cannot set status from cancelled to cancelled');
2016-11-24 06:48:23 +08:00
done();
});
});
});
});
});
});