Skip to content

Commit

Permalink
fix(consumer): guard against nil client
Browse files Browse the repository at this point in the history
Quite minor, but passing a nil client into NewConsumerGroupFromClient
would cause a panic. Add a simple guard to prevent that.

Signed-off-by: Dominic Evans <dominic.evans@uk.ibm.com>
  • Loading branch information
dnwe committed Aug 31, 2023
1 parent f3c4194 commit 9b0419d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func NewConsumerGroup(addrs []string, groupID string, config *Config) (ConsumerG
// necessary to call Close() on the underlying client when shutting down this consumer.
// PLEASE NOTE: consumer groups can only re-use but not share clients.
func NewConsumerGroupFromClient(groupID string, client Client) (ConsumerGroup, error) {
if client == nil {
return nil, ConfigurationError("client must not be nil")
}
// For clients passed in by the client, ensure we don't
// call Close() on it.
cli := &nopCloserClient{client}
Expand Down
8 changes: 8 additions & 0 deletions consumer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func (h *handler) ConsumeClaim(sess ConsumerGroupSession, claim ConsumerGroupCla
return nil
}

func TestNewConsumerGroupFromClient(t *testing.T) {
t.Run("should not permit nil client", func(t *testing.T) {
group, err := NewConsumerGroupFromClient("group", nil)
assert.Nil(t, group)
assert.Error(t, err)
})
}

// TestConsumerGroupNewSessionDuringOffsetLoad ensures that the consumer group
// will retry Join and Sync group operations, if it receives a temporary
// OffsetsLoadInProgress error response, in the same way as it would for a
Expand Down

0 comments on commit 9b0419d

Please sign in to comment.