This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmqtt_test.go
189 lines (152 loc) · 4.84 KB
/
mqtt_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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package ttnsdk
import (
"context"
"os"
"testing"
"time"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
testlog "github.com/TheThingsNetwork/go-utils/log/test"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/mqtt"
. "github.com/smartystreets/assertions"
)
var mqttAddress string
func init() {
mqttAddress = os.Getenv("MQTT_ADDRESS")
if mqttAddress == "" {
mqttAddress = "localhost:1883"
}
}
func TestMQTT(t *testing.T) {
a := New(t)
log := testlog.NewLogger()
ttnlog.Set(log)
defer log.Print(t)
c := new(client)
c.Logger = log
defer c.Close()
c.appID = "test"
c.mqtt.ctx, c.mqtt.cancel = context.WithCancel(context.Background())
c.mqtt.client = mqtt.NewClient(log, "test-mqtt", "", "", "tcp://"+mqttAddress)
err := c.mqtt.client.Connect()
a.So(err, ShouldBeNil)
pubsub, err := c.PubSub()
a.So(err, ShouldBeNil)
defer pubsub.Close()
testDevice := pubsub.Device("test")
defer testDevice.Close()
allDevices := pubsub.AllDevices()
defer allDevices.Close()
testUplink, err := testDevice.SubscribeUplink()
a.So(err, ShouldBeNil)
allUplink, err := allDevices.SubscribeUplink()
a.So(err, ShouldBeNil)
c.mqtt.client.PublishUplink(types.UplinkMessage{
AppID: "test",
DevID: "other",
PayloadRaw: []byte{0x01, 0x02, 0x03, 0x04},
}).Wait()
c.mqtt.client.PublishUplink(types.UplinkMessage{
AppID: "test",
DevID: "test",
PayloadRaw: []byte{0x01, 0x02, 0x03, 0x04},
}).Wait()
select {
case msg := <-testUplink:
a.So(msg.DevID, ShouldEqual, "test")
a.So(msg.PayloadRaw, ShouldResemble, []byte{0x01, 0x02, 0x03, 0x04})
case <-time.After(time.Second):
t.Fatal("Did not receive from testUplink within a second")
}
for i := 0; i < 2; i++ {
select {
case msg := <-allUplink:
a.So(msg.AppID, ShouldEqual, "test")
a.So(msg.PayloadRaw, ShouldResemble, []byte{0x01, 0x02, 0x03, 0x04})
case <-time.After(time.Second):
t.Fatalf("Did not receive %d from allUplink within a second", i)
}
}
a.So(testDevice.UnsubscribeUplink(), ShouldBeNil)
a.So(allDevices.UnsubscribeUplink(), ShouldBeNil)
testEvent, err := testDevice.SubscribeEvents()
a.So(err, ShouldBeNil)
allEvent, err := allDevices.SubscribeEvents()
a.So(err, ShouldBeNil)
c.mqtt.client.PublishDeviceEvent("test", "other", types.UplinkErrorEvent, types.ErrorEventData{
Error: "some error",
}).Wait()
c.mqtt.client.PublishDeviceEvent("test", "test", types.UplinkErrorEvent, types.ErrorEventData{
Error: "some error",
}).Wait()
select {
case msg := <-testEvent:
a.So(msg.DevID, ShouldEqual, "test")
a.So(msg.Data, ShouldNotBeNil)
a.So(msg.Data, ShouldHaveSameTypeAs, new(types.ErrorEventData))
case <-time.After(time.Second):
t.Fatal("Did not receive from testEvent within a second")
}
for i := 0; i < 2; i++ {
select {
case msg := <-allEvent:
a.So(msg.AppID, ShouldEqual, "test")
a.So(msg.Data, ShouldNotBeNil)
a.So(msg.Data, ShouldHaveSameTypeAs, new(types.ErrorEventData))
case <-time.After(time.Second):
t.Fatalf("Did not receive %d from allEvent within a second", i)
}
}
a.So(testDevice.UnsubscribeEvents(), ShouldBeNil)
a.So(allDevices.UnsubscribeEvents(), ShouldBeNil)
testActivation, err := testDevice.SubscribeActivations()
a.So(err, ShouldBeNil)
allActivation, err := allDevices.SubscribeActivations()
a.So(err, ShouldBeNil)
c.mqtt.client.PublishActivation(types.Activation{
AppID: "test",
DevID: "other",
AppEUI: types.AppEUI{1, 2, 3, 4, 5, 6, 7, 8},
}).Wait()
c.mqtt.client.PublishActivation(types.Activation{
AppID: "test",
DevID: "test",
AppEUI: types.AppEUI{1, 2, 3, 4, 5, 6, 7, 8},
}).Wait()
select {
case msg := <-testActivation:
a.So(msg.DevID, ShouldEqual, "test")
case <-time.After(time.Second):
t.Fatal("Did not receive from testActivation within a second")
}
for i := 0; i < 2; i++ {
select {
case msg := <-allActivation:
a.So(msg.AppID, ShouldEqual, "test")
case <-time.After(time.Second):
t.Fatalf("Did not receive %d from allActivation within a second", i)
}
}
a.So(testDevice.UnsubscribeActivations(), ShouldBeNil)
a.So(allDevices.UnsubscribeActivations(), ShouldBeNil)
downlink := make(chan *types.DownlinkMessage)
c.mqtt.client.SubscribeDownlink(func(_ mqtt.Client, appID string, devID string, msg types.DownlinkMessage) {
downlink <- &msg
})
err = pubsub.Publish("test", &types.DownlinkMessage{
AppID: "test",
DevID: "test",
PayloadRaw: []byte{0x01, 0x02, 0x03, 0x04},
})
a.So(err, ShouldBeNil)
select {
case msg := <-downlink:
a.So(msg.AppID, ShouldEqual, "test")
a.So(msg.DevID, ShouldEqual, "test")
a.So(msg.PayloadRaw, ShouldResemble, []byte{0x01, 0x02, 0x03, 0x04})
case <-time.After(time.Second):
t.Fatal("Did not receive on downlink within a second")
}
}