/** * A write that comes while one of the app's own transactions is open waits for it instead of being refused as "database is locked" — which also left the next * transaction unable to commit. Reads are not held, transactions take turns, and a wait that goes on too long is given up. * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; const { createClient } = await import("@libsql/client"); const { drizzle } = await import("drizzle-orm/libsql"); const { sql } = await import("drizzle-orm"); const { serialiseWrites } = await import("../src/db/writeGate.js"); const dir = mkdtempSync(join(tmpdir(), "hm-writegate-")); const url = `file:${join(dir, "test.sqlite")}`; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const isBusy = (err: unknown) => String((err as { code?: string }).code).startsWith("SQLITE_BUSY"); describe("writes and transactions of one process", () => { const client = serialiseWrites(createClient({ url }), 1500); const db = drizzle(client); const rows = async () => (await client.execute("select x from t order by x")).rows.map((r) => Number(r.x)); before(async () => { await client.execute("create table t (x integer)"); }); after(() => { client.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("a write that comes while a transaction is open waits for it, and neither is refused", async () => { const tx = await client.transaction("write"); await tx.execute("insert into t values (1)"); const started = Date.now(); const write = client.execute("insert into t values (2)"); await sleep(250); await tx.commit(); await write; assert.ok(Date.now() - started >= 250, "it waited"); }); test("a refused write used to leave the next transaction unable to commit; now there is none refused", async () => { const tx = await client.transaction("write"); await tx.execute("insert into t values (3)"); const writes = [client.execute("insert into t values (4)"), client.execute("delete from t where x < 0")]; await sleep(100); await tx.commit(); await Promise.all(writes); const later = await client.transaction("write"); await later.execute("insert into t values (5)"); await later.commit(); assert.deepEqual(await rows(), [1, 2, 3, 4, 5]); }); test("reads are not held by a transaction", async () => { const tx = await client.transaction("write"); await tx.execute("insert into t values (6)"); const started = Date.now(); assert.deepEqual(await rows(), [1, 2, 3, 4, 5], "what is committed, at once"); assert.ok(Date.now() - started < 200); await tx.rollback(); }); test("transactions take turns, and a rollback lets the next one in", async () => { const order: string[] = []; const first = await client.transaction("write"); const second = client.transaction("write").then(async (tx) => { order.push("second began"); await tx.execute("insert into t values (8)"); await tx.commit(); }); await first.execute("insert into t values (7)"); await sleep(150); order.push("first ends"); await first.rollback(); await second; assert.deepEqual(order, ["first ends", "second began"]); assert.deepEqual(await rows(), [1, 2, 3, 4, 5, 8]); }); test("drizzle: writes that land in the pauses of a transaction all go through, and the transaction commits", async () => { const results: Promise[] = []; await db.transaction(async (tx) => { for (let i = 0; i < 10; i++) { await tx.run(sql`insert into t values (${100 + i})`); results.push(db.run(sql`insert into t values (${200 + i})`)); await sleep(5); } }); await Promise.all(results); const all = await rows(); assert.equal(all.length, 6 + 20); assert.deepEqual(all.filter((n) => n >= 100 && n < 110).length, 10); assert.deepEqual(all.filter((n) => n >= 200).length, 10); }); test("a transaction that fails inside rolls back, and the writes held behind it go on", async () => { const held: Promise[] = []; await assert.rejects( db.transaction(async (tx) => { await tx.run(sql`insert into t values (300)`); held.push(db.run(sql`insert into t values (301)`)); await sleep(50); throw new Error("no good"); }), /no good/, ); await Promise.all(held); const all = await rows(); assert.ok(!all.includes(300), "rolled back"); assert.ok(all.includes(301), "the write that waited went through"); }); }); describe("when something else holds the lock, or a transaction is never ended", () => { const dir2 = mkdtempSync(join(tmpdir(), "hm-writegate2-")); // Two files: a write that was refused leaves its connection holding the lock until the leftover is cleaned up, which is what the first test makes happen. const url2 = `file:${join(dir2, "first.sqlite")}`; const url3 = `file:${join(dir2, "second.sqlite")}`; after(() => { try { rmSync(dir2, { recursive: true, force: true }); } catch { // see above } }); test("a wait that goes on too long is given up: the write is refused as it would have been", async () => { const client = serialiseWrites(createClient({ url: url2 }), 300); await client.execute("create table t (x integer)"); const forgotten = await client.transaction("write"); await forgotten.execute("insert into t values (1)"); const started = Date.now(); await assert.rejects(client.execute("insert into t values (2)"), isBusy); assert.ok(Date.now() - started >= 250, "it waited first"); await forgotten.rollback(); await client.execute("insert into t values (3)"); assert.deepEqual((await client.execute("select x from t")).rows.map((r) => Number(r.x)), [3]); client.close(); }); test("a transaction that cannot begin (another process holds the lock) does not hold the others up", async () => { const client = serialiseWrites(createClient({ url: url3 }), 300); await client.execute("create table t (x integer)"); const other = createClient({ url: url3 }); const theirs = await other.transaction("write"); await theirs.execute("insert into t values (10)"); await assert.rejects(client.transaction("write"), isBusy); await theirs.rollback(); other.close(); const started = Date.now(); await client.execute("insert into t values (11)"); assert.ok(Date.now() - started < 200, "no wait left behind"); client.close(); }); });