-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathclient_integration_test.go
630 lines (537 loc) · 16.1 KB
/
client_integration_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2018 Comcast Cable Communications Management, LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pulsar
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"testing"
"time"
"github.com/Comcast/pulsar-client-go/api"
)
// TestClient_Int_PubSub creates a producer and multiple consumers.
// Messages are created by the producer, and then it is asserted
// that all the consumers receive those messages.
func TestClient_Int_PubSub(t *testing.T) {
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: pulsarAddr(t),
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
go func() {
for err := range asyncErrs {
t.Log(err)
}
}()
// context for remainder of test
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
connected, err := c.Connect(ctx, "")
if err != nil {
t.Fatalf("client.Connect() err = %v", err)
}
t.Logf("CONNECTED: protocol=%d server=%q", connected.GetProtocolVersion(), connected.GetServerVersion())
if err = c.Ping(ctx); err != nil {
t.Fatalf("pinger.ping() err = %v", err)
} else {
t.Logf("PONG received")
}
topic := fmt.Sprintf("persistent://sample/standalone/ns1/test-%s", randString(32))
t.Logf("test topic: %q", topic)
topicResp, err := c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Fatalf("topic lookup response = %q; expected %q", got, expected)
}
t.Log(topicResp.String())
// number of messages to produce (and consume)
N := 128
payloads := make([]string, N)
for i := 0; i < N; i++ {
payloads[i] = fmt.Sprintf("%02d - Hola mundo, este es mensaje", i)
}
// create multiple consumers
consumers := make([]*Consumer, 32)
subName := randString(16)
for i := range consumers {
name := fmt.Sprintf("%s-%d", subName, i)
consumers[i], err = c.NewExclusiveConsumer(ctx, topic, name, false, make(chan Message, N))
if err != nil {
t.Fatal(err)
}
// Notify pulsar server that it can send us N messages
if err = consumers[i].Flow(uint32(N)); err != nil {
t.Fatal(err)
}
}
// create single producer
topicProducer, err := c.NewProducer(ctx, topic, "test")
if err != nil {
t.Fatal(err)
}
t.Run("write", func(t *testing.T) {
// The "write" subtest cannot be run in parallel with
// the readers/consumers, since it could get scheduled after a
// "read" subtest finishes (timesout)
for i := 0; i < N; i++ {
if _, err := topicProducer.Send(ctx, []byte(payloads[i])); err != nil {
t.Fatal(err)
}
}
if err := topicProducer.Close(ctx); err != nil {
t.Fatal(err)
}
select {
case <-topicProducer.Closed():
default:
t.Fatal("Closed expected to be unblocked")
}
})
t.Run("read-group", func(t *testing.T) {
for i, cs := range consumers {
cs := cs
t.Run(fmt.Sprintf("read-%02d", i+1), func(t *testing.T) {
t.Parallel()
var gotMsgs []string
for i := 0; i < N; i++ {
select {
case <-cs.Closed():
t.Fatal("consumer was unexpectedly closed")
case <-cs.ReachedEndOfTopic():
t.Fatal("consumer unexpectedly reached end of topic")
case msg := <-cs.Messages():
if err := cs.Ack(msg); err != nil {
t.Fatal(err)
}
gotMsgs = append(gotMsgs, string(msg.Payload))
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
// ensure received messages match expected payloads
if got, expected := len(gotMsgs), N; got != expected {
t.Fatalf("received %d messages; expected %d", got, expected)
}
MATCH:
for _, expected := range payloads {
for _, got := range gotMsgs {
if got == expected {
continue MATCH
}
}
t.Fatalf("msg %q expected, but not found in received messages", expected)
}
if err := cs.Close(ctx); err != nil {
t.Fatal(err)
}
})
}
})
}
// TestClient_Int_ServerInitiatedTopicClose creates a producer and
// consumer. It then performs a topic "unload" request via the REST
// API, which will trigger a server-initiated close from Pulsar to
// the producer and consumer.
func TestClient_Int_ServerInitiatedTopicClose(t *testing.T) {
addr := pulsarAddr(t)
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: addr,
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
go func() {
for err := range asyncErrs {
t.Log(err)
}
}()
// context for remainder of test
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
connected, err := c.Connect(ctx, "")
if err != nil {
t.Fatalf("client.Connect() err = %v", err)
}
t.Logf("CONNECTED: protocol=%d server=%q", connected.GetProtocolVersion(), connected.GetServerVersion())
if err = c.Ping(ctx); err != nil {
t.Fatalf("pinger.ping() err = %v", err)
} else {
t.Logf("PONG received")
}
topicName := fmt.Sprintf("test-%s", randString(32))
topic := fmt.Sprintf("persistent://sample/standalone/ns1/%s", topicName)
t.Logf("topic: %q", topic)
topicResp, err := c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Fatalf("topic lookup response = %q; expected %q", got, expected)
}
t.Log(topicResp.String())
subscriptionName := randString(32)
topicConsumer, err := c.NewExclusiveConsumer(ctx, topic, subscriptionName, false, make(chan Message, 1))
if err != nil {
t.Fatal(err)
}
producerName := randString(32)
topicProducer, err := c.NewProducer(ctx, topic, producerName)
if err != nil {
t.Fatal(err)
}
// unload performs an "unload" request to the
// pulsar server via its HTTP REST interface
unload := func() error {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return err
}
restAddr := net.JoinHostPort(host, "8080")
url := fmt.Sprintf("http://%s/admin/namespaces/sample/standalone/ns1/unload", restAddr)
t.Logf("Unload URL = %q", url)
unloadReq, _ := http.NewRequest(http.MethodPut, url, nil)
unloadReq.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(unloadReq)
if err != nil {
return err
}
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("REST response: %q\n%s", resp.Status, body)
}
t.Logf("Unload response: %v\n%s", resp, body)
return nil
}
unloadErr := make(chan error, 1)
go func() {
if err := unload(); err != nil {
unloadErr <- err
}
}()
select {
case <-topicConsumer.Closed():
t.Logf("consumer was successfully closed")
case err := <-unloadErr:
t.Fatalf("error sending unload REST API command: %+v", err)
case <-ctx.Done():
t.Fatal("consumer was not closed")
}
select {
case <-topicProducer.Closed():
t.Logf("producer was successfully closed")
case err := <-unloadErr:
t.Fatalf("error sending unload REST API command: %+v", err)
case <-ctx.Done():
t.Fatal("producer was not closed")
}
}
// TestClient_Int_Unsubscribe creates a consumer and
// then unsubscribes it.
func TestClient_Int_Unsubscribe(t *testing.T) {
asyncErrs := make(chan error, 8)
go func() {
for err := range asyncErrs {
t.Log(err)
}
}()
N := 4
t.Run("group", func(t *testing.T) {
for i := 0; i < N; i++ {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
t.Parallel()
c, err := NewClient(ClientConfig{
Addr: pulsarAddr(t),
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
// context for remainder of test
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
connected, err := c.Connect(ctx, "")
if err != nil {
t.Fatalf("client.Connect() err = %v", err)
}
t.Logf("CONNECTED: protocol=%d server=%q", connected.GetProtocolVersion(), connected.GetServerVersion())
if err = c.Ping(ctx); err != nil {
t.Fatalf("pinger.ping() err = %v", err)
} else {
t.Logf("PONG received")
}
topic := fmt.Sprintf("persistent://sample/standalone/ns1/test-%s", randString(32))
t.Logf("test topic: %q", topic)
topicResp, err := c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
// It's been observed that topic lookups will sometimes initially fail.
// Here we check for those failures and retry with a small timeout to give
// Pulsar a chance to catch-up.
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Logf("RETRYING topic %q", topic)
time.Sleep(time.Millisecond * 250)
topicResp, err = c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Fatalf("topic lookup response = %q; expected %q", got, expected)
}
}
t.Log(topicResp.String())
topicConsumer, err := c.NewExclusiveConsumer(ctx, topic, randString(32), false, make(chan Message, 1))
if err != nil {
t.Fatal(err)
}
if err := topicConsumer.Unsubscribe(ctx); err != nil {
t.Fatalf("Consumer.Unsubscribe() err = %+v; nil expected", err)
}
})
}
})
}
// TestClient_Int_RedeliverOverflow creates a producer and consumer with message
// buffer size of 1.
// Messages are created by the producer, and then it is asserted
// that discarded overflow messages are redelivered after calling
// RedeliverOverflow on the consumer.
func TestClient_Int_RedeliverOverflow(t *testing.T) {
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: pulsarAddr(t),
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
go func() {
for err := range asyncErrs {
t.Log(err)
}
}()
// context for remainder of test
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
connected, err := c.Connect(ctx, "")
if err != nil {
t.Fatalf("client.Connect() err = %v", err)
}
t.Logf("CONNECTED: protocol=%d server=%q", connected.GetProtocolVersion(), connected.GetServerVersion())
if err = c.Ping(ctx); err != nil {
t.Fatalf("pinger.ping() err = %v", err)
} else {
t.Logf("PONG received")
}
topic := fmt.Sprintf("persistent://sample/standalone/ns1/test-%s", randString(32))
t.Logf("test topic: %q", topic)
topicResp, err := c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Fatalf("topic lookup response = %q; expected %q", got, expected)
}
t.Log(topicResp.String())
// number of messages to produce (and consume)
N := 8
payloads := make([]string, N)
for i := 0; i < N; i++ {
payloads[i] = fmt.Sprintf("%02d - Hola mundo, un mensaje asi", i)
}
// create single producer
topicProducer, err := c.NewProducer(ctx, topic, "test")
if err != nil {
t.Fatal(err)
}
// create single consumer with buffer size 1
cs, err := c.NewSharedConsumer(ctx, topic, randString(16), make(chan Message, 1))
if err != nil {
t.Fatal(err)
}
// Produce messages
for i := 0; i < N; i++ {
if _, err := topicProducer.Send(ctx, []byte(payloads[i])); err != nil {
t.Fatal(err)
}
}
if err := topicProducer.Close(ctx); err != nil {
t.Fatal(err)
}
// Consume messages
var gotMsgs []string
for len(gotMsgs) < N {
// Notify pulsar server that it can send us N messages
if err := cs.Flow(uint32(N)); err != nil {
t.Fatal(err)
}
select {
case <-cs.Closed():
t.Fatal("consumer was unexpectedly closed")
case <-cs.ReachedEndOfTopic():
t.Fatal("consumer unexpectedly reached end of topic")
case msg := <-cs.Messages():
if err := cs.Ack(msg); err != nil {
t.Fatal(err)
}
gotMsgs = append(gotMsgs, string(msg.Payload[:]))
t.Logf("got message: %q", string(msg.Payload))
// allow messages to overflow
time.Sleep(time.Millisecond * 250)
if _, err := cs.RedeliverOverflow(ctx); err != nil {
t.Fatal(err)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
// ensure received messages match expected payloads
if got, expected := len(gotMsgs), N; got != expected {
t.Fatalf("received %d messages; expected %d", got, expected)
}
MATCH:
for _, expected := range payloads {
for _, got := range gotMsgs {
if got == expected {
continue MATCH
}
}
t.Fatalf("msg %q expected, but not found in received messages:\n%v", expected, gotMsgs)
}
if err := cs.Close(ctx); err != nil {
t.Fatal(err)
}
}
// TestClient_Int_RedeliverAll creates a producer and consumer with message
// buffer size of 1.
// Messages are created by the producer, and then it is asserted
// that discarded overflow messages are redelivered after calling
// RedeliverOverflow on the consumer.
func TestClient_Int_RedeliverAll(t *testing.T) {
asyncErrs := make(chan error, 8)
c, err := NewClient(ClientConfig{
Addr: pulsarAddr(t),
Errs: asyncErrs,
})
if err != nil {
t.Fatal(err)
}
go func() {
for err := range asyncErrs {
t.Log(err)
}
}()
// context for remainder of test
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
connected, err := c.Connect(ctx, "")
if err != nil {
t.Fatalf("client.Connect() err = %v", err)
}
t.Logf("CONNECTED: protocol=%d server=%q", connected.GetProtocolVersion(), connected.GetServerVersion())
if err = c.Ping(ctx); err != nil {
t.Fatalf("pinger.ping() err = %v", err)
} else {
t.Logf("PONG received")
}
topic := fmt.Sprintf("persistent://sample/standalone/ns1/test-%s", randString(32))
t.Logf("test topic: %q", topic)
topicResp, err := c.LookupTopic(ctx, topic, false)
if err != nil {
t.Fatal(err)
}
if got, expected := topicResp.GetResponse(), api.CommandLookupTopicResponse_Connect; got != expected {
t.Fatalf("topic lookup response = %q; expected %q", got, expected)
}
t.Log(topicResp.String())
// number of messages to produce (and consume)
N := 8
payloads := make([]string, N)
for i := 0; i < N; i++ {
payloads[i] = fmt.Sprintf("%02d - Hola mundo, esto es un mensaje", i)
}
// create single producer
topicProducer, err := c.NewProducer(ctx, topic, "test")
if err != nil {
t.Fatal(err)
}
// create single consumer with buffer size N
cs, err := c.NewExclusiveConsumer(ctx, topic, randString(16), false, make(chan Message, N))
if err != nil {
t.Fatal(err)
}
// Produce messages
for i := 0; i < N; i++ {
if _, err := topicProducer.Send(ctx, []byte(payloads[i])); err != nil {
t.Fatal(err)
}
}
if err := topicProducer.Close(ctx); err != nil {
t.Fatal(err)
}
// Consume messages
// number of times to request unacknowledged messages
M := 3
var gotMsgs []string
for i := 0; i < M; i++ {
if err := cs.RedeliverUnacknowledged(ctx); err != nil {
t.Fatalf("RedeliverUnacknowledged() err = %v; nil expected", err)
}
// Notify pulsar server that it can send us N messages
if err := cs.Flow(uint32(N)); err != nil {
t.Fatal(err)
}
for j := 0; j < N; j++ {
select {
case <-cs.Closed():
t.Fatal("consumer was unexpectedly closed")
case <-cs.ReachedEndOfTopic():
t.Fatal("consumer unexpectedly reached end of topic")
case msg := <-cs.Messages():
// Message is _not_ acked
gotMsgs = append(gotMsgs, string(msg.Payload[:]))
t.Logf("got message: %q", string(msg.Payload))
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
}
// Ensure received messages match expected payloads
if got, expected := len(gotMsgs), N*M; got != expected {
t.Fatalf("received %d messages; expected %d", got, expected)
}
MATCH:
for _, expected := range payloads {
var found int
for _, got := range gotMsgs {
if got == expected {
if found++; found == M {
continue MATCH
}
}
}
t.Fatalf("msg %q expected, but not found in received messages:\n%v", expected, gotMsgs)
}
if err := cs.Close(ctx); err != nil {
t.Fatal(err)
}
}