-
Notifications
You must be signed in to change notification settings - Fork 38
/
consumer_test.go
366 lines (309 loc) · 9.87 KB
/
consumer_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
// 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 (
"bytes"
"context"
"fmt"
"testing"
"time"
"github.com/Comcast/pulsar-client-go/api"
"github.com/Comcast/pulsar-client-go/frame"
"github.com/golang/protobuf/proto"
)
func TestConsumer_Flow(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, 1))
if err := c.Flow(123); err != nil {
t.Fatalf("Flow() err = %v; nil expected", err)
}
if got, expected := len(ms.frames), 1; got != expected {
t.Fatalf("got %d frame; expected %d", got, expected)
}
}
func TestConsumer_Close_Success(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, 1))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
resp := make(chan error, 1)
go func() { resp <- c.Close(ctx) }()
// Allow goroutine time to complete
time.Sleep(100 * time.Millisecond)
select {
case <-c.Closed():
t.Fatalf("Closed() unblocked; expected to be blocked before receiving Close() response")
default:
t.Logf("Closed() blocked")
}
expected := api.CommandSuccess{
RequestId: proto.Uint64(id),
}
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_SUCCESS.Enum(),
Success: &expected,
},
}
if err := dispatcher.notifyReqID(id, f); err != nil {
t.Fatal(err)
}
got := <-resp
if got != nil {
t.Fatalf("Close() err = %v; nil expected", got)
}
if got, expected := len(ms.frames), 1; got != expected {
t.Fatalf("got %d frame; expected %d", got, expected)
}
select {
case <-c.Closed():
t.Logf("Closed() unblocked")
default:
t.Fatalf("Closed() blocked; expected to be unblocked after Close()")
}
}
func TestConsumer_handleMessage(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, 1))
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_MESSAGE.Enum(),
Message: &api.CommandMessage{
ConsumerId: proto.Uint64(consID),
},
},
Metadata: &api.MessageMetadata{
ProducerName: proto.String("hi"),
SequenceId: proto.Uint64(9933),
},
Payload: []byte("hola mundo"),
}
resp := make(chan error, 1)
go func() {
resp <- c.handleMessage(f)
}()
var got Message
select {
case got = <-c.Messages():
case <-time.After(time.Millisecond * 250):
t.Fatal("timeout waiting for message")
}
if !proto.Equal(got.Msg, f.BaseCmd.Message) {
t.Fatalf("got message:\n%+v\nexpected:\n%+v", got.Msg, f.BaseCmd.Message)
}
if !proto.Equal(got.Meta, f.Metadata) {
t.Fatalf("got meta:\n%+v\nexpected:\n%+v", got.Meta, f.Metadata)
}
if !bytes.Equal(got.Payload, f.Payload) {
t.Fatalf("got payload:\n%q\nexpected:\n%q", got.Payload, f.Payload)
}
t.Logf("got message:\n%+v", got)
}
func TestConsumer_handleMessage_fullQueue(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
queueSize := 3
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, queueSize))
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_MESSAGE.Enum(),
Message: &api.CommandMessage{
ConsumerId: proto.Uint64(consID),
},
},
Metadata: &api.MessageMetadata{
ProducerName: proto.String("hi"),
SequenceId: proto.Uint64(9933),
},
Payload: []byte("hola mundo"),
}
for i := 0; i < queueSize; i++ {
if err := c.handleMessage(f); err != nil {
t.Fatalf("handleMessage() err = %v; expected nil for msg number %d and queueSize %d", err, i+1, queueSize)
}
if got, expected := len(c.overflow), 0; got != expected {
t.Fatalf("len(consumer overflow buffer) = %d; expected %d", got, expected)
}
}
// message queue should now be full,
// therefore handleMessage should return an error
err := c.handleMessage(f)
if err == nil {
t.Fatalf("handleMessage() err = %v expected non-nil for msg number %d and queueSize %d", err, queueSize+1, queueSize)
}
t.Logf("handleMessage() err (expected) = %q for msg number %d and queueSize %d", err, queueSize+1, queueSize)
if got, expected := len(c.overflow), 1; got != expected {
t.Fatalf("len(consumer overflow buffer) = %d; expected %d", got, expected)
}
for i := 0; i < queueSize; i++ {
select {
case got := <-c.Messages():
if !proto.Equal(got.Msg, f.BaseCmd.Message) {
t.Fatalf("got message:\n%+v\nexpected:\n%+v", got.Msg, f.BaseCmd.Message)
}
if !proto.Equal(got.Meta, f.Metadata) {
t.Fatalf("got meta:\n%+v\nexpected:\n%+v", got.Meta, f.Metadata)
}
if !bytes.Equal(got.Payload, f.Payload) {
t.Fatalf("got payload:\n%q\nexpected:\n%q", got.Payload, f.Payload)
}
t.Logf("got message:\n%+v", got)
case <-time.After(time.Millisecond * 250):
t.Fatal("timeout waiting for message")
}
}
}
func TestConsumer_handleCloseConsumer(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, 1))
select {
case <-c.Closed():
t.Fatalf("Closed() unblocked; expected to be blocked before receiving handleCloseConsumer()")
default:
t.Logf("Closed() blocked")
}
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_CLOSE_CONSUMER.Enum(),
CloseConsumer: &api.CommandCloseConsumer{
RequestId: proto.Uint64(id),
ConsumerId: proto.Uint64(consID),
},
},
}
if err := c.handleCloseConsumer(f); err != nil {
t.Fatalf("handleCloseConsumer() err = %v; nil expected", err)
}
select {
case <-c.Closed():
t.Logf("Closed() unblocked")
default:
t.Fatalf("Closed() blocked; expected to be unblocked after handleCloseConsumer()")
}
}
func TestConsumer_handleReachedEndOfTopic(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, 1))
select {
case <-c.ReachedEndOfTopic():
t.Fatalf("ReachedEndOfTopic() unblocked; expected to be blocked before receiving handleReachedEndOfTopic()")
default:
t.Logf("ReachedEndOfTopic() blocked")
}
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_REACHED_END_OF_TOPIC.Enum(),
ReachedEndOfTopic: &api.CommandReachedEndOfTopic{
ConsumerId: proto.Uint64(consID),
},
},
}
if err := c.handleReachedEndOfTopic(f); err != nil {
t.Fatalf("handleReachedEndOfTopic() err = %v; nil expected", err)
}
select {
case <-c.ReachedEndOfTopic():
t.Logf("ReachedEndOfTopic() unblocked")
default:
t.Fatalf("ReachedEndOfTopic() blocked; expected to be unblocked after handleReachedEndOfTopic()")
}
}
func TestConsumer_RedeliverOverflow(t *testing.T) {
var ms mockSender
id := uint64(43)
consID := uint64(123)
reqID := monotonicID{id}
dispatcher := newFrameDispatcher()
queueSize := 1
N := 8 // number of messages to push to consumer
c := newConsumer(&ms, dispatcher, "test", &reqID, consID, make(chan Message, queueSize))
for i := 0; i < N; i++ {
entryID := uint64(i)
// the MessageIdData must be unique for each message,
// otherwise the consumer will consider them duplicates
// and not store them in overflow
f := frame.Frame{
BaseCmd: &api.BaseCommand{
Type: api.BaseCommand_MESSAGE.Enum(),
Message: &api.CommandMessage{
ConsumerId: proto.Uint64(consID),
MessageId: &api.MessageIdData{
EntryId: &entryID,
},
},
},
Metadata: &api.MessageMetadata{
ProducerName: proto.String("hi"),
SequenceId: proto.Uint64(9933),
},
Payload: []byte(fmt.Sprintf("%d: Hola", i)),
}
err := c.handleMessage(f)
if i < queueSize {
if err != nil {
t.Fatalf("handleMessage() err = %v; expected nil for msg number %d and queueSize %d", err, i, queueSize)
}
} else {
if err == nil {
t.Fatalf("handleMessage() err = %v; expected non-nil for msg number %d and queueSize %d", err, i, queueSize)
}
}
}
if got, err := c.RedeliverOverflow(context.Background()); err != nil {
t.Fatalf("RedeliverOverflow() err = %v; expected nil", err)
} else if expected := N - queueSize; got != expected {
t.Fatalf("RedeliverOverflow() = %d; expected %d", got, expected)
}
// Ensure correct frame was sent by consumer
sentFrames := ms.getFrames()
if got, expected := len(sentFrames), 1; got != expected {
t.Fatalf("%d frames were sent; expected %d", got, expected)
}
f := sentFrames[0]
if got, expected := f.BaseCmd.GetType(), api.BaseCommand_REDELIVER_UNACKNOWLEDGED_MESSAGES; got != expected {
t.Fatalf("got frame of type %q; expected %q", got, expected)
}
// Ensure messageIDs are correct
messageIDs := f.BaseCmd.GetRedeliverUnacknowledgedMessages().GetMessageIds()
if got, expected := len(messageIDs), N-queueSize; got != expected {
t.Fatalf("REDELIVER_UNACKNOWLEDGED_MESSAGES message contained %d messageIDs; expected %d", got, expected)
}
for i, mid := range messageIDs {
if got, expected := mid.GetEntryId(), uint64(i+1); got != expected {
t.Fatalf("messageID %d: EntryID = %d; expected %d", i, got, expected)
}
}
}