Skip to content

Commit 37e50db

Browse files
fix topic while publishing
1 parent bd387a5 commit 37e50db

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

producer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (p *Producer) MultiPublish(message [][]byte) (err error) {
179179
// first unsuccessful attempt to publish the message. It is the responsibility
180180
// of the caller to retry if necessary.
181181
func (p *Producer) PublishTo(topic string, message []byte) (err error) {
182-
return p.MultiPublishTo(p.topic, [][]byte{message})
182+
return p.MultiPublishTo(topic, [][]byte{message})
183183
}
184184

185185
// MultiPublishTo sends a message batch to a specific topic using the producer p, returning

producer_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,40 @@ func TestProducerBatch(t *testing.T) {
136136
})
137137
}
138138
}
139+
func TestProducer_PublishTo(t *testing.T) {
140+
141+
tests := []struct {
142+
name string
143+
topic string
144+
expectedError string
145+
stopProducer bool
146+
}{
147+
{name: "PublishesMessageToSpecifiedTopic", topic: "test-topic", expectedError: ""},
148+
{name: "FailsWhenPublishingToEmptyTopic", topic: "", expectedError: "topic cannot be empty"},
149+
{name: "FailsWhenProducerIsStopped", topic: "test-topic", expectedError: "producer is stopped", stopProducer: true},
150+
}
151+
152+
for _, test := range tests {
153+
t.Run(test.name, func(t *testing.T) {
154+
p, _ := StartProducer(ProducerConfig{
155+
Address: "localhost:4150",
156+
MaxConcurrency: 1,
157+
})
158+
if test.stopProducer {
159+
p.Stop()
160+
} else {
161+
defer p.Stop()
162+
}
163+
message := []byte("test-message")
164+
err := p.PublishTo(test.topic, message)
165+
166+
if err != nil && test.expectedError == "" {
167+
t.Errorf("expected no error, got %v", err)
168+
}
169+
170+
if err == nil && test.expectedError != "" {
171+
t.Errorf("expected error %v, got nil", test.expectedError)
172+
}
173+
})
174+
}
175+
}

0 commit comments

Comments
 (0)