diff --git a/mocks/async_producer.go b/mocks/async_producer.go index 0e2174b71c..1c54f8c1af 100644 --- a/mocks/async_producer.go +++ b/mocks/async_producer.go @@ -27,12 +27,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{}), diff --git a/mocks/async_producer_test.go b/mocks/async_producer_test.go index 63c5c87f2d..eecee7fbe9 100644 --- a/mocks/async_producer_test.go +++ b/mocks/async_producer_test.go @@ -171,3 +171,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]) + } +} diff --git a/mocks/consumer.go b/mocks/consumer.go index d8a212221f..37a07cf38b 100644 --- a/mocks/consumer.go +++ b/mocks/consumer.go @@ -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, diff --git a/mocks/consumer_test.go b/mocks/consumer_test.go index 9a19b15964..2aa9614647 100644 --- a/mocks/consumer_test.go +++ b/mocks/consumer_test.go @@ -3,6 +3,7 @@ package mocks import ( "errors" "sort" + "strings" "testing" "github.com/Shopify/sarama" @@ -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]) + } +} diff --git a/mocks/sync_producer.go b/mocks/sync_producer.go index ef7d67988b..269510aa32 100644 --- a/mocks/sync_producer.go +++ b/mocks/sync_producer.go @@ -24,11 +24,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), diff --git a/mocks/sync_producer_test.go b/mocks/sync_producer_test.go index fde215d973..b4887892cc 100644 --- a/mocks/sync_producer_test.go +++ b/mocks/sync_producer_test.go @@ -254,3 +254,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]) + } +}