Skip to content

Commit

Permalink
[Issue 1259][producer] Prevent panic when calling Flush on closed pro…
Browse files Browse the repository at this point in the history
…ducer (#1260)

(cherry picked from commit fb805c0)
  • Loading branch information
Gilthoniel authored and RobertIndie committed Jul 31, 2024
1 parent b6df55c commit 53fc938
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,10 @@ func (p *partitionProducer) Flush() error {
}

func (p *partitionProducer) FlushWithCtx(ctx context.Context) error {
if p.getProducerState() != producerReady {
return ErrProducerClosed
}

flushReq := &flushRequest{
doneCh: make(chan struct{}),
err: nil,
Expand Down
15 changes: 15 additions & 0 deletions pulsar/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ func TestFlushInPartitionedProducer(t *testing.T) {
assert.Equal(t, msgCount, numOfMessages/2)
}

func TestProducerReturnsErrorOnFlushWhenClosed(t *testing.T) {
client, err := NewClient(ClientOptions{URL: serviceURL})
assert.NoError(t, err)
defer client.Close()

producer, err := client.CreateProducer(ProducerOptions{Topic: newTopicName()})
assert.NoError(t, err)
assert.NotNil(t, producer)

producer.Close()

err = producer.FlushWithCtx(context.Background())
assert.Error(t, err)
}

func TestRoundRobinRouterPartitionedProducer(t *testing.T) {
topicName := "public/default/partition-testRoundRobinRouterPartitionedProducer"
numberOfPartitions := 5
Expand Down

0 comments on commit 53fc938

Please sign in to comment.