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

Validate the Config when creating a mock producer/consumer #2327

Merged
merged 1 commit into from Sep 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions mocks/async_producer.go
Expand Up @@ -30,12 +30,15 @@ type AsyncProducer struct {

// NewAsyncProducer instantiates a new Producer mock. The t argument should
// be the *testing.T instance of your test method. An error will be written to it if
// an expectation is violated. The config argument is used to determine whether it
// should ack successes on the Successes channel and to handle partitioning.
// an expectation is violated. The config argument is validated and used to determine
// whether it should ack successes on the Successes channel and handle partitioning.
func NewAsyncProducer(t ErrorReporter, config *sarama.Config) *AsyncProducer {
if config == nil {
config = sarama.NewConfig()
}
if err := config.Validate(); err != nil {
t.Errorf("Invalid mock configuration provided: %s", err.Error())
}
mp := &AsyncProducer{
t: t,
closed: make(chan struct{}),
Expand Down
15 changes: 15 additions & 0 deletions mocks/async_producer_test.go
Expand Up @@ -247,3 +247,18 @@ func (brokePartitioner) Partition(msg *sarama.ProducerMessage, n int32) (int32,
}

func (brokePartitioner) RequiresConsistency() bool { return false }

func TestProducerWithInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
mp := NewAsyncProducer(trm, config)
if err := mp.Close(); err != nil {
t.Error(err)
}
if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
6 changes: 5 additions & 1 deletion mocks/consumer.go
Expand Up @@ -20,11 +20,15 @@ type Consumer struct {

// NewConsumer returns a new mock Consumer instance. The t argument should
// be the *testing.T instance of your test method. An error will be written to it if
// an expectation is violated. The config argument can be set to nil.
// an expectation is violated. The config argument can be set to nil; if it is
// non-nil it is validated.
func NewConsumer(t ErrorReporter, config *sarama.Config) *Consumer {
if config == nil {
config = sarama.NewConfig()
}
if err := config.Validate(); err != nil {
t.Errorf("Invalid mock configuration provided: %s", err.Error())
}

c := &Consumer{
t: t,
Expand Down
17 changes: 17 additions & 0 deletions mocks/consumer_test.go
Expand Up @@ -3,6 +3,7 @@ package mocks
import (
"errors"
"sort"
"strings"
"testing"

"github.com/Shopify/sarama"
Expand Down Expand Up @@ -395,3 +396,19 @@ func TestConsumerOffsetsAreManagedCorrectlyWithSpecifiedOffset(t *testing.T) {
t.Errorf("Expected to not report any errors, found: %v", trm.errors)
}
}

func TestConsumerInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
consumer := NewConsumer(trm, config)
if err := consumer.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}
6 changes: 5 additions & 1 deletion mocks/sync_producer.go
Expand Up @@ -28,11 +28,15 @@ type SyncProducer struct {

// NewSyncProducer instantiates a new SyncProducer mock. The t argument should
// be the *testing.T instance of your test method. An error will be written to it if
// an expectation is violated. The config argument is used to handle partitioning.
// an expectation is violated. The config argument is validated and used to handle
// partitioning.
func NewSyncProducer(t ErrorReporter, config *sarama.Config) *SyncProducer {
if config == nil {
config = sarama.NewConfig()
}
if err := config.Validate(); err != nil {
t.Errorf("Invalid mock configuration provided: %s", err.Error())
}
return &SyncProducer{
t: t,
expectations: make([]*producerExpectation, 0),
Expand Down
16 changes: 16 additions & 0 deletions mocks/sync_producer_test.go
Expand Up @@ -352,3 +352,19 @@ func (f faultyEncoder) Encode() ([]byte, error) {
func (f faultyEncoder) Length() int {
return len(f)
}

func TestSyncProducerInvalidConfiguration(t *testing.T) {
trm := newTestReporterMock()
config := NewTestConfig()
config.ClientID = "not a valid client ID"
mp := NewSyncProducer(trm, config)
if err := mp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report a single error")
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
t.Errorf("Unexpected error: %s", trm.errors[0])
}
}