2323 */
2424import { logger } from './mocks' ;
2525import { expect } from 'chai' ;
26- import { RedisQueue , uuid } from '../src' ;
26+ import { RedisQueue , uuid , IMQMode } from '../src' ;
2727import Redis from 'ioredis' ;
2828
2929process . setMaxListeners ( 100 ) ;
@@ -43,13 +43,16 @@ describe('RedisQueue', function() {
4343 } ) ;
4444
4545 describe ( 'constructor()' , ( ) => {
46- it ( 'should not throw' , ( ) => {
47- expect ( ( ) => new ( < any > RedisQueue ) ( ) ) . not . to . throw ( Error ) ;
48- expect ( ( ) => new RedisQueue ( 'IMQUnitTests' ) ) . not . to . throw ( Error ) ;
49- expect ( ( ) => new RedisQueue ( 'IMQUnitTests' , { } ) )
46+ it ( 'should not throw' , async ( ) => {
47+ const instances : RedisQueue [ ] = [ ] ;
48+ expect ( ( ) => instances . push ( new ( < any > RedisQueue ) ( ) ) ) . not . to . throw ( Error ) ;
49+ expect ( ( ) => instances . push ( new RedisQueue ( 'IMQUnitTests' ) ) ) . not . to . throw ( Error ) ;
50+ expect ( ( ) => instances . push ( new RedisQueue ( 'IMQUnitTests' , { } ) ) )
5051 . not . to . throw ( Error ) ;
51- expect ( ( ) => new RedisQueue ( 'IMQUnitTests' , { useGzip : true } ) )
52+ expect ( ( ) => instances . push ( new RedisQueue ( 'IMQUnitTests' , { useGzip : true } ) ) )
5253 . not . to . throw ( Error ) ;
54+
55+ await Promise . all ( instances . map ( instance => instance . destroy ( ) ) ) ;
5356 } ) ;
5457 } ) ;
5558
@@ -58,6 +61,7 @@ describe('RedisQueue', function() {
5861 const rq = new ( < any > RedisQueue ) ( ) ;
5962 try { await rq . start ( ) }
6063 catch ( err ) { expect ( err ) . to . be . instanceof ( TypeError ) }
64+ rq . destroy ( ) . catch ( ) ;
6165 } ) ;
6266
6367 it ( 'should create reader connection' , async ( ) => {
@@ -106,7 +110,7 @@ describe('RedisQueue', function() {
106110 await rq . start ( ) ;
107111 } catch ( err ) { passed = false }
108112 expect ( passed ) . to . be . true ;
109- rq . destroy ( ) . catch ( ) ;
113+ await rq . destroy ( ) ;
110114 } ) ;
111115 } ) ;
112116
@@ -240,4 +244,99 @@ describe('RedisQueue', function() {
240244 } ) ;
241245 } ) ;
242246
247+ describe ( 'processCleanup()' , ( ) => {
248+ it ( 'should perform cleanup when cleanup option is enabled' , async ( ) => {
249+ const rq : any = new RedisQueue ( uuid ( ) , {
250+ logger,
251+ cleanup : true ,
252+ cleanupFilter : 'test*'
253+ } ) ;
254+ await rq . start ( ) ;
255+
256+ // Call processCleanup directly
257+ const result = await rq . processCleanup ( ) ;
258+ expect ( result ) . to . equal ( rq ) ;
259+
260+ await rq . destroy ( ) ;
261+ } ) ;
262+
263+ it ( 'should return early when cleanup option is disabled' , async ( ) => {
264+ const rq : any = new RedisQueue ( uuid ( ) , {
265+ logger,
266+ cleanup : false
267+ } ) ;
268+ await rq . start ( ) ;
269+
270+ const result = await rq . processCleanup ( ) ;
271+ expect ( result ) . to . be . undefined ;
272+
273+ await rq . destroy ( ) ;
274+ } ) ;
275+ } ) ;
276+
277+ describe ( 'lock/unlock methods' , ( ) => {
278+ it ( 'should handle lock/unlock when writer is null' , async ( ) => {
279+ const rq : any = new RedisQueue ( uuid ( ) , { logger } ) ;
280+ // Don't start, so writer will be null
281+
282+ const lockResult = await rq . lock ( ) ;
283+ expect ( lockResult ) . to . be . false ;
284+
285+ const unlockResult = await rq . unlock ( ) ;
286+ expect ( unlockResult ) . to . be . false ;
287+
288+ const isLockedResult = await rq . isLocked ( ) ;
289+ expect ( isLockedResult ) . to . be . false ;
290+
291+ await rq . destroy ( ) ;
292+ } ) ;
293+
294+ it ( 'should handle lock/unlock operations' , async ( ) => {
295+ const rq : any = new RedisQueue ( uuid ( ) , { logger } ) ;
296+ await rq . start ( ) ;
297+
298+ // Test locking
299+ const lockResult = await rq . lock ( ) ;
300+ expect ( lockResult ) . to . be . a ( 'boolean' ) ;
301+
302+ // Test checking if locked
303+ const isLockedResult = await rq . isLocked ( ) ;
304+ expect ( isLockedResult ) . to . be . a ( 'boolean' ) ;
305+
306+ // Test unlocking
307+ const unlockResult = await rq . unlock ( ) ;
308+ expect ( unlockResult ) . to . be . a ( 'boolean' ) ;
309+
310+ await rq . destroy ( ) ;
311+ } ) ;
312+ } ) ;
313+
314+ describe ( 'utility methods' , ( ) => {
315+ it ( 'should test isPublisher and isWorker methods' , async ( ) => {
316+ const publisherQueue = new RedisQueue ( uuid ( ) , { logger } , IMQMode . PUBLISHER ) ;
317+ const workerQueue = new RedisQueue ( uuid ( ) , { logger } , IMQMode . WORKER ) ;
318+
319+ expect ( publisherQueue . isPublisher ( ) ) . to . be . true ;
320+ expect ( publisherQueue . isWorker ( ) ) . to . be . false ;
321+
322+ expect ( workerQueue . isPublisher ( ) ) . to . be . false ;
323+ expect ( workerQueue . isWorker ( ) ) . to . be . true ;
324+
325+ await workerQueue . destroy ( ) ;
326+ await publisherQueue . destroy ( ) ;
327+ } ) ;
328+
329+ it ( 'should test key and lockKey methods' , async ( ) => {
330+ const name = uuid ( ) ;
331+ const rq : any = new RedisQueue ( name , { logger } ) ;
332+
333+ expect ( rq . key ) . to . be . a ( 'string' ) ;
334+ expect ( rq . key ) . to . include ( name ) ;
335+
336+ expect ( rq . lockKey ) . to . be . a ( 'string' ) ;
337+ expect ( rq . lockKey ) . to . include ( 'watch:lock' ) ;
338+
339+ await rq . destroy ( ) ;
340+ } ) ;
341+ } ) ;
243342} ) ;
0 commit comments