This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
206 lines (191 loc) · 5.35 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
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
// Copyright 2013 Rainer Volz.
// See the LICENSE file for license information.
/*
Tests for the Mosquitto MQTT Go wrapper.
Needs a local Mosquitto instance with at least one user (test/test)
for testing the login.
*/
package gomosquittogo
import (
"bytes"
"fmt"
core "github.com/rvolz/gomosquittogo/core"
"testing"
"time"
)
var defaultBroker = "127.0.0.1"
func TestClientConnect_WithSslPsk(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
client1.Port(10884)
rc := client1.SslWithPsk("pid", "deadbeef", core.TlsV11, "")
if rc != core.Success {
t.Error("Setting SSL PSK info failed: ", rc)
}
statusc := client1.Connect()
if !statusc {
t.Error("Client using SSL PSK not connected", statusc)
}
client1.Close()
}
func TestClientNewClient(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
defer client1.Close()
if client1 == nil {
t.Error("New client without channel not created")
}
msgs := make(chan *core.MosquittoMessage, 1)
client2 := NewClient(defaultBroker, msgs)
defer client2.Close()
if client2 == nil {
t.Error("New client with channel not created")
}
}
func TestClientClose(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
client1.Close()
msgs := make(chan *core.MosquittoMessage, 1)
client2 := NewClient(defaultBroker, msgs)
client2.Close()
}
func TestClientNewNamedClient(t *testing.T) {
client1 := NewNamedClient(defaultBroker, nil, "testClient", true)
defer client1.Close()
if client1.name != "testClient" {
t.Error("Client name not set")
}
if client1.cleanSession != true {
t.Error("Named clients should have clean sessions set")
}
}
func TestClientConnect(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
status := client1.Connect()
if !status {
t.Error("Client not connected")
}
client1.Close()
}
func TestClientConnect_WithWillString(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
status := client1.SetWillString("client-test-will-string", "If you read this my connection died ...", core.QosAssuredDelivery, false)
if status != core.Success {
t.Error("Setting a string will did not work ", status)
}
statusc := client1.Connect()
if !statusc {
t.Error("Client with will string not connected", statusc)
}
client1.Close()
}
func TestClientConnect_WithWillBytes(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
status := client1.SetWillBytes("client-test-will-bytes", ([]byte)("If you read this my connection died ..."), core.QosAssuredDelivery, false)
if status != core.Success {
t.Error("Setting a bytes will did not work ", status)
}
statusc := client1.Connect()
if !statusc {
t.Error("Client with will bytes not connected", statusc)
}
client1.Close()
}
func TestClientConnect_WithLogin(t *testing.T) {
client1 := NewClient(defaultBroker, nil)
client1.User("test")
client1.Password("test")
statusc := client1.Connect()
if !statusc {
t.Error("Client with login data not connected", statusc)
}
client1.Close()
}
func TestClientSubscribe(t *testing.T) {
msgs := make(chan *core.MosquittoMessage, 1)
client1 := NewNamedClient(defaultBroker, msgs, "testClient", true)
defer client1.Close()
client1.Connect()
status := client1.SubscribeTopic("test", core.QosFireAndForget)
if status != core.Success {
t.Error("Client not subscribed due to %v", status)
}
}
func TestClientUnsubscribe(t *testing.T) {
msgs := make(chan *core.MosquittoMessage, 1)
client1 := NewNamedClient(defaultBroker, msgs, "testClient", true)
defer client1.Close()
client1.Connect()
client1.SubscribeTopic("test", core.QosFireAndForget)
status := client1.UnsubscribeTopic("test")
if status != core.Success {
t.Error("Client not unsubscribed due to %v", status)
}
}
func TestClientLoopAsync(t *testing.T) {
msgs := make(chan *core.MosquittoMessage, 2)
controlR := make(chan bool)
client1 := NewNamedClient(defaultBroker, msgs, "testClient", true)
client1.Connect()
client1.SubscribeTopic("test2", core.QosFireAndForget)
go func(ch <-chan *core.MosquittoMessage, control <-chan bool) {
for {
select {
case x := <-ch:
if x != nil {
if x.Topic != "test2" {
t.Error("Bad topic ", x.Topic)
}
if (string)(x.Payload) != "Hello World" {
t.Error("Bad payload ", x.Payload)
}
} else {
fmt.Printf("nil received\n")
}
break
case y := <-control:
if y == true {
return
}
break
}
}
}(msgs, controlR)
time.Sleep(1 * time.Second)
client1.SendString("test2", "Hello World", core.QosFireAndForget, false)
time.Sleep(10 * time.Second)
controlR <- true
client1.Close()
close(controlR)
close(msgs)
}
func TestClientSendBytes(t *testing.T) {
var content []byte = ([]byte)("Hello World")
msgs := make(chan *core.MosquittoMessage, 2)
controlR := make(chan bool)
client1 := NewNamedClient(defaultBroker, msgs, "testClient", true)
client1.Connect()
client1.SubscribeTopic("test2", core.QosFireAndForget)
go func(ch <-chan *core.MosquittoMessage, control <-chan bool) {
for {
select {
case x := <-ch:
if x.Topic != "test2" {
t.Error("Bad topic ", x.Topic)
}
if bytes.Compare(x.Payload, content) != 0 {
t.Error("Bad payload ", x.Payload)
}
case y := <-control:
if y == true {
return
}
}
}
}(msgs, controlR)
time.Sleep(1 * time.Second)
client1.SendBytes("test2", content, core.QosFireAndForget, false)
time.Sleep(10 * time.Second)
controlR <- true
client1.Close()
close(controlR)
close(msgs)
}