add tests for worker timeout handling

This commit is contained in:
2016-06-28 18:03:06 -07:00
parent 460d83411e
commit 257645f11c

View File

@@ -567,36 +567,73 @@ describe('Worker class', function () {
}); });
}); });
describe('job timeouts', function () { describe('job timing', function () {
let job; function getFailStub(worker) {
let failStub; return sinon.stub(worker, '_failJob').returns(Promise.resolve());
let worker; }
const timeout = 20;
const timeoutPadding = 10;
beforeEach(function () { it('should timeout if not complete within allotted time', function (done) {
const timeout = 20;
const workerFn = function () { const workerFn = function () {
return new Promise(function (resolve) { return new Promise(function (resolve) {
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, timeout + timeoutPadding); }, timeout * 2);
}); });
}; };
worker = new Worker(mockQueue, 'test', workerFn); const worker = new Worker(mockQueue, 'test', workerFn);
job = { const failStub = getFailStub(worker);
_id: 'testJob1',
const job = {
_id: 'testTimeoutJob',
_source: { _source: {
timeout: timeout, timeout: timeout,
payload: 'test' payload: 'test'
} }
}; };
failStub = sinon.stub(worker, '_failJob').returns(Promise.resolve());
let performJobPromise;
// check for timeout event
worker.once('job_timeout', (err) => {
try {
expect(err).to.have.property('type', 'WorkerTimeoutError');
performJobPromise.then(() => {
sinon.assert.notCalled(failStub);
done();
}).catch(done);
} catch (e) {
done(e);
}
}); });
it('should fail if not complete within allotted time', function () { // fire of the job worker
performJobPromise = worker._performJob(job);
});
it('should fail if worker fails', function () {
const timeout = 20;
const workerFn = function () {
return new Promise(function (resolve, reject) {
setTimeout(() => {
reject();
}, timeout / 2);
});
};
const worker = new Worker(mockQueue, 'test', workerFn);
const failStub = getFailStub(worker);
const job = {
_id: 'testTimeoutJob',
_source: {
timeout: timeout,
payload: 'test'
}
};
return worker._performJob(job) return worker._performJob(job)
.then(() => { .then(() => {
sinon.assert.notCalled(failStub); sinon.assert.calledOnce(failStub);
}); });
}); });
}); });