@@ -9,6 +9,8 @@ import * as FileSystem from "effect/FileSystem";
99import * as Path from "effect/Path" ;
1010import * as SqlClient from "effect/sql/SqlClient" ;
1111import * as Statement from "effect/sql/Statement" ;
12+ import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
13+ import * as Stream from "effect/Stream" ;
1214import type * as Tracer from "effect/Tracer" ;
1315import { vi } from "vite-plus/test" ;
1416
@@ -192,6 +194,34 @@ it.effect("keeps another connection from committing between a transaction's read
192194 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
193195) ;
194196
197+ it . effect ( "waits for another process's lock while the database opens" , ( ) =>
198+ Effect . gen ( function * ( ) {
199+ const fs = yield * FileSystem . FileSystem ;
200+ const path = yield * Path . Path ;
201+ const directory = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-sqlite-open-" } ) ;
202+ const filename = path . join ( directory , "state.sqlite" ) ;
203+ const seed = new NodeSqlite . DatabaseSync ( filename ) ;
204+ seed . exec ( "PRAGMA journal_mode = WAL; CREATE TABLE entries(name TEXT NOT NULL)" ) ;
205+ seed . close ( ) ;
206+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
207+ const holder = yield * spawner . spawn (
208+ ChildProcess . make ( process . execPath , [
209+ "-e" ,
210+ `const db = new (require("node:sqlite").DatabaseSync)(process.argv[1]);
211+ db.exec("PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE");
212+ console.log("held");
213+ setTimeout(() => db.close(), 300);` ,
214+ filename ,
215+ ] ) ,
216+ ) ;
217+ yield * Stream . runHead ( holder . stdout ) ;
218+ yield * Effect . gen ( function * ( ) {
219+ const sql = yield * SqlClient . SqlClient ;
220+ assert . deepEqual ( yield * sql `SELECT COUNT(*) FROM entries` . values , [ [ 0 ] ] ) ;
221+ } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename, checkpointWorker : true } ) ) ) ;
222+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
223+ ) ;
224+
195225it . effect ( "reports a transaction blocked by another writer as a lock timeout" , ( ) =>
196226 Effect . gen ( function * ( ) {
197227 const { filename, other } = yield * makeTempDatabase ;
0 commit comments