Skip to content

Commit

Permalink
Merge pull request #1586 from gabegorelick/wait-multiple-jobs
Browse files Browse the repository at this point in the history
fix: whenCurrentJobsFinished should wait for all jobs
  • Loading branch information
manast committed Jan 22, 2020
2 parents 081b37f + d3c1527 commit f3f2816
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/queue.js
Expand Up @@ -1191,7 +1191,7 @@ Queue.prototype.whenCurrentJobsFinished = function() {
return this.bclient.connect();
});

return Promise.all([this.processing[0]]).then(() => {
return Promise.all(this.processing).then(() => {
return forcedReconnection;
});
};
Expand Down
49 changes: 49 additions & 0 deletions test/test_when_current_jobs_finished.js
Expand Up @@ -63,6 +63,55 @@ describe('.whenCurrentJobsFinished', () => {
);
});

it('should wait for all jobs to complete', async () => {
const queue = await utils.newQueue();

// add multiple jobs to queue
await queue.add({});
await queue.add({});

let finishJob1;
let finishJob2;

// wait for all jobs to be active
await new Promise(resolve => {
let callCount = 0;
queue.process(2, () => {
callCount++;
if (callCount === 1) {
return new Promise(resolve => {
finishJob1 = resolve;
});
}

resolve();
return new Promise(resolve => {
finishJob2 = resolve;
});
});
});

let isFulfilled = false;
const finished = queue.whenCurrentJobsFinished().then(() => {
isFulfilled = true;
});

finishJob2();
await delay(100);

expect(isFulfilled).to.equal(
false,
'should not fulfill until all jobs are finished'
);

finishJob1();
await delay(100);
expect(await finished).to.equal(
undefined,
'whenCurrentJobsFinished should resolve once all jobs are finished'
);
});

it('should wait for job to fail', async () => {
const queue = await utils.newQueue();
await queue.add({});
Expand Down

0 comments on commit f3f2816

Please sign in to comment.