11import { assert , it } from "@effect/vitest" ;
2- import { EventId , ProjectId , ProviderInstanceId , ThreadId } from "@t3tools/contracts" ;
2+ import {
3+ EventId ,
4+ ProjectId ,
5+ ProviderInstanceId ,
6+ ThreadId ,
7+ type ModelSelection ,
8+ } from "@t3tools/contracts" ;
39import * as Cause from "effect/Cause" ;
410import * as Clock from "effect/Clock" ;
511import * as DateTime from "effect/DateTime" ;
612import * as Deferred from "effect/Deferred" ;
713import * as Effect from "effect/Effect" ;
814import * as Exit from "effect/Exit" ;
9- import * as Result from "effect/Result" ;
1015import * as Fiber from "effect/Fiber" ;
1116import * as FileSystem from "effect/FileSystem" ;
1217import * as Layer from "effect/Layer" ;
18+ import * as Option from "effect/Option" ;
1319import * as Path from "effect/Path" ;
1420import * as SqlClient from "effect/sql/SqlClient" ;
21+ import type { SqlError } from "effect/sql/SqlError" ;
1522import * as Statement from "effect/sql/Statement" ;
1623import * as Tracer from "effect/Tracer" ;
1724import * as TestClock from "effect/testing/TestClock" ;
@@ -39,6 +46,19 @@ const FIXED_EPOCH = "2026-01-01T00:00:00.000Z";
3946/** How long a blocked read is given before it is called blocked. */
4047const BLOCKED_TIMEOUT = "3 seconds" ;
4148
49+ /**
50+ * Print a measurement to the test output.
51+ *
52+ * `Effect.log` on its own is swallowed by the test runner, and the effect
53+ * language service rejects `console.log` inside Effect code. The measurement is
54+ * the point of the test, so it has to be readable in the output.
55+ */
56+ const printMeasurement = ( message : string ) =>
57+ Effect . sync ( ( ) => {
58+ // @effect -diagnostics-next-line globalConsoleInEffect:off - the measurement is the test's output, and the test runner captures the console.
59+ console . log ( message ) ;
60+ } ) ;
61+
4262/**
4363 * The scope the writer's row is written under.
4464 *
@@ -64,7 +84,11 @@ const HELD_WRITE_SCOPE = "lockwait-held-write";
6484 */
6585const withWriteLockHeld = < A , E , R > (
6686 effect : Effect . Effect < A , E , R > ,
67- ) : Effect . Effect < { readonly value : A ; readonly heldForMs : number } , E , R | SqlClient . SqlClient > =>
87+ ) : Effect . Effect <
88+ { readonly value : A ; readonly heldForMs : number ; readonly uncommittedRows : number } ,
89+ E | SqlError ,
90+ R | SqlClient . SqlClient
91+ > =>
6892 Effect . gen ( function * ( ) {
6993 const sql = yield * SqlClient . SqlClient ;
7094 const lockHeld = yield * Deferred . make < void > ( ) ;
@@ -73,19 +97,26 @@ const withWriteLockHeld = <A, E, R>(
7397 . withTransaction (
7498 Effect . gen ( function * ( ) {
7599 // A real write, with the table's own columns. A row that does not
76- // insert is not write load.
100+ // insert is not write load. The id is fixed because the database is
101+ // created for this test alone, so it is unique in it.
77102 yield * sql `INSERT INTO orchestration_v2_projection_checkpoints (
78103 checkpoint_id, thread_id, scope_id, run_id, node_id, ordinal_within_scope,
79104 status, captured_at, payload_json
80105 ) VALUES (
81- ${ crypto . randomUUID ( ) } , ${ "lockwait-thread" } , ${ HELD_WRITE_SCOPE } , ${ "run" } , ${ "node" } ,
106+ ${ "lockwait-checkpoint" } , ${ "lockwait-thread" } , ${ HELD_WRITE_SCOPE } , ${ "run" } , ${ "node" } ,
82107 1, 'captured', ${ FIXED_EPOCH } , '{}'
83108 )` ;
84109 yield * Deferred . succeed ( lockHeld , undefined ) ;
85110 yield * Deferred . await ( release ) ;
86111 } ) ,
87112 )
88- . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
113+ . pipe (
114+ // If the write fails, the lock is never held and the read would be
115+ // measuring nothing. Release the reader instead of leaving it to time
116+ // out, and let the writer's own exit report the failure below.
117+ Effect . tapError ( ( ) => Deferred . succeed ( lockHeld , undefined ) ) ,
118+ Effect . forkChild ( { startImmediately : true } ) ,
119+ ) ;
89120 yield * Deferred . await ( lockHeld ) ;
90121 // The write lock is held from here until the effect finishes.
91122 const started = yield * Clock . currentTimeMillis ;
@@ -100,6 +131,8 @@ const withWriteLockHeld = <A, E, R>(
100131 WHERE scope_id = ${ HELD_WRITE_SCOPE }
101132 ` . pipe ( NodeSqliteClient . readOnly ) ;
102133 yield * Deferred . succeed ( release , undefined ) ;
134+ // Joining the writer propagates its failure, so a row that did not insert
135+ // fails the test rather than passing as write load.
103136 yield * Fiber . join ( writer ) ;
104137 return { value, heldForMs, uncommittedRows : uncommittedRows [ 0 ] ?. count ?? - 1 } ;
105138 } ) ;
@@ -157,7 +190,7 @@ const measureReadUnderWriteLock = Effect.fn("measureReadUnderWriteLock")(functio
157190 span . end = ( endTime , exit ) => {
158191 end ( endTime , exit ) ;
159192 const connection = span . attributes . get ( "db.connection" ) ;
160- if ( connection !== undefined ) {
193+ if ( typeof connection === "string" ) {
161194 acquired . push ( {
162195 connection,
163196 lockWaitMs : Number ( span . attributes . get ( "db.lock_wait_ms" ) ?? 0 ) ,
@@ -204,7 +237,7 @@ const measureReadOnWriterConnection = Effect.fn("measureReadOnWriterConnection")
204237 span . end = ( endTime , exit ) => {
205238 end ( endTime , exit ) ;
206239 const connection = span . attributes . get ( "db.connection" ) ;
207- if ( connection !== undefined ) {
240+ if ( typeof connection === "string" ) {
208241 acquired . push ( {
209242 connection,
210243 lockWaitMs : Number ( span . attributes . get ( "db.lock_wait_ms" ) ?? 0 ) ,
@@ -248,8 +281,7 @@ it.effect("the projection read completes while a writer holds the write lock", (
248281 const storeLayer = Layer . mergeAll (
249282 ProjectionStore . layer . pipe ( Layer . provideMerge ( database ) ) ,
250283 database ,
251- NodeServices . layer ,
252- ) ;
284+ ) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
253285
254286 const threadId = ThreadId . make ( "thread:lockwait:reader" ) ;
255287 const { acquired, heldForMs, uncommittedRows } = yield * Effect . gen ( function * ( ) {
@@ -276,7 +308,7 @@ it.effect("the projection read completes while a writer holds the write lock", (
276308 // uncommitted, and so invisible to the reader, when the read finished.
277309 assert . isAtLeast ( heldForMs , 0 ) ;
278310 assert . equal ( uncommittedRows , 0 ) ;
279- console . log (
311+ yield * printMeasurement (
280312 `MEASURE read-on-reader: connection=${ acquired [ 0 ] ?. connection } lock_wait_ms=${ acquired [ 0 ] ?. lockWaitMs } lock_held_for_ms=${ heldForMs } uncommitted_rows_visible=${ uncommittedRows } ` ,
281313 ) ;
282314 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
@@ -291,8 +323,7 @@ it.effect("the same read on the writer connection is blocked by the write lock",
291323 const storeLayer = Layer . mergeAll (
292324 ProjectionStore . layer . pipe ( Layer . provideMerge ( database ) ) ,
293325 database ,
294- NodeServices . layer ,
295- ) ;
326+ ) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ;
296327
297328 const threadId = ThreadId . make ( "thread:lockwait:writer" ) ;
298329 const { acquired, heldForMs, value } = yield * Effect . gen ( function * ( ) {
@@ -309,14 +340,14 @@ it.effect("the same read on the writer connection is blocked by the write lock",
309340 assert . equal ( exit . _tag , "Failure" ) ;
310341 // A read that failed for some other reason would not have waited for the
311342 // bound, so the failure has to BE the timeout.
312- const found = Cause . findError ( exit . cause ) ;
313- assert . isTrue ( Result . isSuccess ( found ) , "expected a typed failure, got " + String ( found ) ) ;
314- const error = found . success ;
343+ const error = Exit . isFailure ( exit )
344+ ? Cause . findErrorOption ( exit . cause ) . pipe ( Option . getOrElse ( ( ) => undefined ) )
345+ : undefined ;
315346 assert . isTrue ( Cause . isTimeoutError ( error ) , "expected a timeout, got " + String ( error ) ) ;
316347 // It waited for the bound, with the lock held the whole time.
317348 assert . isAtLeast ( heldForMs , 2_000 ) ;
318349 assert . deepEqual ( acquired , [ ] ) ;
319- console . log (
350+ yield * printMeasurement (
320351 `MEASURE read-on-writer: outcome=TimeoutError lock_held_for_ms=${ heldForMs } connections_acquired=${ acquired . length } ` ,
321352 ) ;
322353 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
0 commit comments