@@ -6,19 +6,20 @@ 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
1314 const lock1 = locker . newLock ( lockId )
1415 const lock2 = locker . newLock ( lockId )
1516
16- await lock1 . lock ( async ( ) => {
17+ await lock1 . lock ( abortController . signal , async ( ) => {
1718 await lock1 . unlock ( )
1819 cancel ( )
1920 } )
2021
21- await lock2 . lock ( async ( ) => {
22+ await lock2 . lock ( abortController . signal , async ( ) => {
2223 cancel2 ( )
2324 } )
2425
@@ -32,19 +33,21 @@ describe('MemoryLocker', () => {
3233 const locker = new MemoryLocker ( {
3334 acquireLockTimeout : 500 ,
3435 } )
36+ const abortController = new AbortController ( )
37+
3538 const lockId = 'upload-id-1'
3639 const lock = locker . newLock ( lockId )
3740
3841 const cancel = sinon . spy ( )
3942
40- await lock . lock ( async ( ) => {
43+ await lock . lock ( abortController . signal , async ( ) => {
4144 cancel ( )
4245 // We note that the function has been called, but do not
4346 // release the lock
4447 } )
4548
4649 try {
47- await lock . lock ( async ( ) => {
50+ await lock . lock ( abortController . signal , async ( ) => {
4851 throw new Error ( 'panic should not be called' )
4952 } )
5053 } catch ( e ) {
@@ -57,18 +60,20 @@ describe('MemoryLocker', () => {
5760 it ( 'request lock and unlock' , async ( ) => {
5861 const locker = new MemoryLocker ( )
5962 const lockId = 'upload-id-1'
63+ const abortController = new AbortController ( )
64+
6065 const lock = locker . newLock ( lockId )
6166 const lock2 = locker . newLock ( lockId )
6267
6368 const cancel = sinon . spy ( )
64- await lock . lock ( ( ) => {
69+ await lock . lock ( abortController . signal , ( ) => {
6570 cancel ( )
6671 setTimeout ( async ( ) => {
6772 await lock . unlock ( )
6873 } , 50 )
6974 } )
7075
71- await lock2 . lock ( ( ) => {
76+ await lock2 . lock ( abortController . signal , ( ) => {
7277 throw new Error ( 'should not be called' )
7378 } )
7479
@@ -79,4 +84,38 @@ 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 )
97+ const lock2 = locker . newLock ( lockId )
98+
99+ await lock1 . lock ( abortController . signal , 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 ( abortController . signal , 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+ assert ( cancel . callCount > 1 , `calls count dont match ${ cancel . callCount } !== 1` )
119+ assert ( cancel2 . callCount === 0 , `calls count dont match ${ cancel . callCount } !== 1` )
120+ } )
82121} )
0 commit comments