forked from futurehomeno/fimpgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_client_test.go
187 lines (165 loc) · 5.33 KB
/
sync_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
package fimpgo
import (
"sync/atomic"
"testing"
log "github.com/sirupsen/logrus"
"time"
)
func TestSyncClient_Connect(t *testing.T) {
log.SetLevel(log.DebugLevel)
mqtt := NewMqttTransport("tcp://localhost:1883","fimpgotest","","",true,1,1)
err := mqtt.Start()
t.Log("Connected")
if err != nil {
t.Error("Error connecting to broker ",err)
}
inboundChan := make(MessageCh,20)
// starting responder
go func (msgChanS MessageCh) {
for msg := range msgChanS {
if msg.Payload.Type == "cmd.sensor.get_report"{
t.Log("Responde . New message. uid = ",msg.Payload.UID)
adr := Address{MsgType: MsgTypeEvt, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
responseMsg := NewFloatMessage("evt.sensor.report", "temp_sensor", float64(40.0), nil, nil, msg.Payload)
t.Log("Correlation id = ",responseMsg.CorrelationID)
mqtt.Publish(&adr,responseMsg)
}
}
}(inboundChan)
mqtt.Subscribe("pt:j1/mt:cmd/rt:app/rn:testapp/ad:1")
mqtt.RegisterChannel("test",inboundChan)
// Actual test
syncClient := NewSyncClientV2(nil,20,20)
syncClient.Connect("tcp://localhost:1883","fimpgotest2","","",true,1,1)
syncClient.AddSubscription("pt:j1/mt:evt/rt:app/rn:testapp/ad:1")
var counter int32
iterations := 1000
for it:=0 ;it<iterations;it++ {
i := it
go func() {
t.Log("Iteration = ",i)
adr := Address{MsgType: MsgTypeCmd, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
msg := NewFloatMessage("cmd.sensor.get_report", "temp_sensor", float64(35.5), nil, nil, nil)
response,err := syncClient.SendFimp(adr.Serialize(),msg,10)
if err != nil {
t.Error("Error",err)
t.Fail()
}
val , _ := response.GetFloatValue()
if val != 40.0 {
t.Error("Wong result")
t.Fail()
}
atomic.AddInt32(&counter,1)
t.Log("Iteration Done = ",i)
}()
}
for int32(iterations) >counter {
time.Sleep(1 * time.Second)
}
syncClient.Stop()
if counter!=int32(iterations) {
t.Error("Wong counter value")
t.Fail()
}
t.Log("SyncClientConnect test - OK")
}
func TestSyncClient_SendFimp(t *testing.T) {
log.SetLevel(log.DebugLevel)
mqtt := NewMqttTransport("tcp://localhost:1883","fimpgotest","","",true,1,1)
err := mqtt.Start()
t.Log("Connected")
if err != nil {
t.Error("Error connecting to broker ",err)
}
inboundChan := make(MessageCh)
// starting responder
go func (msgChanS MessageCh) {
for msg := range msgChanS {
if msg.Payload.Type == "cmd.sensor.get_report"{
t.Log("Responde . New message. uid = ",msg.Payload.UID)
adr := Address{MsgType: MsgTypeEvt, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
responseMsg := NewFloatMessage("evt.sensor.report", "temp_sensor", float64(40.0), nil, nil, msg.Payload)
t.Log("Correlation id = ",responseMsg.CorrelationID)
mqtt.Publish(&adr,responseMsg)
}
}
}(inboundChan)
mqtt.RegisterChannel("test",inboundChan)
// Actual test
syncClient := NewSyncClient(mqtt)
syncClient.AddSubscription("#")
counter := 0
for i:=0 ;i<5;i++ {
t.Log("Iteration = ",i)
adr := Address{MsgType: MsgTypeCmd, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
msg := NewFloatMessage("cmd.sensor.get_report", "temp_sensor", float64(35.5), nil, nil, nil)
response,err := syncClient.SendFimp(adr.Serialize(),msg,5)
if err != nil {
t.Error("Error",err)
t.Fail()
}
val , _ := response.GetFloatValue()
if val != 40.0 {
t.Error("Wong result")
t.Fail()
}
counter++
}
syncClient.Stop()
if counter!=5 {
t.Error("Wong counter value")
t.Fail()
}
t.Log("SyncClient test - OK")
}
func TestSyncClient_SendFimpWithTopicResponse(t *testing.T) {
log.SetLevel(log.DebugLevel)
mqtt := NewMqttTransport("tcp://localhost:1883","fimpgotest","","",true,1,1)
err := mqtt.Start()
t.Log("Connected")
if err != nil {
t.Error("Error connecting to broker ",err)
}
inboundChan := make(MessageCh)
// starting message responder
go func (msgChanS MessageCh) {
for msg := range msgChanS {
if msg.Payload.Type == "cmd.sensor.get_report"{
t.Log("Responde . New message. uid = ",msg.Payload.UID)
adr := Address{MsgType: MsgTypeEvt, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
responseMsg := NewFloatMessage("evt.sensor.report", "temp_sensor", float64(40.0), nil, nil, nil)
t.Log("Correlation id = ",responseMsg.CorrelationID)
mqtt.Publish(&adr,responseMsg)
}
}
}(inboundChan)
mqtt.RegisterChannel("test",inboundChan)
// Actual test
syncClient := NewSyncClient(mqtt)
syncClient.AddSubscription("#")
counter := 0
for i:=0 ;i<5;i++ {
t.Log("Iteration = ",i)
reqAddr := Address{MsgType: MsgTypeCmd, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
respAddr := Address{MsgType: MsgTypeEvt, ResourceType: ResourceTypeApp, ResourceName: "testapp", ResourceAddress: "1"}
msg := NewFloatMessage("cmd.sensor.get_report", "temp_sensor", float64(35.5), nil, nil, nil)
response,err := syncClient.SendFimpWithTopicResponse(reqAddr.Serialize(),msg,respAddr.Serialize(),"temp_sensor","evt.sensor.report",5)
if err != nil {
t.Error("Error",err)
t.Fail()
}
val , _ := response.GetFloatValue()
if val != 40.0 {
t.Error("Wong result")
t.Fail()
}
counter++
}
syncClient.Stop()
if counter!=5 {
t.Error("Wong counter value")
t.Fail()
}
t.Log("SyncClient test - OK")
}