@@ -45,4 +45,58 @@ describe("RunLocker", () => {
4545 await redis . quit ( ) ;
4646 }
4747 } ) ;
48+
49+ redisTest (
50+ "Test lock throws when callback throws" ,
51+ { timeout : 15_000 } ,
52+ async ( { redisOptions } ) => {
53+ const redis = createRedisClient ( redisOptions ) ;
54+ try {
55+ const runLock = new RunLocker ( { redis } ) ;
56+
57+ expect ( runLock . isInsideLock ( ) ) . toBe ( false ) ;
58+
59+ await expect (
60+ runLock . lock ( [ "test-1" ] , 5000 , async ( ) => {
61+ throw new Error ( "Test error" ) ;
62+ } )
63+ ) . rejects . toThrow ( "Test error" ) ;
64+
65+ // Verify the lock was released
66+ expect ( runLock . isInsideLock ( ) ) . toBe ( false ) ;
67+ } finally {
68+ await redis . quit ( ) ;
69+ }
70+ }
71+ ) ;
72+
73+ redisTest (
74+ "Test nested lock throws when inner callback throws" ,
75+ { timeout : 15_000 } ,
76+ async ( { redisOptions } ) => {
77+ const redis = createRedisClient ( redisOptions ) ;
78+ try {
79+ const runLock = new RunLocker ( { redis } ) ;
80+
81+ expect ( runLock . isInsideLock ( ) ) . toBe ( false ) ;
82+
83+ await expect (
84+ runLock . lock ( [ "test-1" ] , 5000 , async ( ) => {
85+ expect ( runLock . isInsideLock ( ) ) . toBe ( true ) ;
86+
87+ // Nested lock with same resource
88+ await runLock . lock ( [ "test-1" ] , 5000 , async ( ) => {
89+ expect ( runLock . isInsideLock ( ) ) . toBe ( true ) ;
90+ throw new Error ( "Inner lock error" ) ;
91+ } ) ;
92+ } )
93+ ) . rejects . toThrow ( "Inner lock error" ) ;
94+
95+ // Verify all locks were released
96+ expect ( runLock . isInsideLock ( ) ) . toBe ( false ) ;
97+ } finally {
98+ await redis . quit ( ) ;
99+ }
100+ }
101+ ) ;
48102} ) ;
0 commit comments