4 Commits

Author SHA1 Message Date
ef61a33a38 0.3.1 2016-05-13 14:40:46 -07:00
dae14e0edc add tests for job search failures 2016-05-13 14:36:19 -07:00
c51ea64bdd [worker] swollow missing index errors 2016-05-13 14:35:39 -07:00
5d37399fbf run travis on version tags too 2016-05-12 17:27:53 -07:00
4 changed files with 69 additions and 25 deletions

View File

@@ -16,3 +16,4 @@ after_success: npm run coverage
branches:
only:
- master
- /^v[0-9].*$/

View File

@@ -1,6 +1,6 @@
{
"name": "esqueue",
"version": "0.3.0",
"version": "0.3.1",
"description": "Job queue, powered by Elasticsearch",
"main": "lib/index.js",
"scripts": {

View File

@@ -267,6 +267,9 @@ export default class Job extends events.EventEmitter {
return jobs;
})
.catch((err) => {
// ignore missing indices errors
if (err.status === 404) return;
this.debug('job querying failed', err);
this.emit('error', err);
this.queue.emit('worker_error', {

View File

@@ -105,15 +105,9 @@ describe('Worker class', function () {
});
});
describe('searching for jobs', function () {
describe('polling 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(() => {
anchorMoment = moment(anchor);
clock = sinon.useFakeTimers(anchorMoment.valueOf());
@@ -124,37 +118,73 @@ describe('Worker class', function () {
clock.restore();
});
describe('polling interval', function () {
it('should start polling for jobs after interval', function () {
new Worker(mockQueue, 'test', noop);
sinon.assert.notCalled(searchSpy);
clock.tick(defaults.interval);
sinon.assert.calledOnce(searchSpy);
it('should start polling for jobs after interval', function () {
new Worker(mockQueue, 'test', noop);
sinon.assert.notCalled(searchSpy);
clock.tick(defaults.interval);
sinon.assert.calledOnce(searchSpy);
});
it('should use interval option to control polling', function () {
const interval = 567;
new Worker(mockQueue, 'test', noop, { interval });
sinon.assert.notCalled(searchSpy);
clock.tick(interval);
sinon.assert.calledOnce(searchSpy);
});
});
describe('query for pending jobs', function () {
let worker;
let searchStub;
function getSearchParams(jobtype = 'test', params = {}) {
worker = new Worker(mockQueue, jobtype, noop, params);
worker._getPendingJobs();
return searchStub.firstCall.args[0];
}
describe('error handling', function () {
beforeEach(() => {
});
it('should use interval option to control polling', function () {
const interval = 567;
new Worker(mockQueue, 'test', noop, { interval });
sinon.assert.notCalled(searchSpy);
clock.tick(interval);
sinon.assert.calledOnce(searchSpy);
it('should pass search errors', function (done) {
searchStub = sinon.stub(mockQueue.client, 'search', () => Promise.reject());
worker = new Worker(mockQueue, 'test', noop);
worker._getPendingJobs()
.then(() => done(new Error('should not resolve')))
.catch(() => { done(); });
});
it('should swollow index missing errors', function (done) {
searchStub = sinon.stub(mockQueue.client, 'search', () => Promise.reject({
status: 404
}));
worker = new Worker(mockQueue, 'test', noop);
worker._getPendingJobs()
.then(() => { done(); })
.catch(() => done(new Error('should not reject')));
});
});
describe('query parameters', function () {
beforeEach(() => {
searchStub = sinon.stub(mockQueue.client, 'search', () => Promise.resolve());
});
it('should query with version', function () {
const params = getSearchParams('test');
const params = getSearchParams();
expect(params).to.have.property('version', true);
});
it('should query by default doctype', function () {
const params = getSearchParams('test');
const params = getSearchParams();
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 });
const params = getSearchParams('type', { doctype });
expect(params).to.have.property('type', doctype);
});
});
@@ -163,6 +193,16 @@ describe('Worker class', function () {
const conditionPath = 'query.constant_score.filter.bool';
const jobtype = 'test_jobtype';
beforeEach(() => {
searchStub = sinon.stub(mockQueue.client, 'search', () => Promise.resolve());
anchorMoment = moment(anchor);
clock = sinon.useFakeTimers(anchorMoment.valueOf());
});
afterEach(() => {
clock.restore();
});
it('should search by job type', function () {
const { body } = getSearchParams(jobtype);
const conditions = get(body, conditionPath);
@@ -201,9 +241,9 @@ describe('Worker class', function () {
expect(body).to.have.property('size', size);
});
});
});
describe('claiming a job', function () {
let params;
let job;