|
| 1 | +import { afterEach, describe, expect, it } from "vitest" |
| 2 | +import { rm } from "node:fs/promises" |
| 3 | +import { DatabaseSync } from "node:sqlite" |
| 4 | +import { Worker } from "node:worker_threads" |
| 5 | +import { sqlite, type SQLiteDatabase } from "../src/database/sqlite.js" |
| 6 | + |
| 7 | +let database: SQLiteDatabase | undefined |
| 8 | +let blocker: Worker | undefined |
| 9 | +let path: string | undefined |
| 10 | + |
| 11 | +afterEach(async () => { |
| 12 | + await blocker?.terminate() |
| 13 | + await database?.close() |
| 14 | + if (path) await rm(path, { force: true }) |
| 15 | + database = undefined |
| 16 | + blocker = undefined |
| 17 | + path = undefined |
| 18 | +}) |
| 19 | + |
| 20 | +describe("SQLite adapter", () => { |
| 21 | + it("retries a transaction after transient writer contention", async () => { |
| 22 | + path = `/tmp/solid-objects-sqlite-${crypto.randomUUID()}.sqlite3` |
| 23 | + const setup = new DatabaseSync(path) |
| 24 | + setup.exec("CREATE TABLE records(id INTEGER PRIMARY KEY)") |
| 25 | + setup.close() |
| 26 | + blocker = lockDatabase(path) |
| 27 | + await nextMessage(blocker) |
| 28 | + database = sqlite({ path, timeoutMilliseconds: 1, lockRetryAttempts: 10 }) |
| 29 | + |
| 30 | + await database.transaction((connection) => |
| 31 | + connection.run("INSERT INTO records(id) VALUES (?)", [1]), |
| 32 | + ) |
| 33 | + |
| 34 | + await expect( |
| 35 | + database.connection((connection) => |
| 36 | + connection.get<{ count: number | bigint }>("SELECT COUNT(*) AS count FROM records"), |
| 37 | + ), |
| 38 | + ).resolves.toEqual({ count: 1n }) |
| 39 | + }) |
| 40 | + |
| 41 | + it("validates its transaction retry budget", () => { |
| 42 | + expect(() => sqlite({ path: ":memory:", lockRetryAttempts: 0 })).toThrow( |
| 43 | + "lockRetryAttempts must be a positive safe integer", |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it("stops retrying after its transaction retry budget", async () => { |
| 48 | + path = `/tmp/solid-objects-sqlite-${crypto.randomUUID()}.sqlite3` |
| 49 | + const setup = new DatabaseSync(path) |
| 50 | + setup.exec("CREATE TABLE records(id INTEGER PRIMARY KEY)") |
| 51 | + setup.close() |
| 52 | + blocker = lockDatabase(path, { holdMilliseconds: 1_000 }) |
| 53 | + await nextMessage(blocker) |
| 54 | + database = sqlite({ path, timeoutMilliseconds: 1, lockRetryAttempts: 2 }) |
| 55 | + |
| 56 | + const startedAt = performance.now() |
| 57 | + await expect( |
| 58 | + database.transaction((connection) => |
| 59 | + connection.run("INSERT INTO records(id) VALUES (?)", [1]), |
| 60 | + ), |
| 61 | + ).rejects.toThrow("database is locked") |
| 62 | + |
| 63 | + expect(performance.now() - startedAt).toBeLessThan(100) |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +function lockDatabase(databasePath: string, options: { holdMilliseconds?: number } = {}): Worker { |
| 68 | + return new Worker( |
| 69 | + ` |
| 70 | + const { DatabaseSync } = require("node:sqlite") |
| 71 | + const { parentPort, workerData } = require("node:worker_threads") |
| 72 | + const database = new DatabaseSync(workerData.databasePath) |
| 73 | + database.exec("BEGIN IMMEDIATE") |
| 74 | + parentPort.postMessage("locked") |
| 75 | + setTimeout(() => { |
| 76 | + database.exec("ROLLBACK") |
| 77 | + database.close() |
| 78 | + }, workerData.holdMilliseconds) |
| 79 | + `, |
| 80 | + { |
| 81 | + eval: true, |
| 82 | + workerData: { databasePath, holdMilliseconds: options.holdMilliseconds ?? 25 }, |
| 83 | + }, |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +function nextMessage(worker: Worker): Promise<unknown> { |
| 88 | + return new Promise((resolve, reject) => { |
| 89 | + worker.once("message", resolve) |
| 90 | + worker.once("error", reject) |
| 91 | + }) |
| 92 | +} |
0 commit comments