Skip to content

Commit

Permalink
Validate the Config when creating a mock producer/consumer
Browse files Browse the repository at this point in the history
Normally this happens during creation of the `Client`, but for mock
interfaces there is no `Client`.
  • Loading branch information
joewreschnig authored and dnwe committed Sep 27, 2022
1 parent b923960 commit 9a14762
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
7 changes: 5 additions & 2 deletions mocks/async_producer.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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])
}
}

0 comments on commit 9a14762

Please sign in to comment.