forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
139 lines (124 loc) · 2.79 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/service"
)
func TestCommandVersion(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = append(os.Args, "version")
assert.NotPanics(t, func() {
main()
})
}
func TestCommandMigrate(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = append(os.Args, "migrate", "--reset", "--force")
assert.NotPanics(t, func() {
main()
})
}
func TestCommandStart(t *testing.T) {
oldArgs := os.Args
defer func() {
os.Args = oldArgs
service.Stop(func() {})
log.Println("服务已关闭")
}()
go func() {
os.Args = append(os.Args, "start")
main()
}()
// 发送请求
request := func() (maps.MapStr, error) {
time.Sleep(time.Microsecond * 2000)
url := fmt.Sprintf("http://%s:%d/api/user/find/1?select=id,name", "127.0.0.1", config.Conf.Port)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
res := maps.MakeMapStr()
err = jsoniter.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return res, nil
}
// 等待服务启动
time.Sleep(time.Second * 2)
times := 0
for times < 20 { // 2秒超时
times++
fmt.Printf("Trying(%d)...", times)
res, err := request()
if err != nil {
fmt.Printf(" %s\n", err.Error())
continue
}
assert.Equal(t, 1, any.Of(res.Get("id")).CInt())
assert.Equal(t, "管理员", res.Get("name"))
return
}
assert.True(t, false)
}
func TestCommandStop(t *testing.T) {
oldArgs := os.Args
defer func() {
os.Args = oldArgs
}()
go func() {
os.Args = append(os.Args, "start")
main()
}()
// 发送请求
request := func() (maps.MapStr, error) {
time.Sleep(time.Microsecond * 2000)
url := fmt.Sprintf("http://%s:%d/api/user/find/1?select=id,name", "127.0.0.1", config.Conf.Port)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
res := maps.MakeMapStr()
err = jsoniter.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return res, nil
}
// 等待服务启动
time.Sleep(time.Second * 2)
times := 0
for times < 20 { // 2秒超时
times++
res, err := request()
if err != nil {
fmt.Println("REQUEST ERROR:", err)
continue
}
assert.Equal(t, 1, any.Of(res.Get("id")).CInt())
assert.Equal(t, "管理员", res.Get("name"))
// 测试关闭
service.Stop(func() { log.Println("服务已关闭") })
time.Sleep(time.Second * 5)
_, err = request()
assert.NotNil(t, err)
return
}
assert.True(t, false)
}