metadata_response_test.go 3.9 KB

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