update worker failure tests
check for timeouts, rejections and thrown errors explicitely, and make the tests a little more focused
This commit is contained in:
@@ -710,12 +710,17 @@ describe('Worker class', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('job timing', function () {
|
||||
describe('job failures', function () {
|
||||
function getFailStub(worker) {
|
||||
return sinon.stub(worker, '_failJob').returns(Promise.resolve());
|
||||
}
|
||||
|
||||
it('should timeout if not complete within allotted time', function (done) {
|
||||
describe('timeout', function () {
|
||||
let worker;
|
||||
let failStub;
|
||||
let job;
|
||||
|
||||
beforeEach(function () {
|
||||
const timeout = 20;
|
||||
const workerFn = function () {
|
||||
return new Promise(function (resolve) {
|
||||
@@ -724,9 +729,49 @@ describe('Worker class', function () {
|
||||
}, timeout * 2);
|
||||
});
|
||||
};
|
||||
const worker = new Worker(mockQueue, 'test', workerFn);
|
||||
const failStub = getFailStub(worker);
|
||||
worker = new Worker(mockQueue, 'test', workerFn);
|
||||
failStub = getFailStub(worker);
|
||||
|
||||
job = {
|
||||
_id: 'testTimeoutJob',
|
||||
_source: {
|
||||
timeout: timeout,
|
||||
payload: 'test'
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('should not fail job', function () {
|
||||
// fire of the job worker
|
||||
return worker._performJob(job)
|
||||
.then(() => {
|
||||
sinon.assert.notCalled(failStub);
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit timeout if not completed in time', function (done) {
|
||||
worker.once(constants.EVENT_WORKER_JOB_TIMEOUT, (err) => {
|
||||
try {
|
||||
expect(err).to.have.property('error');
|
||||
expect(err).to.have.property('job');
|
||||
expect(err).to.have.property('worker');
|
||||
expect(err.error).to.have.property('type', 'WorkerTimeoutError');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// fire of the job worker
|
||||
worker._performJob(job);
|
||||
});
|
||||
});
|
||||
|
||||
describe('worker failure', function () {
|
||||
let worker;
|
||||
let failStub;
|
||||
|
||||
const timeout = 20;
|
||||
const job = {
|
||||
_id: 'testTimeoutJob',
|
||||
_source: {
|
||||
@@ -735,30 +780,8 @@ describe('Worker class', function () {
|
||||
}
|
||||
};
|
||||
|
||||
let performJobPromise;
|
||||
|
||||
// check for timeout event
|
||||
worker.once(constants.EVENT_WORKER_JOB_TIMEOUT, (err) => {
|
||||
try {
|
||||
expect(err).to.have.property('error');
|
||||
expect(err).to.have.property('job');
|
||||
expect(err).to.have.property('worker');
|
||||
expect(err.error).to.have.property('type', 'WorkerTimeoutError');
|
||||
performJobPromise.then(() => {
|
||||
sinon.assert.notCalled(failStub);
|
||||
done();
|
||||
}).catch(done);
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// fire of the job worker
|
||||
performJobPromise = worker._performJob(job);
|
||||
});
|
||||
|
||||
it('should fail if worker fails', function () {
|
||||
const timeout = 20;
|
||||
describe('reject', function () {
|
||||
beforeEach(function () {
|
||||
const workerFn = function () {
|
||||
return new Promise(function (resolve, reject) {
|
||||
setTimeout(() => {
|
||||
@@ -766,22 +789,66 @@ describe('Worker class', function () {
|
||||
}, timeout / 2);
|
||||
});
|
||||
};
|
||||
const worker = new Worker(mockQueue, 'test', workerFn);
|
||||
const failStub = getFailStub(worker);
|
||||
|
||||
const job = {
|
||||
_id: 'testTimeoutJob',
|
||||
_source: {
|
||||
timeout: timeout,
|
||||
payload: 'test'
|
||||
}
|
||||
};
|
||||
worker = new Worker(mockQueue, 'test', workerFn);
|
||||
failStub = getFailStub(worker);
|
||||
});
|
||||
|
||||
it('should fail the job', function () {
|
||||
return worker._performJob(job)
|
||||
.then(() => {
|
||||
sinon.assert.calledOnce(failStub);
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit worker execution error', function (done) {
|
||||
worker.on(constants.EVENT_WORKER_JOB_EXECUTION_ERROR, (err) => {
|
||||
try {
|
||||
expect(err).to.have.property('error');
|
||||
expect(err).to.have.property('job');
|
||||
expect(err).to.have.property('worker');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// fire of the job worker
|
||||
worker._performJob(job);
|
||||
});
|
||||
});
|
||||
|
||||
describe('throw', function () {
|
||||
beforeEach(function () {
|
||||
const workerFn = function () {
|
||||
throw new Error('test throw');
|
||||
};
|
||||
worker = new Worker(mockQueue, 'test', workerFn);
|
||||
failStub = getFailStub(worker);
|
||||
});
|
||||
|
||||
it('should fail the job', function () {
|
||||
return worker._performJob(job)
|
||||
.then(() => {
|
||||
sinon.assert.calledOnce(failStub);
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit worker execution error', function (done) {
|
||||
worker.on(constants.EVENT_WORKER_JOB_EXECUTION_ERROR, (err) => {
|
||||
try {
|
||||
expect(err).to.have.property('error');
|
||||
expect(err).to.have.property('job');
|
||||
expect(err).to.have.property('worker');
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
|
||||
// fire of the job worker
|
||||
worker._performJob(job);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user