Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: whenCurrentJobsFinished should wait for all jobs #1586

Merged
merged 1 commit into from Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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