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');
|
2019-10-04 00:24:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.DONE);
|
|
|
|
|
|
|
|
jobResult.tryCancel(function (err, body) {
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.equal(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.RUNNING);
|
|
|
|
|
|
|
|
jobResult.cancel(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.CANCELLED);
|
|
|
|
|
|
|
|
jobResult.tryCancel(function (err, body) {
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.equal(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.PENDING);
|
|
|
|
|
|
|
|
jobResult2.cancel(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.CANCELLED);
|
|
|
|
|
|
|
|
jobResult1.cancel(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.CANCELLED);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.RUNNING);
|
|
|
|
|
|
|
|
jobResult.cancel(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.CANCELLED);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.RUNNING);
|
|
|
|
|
|
|
|
jobResult.cancel(function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(job.status, JobStatus.CANCELLED);
|
|
|
|
|
|
|
|
jobResult.tryCancel(function (err, body) {
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.equal(body.error[0], 'Cannot set status from cancelled to cancelled');
|
2016-11-24 06:48:23 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|