Skip to content

Commit 660ff9a

Browse files
author
Guy Baron
authored
fixing issues with invoking the GlobalrawMessageHandler (#189)
Fixing the following issues: #187 #188
1 parent 861f9d8 commit 660ff9a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

gbus/worker.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ func (worker *worker) processMessage(delivery amqp.Delivery, isRPCreply bool) {
290290

291291
if err := worker.runGlobalHandler(&delivery); err != nil {
292292
//when the global handler fails terminate executation and reject the message
293-
_ = worker.reject(true, delivery)
293+
_ = worker.reject(false, delivery)
294+
return
294295
}
295296

296297
//TODO:Dedup message

tests/bus_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,51 @@ func TestRawMessageHandling(t *testing.T) {
320320
proceedOrTimeout(2, proceed, nil, t)
321321
}
322322

323+
func TestGlobalRawMessageHandlingErr(t *testing.T) {
324+
metrics.ResetRejectedMessagesCounter()
325+
/*
326+
tests issues:
327+
https://github.com/wework/grabbit/issues/187
328+
https://github.com/wework/grabbit/issues/188
329+
*/
330+
var otherHandlerCalled bool
331+
332+
handler := func(tx *sql.Tx, delivery *amqp.Delivery) error {
333+
334+
return errors.New("other handlers should not be called")
335+
}
336+
337+
//this handler should not be invoked by the bus, if it does the test should fail
338+
otherHandler := func(invocation gbus.Invocation, message *gbus.BusMessage) error {
339+
340+
otherHandlerCalled = true
341+
return nil
342+
}
343+
svc1 := createNamedBusForTest(testSvc1)
344+
svc1.SetGlobalRawMessageHandler(handler)
345+
svc1.HandleMessage(Command1{}, otherHandler)
346+
_ = svc1.Start()
347+
defer assertBusShutdown(svc1, t)
348+
349+
cmd1 := gbus.NewBusMessage(Command1{})
350+
_ = svc1.Send(context.Background(), testSvc1, cmd1)
351+
352+
if otherHandlerCalled == true {
353+
t.Fail()
354+
}
355+
356+
//delay test execution so to make sure the second handler is not called
357+
time.Sleep(1500 * time.Millisecond)
358+
rejected, _ := metrics.GetRejectedMessagesValue()
359+
if rejected != 1 {
360+
t.Errorf("rejected messages metric was expected to be 1 but was %f", rejected)
361+
}
362+
363+
if otherHandlerCalled {
364+
t.Errorf("other handler that was not expected to be called was called ")
365+
}
366+
}
367+
323368
func TestReturnDeadToQueue(t *testing.T) {
324369

325370
var visited bool

0 commit comments

Comments
 (0)