forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 6
/
statinterceptor_test.go
140 lines (128 loc) · 2.82 KB
/
statinterceptor_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
package serverinterceptors
import (
"context"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/stat"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
)
func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
SetSlowThreshold(time.Second)
assert.Equal(t, time.Second, slowThreshold.Load())
}
func TestUnaryStatInterceptor(t *testing.T) {
metrics := stat.NewMetrics("mock")
interceptor := UnaryStatInterceptor(metrics)
_, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "/",
}, func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, nil
})
assert.Nil(t, err)
}
func TestLogDuration(t *testing.T) {
addrs, err := net.InterfaceAddrs()
assert.Nil(t, err)
assert.True(t, len(addrs) > 0)
tests := []struct {
name string
ctx context.Context
req interface{}
duration time.Duration
}{
{
name: "normal",
ctx: context.Background(),
req: "foo",
},
{
name: "bad req",
ctx: context.Background(),
req: make(chan lang.PlaceholderType), // not marshalable
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: time.Second,
},
{
name: "timeout",
ctx: peer.NewContext(context.Background(), &peer.Peer{
Addr: addrs[0],
}),
req: "foo",
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: slowThreshold.Load() + time.Second,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration)
})
})
}
}
func TestLogDurationWithoutContent(t *testing.T) {
addrs, err := net.InterfaceAddrs()
assert.Nil(t, err)
assert.True(t, len(addrs) > 0)
tests := []struct {
name string
ctx context.Context
req interface{}
duration time.Duration
}{
{
name: "normal",
ctx: context.Background(),
req: "foo",
},
{
name: "bad req",
ctx: context.Background(),
req: make(chan lang.PlaceholderType), // not marshalable
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: time.Second,
},
{
name: "timeout",
ctx: peer.NewContext(context.Background(), &peer.Peer{
Addr: addrs[0],
}),
req: "foo",
},
{
name: "timeout",
ctx: context.Background(),
req: "foo",
duration: slowThreshold.Load() + time.Second,
},
}
DontLogContentForMethod("foo")
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration)
})
})
}
}