Skip to content

Commit 9a14762

Browse files
joewreschnigdnwe
authored andcommitted
Validate the Config when creating a mock producer/consumer
Normally this happens during creation of the `Client`, but for mock interfaces there is no `Client`.
1 parent b923960 commit 9a14762

6 files changed

+63
-4
lines changed

mocks/async_producer.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ type AsyncProducer struct {
3030

3131
// NewAsyncProducer instantiates a new Producer mock. The t argument should
3232
// be the *testing.T instance of your test method. An error will be written to it if
33-
// an expectation is violated. The config argument is used to determine whether it
34-
// should ack successes on the Successes channel and to handle partitioning.
33+
// an expectation is violated. The config argument is validated and used to determine
34+
// whether it should ack successes on the Successes channel and handle partitioning.
3535
func NewAsyncProducer(t ErrorReporter, config *sarama.Config) *AsyncProducer {
3636
if config == nil {
3737
config = sarama.NewConfig()
3838
}
39+
if err := config.Validate(); err != nil {
40+
t.Errorf("Invalid mock configuration provided: %s", err.Error())
41+
}
3942
mp := &AsyncProducer{
4043
t: t,
4144
closed: make(chan struct{}),

mocks/async_producer_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,18 @@ func (brokePartitioner) Partition(msg *sarama.ProducerMessage, n int32) (int32,
247247
}
248248

249249
func (brokePartitioner) RequiresConsistency() bool { return false }
250+
251+
func TestProducerWithInvalidConfiguration(t *testing.T) {
252+
trm := newTestReporterMock()
253+
config := NewTestConfig()
254+
config.ClientID = "not a valid client ID"
255+
mp := NewAsyncProducer(trm, config)
256+
if err := mp.Close(); err != nil {
257+
t.Error(err)
258+
}
259+
if len(trm.errors) != 1 {
260+
t.Error("Expected to report a single error")
261+
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
262+
t.Errorf("Unexpected error: %s", trm.errors[0])
263+
}
264+
}

mocks/consumer.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ type Consumer struct {
2020

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

2933
c := &Consumer{
3034
t: t,

mocks/consumer_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mocks
33
import (
44
"errors"
55
"sort"
6+
"strings"
67
"testing"
78

89
"github.com/Shopify/sarama"
@@ -395,3 +396,19 @@ func TestConsumerOffsetsAreManagedCorrectlyWithSpecifiedOffset(t *testing.T) {
395396
t.Errorf("Expected to not report any errors, found: %v", trm.errors)
396397
}
397398
}
399+
400+
func TestConsumerInvalidConfiguration(t *testing.T) {
401+
trm := newTestReporterMock()
402+
config := NewTestConfig()
403+
config.ClientID = "not a valid client ID"
404+
consumer := NewConsumer(trm, config)
405+
if err := consumer.Close(); err != nil {
406+
t.Error(err)
407+
}
408+
409+
if len(trm.errors) != 1 {
410+
t.Error("Expected to report a single error")
411+
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
412+
t.Errorf("Unexpected error: %s", trm.errors[0])
413+
}
414+
}

mocks/sync_producer.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ type SyncProducer struct {
2828

2929
// NewSyncProducer instantiates a new SyncProducer mock. The t argument should
3030
// be the *testing.T instance of your test method. An error will be written to it if
31-
// an expectation is violated. The config argument is used to handle partitioning.
31+
// an expectation is violated. The config argument is validated and used to handle
32+
// partitioning.
3233
func NewSyncProducer(t ErrorReporter, config *sarama.Config) *SyncProducer {
3334
if config == nil {
3435
config = sarama.NewConfig()
3536
}
37+
if err := config.Validate(); err != nil {
38+
t.Errorf("Invalid mock configuration provided: %s", err.Error())
39+
}
3640
return &SyncProducer{
3741
t: t,
3842
expectations: make([]*producerExpectation, 0),

mocks/sync_producer_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,19 @@ func (f faultyEncoder) Encode() ([]byte, error) {
352352
func (f faultyEncoder) Length() int {
353353
return len(f)
354354
}
355+
356+
func TestSyncProducerInvalidConfiguration(t *testing.T) {
357+
trm := newTestReporterMock()
358+
config := NewTestConfig()
359+
config.ClientID = "not a valid client ID"
360+
mp := NewSyncProducer(trm, config)
361+
if err := mp.Close(); err != nil {
362+
t.Error(err)
363+
}
364+
365+
if len(trm.errors) != 1 {
366+
t.Error("Expected to report a single error")
367+
} else if !strings.Contains(trm.errors[0], "ClientID is invalid") {
368+
t.Errorf("Unexpected error: %s", trm.errors[0])
369+
}
370+
}

0 commit comments

Comments
 (0)