From 8697dd1445f83aef549b04373695cebe528bd618 Mon Sep 17 00:00:00 2001 From: Renee Yang <7040416+reeceyang@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:46:19 -0800 Subject: [PATCH] Test that a cycle cuts before an op that overflows the flush Co-Authored-By: Claude Opus 5.5 --- src/component/worker.test.ts | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/component/worker.test.ts b/src/component/worker.test.ts index 842e734f..55fcd22f 100644 --- a/src/component/worker.test.ts +++ b/src/component/worker.test.ts @@ -922,6 +922,52 @@ describe("transaction limits", () => { }); }); + test("a cycle still drains when its last operation leaves more pending writes than the reserve", async () => { + // Every node stays under the 1 MiB document limit, even mid-split. Deleting + // key 12 merges big nodes: it reads 3.6 MiB and leaves 1.7 MiB more for + // the flush to read. The filler operations before it use up the budget so + // that the check after the last filler passes with less than that left. + const t = initConvexTest({ transactionLimits: true }); + await t.run(async (ctx) => { + await getOrCreateTree(ctx.db, {}, 4, false); + }); + for (let key = 0; key < 30; key++) { + await t.run(async (ctx) => { + await insertHandler(ctx, { key, value: "x".repeat(200_000) }); + }); + } + const fillers = 14; + for (let namespace = 0; namespace < fillers; namespace++) { + await t.run(async (ctx) => { + await getOrCreateTree(ctx.db, { namespace }, 4, false); + await insertHandler(ctx, { + key: 0, + value: "x".repeat(400_000), + namespace, + }); + }); + } + await enqueueRows(t, [ + ...Array.from({ length: fillers }, (_, namespace) => ({ + type: "insert" as const, + key: 1, + value: "y", + namespace, + })), + { type: "delete", key: 12 }, + ]); + + await drainManually(t); + await t.run(async (ctx) => { + expect(await ctx.db.query("pendingOperations").first()).toBeNull(); + expect(await ctx.db.query("deadLetterOperations").first()).toBeNull(); + expect((await aggregateBetweenHandler(ctx, {})).count).toEqual(29); + expect( + (await aggregateBetweenHandler(ctx, { namespace: 0 })).count, + ).toEqual(2); + }); + }, 60_000); + test("an operation too big for a cycle of its own is dead-lettered", async () => { // A 2.2 MiB budget leaves a cycle half of it. The first 400 KB insert // fits; the second reads the first's node and rereads it to write it back.