-
Notifications
You must be signed in to change notification settings - Fork 2
/
consumer_multitopic_test.go
151 lines (118 loc) · 3.73 KB
/
consumer_multitopic_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
//go:build integration
package pulsar
import (
"context"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConsumerTopicPattern(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
topic := randomTopicName()
producer1, _ := newTestProducer(t, client, topic+"-1")
msg1 := sendMessage(t, producer1, "hello world 1")
producer2, _ := newTestProducer(t, client, topic+"-2")
msg2 := sendMessage(t, producer2, "hello world 2")
consConf := ConsumerConfig{
TopicPattern: topic + "-.*",
InitialPosition: EarliestPosition,
}
ctx := context.Background()
consumer, err := client.NewConsumer(ctx, consConf)
require.NoError(t, err)
m1, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m1)
m2, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m2)
assert.NoError(t, consumer.AckMessage(m1))
assert.NoError(t, consumer.AckMessage(m2))
messages := []string{string(m1.Body), string(m2.Body)}
m := map[string]*Message{
string(m1.Body): m1,
string(m2.Body): m2,
}
sort.Strings(messages)
assert.Equal(t, []string{string(msg1.Body), string(msg2.Body)}, messages)
t1, err := NewTopic(m[messages[0]].Topic)
require.NoError(t, err)
assert.Equal(t, topic+"-1", t1.LocalName)
t2, err := NewTopic(m[messages[1]].Topic)
require.NoError(t, err)
assert.NotNil(t, t2)
assert.Equal(t, topic+"-2", t2.LocalName)
}
func TestConsumerTopicPatternDiscovery(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
topic := randomTopicName()
consConf := ConsumerConfig{
TopicPattern: topic + "-.*",
TopicPatternDiscoveryInterval: 500,
InitialPosition: EarliestPosition,
}
ctx := context.Background()
consumer, err := client.NewConsumer(ctx, consConf)
require.NoError(t, err)
time.Sleep(time.Second)
producer, _ := newTestProducer(t, client, topic+"-1")
msg := sendMessage(t, producer, "hello world")
m, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m)
require.NoError(t, consumer.AckMessage(m))
assert.Equal(t, msg.Body, m.Body)
}
func TestConsumerTopicPatternInitialPosition(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
topic := randomTopicName()
cb := func(topic string) (position InitialPosition, StartMessageID []byte, err error) {
if strings.HasSuffix(topic, "1") {
return EarliestPosition, nil, nil
}
return LatestPosition, nil, nil
}
consConf := ConsumerConfig{
TopicPattern: topic + "-.*",
TopicPatternDiscoveryInterval: 500,
InitialPositionCallback: cb,
StartMessageIDInclusive: true,
}
ctx := context.Background()
consumer, err := client.NewConsumer(ctx, consConf)
require.NoError(t, err)
time.Sleep(time.Second)
producer1, _ := newTestProducer(t, client, topic+"-1")
sendMessage(t, producer1, "hello world 1a")
sendMessage(t, producer1, "hello world 1b")
producer2, _ := newTestProducer(t, client, topic+"-2")
sendMessage(t, producer2, "hello world 2a")
sendMessage(t, producer2, "hello world 2b")
m1, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m1)
require.NoError(t, consumer.AckMessage(m1))
m2, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m2)
require.NoError(t, consumer.AckMessage(m2))
m3, err := consumer.ReadMessage(ctx)
require.NoError(t, err)
require.NotNil(t, m3)
require.NoError(t, consumer.AckMessage(m3))
messages := []string{string(m1.Body), string(m2.Body), string(m3.Body)}
sort.Strings(messages)
assert.Equal(t, []string{"hello world 1a", "hello world 1b", "hello world 2b"}, messages)
}