-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_client_test.go
92 lines (80 loc) · 2.27 KB
/
mqtt_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
package go_mqtt_client
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/rs/zerolog/log"
"testing"
"time"
)
var lastPayload string
var handleMqttMessages mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
log.Debug().Str("topic", msg.Topic()).Str("payload", string(msg.Payload())).Msg("Received MQTT message")
lastPayload = string(msg.Payload())
}
func TestMqttClientInit(t *testing.T) {
subTopic := []string{"a", "b", "c"}
broker := "tcp://127.0.0.1:1883"
client := MqttClientInit(handleMqttMessages, subTopic, broker)
err := client.Start()
if err != nil {
t.Error(err)
}
client.Destroy()
}
func TestMqttClientInitFailsToConnect(t *testing.T) {
subTopic := []string{"a", "b", "c"}
broker := "tcp://9.99.9.99:1883"
client := MqttClientInit(handleMqttMessages, subTopic, broker)
client.Options.ConnectTimeout = 1 * time.Second
err := client.Start()
if err.Error() != "network Error : dial tcp 9.99.9.99:1883: i/o timeout" {
t.Error(err)
}
client.Destroy()
}
func TestMqttClientInitSameClientIds(t *testing.T) {
subTopic := []string{"a", "b", "c"}
broker := "tcp://127.0.0.1:1883"
clientA := MqttClientInit(handleMqttMessages, subTopic, broker)
clientA.Options.SetClientID("id1")
err := clientA.Start()
if err != nil {
t.Error(err)
}
clientB := MqttClientInit(handleMqttMessages, subTopic, broker)
clientB.Options.SetClientID("id1")
err = clientB.Start()
if err != nil {
t.Error(err)
}
clientA.Destroy()
clientB.Destroy()
}
func TestMqttClientPublish(t *testing.T) {
broker := "tcp://127.0.0.1:1883"
subTopicA := []string{"b"}
clientA := MqttClientInit(handleMqttMessages, subTopicA, broker)
err := clientA.Start()
if err != nil {
t.Error(err)
}
subTopicB := []string{"a"}
clientB := MqttClientInit(handleMqttMessages, subTopicB, broker)
err = clientB.Start()
if err != nil {
t.Error(err)
}
payload1 := "hello"
clientA.Publish([]byte(payload1), "b")
time.Sleep(1 * time.Millisecond)
if lastPayload != payload1 {
t.Errorf("Expected payload %s got payload %s", payload1, lastPayload)
}
payload2 := "world"
clientB.Publish([]byte(payload2), "a")
time.Sleep(1 * time.Millisecond)
if lastPayload != payload2 {
t.Errorf("Expected payload %s got payload %s", payload2, lastPayload)
}
clientA.Destroy()
clientB.Destroy()
}