-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
176 lines (160 loc) · 4.84 KB
/
client_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
package rabbitmq
import (
"fmt"
"github.com/ysk229/go-rabbitmq/v2/bindings"
"github.com/ysk229/go-rabbitmq/v2/channels"
"github.com/ysk229/go-rabbitmq/v2/consumers"
"github.com/ysk229/go-rabbitmq/v2/exchanges"
"github.com/ysk229/go-rabbitmq/v2/lib"
"github.com/ysk229/go-rabbitmq/v2/msg"
"github.com/ysk229/go-rabbitmq/v2/options"
"github.com/ysk229/go-rabbitmq/v2/producers"
"github.com/ysk229/go-rabbitmq/v2/queues"
"log"
"testing"
"time"
)
func TestClient(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
//new client mq
url := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "admin", "123456", "127.0.0.1", 5672, "")
mq := NewClient(url)
d := make(chan string, 10)
go func() {
for i := 0; i < 3; i++ {
d <- fmt.Sprintf("this is chan %d", i)
//time.Sleep(3*time.Second)
}
close(d)
}()
p := mq.GetProducer()
for c := range d {
go p.Producer(
msg.NewMessage(
msg.WithOptionsChannel(channels.NewChannel(mq.Connection)),
msg.WithOptionsBody("sdfdsfd"+c),
),
producers.WithOptionsProducer(&producers.ProducerOpt{
Exchange: "exchangeName",
ExchangeType: lib.Topic,
RouteKey: "routeKey",
Mandatory: true,
ResendNum: 2,
}),
producers.WithOptionsProducerCallBack(&producers.CallBack{Fnc: func(ret msg.Ret) {
log.Printf("call back %+v", ret)
}}),
)
}
//select {}
}
func TestConsumer(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
//new client mq
url := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "admin", "123456", "127.0.0.1", 5672, "")
mq := NewClient(url)
//new mq channel
channelClient := mq.GetChan()
exchangeName, err := channelClient.Exchange(exchanges.NewExchange(&options.Exchange{ExchangeName: "exchangeName", ExchangeType: lib.Topic}))
if err != nil {
log.Println(err)
}
q, err := channelClient.Queue(queues.NewQueue(&options.Queue{QueueName: "testestet"}))
if err != nil {
log.Println(err)
}
routeKey := "routeKey"
err = channelClient.BindingQueue(bindings.NewBinding(&options.QueueBind{Queue: q.Name, Exchange: exchangeName, RoutingKey: routeKey}))
if err != nil {
log.Println(err)
}
go func() {
mq.GetConsumer().Consumer(
channelClient,
consumers.WithOptionsConsumer(
&consumers.ConsumerOpt{QueueName: q.Name, RoutingKey: routeKey, Exchange: exchangeName, ExchangeType: lib.Topic},
),
consumers.WithOptionsConsumerCallBack(
&consumers.CallBack{Fnc: func(delivery consumers.Delivery) error {
log.Printf("%+v", delivery)
return nil
},
},
),
)
}()
log.Printf("running for %s", "10s")
time.Sleep(10 * time.Second)
}
func TestClientExchange(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
//new client mq
url := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "admin", "123456", "127.0.0.1", 5672, "")
mq := NewClient(url)
//new mq channel
channelClient := mq.GetChan()
// new channel
channelClient2 := mq.NewChan()
////exchanges
//err := channelClient.ExchangeDeclare("test-111111", "fanout", true, false, false, true, nil)
//err := channelClient.Exchange(&exchanges.Exchange{Exchange:&options.Exchange{ExchangeName: "test-22zzzz", ExchangeType: lib.Direct,Durable: true,AutoDelete: false}})
exchangeName, err := channelClient.Exchange(exchanges.NewExchange(&options.Exchange{ExchangeName: "go-test", ExchangeType: lib.Topic}))
if err != nil {
log.Println(err)
}
//log.Println(exchangeName)
////queues
q, err := channelClient.Queue(queues.NewQueue(&options.Queue{QueueName: "go-test"}))
if err != nil {
log.Println(err)
}
log.Println(q.Name)
q2, err := channelClient2.Queue(queues.NewQueue(&options.Queue{QueueName: "testestet2"}))
if err != nil {
log.Println(err)
}
routeKey := "go-test"
err = channelClient.BindingQueue(bindings.NewBinding(&options.QueueBind{Queue: q.Name, Exchange: exchangeName, RoutingKey: routeKey}))
if err != nil {
log.Println(err)
}
log.Println(q2.Name)
//////more channel
log.Println(channelClient)
log.Println(channelClient2)
}
func TestClientProducers(t *testing.T) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
//new client mq
url := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "admin", "123456", "127.0.0.1", 5672, "")
mq := NewClient(url)
d := make(chan string, 10)
go func() {
for i := 0; i < 10; i++ {
d <- fmt.Sprintf("this is chan %d", i)
//time.Sleep(3*time.Second)
}
close(d)
}()
p := mq.NewChanProducer()
for c := range d {
p.Producer(
msg.NewMessage(
//msg.WithOptionsChannel(channels.NewChannel(mq.Connection)),
msg.WithOptionsChannel(p.Channel),
msg.WithOptionsBody("sdfdsfd"+c),
),
producers.WithOptionsProducer(&producers.ProducerOpt{
Exchange: "exchangeName",
ExchangeType: lib.Topic,
RouteKey: "routeKey",
Mandatory: true,
ResendNum: 2,
}),
producers.WithOptionsProducerCallBack(&producers.CallBack{Fnc: func(ret msg.Ret) {
log.Printf("call back %+v", ret)
}}),
)
}
//select {}
}