@@ -168,30 +168,32 @@ const makeTempDatabase = Effect.gen(function* () {
168168 return { filename, other } ;
169169} ) ;
170170
171- it . effect ( "keeps another connection from committing between a transaction's read and write" , ( ) =>
172- Effect . gen ( function * ( ) {
173- const { filename, other } = yield * makeTempDatabase ;
174- yield * Effect . gen ( function * ( ) {
175- const sql = yield * SqlClient . SqlClient ;
176- yield * sql . withTransaction (
177- Effect . gen ( function * ( ) {
178- const [ row ] = yield * sql < { readonly value : number } > `
171+ it . effect . each ( [ { writerWorker : false } , { writerWorker : true } ] ) (
172+ "keeps another connection from committing between a transaction's read and write (writerWorker=$writerWorker)" ,
173+ ( { writerWorker } ) =>
174+ Effect . gen ( function * ( ) {
175+ const { filename, other } = yield * makeTempDatabase ;
176+ yield * Effect . gen ( function * ( ) {
177+ const sql = yield * SqlClient . SqlClient ;
178+ yield * sql . withTransaction (
179+ Effect . gen ( function * ( ) {
180+ const [ row ] = yield * sql < { readonly value : number } > `
179181 SELECT value FROM counters WHERE id = 1
180182 ` ;
181- // Deferred BEGIN permits a competing commit, causing SQLITE_BUSY_SNAPSHOT that no timeout can resolve.
182- yield * Effect . sync ( ( ) =>
183- assert . throws (
184- ( ) => other . exec ( "UPDATE counters SET value = value + 1 WHERE id = 1" ) ,
185- / d a t a b a s e i s l o c k e d / ,
186- ) ,
187- ) ;
188- yield * sql `UPDATE counters SET value = ${ ( row ?. value ?? 0 ) + 10 } WHERE id = 1` ;
189- } ) ,
190- ) ;
191- yield * Effect . sync ( ( ) => other . exec ( "UPDATE counters SET value = value + 1 WHERE id = 1" ) ) ;
192- assert . deepEqual ( yield * sql `SELECT value FROM counters WHERE id = 1` . values , [ [ 11 ] ] ) ;
193- } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename } ) ) ) ;
194- } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
183+ // Deferred BEGIN permits a competing commit, causing SQLITE_BUSY_SNAPSHOT that no timeout can resolve.
184+ yield * Effect . sync ( ( ) =>
185+ assert . throws (
186+ ( ) => other . exec ( "UPDATE counters SET value = value + 1 WHERE id = 1" ) ,
187+ / d a t a b a s e i s l o c k e d / ,
188+ ) ,
189+ ) ;
190+ yield * sql `UPDATE counters SET value = ${ ( row ?. value ?? 0 ) + 10 } WHERE id = 1` ;
191+ } ) ,
192+ ) ;
193+ yield * Effect . sync ( ( ) => other . exec ( "UPDATE counters SET value = value + 1 WHERE id = 1" ) ) ;
194+ assert . deepEqual ( yield * sql `SELECT value FROM counters WHERE id = 1` . values , [ [ 11 ] ] ) ;
195+ } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename, writerWorker } ) ) ) ;
196+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
195197) ;
196198
197199it . effect ( "waits for another process's lock while the database opens" , ( ) =>
@@ -222,21 +224,41 @@ it.effect("waits for another process's lock while the database opens", () =>
222224 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
223225) ;
224226
225- it . effect ( "reports a transaction blocked by another writer as a lock timeout" , ( ) =>
227+ it . effect . each ( [ { writerWorker : false } , { writerWorker : true } ] ) (
228+ "reports a transaction blocked by another writer as a lock timeout (writerWorker=$writerWorker)" ,
229+ ( { writerWorker } ) =>
230+ Effect . gen ( function * ( ) {
231+ const { filename, other } = yield * makeTempDatabase ;
232+ yield * Effect . gen ( function * ( ) {
233+ const sql = yield * SqlClient . SqlClient ;
234+ yield * sql `PRAGMA busy_timeout = 0` ;
235+ const read = sql . withTransaction ( sql `SELECT value FROM counters WHERE id = 1` . values ) ;
236+
237+ yield * Effect . sync ( ( ) => other . exec ( "BEGIN IMMEDIATE" ) ) ;
238+ const error = yield * read . pipe ( Effect . flip ) ;
239+ assert . equal ( error . reason . _tag , "LockTimeoutError" ) ;
240+
241+ yield * Effect . sync ( ( ) => other . exec ( "COMMIT" ) ) ;
242+ assert . deepEqual ( yield * read , [ [ 0 ] ] ) ;
243+ } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename, writerWorker } ) ) ) ;
244+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
245+ ) ;
246+
247+ it . effect ( "keeps the event loop free while a write waits on another connection's lock" , ( ) =>
226248 Effect . gen ( function * ( ) {
227249 const { filename, other } = yield * makeTempDatabase ;
228250 yield * Effect . gen ( function * ( ) {
229251 const sql = yield * SqlClient . SqlClient ;
230- yield * sql `PRAGMA busy_timeout = 0` ;
231- const read = sql . withTransaction ( sql `SELECT value FROM counters WHERE id = 1` . values ) ;
232-
233252 yield * Effect . sync ( ( ) => other . exec ( "BEGIN IMMEDIATE" ) ) ;
234- const error = yield * read . pipe ( Effect . flip ) ;
235- assert . equal ( error . reason . _tag , "LockTimeoutError" ) ;
236-
237- yield * Effect . sync ( ( ) => other . exec ( "COMMIT" ) ) ;
238- assert . deepEqual ( yield * read , [ [ 0 ] ] ) ;
239- } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename } ) ) ) ;
253+ const write = yield * Effect . forkChild (
254+ sql `UPDATE counters SET value = value + 1 WHERE id = 1` ,
255+ { startImmediately : true } ,
256+ ) ;
257+ // A writer on this thread would spin in busy_timeout, so the holder could never commit.
258+ yield * Effect . sync ( ( ) => other . exec ( "UPDATE counters SET value = 10 WHERE id = 1; COMMIT" ) ) ;
259+ yield * Fiber . join ( write ) ;
260+ assert . deepEqual ( yield * sql `SELECT value FROM counters WHERE id = 1` . values , [ [ 11 ] ] ) ;
261+ } ) . pipe ( Effect . provide ( SqliteClient . layer ( { filename, writerWorker : true } ) ) ) ;
240262 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
241263) ;
242264
0 commit comments