-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds more tests! Refactor rw-jobs-worker (#11335)
Closes #11336
- Loading branch information
Showing
5 changed files
with
180 additions
and
37 deletions.
There are no files selected for viewing
113 changes: 108 additions & 5 deletions
113
packages/jobs/src/bins/__tests__/rw-jobs-worker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,111 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
describe('worker', () => { | ||
// TODO: Consider adding tests | ||
it('placeholder', () => { | ||
expect(true).toBeTruthy() | ||
import { MockAdapter, mockLogger } from '../../core/__tests__/mocks.js' | ||
import { JobManager } from '../../core/JobManager.js' | ||
import { Worker } from '../../core/Worker.js' | ||
import { getWorker, processName } from '../rw-jobs-worker.js' | ||
|
||
vi.mock('@redwoodjs/cli-helpers/loadEnvFiles', () => { | ||
return { | ||
loadEnvFiles: () => {}, | ||
} | ||
}) | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
loadJobsManager: vi.fn(), | ||
} | ||
}) | ||
|
||
vi.mock('../../loaders.js', () => { | ||
return { | ||
loadJobsManager: mocks.loadJobsManager, | ||
} | ||
}) | ||
|
||
describe('processName', () => { | ||
it('sets the process title for a single queue', () => { | ||
const title = processName({ id: 1, queues: 'default' }) | ||
|
||
expect(title).toEqual('rw-jobs-worker.default.1') | ||
}) | ||
|
||
it('sets the process title for an array of queues', () => { | ||
const title = processName({ id: 1, queues: ['default', 'email'] }) | ||
|
||
expect(title).toEqual('rw-jobs-worker.default-email.1') | ||
}) | ||
}) | ||
|
||
describe('getWorker', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
it('returns an instance of Worker', async () => { | ||
mocks.loadJobsManager.mockImplementation( | ||
() => | ||
new JobManager({ | ||
adapters: { | ||
test: new MockAdapter(), | ||
}, | ||
logger: mockLogger, | ||
queues: ['default'], | ||
workers: [ | ||
{ | ||
adapter: 'test', | ||
logger: mockLogger, | ||
queue: '*', | ||
count: 1, | ||
}, | ||
], | ||
}), | ||
) | ||
|
||
const worker = await getWorker({ | ||
index: 0, | ||
id: 0, | ||
workoff: false, | ||
clear: false, | ||
}) | ||
|
||
expect(worker).toBeInstanceOf(Worker) | ||
}) | ||
|
||
it('calls getWorker on the manager with the proper values', async () => { | ||
const mockAdapter = new MockAdapter() | ||
mocks.loadJobsManager.mockImplementation( | ||
() => | ||
new JobManager({ | ||
adapters: { | ||
test: mockAdapter, | ||
}, | ||
logger: mockLogger, | ||
queues: ['default'], | ||
workers: [ | ||
{ | ||
adapter: 'test', | ||
logger: mockLogger, | ||
queue: '*', | ||
count: 1, | ||
}, | ||
], | ||
}), | ||
) | ||
const spy = vi.spyOn(JobManager.prototype, 'createWorker') | ||
|
||
await getWorker({ | ||
index: 0, | ||
id: 0, | ||
workoff: false, | ||
clear: false, | ||
}) | ||
|
||
expect(spy).toHaveBeenCalledWith({ | ||
index: 0, | ||
workoff: false, | ||
clear: false, | ||
processName: 'rw-jobs-worker.*.0', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters