-
Notifications
You must be signed in to change notification settings - Fork 5
/
consume_test.go
44 lines (41 loc) · 1.06 KB
/
consume_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
package rabbitmq_test
import (
"context"
"testing"
"github.com/gbeletti/rabbitmq"
amqp "github.com/rabbitmq/amqp091-go"
)
func consumeTest(t *testing.T, ctx context.Context, rabbit rabbitmq.Consumer, queue string) (msg string) {
ctxCancel, cancel := context.WithCancel(ctx)
defer cancel()
gotMessage := make(chan string)
var receiveMessage = func(d *amqp.Delivery) {
gotMessage <- string(d.Body)
err := d.Ack(false)
if err != nil {
t.Errorf("error acking message: %s\n", err)
}
}
go func() {
config := rabbitmq.ConfigConsume{
QueueName: queue,
Consumer: "test",
AutoAck: false,
Exclusive: false,
NoLocal: false,
NoWait: false,
Args: nil,
ExecuteConcurrent: true,
}
if err := rabbit.Consume(ctxCancel, config, receiveMessage); err != nil {
t.Errorf("error consuming from queue: %s\n", err)
}
}()
select {
case <-ctx.Done():
t.Errorf("context canceled before receiving message")
case msg = <-gotMessage:
t.Logf("got message: %s\n", msg)
}
return
}