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

added more jobs and mocks to the zembleBull #135

Merged
merged 3 commits into from
Oct 2, 2024
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/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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to use jest.fn()/mock() to be able to "spy" on it (i.e. to see if it was called, with which arguments etc)?

Actually it might be nice to wrap all the functions with mock() so it's always possible.. :) As you see here it's possible to both provide an implementation and wrap it in mock() (or jest.fn())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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