proper async promise tests, add tests for worker timeouts

also fix intermittent issue with complete_at time check
This commit is contained in:
2016-05-03 11:37:22 -07:00
parent f722bbf5e1
commit 9ef3fb8cec

View File

@@ -323,16 +323,15 @@ describe('Worker class', function () {
.then(() => done());
});
it('should update the job with the workerFn output', function (done) {
it('should update the job with the workerFn output', function () {
const workerFn = function (jobPayload, cb) {
expect(jobPayload).to.eql(payload);
cb(null, payload);
};
const worker = new Worker(mockQueue, 'test', workerFn);
worker._performJob(job)
return worker._performJob(job)
.then(() => {
try {
sinon.assert.calledOnce(updateSpy);
const query = updateSpy.firstCall.args[0];
expect(query).to.have.property('index', job._index);
@@ -342,77 +341,73 @@ describe('Worker class', function () {
expect(query.body.doc).to.have.property('output');
expect(query.body.doc.output).to.have.property('content_type', false);
expect(query.body.doc.output).to.have.property('content', payload);
done();
} catch (err) {
done(err);
}
});
});
it('should update the job status and completed time', function (done) {
it('should update the job status and completed time', function () {
const startTime = moment().valueOf();
const workerFn = function (jobPayload, cb) {
expect(jobPayload).to.eql(payload);
cb(null, payload);
setTimeout(() => cb(null, payload), 10);
};
const worker = new Worker(mockQueue, 'test', workerFn);
worker._performJob(job)
.then(() => {
try {
sinon.assert.calledOnce(updateSpy);
const doc = updateSpy.firstCall.args[0].body.doc;
expect(doc).to.have.property('status', JOB_STATUS_COMPLETED);
expect(doc).to.have.property('completed_at');
const completedTimestamp = moment(doc.completed_at).valueOf();
expect(completedTimestamp).to.be.greaterThan(startTime);
done();
} catch (err) {
done(err);
}
});
});
it('should reject on job errors', function (done) {
const workerFn = function (jobPayload, cb) {
cb(new Error('test error'));
};
const worker = new Worker(mockQueue, 'test', workerFn);
worker._performJob(job)
.then(() => done(new Error('should not resolve')))
.catch((e) => {
expect(e.message).to.equal('test error');
done();
});
});
it('should append error output to job', function (done) {
it('should append error output to job', function () {
const workerFn = function (jobPayload, cb) {
cb(new Error('test error'));
};
const worker = new Worker(mockQueue, 'test', workerFn);
const failStub = sinon.stub(worker, '_failJob');
worker._performJob(job)
.then(() => done(new Error('should not resolve')))
.catch(() => {
try {
return worker._performJob(job)
.then(() => {
sinon.assert.calledOnce(failStub);
sinon.assert.calledWith(failStub, job, 'Error: test error');
done();
} catch (e) {
done(e);
});
});
});
describe('job timeouts', function () {
let job;
let failStub;
let worker;
const timeout = 20;
const timeoutPadding = 10;
beforeEach(function () {
const workerFn = function (jobPayload, cb) {
setTimeout(() => {
cb();
}, timeout + timeoutPadding);
};
worker = new Worker(mockQueue, 'test', workerFn);
job = {
_id: 'testJob1',
_source: {
timeout: timeout,
payload: 'test'
}
});
});
};
failStub = sinon.stub(worker, '_failJob').returns(Promise.resolve());
});
// describe('job timeouts', function () {
// let job;
// let payload;
// let updateSpy;
// });
it('should fail if not complete within allotted time', function () {
return worker._performJob(job)
.then(() => {
sinon.assert.notCalled(failStub);
});
});
});
});