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 broken queue mock. #160

Merged
merged 2 commits into from
Jan 30, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-seals-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zemble/bull": patch
---

Fix broken queue mock.
20 changes: 10 additions & 10 deletions packages/bull/ZembleQueueMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown

private isPaused = false

private jobsRemaining = 0

constructor(
readonly worker: ZembleWorker,
_?: ZembleQueueConfig,
Expand Down Expand Up @@ -54,8 +56,6 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown
return this.#queueInternal
}

#jobsRemaining = 0

async addBulk(jobs: Parameters<ZembleQueueBull<DataType, ResultType>['addBulk']>[number]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
const js = jobs.map((job) => this.#createMockJob(job.name, job.data as any, job.opts))
Expand Down Expand Up @@ -94,14 +94,12 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown
return job
}

#executeWorker(job: Job<DataType, ResultType, string>) {
async #executeWorker(job: Job<DataType, ResultType, string>) {
try {
// eslint-disable-next-line no-plusplus
this.#jobsRemaining++
return this.#worker(job, { logger: zembleContext.logger })
return await this.#worker(job, { logger: zembleContext.logger })
} finally {
// eslint-disable-next-line no-plusplus
this.#jobsRemaining--
this.jobsRemaining--
this.#triggerWaitUntilFinishedIfNeeded()
}
}
Expand All @@ -113,8 +111,10 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown

const job = this.#createMockJob(name, data, opts)
this.jobs.push(job)
// eslint-disable-next-line no-plusplus
this.jobsRemaining++
setTimeout(async () => {
this.#executeWorker(job)
await this.#executeWorker(job)
}, 0)
return job
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown

#triggerWaitUntilFinishedIfNeeded() {
if (this.#waitUntilFinishedResolver) {
if (this.#jobsRemaining === 0) {
if (this.jobsRemaining === 0) {
this.#waitUntilFinishedResolver()
this.#waitUntilFinishedPromise = undefined
this.#waitUntilFinishedResolver = undefined
Expand All @@ -159,7 +159,7 @@ class ZembleQueueMock<DataType = unknown, ResultType extends PromiseLike<unknown

async waitUntilEmpty() {
if (!this.#waitUntilFinishedPromise) {
if (this.#jobsRemaining === 0) {
if (this.jobsRemaining === 0) {
return Promise.resolve()
}

Expand Down