metadata_response_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package protocol
  2. import "testing"
  3. import "sarama/types"
  4. var (
  5. emptyMetadataResponse = []byte{
  6. 0x00, 0x00, 0x00, 0x00,
  7. 0x00, 0x00, 0x00, 0x00}
  8. brokersNoTopicsMetadataResponse = []byte{
  9. 0x00, 0x00, 0x00, 0x02,
  10. 0x00, 0x00, 0xab, 0xff,
  11. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  12. 0x00, 0x00, 0x00, 0x33,
  13. 0x00, 0x01, 0x02, 0x03,
  14. 0x00, 0x0a, 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm',
  15. 0x00, 0x00, 0x01, 0x11,
  16. 0x00, 0x00, 0x00, 0x00}
  17. topicsNoBrokersMetadataResponse = []byte{
  18. 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x02,
  20. 0x00, 0x00,
  21. 0x00, 0x03, 'f', 'o', 'o',
  22. 0x00, 0x00, 0x00, 0x01,
  23. 0x00, 0x04,
  24. 0x00, 0x00, 0x00, 0x01,
  25. 0x00, 0x00, 0x00, 0x07,
  26. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
  27. 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00,
  29. 0x00, 0x03, 'b', 'a', 'r',
  30. 0x00, 0x00, 0x00, 0x00}
  31. )
  32. func TestEmptyMetadataResponse(t *testing.T) {
  33. response := MetadataResponse{}
  34. testDecodable(t, "empty", &response, emptyMetadataResponse)
  35. if len(response.Brokers) != 0 {
  36. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  37. }
  38. if len(response.Topics) != 0 {
  39. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  40. }
  41. }
  42. func TestMetadataResponseWithBrokers(t *testing.T) {
  43. response := MetadataResponse{}
  44. testDecodable(t, "brokers, no topics", &response, brokersNoTopicsMetadataResponse)
  45. if len(response.Brokers) == 2 {
  46. if response.Brokers[0].id != 0xabff {
  47. t.Error("Decoding produced invalid broker 0 id.")
  48. }
  49. if response.Brokers[0].host != "localhost" {
  50. t.Error("Decoding produced invalid broker 0 host.")
  51. }
  52. if response.Brokers[0].port != 0x33 {
  53. t.Error("Decoding produced invalid broker 0 port.")
  54. }
  55. if response.Brokers[1].id != 0x010203 {
  56. t.Error("Decoding produced invalid broker 1 id.")
  57. }
  58. if response.Brokers[1].host != "google.com" {
  59. t.Error("Decoding produced invalid broker 1 host.")
  60. }
  61. if response.Brokers[1].port != 0x111 {
  62. t.Error("Decoding produced invalid broker 1 port.")
  63. }
  64. } else {
  65. t.Error("Decoding produced", len(response.Brokers), "brokers where there were two!")
  66. }
  67. if len(response.Topics) != 0 {
  68. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  69. }
  70. }
  71. func TestMetadataResponseWithTopics(t *testing.T) {
  72. response := MetadataResponse{}
  73. testDecodable(t, "topics, no brokers", &response, topicsNoBrokersMetadataResponse)
  74. if len(response.Brokers) != 0 {
  75. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  76. }
  77. if len(response.Topics) == 2 {
  78. if response.Topics[0].Err != types.NO_ERROR {
  79. t.Error("Decoding produced invalid topic 0 error.")
  80. }
  81. if response.Topics[0].Name != "foo" {
  82. t.Error("Decoding produced invalid topic 0 name.")
  83. }
  84. if len(response.Topics[0].Partitions) == 1 {
  85. if response.Topics[0].Partitions[0].Err != types.INVALID_MESSAGE_SIZE {
  86. t.Error("Decoding produced invalid topic 0 partition 0 error.")
  87. }
  88. if response.Topics[0].Partitions[0].Id != 0x01 {
  89. t.Error("Decoding produced invalid topic 0 partition 0 id.")
  90. }
  91. if response.Topics[0].Partitions[0].Leader != 0x07 {
  92. t.Error("Decoding produced invalid topic 0 partition 0 leader.")
  93. }
  94. if len(response.Topics[0].Partitions[0].Replicas) == 3 {
  95. for i := 0; i < 3; i++ {
  96. if response.Topics[0].Partitions[0].Replicas[i] != int32(i+1) {
  97. t.Error("Decoding produced invalid topic 0 partition 0 replica", i)
  98. }
  99. }
  100. } else {
  101. t.Error("Decoding produced invalid topic 0 partition 0 replicas.")
  102. }
  103. if len(response.Topics[0].Partitions[0].Isr) != 0 {
  104. t.Error("Decoding produced invalid topic 0 partition 0 isr length.")
  105. }
  106. } else {
  107. t.Error("Decoding produced invalid partition count for topic 0.")
  108. }
  109. if response.Topics[1].Err != types.NO_ERROR {
  110. t.Error("Decoding produced invalid topic 1 error.")
  111. }
  112. if response.Topics[1].Name != "bar" {
  113. t.Error("Decoding produced invalid topic 0 name.")
  114. }
  115. if len(response.Topics[1].Partitions) != 0 {
  116. t.Error("Decoding produced invalid partition count for topic 1.")
  117. }
  118. } else {
  119. t.Error("Decoding produced", len(response.Topics), "topics where there were two!")
  120. }
  121. }