Skip to content

Commit cc2a707

Browse files
committed
proper test for large record batch length
Signed-off-by: Ryan Belgrave <[email protected]>
1 parent bc2f7cc commit cc2a707

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

record_test.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
package sarama
44

55
import (
6-
"fmt"
6+
"encoding/binary"
7+
"math"
78
"reflect"
89
"testing"
910
"time"
@@ -256,39 +257,49 @@ func TestRecordBatchDecoding(t *testing.T) {
256257
}
257258
}
258259

259-
func TestRecordBatchInvalidNumRecords(t *testing.T) {
260+
func TestRecordBatchLargeNumRecords(t *testing.T) {
261+
numOfRecords := 10 + (2 * math.MaxUint16)
262+
numofRecordsBytes := make([]byte, 4)
263+
binary.BigEndian.PutUint32(numofRecordsBytes, uint32(numOfRecords))
264+
260265
encodedBatch := []byte{
261266
0, 0, 0, 0, 0, 0, 0, 0, // First Offset
262-
0, 0, 0, 70, // Length
267+
0, 42, 0, 250, // Length
263268
0, 0, 0, 0, // Partition Leader Epoch
264-
2, // Version
265-
91, 48, 202, 99, // CRC
269+
2, // Version
270+
103, 68, 166, 213, // CRC
266271
0, 0, // Attributes
267272
0, 0, 0, 0, // Last Offset Delta
268273
0, 0, 1, 88, 141, 205, 89, 56, // First Timestamp
269274
0, 0, 0, 0, 0, 0, 0, 0, // Max Timestamp
270275
0, 0, 0, 0, 0, 0, 0, 0, // Producer ID
271276
0, 0, // Producer Epoch
272277
0, 0, 0, 0, // First Sequence
273-
0, 1, 255, 255, // Number of Records - 1 + 2*math.MaxUint16
274-
40, // Record Length
275-
0, // Attributes
276-
10, // Timestamp Delta
277-
0, // Offset Delta
278-
8, // Key Length
279-
1, 2, 3, 4,
280-
6, // Value Length
281-
5, 6, 7,
282-
2, // Number of Headers
283-
6, // Header Key Length
284-
8, 9, 10, // Header Key
285-
4, // Header Value Length
286-
11, 12, // Header Value
287278
}
288279

289-
batch := RecordBatch{}
280+
encodedBatch = append(encodedBatch, numofRecordsBytes...)
281+
282+
for range numOfRecords {
283+
encodedBatch = append(encodedBatch, []byte{
284+
40, // Record Length
285+
0, // Attributes
286+
10, // Timestamp Delta
287+
0, // Offset Delta
288+
8, // Key Length
289+
1, 2, 3, 4,
290+
6, // Value Length
291+
5, 6, 7,
292+
2, // Number of Headers
293+
6, // Header Key Length
294+
8, 9, 10, // Header Key
295+
4, // Header Value Length
296+
11, 12, // Header Value
297+
}...)
298+
}
299+
300+
var batch RecordBatch
290301
err := decode(encodedBatch, &batch, nil)
291-
if err != ErrInsufficientData {
292-
t.Fatal(fmt.Errorf("was suppose to get ErrInsufficientData, instead got: %w", err))
302+
if err != nil {
303+
t.Fatal("received error while decoding record batch", err)
293304
}
294305
}

0 commit comments

Comments
 (0)