浏览代码

Proper mock expectations in SyncProducer.SendMessages

Fixing incorrect expectations checking for the SyncProducer mock
in SendMessages call.

1) SendMessages now checks the CheckFunction on each and
every single message/expectation. It's consistent with singular
SendMessage mocked implementation.

2) SendMessages now correctly copies the right size of expectations
to use when matching with produced messages. Fixes off by one error.
Adam Lieskovsky 7 年之前
父节点
当前提交
c7d4e043bf
共有 2 个文件被更改,包括 60 次插入3 次删除
  1. 14 3
      mocks/sync_producer.go
  2. 46 0
      mocks/sync_producer_test.go

+ 14 - 3
mocks/sync_producer.go

@@ -78,14 +78,25 @@ func (sp *SyncProducer) SendMessages(msgs []*sarama.ProducerMessage) error {
 	defer sp.l.Unlock()
 	defer sp.l.Unlock()
 
 
 	if len(sp.expectations) >= len(msgs) {
 	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):]
 		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 {
 			if expectation.Result != errProduceSuccess {
 				return expectation.Result
 				return expectation.Result
 			}
 			}
-
 		}
 		}
 		return nil
 		return nil
 	}
 	}

+ 46 - 0
mocks/sync_producer_test.go

@@ -122,3 +122,49 @@ func TestSyncProducerWithCheckerFunction(t *testing.T) {
 		t.Error("Expected to report an error")
 		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)
+	}
+}