@@ -183,12 +183,12 @@ describe('Test baseQueueHandler', function () {
183183 DemoHandler . prototype . afterDlq = originalAfterDlq ;
184184 } ) ;
185185
186- it ( 'should add string to dlq because afterDlq throws error' , async function ( ) {
187- sandbox . useFakeTimers ( ) ;
186+ it ( 'should add raw buffer to dlq when afterDlq throws to prevent double-encoding' , async function ( ) {
188187 const handler = new DemoHandler ( this . name , rabbit , {
189188 retries : 2 ,
190- retryDelay : 100
189+ retryDelay : 10
191190 } ) ;
191+ await handler . created ;
192192 handler . handle = sandbox . spy ( ( ) => {
193193 throw new Error ( 'test error' ) ;
194194 } ) ;
@@ -198,18 +198,28 @@ describe('Test baseQueueHandler', function () {
198198 throw new Error ( 'test error' ) ;
199199 }
200200 } ) ) ;
201+
202+ const dlqMessages : any [ ] = [ ] ;
203+ await rabbit . subscribe ( this . name + '_dlq' , ( msg , ack ) => {
204+ dlqMessages . push ( { event : JSON . parse ( msg . content . toString ( ) ) , content : msg . content } ) ;
205+ ack ( null ) ;
206+ } ) ;
207+
201208 await rabbit . publish ( this . name , { test : 'data' } , { correlationId : '3' } ) ;
202- const publish = ( handler . rabbit . publish = sandbox . spy ( handler . rabbit , 'publish' ) ) ;
203- sandbox . clock . tick ( 100 ) ;
204- await rabbit . connected ;
205- sandbox . clock . tick ( 100 ) ;
206- await rabbit . connected ;
207- sandbox . clock . tick ( 100 ) ;
208- sandbox . clock . restore ( ) ;
209- await new Promise ( resolve => setTimeout ( resolve , 400 ) ) ;
209+ const publish = sandbox . spy ( handler . rabbit , 'publish' ) ;
210+ await new Promise ( resolve => setTimeout ( resolve , 300 ) ) ;
211+
210212 afterDlq . calledOnce . should . be . true ( ) ;
211213 publish . calledTwice . should . be . true ( ) ;
212- publish . args [ publish . callCount - 1 ] [ 1 ] . should . eql ( '{"test":"data"}' ) ;
214+ const fallbackPayload = publish . args [ publish . callCount - 1 ] [ 1 ] ;
215+ Buffer . isBuffer ( fallbackPayload ) . should . be . true ( ) ;
216+ fallbackPayload . toString ( ) . should . eql ( '{"test":"data"}' ) ;
217+
218+ // TODO: This flow also produces duplicate messages on the DLQ,
219+ // to be investigated and handled on the next PR
220+ dlqMessages . length . should . equal ( 2 ) ;
221+ dlqMessages [ 0 ] . event . should . eql ( { test : 'data' } ) ;
222+ dlqMessages [ 1 ] . event . should . eql ( { test : 'data' } ) ;
213223 } ) ;
214224
215225 it ( 'should add to dlq after x retries and get error response even if afterDlq throws error' , async function ( ) {
0 commit comments