-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubt_test.go
261 lines (228 loc) · 5.78 KB
/
ubt_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package UBT
import (
"errors"
"fmt"
errors2 "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"testing"
)
const testAppName = "hushan-dev"
const testAppVersion = "0.1.0"
var ubt *UBT
func init() {
InitUBT()
}
func InitUBT() *UBT {
if ubt != nil {
return ubt
}
ubt = Init(&ClientOptions{
UBTServer: "https://metrics-dev.spacecycle.com",
AppName: testAppName,
AppVersion: testAppVersion,
ci: true,
DebugMode: true,
})
return ubt
}
func TestUBT_Debug(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Debug("hello, debug", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, debug",
"logLevel":"debug",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
// 测试携带module参数
ubt.Debug("hello, debug", &ExtraMessage{
Module: "ubt.debug",
})
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, debug",
"module": "ubt.debug",
"logLevel":"debug",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
// 测试携带BusinessInfo参数
ubt.Debug("hello, debug", &ExtraMessage{
BusinessInfo: map[string]string{
"test": "1",
"test2": "1",
},
Module: "ubt.debug",
})
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, debug",
"businessInfo": {
"test": "1",
"test2": "1"
},
"module": "ubt.debug",
"logLevel":"debug",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Info(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Info("hello, info", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, info",
"logLevel":"info",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Warn(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Warn("hello, warn", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, warn",
"logLevel":"warn",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Error(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Error("hello, error", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, error",
"logLevel":"error",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Critical(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Critical("hello, critical", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, critical",
"logLevel":"critical",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Alert(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Alert("hello, alert", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, alert",
"logLevel":"alert",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func TestUBT_Fatal(t *testing.T) {
assertNew := assert.New(t)
// 测试最简单的debug message
ubt.Fatal("hello, fatal", nil)
assertNew.JSONEq(ubt.messageText, fmt.Sprintf(`
{
"appName": "%s",
"appVersion":"%s",
"sdkVersion": "%s",
"msg": "hello, fatal",
"logLevel":"fatal",
"serverAddr": "192.168.1.1",
"serverHostname": "hostname-xxx"
}
`, testAppName, testAppVersion, SdkVersion))
assertNew.Equal(ubt.err, nil)
assertNew.Contains(ubt.resText, "\"code\":0")
}
func fn1() error {
return fn2()
}
func fn2() error {
return fn3()
}
func fn3() error {
err := errors2.New("test error2")
return err
}
// 测试正常的Error
func TestUBT_SendError(t *testing.T) {
assertNew := assert.New(t)
err := errors.New("test error")
ubt.SendError(err, nil)
// 推断读取到了error的内容
assertNew.Contains(ubt.messageText, "test error")
assertNew.Contains(ubt.resText, "\"code\":0")
assertNew.Equal(ubt.err, nil)
}
// 测试github.com/pkg/errors里面的Error类型
func TestUBT_SendError2(t *testing.T) {
assertNew := assert.New(t)
err2 := fn1()
ubt.SendError(err2, nil)
fmt.Println("test error finish")
// 推断读取到了error的内容
assertNew.Contains(ubt.messageText, "test error")
assertNew.Contains(ubt.resText, "\"code\":0")
assertNew.Equal(ubt.err, nil)
}