-
Notifications
You must be signed in to change notification settings - Fork 2
/
producer_test.go
187 lines (152 loc) · 4.09 KB
/
producer_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
//go:build integration
package pulsar
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
func newTestProducer(t *testing.T, client *Client, topic string) (*Producer, string) {
t.Helper()
if topic == "" {
topic = randomTopicName()
}
prodConf := ProducerConfig{
Topic: topic,
Name: "test-producer",
BatchSize: 1,
}
ctx := context.Background()
var err error
producer, err := client.NewProducer(ctx, prodConf)
require.NoError(t, err)
return producer, topic
}
func sendMessage(t *testing.T, producer *Producer, s string) *Message {
t.Helper()
m := &Message{
Body: []byte(s),
}
var err error
ctx := context.Background()
id, err := producer.WriteMessage(ctx, m.Body)
if id.BatchIndex == nil {
id.BatchIndex = proto.Int32(0)
}
require.NoError(t, err)
require.NotNil(t, id)
m.ID = id
return m
}
func sendMessageAsync(t *testing.T, producer *Producer, s string) *Message {
t.Helper()
m := &Message{
Body: []byte(s),
}
ctx := context.Background()
require.NoError(t, producer.WriteMessageAsync(ctx, m.Body))
return m
}
func TestProducerConfigValidate(t *testing.T) {
conf := &ProducerConfig{
Topic: "test-topic-pulsar-go",
}
err := conf.Validate()
assert.NoError(t, err)
}
func TestProducerRestartSequence(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
prod, topic := newTestProducer(t, client, "")
m1 := sendMessage(t, prod, "hello world 1")
assert.EqualValues(t, 0, *m1.ID.EntryId)
m2 := sendMessage(t, prod, "hello world 2")
assert.EqualValues(t, 1, *m2.ID.EntryId)
// restart producer
err := prod.Close()
require.NoError(t, err)
prod, _ = newTestProducer(t, client, topic)
assert.EqualValues(t, 0, prod.sequenceID)
m3 := sendMessage(t, prod, "hello world 3")
assert.EqualValues(t, 2, *m3.ID.EntryId)
assert.Equal(t, *m1.ID.LedgerId, *m3.ID.LedgerId)
}
func TestProducerBrokerGeneratedName(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
prodConf := ProducerConfig{
Topic: randomTopicName(),
}
ctx := context.Background()
prod, err := client.NewProducer(ctx, prodConf)
require.NoError(t, err)
assert.NotEmpty(t, prod.name)
}
func TestProducerBatchSize(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
topic := randomTopicName()
prodConf := ProducerConfig{
Topic: topic,
Name: "test-producer",
BatchSize: 2,
BatchTimeout: time.Minute,
}
ctx := context.Background()
prod, err := client.NewProducer(ctx, prodConf)
require.NoError(t, err)
consConf := ConsumerConfig{
Topic: topic,
InitialPosition: EarliestPosition,
}
consumer, err := client.NewConsumer(ctx, consConf)
require.NoError(t, err)
msg1 := sendMessageAsync(t, prod, "hello world 1")
msg2 := sendMessageAsync(t, prod, "hello world 2")
// wait for message to be available
timeout := time.After(1 * time.Second)
<-timeout
assert.True(t, consumer.HasNext())
readMessageAndCompare(t, consumer, msg1)
readMessageAndCompare(t, consumer, msg2)
}
func TestProducerBatchTimeout(t *testing.T) {
client := setup(t)
defer func() {
assert.NoError(t, client.Close())
}()
topic := randomTopicName()
prodConf := ProducerConfig{
Topic: topic,
Name: "test-producer",
BatchSize: 100,
BatchTimeout: 500 * time.Millisecond,
}
ctx := context.Background()
consConf := ConsumerConfig{
Topic: topic,
InitialPosition: EarliestPosition,
ForceTopicCreation: true,
}
// start consumer before producer to make for better test timing
consumer, err := client.NewConsumer(ctx, consConf)
require.NoError(t, err)
prod, err := client.NewProducer(ctx, prodConf)
require.NoError(t, err)
msg1 := sendMessageAsync(t, prod, "hello world 1")
msg2 := sendMessageAsync(t, prod, "hello world 2")
// wait for message to be available
timeout := time.After(1100 * time.Millisecond)
<-timeout
assert.True(t, consumer.HasNext())
readMessageAndCompare(t, consumer, msg1)
readMessageAndCompare(t, consumer, msg2)
}