mockresponses.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package sarama
  2. import (
  3. "fmt"
  4. )
  5. // TestReporter has methods matching go's testing.T to avoid importing
  6. // `testing` in the main part of the library.
  7. type TestReporter interface {
  8. Error(...interface{})
  9. Errorf(string, ...interface{})
  10. Fatal(...interface{})
  11. Fatalf(string, ...interface{})
  12. }
  13. // MockResponse is a response builder interface it defines one method that
  14. // allows generating a response based on a request body. MockResponses are used
  15. // to program behavior of MockBroker in tests.
  16. type MockResponse interface {
  17. For(reqBody versionedDecoder) (res encoder)
  18. }
  19. // MockWrapper is a mock response builder that returns a particular concrete
  20. // response regardless of the actual request passed to the `For` method.
  21. type MockWrapper struct {
  22. res encoder
  23. }
  24. func (mw *MockWrapper) For(reqBody versionedDecoder) (res encoder) {
  25. return mw.res
  26. }
  27. func NewMockWrapper(res encoder) *MockWrapper {
  28. return &MockWrapper{res: res}
  29. }
  30. // MockSequence is a mock response builder that is created from a sequence of
  31. // concrete responses. Every time when a `MockBroker` calls its `For` method
  32. // the next response from the sequence is returned. When the end of the
  33. // sequence is reached the last element from the sequence is returned.
  34. type MockSequence struct {
  35. responses []MockResponse
  36. }
  37. func NewMockSequence(responses ...interface{}) *MockSequence {
  38. ms := &MockSequence{}
  39. ms.responses = make([]MockResponse, len(responses))
  40. for i, res := range responses {
  41. switch res := res.(type) {
  42. case MockResponse:
  43. ms.responses[i] = res
  44. case encoder:
  45. ms.responses[i] = NewMockWrapper(res)
  46. default:
  47. panic(fmt.Sprintf("Unexpected response type: %T", res))
  48. }
  49. }
  50. return ms
  51. }
  52. func (mc *MockSequence) For(reqBody versionedDecoder) (res encoder) {
  53. res = mc.responses[0].For(reqBody)
  54. if len(mc.responses) > 1 {
  55. mc.responses = mc.responses[1:]
  56. }
  57. return res
  58. }
  59. // MockMetadataResponse is a `MetadataResponse` builder.
  60. type MockMetadataResponse struct {
  61. leaders map[string]map[int32]int32
  62. brokers map[string]int32
  63. t TestReporter
  64. }
  65. func NewMockMetadataResponse(t TestReporter) *MockMetadataResponse {
  66. return &MockMetadataResponse{
  67. leaders: make(map[string]map[int32]int32),
  68. brokers: make(map[string]int32),
  69. t: t,
  70. }
  71. }
  72. func (mmr *MockMetadataResponse) SetLeader(topic string, partition, brokerID int32) *MockMetadataResponse {
  73. partitions := mmr.leaders[topic]
  74. if partitions == nil {
  75. partitions = make(map[int32]int32)
  76. mmr.leaders[topic] = partitions
  77. }
  78. partitions[partition] = brokerID
  79. return mmr
  80. }
  81. func (mmr *MockMetadataResponse) SetBroker(addr string, brokerID int32) *MockMetadataResponse {
  82. mmr.brokers[addr] = brokerID
  83. return mmr
  84. }
  85. func (mmr *MockMetadataResponse) For(reqBody versionedDecoder) encoder {
  86. metadataRequest := reqBody.(*MetadataRequest)
  87. metadataResponse := &MetadataResponse{}
  88. for addr, brokerID := range mmr.brokers {
  89. metadataResponse.AddBroker(addr, brokerID)
  90. }
  91. if len(metadataRequest.Topics) == 0 {
  92. for topic, partitions := range mmr.leaders {
  93. for partition, brokerID := range partitions {
  94. metadataResponse.AddTopicPartition(topic, partition, brokerID, nil, nil, ErrNoError)
  95. }
  96. }
  97. return metadataResponse
  98. }
  99. for _, topic := range metadataRequest.Topics {
  100. for partition, brokerID := range mmr.leaders[topic] {
  101. metadataResponse.AddTopicPartition(topic, partition, brokerID, nil, nil, ErrNoError)
  102. }
  103. }
  104. return metadataResponse
  105. }
  106. // MockOffsetResponse is an `OffsetResponse` builder.
  107. type MockOffsetResponse struct {
  108. offsets map[string]map[int32]map[int64]int64
  109. t TestReporter
  110. }
  111. func NewMockOffsetResponse(t TestReporter) *MockOffsetResponse {
  112. return &MockOffsetResponse{
  113. offsets: make(map[string]map[int32]map[int64]int64),
  114. t: t,
  115. }
  116. }
  117. func (mor *MockOffsetResponse) SetOffset(topic string, partition int32, time, offset int64) *MockOffsetResponse {
  118. partitions := mor.offsets[topic]
  119. if partitions == nil {
  120. partitions = make(map[int32]map[int64]int64)
  121. mor.offsets[topic] = partitions
  122. }
  123. times := partitions[partition]
  124. if times == nil {
  125. times = make(map[int64]int64)
  126. partitions[partition] = times
  127. }
  128. times[time] = offset
  129. return mor
  130. }
  131. func (mor *MockOffsetResponse) For(reqBody versionedDecoder) encoder {
  132. offsetRequest := reqBody.(*OffsetRequest)
  133. offsetResponse := &OffsetResponse{}
  134. for topic, partitions := range offsetRequest.blocks {
  135. for partition, block := range partitions {
  136. offset := mor.getOffset(topic, partition, block.time)
  137. offsetResponse.AddTopicPartition(topic, partition, offset)
  138. }
  139. }
  140. return offsetResponse
  141. }
  142. func (mor *MockOffsetResponse) getOffset(topic string, partition int32, time int64) int64 {
  143. partitions := mor.offsets[topic]
  144. if partitions == nil {
  145. mor.t.Errorf("missing topic: %s", topic)
  146. }
  147. times := partitions[partition]
  148. if times == nil {
  149. mor.t.Errorf("missing partition: %d", partition)
  150. }
  151. offset, ok := times[time]
  152. if !ok {
  153. mor.t.Errorf("missing time: %d", time)
  154. }
  155. return offset
  156. }
  157. // MockFetchResponse is a `FetchResponse` builder.
  158. type MockFetchResponse struct {
  159. messages map[string]map[int32]map[int64]Encoder
  160. highWaterMarks map[string]map[int32]int64
  161. t TestReporter
  162. batchSize int
  163. }
  164. func NewMockFetchResponse(t TestReporter, batchSize int) *MockFetchResponse {
  165. return &MockFetchResponse{
  166. messages: make(map[string]map[int32]map[int64]Encoder),
  167. highWaterMarks: make(map[string]map[int32]int64),
  168. t: t,
  169. batchSize: batchSize,
  170. }
  171. }
  172. func (mfr *MockFetchResponse) SetMessage(topic string, partition int32, offset int64, msg Encoder) *MockFetchResponse {
  173. partitions := mfr.messages[topic]
  174. if partitions == nil {
  175. partitions = make(map[int32]map[int64]Encoder)
  176. mfr.messages[topic] = partitions
  177. }
  178. messages := partitions[partition]
  179. if messages == nil {
  180. messages = make(map[int64]Encoder)
  181. partitions[partition] = messages
  182. }
  183. messages[offset] = msg
  184. return mfr
  185. }
  186. func (mfr *MockFetchResponse) SetHighWaterMark(topic string, partition int32, offset int64) *MockFetchResponse {
  187. partitions := mfr.highWaterMarks[topic]
  188. if partitions == nil {
  189. partitions = make(map[int32]int64)
  190. mfr.highWaterMarks[topic] = partitions
  191. }
  192. partitions[partition] = offset
  193. return mfr
  194. }
  195. func (mfr *MockFetchResponse) For(reqBody versionedDecoder) encoder {
  196. fetchRequest := reqBody.(*FetchRequest)
  197. res := &FetchResponse{}
  198. for topic, partitions := range fetchRequest.blocks {
  199. for partition, block := range partitions {
  200. initialOffset := block.fetchOffset
  201. offset := initialOffset
  202. maxOffset := initialOffset + int64(mfr.getMessageCount(topic, partition))
  203. for i := 0; i < mfr.batchSize && offset < maxOffset; {
  204. msg := mfr.getMessage(topic, partition, offset)
  205. if msg != nil {
  206. res.AddMessage(topic, partition, nil, msg, offset)
  207. i++
  208. }
  209. offset++
  210. }
  211. fb := res.GetBlock(topic, partition)
  212. if fb == nil {
  213. res.AddError(topic, partition, ErrNoError)
  214. fb = res.GetBlock(topic, partition)
  215. }
  216. fb.HighWaterMarkOffset = mfr.getHighWaterMark(topic, partition)
  217. }
  218. }
  219. return res
  220. }
  221. func (mfr *MockFetchResponse) getMessage(topic string, partition int32, offset int64) Encoder {
  222. partitions := mfr.messages[topic]
  223. if partitions == nil {
  224. return nil
  225. }
  226. messages := partitions[partition]
  227. if messages == nil {
  228. return nil
  229. }
  230. return messages[offset]
  231. }
  232. func (mfr *MockFetchResponse) getMessageCount(topic string, partition int32) int {
  233. partitions := mfr.messages[topic]
  234. if partitions == nil {
  235. return 0
  236. }
  237. messages := partitions[partition]
  238. if messages == nil {
  239. return 0
  240. }
  241. return len(messages)
  242. }
  243. func (mfr *MockFetchResponse) getHighWaterMark(topic string, partition int32) int64 {
  244. partitions := mfr.highWaterMarks[topic]
  245. if partitions == nil {
  246. return 0
  247. }
  248. return partitions[partition]
  249. }
  250. // MockConsumerMetadataResponse is a `ConsumerMetadataResponse` builder.
  251. type MockConsumerMetadataResponse struct {
  252. coordinators map[string]interface{}
  253. t TestReporter
  254. }
  255. func NewMockConsumerMetadataResponse(t TestReporter) *MockConsumerMetadataResponse {
  256. return &MockConsumerMetadataResponse{
  257. coordinators: make(map[string]interface{}),
  258. t: t,
  259. }
  260. }
  261. func (mr *MockConsumerMetadataResponse) SetCoordinator(group string, broker *MockBroker) *MockConsumerMetadataResponse {
  262. mr.coordinators[group] = broker
  263. return mr
  264. }
  265. func (mr *MockConsumerMetadataResponse) SetError(group string, kerror KError) *MockConsumerMetadataResponse {
  266. mr.coordinators[group] = kerror
  267. return mr
  268. }
  269. func (mr *MockConsumerMetadataResponse) For(reqBody versionedDecoder) encoder {
  270. req := reqBody.(*ConsumerMetadataRequest)
  271. group := req.ConsumerGroup
  272. res := &ConsumerMetadataResponse{}
  273. v := mr.coordinators[group]
  274. switch v := v.(type) {
  275. case *MockBroker:
  276. res.Coordinator = &Broker{id: v.BrokerID(), addr: v.Addr()}
  277. case KError:
  278. res.Err = v
  279. }
  280. return res
  281. }
  282. // MockOffsetCommitResponse is a `OffsetCommitResponse` builder.
  283. type MockOffsetCommitResponse struct {
  284. errors map[string]map[string]map[int32]KError
  285. t TestReporter
  286. }
  287. func NewMockOffsetCommitResponse(t TestReporter) *MockOffsetCommitResponse {
  288. return &MockOffsetCommitResponse{t: t}
  289. }
  290. func (mr *MockOffsetCommitResponse) SetError(group, topic string, partition int32, kerror KError) *MockOffsetCommitResponse {
  291. if mr.errors == nil {
  292. mr.errors = make(map[string]map[string]map[int32]KError)
  293. }
  294. topics := mr.errors[group]
  295. if topics == nil {
  296. topics = make(map[string]map[int32]KError)
  297. mr.errors[group] = topics
  298. }
  299. partitions := topics[topic]
  300. if partitions == nil {
  301. partitions = make(map[int32]KError)
  302. topics[topic] = partitions
  303. }
  304. partitions[partition] = kerror
  305. return mr
  306. }
  307. func (mr *MockOffsetCommitResponse) For(reqBody versionedDecoder) encoder {
  308. req := reqBody.(*OffsetCommitRequest)
  309. group := req.ConsumerGroup
  310. res := &OffsetCommitResponse{}
  311. for topic, partitions := range req.blocks {
  312. for partition := range partitions {
  313. res.AddError(topic, partition, mr.getError(group, topic, partition))
  314. }
  315. }
  316. return res
  317. }
  318. func (mr *MockOffsetCommitResponse) getError(group, topic string, partition int32) KError {
  319. topics := mr.errors[group]
  320. if topics == nil {
  321. return ErrNoError
  322. }
  323. partitions := topics[topic]
  324. if partitions == nil {
  325. return ErrNoError
  326. }
  327. kerror, ok := partitions[partition]
  328. if !ok {
  329. return ErrNoError
  330. }
  331. return kerror
  332. }
  333. // MockProduceResponse is a `ProduceResponse` builder.
  334. type MockProduceResponse struct {
  335. errors map[string]map[int32]KError
  336. t TestReporter
  337. }
  338. func NewMockProduceResponse(t TestReporter) *MockProduceResponse {
  339. return &MockProduceResponse{t: t}
  340. }
  341. func (mr *MockProduceResponse) SetError(topic string, partition int32, kerror KError) *MockProduceResponse {
  342. if mr.errors == nil {
  343. mr.errors = make(map[string]map[int32]KError)
  344. }
  345. partitions := mr.errors[topic]
  346. if partitions == nil {
  347. partitions = make(map[int32]KError)
  348. mr.errors[topic] = partitions
  349. }
  350. partitions[partition] = kerror
  351. return mr
  352. }
  353. func (mr *MockProduceResponse) For(reqBody versionedDecoder) encoder {
  354. req := reqBody.(*ProduceRequest)
  355. res := &ProduceResponse{}
  356. for topic, partitions := range req.msgSets {
  357. for partition := range partitions {
  358. res.AddTopicPartition(topic, partition, mr.getError(topic, partition))
  359. }
  360. }
  361. return res
  362. }
  363. func (mr *MockProduceResponse) getError(topic string, partition int32) KError {
  364. partitions := mr.errors[topic]
  365. if partitions == nil {
  366. return ErrNoError
  367. }
  368. kerror, ok := partitions[partition]
  369. if !ok {
  370. return ErrNoError
  371. }
  372. return kerror
  373. }
  374. // MockOffsetFetchResponse is a `OffsetFetchResponse` builder.
  375. type MockOffsetFetchResponse struct {
  376. offsets map[string]map[string]map[int32]*OffsetFetchResponseBlock
  377. t TestReporter
  378. }
  379. func NewMockOffsetFetchResponse(t TestReporter) *MockOffsetFetchResponse {
  380. return &MockOffsetFetchResponse{t: t}
  381. }
  382. func (mr *MockOffsetFetchResponse) SetOffset(group, topic string, partition int32, offset int64, metadata string, kerror KError) *MockOffsetFetchResponse {
  383. if mr.offsets == nil {
  384. mr.offsets = make(map[string]map[string]map[int32]*OffsetFetchResponseBlock)
  385. }
  386. topics := mr.offsets[group]
  387. if topics == nil {
  388. topics = make(map[string]map[int32]*OffsetFetchResponseBlock)
  389. mr.offsets[group] = topics
  390. }
  391. partitions := topics[topic]
  392. if partitions == nil {
  393. partitions = make(map[int32]*OffsetFetchResponseBlock)
  394. topics[topic] = partitions
  395. }
  396. partitions[partition] = &OffsetFetchResponseBlock{offset, metadata, kerror}
  397. return mr
  398. }
  399. func (mr *MockOffsetFetchResponse) For(reqBody versionedDecoder) encoder {
  400. req := reqBody.(*OffsetFetchRequest)
  401. group := req.ConsumerGroup
  402. res := &OffsetFetchResponse{}
  403. for topic, partitions := range mr.offsets[group] {
  404. for partition, block := range partitions {
  405. res.AddBlock(topic, partition, block)
  406. }
  407. }
  408. return res
  409. }