@@ -6,12 +6,13 @@ describe('MemoryLocker', () => {
66 it ( 'will acquire a lock by notifying another to release it' , async ( ) => {
77 const locker = new MemoryLocker ( )
88 const lockId = 'upload-id-1'
9+ const abortController = new AbortController ( )
910
1011 const cancel = sinon . spy ( )
1112 const cancel2 = sinon . spy ( )
1213
13- const lock1 = locker . newLock ( lockId )
14- const lock2 = locker . newLock ( lockId )
14+ const lock1 = locker . newLock ( lockId , abortController . signal )
15+ const lock2 = locker . newLock ( lockId , abortController . signal )
1516
1617 await lock1 . lock ( async ( ) => {
1718 await lock1 . unlock ( )
@@ -32,8 +33,10 @@ describe('MemoryLocker', () => {
3233 const locker = new MemoryLocker ( {
3334 acquireLockTimeout : 500 ,
3435 } )
36+ const abortController = new AbortController ( )
37+
3538 const lockId = 'upload-id-1'
36- const lock = locker . newLock ( lockId )
39+ const lock = locker . newLock ( lockId , abortController . signal )
3740
3841 const cancel = sinon . spy ( )
3942
@@ -57,8 +60,10 @@ describe('MemoryLocker', () => {
5760 it ( 'request lock and unlock' , async ( ) => {
5861 const locker = new MemoryLocker ( )
5962 const lockId = 'upload-id-1'
60- const lock = locker . newLock ( lockId )
61- const lock2 = locker . newLock ( lockId )
63+ const abortController = new AbortController ( )
64+
65+ const lock = locker . newLock ( lockId , abortController . signal )
66+ const lock2 = locker . newLock ( lockId , abortController . signal )
6267
6368 const cancel = sinon . spy ( )
6469 await lock . lock ( ( ) => {
@@ -79,4 +84,39 @@ describe('MemoryLocker', () => {
7984 `request released called more times than expected - ${ cancel . callCount } `
8085 )
8186 } )
87+
88+ it ( 'will stop trying to acquire the lock if the abort signal is aborted' , async ( ) => {
89+ const locker = new MemoryLocker ( )
90+ const lockId = 'upload-id-1'
91+ const abortController = new AbortController ( )
92+
93+ const cancel = sinon . spy ( )
94+ const cancel2 = sinon . spy ( )
95+
96+ const lock1 = locker . newLock ( lockId , abortController . signal )
97+ const lock2 = locker . newLock ( lockId , abortController . signal )
98+
99+ await lock1 . lock ( async ( ) => {
100+ // do not unlock when requested
101+ cancel ( )
102+ } )
103+
104+ // Abort signal is aborted after lock2 tries to acquire the lock
105+ setTimeout ( ( ) => {
106+ abortController . abort ( )
107+ } , 100 )
108+
109+ try {
110+ await lock2 . lock ( async ( ) => {
111+ cancel2 ( )
112+ } )
113+ assert ( false , 'lock2 should not have been acquired' )
114+ } catch ( e ) {
115+ assert ( e === ERRORS . ERR_LOCK_TIMEOUT , `error returned is not correct ${ e } ` )
116+ }
117+
118+
119+ assert ( cancel . callCount > 1 , `calls count dont match ${ cancel . callCount } !== 1` )
120+ assert ( cancel2 . callCount === 0 , `calls count dont match ${ cancel . callCount } !== 1` )
121+ } )
82122} )
0 commit comments