|
| 1 | +import { setupIntegrationTest } from "@backend/testing/setup/test-utils"; |
| 2 | +import { sql } from "kysely"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +import { defineQueue, enqueue, startQueues, stopQueues, type Task } from "./queue"; |
| 6 | + |
| 7 | +// Needs the test Postgres and Valkey (`pnpm test:setup`). The `.integration.test.ts` name is what |
| 8 | +// keeps it out of runs that have no services, like a fresh checkout or the registry e2e lanes. |
| 9 | +const SCHEMA = "pgboss_spec"; |
| 10 | +const payload = z.object({ value: z.string() }); |
| 11 | +type Payload = z.infer<typeof payload>; |
| 12 | + |
| 13 | +const until = (check: () => boolean, timeoutMs = 20_000): Promise<void> => |
| 14 | + vi.waitFor( |
| 15 | + () => { |
| 16 | + if (!check()) throw new Error("Timed out waiting for the queue"); |
| 17 | + }, |
| 18 | + { timeout: timeoutMs, interval: 100 }, |
| 19 | + ); |
| 20 | + |
| 21 | +describe("task queue", () => { |
| 22 | + const env = setupIntegrationTest(); |
| 23 | + |
| 24 | + const seen: Task<Payload>[] = []; |
| 25 | + const exhausted: Task<Payload>[] = []; |
| 26 | + let running = 0; |
| 27 | + let peak = 0; |
| 28 | + |
| 29 | + const plain = defineQueue({ |
| 30 | + name: "spec-plain", |
| 31 | + schema: payload, |
| 32 | + run: async (task) => { |
| 33 | + seen.push(task); |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + const failing = defineQueue({ |
| 38 | + name: "spec-failing", |
| 39 | + schema: payload, |
| 40 | + retryLimit: 2, |
| 41 | + retryDelay: 0, |
| 42 | + retryBackoff: false, |
| 43 | + run: async (task) => { |
| 44 | + seen.push(task); |
| 45 | + throw new Error("always fails"); |
| 46 | + }, |
| 47 | + onExhausted: async (task) => { |
| 48 | + exhausted.push(task); |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const capped = defineQueue({ |
| 53 | + name: "spec-capped", |
| 54 | + schema: payload, |
| 55 | + concurrency: 2, |
| 56 | + run: async (task) => { |
| 57 | + running += 1; |
| 58 | + peak = Math.max(peak, running); |
| 59 | + await new Promise((resolve) => setTimeout(resolve, 300)); |
| 60 | + running -= 1; |
| 61 | + seen.push(task); |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const exclusive = defineQueue({ name: "spec-exclusive", schema: payload, exclusive: true, run: async () => undefined }); |
| 66 | + |
| 67 | + const unregistered = defineQueue({ name: "spec-unregistered", schema: payload, run: async () => undefined }); |
| 68 | + |
| 69 | + // The shared test environment already started the app's own (empty) registry, and startQueues |
| 70 | + // is a no-op while an instance is up, so swap it for this spec's queues in a throwaway schema. |
| 71 | + const start = async (workers: boolean) => { |
| 72 | + await stopQueues(); |
| 73 | + await startQueues({ |
| 74 | + queues: [plain, failing, capped, exclusive], |
| 75 | + postgres: env.postgres, |
| 76 | + workers, |
| 77 | + pollingIntervalSeconds: 0.5, |
| 78 | + boss: { schema: SCHEMA }, |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + seen.length = 0; |
| 84 | + exhausted.length = 0; |
| 85 | + running = 0; |
| 86 | + peak = 0; |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(async () => { |
| 90 | + await stopQueues(); |
| 91 | + await sql`drop schema if exists ${sql.id(SCHEMA)} cascade`.execute(env.db); |
| 92 | + }); |
| 93 | + |
| 94 | + it("costs nothing until a queue is registered: no schema, and a clear error on enqueue", async () => { |
| 95 | + await stopQueues(); |
| 96 | + |
| 97 | + await startQueues({ queues: [], postgres: env.postgres, boss: { schema: SCHEMA } }); |
| 98 | + |
| 99 | + const schemas = await sql<{ count: string }>` |
| 100 | + select count(*) as count from information_schema.schemata where schema_name = ${SCHEMA} |
| 101 | + `.execute(env.db); |
| 102 | + expect(Number(schemas.rows[0]?.count)).toBe(0); |
| 103 | + await expect(enqueue(plain, { value: "nowhere to go" })).rejects.toThrow(/task queue is not running/); |
| 104 | + }); |
| 105 | + |
| 106 | + it("runs an enqueued task with its payload and attempt number", async () => { |
| 107 | + await start(true); |
| 108 | + await enqueue(plain, { value: "hello" }); |
| 109 | + |
| 110 | + await until(() => seen.length === 1); |
| 111 | + expect(seen[0]?.data).toEqual({ value: "hello" }); |
| 112 | + expect(seen[0]?.attempt).toBe(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it("drops the task when the enqueueing transaction rolls back", async () => { |
| 116 | + await start(true); |
| 117 | + |
| 118 | + await env.db |
| 119 | + .transaction() |
| 120 | + .execute(async (trx) => { |
| 121 | + await enqueue(plain, { value: "rolled back" }, { trx }); |
| 122 | + throw new Error("rollback"); |
| 123 | + }) |
| 124 | + .catch(() => undefined); |
| 125 | + await env.db.transaction().execute(async (trx) => { |
| 126 | + await enqueue(plain, { value: "committed" }, { trx }); |
| 127 | + }); |
| 128 | + |
| 129 | + await until(() => seen.length === 1); |
| 130 | + // Give a wrongly surviving task two more polls to show up. |
| 131 | + await new Promise((resolve) => setTimeout(resolve, 1200)); |
| 132 | + expect(seen.map((task) => task.data.value)).toEqual(["committed"]); |
| 133 | + }); |
| 134 | + |
| 135 | + it("retries a failing task up to the limit, then reports it exhausted once", async () => { |
| 136 | + await start(true); |
| 137 | + await enqueue(failing, { value: "doomed" }); |
| 138 | + |
| 139 | + await until(() => exhausted.length === 1); |
| 140 | + expect(seen.map((task) => task.attempt)).toEqual([1, 2, 3]); |
| 141 | + expect(exhausted[0]?.attempt).toBe(3); |
| 142 | + }); |
| 143 | + |
| 144 | + it("never runs more tasks at once than the queue's concurrency", async () => { |
| 145 | + await start(true); |
| 146 | + await Promise.all(["a", "b", "c", "d", "e"].map((value) => enqueue(capped, { value }))); |
| 147 | + |
| 148 | + await until(() => seen.length === 5); |
| 149 | + expect(peak).toBe(2); |
| 150 | + }); |
| 151 | + |
| 152 | + it("keeps one task per key on an exclusive queue", async () => { |
| 153 | + await start(false); |
| 154 | + |
| 155 | + const first = await enqueue(exclusive, { value: "one" }, { key: "row-1" }); |
| 156 | + const duplicate = await enqueue(exclusive, { value: "two" }, { key: "row-1" }); |
| 157 | + const other = await enqueue(exclusive, { value: "three" }, { key: "row-2" }); |
| 158 | + |
| 159 | + expect(first).toEqual(expect.any(String)); |
| 160 | + expect(duplicate).toBeNull(); |
| 161 | + expect(other).toEqual(expect.any(String)); |
| 162 | + await expect(enqueue(exclusive, { value: "no key" })).rejects.toThrow(/needs a key/); |
| 163 | + }); |
| 164 | + |
| 165 | + it("accepts tasks but runs none when workers are off", async () => { |
| 166 | + await start(false); |
| 167 | + await enqueue(plain, { value: "parked" }); |
| 168 | + |
| 169 | + await new Promise((resolve) => setTimeout(resolve, 1500)); |
| 170 | + expect(seen).toHaveLength(0); |
| 171 | + }); |
| 172 | + |
| 173 | + it("rejects a bad payload and an unregistered queue", async () => { |
| 174 | + await start(false); |
| 175 | + |
| 176 | + await expect(enqueue(plain, { value: 1 } as unknown as Payload)).rejects.toThrow(/expected string/i); |
| 177 | + await expect(enqueue(unregistered, { value: "x" })).rejects.toThrow(/not registered/); |
| 178 | + }); |
| 179 | +}); |
0 commit comments