66import { existsSync , mkdirSync , statSync , utimesSync } from 'node:fs'
77import { tmpdir } from 'node:os'
88import path from 'node:path'
9- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
9+ import { afterEach , beforeEach , describe , expect , it } from 'vitest'
1010
1111import { safeDelete } from '../src/fs'
1212import { processLock } from '../src/process-lock'
@@ -19,7 +19,10 @@ describe('process-lock', () => {
1919
2020 beforeEach ( ( ) => {
2121 // Create unique test directory for each test.
22- testDir = path . join ( tmpdir ( ) , `process-lock-test-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` )
22+ testDir = path . join (
23+ tmpdir ( ) ,
24+ `process-lock-test-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ,
25+ )
2326 lockPath = path . join ( testDir , 'test.lock' )
2427
2528 // Ensure test directory exists.
@@ -51,7 +54,9 @@ describe('process-lock', () => {
5154
5255 try {
5356 // Second acquire should fail after retries.
54- await expect ( processLock . acquire ( lockPath , { retries : 1 , baseDelayMs : 10 } ) ) . rejects . toThrow ( / L o c k a l r e a d y e x i s t s / )
57+ await expect (
58+ processLock . acquire ( lockPath , { retries : 1 , baseDelayMs : 10 } ) ,
59+ ) . rejects . toThrow ( / L o c k a l r e a d y e x i s t s / )
5560 } finally {
5661 release1 ( )
5762 }
@@ -101,18 +106,20 @@ describe('process-lock', () => {
101106 it ( 'should respect custom stale timeout' , async ( ) => {
102107 // Create a lock that's 6 seconds old.
103108 mkdirSync ( lockPath )
104- const sixSecondsAgo = Date . now ( ) - 6_000
109+ const sixSecondsAgo = Date . now ( ) - 6000
105110 utimesSync ( lockPath , new Date ( sixSecondsAgo ) , new Date ( sixSecondsAgo ) )
106111
107112 // With 10s stale timeout, should not be stale yet.
108- await expect ( processLock . acquire ( lockPath , {
109- retries : 1 ,
110- baseDelayMs : 10 ,
111- staleMs : 10_000 ,
112- } ) ) . rejects . toThrow ( )
113+ await expect (
114+ processLock . acquire ( lockPath , {
115+ retries : 1 ,
116+ baseDelayMs : 10 ,
117+ staleMs : 10_000 ,
118+ } ) ,
119+ ) . rejects . toThrow ( )
113120
114121 // With 5s stale timeout, should be stale and acquirable.
115- const release = await processLock . acquire ( lockPath , { staleMs : 5_000 } )
122+ const release = await processLock . acquire ( lockPath , { staleMs : 5000 } )
116123 expect ( existsSync ( lockPath ) ) . toBe ( true )
117124
118125 release ( )
@@ -170,10 +177,12 @@ describe('process-lock', () => {
170177 } )
171178
172179 it ( 'should release lock even if callback throws' , async ( ) => {
173- await expect ( processLock . withLock ( lockPath , async ( ) => {
174- expect ( existsSync ( lockPath ) ) . toBe ( true )
175- throw new Error ( 'Test error' )
176- } ) ) . rejects . toThrow ( 'Test error' )
180+ await expect (
181+ processLock . withLock ( lockPath , async ( ) => {
182+ expect ( existsSync ( lockPath ) ) . toBe ( true )
183+ throw new Error ( 'Test error' )
184+ } ) ,
185+ ) . rejects . toThrow ( 'Test error' )
177186
178187 // Lock should be released despite error.
179188 expect ( existsSync ( lockPath ) ) . toBe ( false )
@@ -183,20 +192,28 @@ describe('process-lock', () => {
183192 const executionOrder : string [ ] = [ ]
184193
185194 // Start two withLock calls concurrently.
186- const promise1 = processLock . withLock ( lockPath , async ( ) => {
187- executionOrder . push ( 'start-1' )
188- await new Promise ( resolve => setTimeout ( resolve , 100 ) )
189- executionOrder . push ( 'end-1' )
190- } , { retries : 10 , baseDelayMs : 20 } )
195+ const promise1 = processLock . withLock (
196+ lockPath ,
197+ async ( ) => {
198+ executionOrder . push ( 'start-1' )
199+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
200+ executionOrder . push ( 'end-1' )
201+ } ,
202+ { retries : 10 , baseDelayMs : 20 } ,
203+ )
191204
192205 // Small delay to ensure first lock is acquired.
193206 await new Promise ( resolve => setTimeout ( resolve , 10 ) )
194207
195- const promise2 = processLock . withLock ( lockPath , async ( ) => {
196- executionOrder . push ( 'start-2' )
197- await new Promise ( resolve => setTimeout ( resolve , 50 ) )
198- executionOrder . push ( 'end-2' )
199- } , { retries : 10 , baseDelayMs : 20 } )
208+ const promise2 = processLock . withLock (
209+ lockPath ,
210+ async ( ) => {
211+ executionOrder . push ( 'start-2' )
212+ await new Promise ( resolve => setTimeout ( resolve , 50 ) )
213+ executionOrder . push ( 'end-2' )
214+ } ,
215+ { retries : 10 , baseDelayMs : 20 } ,
216+ )
200217
201218 await Promise . all ( [ promise1 , promise2 ] )
202219
@@ -209,13 +226,17 @@ describe('process-lock', () => {
209226 retries : 5 ,
210227 baseDelayMs : 50 ,
211228 maxDelayMs : 200 ,
212- staleMs : 5_000 ,
229+ staleMs : 5000 ,
213230 }
214231
215232 let executed = false
216- await processLock . withLock ( lockPath , async ( ) => {
217- executed = true
218- } , options )
233+ await processLock . withLock (
234+ lockPath ,
235+ async ( ) => {
236+ executed = true
237+ } ,
238+ options ,
239+ )
219240
220241 expect ( executed ) . toBe ( true )
221242 } )
@@ -226,12 +247,20 @@ describe('process-lock', () => {
226247 // Create stale lock (15 seconds old).
227248 mkdirSync ( lockPath )
228249 const fifteenSecondsAgo = Date . now ( ) - 15_000
229- utimesSync ( lockPath , new Date ( fifteenSecondsAgo ) , new Date ( fifteenSecondsAgo ) )
250+ utimesSync (
251+ lockPath ,
252+ new Date ( fifteenSecondsAgo ) ,
253+ new Date ( fifteenSecondsAgo ) ,
254+ )
230255
231256 // Should remove stale lock and acquire.
232- const release = await processLock . withLock ( lockPath , async ( ) => {
233- return 'acquired'
234- } , { staleMs : 10_000 } )
257+ const release = await processLock . withLock (
258+ lockPath ,
259+ async ( ) => {
260+ return 'acquired'
261+ } ,
262+ { staleMs : 10_000 } ,
263+ )
235264
236265 expect ( release ) . toBe ( 'acquired' )
237266 expect ( existsSync ( lockPath ) ) . toBe ( false )
@@ -243,10 +272,12 @@ describe('process-lock', () => {
243272
244273 try {
245274 // Attempt to acquire with low retry count should fail.
246- await expect ( processLock . acquire ( lockPath , {
247- retries : 1 ,
248- baseDelayMs : 10 ,
249- } ) ) . rejects . toThrow ( )
275+ await expect (
276+ processLock . acquire ( lockPath , {
277+ retries : 1 ,
278+ baseDelayMs : 10 ,
279+ } ) ,
280+ ) . rejects . toThrow ( )
250281 } finally {
251282 release1 ( )
252283 }
@@ -291,16 +322,20 @@ describe('process-lock', () => {
291322 // Use a path we definitely can't write to.
292323 const invalidPath = '/root/socket-test-lock-no-permission'
293324
294- await expect ( processLock . acquire ( invalidPath , {
295- retries : 1 ,
296- baseDelayMs : 10 ,
297- } ) ) . rejects . toThrow ( )
325+ await expect (
326+ processLock . acquire ( invalidPath , {
327+ retries : 1 ,
328+ baseDelayMs : 10 ,
329+ } ) ,
330+ ) . rejects . toThrow ( )
298331 } )
299332
300333 it ( 'should clean up on errors' , async ( ) => {
301- await expect ( processLock . withLock ( lockPath , async ( ) => {
302- throw new Error ( 'Intentional error' )
303- } ) ) . rejects . toThrow ( 'Intentional error' )
334+ await expect (
335+ processLock . withLock ( lockPath , async ( ) => {
336+ throw new Error ( 'Intentional error' )
337+ } ) ,
338+ ) . rejects . toThrow ( 'Intentional error' )
304339
305340 // Lock should be cleaned up.
306341 expect ( existsSync ( lockPath ) ) . toBe ( false )
0 commit comments