Skip to content

Commit 3cc55ae

Browse files
adiweissGuy Baron
adiweiss
authored and
Guy Baron
committed
Fix handle empty body (#156)
1 parent 1a33290 commit 3cc55ae

6 files changed

+98
-7
lines changed

gbus/worker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (worker *worker) processMessage(delivery amqp.Delivery, isRPCreply bool) {
306306
return
307307
}
308308

309-
if delivery.Body == nil || len(delivery.Body) == 0 {
309+
if delivery.Body == nil {
310310
worker.log().
311311
WithFields(
312312
logrus.Fields{"message-name": msgName}).

tests/bus_test.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/wework/grabbit/gbus/serialization"
13+
1214
"github.com/wework/grabbit/gbus/metrics"
1315

1416
"github.com/opentracing/opentracing-go"
@@ -562,9 +564,9 @@ func TestEmptyMessageInvokesDeadHanlder(t *testing.T) {
562564
proceedOrTimeout(2, proceed, nil, t)
563565
}
564566

565-
func TestFailHandlerInvokeOfMessageWithEmptyBody(t *testing.T) {
567+
func TestOnlyRawMessageHandlersInvoked(t *testing.T) {
566568
/*
567-
The global and dead letter handlers can consume message with 0 or nil body but
569+
The global and dead letter handlers can consume message with nil body but
568570
"normal" handlers cannot.
569571
If a "normal" handler is registered for this type of message, the bus must reject this message.
570572
*/
@@ -619,6 +621,48 @@ func TestFailHandlerInvokeOfMessageWithEmptyBody(t *testing.T) {
619621
}, t)
620622
}
621623

624+
func TestSendEmptyBody(t *testing.T) {
625+
/*
626+
test sending of message with len(payload) == 0 .
627+
for example, the body of proto message with 1 "false" field is len 0.
628+
*/
629+
630+
logger := log.WithField("test", "empty_body")
631+
serializer := serialization.NewProtoSerializer(logger)
632+
msg := EmptyProtoCommand{}
633+
cmd := gbus.NewBusMessage(&msg)
634+
proceed := make(chan bool)
635+
636+
cfgSerializer := func(builder gbus.Builder) {
637+
builder.WithSerializer(serializer)
638+
}
639+
b := createBusWithConfig(testSvc1, "grabbit-dead", true, true,
640+
gbus.BusConfiguration{MaxRetryCount: 0, BaseRetryDuration: 0}, cfgSerializer)
641+
642+
handler := func(invocation gbus.Invocation, message *gbus.BusMessage) error {
643+
proceed <- true
644+
return nil
645+
}
646+
647+
err := b.HandleMessage(&EmptyProtoCommand{}, handler)
648+
if err != nil {
649+
t.Errorf("Registering handler returned false, expected true with error: %s", err.Error())
650+
}
651+
652+
err = b.Start()
653+
if err != nil {
654+
t.Errorf("could not start bus for test error: %s", err.Error())
655+
}
656+
defer assertBusShutdown(b, t)
657+
658+
err = b.Send(noopTraceContext(), testSvc1, cmd)
659+
if err != nil {
660+
t.Errorf("could not send message error: %s", err.Error())
661+
return
662+
}
663+
proceedOrTimeout(2, proceed, nil, t)
664+
}
665+
622666
func TestHealthCheck(t *testing.T) {
623667
svc1 := createNamedBusForTest(testSvc1)
624668
err := svc1.Start()

tests/consts.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ func init() {
2222
testSvc4 = "test-svc4"
2323
}
2424

25-
func createBusWithConfig(svcName string, deadletter string, txnl, pos bool, conf gbus.BusConfiguration) gbus.Bus {
25+
type configBilder func(builder gbus.Builder)
26+
27+
func createBusWithConfig(svcName string, deadletter string, txnl, pos bool, conf gbus.BusConfiguration, cf ...configBilder) gbus.Bus {
2628
busBuilder := builder.
2729
New().
2830
Bus(connStr).
@@ -41,6 +43,10 @@ func createBusWithConfig(svcName string, deadletter string, txnl, pos bool, conf
4143
busBuilder = busBuilder.PurgeOnStartUp()
4244
}
4345

46+
for _, c := range cf {
47+
c(busBuilder)
48+
}
49+
4450
return busBuilder.Build(svcName)
4551
}
4652

tests/protoMessages.pb.go

+36-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/protoMessages.proto

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ message ProtoCommand {
55
int64 some_number = 1;
66
string some_data = 2;
77
}
8+
9+
10+
message EmptyProtoCommand {
11+
}

tests/protoMessagesBase.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ package tests
44
func (*ProtoCommand) SchemaName() string {
55
return "ProtoCommand"
66
}
7+
8+
func (*EmptyProtoCommand) SchemaName() string {
9+
return "EmptyProtoCommand"
10+
}

0 commit comments

Comments
 (0)