-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
167 lines (156 loc) · 4.56 KB
/
client_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
163
164
165
166
167
package kafka
import (
"sort"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/spf13/cast"
)
func TestNewClient(t *testing.T) {
seedBroker, controllerBroker := getTestingBrokers(t)
client, err := NewClient(seedBroker.Addr(), controllerBroker.Addr())
if err != nil {
t.Fatal(err)
}
err = client.Close()
if err != nil {
t.Fatal(err)
}
seedBroker.Close()
}
func TestCustomClient(t *testing.T) {
seedBroker, controllerBroker := getTestingBrokers(t)
conf := GetConf("testID")
conf.Metadata.Retry.Max = 0
client, err := NewCustomClient(conf, seedBroker.Addr(), controllerBroker.Addr())
if err != nil {
t.Fatal(err)
}
err = client.Close()
if err != nil {
t.Fatal(err)
}
seedBroker.Close()
}
func TestLogging(t *testing.T) {
m := `testmessage`
Log("Log Logging Validation:", m)
Logf("Logf Logging Validation: %v\n", m)
Warnf("Warnf Logging Validation: %v\n", m)
}
func TestClientLogging(t *testing.T) {
seedBroker, controllerBroker := getTestingBrokers(t)
client, err := NewClient(seedBroker.Addr(), controllerBroker.Addr())
if err != nil {
t.Fatal(err)
}
m := `testmessage`
client.Log("Log Logging Validation:", m)
client.Logf("Logf Logging Validation: %v\n", m)
client.Warnf("Warnf Logging Validation: %v\n", m)
err = client.Close()
if err != nil {
t.Fatal(err)
}
seedBroker.Close()
}
func TestClusterMetaRequest(t *testing.T) {
clientTimeout := (time.Second * 5)
clientRetries := 1
seedBroker, controllerBroker := getTestingBrokers(t)
defer seedBroker.Close()
defer controllerBroker.Close()
conf := GetConf()
conf.Net.DialTimeout = clientTimeout
conf.Net.ReadTimeout = clientTimeout
conf.Net.WriteTimeout = clientTimeout
conf.Metadata.Retry.Max = clientRetries
conf.Version = MinKafkaVersion
client, err := NewCustomClient(conf, seedBroker.Addr())
if err != nil {
t.Fatal(err)
}
cm, err := client.clusterMetaTest()
if err != nil {
t.Fatal(err)
}
if cm.BrokerCount() != 2 {
t.Error("Client returned incorrect number of available brokers, expected 2, received:", cm.BrokerCount())
}
client.Logf("Found %v Brokers, %v Topics, %v Groups", cm.BrokerCount(), cm.TopicCount(), cm.GroupCount())
err = client.Close()
if err != nil {
t.Fatal(err)
}
}
func (kc *KClient) clusterMetaTest() (ClusterMeta, error) {
cm := ClusterMeta{}
res, err := kc.ReqMetadata()
if err != nil {
return cm, err
}
grps, errs := kc.ListGroups()
if len(errs) > 0 {
cm.ErrorStack = append(cm.ErrorStack, errs...)
}
cm.Controller = res.ControllerID
for _, b := range res.Brokers {
id := b.ID()
addr := b.Addr()
broker := string(addr + "/" + cast.ToString(id))
cm.Brokers = append(cm.Brokers, broker)
cm.BrokerIDs = append(cm.BrokerIDs, id)
}
for _, t := range res.Topics {
cm.Topics = append(cm.Topics, t.Name)
}
cm.Groups = grps
sort.Strings(cm.Groups)
sort.Strings(cm.Brokers)
sort.Strings(cm.Topics)
return cm, nil
}
func getTestingClient(seedBroker *sarama.MockBroker) (*KClient, error) {
clientTimeout := (time.Second * 5)
clientRetries := 1
conf := GetConf()
conf.Net.DialTimeout = clientTimeout
conf.Net.ReadTimeout = clientTimeout
conf.Net.WriteTimeout = clientTimeout
conf.Metadata.Retry.Max = clientRetries
conf.Version = MinKafkaVersion
return NewCustomClient(conf, seedBroker.Addr())
}
func getTestingBrokers(t *testing.T) (seedBroker, controllerBroker *sarama.MockBroker) {
seedBroker = sarama.NewMockBroker(t, 1)
controllerBroker = sarama.NewMockBroker(t, 2)
seedBroker.SetHandlerByMap(map[string]sarama.MockResponse{
"MetadataRequest": sarama.NewMockMetadataResponse(t).
SetController(controllerBroker.BrokerID()).
SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
SetLeader("testTopic", 0, seedBroker.BrokerID()).
SetLeader("testTopic", 1, controllerBroker.BrokerID()),
})
return
}
/*
func getTestingBrokers(t *testing.T) (seedBroker, controllerBroker *sarama.MockBroker) {
seedBroker = sarama.NewMockBroker(t, 1)
controllerBroker = sarama.NewMockBroker(t, 2)
seedBroker.SetHandlerByMap(map[string]sarama.MockResponse{
"MetadataRequest": sarama.NewMockMetadataResponse(t).
SetController(controllerBroker.BrokerID()).
SetBroker(seedBroker.Addr(), seedBroker.BrokerID()).
SetBroker(controllerBroker.Addr(), controllerBroker.BrokerID()).
SetLeader("testTopic", 0, seedBroker.BrokerID()).
SetLeader("testTopic", 1, controllerBroker.BrokerID()),
"DescribeGroupsRequest": sarama.NewMockDescribeGroupsResponse(t).
AddGroupDescription("testGroup", sarama.GroupDescription{
Err: nil,
GroupID: "testGroup",
}),
})
return
}
*/