fetch_request_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package sarama
  2. import "testing"
  3. var (
  4. fetchRequestNoBlocks = []byte{
  5. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6. 0x00, 0x00, 0x00, 0x00}
  7. fetchRequestWithProperties = []byte{
  8. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xEF,
  9. 0x00, 0x00, 0x00, 0x00}
  10. fetchRequestOneBlock = []byte{
  11. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12. 0x00, 0x00, 0x00, 0x01,
  13. 0x00, 0x05, 't', 'o', 'p', 'i', 'c',
  14. 0x00, 0x00, 0x00, 0x01,
  15. 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x56}
  16. fetchRequestOneBlockV4 = []byte{
  17. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0xFF,
  19. 0x01,
  20. 0x00, 0x00, 0x00, 0x01,
  21. 0x00, 0x05, 't', 'o', 'p', 'i', 'c',
  22. 0x00, 0x00, 0x00, 0x01,
  23. 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x56}
  24. fetchRequestOneBlockV11 = []byte{
  25. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0xFF,
  27. 0x01,
  28. 0x00, 0x00, 0x00, 0xAA, // sessionID
  29. 0x00, 0x00, 0x00, 0xEE, // sessionEpoch
  30. 0x00, 0x00, 0x00, 0x01,
  31. 0x00, 0x05, 't', 'o', 'p', 'i', 'c',
  32. 0x00, 0x00, 0x00, 0x01,
  33. 0x00, 0x00, 0x00, 0x12, // partitionID
  34. 0xFF, 0xFF, 0xFF, 0xFF, // currentLeaderEpoch
  35. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, // fetchOffset
  36. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // logStartOffset
  37. 0x00, 0x00, 0x00, 0x56, // maxBytes
  38. 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x06, 'r', 'a', 'c', 'k', '0', '1', // rackID
  40. }
  41. )
  42. func TestFetchRequest(t *testing.T) {
  43. t.Run("no blocks", func(t *testing.T) {
  44. request := new(FetchRequest)
  45. request.ReplicaID = -1
  46. request.Topics = []FetchableTopic{}
  47. testRequest(t, "no blocks", request, fetchRequestNoBlocks)
  48. })
  49. t.Run("with properties", func(t *testing.T) {
  50. request := new(FetchRequest)
  51. request.ReplicaID = -1
  52. request.MaxWait = 0x20
  53. request.MinBytes = 0xEF
  54. request.Topics = []FetchableTopic{}
  55. testRequest(t, "with properties", request, fetchRequestWithProperties)
  56. })
  57. // AddBlock(topic string, partitionID int32, fetchOffset int64, maxBytes int32) {
  58. t.Run("one block", func(t *testing.T) {
  59. request := new(FetchRequest)
  60. request.ReplicaID = -1
  61. request.MaxWait = 0
  62. request.MinBytes = 0
  63. request.Topics = []FetchableTopic{
  64. {
  65. Name: "topic",
  66. FetchPartitions: []FetchPartition{
  67. {
  68. PartitionIndex: 0x12,
  69. FetchOffset: 0x34,
  70. MaxBytes: 0x56,
  71. },
  72. },
  73. },
  74. }
  75. testRequest(t, "one block", request, fetchRequestOneBlock)
  76. })
  77. t.Run("one block v4", func(t *testing.T) {
  78. request := new(FetchRequest)
  79. request.Version = 4
  80. request.ReplicaID = -1
  81. request.MaxBytes = 0xFF
  82. request.IsolationLevel = ReadCommitted
  83. request.Topics = []FetchableTopic{
  84. {
  85. Version: 4,
  86. Name: "topic",
  87. FetchPartitions: []FetchPartition{
  88. {
  89. Version: 4,
  90. PartitionIndex: 0x12,
  91. FetchOffset: 0x34,
  92. MaxBytes: 0x56,
  93. },
  94. },
  95. },
  96. }
  97. testRequest(t, "one block v4", request, fetchRequestOneBlockV4)
  98. })
  99. t.Run("one block v11 rackid", func(t *testing.T) {
  100. request := new(FetchRequest)
  101. request.Version = 11
  102. request.ReplicaID = -1
  103. request.MaxBytes = 0xFF
  104. request.IsolationLevel = ReadCommitted
  105. request.SessionID = 0xAA
  106. request.Epoch = 0xEE
  107. request.Topics = []FetchableTopic{
  108. {
  109. Version: 11,
  110. Name: "topic",
  111. FetchPartitions: []FetchPartition{
  112. {
  113. Version: 11,
  114. PartitionIndex: 0x12,
  115. CurrentLeaderEpoch: -1,
  116. FetchOffset: 0x34,
  117. MaxBytes: 0x56,
  118. },
  119. },
  120. },
  121. }
  122. request.Forgotten = []ForgottenTopic{}
  123. request.RackID = "rack01"
  124. testRequest(t, "one block v11 rackid", request, fetchRequestOneBlockV11)
  125. })
  126. }