metadata_request_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package sarama
  2. import "testing"
  3. var (
  4. metadataRequestNoTopicsV0 = []byte{
  5. 0x00, 0x00, 0x00, 0x00}
  6. metadataRequestOneTopicV0 = []byte{
  7. 0x00, 0x00, 0x00, 0x01,
  8. 0x00, 0x06, 't', 'o', 'p', 'i', 'c', '1'}
  9. metadataRequestThreeTopicsV0 = []byte{
  10. 0x00, 0x00, 0x00, 0x03,
  11. 0x00, 0x03, 'f', 'o', 'o',
  12. 0x00, 0x03, 'b', 'a', 'r',
  13. 0x00, 0x03, 'b', 'a', 'z'}
  14. metadataRequestNoTopicsV1 = []byte{
  15. 0xff, 0xff, 0xff, 0xff}
  16. metadataRequestAutoCreateV4 = append(metadataRequestOneTopicV0, byte(1))
  17. metadataRequestNoAutoCreateV4 = append(metadataRequestOneTopicV0, byte(0))
  18. )
  19. func TestMetadataRequestV0(t *testing.T) {
  20. request := new(MetadataRequest)
  21. testRequest(t, "no topics", request, metadataRequestNoTopicsV0)
  22. request.Topics = []string{"topic1"}
  23. testRequest(t, "one topic", request, metadataRequestOneTopicV0)
  24. request.Topics = []string{"foo", "bar", "baz"}
  25. testRequest(t, "three topics", request, metadataRequestThreeTopicsV0)
  26. }
  27. func TestMetadataRequestV1(t *testing.T) {
  28. request := new(MetadataRequest)
  29. request.Version = 1
  30. testRequest(t, "no topics", request, metadataRequestNoTopicsV1)
  31. request.Topics = []string{"topic1"}
  32. testRequest(t, "one topic", request, metadataRequestOneTopicV0)
  33. request.Topics = []string{"foo", "bar", "baz"}
  34. testRequest(t, "three topics", request, metadataRequestThreeTopicsV0)
  35. }
  36. func TestMetadataRequestV2(t *testing.T) {
  37. request := new(MetadataRequest)
  38. request.Version = 2
  39. testRequest(t, "no topics", request, metadataRequestNoTopicsV1)
  40. request.Topics = []string{"topic1"}
  41. testRequest(t, "one topic", request, metadataRequestOneTopicV0)
  42. }
  43. func TestMetadataRequestV3(t *testing.T) {
  44. request := new(MetadataRequest)
  45. request.Version = 3
  46. testRequest(t, "no topics", request, metadataRequestNoTopicsV1)
  47. request.Topics = []string{"topic1"}
  48. testRequest(t, "one topic", request, metadataRequestOneTopicV0)
  49. }
  50. func TestMetadataRequestV4(t *testing.T) {
  51. request := new(MetadataRequest)
  52. request.Version = 4
  53. request.Topics = []string{"topic1"}
  54. request.AllowAutoTopicCreation = true
  55. testRequest(t, "one topic", request, metadataRequestAutoCreateV4)
  56. request.AllowAutoTopicCreation = false
  57. testRequest(t, "one topic", request, metadataRequestNoAutoCreateV4)
  58. }