-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwarpper_test.go
More file actions
160 lines (154 loc) · 3.77 KB
/
warpper_test.go
File metadata and controls
160 lines (154 loc) · 3.77 KB
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
package errors
import (
"encoding/json"
"errors"
stderrs "errors"
"fmt"
"runtime"
"testing"
pkgerrs "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestWarpNew(t *testing.T) {
err := stderrs.New(errMsg)
e := Wrap(err, errTrace).(*wrapper)
t.Log("\n", e)
t.Log(e.parse())
bs, err := e.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(bs))
bs, err = json.Marshal(e)
if err != nil {
t.Fatal(err)
}
t.Log(string(bs))
}
func Test_wrapper(t *testing.T) {
err := stderrs.New(errMsg)
t.Run("Wrap.nil", func(t *testing.T) {
assert.Nil(t, Wrap(nil, errMsg))
})
t.Run("Wrap.stderr", func(t *testing.T) {
pcs := [1]uintptr{}
_, e := runtime.Callers(baseSkip, pcs[:]), Wrap(err, errTrace).(*wrapper)
f, _ := runtime.CallersFrames(pcs[:]).Next()
assert.Equal(t, e.parse().stack, toCaller(f).String())
})
t.Run("wrapper.parse.cache", func(t *testing.T) {
e := Wrap(err, errTrace).(*wrapper)
delete(mFrames, e.pc[0])
caller := e.parse()
cacheCaller := e.parse()
assert.Equal(t, caller, cacheCaller)
})
t.Run("wrapper.parse", func(t *testing.T) {
e := Wrap(err, errTrace).(*wrapper)
e.pc = testFrame
caller := e.parse().stack
str := testFrameFunc
assert.Equal(t, caller, str)
})
t.Run("wrapper.Unwrap", func(t *testing.T) {
e := Wrap(err, errTrace).(*wrapper)
err1 := e.Unwrap()
assert.Equal(t, MarshalText(err), MarshalText(err1))
})
t.Run("wrapper.MarshalJSON", func(t *testing.T) {
e := Wrap(err, errTrace).(*wrapper)
e.pc = testFrame
bs, err := json.Marshal(e)
assert.Nil(t, err)
str := `{"cause":"msg!","wrapper":[{"trace":"trace!","caller":"(file1:88) func1"}]}`
assert.Equal(t, string(bs), str)
})
t.Run("wrapper.Error", func(t *testing.T) {
e := Wrap(err, errTrace)
e = Wrap(e, errTrace)
w := e.(*wrapper)
w.pc = testFrame
bs := e.Error()
assert.Equal(t, string(bs), string(w.Error()))
str := "trace!,\n (file1:88) func1;"
assert.Equal(t, string(bs), str)
})
t.Run("wrapper.%+v", func(t *testing.T) {
e := Wrap(err, errTrace)
e.(*wrapper).pc = testFrame
e = Wrap(e, errTrace)
w := e.(*wrapper)
w.pc = testFrame
bs := fmt.Sprintf("%s", e)
assert.Equal(t, string(bs), string(w.Error()))
bs1 := fmt.Sprintf("%v", e)
assert.Equal(t, string(bs), string(bs1))
bs2 := fmt.Sprintf("%+v", e)
str := "msg!;\ntrace!,\n (file1:88) func1;\ntrace!,\n (file1:88) func1;"
assert.Equal(t, string(bs2), str)
})
}
func BenchmarkWrap(b *testing.B) {
runs := []struct {
funcName string //函数名字
f func(depth int) error //调用方法
}{
{"stdWrap", func(depth int) error {
err := errors.New(errMsg)
for i := 0; i < depth; i++ {
err = fmt.Errorf("%w", errors.New(errTrace))
}
return err
}},
{"Wrap", func(depth int) error {
err := errors.New(errMsg)
for i := 0; i < depth; i++ {
err = Wrap(err, errTrace)
}
return err
}},
{"pkg.Wrap", func(depth int) error {
err := errors.New(errMsg)
for i := 0; i < depth; i++ {
err = pkgerrs.Wrap(err, errTrace)
}
return err
}},
}
depths := []int{1, 10} //嵌套深度
for _, r := range runs {
for _, depth := range depths {
name := fmt.Sprintf("%s-%d", r.funcName, depth)
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r.f(depth) //nolint
}
b.StopTimer()
})
}
}
}
func BenchmarkWrapperMarshal(b *testing.B) {
// TODO
var err error = NewCause(0, errCode, errMsg)
for i := 0; i < 0; i++ {
err = Wrap(err, errTrace)
}
b.Run("json", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
MarshalJSON(err)
}
b.StopTimer()
})
b.Run("MarshalText", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
MarshalText(err)
}
b.StopTimer()
})
}