forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_advanced_test.go
259 lines (230 loc) · 8.46 KB
/
option_advanced_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
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package server
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/cloudwego/kitex/internal/mocks"
mock_remote "github.com/cloudwego/kitex/internal/mocks/remote"
internal_server "github.com/cloudwego/kitex/internal/server"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/acl"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/rpcinfo"
)
// TestWithServerBasicInfo tests the creation of a server with basic info
func TestWithServerBasicInfo(t *testing.T) {
svcName := "svcNameWithServerBasicInfo"
svr := NewServer(WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: svcName,
}))
iSvr := svr.(*server)
test.Assert(t, iSvr.opt.Svr.ServiceName == svcName, iSvr.opt.Svr.ServiceName)
}
// TestWithServerBasicInfo tests the creation of a server with ACLRules option
func TestACLRulesOption(t *testing.T) {
var rules []acl.RejectFunc
rules = append(rules, func(ctx context.Context, request interface{}) error {
return nil
})
rules = append(rules, func(ctx context.Context, request interface{}) error {
return nil
})
svr, _ := NewTestServer(WithACLRules(rules...))
err := svr.RegisterService(mocks.ServiceInfo(), mocks.MyServiceHandler())
test.Assert(t, err == nil, err)
time.AfterFunc(100*time.Millisecond, func() {
err := svr.Stop()
test.Assert(t, err == nil, err)
})
err = svr.Run()
test.Assert(t, err == nil, err)
iSvr := svr.(*server)
test.Assert(t, len(iSvr.opt.ACLRules) == 2, len(iSvr.opt.ACLRules))
test.Assert(t, iSvr.opt.ACLRules[0] != nil)
test.Assert(t, iSvr.opt.ACLRules[1] != nil)
}
// TestProxyOptionPanic tests the creation of a server with Proxy option,if the proxy is not nil, should panic
func TestProxyOptionPanic(t *testing.T) {
o := internal_server.NewOptions(nil)
o.Proxy = &proxyMock{}
opts := []Option{
WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: "svcName",
}),
WithProxy(&proxyMock{}),
}
test.Panic(t, func() {
internal_server.ApplyOptions(opts, o)
})
}
type myLimitReporter struct{}
func (m *myLimitReporter) ConnOverloadReport() {
}
func (m *myLimitReporter) QPSOverloadReport() {
}
// TestLimitReporterOption tests the creation of a server with LimitReporter option
func TestLimitReporterOption(t *testing.T) {
my := &myLimitReporter{}
svr, _ := NewTestServer(WithLimitReporter(my))
err := svr.RegisterService(mocks.ServiceInfo(), mocks.MyServiceHandler())
test.Assert(t, err == nil, err)
time.AfterFunc(100*time.Millisecond, func() {
err := svr.Stop()
test.Assert(t, err == nil, err)
})
err = svr.Run()
test.Assert(t, err == nil, err)
iSvr := svr.(*server)
test.Assert(t, iSvr.opt.Limit.LimitReporter != nil)
test.DeepEqual(t, iSvr.opt.Limit.LimitReporter, my)
}
// TestGenericOptionPanic tests the creation of a server with RemoteOpt.PayloadCodec,if the generic is nil, should panic
func TestGenericOptionPanic(t *testing.T) {
test.Panic(t, func() {
NewServer(WithGeneric(nil))
})
}
// TestGenericOption tests the creation of a server with RemoteOpt.PayloadCodec option
func TestGenericOption(t *testing.T) {
g := generic.BinaryThriftGeneric()
svr, _ := NewTestServer(WithGeneric(g))
err := svr.RegisterService(mocks.ServiceInfo(), mocks.MyServiceHandler())
test.Assert(t, err == nil, err)
time.AfterFunc(100*time.Millisecond, func() {
err := svr.Stop()
test.Assert(t, err == nil, err)
})
err = svr.Run()
test.Assert(t, err == nil, err)
iSvr := svr.(*server)
test.DeepEqual(t, iSvr.opt.RemoteOpt.PayloadCodec, g.PayloadCodec())
}
// TestBoundHandlerOptionPanic tests the creation of a server with remote.BoundHandler option,if the bound handler is nil, should panic
func TestBoundHandlerOptionPanic(t *testing.T) {
test.Panic(t, func() {
NewServer(WithBoundHandler(nil))
})
}
// TestWithBoundHandler tests the creation of a server with remote.BoundHandler option, if there are duplicate bound handlers, avoid creating an option
func TestWithBoundHandler(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockInboundHandler := mock_remote.NewMockInboundHandler(ctrl)
opts := internal_server.NewOptions([]internal_server.Option{WithBoundHandler(mockInboundHandler)})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 0)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 1)
opts = internal_server.NewOptions([]internal_server.Option{WithBoundHandler(mockInboundHandler), WithBoundHandler(mockInboundHandler)})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 0)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 1)
mockInboundHandler2 := mock_remote.NewMockInboundHandler(ctrl)
opts = internal_server.NewOptions([]internal_server.Option{WithBoundHandler(mockInboundHandler), WithBoundHandler(mockInboundHandler2)})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 0)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 1)
mockOutboundHandler := mock_remote.NewMockOutboundHandler(ctrl)
opts = internal_server.NewOptions([]internal_server.Option{WithBoundHandler(mockOutboundHandler)})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 1)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 0)
opts = internal_server.NewOptions(
[]internal_server.Option{WithBoundHandler(mockOutboundHandler), WithBoundHandler(mockOutboundHandler)})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 1)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 0)
mockOutboundHandler2 := mock_remote.NewMockOutboundHandler(ctrl)
opts = internal_server.NewOptions([]internal_server.Option{
WithBoundHandler(mockOutboundHandler), WithBoundHandler(mockOutboundHandler2), WithBoundHandler(mockOutboundHandler),
})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 1)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 0)
mockDuplexBoundHandler := mock_remote.NewMockDuplexBoundHandler(ctrl)
opts = internal_server.NewOptions([]internal_server.Option{
WithBoundHandler(mockDuplexBoundHandler), WithBoundHandler(mockDuplexBoundHandler),
})
test.Assert(t, len(opts.RemoteOpt.Outbounds) == 1)
test.Assert(t, len(opts.RemoteOpt.Inbounds) == 1)
}
// TestExitSignalOption tests the creation of a server with ExitSignal option
func TestExitSignalOption(t *testing.T) {
stopSignal := make(chan error, 1)
stopErr := errors.New("stop signal")
svr, _ := NewTestServer(WithExitSignal(func() <-chan error {
return stopSignal
}))
err := svr.RegisterService(mocks.ServiceInfo(), mocks.MyServiceHandler())
test.Assert(t, err == nil, err)
time.AfterFunc(100*time.Millisecond, func() {
stopSignal <- stopErr
})
err = svr.Run()
test.Assert(t, err == stopErr, err)
}
func TestWithSupportedTransportsFunc(t *testing.T) {
cases := []struct {
options []Option
wantTransports []string
}{
{
options: []Option{
WithSupportedTransportsFunc(func(option remote.ServerOption) []string {
return []string{"mock1", "mock2"}
}),
},
wantTransports: []string{"mock1", "mock2"},
},
{
options: []Option{
WithSupportedTransportsFunc(func(option remote.ServerOption) []string {
return []string{}
}),
},
wantTransports: []string{},
},
{
wantTransports: []string{"ttheader", "framed", "ttheader_framed", "grpc"},
},
{
options: []Option{
WithMuxTransport(),
},
wantTransports: []string{"ttheader_mux"},
},
{
options: []Option{
WithTransHandlerFactory(nil),
},
wantTransports: nil,
},
}
var svr Server
for _, tcase := range cases {
svr = NewServer(tcase.options...)
svcInfo := mocks.ServiceInfo()
svr.RegisterService(svcInfo, new(mockImpl))
svr.(*server).fillMoreServiceInfo(nil)
svcInfo = svr.(*server).svcs.SearchService(svcInfo.ServiceName, mocks.MockMethod, false)
test.Assert(t, reflect.DeepEqual(svcInfo.Extra["transports"], tcase.wantTransports))
}
}
// GenericServiceImpl ...
type mockImpl struct{}
// GenericCall ...
func (g *mockImpl) GenericCall(ctx context.Context, method string, request interface{}) (response interface{}, err error) {
return nil, nil
}