Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/component/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import {
getDocumentSize,
} from "convex/values";
import schema, { type Operation } from "./schema.js";
import { ping } from "@convex-dev/batch-worker";
import { initConvexTest } from "./setup.test.js";

vi.mock("@convex-dev/batch-worker", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@convex-dev/batch-worker")>();
return { ...actual, ping: vi.fn(actual.ping) };
});
import {
aggregateBetweenHandler,
getHandler,
Expand Down Expand Up @@ -893,3 +900,27 @@ describe("guards", () => {
).rejects.toThrow(/no progress/);
});
});

describe("pinging the worker", () => {
beforeEach(() => {
vi.mocked(ping).mockClear();
});

test("only the first enqueue in a transaction pings", async () => {
const t = initConvexTest();
await t.run(async (ctx) => {
await enqueueOperations(ctx, [{ type: "insert", key: 1, value: "a" }]);
expect(ping).toHaveBeenCalledTimes(1);
await enqueueOperations(ctx, [{ type: "insert", key: 2, value: "b" }]);
await enqueueOperations(ctx, [{ type: "insert", key: 3, value: "c" }]);
expect(ping).toHaveBeenCalledTimes(1);
});

// The one ping still gets everything the transaction enqueued drained.
await drainViaWorker(t);
await t.run(async (ctx) => {
expect(await getHandler(ctx, { key: 3 })).toEqual({ k: 3, v: "c", s: 0 });
Comment thread
reeceyang marked this conversation as resolved.
Resolved
expect(await ctx.db.query("pendingOperations").collect()).toEqual([]);
});
});
});
4 changes: 4 additions & 0 deletions src/component/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export async function enqueueOperations(
}
await ctx.db.insert("pendingOperations", entry);
}
// Only ping when `enqueueOperations` is called for the first time
if (newestEntry) {
return;
}
await ping(ctx, components.batchWorker, {
// TODO: explore separate queues by namespace
name: OPS_WORKER_NAME,
Expand Down
Loading