metadata_response_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package sarama
  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].addr != "localhost:51" {
  49. t.Error("Decoding produced invalid broker 0 address.")
  50. }
  51. if response.Brokers[1].id != 0x010203 {
  52. t.Error("Decoding produced invalid broker 1 id.")
  53. }
  54. if response.Brokers[1].addr != "google.com:273" {
  55. t.Error("Decoding produced invalid broker 1 address.")
  56. }
  57. } else {
  58. t.Error("Decoding produced", len(response.Brokers), "brokers where there were two!")
  59. }
  60. if len(response.Topics) != 0 {
  61. t.Error("Decoding produced", len(response.Topics), "topics where there were none!")
  62. }
  63. }
  64. func TestMetadataResponseWithTopics(t *testing.T) {
  65. response := MetadataResponse{}
  66. testDecodable(t, "topics, no brokers", &response, topicsNoBrokersMetadataResponse)
  67. if len(response.Brokers) != 0 {
  68. t.Error("Decoding produced", len(response.Brokers), "brokers where there were none!")
  69. }
  70. if len(response.Topics) == 2 {
  71. if response.Topics[0].Err != NoError {
  72. t.Error("Decoding produced invalid topic 0 error.")
  73. }
  74. if response.Topics[0].Name != "foo" {
  75. t.Error("Decoding produced invalid topic 0 name.")
  76. }
  77. if len(response.Topics[0].Partitions) == 1 {
  78. if response.Topics[0].Partitions[0].Err != InvalidMessageSize {
  79. t.Error("Decoding produced invalid topic 0 partition 0 error.")
  80. }
  81. if response.Topics[0].Partitions[0].ID != 0x01 {
  82. t.Error("Decoding produced invalid topic 0 partition 0 id.")
  83. }
  84. if response.Topics[0].Partitions[0].Leader != 0x07 {
  85. t.Error("Decoding produced invalid topic 0 partition 0 leader.")
  86. }
  87. if len(response.Topics[0].Partitions[0].Replicas) == 3 {
  88. for i := 0; i < 3; i++ {
  89. if response.Topics[0].Partitions[0].Replicas[i] != int32(i+1) {
  90. t.Error("Decoding produced invalid topic 0 partition 0 replica", i)
  91. }
  92. }
  93. } else {
  94. t.Error("Decoding produced invalid topic 0 partition 0 replicas.")
  95. }
  96. if len(response.Topics[0].Partitions[0].Isr) != 0 {
  97. t.Error("Decoding produced invalid topic 0 partition 0 isr length.")
  98. }
  99. } else {
  100. t.Error("Decoding produced invalid partition count for topic 0.")
  101. }
  102. if response.Topics[1].Err != NoError {
  103. t.Error("Decoding produced invalid topic 1 error.")
  104. }
  105. if response.Topics[1].Name != "bar" {
  106. t.Error("Decoding produced invalid topic 0 name.")
  107. }
  108. if len(response.Topics[1].Partitions) != 0 {
  109. t.Error("Decoding produced invalid partition count for topic 1.")
  110. }
  111. } else {
  112. t.Error("Decoding produced", len(response.Topics), "topics where there were two!")
  113. }
  114. }