Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper mock expectations in SyncProducer.SendMessages #1035

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions mocks/sync_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,25 @@ func (sp *SyncProducer) SendMessages(msgs []*sarama.ProducerMessage) error {
defer sp.l.Unlock()

if len(sp.expectations) >= len(msgs) {
expectations := sp.expectations[0 : len(msgs)-1]
expectations := sp.expectations[0:len(msgs)]
sp.expectations = sp.expectations[len(msgs):]

for _, expectation := range expectations {
for i, expectation := range expectations {
if expectation.CheckFunction != nil {
val, err := msgs[i].Value.Encode()
if err != nil {
sp.t.Errorf("Input message encoding failed: %s", err.Error())
return err
}
errCheck := expectation.CheckFunction(val)
if errCheck != nil {
sp.t.Errorf("Check function returned an error: %s", errCheck.Error())
return errCheck
}
}
if expectation.Result != errProduceSuccess {
return expectation.Result
}

}
return nil
}
Expand Down
126 changes: 126 additions & 0 deletions mocks/sync_producer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mocks

import (
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -122,3 +123,128 @@ func TestSyncProducerWithCheckerFunction(t *testing.T) {
t.Error("Expected to report an error")
}
}

func TestSyncProducerWithCheckerFunctionForSendMessagesWithError(t *testing.T) {
trm := newTestReporterMock()

sp := NewSyncProducer(trm, nil)
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes$"))

msg1 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
msg2 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
msgs := []*sarama.ProducerMessage{msg1, msg2}

if err := sp.SendMessages(msgs); err == nil || !strings.HasPrefix(err.Error(), "No match") {
t.Error("Error during value check expected on second message, found: ", err)
}

if err := sp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report an error")
}
}

func TestSyncProducerWithCheckerFunctionForSendMessagesWithoutError(t *testing.T) {
trm := newTestReporterMock()

sp := NewSyncProducer(trm, nil)
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))

msg1 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
msgs := []*sarama.ProducerMessage{msg1}

if err := sp.SendMessages(msgs); err != nil {
t.Error("No error expected on SendMessages call, found: ", err)
}

if err := sp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 0 {
t.Errorf("Expected to not report any errors, found: %v", trm.errors)
}
}

func TestSyncProducerSendMessagesExpectationsMismatchTooFew(t *testing.T) {
trm := newTestReporterMock()

sp := NewSyncProducer(trm, nil)
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))

msg1 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
msg2 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}

msgs := []*sarama.ProducerMessage{msg1, msg2}

if err := sp.SendMessages(msgs); err == nil {
t.Error("Error during value check expected on second message, found: ", err)
}

if err := sp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 2 {
t.Error("Expected to report 2 errors")
}
}

func TestSyncProducerSendMessagesExpectationsMismatchTooMany(t *testing.T) {
trm := newTestReporterMock()

sp := NewSyncProducer(trm, nil)
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))

msg1 := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
msgs := []*sarama.ProducerMessage{msg1}

if err := sp.SendMessages(msgs); err != nil {
t.Error("No error expected on SendMessages call, found: ", err)
}

if err := sp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report 1 errors")
}
}

func TestSyncProducerSendMessagesFaultyEncoder(t *testing.T) {
trm := newTestReporterMock()

sp := NewSyncProducer(trm, nil)
sp.ExpectSendMessageWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))

msg1 := &sarama.ProducerMessage{Topic: "test", Value: faultyEncoder("123")}
msgs := []*sarama.ProducerMessage{msg1}

if err := sp.SendMessages(msgs); err == nil || !strings.HasPrefix(err.Error(), "encode error") {
t.Error("Encoding error expected, found: ", err)
}

if err := sp.Close(); err != nil {
t.Error(err)
}

if len(trm.errors) != 1 {
t.Error("Expected to report 1 errors")
}
}

type faultyEncoder []byte

func (f faultyEncoder) Encode() ([]byte, error) {
return nil, errors.New("encode error")
}

func (f faultyEncoder) Length() int {
return len(f)
}