fix destroy test
This commit is contained in:
@@ -187,24 +187,63 @@ describe('Worker class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not poll once destroyed', function () {
|
it('should not poll once destroyed', function () {
|
||||||
|
// remove the search spy
|
||||||
|
mockQueue.client.search.restore();
|
||||||
|
|
||||||
|
// mock the search, return 0 new jobs
|
||||||
|
const zeroHits = { hits: { hits: [] } };
|
||||||
|
const searchStub = sinon.stub(mockQueue.client, 'search', () => Promise.resolve(zeroHits));
|
||||||
|
|
||||||
const worker = new Worker(mockQueue, 'test', noop);
|
const worker = new Worker(mockQueue, 'test', noop);
|
||||||
|
|
||||||
|
function waitForSearch() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
worker.once(constants.EVENT_WORKER_JOB_SEARCH_COMPLETE, () => {
|
||||||
|
resolve()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function waitForPoller() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
worker.once(constants.EVENT_WORKER_JOB_POLLING_READY, () => {
|
||||||
|
resolve()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// move the clock a couple times, test for searches each time
|
// move the clock a couple times, test for searches each time
|
||||||
sinon.assert.notCalled(searchSpy);
|
sinon.assert.notCalled(searchStub);
|
||||||
clock.tick(defaults.interval);
|
|
||||||
sinon.assert.calledOnce(searchSpy);
|
|
||||||
clock.tick(defaults.interval);
|
|
||||||
sinon.assert.calledTwice(searchSpy);
|
|
||||||
|
|
||||||
// destroy the worker, move the clock, make sure another search doesn't happen
|
const firstWait = waitForSearch();
|
||||||
worker.destroy();
|
|
||||||
clock.tick(defaults.interval);
|
clock.tick(defaults.interval);
|
||||||
sinon.assert.calledTwice(searchSpy);
|
|
||||||
|
|
||||||
// manually call job poller, move the clock, make sure another search doesn't happen
|
return firstWait
|
||||||
worker._startJobPolling();
|
.then(() => {
|
||||||
clock.tick(defaults.interval);
|
sinon.assert.calledOnce(searchStub);
|
||||||
sinon.assert.calledTwice(searchSpy);
|
return waitForPoller();
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
const secondWait = waitForSearch();
|
||||||
|
clock.tick(defaults.interval);
|
||||||
|
return secondWait;
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
sinon.assert.calledTwice(searchStub);
|
||||||
|
return waitForPoller();
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
// destroy the worker, move the clock, make sure another search doesn't happen
|
||||||
|
worker.destroy();
|
||||||
|
|
||||||
|
clock.tick(defaults.interval);
|
||||||
|
sinon.assert.calledTwice(searchStub);
|
||||||
|
|
||||||
|
// manually call job poller, move the clock, make sure another search doesn't happen
|
||||||
|
worker._startJobPolling();
|
||||||
|
clock.tick(defaults.interval);
|
||||||
|
sinon.assert.calledTwice(searchStub);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user