Browse Source

Add package level godoc, fix some typos.

Willem van Bergen 10 years ago
parent
commit
e4ee98f586
4 changed files with 40 additions and 31 deletions
  1. 0 20
      mocks/expectations.go
  2. 33 0
      mocks/mocks.go
  3. 5 6
      mocks/producer.go
  4. 2 5
      mocks/sync_producer.go

+ 0 - 20
mocks/expectations.go

@@ -1,20 +0,0 @@
-package mocks
-
-import (
-	"errors"
-)
-
-// A simple interafce that includes the testing.T methods we use to report
-// expectation violations when using the mock objects.
-type ExpectationViolationReporter interface {
-	Errorf(string, ...interface{})
-}
-
-var (
-	errProduceSuccess    error = nil
-	errOutOfExpectations       = errors.New("No more expectations set on mock producer")
-)
-
-type producerExpectation struct {
-	Result error
-}

+ 33 - 0
mocks/mocks.go

@@ -0,0 +1,33 @@
+/*
+Package mocks provides mocks that can be used for testing applications
+that use Sarama. The mock types provided by this package implement the
+interfaces Sarama exports, so you can use them for dependency injection
+in your tests.
+
+All mock instances require you to set expectations on them before you
+can use them. It will determine how the mock will behave. If an
+expectation is not met, it will make your test fail.
+
+NOTE: this package currently does not fall under the API stability
+guarantee of Sarama as it is still considered experimental.
+*/
+package mocks
+
+import (
+	"errors"
+)
+
+// A simple interface that includes the testing.T methods we use to report
+// expectation violations when using the mock objects.
+type ErrorReporter interface {
+	Errorf(string, ...interface{})
+}
+
+var (
+	errProduceSuccess    error = nil
+	errOutOfExpectations       = errors.New("No more expectations set on mock producer")
+)
+
+type producerExpectation struct {
+	Result error
+}

+ 5 - 6
mocks/producer.go

@@ -10,11 +10,9 @@ import (
 // Before you can send messages to it's Input channel, you have to set expectations
 // so it knows how to handle the input. This way you can easily test success and
 // failure scenarios.
-//
-// NOTE: the SyncProducer type currently does not fall under the API stability
-// guarantee of Sarama as it is stiull considered experimental.
 type Producer struct {
 	l            sync.Mutex
+	t            ErrorReporter
 	expectations []*producerExpectation
 	closed       chan struct{}
 	input        chan *sarama.ProducerMessage
@@ -26,11 +24,12 @@ type Producer struct {
 // 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.
-func NewProducer(t ExpectationViolationReporter, config *sarama.Config) *Producer {
+func NewProducer(t ErrorReporter, config *sarama.Config) *Producer {
 	if config == nil {
 		config = sarama.NewConfig()
 	}
 	mp := &Producer{
+		t:            t,
 		closed:       make(chan struct{}, 0),
 		expectations: make([]*producerExpectation, 0),
 		input:        make(chan *sarama.ProducerMessage, config.ChannelBufferSize),
@@ -48,7 +47,7 @@ func NewProducer(t ExpectationViolationReporter, config *sarama.Config) *Produce
 			mp.l.Lock()
 			if mp.expectations == nil || len(mp.expectations) == 0 {
 				mp.expectations = nil
-				t.Errorf("No more expectation set on this mock producer to handle the input message.")
+				mp.t.Errorf("No more expectation set on this mock producer to handle the input message.")
 			} else {
 				expectation := mp.expectations[0]
 				mp.expectations = mp.expectations[1:]
@@ -65,7 +64,7 @@ func NewProducer(t ExpectationViolationReporter, config *sarama.Config) *Produce
 
 		mp.l.Lock()
 		if len(mp.expectations) > 0 {
-			t.Errorf("Expected to exhaust all expectations, but %d are left.", len(mp.expectations))
+			mp.t.Errorf("Expected to exhaust all expectations, but %d are left.", len(mp.expectations))
 		}
 		mp.l.Unlock()
 

+ 2 - 5
mocks/sync_producer.go

@@ -9,12 +9,9 @@ import (
 // Before you can use it, you have to set expectations on the mock SyncProducer
 // to tell it how to handle calls to SendMessage, so you can easily test success
 // and failure scenarios.
-//
-// NOTE: the SyncProducer type currently does not fall under the API stability
-// guarantee of Sarama as it is stiull considered experimental.
 type SyncProducer struct {
 	l            sync.Mutex
-	t            ExpectationViolationReporter
+	t            ErrorReporter
 	expectations []*producerExpectation
 	lastOffset   int64
 }
@@ -23,7 +20,7 @@ type SyncProducer struct {
 // 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 currently unused, but is
 // maintained to be compatible with the async Producer.
-func NewSyncProducer(t ExpectationViolationReporter, config *sarama.Config) *SyncProducer {
+func NewSyncProducer(t ErrorReporter, config *sarama.Config) *SyncProducer {
 	return &SyncProducer{
 		t:            t,
 		expectations: make([]*producerExpectation, 0),