add more tests around job query
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import expect from 'expect.js';
|
import expect from 'expect.js';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { noop, random } from 'lodash';
|
import { noop, random, get, find } from 'lodash';
|
||||||
import Worker from '../../lib/worker';
|
import Worker from '../../lib/worker';
|
||||||
import elasticsearchMock from '../fixtures/elasticsearch';
|
import elasticsearchMock from '../fixtures/elasticsearch';
|
||||||
import { JOB_STATUS_PROCESSING, JOB_STATUS_COMPLETED, JOB_STATUS_FAILED } from '../../lib/helpers/constants';
|
import constants from '../../lib/helpers/constants';
|
||||||
|
|
||||||
const anchor = '2016-04-02T01:02:03.456'; // saturday
|
const anchor = '2016-04-02T01:02:03.456'; // saturday
|
||||||
const defaults = {
|
const defaults = {
|
||||||
@@ -106,17 +106,26 @@ describe('Worker class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('searching for jobs', function () {
|
describe('searching for jobs', function () {
|
||||||
|
let searchSpy;
|
||||||
|
|
||||||
|
function getSearchParams(jobtype, params = {}) {
|
||||||
|
new Worker(mockQueue, jobtype, noop, params);
|
||||||
|
clock.tick(defaults.interval);
|
||||||
|
return searchSpy.firstCall.args[0];
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
anchorMoment = moment(anchor);
|
anchorMoment = moment(anchor);
|
||||||
clock = sinon.useFakeTimers(anchorMoment.valueOf());
|
clock = sinon.useFakeTimers(anchorMoment.valueOf());
|
||||||
|
searchSpy = sinon.spy(mockQueue.client, 'search');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
clock.restore();
|
clock.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('polling interval', function () {
|
||||||
it('should start polling for jobs after interval', function () {
|
it('should start polling for jobs after interval', function () {
|
||||||
const searchSpy = sinon.spy(mockQueue.client, 'search');
|
|
||||||
new Worker(mockQueue, 'test', noop);
|
new Worker(mockQueue, 'test', noop);
|
||||||
sinon.assert.notCalled(searchSpy);
|
sinon.assert.notCalled(searchSpy);
|
||||||
clock.tick(defaults.interval);
|
clock.tick(defaults.interval);
|
||||||
@@ -125,31 +134,76 @@ describe('Worker class', function () {
|
|||||||
|
|
||||||
it('should use interval option to control polling', function () {
|
it('should use interval option to control polling', function () {
|
||||||
const interval = 567;
|
const interval = 567;
|
||||||
const searchSpy = sinon.spy(mockQueue.client, 'search');
|
|
||||||
new Worker(mockQueue, 'test', noop, { interval });
|
new Worker(mockQueue, 'test', noop, { interval });
|
||||||
sinon.assert.notCalled(searchSpy);
|
sinon.assert.notCalled(searchSpy);
|
||||||
clock.tick(interval);
|
clock.tick(interval);
|
||||||
sinon.assert.calledOnce(searchSpy);
|
sinon.assert.calledOnce(searchSpy);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('query parameters', function () {
|
||||||
|
it('should query with version', function () {
|
||||||
|
const params = getSearchParams('test');
|
||||||
|
expect(params).to.have.property('version', true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should query by default doctype', function () {
|
||||||
|
const params = getSearchParams('test');
|
||||||
|
expect(params).to.have.property('type', constants.DEFAULT_SETTING_DOCTYPE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should query by custom doctype', function () {
|
||||||
|
const doctype = 'custom_test';
|
||||||
|
const params = getSearchParams('test', { doctype });
|
||||||
|
expect(params).to.have.property('type', doctype);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('query body', function () {
|
||||||
|
const conditionPath = 'query.constant_score.filter.bool';
|
||||||
|
const jobtype = 'test_jobtype';
|
||||||
|
|
||||||
|
it('should search by job type', function () {
|
||||||
|
const { body } = getSearchParams(jobtype);
|
||||||
|
const conditions = get(body, conditionPath);
|
||||||
|
expect(conditions).to.have.property('must');
|
||||||
|
expect(conditions.must).to.eql({ term: { jobtype: jobtype } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should search for pending or expired jobs', function () {
|
||||||
|
const { body } = getSearchParams(jobtype);
|
||||||
|
const conditions = get(body, conditionPath);
|
||||||
|
expect(conditions).to.have.property('should');
|
||||||
|
|
||||||
|
// this works because we are stopping the clock, so all times match
|
||||||
|
const nowTime = moment().toISOString();
|
||||||
|
const pending = { term: { status: 'pending'} };
|
||||||
|
const expired = { bool: { must: [
|
||||||
|
{ term: { status: 'processing' } },
|
||||||
|
{ range: { process_expiration: { lte: nowTime } } }
|
||||||
|
] } };
|
||||||
|
|
||||||
|
const pendingMatch = find(conditions.should, pending);
|
||||||
|
expect(pendingMatch).to.not.be(undefined);
|
||||||
|
|
||||||
|
const expiredMatch = find(conditions.should, expired);
|
||||||
|
expect(expiredMatch).to.not.be(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
it('should use default size', function () {
|
it('should use default size', function () {
|
||||||
const searchSpy = sinon.spy(mockQueue.client, 'search');
|
const { body } = getSearchParams(jobtype);
|
||||||
new Worker(mockQueue, 'test', noop);
|
|
||||||
clock.tick(defaults.interval);
|
|
||||||
const body = searchSpy.firstCall.args[0].body;
|
|
||||||
expect(body).to.have.property('size', defaults.size);
|
expect(body).to.have.property('size', defaults.size);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should observe the size option', function () {
|
it('should observe the size option', function () {
|
||||||
const size = 25;
|
const size = 25;
|
||||||
const searchSpy = sinon.spy(mockQueue.client, 'search');
|
const { body } = getSearchParams(jobtype, { size });
|
||||||
new Worker(mockQueue, 'test', noop, { size });
|
|
||||||
clock.tick(defaults.interval);
|
|
||||||
const body = searchSpy.firstCall.args[0].body;
|
|
||||||
expect(body).to.have.property('size', size);
|
expect(body).to.have.property('size', size);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('claiming a job', function () {
|
describe('claiming a job', function () {
|
||||||
let params;
|
let params;
|
||||||
let job;
|
let job;
|
||||||
@@ -193,7 +247,7 @@ describe('Worker class', function () {
|
|||||||
it('should update the job status', function () {
|
it('should update the job status', function () {
|
||||||
worker._claimJob(job);
|
worker._claimJob(job);
|
||||||
const doc = updateSpy.firstCall.args[0].body.doc;
|
const doc = updateSpy.firstCall.args[0].body.doc;
|
||||||
expect(doc).to.have.property('status', JOB_STATUS_PROCESSING);
|
expect(doc).to.have.property('status', constants.JOB_STATUS_PROCESSING);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set job expiration time', function () {
|
it('should set job expiration time', function () {
|
||||||
@@ -267,7 +321,7 @@ describe('Worker class', function () {
|
|||||||
it('should set status to failed', function () {
|
it('should set status to failed', function () {
|
||||||
worker._failJob(job);
|
worker._failJob(job);
|
||||||
const doc = updateSpy.firstCall.args[0].body.doc;
|
const doc = updateSpy.firstCall.args[0].body.doc;
|
||||||
expect(doc).to.have.property('status', JOB_STATUS_FAILED);
|
expect(doc).to.have.property('status', constants.JOB_STATUS_FAILED);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should append error message if supplied', function () {
|
it('should append error message if supplied', function () {
|
||||||
@@ -292,7 +346,7 @@ describe('Worker class', function () {
|
|||||||
worker._failJob(job, msg);
|
worker._failJob(job, msg);
|
||||||
const doc = updateSpy.firstCall.args[0].body.doc;
|
const doc = updateSpy.firstCall.args[0].body.doc;
|
||||||
expect(doc).to.have.property('output');
|
expect(doc).to.have.property('output');
|
||||||
expect(doc).to.have.property('status', JOB_STATUS_FAILED);
|
expect(doc).to.have.property('status', constants.JOB_STATUS_FAILED);
|
||||||
expect(doc).to.have.property('completed_at');
|
expect(doc).to.have.property('completed_at');
|
||||||
const completedTimestamp = moment(doc.completed_at).valueOf();
|
const completedTimestamp = moment(doc.completed_at).valueOf();
|
||||||
expect(completedTimestamp).to.be.greaterThan(startTime);
|
expect(completedTimestamp).to.be.greaterThan(startTime);
|
||||||
@@ -357,7 +411,7 @@ describe('Worker class', function () {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
sinon.assert.calledOnce(updateSpy);
|
sinon.assert.calledOnce(updateSpy);
|
||||||
const doc = updateSpy.firstCall.args[0].body.doc;
|
const doc = updateSpy.firstCall.args[0].body.doc;
|
||||||
expect(doc).to.have.property('status', JOB_STATUS_COMPLETED);
|
expect(doc).to.have.property('status', constants.JOB_STATUS_COMPLETED);
|
||||||
expect(doc).to.have.property('completed_at');
|
expect(doc).to.have.property('completed_at');
|
||||||
const completedTimestamp = moment(doc.completed_at).valueOf();
|
const completedTimestamp = moment(doc.completed_at).valueOf();
|
||||||
expect(completedTimestamp).to.be.greaterThan(startTime);
|
expect(completedTimestamp).to.be.greaterThan(startTime);
|
||||||
|
|||||||
Reference in New Issue
Block a user