@@ -228,10 +228,8 @@ func TestRPC(t *testing.T) {
228228}
229229
230230func TestDeadlettering (t * testing.T ) {
231- rejectedMessages , err := metrics .GetRejectedMessagesValue ()
232- if err != nil {
233- t .Error ("failed to get rejected messages value" )
234- }
231+ metrics .ResetRejectedMessagesCounter ()
232+
235233 proceed := make (chan bool )
236234 poison := gbus .NewBusMessage (PoisonMessage {})
237235 service1 := createNamedBusForTest (testSvc1 )
@@ -259,7 +257,7 @@ func TestDeadlettering(t *testing.T) {
259257
260258 <- proceed
261259 count , _ := metrics .GetRejectedMessagesValue ()
262- if count != rejectedMessages + 1 {
260+ if count != 1 {
263261 t .Error ("Should have one rejected message" )
264262 }
265263
@@ -348,11 +346,7 @@ func TestReturnDeadToQueue(t *testing.T) {
348346
349347func TestDeadLetterHandlerPanic (t * testing.T ) {
350348 proceed := make (chan bool )
351- rejectedMessages , err := metrics .GetRejectedMessagesValue ()
352- if err != nil {
353- t .Error ("failed to get rejected messages value" )
354- }
355-
349+ metrics .ResetRejectedMessagesCounter ()
356350 poison := gbus .NewBusMessage (Command1 {})
357351 service1 := createBusWithConfig (testSvc1 , "grabbit-dead" , true , true ,
358352 gbus.BusConfiguration {MaxRetryCount : 0 , BaseRetryDuration : 0 })
@@ -361,6 +355,14 @@ func TestDeadLetterHandlerPanic(t *testing.T) {
361355 gbus.BusConfiguration {MaxRetryCount : 0 , BaseRetryDuration : 0 })
362356 visited := false
363357 deadMessageHandler := func (tx * sql.Tx , poison * amqp.Delivery ) error {
358+ /*
359+ this handler will be called more than once since when grabbit rejects
360+ a message from a deadletter queue to rejects it with the requeu option set to
361+ true and that is why this will be called more than once even though the retry count
362+ is set to 0
363+
364+ */
365+
364366 if ! visited {
365367 visited = true
366368 panic ("PANIC DEAD HANDLER aaahhh!!!!!!" )
@@ -374,7 +376,7 @@ func TestDeadLetterHandlerPanic(t *testing.T) {
374376 }
375377
376378 deadletterSvc .HandleDeadletter (deadMessageHandler )
377- err = service1 .HandleMessage (Command1 {}, faultyHandler )
379+ err : = service1 .HandleMessage (Command1 {}, faultyHandler )
378380 if err != nil {
379381 t .Error ("failed to register faultyhandler" )
380382 }
@@ -388,8 +390,12 @@ func TestDeadLetterHandlerPanic(t *testing.T) {
388390 select {
389391 case <- proceed :
390392 count , _ := metrics .GetRejectedMessagesValue ()
391- if count != rejectedMessages + 2 {
392- t .Error ("Should have 2 rejected messages" )
393+ //we expect only 1 rejcted meessage from the counter since rejected messages that get
394+ //requeued are not reported to the metric so the counter won't be increment when the message
395+ //in the dlq gets rejected as it is rejected with the requeue option set to true
396+ if count != 1 {
397+
398+ t .Errorf ("Should have 1 rejected messages but was %v" , count )
393399 }
394400 case <- time .After (2 * time .Second ):
395401 t .Fatal ("timeout, dlq failed to reject message after handler panicked" )
0 commit comments