-
Notifications
You must be signed in to change notification settings - Fork 2
/
amqp_test.go
134 lines (124 loc) · 3.22 KB
/
amqp_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
//go:build integration
package utils_test
import (
"fmt"
"sync"
"testing"
"github.com/Azure/go-amqp"
"github.com/stretchr/testify/suite"
"github.com/dollarsignteam/go-utils"
)
type AMQPTestSuite struct {
suite.Suite
amqpClient *utils.AMQPClient
}
func (suite *AMQPTestSuite) SetupTest() {
client, err := utils.AMQP.New(utils.AMQPConfig{
URL: "amqp://admin:admin@localhost:5672"})
if err != nil {
suite.T().Fatal(err)
}
suite.amqpClient = client
}
func (suite *AMQPTestSuite) TearDownSuite() {
if suite.amqpClient != nil {
suite.amqpClient.Close()
}
}
func (suite *AMQPTestSuite) TestSendToQueue() {
queueName := "test-queue"
messageCount := 1000
sender, err := suite.amqpClient.NewSender(queueName)
if err != nil {
suite.FailNow("failed to create sender", err)
return
}
suite.NotNil(sender)
wg := sync.WaitGroup{}
count := 0
receiverListCount := 9
r, e := suite.amqpClient.NewReceiver(queueName)
suite.NoError(e)
rList, eList := suite.amqpClient.NewReceiverList(queueName, receiverListCount)
suite.NoError(eList)
suite.Len(rList, receiverListCount)
handlerFunc := func(message *amqp.Message, err error) *utils.AMQPMessageHandler {
suite.NoError(err)
if err != nil {
return &utils.AMQPMessageHandler{
Rejected: true,
IsClosed: false,
}
}
subject := utils.AMQP.GetSubject(message)
data := string(message.GetData())
suite.Equal(subject, data)
count++
defer wg.Done()
return nil
}
go suite.amqpClient.Received(r, handlerFunc)
go suite.amqpClient.ReceivedList(rList, handlerFunc)
suite.T().Log("Waiting for all messages to be received...")
for i := 0; i < messageCount; i++ {
wg.Add(1)
go func(index int) {
id := fmt.Sprintf("index[%d]", index)
message := amqp.NewMessage([]byte(id))
message.Properties = &amqp.MessageProperties{
GroupID: utils.PointerOf(id),
Subject: utils.PointerOf(id),
}
err := suite.amqpClient.Send(sender, message, false)
suite.NoError(err)
}(i)
}
suite.T().Log("Waiting for all messages to be sent...")
wg.Wait()
suite.Equal(messageCount, count)
}
func (suite *AMQPTestSuite) TestPublishToTopic() {
topicName := "test-topic"
messageCount := 1000
publisher, err := suite.amqpClient.NewPublisher(topicName)
if err != nil {
suite.FailNow("failed to create publisher", err)
return
}
suite.NotNil(publisher)
wg := sync.WaitGroup{}
count := 0
r, e := suite.amqpClient.NewSubscriber(topicName)
suite.NoError(e)
handlerFunc := func(message *amqp.Message, err error) *utils.AMQPMessageHandler {
suite.NoError(err)
if err != nil {
return &utils.AMQPMessageHandler{
Rejected: true,
IsClosed: false,
}
}
count++
defer wg.Done()
return &utils.AMQPMessageHandler{
Rejected: true,
}
}
go suite.amqpClient.Received(r, handlerFunc)
suite.T().Log("Waiting for all messages to be received...")
for i := 0; i < messageCount; i++ {
wg.Add(1)
go func(index int) {
id := fmt.Sprintf("index[%d]", index)
message := amqp.NewMessage([]byte(id))
err := suite.amqpClient.Publish(publisher, message)
suite.NoError(err)
}(i)
}
suite.T().Log("Waiting for all messages to be publish...")
wg.Wait()
suite.Equal(messageCount, count)
}
func TestIntegrationAMQPTestSuite(t *testing.T) {
suite.Run(t, new(AMQPTestSuite))
}