-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrabbit_test.go
120 lines (106 loc) · 2.57 KB
/
rabbit_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
package rabbitgo
import (
"fmt"
"testing"
"time"
"github.com/streadway/amqp"
)
var rabbit *RabbitPool
var queue string
func init() {
queue = "rabbitgo_pool"
rabbit = New(fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "guest", "guest", "127.0.0.1", 5672, ""),
Config{
ConnectionMax: 2,
ChannelMax: 10,
ChannelActive: 2,
ChannelIdle: 5,
})
}
func sendWithoutConfirm(t *testing.T) error {
ch, err := rabbit.Get()
if err != nil {
return fmt.Errorf("Get channel error, %s", err.Error())
}
defer rabbit.Push(ch)
t.Logf("Send messages without using confirm mode......, ChId %d", ch.ChId)
qu, err := ch.Ch.QueueDeclare(queue, true, false, false, false, nil)
if err != nil {
return fmt.Errorf("Queue declare error, %s", err.Error())
}
data := fmt.Sprintf("{\"code\":200,\"message\":\"success\",\"data\":\"%s\"}", time.Now().String())
err = ch.Ch.Publish(
"",
qu.Name,
false,
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "appliction/plain",
Body: []byte(data),
},
)
if err != nil {
return fmt.Errorf("Send message error, %s", err.Error())
}
return nil
}
// go test -v -run TestSend
func TestSend(t *testing.T) {
err := sendWithoutConfirm(t)
if err != nil {
t.Logf(err.Error())
}
}
// go test -v -run TestSendConfirm
func TestSendConfirm(t *testing.T) {
go func() {
for {
if err := sendWithoutConfirm(t); err != nil {
t.Logf(err.Error())
}
time.Sleep(time.Second)
}
}()
for {
func() {
ch, err := rabbit.Get()
if err != nil {
t.Logf("Get channel error, %s", err.Error())
}
defer rabbit.Push(ch)
// confirm mode
ch.Confirm(false)
defer func() {
if confirmed := <-ch.NotifyConfirm; confirmed.Ack {
// code when messages is confirmed
t.Logf("Confirmed tag %d, ChId %d", confirmed.DeliveryTag, ch.ChId)
} else {
// code when messages is nacked
t.Logf("Nacked tag %d, ChId %d", confirmed.DeliveryTag, ch.ChId)
}
}()
qu, err := ch.Ch.QueueDeclare(queue, true, false, false, false, nil)
if err != nil {
t.Logf("Queue declare error, %s", err.Error())
}
data := fmt.Sprintf("{\"code\":200,\"message\":\"success\",\"data\":\"%s\"}", time.Now().String())
err = ch.Ch.Publish(
"",
qu.Name,
false,
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "appliction/plain",
Body: []byte(data),
},
)
if err != nil {
t.Logf("Send message error, %s", err.Error())
}
t.Logf("Send messages with using confirm mode......, ChId %d", ch.ChId)
}()
time.Sleep(time.Second)
}
}