forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_base_test.go
223 lines (197 loc) · 5.2 KB
/
client_base_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright Go-IIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libmqtt
import (
"bytes"
"testing"
"time"
)
type extraHandler struct {
onConnHandle func(client Client, server string, code byte, err error) bool
afterConnSuccess func(client Client)
afterPubSuccess func(client Client)
afterSubSuccess func(client Client)
afterUnSubSuccess func(client Client)
}
func testTLSOption() Option {
return WithTLS(
"./testdata/client-cert.pem",
"./testdata/client-key.pem",
"./testdata/ca-cert.pem",
"MacBook-Air.local",
true)
}
func baseClient(t *testing.T, handler *extraHandler) Client {
var (
c Client
err error
)
c, err = NewClient(
WithLog(Verbose),
// WithVersion(V5, true),
WithDialTimeout(10),
WithKeepalive(10, 1.2),
WithAutoReconnect(true),
WithBackoffStrategy(1*time.Second, 5*time.Second, 1.5),
WithConnPacket(ConnPacket{
Username: "admin",
Password: "public",
WillTopic: "test",
WillQos: Qos0,
WillRetain: false,
WillMessage: []byte("test data"),
}),
WithConnHandleFunc(testConnHandler(&c, t, handler)),
WithPubHandleFunc(func(client Client, topic string, err error) {
if err != nil {
t.Error(err)
t.FailNow()
}
if handler != nil && handler.afterPubSuccess != nil {
println("afterPubSuccess()")
handler.afterPubSuccess(c)
}
}),
WithSubHandleFunc(func(client Client, topics []*Topic, err error) {
println("exH.Sub")
if err != nil {
t.Error(err)
t.FailNow()
}
if handler != nil && handler.afterSubSuccess != nil {
println("afterSubSuccess()")
handler.afterSubSuccess(c)
}
}),
WithUnsubHandleFunc(func(client Client, topics []string, err error) {
println("exH.UnSub")
if err != nil {
t.Error(err)
t.FailNow()
}
if handler != nil && handler.afterUnSubSuccess != nil {
println("afterUnSubSuccess()")
handler.afterUnSubSuccess(c)
}
}),
WithNetHandleFunc(func(client Client, server string, err error) {
if err != nil {
// t.Error(err)
// t.FailNow()
}
}),
WithPersistHandleFunc(func(client Client, packet Packet, err error) {}),
)
if err != nil {
panic("create baseClient failed")
}
return c
}
func testConnHandler(c *Client, t *testing.T, exH *extraHandler) ConnHandleFunc {
return func(client Client, server string, code byte, err error) {
if exH != nil && exH.onConnHandle != nil {
println("onConnHandle()")
if exH.onConnHandle(*c, server, code, err) {
return
}
}
if err != nil {
t.Errorf("connect errored: %v", err)
t.FailNow()
}
if code != CodeSuccess {
t.Errorf("connect failed with code: %d", code)
t.FailNow()
}
if exH != nil && exH.afterConnSuccess != nil {
println("afterConnSuccess()")
exH.afterConnSuccess(*c)
}
}
}
func websocketPlainClient(t *testing.T, exH *extraHandler) (c Client, connFunc func()) {
c = baseClient(t, exH)
connFunc = func() {
if err := c.ConnectServer("localhost:8083/mqtt",
WithWebSocketConnector(0, nil),
); err != nil {
t.Error(err)
t.FailNow()
}
}
return
}
func websocketTLSClient(t *testing.T, exH *extraHandler) (c Client, connFunc func()) {
c = baseClient(t, exH)
connFunc = func() {
if err := c.ConnectServer("localhost:8084/mqtt",
testTLSOption(),
WithWebSocketConnector(0, nil),
); err != nil {
t.Error(err)
t.FailNow()
}
}
return
}
func tcpPlainClient(t *testing.T, exH *extraHandler) (c Client, connFunc func()) {
c = baseClient(t, exH)
connFunc = func() {
if err := c.ConnectServer("localhost:1883"); err != nil {
t.Error(err)
t.FailNow()
}
}
return
}
func tcpTLSClient(t *testing.T, exH *extraHandler) (c Client, connFunc func()) {
c = baseClient(t, exH)
connFunc = func() {
if err := c.ConnectServer("localhost:8883",
testTLSOption(),
); err != nil {
t.Error(err)
t.FailNow()
}
}
return
}
func allClients(t *testing.T, handler *extraHandler) map[Client]func() {
ret := make(map[Client]func())
// tcp based
tcp, tcpC := tcpPlainClient(t, handler)
ret[tcp] = tcpC
tcps, tcpsC := tcpTLSClient(t, handler)
ret[tcps] = tcpsC
// websocket based
ws, wsC := websocketPlainClient(t, handler)
ret[ws] = wsC
wss, wssC := websocketTLSClient(t, handler)
ret[wss] = wssC
return ret
}
func handleTopicAndSub(c Client, t *testing.T) {
for i := range testTopics {
c.HandleTopic(testTopics[i], func(client Client, topic string, maxQos byte, msg []byte) {
if maxQos != testPubMsgs[i].Qos || bytes.Compare(testPubMsgs[i].Payload, msg) != 0 {
t.Errorf("fail at sub topic = %v, content unexpected, payload = %v, target payload = %v",
topic, string(msg), string(testPubMsgs[i].Payload))
t.FailNow()
}
})
}
c.Subscribe(testSubTopics...)
}