Просмотр исходного кода

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 8 лет назад
Родитель
Сommit
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)
+	}
+}