release: WRNexusJS 0.4.0
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { test, expect } from "bun:test";
|
||||
import { createQueue, cronToInterval, defineWorkflow } from "../src/index.ts";
|
||||
import {
|
||||
createDurableQueue,
|
||||
createQueue,
|
||||
cronToInterval,
|
||||
defineWorkflow,
|
||||
memoryQueueStore,
|
||||
} from "../src/index.ts";
|
||||
|
||||
test("processes a job", async () => {
|
||||
const queue = createQueue();
|
||||
@@ -138,3 +144,49 @@ test("runs typed workflows and parses supported cron expressions", async () => {
|
||||
expect(cronToInterval("@hourly")).toBe(3_600_000);
|
||||
expect(cronToInterval("*/5 * * * *")).toBe(300_000);
|
||||
});
|
||||
|
||||
test("durable queue persists retries and dead-letters exhausted jobs", async () => {
|
||||
let clock = 0;
|
||||
const store = memoryQueueStore();
|
||||
const dead: string[] = [];
|
||||
const queue = createDurableQueue({
|
||||
store,
|
||||
maxAttempts: 2,
|
||||
now: () => clock,
|
||||
backoff: () => 100,
|
||||
onDeadLetter: (job) => void dead.push(job.id),
|
||||
});
|
||||
let calls = 0;
|
||||
queue.process("flaky", () => {
|
||||
calls++;
|
||||
throw new Error("boom");
|
||||
});
|
||||
|
||||
const job = await queue.add("flaky", { id: 1 });
|
||||
expect(await queue.drain()).toBe(1);
|
||||
expect((await store.get(job.id))?.attempts).toBe(1);
|
||||
clock = 100;
|
||||
expect(await queue.drain()).toBe(1);
|
||||
expect(await store.get(job.id)).toBeNull();
|
||||
expect(calls).toBe(2);
|
||||
expect(dead).toEqual([job.id]);
|
||||
expect(queue.failed()).toHaveLength(1);
|
||||
|
||||
expect(await queue.retry(job.id)).toBe(true);
|
||||
expect((await store.get(job.id))?.attempts).toBe(0);
|
||||
});
|
||||
|
||||
test("durable queue does not remove a job before its handler succeeds", async () => {
|
||||
const store = memoryQueueStore();
|
||||
const queue = createDurableQueue({ store });
|
||||
let visibleDuringHandler = false;
|
||||
queue.process("work", async (job) => {
|
||||
visibleDuringHandler = (await store.get(job.id)) !== null;
|
||||
});
|
||||
const job = await queue.add("work", {});
|
||||
|
||||
await queue.drain();
|
||||
|
||||
expect(visibleDuringHandler).toBe(true);
|
||||
expect(await store.get(job.id)).toBeNull();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user