metadata_request_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package sarama
  2. import "testing"
  3. var (
  4. // The v0 metadata request has a non-nullable array of topic names
  5. // to request metadata for. An empty array fetches metadata for all topics
  6. metadataRequestNoTopicsV0 = []byte{
  7. 0x00, 0x00, 0x00, 0x00,
  8. }
  9. metadataRequestOneTopicV0 = []byte{
  10. 0x00, 0x00, 0x00, 0x01,
  11. 0x00, 0x06, 't', 'o', 'p', 'i', 'c', '1',
  12. }
  13. metadataRequestThreeTopicsV0 = []byte{
  14. 0x00, 0x00, 0x00, 0x03,
  15. 0x00, 0x03, 'f', 'o', 'o',
  16. 0x00, 0x03, 'b', 'a', 'r',
  17. 0x00, 0x03, 'b', 'a', 'z',
  18. }
  19. // The v1 metadata request is the same as v0 except that the array is now
  20. // nullable and should be explicitly null if all topics are required (an
  21. // empty list requests no topics)
  22. metadataRequestNoTopicsV1 = []byte{
  23. 0xff, 0xff, 0xff, 0xff,
  24. }
  25. metadataRequestOneTopicV1 = metadataRequestOneTopicV0
  26. metadataRequestThreeTopicsV1 = metadataRequestThreeTopicsV0
  27. // The v2 metadata request is the same as v1. An additional field for
  28. // cluster id has been added to the v2 metadata response
  29. metadataRequestNoTopicsV2 = metadataRequestNoTopicsV1
  30. metadataRequestOneTopicV2 = metadataRequestOneTopicV1
  31. metadataRequestThreeTopicsV2 = metadataRequestThreeTopicsV1
  32. // The v3 metadata request is the same as v1 and v2. An additional field
  33. // for throttle time has been added to the v3 metadata response
  34. metadataRequestNoTopicsV3 = metadataRequestNoTopicsV2
  35. metadataRequestOneTopicV3 = metadataRequestOneTopicV2
  36. metadataRequestThreeTopicsV3 = metadataRequestThreeTopicsV2
  37. // The v4 metadata request has an additional field for allowing auto topic
  38. // creation. The response is the same as v3.
  39. metadataRequestNoTopicsV4 = append(metadataRequestNoTopicsV1, byte(0))
  40. metadataRequestAutoCreateV4 = append(metadataRequestOneTopicV3, byte(1))
  41. metadataRequestNoAutoCreateV4 = append(metadataRequestOneTopicV3, byte(0))
  42. // The v5 metadata request is the same as v4. An additional field for
  43. // offline_replicas has been added to the v5 metadata response
  44. metadataRequestNoTopicsV5 = append(metadataRequestNoTopicsV1, byte(0))
  45. metadataRequestAutoCreateV5 = append(metadataRequestOneTopicV3, byte(1))
  46. metadataRequestNoAutoCreateV5 = append(metadataRequestOneTopicV3, byte(0))
  47. )
  48. func TestMetadataRequestV0(t *testing.T) {
  49. request := new(MetadataRequest)
  50. testRequest(t, "no topics", request, metadataRequestNoTopicsV0)
  51. request.Topics = []string{"topic1"}
  52. testRequest(t, "one topic", request, metadataRequestOneTopicV0)
  53. request.Topics = []string{"foo", "bar", "baz"}
  54. testRequest(t, "three topics", request, metadataRequestThreeTopicsV0)
  55. }
  56. func TestMetadataRequestV1(t *testing.T) {
  57. request := new(MetadataRequest)
  58. request.Version = 1
  59. testRequest(t, "no topics", request, metadataRequestNoTopicsV1)
  60. request.Topics = []string{"topic1"}
  61. testRequest(t, "one topic", request, metadataRequestOneTopicV1)
  62. request.Topics = []string{"foo", "bar", "baz"}
  63. testRequest(t, "three topics", request, metadataRequestThreeTopicsV1)
  64. }
  65. func TestMetadataRequestV2(t *testing.T) {
  66. request := new(MetadataRequest)
  67. request.Version = 2
  68. testRequest(t, "no topics", request, metadataRequestNoTopicsV2)
  69. request.Topics = []string{"topic1"}
  70. testRequest(t, "one topic", request, metadataRequestOneTopicV2)
  71. request.Topics = []string{"foo", "bar", "baz"}
  72. testRequest(t, "three topics", request, metadataRequestThreeTopicsV2)
  73. }
  74. func TestMetadataRequestV3(t *testing.T) {
  75. request := new(MetadataRequest)
  76. request.Version = 3
  77. testRequest(t, "no topics", request, metadataRequestNoTopicsV3)
  78. request.Topics = []string{"topic1"}
  79. testRequest(t, "one topic", request, metadataRequestOneTopicV3)
  80. request.Topics = []string{"foo", "bar", "baz"}
  81. testRequest(t, "three topics", request, metadataRequestThreeTopicsV3)
  82. }
  83. func TestMetadataRequestV4(t *testing.T) {
  84. request := new(MetadataRequest)
  85. request.Version = 4
  86. testRequest(t, "no topics", request, metadataRequestNoTopicsV4)
  87. request.Topics = []string{"topic1"}
  88. request.AllowAutoTopicCreation = true
  89. testRequest(t, "one topic", request, metadataRequestAutoCreateV4)
  90. request.AllowAutoTopicCreation = false
  91. testRequest(t, "one topic", request, metadataRequestNoAutoCreateV4)
  92. }
  93. func TestMetadataRequestV5(t *testing.T) {
  94. request := new(MetadataRequest)
  95. request.Version = 5
  96. testRequest(t, "no topics", request, metadataRequestNoTopicsV5)
  97. request.Topics = []string{"topic1"}
  98. request.AllowAutoTopicCreation = true
  99. testRequest(t, "one topic", request, metadataRequestAutoCreateV5)
  100. request.AllowAutoTopicCreation = false
  101. testRequest(t, "one topic", request, metadataRequestNoAutoCreateV5)
  102. }