mockresponses.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. version int16
  164. }
  165. func NewMockFetchResponse(t TestReporter, batchSize int) *MockFetchResponse {
  166. return &MockFetchResponse{
  167. messages: make(map[string]map[int32]map[int64]Encoder),
  168. highWaterMarks: make(map[string]map[int32]int64),
  169. t: t,
  170. batchSize: batchSize,
  171. }
  172. }
  173. func (mfr *MockFetchResponse) SetVersion(version int16) *MockFetchResponse {
  174. mfr.version = version
  175. return mfr
  176. }
  177. func (mfr *MockFetchResponse) SetMessage(topic string, partition int32, offset int64, msg Encoder) *MockFetchResponse {
  178. partitions := mfr.messages[topic]
  179. if partitions == nil {
  180. partitions = make(map[int32]map[int64]Encoder)
  181. mfr.messages[topic] = partitions
  182. }
  183. messages := partitions[partition]
  184. if messages == nil {
  185. messages = make(map[int64]Encoder)
  186. partitions[partition] = messages
  187. }
  188. messages[offset] = msg
  189. return mfr
  190. }
  191. func (mfr *MockFetchResponse) SetHighWaterMark(topic string, partition int32, offset int64) *MockFetchResponse {
  192. partitions := mfr.highWaterMarks[topic]
  193. if partitions == nil {
  194. partitions = make(map[int32]int64)
  195. mfr.highWaterMarks[topic] = partitions
  196. }
  197. partitions[partition] = offset
  198. return mfr
  199. }
  200. func (mfr *MockFetchResponse) For(reqBody versionedDecoder) encoder {
  201. fetchRequest := reqBody.(*FetchRequest)
  202. res := &FetchResponse{
  203. Version: mfr.version,
  204. }
  205. for topic, partitions := range fetchRequest.blocks {
  206. for partition, block := range partitions {
  207. initialOffset := block.fetchOffset
  208. offset := initialOffset
  209. maxOffset := initialOffset + int64(mfr.getMessageCount(topic, partition))
  210. for i := 0; i < mfr.batchSize && offset < maxOffset; {
  211. msg := mfr.getMessage(topic, partition, offset)
  212. if msg != nil {
  213. res.AddMessage(topic, partition, nil, msg, offset)
  214. i++
  215. }
  216. offset++
  217. }
  218. fb := res.GetBlock(topic, partition)
  219. if fb == nil {
  220. res.AddError(topic, partition, ErrNoError)
  221. fb = res.GetBlock(topic, partition)
  222. }
  223. fb.HighWaterMarkOffset = mfr.getHighWaterMark(topic, partition)
  224. }
  225. }
  226. return res
  227. }
  228. func (mfr *MockFetchResponse) getMessage(topic string, partition int32, offset int64) Encoder {
  229. partitions := mfr.messages[topic]
  230. if partitions == nil {
  231. return nil
  232. }
  233. messages := partitions[partition]
  234. if messages == nil {
  235. return nil
  236. }
  237. return messages[offset]
  238. }
  239. func (mfr *MockFetchResponse) getMessageCount(topic string, partition int32) int {
  240. partitions := mfr.messages[topic]
  241. if partitions == nil {
  242. return 0
  243. }
  244. messages := partitions[partition]
  245. if messages == nil {
  246. return 0
  247. }
  248. return len(messages)
  249. }
  250. func (mfr *MockFetchResponse) getHighWaterMark(topic string, partition int32) int64 {
  251. partitions := mfr.highWaterMarks[topic]
  252. if partitions == nil {
  253. return 0
  254. }
  255. return partitions[partition]
  256. }
  257. // MockConsumerMetadataResponse is a `ConsumerMetadataResponse` builder.
  258. type MockConsumerMetadataResponse struct {
  259. coordinators map[string]interface{}
  260. t TestReporter
  261. }
  262. func NewMockConsumerMetadataResponse(t TestReporter) *MockConsumerMetadataResponse {
  263. return &MockConsumerMetadataResponse{
  264. coordinators: make(map[string]interface{}),
  265. t: t,
  266. }
  267. }
  268. func (mr *MockConsumerMetadataResponse) SetCoordinator(group string, broker *MockBroker) *MockConsumerMetadataResponse {
  269. mr.coordinators[group] = broker
  270. return mr
  271. }
  272. func (mr *MockConsumerMetadataResponse) SetError(group string, kerror KError) *MockConsumerMetadataResponse {
  273. mr.coordinators[group] = kerror
  274. return mr
  275. }
  276. func (mr *MockConsumerMetadataResponse) For(reqBody versionedDecoder) encoder {
  277. req := reqBody.(*ConsumerMetadataRequest)
  278. group := req.ConsumerGroup
  279. res := &ConsumerMetadataResponse{}
  280. v := mr.coordinators[group]
  281. switch v := v.(type) {
  282. case *MockBroker:
  283. res.Coordinator = &Broker{id: v.BrokerID(), addr: v.Addr()}
  284. case KError:
  285. res.Err = v
  286. }
  287. return res
  288. }
  289. // MockOffsetCommitResponse is a `OffsetCommitResponse` builder.
  290. type MockOffsetCommitResponse struct {
  291. errors map[string]map[string]map[int32]KError
  292. t TestReporter
  293. }
  294. func NewMockOffsetCommitResponse(t TestReporter) *MockOffsetCommitResponse {
  295. return &MockOffsetCommitResponse{t: t}
  296. }
  297. func (mr *MockOffsetCommitResponse) SetError(group, topic string, partition int32, kerror KError) *MockOffsetCommitResponse {
  298. if mr.errors == nil {
  299. mr.errors = make(map[string]map[string]map[int32]KError)
  300. }
  301. topics := mr.errors[group]
  302. if topics == nil {
  303. topics = make(map[string]map[int32]KError)
  304. mr.errors[group] = topics
  305. }
  306. partitions := topics[topic]
  307. if partitions == nil {
  308. partitions = make(map[int32]KError)
  309. topics[topic] = partitions
  310. }
  311. partitions[partition] = kerror
  312. return mr
  313. }
  314. func (mr *MockOffsetCommitResponse) For(reqBody versionedDecoder) encoder {
  315. req := reqBody.(*OffsetCommitRequest)
  316. group := req.ConsumerGroup
  317. res := &OffsetCommitResponse{}
  318. for topic, partitions := range req.blocks {
  319. for partition := range partitions {
  320. res.AddError(topic, partition, mr.getError(group, topic, partition))
  321. }
  322. }
  323. return res
  324. }
  325. func (mr *MockOffsetCommitResponse) getError(group, topic string, partition int32) KError {
  326. topics := mr.errors[group]
  327. if topics == nil {
  328. return ErrNoError
  329. }
  330. partitions := topics[topic]
  331. if partitions == nil {
  332. return ErrNoError
  333. }
  334. kerror, ok := partitions[partition]
  335. if !ok {
  336. return ErrNoError
  337. }
  338. return kerror
  339. }
  340. // MockProduceResponse is a `ProduceResponse` builder.
  341. type MockProduceResponse struct {
  342. errors map[string]map[int32]KError
  343. t TestReporter
  344. }
  345. func NewMockProduceResponse(t TestReporter) *MockProduceResponse {
  346. return &MockProduceResponse{t: t}
  347. }
  348. func (mr *MockProduceResponse) SetError(topic string, partition int32, kerror KError) *MockProduceResponse {
  349. if mr.errors == nil {
  350. mr.errors = make(map[string]map[int32]KError)
  351. }
  352. partitions := mr.errors[topic]
  353. if partitions == nil {
  354. partitions = make(map[int32]KError)
  355. mr.errors[topic] = partitions
  356. }
  357. partitions[partition] = kerror
  358. return mr
  359. }
  360. func (mr *MockProduceResponse) For(reqBody versionedDecoder) encoder {
  361. req := reqBody.(*ProduceRequest)
  362. res := &ProduceResponse{}
  363. for topic, partitions := range req.msgSets {
  364. for partition := range partitions {
  365. res.AddTopicPartition(topic, partition, mr.getError(topic, partition))
  366. }
  367. }
  368. return res
  369. }
  370. func (mr *MockProduceResponse) getError(topic string, partition int32) KError {
  371. partitions := mr.errors[topic]
  372. if partitions == nil {
  373. return ErrNoError
  374. }
  375. kerror, ok := partitions[partition]
  376. if !ok {
  377. return ErrNoError
  378. }
  379. return kerror
  380. }
  381. // MockOffsetFetchResponse is a `OffsetFetchResponse` builder.
  382. type MockOffsetFetchResponse struct {
  383. offsets map[string]map[string]map[int32]*OffsetFetchResponseBlock
  384. t TestReporter
  385. }
  386. func NewMockOffsetFetchResponse(t TestReporter) *MockOffsetFetchResponse {
  387. return &MockOffsetFetchResponse{t: t}
  388. }
  389. func (mr *MockOffsetFetchResponse) SetOffset(group, topic string, partition int32, offset int64, metadata string, kerror KError) *MockOffsetFetchResponse {
  390. if mr.offsets == nil {
  391. mr.offsets = make(map[string]map[string]map[int32]*OffsetFetchResponseBlock)
  392. }
  393. topics := mr.offsets[group]
  394. if topics == nil {
  395. topics = make(map[string]map[int32]*OffsetFetchResponseBlock)
  396. mr.offsets[group] = topics
  397. }
  398. partitions := topics[topic]
  399. if partitions == nil {
  400. partitions = make(map[int32]*OffsetFetchResponseBlock)
  401. topics[topic] = partitions
  402. }
  403. partitions[partition] = &OffsetFetchResponseBlock{offset, metadata, kerror}
  404. return mr
  405. }
  406. func (mr *MockOffsetFetchResponse) For(reqBody versionedDecoder) encoder {
  407. req := reqBody.(*OffsetFetchRequest)
  408. group := req.ConsumerGroup
  409. res := &OffsetFetchResponse{}
  410. for topic, partitions := range mr.offsets[group] {
  411. for partition, block := range partitions {
  412. res.AddBlock(topic, partition, block)
  413. }
  414. }
  415. return res
  416. }