2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-17 18:33:49 +08:00
|
|
|
require('../../helper');
|
|
|
|
var assert = require('../../support/assert');
|
|
|
|
|
|
|
|
var TestClient = require('../../support/test-client');
|
|
|
|
var BatchTestClient = require('../../support/batch-test-client');
|
2019-10-04 00:24:39 +08:00
|
|
|
var JobStatus = require('../../../lib/batch/job-status');
|
2016-10-17 18:33:49 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('multiple batch clients and users, job query order', function () {
|
|
|
|
before(function (done) {
|
2016-10-17 18:33:49 +08:00
|
|
|
this.batchTestClientA = new BatchTestClient({ name: 'consumerA' });
|
|
|
|
this.batchTestClientB = new BatchTestClient({ name: 'consumerB' });
|
|
|
|
|
2016-10-19 22:58:57 +08:00
|
|
|
this.testClient = new TestClient();
|
|
|
|
this.testClient.getResult(
|
|
|
|
[
|
|
|
|
'drop table if exists ordered_inserts_a',
|
|
|
|
'drop table if exists ordered_inserts_bbbbb',
|
|
|
|
'create table ordered_inserts_a (status numeric)',
|
|
|
|
'create table ordered_inserts_bbbbb (status numeric)'
|
|
|
|
].join(';'),
|
2016-10-17 18:33:49 +08:00
|
|
|
done
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function (done) {
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClientA.drain(function (err) {
|
2016-10-17 18:33:49 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.batchTestClientB.drain(done);
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function createJob (queries) {
|
2016-10-17 18:33:49 +08:00
|
|
|
return {
|
|
|
|
query: queries
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should run job queries in order (multiple consumers)', function (done) {
|
|
|
|
var jobRequestA1 = createJob([
|
2019-12-24 01:19:08 +08:00
|
|
|
'insert into ordered_inserts_a values(1)',
|
|
|
|
'select pg_sleep(0.25)',
|
|
|
|
'insert into ordered_inserts_a values(2)'
|
2016-10-17 18:33:49 +08:00
|
|
|
]);
|
|
|
|
var jobRequestA2 = createJob([
|
2019-12-24 01:19:08 +08:00
|
|
|
'insert into ordered_inserts_a values(3)'
|
2016-10-17 18:33:49 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
var jobRequestB1 = createJob([
|
2019-12-24 01:19:08 +08:00
|
|
|
'insert into ordered_inserts_bbbbb values(1)'
|
2016-10-17 18:33:49 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClientA.createJob(jobRequestA1, function (err, jobResultA1) {
|
2016-10-17 18:33:49 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2016-10-19 22:58:57 +08:00
|
|
|
var override = { host: 'cartodb250user.cartodb.com' };
|
2019-12-24 01:19:08 +08:00
|
|
|
self.batchTestClientB.createJob(jobRequestB1, override, function (err, jobResultB1) {
|
2016-10-17 18:33:49 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2016-10-19 22:58:57 +08:00
|
|
|
// we don't care about the producer
|
2019-12-24 01:19:08 +08:00
|
|
|
self.batchTestClientB.createJob(jobRequestA2, function (err, jobResultA2) {
|
2016-10-17 18:33:49 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobResultA1.getStatus(function (err, jobA1) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
jobResultA2.getStatus(function (err, jobA2) {
|
2016-10-17 18:33:49 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
jobResultB1.getStatus(function (err, jobB1) {
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(jobA1.status, JobStatus.DONE);
|
|
|
|
assert.strictEqual(jobA2.status, JobStatus.DONE);
|
|
|
|
assert.strictEqual(jobB1.status, JobStatus.DONE);
|
2016-10-17 18:33:49 +08:00
|
|
|
|
2016-10-19 22:58:57 +08:00
|
|
|
assert.ok(
|
|
|
|
new Date(jobA1.updated_at).getTime() < new Date(jobA2.updated_at).getTime(),
|
|
|
|
'A1 (' + jobA1.updated_at + ') ' +
|
|
|
|
'should finish before A2 (' + jobA2.updated_at + ')'
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
new Date(jobB1.updated_at).getTime() < new Date(jobA1.updated_at).getTime(),
|
|
|
|
'B1 (' + jobA1.updated_at + ') ' +
|
|
|
|
'should finish before A1 (' + jobA1.updated_at + ')'
|
|
|
|
);
|
|
|
|
|
|
|
|
function statusMapper (status) { return { status: status }; }
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
self.testClient.getResult('select * from ordered_inserts_a', function (err, rows) {
|
2016-10-17 18:33:49 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
// cartodb250user and vizzuality test users share database
|
2016-10-19 22:58:57 +08:00
|
|
|
var expectedRows = [1, 2, 3].map(statusMapper);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(rows, expectedRows);
|
2016-10-19 22:58:57 +08:00
|
|
|
|
|
|
|
var query = 'select * from ordered_inserts_bbbbb';
|
2019-12-24 01:19:08 +08:00
|
|
|
self.testClient.getResult(query, override, function (err, rows) {
|
2016-10-19 22:58:57 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
|
|
|
var expectedRows = [1].map(statusMapper);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(rows, expectedRows);
|
2016-10-19 22:58:57 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2016-10-17 18:33:49 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|