@@ -5,6 +5,7 @@ import { mkdtempSync, rmSync } from "node:fs";
55import { tmpdir } from "node:os" ;
66import { join } from "node:path" ;
77import { $ } from "bun" ;
8+ import * as logger from "../../../shared/logger" ;
89import { Database } from "../../../shared/sqlite" ;
910import { closeQuietly } from "../../../shared/sqlite-helpers" ;
1011import { runMigrations } from "../migrations" ;
@@ -150,6 +151,61 @@ describe("dreamer lease (atomic CAS)", () => {
150151 closeQuietly ( db ) ;
151152 } ) ;
152153
154+ it ( "logs a slow guarded write after COMMIT with its lease domain" , ( ) => {
155+ const db = makeDb ( ) ;
156+ const events : string [ ] = [ ] ;
157+ const realExec = db . exec . bind ( db ) ;
158+ const execSpy = spyOn ( db , "exec" ) . mockImplementation ( ( sql : string ) => {
159+ events . push ( sql ) ;
160+ return realExec ( sql ) ;
161+ } ) ;
162+ const logSpy = spyOn ( logger , "log" ) . mockImplementation ( ( message : string ) => {
163+ events . push ( `log:${ message } ` ) ;
164+ } ) ;
165+ const nowSpy = spyOn ( performance , "now" )
166+ . mockImplementationOnce ( ( ) => 0 )
167+ . mockImplementationOnce ( ( ) => 1_001 ) ;
168+ try {
169+ expect ( acquireLease ( db , "holder-a" , "memory:proj" ) ) . toBe ( true ) ;
170+ db . prepare ( "CREATE TABLE guarded_write_timing (value TEXT)" ) . run ( ) ;
171+ runLeaseGuardedWrite ( db , "holder-a" , "memory:proj" , ( ) => {
172+ events . push ( "body" ) ;
173+ db . prepare ( "INSERT INTO guarded_write_timing (value) VALUES ('committed')" ) . run ( ) ;
174+ } ) ;
175+
176+ const commitIndex = events . indexOf ( "COMMIT" ) ;
177+ const logIndex = events . findIndex ( ( event ) => event . startsWith ( "log:" ) ) ;
178+ expect ( commitIndex ) . toBeGreaterThanOrEqual ( 0 ) ;
179+ expect ( logIndex ) . toBeGreaterThan ( commitIndex ) ;
180+ expect ( logSpy ) . toHaveBeenCalledWith (
181+ expect . stringContaining ( "site=lease-guarded-write:memory:proj" ) ,
182+ ) ;
183+ expect ( logSpy . mock . calls [ 0 ] ?. [ 0 ] ) . toMatch ( / h e l d = 1 0 0 1 \. 0 m s / ) ;
184+ } finally {
185+ nowSpy . mockRestore ( ) ;
186+ logSpy . mockRestore ( ) ;
187+ execSpy . mockRestore ( ) ;
188+ closeQuietly ( db ) ;
189+ }
190+ } ) ;
191+
192+ it ( "does not log a guarded write below the slow-write threshold" , ( ) => {
193+ const db = makeDb ( ) ;
194+ const logSpy = spyOn ( logger , "log" ) . mockImplementation ( ( ) => { } ) ;
195+ const nowSpy = spyOn ( performance , "now" )
196+ . mockImplementationOnce ( ( ) => 0 )
197+ . mockImplementationOnce ( ( ) => 999 ) ;
198+ try {
199+ expect ( acquireLease ( db , "holder-a" , "memory:proj" ) ) . toBe ( true ) ;
200+ runLeaseGuardedWrite ( db , "holder-a" , "memory:proj" , ( ) => { } ) ;
201+ expect ( logSpy ) . not . toHaveBeenCalled ( ) ;
202+ } finally {
203+ nowSpy . mockRestore ( ) ;
204+ logSpy . mockRestore ( ) ;
205+ closeQuietly ( db ) ;
206+ }
207+ } ) ;
208+
153209 it ( "allows exactly one winner across separate DB handles" , ( ) => {
154210 const dir = mkdtempSync ( join ( tmpdir ( ) , "mc-dream-lease-handles-" ) ) ;
155211 const path = join ( dir , "context.db" ) ;
0 commit comments