beef up the job document's body, update tests
This commit is contained in:
19
src/job.js
19
src/job.js
@@ -1,5 +1,7 @@
|
|||||||
import events from 'events';
|
import events from 'events';
|
||||||
import { isPlainObject } from 'lodash';
|
import { isPlainObject, omit } from 'lodash';
|
||||||
|
|
||||||
|
const PENDING = 0;
|
||||||
|
|
||||||
export default class Job extends events.EventEmitter {
|
export default class Job extends events.EventEmitter {
|
||||||
constructor(queue, type, payload, options = {}) {
|
constructor(queue, type, payload, options = {}) {
|
||||||
@@ -12,13 +14,22 @@ export default class Job extends events.EventEmitter {
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
this.timeout = options.timeout || 10000;
|
this.timeout = options.timeout || 10000;
|
||||||
|
this.options = omit(options, [ 'timeout' ]);
|
||||||
|
this.status = PENDING;
|
||||||
|
|
||||||
this.ready = this.queue.client.index({
|
this.ready = this.queue.client.index({
|
||||||
index: this.queue.index,
|
index: this.queue.index,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
body: Object.assign({}, options, {
|
body: {
|
||||||
payload: payload
|
payload: this.payload,
|
||||||
})
|
timeout: this.timeout,
|
||||||
|
options: this.options,
|
||||||
|
created: new Date(),
|
||||||
|
started: null,
|
||||||
|
completed: null,
|
||||||
|
attempts: 0,
|
||||||
|
status: PENDING,
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.then((doc) => {
|
.then((doc) => {
|
||||||
this.document = {
|
this.document = {
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import events from 'events';
|
import events from 'events';
|
||||||
import expect from 'expect.js';
|
import expect from 'expect.js';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
|
import _ from 'lodash';
|
||||||
import Job from '../../lib/job';
|
import Job from '../../lib/job';
|
||||||
import * as elasticsearchMock from '../fixtures/elasticsearch';
|
import * as elasticsearchMock from '../fixtures/elasticsearch';
|
||||||
|
|
||||||
describe('Jobs', function () {
|
describe('Job Class', function () {
|
||||||
let mockQueue;
|
let mockQueue;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockQueue = {
|
mockQueue = {
|
||||||
index: 'test',
|
index: 'test',
|
||||||
@@ -36,55 +38,69 @@ describe('Jobs', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('construction', function () {
|
describe('construction', function () {
|
||||||
|
const PENDING = 0;
|
||||||
let type;
|
let type;
|
||||||
let payload;
|
let payload;
|
||||||
let options;
|
let options;
|
||||||
|
|
||||||
|
function validateDoc(spy) {
|
||||||
|
expect(spy.callCount).to.be(1);
|
||||||
|
const spyCall = spy.getCall(0);
|
||||||
|
return spyCall.args[0];
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
type = 'type1';
|
type = 'type1';
|
||||||
payload = { id: '123' };
|
payload = { id: '123' };
|
||||||
options = { timeout: 1234 };
|
options = { timeout: 1234, test: 'options' };
|
||||||
sinon.spy(mockQueue.client, 'index');
|
sinon.spy(mockQueue.client, 'index');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should index the payload', function () {
|
it('should index the payload', function () {
|
||||||
new Job(mockQueue, type, payload);
|
new Job(mockQueue, type, payload);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
sinon.assert.calledOnce(mockQueue.client.index);
|
expect(newDoc).to.have.property('index', mockQueue.index);
|
||||||
sinon.assert.calledWith(mockQueue.client.index, {
|
expect(newDoc).to.have.property('type', type);
|
||||||
index: mockQueue.index,
|
expect(newDoc).to.have.property('body');
|
||||||
type: type,
|
expect(newDoc.body).to.have.property('payload', payload);
|
||||||
body: {
|
|
||||||
payload: payload
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should index any optional params', function () {
|
it('should index any optional params', function () {
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, options);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
sinon.assert.calledOnce(mockQueue.client.index);
|
expect(newDoc.body).to.have.property('options');
|
||||||
sinon.assert.calledWith(mockQueue.client.index, {
|
|
||||||
index: mockQueue.index,
|
|
||||||
type: type,
|
|
||||||
body: Object.assign(options, {
|
|
||||||
payload: payload
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not allow options to clobber payload', function () {
|
it('should index timeout value from options', function () {
|
||||||
options = { payload: 1234 };
|
|
||||||
new Job(mockQueue, type, payload, options);
|
new Job(mockQueue, type, payload, options);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
|
expect(newDoc.body).to.have.property('timeout', options.timeout);
|
||||||
|
});
|
||||||
|
|
||||||
sinon.assert.calledOnce(mockQueue.client.index);
|
it('should not use timeout as an option', function () {
|
||||||
sinon.assert.calledWith(mockQueue.client.index, {
|
new Job(mockQueue, type, payload, options);
|
||||||
index: mockQueue.index,
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
type: type,
|
expect(newDoc.body.options).to.eql(_.omit(options, [ 'timeout' ]));
|
||||||
body: {
|
});
|
||||||
payload: payload
|
|
||||||
}
|
it('should set event times', function () {
|
||||||
});
|
new Job(mockQueue, type, payload, options);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
|
expect(newDoc.body).to.have.property('created');
|
||||||
|
expect(newDoc.body).to.have.property('started');
|
||||||
|
expect(newDoc.body).to.have.property('completed');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set attempt count', function () {
|
||||||
|
new Job(mockQueue, type, payload, options);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
|
expect(newDoc.body).to.have.property('attempts');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set status as pending', function () {
|
||||||
|
new Job(mockQueue, type, payload, options);
|
||||||
|
const newDoc = validateDoc(mockQueue.client.index);
|
||||||
|
expect(newDoc.body).to.have.property('status', PENDING);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user