-
Notifications
You must be signed in to change notification settings - Fork 8
/
interceptor_test.go
160 lines (141 loc) · 3.63 KB
/
interceptor_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
package hutils
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
)
var (
goodPing = &pb_testproto.PingRequest{Value: "goodPing", SleepTimeMs: 999}
skyClientPing = &pb_testproto.PingRequest{Value: "skyClientPing", SleepTimeMs: 999}
skyServerPing = &pb_testproto.PingRequest{Value: "skyServerPing", SleepTimeMs: 999}
)
type LogTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}
type SkywalkingClientTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}
type SkywalkingServerTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}
func TestLogTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
os.Stdout = w
logger := &Logger{}
sugarLog := logger.Init(LoggerOpt{EnableStdout: true}).Sugar()
s := &LogTestSuite{
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(NewUnaryServerAccessLogInterceptor(sugarLog, nil)),
},
},
}
s.reader = r
s.writer = w
suite.Run(t, s)
}
func (s *LogTestSuite) TestNewUnaryServerAccessLogInterceptor() {
_, err := s.Client.Ping(s.SimpleCtx(), goodPing)
s.NoError(err)
var buf bytes.Buffer
output := make(chan string, 1)
go func() {
io.Copy(&buf, s.reader)
output <- buf.String()
s.reader.Close()
}()
s.writer.Close()
o := strings.Split(<-output, "\n")
// 输出空行
s.Len(o, 1)
s.Equal(o[0], "")
}
func (s *LogTestSuite) TestMarshalJSON() {
buf, err := MarshalJSON(goodPing)
s.Nil(err)
s.True(strings.Contains(string(buf), "goodPing"))
}
func TestSkywalkingClientTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
report, err := reporter.NewLogReporter()
if err != nil {
return
}
tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(report))
os.Stdout = w
s := &SkywalkingClientTestSuite{
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
ClientOpts: []grpc.DialOption{
grpc.WithUnaryInterceptor(NewUnaryClientSkywalkingInterceptor(tracer)),
},
},
}
s.reader = r
s.writer = w
suite.Run(t, s)
}
func (c *SkywalkingClientTestSuite) TestNewUnaryClientSkywalkingInterceptor() {
_, err := c.Client.Ping(c.SimpleCtx(), skyClientPing)
c.NoError(err)
var buf bytes.Buffer
output := make(chan string, 1)
go func() {
io.Copy(&buf, c.reader)
output <- buf.String()
c.reader.Close()
}()
c.writer.Close()
o := strings.Split(<-output, "\n")
// 输出空行
c.Len(o, 1)
c.Equal(o[0], "")
}
func TestSkywalkingServerTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
os.Stdout = w
report, err := reporter.NewLogReporter()
if err != nil {
return
}
tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(report))
s := &SkywalkingServerTestSuite{
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(NewUnaryServerSkywalkingInterceptor(tracer)),
},
},
}
s.reader = r
s.writer = w
suite.Run(t, s)
}
func (s *SkywalkingServerTestSuite) TestNewUnaryServerSkywalkingInterceptor() {
_, err := s.Client.Ping(s.SimpleCtx(), skyServerPing)
s.NoError(err)
var buf bytes.Buffer
output := make(chan string, 1)
go func() {
io.Copy(&buf, s.reader)
output <- buf.String()
s.reader.Close()
}()
s.writer.Close()
o := strings.Split(<-output, "\n")
// 输出空行
s.Len(o, 1)
s.Equal(o[0], "")
}