@@ -6,13 +6,16 @@ import (
6
6
"time"
7
7
8
8
"github.com/brocaar/loraserver/api/gw"
9
+ "github.com/brocaar/loraserver/internal/common"
10
+ "github.com/brocaar/loraserver/internal/test"
9
11
"github.com/brocaar/lorawan"
10
12
"github.com/eclipse/paho.mqtt.golang"
11
13
. "github.com/smartystreets/goconvey/convey"
12
14
)
13
15
14
16
func TestBackend (t * testing.T ) {
15
17
conf := getConfig ()
18
+ r := common .NewRedisPool (conf .RedisURL )
16
19
17
20
Convey ("Given a MQTT client" , t , func () {
18
21
opts := mqtt .NewClientOptions ().AddBroker (conf .Server ).SetUsername (conf .Username ).SetPassword (conf .Password )
@@ -22,7 +25,8 @@ func TestBackend(t *testing.T) {
22
25
So (token .Error (), ShouldBeNil )
23
26
24
27
Convey ("Given a new Backend" , func () {
25
- backend , err := NewBackend (conf .Server , conf .Username , conf .Password )
28
+ test .MustFlushRedis (r )
29
+ backend , err := NewBackend (r , conf .Server , conf .Username , conf .Password )
26
30
So (err , ShouldBeNil )
27
31
defer backend .Close ()
28
32
time .Sleep (time .Millisecond * 100 ) // give the backend some time to subscribe to the topic
@@ -39,7 +43,7 @@ func TestBackend(t *testing.T) {
39
43
token .Wait ()
40
44
So (token .Error (), ShouldBeNil )
41
45
42
- Convey ("When sending a TXPacket (from the Backend) " , func () {
46
+ Convey ("Given a TXPacket" , func () {
43
47
txPacket := gw.TXPacket {
44
48
TXInfo : gw.TXInfo {
45
49
MAC : [8 ]byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 },
@@ -52,15 +56,19 @@ func TestBackend(t *testing.T) {
52
56
MACPayload : & lorawan.MACPayload {},
53
57
},
54
58
}
55
- So (backend .SendTXPacket (txPacket ), ShouldBeNil )
56
59
57
- Convey ("Then the same packet is consumed by the MQTT client" , func () {
58
- packet := <- txPacketChan
59
- So (packet , ShouldResemble , txPacket )
60
+ Convey ("When sending it from the backend" , func () {
61
+ So (backend .SendTXPacket (txPacket ), ShouldBeNil )
62
+
63
+ Convey ("Then the same packet has been received" , func () {
64
+ packet := <- txPacketChan
65
+ So (packet , ShouldResemble , txPacket )
66
+ })
60
67
})
68
+
61
69
})
62
70
63
- Convey ("When sending a RXPacket (from the MQTT client) " , func () {
71
+ Convey ("Given an RXPacket" , func () {
64
72
rxPacket := gw.RXPacket {
65
73
RXInfo : gw.RXInfo {
66
74
Time : time .Now ().UTC (),
@@ -74,15 +82,59 @@ func TestBackend(t *testing.T) {
74
82
MACPayload : & lorawan.MACPayload {},
75
83
},
76
84
}
77
- b , err := json .Marshal (rxPacket )
78
- So (err , ShouldBeNil )
79
- token := c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
80
- token .Wait ()
81
- So (token .Error (), ShouldBeNil )
82
-
83
- Convey ("Then the same packet is consumed by the backend" , func () {
84
- packet := <- backend .RXPacketChan ()
85
- So (packet , ShouldResemble , rxPacket )
85
+
86
+ Convey ("When sending it once" , func () {
87
+ b , err := json .Marshal (rxPacket )
88
+ So (err , ShouldBeNil )
89
+ token := c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
90
+ token .Wait ()
91
+ So (token .Error (), ShouldBeNil )
92
+
93
+ Convey ("Then the same packet is consumed by the backend" , func () {
94
+ packet := <- backend .RXPacketChan ()
95
+ So (packet , ShouldResemble , rxPacket )
96
+ })
97
+ })
98
+
99
+ Convey ("When sending it twice with the same MAC" , func () {
100
+ b , err := json .Marshal (rxPacket )
101
+ So (err , ShouldBeNil )
102
+ token := c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
103
+ token .Wait ()
104
+ So (token .Error (), ShouldBeNil )
105
+ token = c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
106
+ token .Wait ()
107
+ So (token .Error (), ShouldBeNil )
108
+
109
+ Convey ("Then it is received only once" , func () {
110
+ <- backend .RXPacketChan ()
111
+
112
+ var received bool
113
+ select {
114
+ case <- backend .RXPacketChan ():
115
+ received = true
116
+ case <- time .After (time .Millisecond * 100 ):
117
+ }
118
+ So (received , ShouldBeFalse )
119
+ })
120
+ })
121
+
122
+ Convey ("When sending it twice with different MACs" , func () {
123
+ b , err := json .Marshal (rxPacket )
124
+ So (err , ShouldBeNil )
125
+ token := c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
126
+ token .Wait ()
127
+
128
+ rxPacket .RXInfo .MAC = [8 ]byte {8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }
129
+ b , err = json .Marshal (rxPacket )
130
+ So (err , ShouldBeNil )
131
+ token = c .Publish ("gateway/0102030405060708/rx" , 0 , false , b )
132
+ token .Wait ()
133
+
134
+ Convey ("Then it is received twice" , func () {
135
+ <- backend .RXPacketChan ()
136
+ <- backend .RXPacketChan ()
137
+ })
86
138
})
87
139
})
88
140
})
0 commit comments