Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] [issue 877] Fix ctx in partitionProducer.Send() is not performing as expected #1053

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,13 @@ func (p *partitionProducer) Send(ctx context.Context, msg *ProducerMessage) (Mes
}, true)

// wait for send request to finish
<-doneCh

return msgID, err
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-doneCh:
// send request has been finished
return msgID, err
}
}

func (p *partitionProducer) SendAsync(ctx context.Context, msg *ProducerMessage,
Expand Down
26 changes: 26 additions & 0 deletions pulsar/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,3 +2024,29 @@ func testSendMessagesWithMetadata(t *testing.T, disableBatch bool) {
assert.Equal(t, msg.OrderingKey, recvMsg.OrderingKey())
assert.Equal(t, msg.Properties, recvMsg.Properties())
}

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

topicName := newTopicName()
// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topicName,
DisableBatching: true,
})
assert.Nil(t, err)
defer producer.Close()

ctx, cancel := context.WithCancel(context.Background())
// Make ctx be canceled to invalidate the context immediately
cancel()
_, err = producer.Send(ctx, &ProducerMessage{
Payload: make([]byte, 1024*1024),
})
// producer.Send should fail and return err context.Canceled
assert.True(t, errors.Is(err, context.Canceled))
}