-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmq_test.go
43 lines (40 loc) · 894 Bytes
/
rmq_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
package urmq
import (
"testing"
"time"
zlog "github.com/rs/zerolog/log"
"github.com/streadway/amqp"
)
func TestNew(t *testing.T) {
c := Config{
Url: "amqp://guest:guest@127.0.0.1:5672",
Exchange: "test",
Queue: "test",
BindKeys: []string{"test.#"},
SendExchange: "test_send",
SendExchangeKind: "fanout",
}
// 带session的log
slog := zlog.With().
Str("service", "rmq_test").
Logger()
rmq := NewRMQ(&slog, c)
go func() {
rmq.Connect() // 连接
rmq.Handle(func(msgChan <-chan amqp.Delivery) {
slog.Print("consumer")
for d := range msgChan {
// 操作接受到的消息d
_ = d
// ...
}
slog.Print("consumer all")
})
slog.Print("consumer")
}()
rmq.Publish(&slog, []byte(`{"msg":"debug"}`), "", "")
time.Sleep(1e9 * 50)
slog.Print("准备关闭连接")
rmq.Close()
time.Sleep(1e9 * 3)
}