forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_test.go
162 lines (146 loc) · 3.48 KB
/
write_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package kafka
import (
"bufio"
"bytes"
"testing"
"time"
)
const (
testCorrelationID = 1
testClientID = "localhost"
testTopic = "topic"
testPartition = 42
)
func TestWriteOptimizations(t *testing.T) {
t.Parallel()
t.Run("writeFetchRequestV1", testWriteFetchRequestV1)
t.Run("writeListOffsetRequestV1", testWriteListOffsetRequestV1)
t.Run("writeProduceRequestV2", testWriteProduceRequestV2)
}
func testWriteFetchRequestV1(t *testing.T) {
const offset = 42
const minBytes = 10
const maxBytes = 1000
const maxWait = 100 * time.Millisecond
testWriteOptimization(t,
requestHeader{
ApiKey: int16(fetchRequest),
ApiVersion: int16(v1),
CorrelationID: testCorrelationID,
ClientID: testClientID,
},
fetchRequestV1{
ReplicaID: -1,
MaxWaitTime: milliseconds(maxWait),
MinBytes: minBytes,
Topics: []fetchRequestTopicV1{{
TopicName: testTopic,
Partitions: []fetchRequestPartitionV1{{
Partition: testPartition,
FetchOffset: offset,
MaxBytes: maxBytes,
}},
}},
},
func(w *bufio.Writer) {
writeFetchRequestV1(w, testCorrelationID, testClientID, testTopic, testPartition, offset, minBytes, maxBytes, maxWait)
},
)
}
func testWriteListOffsetRequestV1(t *testing.T) {
const time = -1
testWriteOptimization(t,
requestHeader{
ApiKey: int16(listOffsetRequest),
ApiVersion: int16(v1),
CorrelationID: testCorrelationID,
ClientID: testClientID,
},
listOffsetRequestV1{
ReplicaID: -1,
Topics: []listOffsetRequestTopicV1{{
TopicName: testTopic,
Partitions: []listOffsetRequestPartitionV1{{
Partition: testPartition,
Time: time,
}},
}},
},
func(w *bufio.Writer) {
writeListOffsetRequestV1(w, testCorrelationID, testClientID, testTopic, testPartition, time)
},
)
}
func testWriteProduceRequestV2(t *testing.T) {
key := []byte(nil)
val := []byte("Hello World!")
msg := messageSetItem{
Offset: 10,
Message: message{
MagicByte: 1,
Attributes: 0,
Key: key,
Value: val,
},
}
msg.MessageSize = msg.Message.size()
msg.Message.CRC = msg.Message.crc32()
const timeout = 100
testWriteOptimization(t,
requestHeader{
ApiKey: int16(produceRequest),
ApiVersion: int16(v2),
CorrelationID: testCorrelationID,
ClientID: testClientID,
},
produceRequestV2{
RequiredAcks: -1,
Timeout: timeout,
Topics: []produceRequestTopicV2{{
TopicName: testTopic,
Partitions: []produceRequestPartitionV2{{
Partition: testPartition,
MessageSetSize: msg.size(),
MessageSet: messageSet{msg},
}},
}},
},
func(w *bufio.Writer) {
writeProduceRequestV2(w, testCorrelationID, testClientID, testTopic, testPartition, timeout*time.Millisecond, Message{
Offset: 10,
Key: key,
Value: val,
})
},
)
}
func testWriteOptimization(t *testing.T, h requestHeader, r request, f func(*bufio.Writer)) {
b1 := &bytes.Buffer{}
w1 := bufio.NewWriter(b1)
b2 := &bytes.Buffer{}
w2 := bufio.NewWriter(b2)
h.Size = (h.size() + r.size()) - 4
h.writeTo(w1)
r.writeTo(w1)
w1.Flush()
f(w2)
w2.Flush()
c1 := b1.Bytes()
c2 := b2.Bytes()
if !bytes.Equal(c1, c2) {
t.Error("content differs")
n1 := len(c1)
n2 := len(c2)
if n1 != n2 {
t.Log("content length 1 =", n1)
t.Log("content length 2 =", n2)
} else {
for i := 0; i != n1; i++ {
if c1[i] != c2[i] {
t.Logf("byte at offset %d/%d: %#x != %#x", i, n1, c1[i], c2[i])
break
}
}
}
}
}