@@ -7,11 +7,12 @@ import Exchange from '../ts/exchange';
77import * as ReplyQueue from '../ts/reply-queue' ;
88import * as DelayQueue from '../ts/delay-queue' ;
99import * as amqp from 'amqplib' ;
10+ import { randomUUID } from 'crypto' ;
1011
1112const sandbox = sinon . createSandbox ( ) ;
1213
1314describe ( 'Test rabbit class' , function ( ) {
14- let rabbit : Rabbit ;
15+ let rabbit : Rabbit | null ;
1516
1617 before ( function ( ) {
1718 this . url = process . env . RABBIT_URL || 'amqp://localhost' ;
@@ -364,4 +365,32 @@ describe('Test rabbit class', function() {
364365 await rabbit . destroyQueue ( this . name ) ;
365366 stub . calledWith ( rabbit . consumeChannel , `test_${ this . name } ` ) . should . be . true ( ) ;
366367 } ) ;
368+
369+ it ( 'should register sigtermHandler in SIGTERM process listeners and remove on close' , async function ( ) {
370+ rabbit = new Rabbit ( this . url ) ;
371+ await rabbit . connected ;
372+
373+ const handler = ( rabbit as any ) . sigtermHandler ;
374+
375+ process . listeners ( 'SIGTERM' ) . should . containEql ( handler ) ;
376+
377+ rabbit . close ( ) ;
378+ rabbit = null ;
379+
380+ process . listeners ( 'SIGTERM' ) . should . not . containEql ( handler ) ;
381+ } ) ;
382+
383+ it ( 'should call unsubscribe for all queues on SIGTERM' , async function ( ) {
384+ const stub = sandbox . stub ( Queue . prototype , 'unsubscribe' ) . resolves ( ) ;
385+
386+ rabbit = new Rabbit ( this . url ) ;
387+ await rabbit . connected ;
388+
389+ const handler = ( ) => ( { } ) ;
390+ await rabbit . createQueue ( randomUUID ( ) . toString ( ) , { exclusive : true } , handler ) ;
391+ await rabbit . createQueue ( randomUUID ( ) . toString ( ) , { exclusive : true } , handler ) ;
392+
393+ ( rabbit as any ) . sigtermHandler ( ) ;
394+ stub . calledTwice . should . be . true ( ) ;
395+ } ) ;
367396} ) ;
0 commit comments