Skip to content

Commit

Permalink
Fix broken queue mock. (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezula authored Jan 30, 2025
2 parents 4300521 + 9b71567 commit 5c6572f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
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

0 comments on commit 5c6572f

Please sign in to comment.