Browse Source

Small improvements to mock producer

Willem van Bergen 10 years ago
parent
commit
129a010c9e
2 changed files with 18 additions and 2 deletions
  1. 14 2
      mocks/producer.go
  2. 4 0
      mocks/sync_producer.go

+ 14 - 2
mocks/producer.go

@@ -2,6 +2,7 @@ package mocks
 
 
 import (
 import (
 	"errors"
 	"errors"
+	"sync"
 
 
 	"github.com/Shopify/sarama"
 	"github.com/Shopify/sarama"
 )
 )
@@ -20,6 +21,7 @@ type producerExpectation struct {
 }
 }
 
 
 type Producer struct {
 type Producer struct {
+	l            sync.Mutex
 	expectations []*producerExpectation
 	expectations []*producerExpectation
 	closed       chan struct{}
 	closed       chan struct{}
 	input        chan *sarama.ProducerMessage
 	input        chan *sarama.ProducerMessage
@@ -46,23 +48,29 @@ func NewProducer(t TestReporter, config *sarama.Config) *Producer {
 		}()
 		}()
 
 
 		for msg := range mp.input {
 		for msg := range mp.input {
+			mp.l.Lock()
 			if mp.expectations == nil || len(mp.expectations) == 0 {
 			if mp.expectations == nil || len(mp.expectations) == 0 {
 				mp.expectations = nil
 				mp.expectations = nil
 				t.Errorf("No more expectation set on this mock producer to handle the input message.")
 				t.Errorf("No more expectation set on this mock producer to handle the input message.")
 			} else {
 			} else {
 				expectation := mp.expectations[0]
 				expectation := mp.expectations[0]
 				mp.expectations = mp.expectations[1:]
 				mp.expectations = mp.expectations[1:]
-				if expectation.Result == errProduceSuccess && config.Producer.AckSuccesses {
-					mp.successes <- msg
+				if expectation.Result == errProduceSuccess {
+					if config.Producer.AckSuccesses {
+						mp.successes <- msg
+					}
 				} else {
 				} else {
 					mp.errors <- &sarama.ProducerError{Err: expectation.Result, Msg: msg}
 					mp.errors <- &sarama.ProducerError{Err: expectation.Result, Msg: msg}
 				}
 				}
 			}
 			}
+			mp.l.Unlock()
 		}
 		}
 
 
+		mp.l.Lock()
 		if len(mp.expectations) > 0 {
 		if len(mp.expectations) > 0 {
 			t.Errorf("Expected to exhaust all expectations, but %d are left.", len(mp.expectations))
 			t.Errorf("Expected to exhaust all expectations, but %d are left.", len(mp.expectations))
 		}
 		}
+		mp.l.Unlock()
 
 
 		close(mp.closed)
 		close(mp.closed)
 	}()
 	}()
@@ -97,9 +105,13 @@ func (mp *Producer) Errors() <-chan *sarama.ProducerError {
 // Setting expectations
 // Setting expectations
 
 
 func (mp *Producer) ExpectInputAndSucceed() {
 func (mp *Producer) ExpectInputAndSucceed() {
+	mp.l.Lock()
+	defer mp.l.Unlock()
 	mp.expectations = append(mp.expectations, &producerExpectation{Result: errProduceSuccess})
 	mp.expectations = append(mp.expectations, &producerExpectation{Result: errProduceSuccess})
 }
 }
 
 
 func (mp *Producer) ExpectInputAndFail(err error) {
 func (mp *Producer) ExpectInputAndFail(err error) {
+	mp.l.Lock()
+	defer mp.l.Unlock()
 	mp.expectations = append(mp.expectations, &producerExpectation{Result: err})
 	mp.expectations = append(mp.expectations, &producerExpectation{Result: err})
 }
 }

+ 4 - 0
mocks/sync_producer.go

@@ -51,9 +51,13 @@ func (sp *SyncProducer) Close() error {
 }
 }
 
 
 func (sp *SyncProducer) ExpectSendMessageAndSucceed() {
 func (sp *SyncProducer) ExpectSendMessageAndSucceed() {
+	sp.l.Lock()
+	defer sp.l.Unlock()
 	sp.expectations = append(sp.expectations, &producerExpectation{Result: errProduceSuccess})
 	sp.expectations = append(sp.expectations, &producerExpectation{Result: errProduceSuccess})
 }
 }
 
 
 func (sp *SyncProducer) ExpectSendMessageAndFail(err error) {
 func (sp *SyncProducer) ExpectSendMessageAndFail(err error) {
+	sp.l.Lock()
+	defer sp.l.Unlock()
 	sp.expectations = append(sp.expectations, &producerExpectation{Result: err})
 	sp.expectations = append(sp.expectations, &producerExpectation{Result: err})
 }
 }