metadata_response_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package sarama
  2. import "testing"
  3. var (
  4. emptyMetadataResponseV0 = []byte{
  5. 0x00, 0x00, 0x00, 0x00,
  6. 0x00, 0x00, 0x00, 0x00}
  7. brokersNoTopicsMetadataResponseV0 = []byte{
  8. 0x00, 0x00, 0x00, 0x02,
  9. 0x00, 0x00, 0xab, 0xff,
  10. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  11. 0x00, 0x00, 0x00, 0x33,
  12. 0x00, 0x01, 0x02, 0x03,
  13. 0x00, 0x0a, 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
  14. 0x00, 0x00, 0x01, 0x11,
  15. 0x00, 0x00, 0x00, 0x00}
  16. topicsNoBrokersMetadataResponseV0 = []byte{
  17. 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x02,
  19. 0x00, 0x00,
  20. 0x00, 0x03, 'f', 'o', 'o',
  21. 0x00, 0x00, 0x00, 0x01,
  22. 0x00, 0x04,
  23. 0x00, 0x00, 0x00, 0x01,
  24. 0x00, 0x00, 0x00, 0x07,
  25. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
  26. 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00,
  28. 0x00, 0x03, 'b', 'a', 'r',
  29. 0x00, 0x00, 0x00, 0x00}
  30. brokersNoTopicsMetadataResponseV1 = []byte{
  31. 0x00, 0x00, 0x00, 0x02,
  32. 0x00, 0x00, 0xab, 0xff,
  33. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  34. 0x00, 0x00, 0x00, 0x33,
  35. 0x00, 0x05, 'r', 'a', 'c', 'k', '0',
  36. 0x00, 0x01, 0x02, 0x03,
  37. 0x00, 0x0a, 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
  38. 0x00, 0x00, 0x01, 0x11,
  39. 0x00, 0x05, 'r', 'a', 'c', 'k', '1',
  40. 0x00, 0x00, 0x00, 0x01,
  41. 0x00, 0x00, 0x00, 0x00}
  42. topicsNoBrokersMetadataResponseV1 = []byte{
  43. 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x04,
  45. 0x00, 0x00, 0x00, 0x02,
  46. 0x00, 0x00,
  47. 0x00, 0x03, 'f', 'o', 'o',
  48. 0x00,
  49. 0x00, 0x00, 0x00, 0x01,
  50. 0x00, 0x04,
  51. 0x00, 0x00, 0x00, 0x01,
  52. 0x00, 0x00, 0x00, 0x07,
  53. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
  54. 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00,
  56. 0x00, 0x03, 'b', 'a', 'r',
  57. 0x01,
  58. 0x00, 0x00, 0x00, 0x00}
  59. noBrokersNoTopicsWithThrottleTimeAndClusterIDV3 = []byte{
  60. 0x00, 0x00, 0x00, 0x10,
  61. 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x09, 'c', 'l', 'u', 's', 't', 'e', 'r', 'I', 'd',
  63. 0x00, 0x00, 0x00, 0x01,
  64. 0x00, 0x00, 0x00, 0x00}
  65. noBrokersOneTopicWithOfflineReplicasV5 = []byte{
  66. 0x00, 0x00, 0x00, 0x05,
  67. 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x09, 'c', 'l', 'u', 's', 't', 'e', 'r', 'I', 'd',
  69. 0x00, 0x00, 0x00, 0x02,
  70. 0x00, 0x00, 0x00, 0x01,
  71. 0x00, 0x00,
  72. 0x00, 0x03, 'f', 'o', 'o',
  73. 0x00,
  74. 0x00, 0x00, 0x00, 0x01,
  75. 0x00, 0x04,
  76. 0x00, 0x00, 0x00, 0x01,
  77. 0x00, 0x00, 0x00, 0x07,
  78. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
  79. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
  80. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
  81. }
  82. )
  83. func TestEmptyMetadataResponseV0(t *testing.T) {
  84. response := MetadataResponse{}
  85. testVersionDecodable(t, "empty, V0", &response, emptyMetadataResponseV0, 0)
  86. if len(response.Brokers) != 0 {
  87. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  88. }
  89. if len(response.Topics) != 0 {
  90. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  91. }
  92. }
  93. func TestMetadataResponseWithBrokersV0(t *testing.T) {
  94. response := MetadataResponse{}
  95. testVersionDecodable(t, "brokers, no topics, V0", &response, brokersNoTopicsMetadataResponseV0, 0)
  96. if len(response.Brokers) != 2 {
  97. t.Fatal("Decoding produced", len(response.Brokers), "brokers where there were two!")
  98. }
  99. if response.Brokers[0].id != 0xabff {
  100. t.Error("Decoding produced invalid broker 0 id.")
  101. }
  102. if response.Brokers[0].addr != "localhost:51" {
  103. t.Error("Decoding produced invalid broker 0 address.")
  104. }
  105. if response.Brokers[1].id != 0x010203 {
  106. t.Error("Decoding produced invalid broker 1 id.")
  107. }
  108. if response.Brokers[1].addr != "google.com:273" {
  109. t.Error("Decoding produced invalid broker 1 address.")
  110. }
  111. if len(response.Topics) != 0 {
  112. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  113. }
  114. }
  115. func TestMetadataResponseWithTopicsV0(t *testing.T) {
  116. response := MetadataResponse{}
  117. testVersionDecodable(t, "topics, no brokers, V0", &response, topicsNoBrokersMetadataResponseV0, 0)
  118. if len(response.Brokers) != 0 {
  119. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  120. }
  121. if len(response.Topics) != 2 {
  122. t.Fatal("Decoding produced", len(response.Topics), "topics where there were two!")
  123. }
  124. if response.Topics[0].Err != ErrNoError {
  125. t.Error("Decoding produced invalid topic 0 error.")
  126. }
  127. if response.Topics[0].Name != "foo" {
  128. t.Error("Decoding produced invalid topic 0 name.")
  129. }
  130. if len(response.Topics[0].Partitions) != 1 {
  131. t.Fatal("Decoding produced invalid partition count for topic 0.")
  132. }
  133. if response.Topics[0].Partitions[0].Err != ErrInvalidMessageSize {
  134. t.Error("Decoding produced invalid topic 0 partition 0 error.")
  135. }
  136. if response.Topics[0].Partitions[0].ID != 0x01 {
  137. t.Error("Decoding produced invalid topic 0 partition 0 id.")
  138. }
  139. if response.Topics[0].Partitions[0].Leader != 0x07 {
  140. t.Error("Decoding produced invalid topic 0 partition 0 leader.")
  141. }
  142. if len(response.Topics[0].Partitions[0].Replicas) != 3 {
  143. t.Fatal("Decoding produced invalid topic 0 partition 0 replicas.")
  144. }
  145. for i := 0; i < 3; i++ {
  146. if response.Topics[0].Partitions[0].Replicas[i] != int32(i+1) {
  147. t.Error("Decoding produced invalid topic 0 partition 0 replica", i)
  148. }
  149. }
  150. if len(response.Topics[0].Partitions[0].Isr) != 0 {
  151. t.Error("Decoding produced invalid topic 0 partition 0 isr length.")
  152. }
  153. if response.Topics[1].Err != ErrNoError {
  154. t.Error("Decoding produced invalid topic 1 error.")
  155. }
  156. if response.Topics[1].Name != "bar" {
  157. t.Error("Decoding produced invalid topic 0 name.")
  158. }
  159. if len(response.Topics[1].Partitions) != 0 {
  160. t.Error("Decoding produced invalid partition count for topic 1.")
  161. }
  162. }
  163. func TestMetadataResponseWithBrokersV1(t *testing.T) {
  164. response := MetadataResponse{}
  165. testVersionDecodable(t, "topics, V1", &response, brokersNoTopicsMetadataResponseV1, 1)
  166. if len(response.Brokers) != 2 {
  167. t.Error("Decoding produced", len(response.Brokers), "brokers where there were 2!")
  168. }
  169. if response.Brokers[0].rack == nil || *response.Brokers[0].rack != "rack0" {
  170. t.Error("Decoding produced invalid broker 0 rack.")
  171. }
  172. if response.Brokers[1].rack == nil || *response.Brokers[1].rack != "rack1" {
  173. t.Error("Decoding produced invalid broker 1 rack.")
  174. }
  175. if response.ControllerID != 1 {
  176. t.Error("Decoding produced", response.ControllerID, "should have been 1!")
  177. }
  178. if len(response.Topics) != 0 {
  179. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  180. }
  181. }
  182. func TestMetadataResponseWithTopicsV1(t *testing.T) {
  183. response := MetadataResponse{}
  184. testVersionDecodable(t, "topics, V1", &response, topicsNoBrokersMetadataResponseV1, 1)
  185. if len(response.Brokers) != 0 {
  186. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  187. }
  188. if response.ControllerID != 4 {
  189. t.Error("Decoding produced", response.ControllerID, "should have been 4!")
  190. }
  191. if len(response.Topics) != 2 {
  192. t.Error("Decoding produced", len(response.Topics), "topics where there were 2!")
  193. }
  194. if response.Topics[0].IsInternal {
  195. t.Error("Decoding produced", response.Topics[0], "topic0 should have been false!")
  196. }
  197. if !response.Topics[1].IsInternal {
  198. t.Error("Decoding produced", response.Topics[1], "topic1 should have been true!")
  199. }
  200. }
  201. func TestMetadataResponseWithThrottleTime(t *testing.T) {
  202. response := MetadataResponse{}
  203. testVersionDecodable(t, "no topics, no brokers, throttle time and cluster Id V3", &response, noBrokersNoTopicsWithThrottleTimeAndClusterIDV3, 3)
  204. if response.ThrottleTimeMs != int32(16) {
  205. t.Error("Decoding produced", response.ThrottleTimeMs, "should have been 16!")
  206. }
  207. if len(response.Brokers) != 0 {
  208. t.Error("Decoding produced", response.Brokers, "should have been 0!")
  209. }
  210. if response.ControllerID != int32(1) {
  211. t.Error("Decoding produced", response.ControllerID, "should have been 1!")
  212. }
  213. if *response.ClusterID != "clusterId" {
  214. t.Error("Decoding produced", response.ClusterID, "should have been clusterId!")
  215. }
  216. if len(response.Topics) != 0 {
  217. t.Error("Decoding produced", len(response.Topics), "should have been 0!")
  218. }
  219. }
  220. func TestMetadataResponseWithOfflineReplicasV5(t *testing.T) {
  221. response := MetadataResponse{}
  222. testVersionDecodable(t, "no brokers, 1 topic with offline replica V5", &response, noBrokersOneTopicWithOfflineReplicasV5, 5)
  223. if response.ThrottleTimeMs != int32(5) {
  224. t.Error("Decoding produced", response.ThrottleTimeMs, "should have been 5!")
  225. }
  226. if len(response.Brokers) != 0 {
  227. t.Error("Decoding produced", response.Brokers, "should have been 0!")
  228. }
  229. if response.ControllerID != int32(2) {
  230. t.Error("Decoding produced", response.ControllerID, "should have been 21!")
  231. }
  232. if *response.ClusterID != "clusterId" {
  233. t.Error("Decoding produced", response.ClusterID, "should have been clusterId!")
  234. }
  235. if len(response.Topics) != 1 {
  236. t.Error("Decoding produced", len(response.Topics), "should have been 1!")
  237. }
  238. if len(response.Topics[0].Partitions[0].OfflineReplicas) != 1 {
  239. t.Error("Decoding produced", len(response.Topics[0].Partitions[0].OfflineReplicas), "should have been 1!")
  240. }
  241. }