Skip to content

Commit

Permalink
check multiple topics in one batch (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
maoruilei3120 committed Nov 30, 2021
1 parent a8b10ce commit aae593e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ var (
ErrMessageEmpty = errors.New("message is nil")
ErrNotRunning = errors.New("producer not started")
ErrPullConsumer = errors.New("pull consumer has not supported")
ErrMultipleTopics = errors.New("the topic of the messages in one batch should be the same")
)
8 changes: 8 additions & 0 deletions producer/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func (p *defaultProducer) checkMsg(msgs ...*primitive.Message) error {
if len(msgs[0].Topic) == 0 {
return errors2.ErrTopicEmpty
}

topic := msgs[0].Topic
for _, msg := range msgs {
if msg.Topic != topic {
return errors2.ErrMultipleTopics
}
}

return nil
}

Expand Down
34 changes: 34 additions & 0 deletions producer/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,37 @@ func TestSyncWithNamespace(t *testing.T) {
assert.Equal(t, expectedResp, resp)
assert.Equal(t, namespaceTopic, msg.Topic)
}

func TestBatchSendDifferentTopics(t *testing.T) {
p, _ := NewDefaultProducer(
WithNsResolver(primitive.NewPassthroughResolver([]string{"127.0.0.1:9876"})),
WithRetry(2),
WithQueueSelector(NewManualQueueSelector()),
)

ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := internal.NewMockRMQClient(ctrl)
p.client = client

client.EXPECT().RegisterProducer(gomock.Any(), gomock.Any()).Return()
client.EXPECT().Start().Return()
err := p.Start()
assert.Nil(t, err)

ctx := context.Background()
msgToA := &primitive.Message{
Topic: "topic-A",
Body: []byte("this is a message body"),
}

msgToB := &primitive.Message{
Topic: "topic-B",
Body: []byte("this is a message body"),
}

resp, err := p.SendSync(ctx, []*primitive.Message{msgToA, msgToB}...)
assert.Nil(t, resp)
assert.NotNil(t, err)
assert.Equal(t, err, errors.ErrMultipleTopics)
}

0 comments on commit aae593e

Please sign in to comment.