Skip to content

Commit

Permalink
Permit setting version on mock produce response
Browse files Browse the repository at this point in the history
This allows setting the version of the message data struct for
MockProduceResponse. This change is very similar to what was already
done in pull request IBM#939

In order to mock a "produce" request for Kafka version 0.10.2.0 you
would use the following:

    leader.SetHandlerByMap(map[string]MockResponse{
    	"ProduceRequest": NewMockProduceResponse(t).
    		SetVersion(2).
    		SetError("my_topic", 0, ErrNoError),
    })
  • Loading branch information
fgrosse committed Jan 4, 2018
1 parent f196330 commit 893be7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions async_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ func TestAsyncProducerFlusherRetryCondition(t *testing.T) {

leader.SetHandlerByMap(map[string]MockResponse{
"ProduceRequest": NewMockProduceResponse(t).
SetVersion(0).
SetError("my_topic", 0, ErrNoError),
})

Expand Down
10 changes: 9 additions & 1 deletion mockresponses.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func (mr *MockOffsetCommitResponse) getError(group, topic string, partition int3

// MockProduceResponse is a `ProduceResponse` builder.
type MockProduceResponse struct {
version int16
errors map[string]map[int32]KError
t TestReporter
}
Expand All @@ -392,6 +393,11 @@ func NewMockProduceResponse(t TestReporter) *MockProduceResponse {
return &MockProduceResponse{t: t}
}

func (mr *MockProduceResponse) SetVersion(version int16) *MockProduceResponse {
mr.version = version
return mr
}

func (mr *MockProduceResponse) SetError(topic string, partition int32, kerror KError) *MockProduceResponse {
if mr.errors == nil {
mr.errors = make(map[string]map[int32]KError)
Expand All @@ -407,7 +413,9 @@ func (mr *MockProduceResponse) SetError(topic string, partition int32, kerror KE

func (mr *MockProduceResponse) For(reqBody versionedDecoder) encoder {
req := reqBody.(*ProduceRequest)
res := &ProduceResponse{}
res := &ProduceResponse{
Version: mr.version,
}
for topic, partitions := range req.records {
for partition := range partitions {
res.AddTopicPartition(topic, partition, mr.getError(topic, partition))
Expand Down

0 comments on commit 893be7d

Please sign in to comment.