metadata_response_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package protocol
  2. import "testing"
  3. var (
  4. emptyMetadataResponse = []byte{
  5. 0x00, 0x00, 0x00, 0x00,
  6. 0x00, 0x00, 0x00, 0x00}
  7. brokersNoTopicsMetadataResponse = []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. topicsNoBrokersMetadataResponse = []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. )
  31. func TestEmptyMetadataResponse(t *testing.T) {
  32. response := MetadataResponse{}
  33. testDecodable(t, "empty", &response, emptyMetadataResponse)
  34. if len(response.Brokers) != 0 {
  35. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  36. }
  37. if len(response.Topics) != 0 {
  38. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  39. }
  40. }
  41. func TestMetadataResponseWithBrokers(t *testing.T) {
  42. response := MetadataResponse{}
  43. testDecodable(t, "brokers, no topics", &response, brokersNoTopicsMetadataResponse)
  44. if len(response.Brokers) == 2 {
  45. if response.Brokers[0].id != 0xabff {
  46. t.Error("Decoding produced invalid broker 0 id.")
  47. }
  48. if response.Brokers[0].host != "localhost" {
  49. t.Error("Decoding produced invalid broker 0 host.")
  50. }
  51. if response.Brokers[0].port != 0x33 {
  52. t.Error("Decoding produced invalid broker 0 port.")
  53. }
  54. if response.Brokers[1].id != 0x010203 {
  55. t.Error("Decoding produced invalid broker 1 id.")
  56. }
  57. if response.Brokers[1].host != "google.com" {
  58. t.Error("Decoding produced invalid broker 1 host.")
  59. }
  60. if response.Brokers[1].port != 0x111 {
  61. t.Error("Decoding produced invalid broker 1 port.")
  62. }
  63. } else {
  64. t.Error("Decoding produced", len(response.Brokers), "brokers where there were two!")
  65. }
  66. if len(response.Topics) != 0 {
  67. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  68. }
  69. }
  70. func TestMetadataResponseWithTopics(t *testing.T) {
  71. response := MetadataResponse{}
  72. testDecodable(t, "topics, no brokers", &response, topicsNoBrokersMetadataResponse)
  73. if len(response.Brokers) != 0 {
  74. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  75. }
  76. if len(response.Topics) == 2 {
  77. if response.Topics[0].Err != NO_ERROR {
  78. t.Error("Decoding produced invalid topic 0 error.")
  79. }
  80. if response.Topics[0].Name != "foo" {
  81. t.Error("Decoding produced invalid topic 0 name.")
  82. }
  83. if len(response.Topics[0].Partitions) == 1 {
  84. if response.Topics[0].Partitions[0].Err != INVALID_MESSAGE_SIZE {
  85. t.Error("Decoding produced invalid topic 0 partition 0 error.")
  86. }
  87. if response.Topics[0].Partitions[0].Id != 0x01 {
  88. t.Error("Decoding produced invalid topic 0 partition 0 id.")
  89. }
  90. if response.Topics[0].Partitions[0].Leader != 0x07 {
  91. t.Error("Decoding produced invalid topic 0 partition 0 leader.")
  92. }
  93. if len(response.Topics[0].Partitions[0].Replicas) == 3 {
  94. for i:=0; i<3; i++ {
  95. if response.Topics[0].Partitions[0].Replicas[i] != int32(i+1) {
  96. t.Error("Decoding produced invalid topic 0 partition 0 replica", i)
  97. }
  98. }
  99. } else {
  100. t.Error("Decoding produced invalid topic 0 partition 0 replicas.")
  101. }
  102. if len(response.Topics[0].Partitions[0].Isr) != 0 {
  103. t.Error("Decoding produced invalid topic 0 partition 0 isr length.")
  104. }
  105. } else {
  106. t.Error("Decoding produced invalid partition count for topic 0.")
  107. }
  108. if response.Topics[1].Err != NO_ERROR {
  109. t.Error("Decoding produced invalid topic 1 error.")
  110. }
  111. if response.Topics[1].Name != "bar" {
  112. t.Error("Decoding produced invalid topic 0 name.")
  113. }
  114. if len(response.Topics[1].Partitions) != 0 {
  115. t.Error("Decoding produced invalid partition count for topic 1.")
  116. }
  117. } else {
  118. t.Error("Decoding produced", len(response.Topics), "topics where there were two!")
  119. }
  120. }