|
@@ -1,6 +1,7 @@
|
|
|
package mocks
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "regexp"
|
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
"github.com/Shopify/sarama"
|
|
@@ -34,7 +35,9 @@ func NewSyncProducer(t ErrorReporter, config *sarama.Config) *SyncProducer {
|
|
|
|
|
|
|
|
// SendMessage corresponds with the SendMessage method of sarama's SyncProducer implementation.
|
|
// SendMessage corresponds with the SendMessage method of sarama's SyncProducer implementation.
|
|
|
// You have to set expectations on the mock producer before calling SendMessage, so it knows
|
|
// You have to set expectations on the mock producer before calling SendMessage, so it knows
|
|
|
-// how to handle them. If there is no more remaining expectations when SendMessage is called,
|
|
|
|
|
|
|
+// how to handle them. You can set a regexp in each expectation so that the message value
|
|
|
|
|
+// is matched against this regexp and an error is returned if the match fails.
|
|
|
|
|
+// If there is no more remaining expectation when SendMessage is called,
|
|
|
// the mock producer will write an error to the test state object.
|
|
// the mock producer will write an error to the test state object.
|
|
|
func (sp *SyncProducer) SendMessage(msg *sarama.ProducerMessage) (partition int32, offset int64, err error) {
|
|
func (sp *SyncProducer) SendMessage(msg *sarama.ProducerMessage) (partition int32, offset int64, err error) {
|
|
|
sp.l.Lock()
|
|
sp.l.Lock()
|
|
@@ -43,7 +46,17 @@ func (sp *SyncProducer) SendMessage(msg *sarama.ProducerMessage) (partition int3
|
|
|
if len(sp.expectations) > 0 {
|
|
if len(sp.expectations) > 0 {
|
|
|
expectation := sp.expectations[0]
|
|
expectation := sp.expectations[0]
|
|
|
sp.expectations = sp.expectations[1:]
|
|
sp.expectations = sp.expectations[1:]
|
|
|
-
|
|
|
|
|
|
|
+ if len(expectation.MatchPattern) != 0 {
|
|
|
|
|
+ matched, err := regexp.MatchString(expectation.MatchPattern, msg.Value.String())
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ sp.t.Errorf("Error while trying to match the input message with the expected pattern: " + err.Error())
|
|
|
|
|
+ panic(err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
+ if !matched {
|
|
|
|
|
+ sp.t.Errorf("Input value \"%s\" did not match expected pattern \"%s\"", msg.Value.String(), expectation.MatchPattern)
|
|
|
|
|
+ return -1, -1, errNoMatch
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
if expectation.Result == errProduceSuccess {
|
|
if expectation.Result == errProduceSuccess {
|
|
|
sp.lastOffset++
|
|
sp.lastOffset++
|
|
|
msg.Offset = sp.lastOffset
|
|
msg.Offset = sp.lastOffset
|
|
@@ -75,20 +88,36 @@ func (sp *SyncProducer) Close() error {
|
|
|
// Setting expectations
|
|
// Setting expectations
|
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
+// ExpectSendMessageWithPatternAndSucceed sets an expectation on the mock producer that SendMessage
|
|
|
|
|
+// will be called with a message value matching a given regexp. The mock producer will first check the
|
|
|
|
|
+// message value against the pattern. It will return an error if the matching fails or handle
|
|
|
|
|
+// the message as if it produced successfully, i.e. by returning a valid partition, and offset, and a nil error.
|
|
|
|
|
+func (sp *SyncProducer) ExpectSendMessageWithPatternAndSucceed(pattern string) {
|
|
|
|
|
+ sp.l.Lock()
|
|
|
|
|
+ defer sp.l.Unlock()
|
|
|
|
|
+ sp.expectations = append(sp.expectations, &producerExpectation{Result: errProduceSuccess, MatchPattern: pattern})
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ExpectSendMessageAndFail sets an expectation on the mock producer that SendMessage will be
|
|
|
|
|
+// called with a message value matching a given regexp. The mock producer will first check the
|
|
|
|
|
+// message value against the pattern. It will return an error if the matching fails or handle
|
|
|
|
|
+// the message as if it failed to produce successfully, i.e. by returning the provided error.
|
|
|
|
|
+func (sp *SyncProducer) ExpectSendMessageWithPatternAndFail(pattern string, err error) {
|
|
|
|
|
+ sp.l.Lock()
|
|
|
|
|
+ defer sp.l.Unlock()
|
|
|
|
|
+ sp.expectations = append(sp.expectations, &producerExpectation{Result: err, MatchPattern: pattern})
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// ExpectSendMessageAndSucceed sets an expectation on the mock producer that SendMessage will be
|
|
// ExpectSendMessageAndSucceed sets an expectation on the mock producer that SendMessage will be
|
|
|
// called. The mock producer will handle the message as if it produced successfully, i.e. by
|
|
// called. The mock producer will handle the message as if it produced successfully, i.e. by
|
|
|
// returning a valid partition, and offset, and a nil error.
|
|
// returning a valid partition, and offset, and a nil 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.ExpectSendMessageWithPatternAndSucceed("")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ExpectSendMessageAndFail sets an expectation on the mock producer that SendMessage will be
|
|
// ExpectSendMessageAndFail sets an expectation on the mock producer that SendMessage will be
|
|
|
// called. The mock producer will handle the message as if it failed to produce
|
|
// called. The mock producer will handle the message as if it failed to produce
|
|
|
// successfully, i.e. by returning the provided error.
|
|
// successfully, i.e. by returning the provided error.
|
|
|
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.ExpectSendMessageWithPatternAndFail("", err)
|
|
|
}
|
|
}
|