-
Notifications
You must be signed in to change notification settings - Fork 94
/
trpc_test.go
286 lines (233 loc) · 7.62 KB
/
trpc_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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package trpc_test
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
trpc "trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/plugin"
"trpc.group/trpc-go/trpc-go/transport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
var ctx = context.Background()
func init() {
trpc.LoadGlobalConfig("testdata/trpc_go.yaml")
trpc.Setup(trpc.GlobalConfig())
}
// go test -v -coverprofile=cover.out
// go tool cover -func=cover.out
func TestCodec(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
val := trpc.GetMetaData(ctx, "no-exist")
assert.Nil(t, val)
trpc.SetMetaData(ctx, "exist1", []byte("value1"))
val = trpc.GetMetaData(ctx, "exist1")
assert.NotNil(t, val)
assert.Equal(t, []byte("value1"), val)
trpc.SetMetaData(ctx, "exist2", []byte("value2"))
val = trpc.GetMetaData(ctx, "exist2")
assert.NotNil(t, val)
assert.Equal(t, []byte("value2"), val)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
frameBuilder := transport.GetFramerBuilder("trpc")
assert.Equal(t, trpc.DefaultServerCodec, serverCodec)
assert.Equal(t, trpc.DefaultClientCodec, clientCodec)
request := trpc.Request(ctx)
response := trpc.Response(ctx)
assert.NotNil(t, request)
assert.NotNil(t, response)
msg = trpc.Message(ctx)
msg.WithServerReqHead(request)
msg.WithServerRspHead(response)
request = trpc.Request(ctx)
response = trpc.Response(ctx)
assert.NotNil(t, request)
assert.NotNil(t, response)
request.Func = []byte("test")
data, err := proto.Marshal(request)
assert.Nil(t, err)
assert.Equal(t, []byte{0x3a, 0x4, 0x74, 0x65, 0x73, 0x74}, data)
response.ErrorMsg = []byte("ok")
data, err = proto.Marshal(response)
assert.Nil(t, err)
assert.Equal(t, []byte{0x32, 0x2, 0x6f, 0x6b}, data)
// frame head: 2 bytes magic stx(0x930) + 1 byte stream type(1) + 1 byte stream frame type(2)
// + 4 bytes total len(23) + 2 bytes pb header len(6) + 4 bytes stream id(0)
// + 2 bytes reserved(0) + head + body
in := []byte{0x9, 0x30, 0, 2, 0, 0, 0, 23, 0, 6, 0, 0, 0, 0, 0, 0, 0x3a, 0x4, 0x74, 0x65, 0x73, 0x74, 1}
reader := bytes.NewReader(in)
frame := frameBuilder.New(reader)
data, err = frame.ReadFrame()
assert.Nil(t, err)
assert.Equal(t, in, data)
// invalid magic num
in1 := []byte{0x30, 0x9, 1, 2, 0, 0, 0, 23, 0, 6, 0, 0, 0, 0, 0, 0, 0x3a, 0x4, 0x74, 0x65, 0x73, 0x74, 1}
reader = bytes.NewReader(in1)
frame = frameBuilder.New(reader)
_, err = frame.ReadFrame()
assert.Contains(t, err.Error(), "not match")
msg = codec.Message(ctx)
reqBody, err := serverCodec.Decode(msg, in)
assert.Nil(t, err)
assert.Equal(t, []byte{1}, reqBody)
// head len invalid
in2 := []byte{0x30, 0x9, 0, 2, 0, 0, 0, 23, 0, 7, 0, 0, 0, 0, 0, 0, 0x3a, 0x4, 0x74, 0x65, 0x73, 0x74, 1}
reqBody2, err := serverCodec.Decode(msg, in2)
assert.NotNil(t, err)
assert.Nil(t, reqBody2)
rspBuf, err := serverCodec.Encode(msg, reqBody)
assert.Nil(t, err)
assert.NotNil(t, rspBuf)
reqBuf, err := clientCodec.Encode(msg, reqBody)
assert.Nil(t, err)
assert.NotNil(t, reqBuf)
in3 := []byte{0x9, 0x30, 0, 2, 0, 0, 0, 21, 0, 4, 0, 0, 0, 0, 0, 0, 0x32, 0x2, 0x6f, 0x6b, 1}
msg.ClientReqHead().(*trpcpb.RequestProtocol).RequestId = 0
rspBody, err := clientCodec.Decode(msg, in3)
assert.Nil(t, err)
assert.Equal(t, []byte{1}, rspBody)
}
func TestVersion(t *testing.T) {
version := trpc.Version()
assert.NotNil(t, version)
}
func TestConfig(t *testing.T) {
trpc.ServerConfigPath = "./testdata/trpc_go.yaml"
conf := trpc.GlobalConfig()
assert.NotNil(t, conf)
assert.Equal(t, 3, len(conf.Server.Service))
assert.Equal(t, "trpc.test.helloworld.Greeter1", conf.Server.Service[0].Name)
assert.Equal(t, true, *conf.Server.Service[0].ServerAsync)
assert.Equal(t, 1000, conf.Server.Service[1].MaxRoutines)
assert.Equal(t, false, *conf.Server.Service[0].Writev)
cfg := &trpc.Config{}
cfg.Server.Network = "tcp"
cfg.Server.Protocol = "trpc"
cfg.Client.Network = "tcp"
cfg.Client.Protocol = "trpc"
trpc.SetGlobalConfig(cfg)
}
func TestNewServer(t *testing.T) {
trpc.ServerConfigPath = "./testdata/trpc_go.yaml"
logger := log.NewZapLog(log.Config{
{
Writer: log.OutputFile,
WriteConfig: log.WriteConfig{
LogPath: os.TempDir(),
Filename: "trpc.log",
WriteMode: 1,
},
Level: "DEBUG",
},
})
dftLogger := log.DefaultLogger
log.SetLogger(logger)
defer log.SetLogger(dftLogger)
fp := filepath.Join(os.TempDir(), "trpc.log")
defer os.Remove(fp)
s := trpc.NewServer()
assert.NotNil(t, s)
assert.NotNil(t, s.Service("trpc.test.helloworld.Greeter1"))
assert.NotNil(t, s.Service("trpc.test.helloworld.Greeter2"))
assert.NotNil(t, s.Service("trpc.test.helloworld.Greeter3"))
assert.Equal(t, codec.DefaultReaderSize, codec.GetReaderSize())
buf, err := os.ReadFile(fp)
assert.Nil(t, err)
// test namingservice not exist
// registry set for service1、service2
assert.Contains(t, string(buf), "trpc.test.helloworld.Greeter1 registry not exist")
assert.Contains(t, string(buf), "trpc.test.helloworld.Greeter2 registry not exist")
// registry not set for service3
assert.NotContains(t, string(buf), "trpc.test.helloworld.Greeter3 registry not exist")
}
func TestProtocol(t *testing.T) {
request := trpc.Request(ctx)
response := trpc.Response(ctx)
assert.NotNil(t, request.String())
assert.NotNil(t, response.String())
assert.Equal(t, uint32(0), request.GetContentType())
assert.Equal(t, uint32(0), request.GetRequestId())
assert.Equal(t, uint32(0), request.GetCallType())
assert.Equal(t, uint32(0), request.GetVersion())
assert.Equal(t, uint32(0), request.GetMessageType())
assert.Nil(t, response.GetErrorMsg())
assert.Nil(t, request.GetCallee())
assert.Nil(t, request.GetCaller())
}
func TestGetAdminService(t *testing.T) {
cfg := t.TempDir() + "trpc_go.yaml"
require.Nil(t, os.WriteFile(cfg, []byte{}, 0644))
oldPath := trpc.ServerConfigPath
trpc.ServerConfigPath = cfg
defer func() { trpc.ServerConfigPath = oldPath }()
_ = trpc.NewServer()
admin, err := trpc.GetAdminService(trpc.NewServer())
require.Nil(t, err)
require.NotNil(t, admin)
require.Nil(t, os.WriteFile(cfg, []byte(`
server:
admin:
port: 9528
`), 0644))
s := trpc.NewServer()
adminService, err := trpc.GetAdminService(s)
require.Nil(t, err)
require.NotNil(t, adminService)
}
func TestNewServerWithClosablePlugin(t *testing.T) {
closed := make(chan struct{})
plugin.Register("default", &closablePlugin{onClose: func() error {
close(closed)
return nil
}})
cfg := t.TempDir() + "trpc_go.yaml"
require.Nil(t, os.WriteFile(cfg, []byte(`
plugins:
closable_plugin:
default:
`), 0644))
oldPath := trpc.ServerConfigPath
trpc.ServerConfigPath = cfg
defer func() { trpc.ServerConfigPath = oldPath }()
s := trpc.NewServer()
require.Nil(t, s.Close(nil))
select {
case <-closed:
default:
require.FailNow(t, "plugin is not closed when server close")
}
}
type closablePlugin struct {
onClose func() error
}
func (*closablePlugin) Type() string {
return "closable_plugin"
}
func (*closablePlugin) Setup(string, plugin.Decoder) error {
return nil
}
func (p *closablePlugin) Close() error {
return p.onClose()
}