-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbitmq_test.go
55 lines (49 loc) · 1.38 KB
/
rabbitmq_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
package rabbitmq_test
import (
"fmt"
"github.com/gozelle/amqp"
"github.com/gozelle/rabbitmq"
"github.com/gozelle/rabbitmq/consumer"
"github.com/gozelle/rabbitmq/exchange"
"github.com/gozelle/rabbitmq/message"
"github.com/gozelle/rabbitmq/producer"
"github.com/gozelle/rabbitmq/queue"
"github.com/gozelle/testify/require"
"testing"
)
type Message struct {
Id int64
}
func TestProducer(t *testing.T) {
r := rabbitmq.NewRabbitMQ[string]("amqp://root:[email protected]:5672")
p, err := r.NewProducer(
producer.WithExchange(
exchange.NewExchange(
exchange.WithName("test_exchange"), // 指定交换器
exchange.WithKind(amqp.ExchangeFanout), // 交换器类型
exchange.WithDurable(true), // 交换器持久化
),
),
producer.WithQueues(
queue.NewQueue(
queue.WithName("test_exchange_q1"), // 队列名称
queue.WithDurable(true), // 队列持久化
),
),
)
require.NoError(t, err)
err = p.Publish(message.NewMessage(Message{Id: 123}).WitPersistent(true))
require.NoError(t, err)
}
func TestConsumer(t *testing.T) {
r := rabbitmq.NewRabbitMQ[*Message]("amqp://root:[email protected]:5672")
c, err := r.NewConsumer(
consumer.WithQueue("test_exchange_q1"), // 指定消费的队列
)
require.NoError(t, err)
err = c.Consume(func(message *Message) error {
fmt.Println(message)
return nil
})
require.NoError(t, err)
}