Skip to content

Commit

Permalink
added more jobs and mocks to the zembleBull (#135)
Browse files Browse the repository at this point in the history
Two new jobs in ZembleBullQeueue and more mock functions ti
ZembleQueueMock
- remove
- getJob
- pause queue
- resume queue
  • Loading branch information
RevanToma authored Oct 2, 2024
1 parent 04d9f88 commit 498d500
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-teachers-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zemble/bull": minor
---

Two new jobs in ZembleBullQueue resume and pause. Four new mock functions in ZembleQueueMock, remove, getJob, resume and pause
8 changes: 8 additions & 0 deletions packages/bull/ZembleQueueBull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,13 @@ export class ZembleQueueBull<DataType = unknown, ReturnType = unknown> {
async getDelayed(...args: Parameters<Queue<DataType, ReturnType>['getDelayed']>) {
return this.#queue.getDelayed(...args)
}

async pause() {
return this.#queue.pause()
}

async resume() {
return this.#queue.resume()
}
}
export default ZembleQueueBull
41 changes: 41 additions & 0 deletions packages/bull/ZembleQueueMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable functional/prefer-readonly-type */

import zembleContext from '@zemble/core/zembleContext'
import { jest } from 'bun:test'

import type { ZembleQueueBull, ZembleQueueConfig, ZembleWorker } from './ZembleQueueBull'
import type {
Expand All @@ -11,15 +12,31 @@ import type {
interface IZembleQueue<DataType = unknown, ReturnType = unknown> {
readonly add: ZembleQueueBull<DataType, ReturnType>['add']
readonly addBulk: ZembleQueueBull<DataType, ReturnType>['addBulk']
readonly remove: ZembleQueueBull<DataType, ReturnType>['remove']
readonly getJob: ZembleQueueBull<DataType, ReturnType>['getJob']
readonly resume: ZembleQueueBull<DataType, ReturnType>['resume']
readonly pause: ZembleQueueBull<DataType, ReturnType>['pause']
}

class ZembleQueueMock<DataType = unknown, ReturnType = unknown> implements IZembleQueue<DataType, ReturnType> {
private jobs: Array<Job<DataType, ReturnType, string>> = []

private isPaused = false

constructor(
readonly worker: ZembleWorker,
_?: ZembleQueueConfig,
) {
this.#worker = worker
// this.#config = config

// wrap all functions with jest.fn to be able to spy on them
this.add = jest.fn(this.add.bind(this))
this.addBulk = jest.fn(this.addBulk.bind(this))
this.remove = jest.fn(this.remove.bind(this))
this.getJob = jest.fn(this.getJob.bind(this))
this.pause = jest.fn(this.pause.bind(this))
this.resume = jest.fn(this.resume.bind(this))
}

readonly #worker: ZembleWorker
Expand Down Expand Up @@ -72,10 +89,34 @@ class ZembleQueueMock<DataType = unknown, ReturnType = unknown> implements IZemb
}

async add(name: string, data: DataType, opts?: JobsOptions | undefined): Promise<Job<DataType, ReturnType, string>> {
if (this.isPaused) {
throw new Error('Queue is paused')
}

const job = this.#createMockJob(name, data, opts)
this.jobs.push(job)
setTimeout(async () => this.#worker(job, { logger: zembleContext.logger }), 0)
return job
}

async remove(jobId: string) {
const initialLength = this.jobs.length
this.jobs = this.jobs.filter((job) => job.id !== jobId)
const removedCount = initialLength - this.jobs.length
return removedCount
}

async getJob(jobId: string): Promise<Job<DataType, ReturnType, string> | undefined> {
return this.jobs.find((job) => job.id === jobId)
}

async pause() {
this.isPaused = true
}

async resume(): Promise<void> {
this.isPaused = false
}
}

export default ZembleQueueMock

0 comments on commit 498d500

Please sign in to comment.