/
rabbit_test.go
125 lines (94 loc) · 2.21 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
121
122
123
124
125
package webgo_modules
import (
"github.com/IntelliQru/logger"
"github.com/streadway/amqp"
"encoding/json"
"sync"
"testing"
"time"
)
func TestSendGet(t *testing.T) {
wg := sync.WaitGroup{}
testValue := time.Now().Format("2006-01-02T15:04:05.999999-07:00")
queueName := "TestSendGet"
listen := func(dl *amqp.Delivery) {
defer wg.Done()
dl.Ack(true)
var message interface{}
err := json.Unmarshal(dl.Body, &message)
if err != nil {
t.Error(err.Error())
}
if testValue != message {
t.Errorf("Wrong value '%v' != '%v'", testValue, message)
}
}
queue := &Queue{
Name: queueName,
Key: "Key",
Exchange: "",
Durable: true,
Autodelete: false,
Handler: listen,
}
conn := getConnection()
conn.AddQueue(queue)
go conn.ServeMQ()
time.Sleep(time.Second) // Waiting initialize
wg.Add(1)
if err := conn.Publish("", queueName, testValue); err != nil {
t.Error(err.Error())
}
wg.Wait()
}
func TestMultyThread(t *testing.T) {
wg := sync.WaitGroup{}
testValue := time.Now().Format("2006-01-02T15:04:05.999999-07:00")
queueName := "TestMultyThread"
listen := func(dl *amqp.Delivery) {
defer wg.Done()
dl.Ack(true)
var message interface{}
err := json.Unmarshal(dl.Body, &message)
if err != nil {
t.Error(err.Error())
}
if testValue != message {
t.Errorf("Wrong value '%v' != '%v'", testValue, message)
}
}
queue := &Queue{
Name: queueName,
Key: "Key",
Exchange: "",
Durable: true,
Autodelete: false,
Handler: listen,
}
conn := getConnection()
conn.AddQueue(queue)
go conn.ServeMQ()
time.Sleep(time.Second) // Waiting initialize
for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
if err := conn.Publish("", queueName, testValue); err != nil {
t.Error(err.Error())
}
}()
}
wg.Wait()
}
func getConnection() *RabbitConnection {
cp := logger.ConsoleProvider{}
log := logger.NewLogger()
log.RegisterProvider(cp)
log.AddLogProvider(cp.GetID())
log.AddErrorProvider(cp.GetID())
log.AddFatalProvider(cp.GetID())
log.AddDebugProvider(cp.GetID())
rabbitMQ := NewRabbitCluster(log)
url := "amqp://guest:guest@127.0.0.1:5672/"
conn := rabbitMQ.NewConnection("connectionId", url)
return conn
}