-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubsub_test.go
234 lines (210 loc) · 6 KB
/
pubsub_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package kafclient
import (
"log"
"strings"
"time"
"testing"
)
type User struct {
Name string
Age int
}
/*
Test with consumer group:
* case 1: 2 consumer group
publisher: go test -run TestCGPush2Message
consumer: CG=cg1 go test -run TestCGHandleMessage | CG=cg2 go test -run TestCGHandleMessage
* case 2: 1 consumer group
publisher: go test -run TestCGPush2Message
consumer: CG=cg1 go test -run TestCGHandleMessage | CG=cg1 go test -run TestCGHandleMessage
Test with consumer and publisher
//**************** Test send message to topic using sync **********************/
func TestNormalCaseSync(t *testing.T) {
ps := &Client{}
brokers := strings.Split("localhost:9092", ",")
ps.InitPublisher(brokers...)
log.Print("init done publiser")
for i := 1; i <= 30; i++ {
err := ps.Publish("topic-0", &User{
Name: "hoa",
Age: i,
})
log.Print(i, err)
time.Sleep(100 * time.Millisecond)
}
defer ps.Close()
err := ps.InitConsumerGroup("CG-0", brokers...)
if err != nil {
log.Print(err)
return
}
log.Print("init done consumer")
buff := make(chan Message, 20)
go func() {
for {
d := <-buff
user := &User{}
BodyParse(d.Body, user)
log.Print(d.Headers, d.Partition, user, " ", d.Partition)
d.Commit()
}
}()
err = ps.OnAsyncSubscribe([]*Topic{{
"topic-0", false, nil, false,
}}, 1, buff)
log.Print(err)
}
func testPublishMessages(brokerURL, topic string, count int) {
ps := &Client{}
brokers := strings.Split(brokerURL, ",")
ps.InitPublisher(brokers...)
log.Print("init done publiser")
for i := 1; i <= count; i++ {
err := ps.Publish(topic, &User{
Name: "hoa",
Age: i,
})
log.Print(i, err)
}
ps.Close()
}
func TestPuslishMessages(t *testing.T) {
testPublishMessages("b-1.urboxhnstaging.k17vzh.c3.kafka.ap-southeast-1.amazonaws.com:9092,b-2.urboxhnstaging.k17vzh.c3.kafka.ap-southeast-1.amazonaws.com:9092", "topic-0", 100)
}
// ***** Test send message with config ****/
func testPuslishMessagesWithConfig(brokerURL, topic1, topic2 string) {
ps := &Client{}
brokers := strings.Split(brokerURL, ",")
ps.InitPublisher(brokers...)
config := &SenderConfig{
Headers: map[string]string{"token": "abc123"},
}
topic := &Topic{Name: topic1}
for i := 1; i <= 20; i++ {
err := ps.PublishWithConfig(topic, config, &User{
Name: "nguoi dau tien",
Age: i,
})
log.Print(i, err)
time.Sleep(100 * time.Millisecond)
}
// *************** Send message with specific partition ***********
topic = &Topic{Name: topic2, AutoCommit: true, Partition: ToPInt32(1)}
for i := 1; i <= 20; i++ {
err := ps.PublishWithConfig(topic, config, &User{
Name: "nguoi thu 2",
Age: i,
})
log.Print(err)
time.Sleep(100 * time.Millisecond)
}
}
func TestPuslishMessagesWithConfig(t *testing.T) {
testPuslishMessagesWithConfig("0.0.0.0:9092", "topic-1", "topic-2")
}
//**************** Test handle message when listen event auto commit **********************/
func testSubscribeSimpleAutoCommit(brokerURL, group, topic1, topic2 string) {
ps := &Client{}
brokers := strings.Split(brokerURL, ",")
err := ps.InitConsumerGroup(group, brokers...)
if err != nil {
log.Print(err)
return
}
log.Print("init done consumer")
buff := make(chan Message, 5)
go func() {
for {
d := <-buff
user := &User{}
BodyParse(d.Body, user)
log.Print(d.Headers, d.Partition, user)
}
}()
err = ps.OnAsyncSubscribe([]*Topic{
{
Name: topic1, AutoCommit: true,
}, {
Name: topic2, AutoCommit: true, Partition: ToPInt32(1),
},
}, 1, buff)
log.Print(err)
}
func TestSubscribeSimpleAutoCommit(t *testing.T) {
testSubscribeSimpleAutoCommit("0.0.0.0:9092", "CG-0", "topic-1", "topic-2")
}
// ********** Test listen message when listen message and manual commit ****/
func testSubscribeSimpleManualCommit(brokerURL, group, topic string) {
ps := &Client{}
brokers := strings.Split(brokerURL, ",")
err := ps.InitConsumerGroup(group, brokers...)
if err != nil {
log.Print(err)
return
}
log.Print("init done consumer")
buff := make(chan Message, 20)
go func() {
for {
d := <-buff
user := &User{}
BodyParse(d.Body, user)
log.Print(d.Headers, d.Partition, user)
if user.Age != 100 {
d.Commit()
}
}
}()
err = ps.OnAsyncSubscribe([]*Topic{{
topic, false, nil, false,
}}, 1, buff)
log.Print(err)
}
func TestSubscribeSimpleManualCommit(t *testing.T) {
testSubscribeSimpleManualCommit("b-1.urboxhnstaging.k17vzh.c3.kafka.ap-southeast-1.amazonaws.com:9092,b-2.urboxhnstaging.k17vzh.c3.kafka.ap-southeast-1.amazonaws.com:9092", "CG-0", "topic-0")
}
func TestListTopic(t *testing.T) {
ps := &Client{}
brokers := strings.Split("0.0.0.0:9092", ",")
ts, err := ps.ListTopics(brokers...)
log.Print(ts, err)
}
// --------------------- Test full publish and subscribe ---------------------
func TestSimplePublishAndSubscibe(t *testing.T) {
lock := make(chan bool)
go t.Run("subscribe 1", func(t *testing.T) {
testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-0", "topic-3")
})
go t.Run("subscribe 2", func(t *testing.T) {
testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-3")
})
time.Sleep(5 * time.Second)
t.Run("publish to topic", func(t *testing.T) {
testPublishMessages("0.0.0.0:9092", "topic-3", 20)
})
<-lock
}
// ------------------- Benchmark test ----------------
func BenchmarkPublishMessages100z(b *testing.B) {
testPublishMessages("0.0.0.0:9092", "topic-4", 100)
}
func BenchmarkPublishMessages10000z(b *testing.B) {
testPublishMessages("0.0.0.0:9092", "topic-5", 10000)
}
func BenchmarkSubscribeSimpleManualCommit(b *testing.B) {
testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-5")
}
func TestSimplePublishAndSubscibeResub(t *testing.T) {
lock := make(chan bool)
go t.Run("subscribe 1", func(t *testing.T) {
testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-0", "topic-3")
})
go t.Run("subscribe 2", func(t *testing.T) {
testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-3")
})
time.Sleep(5 * time.Second)
t.Run("publish to topic", func(t *testing.T) {
testPublishMessages("0.0.0.0:9092", "topic-3", 20)
})
<-lock
}