-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PrismaQueue): better job chaining, added unit tests
- Loading branch information
Showing
4 changed files
with
135 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { PrismaJob } from "src/PrismaJob"; | |
import { debug, serializeError, waitFor } from "src/utils"; | ||
import { | ||
createEmailQueue, | ||
DEFAULT_POLL_INTERVAL, | ||
prisma, | ||
waitForNextEvent, | ||
waitForNextJob, | ||
|
@@ -148,6 +149,40 @@ describe("PrismaQueue", () => { | |
expect(record?.finishedAt).toBeNull(); | ||
expect(record?.error).toEqual(serializeError(error)); | ||
}); | ||
it("should properly dequeue multiple jobs in a row", async () => { | ||
const JOB_WAIT = 50; | ||
queue.worker = vi.fn(async (_job) => { | ||
await waitFor(JOB_WAIT); | ||
return { code: "200" }; | ||
}); | ||
await Promise.all([ | ||
queue.enqueue({ email: "[email protected]" }), | ||
queue.enqueue({ email: "[email protected]" }), | ||
]); | ||
await waitFor(DEFAULT_POLL_INTERVAL + JOB_WAIT * 2 + 100); | ||
expect(queue.worker).toHaveBeenCalledTimes(2); | ||
expect(queue.worker).toHaveBeenNthCalledWith(2, expect.any(PrismaJob), expect.any(PrismaClient)); | ||
}); | ||
it("should properly handle multiple restarts", async () => { | ||
const JOB_WAIT = 50; | ||
await queue.stop(); | ||
queue.worker = vi.fn(async (_job) => { | ||
await waitFor(JOB_WAIT); | ||
return { code: "200" }; | ||
}); | ||
await Promise.all([ | ||
queue.enqueue({ email: "[email protected]" }), | ||
queue.enqueue({ email: "[email protected]" }), | ||
]); | ||
queue.start(); | ||
expect(queue.worker).toHaveBeenCalledTimes(0); | ||
await queue.stop(); | ||
queue.start(); | ||
await waitFor(10); | ||
expect(queue.worker).toHaveBeenCalledTimes(1); | ||
await waitFor(JOB_WAIT + 10); | ||
expect(queue.worker).toHaveBeenCalledTimes(1); | ||
}); | ||
afterAll(() => { | ||
queue.stop(); | ||
}); | ||
|
@@ -246,6 +281,37 @@ describe("PrismaQueue", () => { | |
}); | ||
}); | ||
|
||
describe("maxConcurrency", () => { | ||
let queue: PrismaQueue<JobPayload, JobResult>; | ||
beforeAll(async () => { | ||
queue = createEmailQueue({ maxConcurrency: 2 }); | ||
}); | ||
beforeEach(async () => { | ||
await prisma.queueJob.deleteMany(); | ||
queue.start(); | ||
}); | ||
afterEach(async () => { | ||
queue.stop(); | ||
}); | ||
it("should properly dequeue multiple jobs in a row according to maxConcurrency", async () => { | ||
const JOB_WAIT = 100; | ||
queue.worker = vi.fn(async (_job) => { | ||
await waitFor(JOB_WAIT); | ||
return { code: "200" }; | ||
}); | ||
await Promise.all([ | ||
queue.enqueue({ email: "[email protected]" }), | ||
queue.enqueue({ email: "[email protected]" }), | ||
]); | ||
await waitFor(DEFAULT_POLL_INTERVAL + 100); | ||
expect(queue.worker).toHaveBeenCalledTimes(2); | ||
expect(queue.worker).toHaveBeenNthCalledWith(2, expect.any(PrismaJob), expect.any(PrismaClient)); | ||
}); | ||
afterAll(() => { | ||
queue.stop(); | ||
}); | ||
}); | ||
|
||
describe("priority", () => { | ||
let queue: PrismaQueue<JobPayload, JobResult>; | ||
beforeAll(async () => { | ||
|
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