70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import { randomBytes } from "node:crypto";
|
|
import { env, exports } from "cloudflare:workers";
|
|
import { it } from "vitest";
|
|
import type { QueueJob } from "../src/index";
|
|
|
|
it("consumes queue messages", async ({ expect }) => {
|
|
// `exports.default` here points to the worker running in the current isolate.
|
|
// This gets its handler from the `main` option in `vitest.config.mts`.
|
|
// Importantly, it uses the exact `import("../src").default` instance we could
|
|
// import in this file as its handler. Note the `exports.default.queue()` method
|
|
// is experimental, and requires the `service_binding_extra_handlers`
|
|
// compatibility flag to be enabled.
|
|
const messages: ServiceBindingQueueMessage<QueueJob>[] = [
|
|
{
|
|
id: randomBytes(16).toString("hex"),
|
|
timestamp: new Date(1000),
|
|
attempts: 1,
|
|
body: { key: "/1", value: "one" },
|
|
},
|
|
{
|
|
id: randomBytes(16).toString("hex"),
|
|
timestamp: new Date(2000),
|
|
attempts: 1,
|
|
body: { key: "/2", value: "two" },
|
|
},
|
|
];
|
|
const result = await exports.default.queue("queue", messages);
|
|
expect(result.outcome).toBe("ok");
|
|
expect(result.retryBatch.retry).toBe(false); // `true` if `batch.retryAll()` called
|
|
expect(result.ackAll).toBe(false); // `true` if `batch.ackAll()` called
|
|
expect(result.retryMessages).toStrictEqual([]);
|
|
expect(result.explicitAcks).toStrictEqual([messages[0].id, messages[1].id]);
|
|
|
|
expect(await env.QUEUE_RESULTS.get("/1")).toBe("ONE");
|
|
expect(await env.QUEUE_RESULTS.get("/2")).toBe("TWO");
|
|
});
|