consumer_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package mocks
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/Shopify/sarama"
  6. )
  7. func TestMockConsumerImplementsConsumerInterface(t *testing.T) {
  8. var c interface{} = &Consumer{}
  9. if _, ok := c.(sarama.Consumer); !ok {
  10. t.Error("The mock consumer should implement the sarama.Consumer interface.")
  11. }
  12. var pc interface{} = &PartitionConsumer{}
  13. if _, ok := pc.(sarama.PartitionConsumer); !ok {
  14. t.Error("The mock partitionconsumer should implement the sarama.PartitionConsumer interface.")
  15. }
  16. }
  17. func TestConsumerHandlesExpectations(t *testing.T) {
  18. consumer := NewConsumer(t, nil)
  19. defer func() {
  20. if err := consumer.Close(); err != nil {
  21. t.Error(err)
  22. }
  23. }()
  24. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest).YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello world")})
  25. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest).YieldError(sarama.ErrOutOfBrokers)
  26. consumer.ExpectConsumePartition("test", 1, sarama.OffsetOldest).YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello world again")})
  27. consumer.ExpectConsumePartition("other", 0, AnyOffset).YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello other")})
  28. pc_test0, err := consumer.ConsumePartition("test", 0, sarama.OffsetOldest)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. test0_msg := <-pc_test0.Messages()
  33. if test0_msg.Topic != "test" || test0_msg.Partition != 0 || string(test0_msg.Value) != "hello world" {
  34. t.Error("Message was not as expected:", test0_msg)
  35. }
  36. test0_err := <-pc_test0.Errors()
  37. if test0_err.Err != sarama.ErrOutOfBrokers {
  38. t.Error("Expected sarama.ErrOutOfBrokers, found:", test0_err.Err)
  39. }
  40. pc_test1, err := consumer.ConsumePartition("test", 1, sarama.OffsetOldest)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. test1_msg := <-pc_test1.Messages()
  45. if test1_msg.Topic != "test" || test1_msg.Partition != 1 || string(test1_msg.Value) != "hello world again" {
  46. t.Error("Message was not as expected:", test1_msg)
  47. }
  48. pc_other0, err := consumer.ConsumePartition("other", 0, sarama.OffsetNewest)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. other0_msg := <-pc_other0.Messages()
  53. if other0_msg.Topic != "other" || other0_msg.Partition != 0 || string(other0_msg.Value) != "hello other" {
  54. t.Error("Message was not as expected:", other0_msg)
  55. }
  56. }
  57. func TestConsumerReturnsNonconsumedErrorsOnClose(t *testing.T) {
  58. consumer := NewConsumer(t, nil)
  59. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest).YieldError(sarama.ErrOutOfBrokers)
  60. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest).YieldError(sarama.ErrOutOfBrokers)
  61. pc, err := consumer.ConsumePartition("test", 0, sarama.OffsetOldest)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. select {
  66. case <-pc.Messages():
  67. t.Error("Did not epxect a message on the messages channel.")
  68. case err := <-pc.Errors():
  69. if err.Err != sarama.ErrOutOfBrokers {
  70. t.Error("Expected sarama.ErrOutOfBrokers, found", err)
  71. }
  72. }
  73. errs := pc.Close().(sarama.ConsumerErrors)
  74. if len(errs) != 1 && errs[0].Err != sarama.ErrOutOfBrokers {
  75. t.Error("Expected Close to return the remaining sarama.ErrOutOfBrokers")
  76. }
  77. }
  78. func TestConsumerWithoutExpectationsOnPartition(t *testing.T) {
  79. trm := newTestReporterMock()
  80. consumer := NewConsumer(trm, nil)
  81. _, err := consumer.ConsumePartition("test", 1, sarama.OffsetOldest)
  82. if err != errOutOfExpectations {
  83. t.Error("Expected ConsumePartition to return errOutOfExpectations")
  84. }
  85. if err := consumer.Close(); err != nil {
  86. t.Error("No error expected on close, but found:", err)
  87. }
  88. if len(trm.errors) != 1 {
  89. t.Errorf("Expected an expectation failure to be set on the error reporter.")
  90. }
  91. }
  92. func TestConsumerWithExpectationsOnUnconsumedPartition(t *testing.T) {
  93. trm := newTestReporterMock()
  94. consumer := NewConsumer(trm, nil)
  95. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest).YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello world")})
  96. if err := consumer.Close(); err != nil {
  97. t.Error("No error expected on close, but found:", err)
  98. }
  99. if len(trm.errors) != 1 {
  100. t.Errorf("Expected an expectation failure to be set on the error reporter.")
  101. }
  102. }
  103. func TestConsumerWithWrongOffsetExpectation(t *testing.T) {
  104. trm := newTestReporterMock()
  105. consumer := NewConsumer(trm, nil)
  106. consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest)
  107. _, err := consumer.ConsumePartition("test", 0, sarama.OffsetNewest)
  108. if err != nil {
  109. t.Error("Did not expect error, found:", err)
  110. }
  111. if len(trm.errors) != 1 {
  112. t.Errorf("Expected an expectation failure to be set on the error reporter.")
  113. }
  114. if err := consumer.Close(); err != nil {
  115. t.Error(err)
  116. }
  117. }
  118. func TestConsumerViolatesMessagesDrainedExpectation(t *testing.T) {
  119. trm := newTestReporterMock()
  120. consumer := NewConsumer(trm, nil)
  121. pcmock := consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest)
  122. pcmock.YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello")})
  123. pcmock.YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello")})
  124. pcmock.ExpectMessagesDrainedOnClose()
  125. pc, err := consumer.ConsumePartition("test", 0, sarama.OffsetOldest)
  126. if err != nil {
  127. t.Error(err)
  128. }
  129. // consume first message, not second one
  130. <-pc.Messages()
  131. if err := consumer.Close(); err != nil {
  132. t.Error(err)
  133. }
  134. if len(trm.errors) != 1 {
  135. t.Errorf("Expected an expectation failure to be set on the error reporter.")
  136. }
  137. }
  138. func TestConsumerMeetsErrorsDrainedExpectation(t *testing.T) {
  139. trm := newTestReporterMock()
  140. consumer := NewConsumer(trm, nil)
  141. pcmock := consumer.ExpectConsumePartition("test", 0, sarama.OffsetOldest)
  142. pcmock.YieldError(sarama.ErrInvalidMessage)
  143. pcmock.YieldError(sarama.ErrInvalidMessage)
  144. pcmock.ExpectErrorsDrainedOnClose()
  145. pc, err := consumer.ConsumePartition("test", 0, sarama.OffsetOldest)
  146. if err != nil {
  147. t.Error(err)
  148. }
  149. // consume first and second error,
  150. <-pc.Errors()
  151. <-pc.Errors()
  152. if err := consumer.Close(); err != nil {
  153. t.Error(err)
  154. }
  155. if len(trm.errors) != 0 {
  156. t.Errorf("Expected no expectation failures to be set on the error reporter.")
  157. }
  158. }
  159. func TestConsumerTopicMetadata(t *testing.T) {
  160. trm := newTestReporterMock()
  161. consumer := NewConsumer(trm, nil)
  162. consumer.SetTopicMetadata(map[string][]int32{
  163. "test1": []int32{0, 1, 2, 3},
  164. "test2": []int32{0, 1, 2, 3, 4, 5, 6, 7},
  165. })
  166. topics, err := consumer.Topics()
  167. if err != nil {
  168. t.Error(t)
  169. }
  170. sortedTopics := sort.StringSlice(topics)
  171. sortedTopics.Sort()
  172. if len(sortedTopics) != 2 || sortedTopics[0] != "test1" || sortedTopics[1] != "test2" {
  173. t.Error("Unexpected topics returned:", sortedTopics)
  174. }
  175. partitions1, err := consumer.Partitions("test1")
  176. if err != nil {
  177. t.Error(t)
  178. }
  179. if len(partitions1) != 4 {
  180. t.Error("Unexpected partitions returned:", len(partitions1))
  181. }
  182. partitions2, err := consumer.Partitions("test2")
  183. if err != nil {
  184. t.Error(t)
  185. }
  186. if len(partitions2) != 8 {
  187. t.Error("Unexpected partitions returned:", len(partitions2))
  188. }
  189. if len(trm.errors) != 0 {
  190. t.Errorf("Expected no expectation failures to be set on the error reporter.")
  191. }
  192. }
  193. func TestConsumerUnexpectedTopicMetadata(t *testing.T) {
  194. trm := newTestReporterMock()
  195. consumer := NewConsumer(trm, nil)
  196. if _, err := consumer.Topics(); err != sarama.ErrOutOfBrokers {
  197. t.Error("Expected sarama.ErrOutOfBrokers, found", err)
  198. }
  199. if len(trm.errors) != 1 {
  200. t.Errorf("Expected an expectation failure to be set on the error reporter.")
  201. }
  202. }