2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-10 21:38:38 +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-10 21:38:38 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('multiple batch clients job query order', function () {
|
|
|
|
before(function (done) {
|
2016-10-11 01:46:39 +08:00
|
|
|
this.batchTestClient1 = new BatchTestClient({ name: 'consumerA' });
|
|
|
|
this.batchTestClient2 = new BatchTestClient({ name: 'consumerB' });
|
2016-10-10 21:38:38 +08:00
|
|
|
|
|
|
|
this.testClient = new TestClient();
|
2016-10-11 01:46:53 +08:00
|
|
|
this.testClient.getResult(
|
|
|
|
'drop table if exists ordered_inserts; create table ordered_inserts (status numeric)',
|
|
|
|
done
|
|
|
|
);
|
2016-10-10 21:38:38 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function (done) {
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClient1.drain(function (err) {
|
2016-10-10 21:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
2016-10-11 01:46:53 +08:00
|
|
|
this.batchTestClient2.drain(done);
|
2016-10-10 21:38:38 +08:00
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function createJob (queries) {
|
2016-10-10 21:38:38 +08:00
|
|
|
return {
|
|
|
|
query: queries
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should run job queries in order (multiple consumers)', function (done) {
|
2016-10-11 01:47:11 +08:00
|
|
|
var jobRequest1 = createJob([
|
2019-12-24 01:19:08 +08:00
|
|
|
'insert into ordered_inserts values(1)',
|
|
|
|
'select pg_sleep(0.25)',
|
|
|
|
'insert into ordered_inserts values(2)'
|
2016-10-11 01:47:11 +08:00
|
|
|
]);
|
|
|
|
var jobRequest2 = createJob([
|
2019-12-24 01:19:08 +08:00
|
|
|
'insert into ordered_inserts values(3)'
|
2016-10-11 01:47:11 +08:00
|
|
|
]);
|
2016-10-10 21:38:38 +08:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClient1.createJob(jobRequest1, function (err, jobResult1) {
|
2016-10-10 21:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
this.batchTestClient2.createJob(jobRequest2, function (err, jobResult2) {
|
2016-10-10 21:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
jobResult1.getStatus(function (err, job1) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-24 01:19:08 +08:00
|
|
|
jobResult2.getStatus(function (err, job2) {
|
2016-10-10 21:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(job1.status, JobStatus.DONE);
|
|
|
|
assert.strictEqual(job2.status, JobStatus.DONE);
|
2016-10-10 21:38:38 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
self.testClient.getResult('select * from ordered_inserts', function (err, rows) {
|
2016-10-10 21:38:38 +08:00
|
|
|
assert.ok(!err);
|
|
|
|
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(rows, [{ status: 1 }, { status: 2 }, { status: 3 }]);
|
2016-10-10 21:38:38 +08:00
|
|
|
assert.ok(
|
|
|
|
new Date(job1.updated_at).getTime() < new Date(job2.updated_at).getTime(),
|
|
|
|
'job1 (' + job1.updated_at + ') should finish before job2 (' + job2.updated_at + ')'
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
});
|