-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
193 lines (165 loc) · 4.6 KB
/
suite.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package mq
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
gmq "github.com/woofdogtw/sylvia-iot-go/general-mq"
"github.com/woofdogtw/sylvia-iot-go/sdk/mq"
)
type MqEngine int
const (
EngineRabbitMQ MqEngine = iota
EngineEMQX
retry10Ms = 100
mqttSharedPrefix = "$share/sylvia-iot-sdk/"
timeFormat = "2006-01-02T15:04:05.999Z"
)
var (
testMqEngine MqEngine
testMgrConns = mq.NewConnectionPool()
testAppMgrs []*mq.ApplicationMgr
testNetMgrs []*mq.NetworkMgr
testAppNetConn gmq.GmqConnection // application/network side connection.
testAppNetQueues []gmq.GmqQueue // application/network side queues.
)
func (e MqEngine) String() string {
switch e {
case EngineEMQX:
return "emqx"
case EngineRabbitMQ:
return "rabbitmq"
}
return "unknown"
}
func Suite(mqEngine MqEngine) func() {
return func() {
Describe(fmt.Sprintf("ApplicationMgr - %s", mqEngine), func() {
BeforeEach(func() {
testMqEngine = mqEngine
})
It("NewApplicationMgr() with default options", appNewDefault)
It("NewApplicationMgr() with manual options", appNewManual)
It("NewApplicationMgr() with wrong options", appNewWrongOpts)
It("Close()", appClose)
It("uldata", appUlData)
It("uldata with wrong content", appUlDataWrong)
It("dldata", appDlData)
It("dldata with wrong content", appDlDataWrong)
It("dldata-resp", appDlDataResp)
It("dldata-resp with wrong content", appDlDataRespWrong)
It("dldata-result", appDlDataResult)
It("dldata-result with wrong content", appDlDataResultWrong)
AfterEach(afterEachFn)
})
Describe(fmt.Sprintf("NetworkMgr - %s", mqEngine), func() {
BeforeEach(func() {
testMqEngine = mqEngine
})
It("NewNetworkMgr() with default options", netNewDefault)
It("NewNetworkMgr() with manual options", netNewManual)
It("NewNetworkMgr() with wrong options", netNewWrongOpts)
It("Close()", netClose)
It("uldata", netUlData)
It("uldata with wrong content", netUlDataWrong)
It("dldata", netDlData)
It("dldata with wrong content", netDlDataWrong)
It("dldata-result", netDlDataResult)
It("dldata-result with wrong content", netDlDataResultWrong)
It("ctrl", netCtrl)
It("ctrl with wrong content", netCtrlWrong)
AfterEach(afterEachFn)
})
}
}
func afterEachFn() {
for i := range testAppMgrs {
_ = testAppMgrs[i].Close()
}
testAppMgrs = nil
for i := range testNetMgrs {
_ = testNetMgrs[i].Close()
}
testNetMgrs = nil
if testMgrConns != nil {
testMgrConns.ForceClear()
}
testMgrConns = mq.NewConnectionPool()
for i := range testAppNetQueues {
_ = testAppNetQueues[i].Close()
}
testAppNetQueues = nil
if testAppNetConn != nil {
_ = testAppNetConn.Close()
}
testAppNetConn = nil
if testMqEngine == EngineRabbitMQ {
removeRabbitmqQueues()
}
}
func newConnection(mqEngine MqEngine) gmq.GmqConnection {
switch mqEngine {
case EngineRabbitMQ:
conn, err := gmq.NewAmqpConnection(gmq.AmqpConnectionOptions{})
Expect(err).ShouldNot(HaveOccurred())
conn.Connect()
waitConnConnected(conn)
return conn
case EngineEMQX:
conn, err := gmq.NewMqttConnection(gmq.MqttConnectionOptions{})
Expect(err).ShouldNot(HaveOccurred())
conn.Connect()
waitConnConnected(conn)
return conn
default:
panic("unsupport mq_engine")
}
}
func connHostUri(mqEngine MqEngine) (*url.URL, error) {
switch mqEngine {
case EngineRabbitMQ:
return url.Parse("amqp://localhost")
case EngineEMQX:
return url.Parse("mqtt://localhost")
default:
return nil, errors.New("unsupport mq_engine")
}
}
func waitConnConnected(c gmq.GmqConnection) {
for retry := retry10Ms; retry > 0; retry-- {
time.Sleep(10 * time.Millisecond)
if c.Status() == gmq.Connected {
break
}
}
Expect(c.Status()).Should(Equal(gmq.Connected))
}
func removeRabbitmqQueues() {
client := http.Client{}
baseUrl := "http://guest:guest@localhost:15672/api/queues/%2f"
req, err := http.NewRequest(http.MethodGet, baseUrl, nil)
Expect(err).ShouldNot(HaveOccurred())
res, err := client.Do(req)
Expect(err).ShouldNot(HaveOccurred())
defer res.Body.Close()
Expect(res.StatusCode).Should(Equal(http.StatusOK))
bodyBytes, err := io.ReadAll(res.Body)
Expect(err).ShouldNot(HaveOccurred())
var queues []struct {
Name string `json:"name"`
}
err = json.Unmarshal(bodyBytes, &queues)
Expect(err).ShouldNot(HaveOccurred())
for _, queue := range queues {
req, err = http.NewRequest(http.MethodDelete, baseUrl+"/"+queue.Name, nil)
Expect(err).ShouldNot(HaveOccurred())
res, err := client.Do(req)
Expect(err).ShouldNot(HaveOccurred())
defer res.Body.Close()
}
}