-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathoffset_response_test.go
135 lines (104 loc) · 3.72 KB
/
offset_response_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package sarama
import (
"errors"
"testing"
)
var (
emptyOffsetResponse = []byte{
0x00, 0x00, 0x00, 0x00,
}
normalOffsetResponse = []byte{
0x00, 0x00, 0x00, 0x02,
0x00, 0x01, 'a',
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 'z',
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
}
normalOffsetResponseV1 = []byte{
0x00, 0x00, 0x00, 0x02,
0x00, 0x01, 'a',
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 'z',
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00,
0x00, 0x00, 0x01, 0x58, 0x1A, 0xE6, 0x48, 0x86,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
}
offsetResponseV4 = []byte{
0x00, 0x00, 0x00, 0x00, // throttle time
0x00, 0x00, 0x00, 0x01, // length of topics
0x00, 0x04, 0x64, 0x6e, 0x77, 0x65, // topic name
0x00, 0x00, 0x00, 0x01, // length of partitions
0x00, 0x00, 0x00, 0x09, // partitionID
0x00, 0x00, // err
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // timestamp
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // offset
0xff, 0xff, 0xff, 0xff, // leaderEpoch
}
)
func TestEmptyOffsetResponse(t *testing.T) {
response := OffsetResponse{}
testVersionDecodable(t, "empty", &response, emptyOffsetResponse, 0)
if len(response.Blocks) != 0 {
t.Error("Decoding produced", len(response.Blocks), "topics where there were none.")
}
response = OffsetResponse{}
testVersionDecodable(t, "empty", &response, emptyOffsetResponse, 1)
if len(response.Blocks) != 0 {
t.Error("Decoding produced", len(response.Blocks), "topics where there were none.")
}
}
func TestNormalOffsetResponse(t *testing.T) {
response := OffsetResponse{}
testVersionDecodable(t, "normal", &response, normalOffsetResponse, 0)
if len(response.Blocks) != 2 {
t.Fatal("Decoding produced", len(response.Blocks), "topics where there were two.")
}
if len(response.Blocks["a"]) != 0 {
t.Fatal("Decoding produced", len(response.Blocks["a"]), "partitions for topic 'a' where there were none.")
}
if len(response.Blocks["z"]) != 1 {
t.Fatal("Decoding produced", len(response.Blocks["z"]), "partitions for topic 'z' where there was one.")
}
if !errors.Is(response.Blocks["z"][2].Err, ErrNoError) {
t.Fatal("Decoding produced invalid error for topic z partition 2.")
}
if len(response.Blocks["z"][2].Offsets) != 2 {
t.Fatal("Decoding produced invalid number of offsets for topic z partition 2.")
}
if response.Blocks["z"][2].Offsets[0] != 5 || response.Blocks["z"][2].Offsets[1] != 6 {
t.Fatal("Decoding produced invalid offsets for topic z partition 2.")
}
}
func TestNormalOffsetResponseV1(t *testing.T) {
response := OffsetResponse{}
testVersionDecodable(t, "normal", &response, normalOffsetResponseV1, 1)
if len(response.Blocks) != 2 {
t.Fatal("Decoding produced", len(response.Blocks), "topics where there were two.")
}
if len(response.Blocks["a"]) != 0 {
t.Fatal("Decoding produced", len(response.Blocks["a"]), "partitions for topic 'a' where there were none.")
}
if len(response.Blocks["z"]) != 1 {
t.Fatal("Decoding produced", len(response.Blocks["z"]), "partitions for topic 'z' where there was one.")
}
if !errors.Is(response.Blocks["z"][2].Err, ErrNoError) {
t.Fatal("Decoding produced invalid error for topic z partition 2.")
}
if response.Blocks["z"][2].Timestamp != 1477920049286 {
t.Fatal("Decoding produced invalid timestamp for topic z partition 2.", response.Blocks["z"][2].Timestamp)
}
if response.Blocks["z"][2].Offset != 6 {
t.Fatal("Decoding produced invalid offsets for topic z partition 2.")
}
}
func TestOffsetResponseV4(t *testing.T) {
response := OffsetResponse{}
testVersionDecodable(t, "v4", &response, offsetResponseV4, 4)
}