-
Notifications
You must be signed in to change notification settings - Fork 9
/
error-report_test.go
135 lines (119 loc) · 2.95 KB
/
error-report_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
package errorx
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
)
func startReportServer(port string) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
buf, e := ioutil.ReadAll(r.Body)
if e != nil {
fmt.Println(Wrap(e).Error())
return
}
fmt.Println("Recv:", string(buf))
w.WriteHeader(200)
w.Write([]byte("ok"))
})
server := &http.Server{
Addr: port,
WriteTimeout: time.Second * 3, //设置3秒的写超时
Handler: mux,
}
server.ListenAndServe()
}
func TestReporter(t *testing.T) {
var serverStart = make(chan int, 0)
go func() {
go startReportServer(":8196")
go startReportServer(":8197")
time.Sleep(2 * time.Second)
serverStart <- 1
}()
<-serverStart
rp := NewReporter("pro")
rp.SetContextName("request")
rp.AddURL("dev", "http://localhost:8196").
AddURL("pro", "http://localhost:8197")
rp.AddModeHandler("dev", rp.DefaultHandler).
AddModeHandler("pro", rp.ReportURLHandler)
_ = rp.Mode("dev").SaveError(errors.New("nil return"), map[string]interface{}{
"api": "/xxx/yyy/",
})
_ = rp.Mode("dev").SaveError(errors.New("nil return"), nil)
go rp.SaveError(errors.New("nil return"), map[string]interface{}{
"api": "/xxx/yyy/",
})
go rp.SaveError(errors.New("nil return"), nil)
time.Sleep(10 * time.Second)
}
func TestReporter_JSONIndent_JSON(t *testing.T) {
rp := NewReporter("pro")
eUuid, buf, e := rp.JSON(NewFromString("nil return"), map[string]interface{}{
"api": "/xx/xxx/xx",
})
if e != nil {
fmt.Println(e.Error())
t.Fail()
return
}
fmt.Println(eUuid)
fmt.Println(string(buf))
eUuid, buf, e = JSON(NewFromString("nil return"), map[string]interface{}{
"api": "/xx/xxx/xx",
})
if e != nil {
fmt.Println(e.Error())
t.Fail()
return
}
fmt.Println(eUuid)
fmt.Println(string(buf))
eUuid, buf, e = rp.JSONIndent(NewFromString("nil return"), map[string]interface{}{
"api": "/xx/xxx/xx",
}, " ", " ")
if e != nil {
fmt.Println(e.Error())
t.Fail()
return
}
fmt.Println(eUuid)
fmt.Println(string(buf))
eUuid, buf, e = JSONIndent(NewFromString("nil return"), map[string]interface{}{
"api": "/xx/xxx/xx",
}, " ", " ")
if e != nil {
fmt.Println(e.Error())
t.Fail()
return
}
fmt.Println(eUuid)
fmt.Println(string(buf))
}
func TestNewReporterConcurrent(t *testing.T) {
var serverStart = make(chan int, 0)
go func() {
go startReportServer(":7123")
time.Sleep(2 * time.Second)
serverStart <- 1
}()
rp := NewReporter("pro")
rp.AddURL("dev", "http://localhost:8196").
AddURL("pro", "http://localhost:7123")
rp.AddModeHandler("dev", DefaultHandler).
AddModeHandler("pro", rp.Mode("pro").ReportURLHandler)
<-serverStart
for i := 0; i < 100; i++ {
go rp.Mode("dev").SaveError(errors.New("nil return"), map[string]interface{}{
"api": "/xxx/yyy/",
})
go rp.Mode("pro").SaveError(errors.New("nil return"), map[string]interface{}{
"api": "/xxx/yyy/",
})
}
time.Sleep(20 * time.Second)
}