@@ -10,7 +10,9 @@ import { delay } from '../../src/utils/index'
1010import { client1 as client } from '../redisClient'
1111import { downRedisServer , upRedisServer } from '../shell'
1212import {
13- catchUnhandledRejection , throwUnhandledRejection , unhandledRejectionSpy
13+ catchUnhandledRejection ,
14+ throwUnhandledRejection ,
15+ unhandledRejectionSpy
1416} from '../unhandledRejection'
1517
1618const timeoutOptions : TimeoutOptions = {
@@ -23,28 +25,28 @@ const timeoutOptions: TimeoutOptions = {
2325describe ( 'MultiSemaphore' , ( ) => {
2426 it ( 'should fail on invalid arguments' , ( ) => {
2527 expect (
26- ( ) => new MultiSemaphore ( ( null as unknown ) as Redis , 'key' , 5 , 2 )
28+ ( ) => new MultiSemaphore ( null as unknown as Redis , 'key' , 5 , 2 )
2729 ) . to . throw ( '"client" is required' )
2830 expect (
29- ( ) => new MultiSemaphore ( ( { } as unknown ) as Redis , 'key' , 5 , 2 )
31+ ( ) => new MultiSemaphore ( { } as unknown as Redis , 'key' , 5 , 2 )
3032 ) . to . throw ( '"client" must be instance of ioredis client' )
3133 expect ( ( ) => new MultiSemaphore ( client , '' , 5 , 2 ) ) . to . throw (
3234 '"key" is required'
3335 )
3436 expect (
35- ( ) => new MultiSemaphore ( client , ( 1 as unknown ) as string , 5 , 2 )
37+ ( ) => new MultiSemaphore ( client , 1 as unknown as string , 5 , 2 )
3638 ) . to . throw ( '"key" must be a string' )
3739 expect ( ( ) => new MultiSemaphore ( client , 'key' , 0 , 2 ) ) . to . throw (
3840 '"limit" is required'
3941 )
4042 expect (
41- ( ) => new MultiSemaphore ( client , 'key' , ( '10' as unknown ) as number , 2 )
43+ ( ) => new MultiSemaphore ( client , 'key' , '10' as unknown as number , 2 )
4244 ) . to . throw ( '"limit" must be a number' )
4345 expect ( ( ) => new MultiSemaphore ( client , 'key' , 5 , 0 ) ) . to . throw (
4446 '"permits" is required'
4547 )
4648 expect (
47- ( ) => new MultiSemaphore ( client , 'key' , 5 , ( '2' as unknown ) as number )
49+ ( ) => new MultiSemaphore ( client , 'key' , 5 , '2' as unknown as number )
4850 ) . to . throw ( '"permits" must be a number' )
4951 } )
5052 it ( 'should acquire and release semaphore' , async ( ) => {
@@ -136,6 +138,25 @@ describe('MultiSemaphore', () => {
136138 . and . not . include ( ids1 [ 1 ] )
137139 . and . not . include ( ids1 [ 2 ] )
138140 } )
141+ it ( 'should support externally acquired semaphore' , async ( ) => {
142+ const externalSemaphore = new MultiSemaphore ( client , 'key' , 3 , 2 , {
143+ ...timeoutOptions ,
144+ refreshInterval : 0
145+ } )
146+ const localSemaphore = new MultiSemaphore ( client , 'key' , 3 , 2 , {
147+ ...timeoutOptions ,
148+ externallyAcquiredIdentifier : externalSemaphore . identifier
149+ } )
150+ await externalSemaphore . acquire ( )
151+ await localSemaphore . acquire ( )
152+ await delay ( 400 )
153+ expect ( await client . zrange ( 'semaphore:key' , 0 , - 1 ) ) . to . be . eql ( [
154+ localSemaphore . identifier + '_0' ,
155+ localSemaphore . identifier + '_1'
156+ ] )
157+ await localSemaphore . release ( )
158+ expect ( await client . zcard ( 'semaphore:key' ) ) . to . be . eql ( 0 )
159+ } )
139160 describe ( 'lost lock case' , ( ) => {
140161 beforeEach ( ( ) => {
141162 catchUnhandledRejection ( )
0 commit comments