forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_data_toudp_test.go
155 lines (134 loc) · 4.3 KB
/
target_data_toudp_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
package test
import (
"fmt"
"log"
"os/exec"
"net"
"testing"
"time"
"github.com/go-playground/assert/v2"
httpserver "github.com/i4de/rulex/plugin/http_server"
"github.com/i4de/rulex/typex"
)
var _DataToUdp_luaCase = `function Main(arg) for i = 1, 3, 1 do local err = applib:DataToUdp('UdpServer',applib:T2J({temp = 20,humi = 13.45})) applib:log('result =>',err) applib:Sleep(100) end return 0 end`
// go test -timeout 30s -run ^Test_DataToUdp github.com/i4de/rulex/test -v -count=1
func Test_DataToUdp(t *testing.T) {
RmUnitTestDbFile(t)
engine := RunTestEngine()
engine.Start()
hh := httpserver.NewHttpApiServer()
// UdpApiServer loaded default
if err := engine.LoadPlugin("plugin.http_server", hh); err != nil {
t.Fatal(err)
}
go _start_simple_Udp_server()
//
UdpServer := typex.NewOutEnd(typex.UDP_TARGET,
"Udp", "Udp", map[string]interface{}{
"host": "127.0.0.1",
"port": 8891,
},
)
UdpServer.UUID = "UdpServer"
if err := engine.LoadOutEnd(UdpServer); err != nil {
t.Fatal(err)
}
uuid := _createTestApp_1(t)
time.Sleep(1 * time.Second)
_updateTestApp_1(t, uuid)
time.Sleep(20 * time.Second)
_deleteTestApp_1(t, uuid)
engine.Stop()
}
//--------------------------------------------------------------------------------------------------
// 起一个Udp服务器
//--------------------------------------------------------------------------------------------------
func _start_simple_Udp_server() {
//UDP服务器监听 接收广播数据
udp_addr, err := net.ResolveUDPAddr("udp", "0.0.0.0:8891")
if err != nil {
log.Fatal(err)
}
udp_conn, err := net.ListenUDP("udp", udp_addr)
if err != nil {
log.Fatal(err)
}
defer udp_conn.Close()
data := [100]byte{}
for {
udp_conn.SetReadDeadline(time.Now().Add(2 * time.Second))
n, _, err := udp_conn.ReadFromUDP(data[:])
if err != nil {
log.Fatal(err)
}
udp_conn.SetReadDeadline(time.Time{})
log.Println("UDP Received ============>:", string(data[:n]))
}
}
//--------------------------------------------------------------------------------------------------
// 资源操作
//--------------------------------------------------------------------------------------------------
func _createTestApp_1(t *testing.T) string {
// 通过接口创建一个App
body := `{"name": "testlua1","version": "1.0.0","autoStart": false,"description": "hello world"}`
output, err := exec.Command("curl",
"-X", "POST", "http://127.0.0.1:2580/api/v1/app",
"-H", "Content-Type: application/json", "-d", body).Output()
if err != nil {
t.Fatal(err)
}
t.Log("UT_createApp: ", string(output))
//
LoadDB()
mApp := []httpserver.MApp{}
unitTestDB.Raw("SELECT * FROM m_apps").Find(&mApp)
assert.Equal(t, 1, len(mApp))
t.Log(mApp[0].UUID)
assert.Equal(t, mApp[0].Name, "testlua1")
assert.Equal(t, mApp[0].Version, "1.0.0")
assert.Equal(t, mApp[0].AutoStart, false)
return mApp[0].UUID
}
func _updateTestApp_1(t *testing.T, uuid string) {
body := `{"uuid": "%s","name": "testlua11","version": "1.0.1","autoStart": true,"luaSource":"AppNAME='OK1'\nAppVERSION='0.0.3'\n%s"}`
t.Logf(body, uuid, _DataToUdp_luaCase)
output, err := exec.Command("curl",
"-X", "PUT", "http://127.0.0.1:2580/api/v1/app",
"-H", "Content-Type: application/json", "-d", fmt.Sprintf(body, uuid, _DataToUdp_luaCase)).Output()
if err != nil {
t.Fatal(err)
}
t.Log("UT_updateApp: ", string(output))
LoadDB()
mApp := []httpserver.MApp{}
unitTestDB.Raw("SELECT * FROM m_apps").Find(&mApp)
assert.Equal(t, 1, len(mApp))
t.Log("APP UUID ==> ", mApp[0].UUID)
assert.Equal(t, mApp[0].Name, "testlua11")
assert.Equal(t, mApp[0].Version, "1.0.1")
assert.Equal(t, mApp[0].AutoStart, true)
// _startTestApp
time.Sleep(1 * time.Second)
_startTestApp_1(t, mApp[0].UUID)
}
func _deleteTestApp_1(t *testing.T, uuid string) {
// 删除一个App
output, err := exec.Command("curl", "-X", "DELETE", "http://127.0.0.1:2580/api/v1/app?uuid="+uuid).Output()
if err != nil {
t.Fatal(err)
}
t.Log("UT_deleteApp: ", string(output))
//
LoadDB()
mApp := []httpserver.MApp{}
unitTestDB.Raw("SELECT * FROM m_apps").Find(&mApp)
assert.Equal(t, 0, len(mApp))
}
func _startTestApp_1(t *testing.T, uuid string) {
output, err := exec.Command("curl",
"-X", "PUT", "http://127.0.0.1:2580/api/v1/app/start?uuid="+uuid).Output()
if err != nil {
t.Fatal(err)
}
t.Log("UT_startTestApp: ", string(output))
}