async_producer_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package mocks
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. "testing"
  8. "github.com/Shopify/sarama"
  9. )
  10. func generateRegexpChecker(re string) func([]byte) error {
  11. return func(val []byte) error {
  12. matched, err := regexp.MatchString(re, string(val))
  13. if err != nil {
  14. return errors.New("Error while trying to match the input message with the expected pattern: " + err.Error())
  15. }
  16. if !matched {
  17. return fmt.Errorf("No match between input value \"%s\" and expected pattern \"%s\"", val, re)
  18. }
  19. return nil
  20. }
  21. }
  22. type testReporterMock struct {
  23. errors []string
  24. }
  25. func newTestReporterMock() *testReporterMock {
  26. return &testReporterMock{errors: make([]string, 0)}
  27. }
  28. func (trm *testReporterMock) Errorf(format string, args ...interface{}) {
  29. trm.errors = append(trm.errors, fmt.Sprintf(format, args...))
  30. }
  31. func TestMockAsyncProducerImplementsAsyncProducerInterface(t *testing.T) {
  32. var mp interface{} = &AsyncProducer{}
  33. if _, ok := mp.(sarama.AsyncProducer); !ok {
  34. t.Error("The mock producer should implement the sarama.Producer interface.")
  35. }
  36. }
  37. func TestProducerReturnsExpectationsToChannels(t *testing.T) {
  38. config := sarama.NewConfig()
  39. config.Producer.Return.Successes = true
  40. mp := NewAsyncProducer(t, config)
  41. mp.ExpectInputAndSucceed()
  42. mp.ExpectInputAndSucceed()
  43. mp.ExpectInputAndFail(sarama.ErrOutOfBrokers)
  44. mp.Input() <- &sarama.ProducerMessage{Topic: "test 1"}
  45. mp.Input() <- &sarama.ProducerMessage{Topic: "test 2"}
  46. mp.Input() <- &sarama.ProducerMessage{Topic: "test 3"}
  47. msg1 := <-mp.Successes()
  48. msg2 := <-mp.Successes()
  49. err1 := <-mp.Errors()
  50. if msg1.Topic != "test 1" {
  51. t.Error("Expected message 1 to be returned first")
  52. }
  53. if msg2.Topic != "test 2" {
  54. t.Error("Expected message 2 to be returned second")
  55. }
  56. if err1.Msg.Topic != "test 3" || err1.Err != sarama.ErrOutOfBrokers {
  57. t.Error("Expected message 3 to be returned as error")
  58. }
  59. if err := mp.Close(); err != nil {
  60. t.Error(err)
  61. }
  62. }
  63. func TestProducerWithTooFewExpectations(t *testing.T) {
  64. trm := newTestReporterMock()
  65. mp := NewAsyncProducer(trm, nil)
  66. mp.ExpectInputAndSucceed()
  67. mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
  68. mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
  69. if err := mp.Close(); err != nil {
  70. t.Error(err)
  71. }
  72. if len(trm.errors) != 1 {
  73. t.Error("Expected to report an error")
  74. }
  75. }
  76. func TestProducerWithTooManyExpectations(t *testing.T) {
  77. trm := newTestReporterMock()
  78. mp := NewAsyncProducer(trm, nil)
  79. mp.ExpectInputAndSucceed()
  80. mp.ExpectInputAndFail(sarama.ErrOutOfBrokers)
  81. mp.Input() <- &sarama.ProducerMessage{Topic: "test"}
  82. if err := mp.Close(); err != nil {
  83. t.Error(err)
  84. }
  85. if len(trm.errors) != 1 {
  86. t.Error("Expected to report an error")
  87. }
  88. }
  89. func TestProducerWithCheckerFunction(t *testing.T) {
  90. trm := newTestReporterMock()
  91. mp := NewAsyncProducer(trm, nil)
  92. mp.ExpectInputWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes"))
  93. mp.ExpectInputWithCheckerFunctionAndSucceed(generateRegexpChecker("^tes$"))
  94. mp.Input() <- &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
  95. mp.Input() <- &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test")}
  96. if err := mp.Close(); err != nil {
  97. t.Error(err)
  98. }
  99. if len(mp.Errors()) != 1 {
  100. t.Error("Expected to report an error")
  101. }
  102. err1 := <-mp.Errors()
  103. if !strings.HasPrefix(err1.Err.Error(), "No match") {
  104. t.Error("Expected to report a value check error, found: ", err1.Err)
  105. }
  106. }