forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_test.go
115 lines (101 loc) · 2.45 KB
/
protocol_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
package kafka
import (
"bufio"
"bytes"
"fmt"
"reflect"
"testing"
)
func TestProtocol(t *testing.T) {
t.Parallel()
tests := []interface{}{
int8(42),
int16(42),
int32(42),
int64(42),
"",
"Hello World!",
[]byte(nil),
[]byte("Hello World!"),
requestHeader{
Size: 26,
ApiKey: int16(offsetCommitRequest),
ApiVersion: int16(v2),
CorrelationID: 42,
ClientID: "Hello World!",
},
message{
MagicByte: 1,
Timestamp: 42,
Key: nil,
Value: []byte("Hello World!"),
},
topicMetadataRequestV1{"A", "B", "C"},
metadataResponseV1{
Brokers: []brokerMetadataV1{
{NodeID: 1, Host: "localhost", Port: 9001},
{NodeID: 2, Host: "localhost", Port: 9002, Rack: "rack2"},
},
ControllerID: 2,
Topics: []topicMetadataV1{
{TopicErrorCode: 0, Internal: true, Partitions: []partitionMetadataV1{{
PartitionErrorCode: 0,
PartitionID: 1,
Leader: 2,
Replicas: []int32{1},
Isr: []int32{1},
}}},
},
},
listOffsetRequestV1{
ReplicaID: 1,
Topics: []listOffsetRequestTopicV1{
{TopicName: "A", Partitions: []listOffsetRequestPartitionV1{
{Partition: 0, Time: -1},
{Partition: 1, Time: -1},
{Partition: 2, Time: -1},
}},
{TopicName: "B", Partitions: []listOffsetRequestPartitionV1{
{Partition: 0, Time: -2},
}},
{TopicName: "C", Partitions: []listOffsetRequestPartitionV1{
{Partition: 0, Time: 42},
}},
},
},
listOffsetResponseV1{
{TopicName: "A", PartitionOffsets: []partitionOffsetV1{
{Partition: 0, Timestamp: 42, Offset: 1},
}},
{TopicName: "B", PartitionOffsets: []partitionOffsetV1{
{Partition: 0, Timestamp: 43, Offset: 10},
{Partition: 1, Timestamp: 44, Offset: 100},
}},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%T", test), func(t *testing.T) {
b := &bytes.Buffer{}
r := bufio.NewReader(b)
w := &writeBuffer{w: b}
w.write(test)
if size := int(sizeof(test)); size != b.Len() {
t.Error("invalid size:", size, "!=", b.Len())
}
v := reflect.New(reflect.TypeOf(test))
n := b.Len()
n, err := read(r, n, v.Interface())
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("%d unread bytes", n)
}
if !reflect.DeepEqual(test, v.Elem().Interface()) {
t.Error("values don't match:")
t.Logf("expected: %#v", test)
t.Logf("found: %#v", v.Elem().Interface())
}
})
}
}