22import * as NodeFS from "node:fs" ;
33import * as NodeOS from "node:os" ;
44import * as NodePath from "node:path" ;
5- import * as NodeSqlite from "node:sqlite" ;
65import type * as NodeWorkerThreads from "node:worker_threads" ;
76
87import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
@@ -23,92 +22,19 @@ import * as ServerOwnership from "../serverOwnership.ts";
2322import { migrationManifest , runMigrations } from "./Migrations.ts" ;
2423import * as SqlitePersistence from "./Sqlite.ts" ;
2524
26- const workers = vi . hoisted ( ( ) : Array < NodeWorkerThreads . Worker > => [ ] ) ;
2725const workerData = vi . hoisted ( ( ) : Array < unknown > => [ ] ) ;
2826
2927vi . mock ( "node:worker_threads" , async ( importOriginal ) => {
3028 const actual = await importOriginal < typeof NodeWorkerThreads > ( ) ;
3129 class RecordedWorker extends actual . Worker {
3230 constructor ( ...args : ConstructorParameters < typeof actual . Worker > ) {
3331 super ( ...args ) ;
34- workers . push ( this ) ;
3532 workerData . push ( args [ 1 ] ?. workerData ) ;
3633 }
3734 }
3835 return { ...actual , Worker : RecordedWorker } ;
3936} ) ;
4037
41- it . effect ( "keeps writes moving past a 48 MiB WAL while a reader retains its snapshot" , ( ) => {
42- const tempDir = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-sqlite-burst-" ) ) ;
43- const dbPath = NodePath . join ( tempDir , "state.sqlite" ) ;
44- const persistence = SqlitePersistence . layerFromPath ( dbPath ) . pipe (
45- Layer . provide ( NodeServices . layer ) ,
46- ) ;
47-
48- return Effect . gen ( function * ( ) {
49- const sql = yield * SqlClient . SqlClient ;
50- const checkpointWorker = workers . at ( - 1 ) ! ;
51- yield * sql `CREATE TABLE burst_probe(id INTEGER PRIMARY KEY, payload BLOB)` ;
52- yield * sql `INSERT INTO burst_probe VALUES (1, NULL)` ;
53- yield * sql `PRAGMA wal_checkpoint(TRUNCATE)` ;
54- const reader = yield * Effect . acquireRelease (
55- Effect . sync ( ( ) => new NodeSqlite . DatabaseSync ( dbPath , { readOnly : true } ) ) ,
56- ( database ) => Effect . sync ( ( ) => database . close ( ) ) ,
57- ) ;
58- reader . exec ( "BEGIN" ) ;
59- assert . equal ( reader . prepare ( "SELECT COUNT(*) AS count FROM burst_probe" ) . get ( ) ?. count , 1 ) ;
60-
61- yield * sql . withTransaction (
62- sql `INSERT INTO burst_probe VALUES (2, zeroblob(${ 48 * 1024 * 1024 } ))` ,
63- ) ;
64- const state = yield * Effect . callback < {
65- readonly log : number ;
66- readonly checkpointed : number ;
67- readonly blocked : boolean ;
68- } > ( ( resume ) => {
69- const onMessage = ( message : {
70- readonly type ?: string ;
71- readonly log ?: number ;
72- readonly checkpointed ?: number ;
73- readonly blocked ?: boolean ;
74- } ) => {
75- if (
76- message . type === "checkpoint" &&
77- typeof message . log === "number" &&
78- message . log > 10_000 &&
79- typeof message . checkpointed === "number" &&
80- typeof message . blocked === "boolean"
81- ) {
82- checkpointWorker . off ( "message" , onMessage ) ;
83- resume (
84- Effect . succeed ( {
85- log : message . log ,
86- checkpointed : message . checkpointed ,
87- blocked : message . blocked ,
88- } ) ,
89- ) ;
90- }
91- } ;
92- checkpointWorker . on ( "message" , onMessage ) ;
93- checkpointWorker . ref ( ) ;
94- // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
95- checkpointWorker . postMessage ( "barrier" ) ;
96- return Effect . sync ( ( ) => checkpointWorker . off ( "message" , onMessage ) ) ;
97- } ) ;
98- assert . isBelow ( state . checkpointed , state . log ) ;
99- assert . isFalse ( state . blocked ) ;
100-
101- yield * sql . withTransaction ( sql `INSERT INTO burst_probe VALUES (3, NULL)` ) ;
102- assert . equal ( reader . prepare ( "SELECT COUNT(*) AS count FROM burst_probe" ) . get ( ) ?. count , 1 ) ;
103- reader . exec ( "COMMIT" ) ;
104- assert . equal ( reader . prepare ( "SELECT COUNT(*) AS count FROM burst_probe" ) . get ( ) ?. count , 3 ) ;
105- } ) . pipe (
106- Effect . scoped ,
107- Effect . provide ( persistence ) ,
108- Effect . ensuring ( Effect . sync ( ( ) => NodeFS . rmSync ( tempDir , { recursive : true , force : true } ) ) ) ,
109- ) ;
110- } ) ;
111-
11238// Linux names an open, already-deleted file "<path> (deleted)" in /proc/self/fd.
11339// SQLite names its temp files "etilqs_*" and, without TMPDIR, puts them in
11440// /var/tmp while os.tmpdir() is /tmp, so match the name, not the directory.
@@ -223,6 +149,7 @@ it.effect("upgrades an existing database on a read-write worker connection", ()
223149 assert . deepInclude ( workerData . slice ( startedBefore ) , {
224150 filename : dbPath ,
225151 readOnly : false ,
152+ requireWal : false ,
226153 allowExtension : false ,
227154 prepareCacheSize : 200 ,
228155 } ) ;
0 commit comments