forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_inline_test.go
374 lines (316 loc) · 10.7 KB
/
service_inline_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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright 2023 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 client
import (
"context"
"errors"
"runtime"
"runtime/debug"
"sync/atomic"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/internal/client"
"github.com/cloudwego/kitex/internal/mocks"
internal_server "github.com/cloudwego/kitex/internal/server"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/consts"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/rpcinfo/remoteinfo"
"github.com/cloudwego/kitex/pkg/serviceinfo"
)
type serverInitialInfoImpl struct {
EndpointsFunc func(ctx context.Context, req, resp interface{}) (err error)
}
func (s serverInitialInfoImpl) Endpoints() endpoint.Endpoint {
if s.EndpointsFunc != nil {
return s.EndpointsFunc
}
return func(ctx context.Context, req, resp interface{}) (err error) {
return nil
}
}
func (s serverInitialInfoImpl) Option() *internal_server.Options {
return internal_server.NewOptions(nil)
}
func (s serverInitialInfoImpl) GetServiceInfos() map[string]*serviceinfo.ServiceInfo {
return nil
}
func newMockServerInitialInfo() ServerInitialInfo {
return &serverInitialInfoImpl{}
}
func newMockServiceInlineClient(tb testing.TB, ctrl *gomock.Controller, extra ...Option) Client {
opts := []Option{
WithTransHandlerFactory(newMockCliTransHandlerFactory(ctrl)),
WithResolver(resolver404(ctrl)),
WithDialer(newDialer(ctrl)),
WithDestService("destService"),
}
opts = append(opts, extra...)
svcInfo := mocks.ServiceInfo()
cli, err := NewServiceInlineClient(svcInfo, newMockServerInitialInfo(), opts...)
test.Assert(tb, err == nil)
return cli
}
func TestServiceInlineCall(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
cli := newMockServiceInlineClient(t, ctrl)
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil, err)
}
func TestServiceInlineTagOptions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
tgs := map[string]string{}
cls := func(m map[string]string) {
for k := range m {
delete(m, k)
}
}
md := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, res interface{}) error {
ri := rpcinfo.GetRPCInfo(ctx)
test.Assert(t, ri != nil)
to := ri.To()
test.Assert(t, to != nil)
re := remoteinfo.AsRemoteInfo(to)
test.Assert(t, re != nil)
for k, v := range tgs {
val, ok := re.Tag(k)
test.Assertf(t, ok, "expected tag not found: %s", k)
test.Assertf(t, v == val, "values of tag '%s' not equal: '%s' != '%s'", k, v, val)
}
return nil
}
}
var options []client.Option
options = append(options, WithMiddleware(md))
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
cli := newMockServiceInlineClient(t, ctrl, options...)
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
cls(tgs)
tgs["cluster"] = "client cluster"
tgs["idc"] = "client idc"
cli = newMockServiceInlineClient(t, ctrl, WithTag("cluster", "client cluster"), WithTag("idc", "client idc"))
err = cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
cls(tgs)
tgs["cluster"] = "call cluster"
tgs["idc"] = "call idc"
ctx = NewCtxWithCallOptions(ctx, []callopt.Option{
callopt.WithTag("cluster", "cluster"),
callopt.WithTag("idc", "idc"),
})
err = cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
}
func TestServiceInlineTagOptionLocks0(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
md := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, res interface{}) error {
re := remoteinfo.AsRemoteInfo(rpcinfo.GetRPCInfo(ctx).To())
var err error
err = re.SetTag("cluster", "clusterx")
test.Assert(t, err == nil)
test.Assert(t, re.DefaultTag("cluster", "") == "clusterx")
err = re.SetTag("idc", "idcx")
test.Assert(t, err == nil)
test.Assert(t, re.DefaultTag("idc", "") == "idcx")
return nil
}
}
var options []client.Option
options = append(options, WithMiddleware(md))
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
cli := newMockServiceInlineClient(t, ctrl, options...)
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
}
func TestServiceInlineTagOptionLocks1(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
md := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, res interface{}) error {
re := remoteinfo.AsRemoteInfo(rpcinfo.GetRPCInfo(ctx).To())
var err error
err = re.SetTag("cluster", "whatever")
test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
err = re.SetTag("idc", "whatever")
test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
return nil
}
}
var options []client.Option
options = append(options, WithMiddleware(md))
options = append(options, WithTag("cluster", "client cluster"))
options = append(options, WithTag("idc", "client idc"))
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
cli := newMockServiceInlineClient(t, ctrl, options...)
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
}
func TestServiceInlineTagOptionLocks2(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockOnewayMethod
md := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, res interface{}) error {
re := remoteinfo.AsRemoteInfo(rpcinfo.GetRPCInfo(ctx).To())
var err error
err = re.SetTag("cluster", "whatever")
test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
err = re.SetTag("idc", "whatever")
test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
return nil
}
}
var options []client.Option
options = append(options, WithMiddleware(md))
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
cli := newMockServiceInlineClient(t, ctrl, options...)
ctx = NewCtxWithCallOptions(ctx, []callopt.Option{
callopt.WithTag("cluster", "cluster"),
callopt.WithTag("idc", "idc"),
})
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
}
func TestServiceInlineTimeoutOptions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
md := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req, res interface{}) error {
ri := rpcinfo.GetRPCInfo(ctx)
test.Assert(t, ri != nil)
cfgs := ri.Config()
test.Assert(t, cfgs != nil)
mcfg := rpcinfo.AsMutableRPCConfig(cfgs)
test.Assert(t, mcfg != nil)
var err error
err = mcfg.SetRPCTimeout(time.Hour)
test.Assert(t, err == nil)
test.Assert(t, cfgs.RPCTimeout() == time.Hour)
err = mcfg.SetConnectTimeout(time.Hour * 2)
test.Assert(t, err == nil)
test.Assert(t, cfgs.ConnectTimeout() == time.Hour*2)
return nil
}
}
var options []client.Option
options = append(options, WithMiddleware(md))
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
cli := newMockServiceInlineClient(t, ctrl, options...)
err := cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil)
}
func TestServiceInlineClientFinalizer(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
debug.SetGCPercent(-1)
defer debug.SetGCPercent(100)
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
t.Logf("Before new clients, allocation: %f Mb, Number of allocation: %d\n", mb(ms.HeapAlloc), ms.HeapObjects)
var (
closeCalledCnt int32
succeedCnt = 10000
failedCnt = 10000
cliCnt = succeedCnt + failedCnt
)
clis := make([]Client, cliCnt)
// clients that init successfully.
for i := 0; i < succeedCnt; i++ {
svcInfo := mocks.ServiceInfo()
mockClient, err := NewClient(svcInfo, WithDestService("destService"), WithShortConnection(),
WithCloseCallbacks(func() error {
atomic.AddInt32(&closeCalledCnt, 1)
return nil
}))
test.Assert(t, err == nil, err)
clis[i] = mockClient
}
// clients that init failed, closeCallback should be called
for i := succeedCnt; i < cliCnt; i++ {
mockClient, err := NewClient(svcInfo, WithDestService(""), WithShortConnection(),
WithCloseCallbacks(func() error {
atomic.AddInt32(&closeCalledCnt, 1)
return nil
}))
test.Assert(t, err != nil, err)
clis[i] = mockClient
}
runtime.ReadMemStats(&ms)
t.Logf("After new clients, allocation: %f Mb, Number of allocation: %d\n", mb(ms.HeapAlloc), ms.HeapObjects)
runtime.GC()
runtime.ReadMemStats(&ms)
firstGCHeapAlloc, firstGCHeapObjects := mb(ms.HeapAlloc), ms.HeapObjects
t.Logf("After first GC, allocation: %f Mb, Number of allocation: %d\n", firstGCHeapAlloc, firstGCHeapObjects)
time.Sleep(200 * time.Millisecond) // ensure the finalizer be executed
test.Assert(t, atomic.LoadInt32(&closeCalledCnt) == int32(cliCnt)) // ensure CloseCallback of client has been called
runtime.GC()
runtime.ReadMemStats(&ms)
secondGCHeapAlloc, secondGCHeapObjects := mb(ms.HeapAlloc), ms.HeapObjects
t.Logf("After second GC, allocation: %f Mb, Number of allocation: %d\n", secondGCHeapAlloc, secondGCHeapObjects)
test.Assert(t, secondGCHeapAlloc < firstGCHeapAlloc/2 && secondGCHeapObjects < firstGCHeapObjects/2)
}
func TestServiceInlineMethodKeyCall(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mtd := mocks.MockMethod
opts := []Option{
WithTransHandlerFactory(newMockCliTransHandlerFactory(ctrl)),
WithResolver(resolver404(ctrl)),
WithDialer(newDialer(ctrl)),
WithDestService("destService"),
}
svcInfo := mocks.ServiceInfo()
s := serverInitialInfoImpl{}
s.EndpointsFunc = func(ctx context.Context, req, resp interface{}) (err error) {
test.Assert(t, ctx.Value(consts.CtxKeyMethod) == mtd)
return nil
}
cli, err := NewServiceInlineClient(svcInfo, s, opts...)
test.Assert(t, err == nil)
ctx := context.Background()
req := new(MockTStruct)
res := new(MockTStruct)
err = cli.Call(ctx, mtd, req, res)
test.Assert(t, err == nil, err)
}