-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathconfig_test.go
336 lines (300 loc) · 9.81 KB
/
config_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
//
//
// 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 client_test
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v3"
trpc "trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/client"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/filter"
"trpc.group/trpc-go/trpc-go/internal/rand"
"trpc.group/trpc-go/trpc-go/naming/registry"
"trpc.group/trpc-go/trpc-go/naming/selector"
"trpc.group/trpc-go/trpc-go/transport"
)
func TestConfigOptions(t *testing.T) {
clientOpts := &client.Options{}
transportOpts := &transport.RoundTripOptions{}
node := ®istry.Node{
Address: "127.0.0.1:8080",
Network: "udp",
Protocol: "trpc",
}
clientOpts.LoadNodeConfig(node)
for _, o := range clientOpts.CallOptions {
o(transportOpts)
}
assert.Equal(t, "127.0.0.1:8080", transportOpts.Address)
assert.Equal(t, "udp", transportOpts.Network)
assert.Equal(t, trpc.DefaultClientCodec, clientOpts.Codec)
filter.Register("Monitoring", filter.NoopServerFilter, filter.NoopClientFilter)
filter.Register("Authentication", filter.NoopServerFilter, filter.NoopClientFilter)
}
func TestConfigNoDiscovery(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Discovery: "no-exists",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring", "Authentication"},
}
err := client.RegisterClientConfig("trpc.test.nodiscovery", backconfig)
assert.NotNil(t, err)
}
func TestConfigNoServiceRouter(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
ServiceRouter: "no-exists",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring", "Authentication"},
}
err := client.RegisterClientConfig("trpc.test.noservicerouter", backconfig)
assert.NotNil(t, err)
}
func TestConfigNoBalance(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Loadbalance: "no-exists",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring", "Authentication"},
}
err := client.RegisterClientConfig("trpc.test.nobalance", backconfig)
assert.NotNil(t, err)
}
type testOptionsSelector struct {
f func(*selector.Options)
}
var testOptionsSelectorError = errors.New("test options selector error")
func (s *testOptionsSelector) Select(serviceName string, opt ...selector.Option) (*registry.Node, error) {
opts := &selector.Options{}
for _, o := range opt {
o(opts)
}
s.f(opts)
return nil, testOptionsSelectorError
}
func (s *testOptionsSelector) Report(node *registry.Node, cost time.Duration, err error) error {
return nil
}
func TestConfigCalleeMetadata(t *testing.T) {
ctx := context.Background()
reqBody := codec.Body{}
rspBody := codec.Body{}
calleeMetadata := map[string]string{
"key1": "val1",
"key2": "val2",
}
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
CalleeMetadata: calleeMetadata,
}
err := client.RegisterClientConfig("trpc.test.client.metadata", backconfig)
assert.Nil(t, err)
s := &testOptionsSelector{
f: func(opts *selector.Options) {
assert.Equal(t, calleeMetadata, opts.DestinationMetadata)
},
}
selector.Register("test-options-selector", s)
cli := client.New()
assert.Equal(t, cli, client.DefaultClient)
ctx, msg := codec.WithNewMessage(ctx)
msg.WithCalleeServiceName("trpc.test.client.metadata")
err = cli.Invoke(ctx, reqBody, rspBody,
client.WithTarget("test-options-selector://trpc.test.client.metadata"),
)
require.Contains(t, err.Error(), testOptionsSelectorError.Error())
}
func TestConfigNoBreaker(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Circuitbreaker: "no-exists",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring", "Authentication"},
}
err := client.RegisterClientConfig("trpc.test.nobreaker", backconfig)
assert.NotNil(t, err)
}
func TestConfigNoFilter(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring", "no-exists"},
}
err := client.RegisterClientConfig("trpc.test.nofilter", backconfig)
assert.NotNil(t, err)
}
func TestConfigFilter(t *testing.T) {
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld3", // backend service name
Namespace: "Development",
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Filter: []string{"Monitoring"},
}
filter.Register("Monitoring", nil, filter.NoopClientFilter)
err := client.RegisterClientConfig("trpc.test.filter", backconfig)
assert.Nil(t, err)
}
func TestLoadClientFilterConfigSelectorFilter(t *testing.T) {
const callee = "test_selector_filter"
require.Nil(t, client.RegisterClientConfig(callee, &client.BackendConfig{
Filter: []string{client.DefaultSelectorFilterName},
}))
}
func TestRegisterConfigParallel(t *testing.T) {
safeRand := rand.NewSafeRand(time.Now().UnixNano())
for i := 0; i < safeRand.Intn(100); i++ {
t.Run("Parallel", func(t *testing.T) {
t.Parallel()
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld1", // backend service name
Target: "ip://1.1.1.1:2222", // backend address
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
}
conf := map[string]*client.BackendConfig{
"trpc.test.helloworld": backconfig,
}
err := client.RegisterConfig(conf)
assert.Nil(t, err)
assert.Equal(t, client.DefaultClientConfig(), conf)
})
}
}
func TestLoadClientConfig(t *testing.T) {
err := client.LoadClientConfig("../testdata/trpc_go.yaml")
assert.Nil(t, err)
}
type testTransport struct{}
func (t *testTransport) RoundTrip(ctx context.Context, req []byte,
opts ...transport.RoundTripOption) ([]byte, error) {
return nil, nil
}
func TestConfigTransport(t *testing.T) {
t.Run("Client Config", func(t *testing.T) {
tr := &testTransport{}
transport.RegisterClientTransport("test-transport", tr)
var cfg client.BackendConfig
require.Nil(t, yaml.Unmarshal([]byte(`
transport: test-transport
`), &cfg))
require.Equal(t, "test-transport", cfg.Transport)
require.Nil(t, client.RegisterClientConfig("trpc.test.hello", &cfg))
})
}
func TestConfigStreamFilter(t *testing.T) {
filterName := "sf1"
cfg := &client.BackendConfig{}
require.Nil(t, yaml.Unmarshal([]byte(`
stream_filter:
- sf1
`), cfg))
require.Equal(t, filterName, cfg.StreamFilter[0])
// return error if stream filter no registered
err := client.RegisterClientConfig("trpc.test.hello", cfg)
assert.NotNil(t, err)
client.RegisterStreamFilter("sf1", func(ctx context.Context, desc *client.ClientStreamDesc,
streamer client.Streamer) (client.ClientStream, error) {
return nil, nil
})
require.Nil(t, client.RegisterClientConfig("trpc.test.hello", cfg))
}
func TestConfig(t *testing.T) {
require.Nil(t, client.RegisterConfig(make(map[string]*client.BackendConfig)))
c := client.Config("empty")
assert.Equal(t, "", c.ServiceName)
assert.Equal(t, "tcp", c.Network)
assert.Equal(t, "trpc", c.Protocol)
backconfig := &client.BackendConfig{
ServiceName: "trpc.test.helloworld1", // backend service name
Target: "ip://1.1.1.1:2222", // backend address
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
}
conf := map[string]*client.BackendConfig{
"trpc.test.helloworld": backconfig,
}
err := client.RegisterConfig(conf)
assert.Nil(t, err)
assert.Equal(t, client.DefaultClientConfig(), conf)
require.Nil(t, client.RegisterClientConfig("trpc.test.helloworld2", backconfig))
assert.Equal(t, "trpc.test.helloworld1", client.Config("trpc.test.helloworld2").ServiceName)
c = client.Config("no-exist")
assert.Equal(t, "", c.ServiceName)
assert.Equal(t, "tcp", c.Network)
assert.Equal(t, "trpc", c.Protocol)
c = client.Config("trpc.test.helloworld")
assert.Equal(t, "trpc.test.helloworld1", c.ServiceName)
assert.Equal(t, "tcp", c.Network)
assert.Equal(t, "ip://1.1.1.1:2222", c.Target)
assert.Equal(t, 1000, c.Timeout)
assert.Equal(t, "trpc", c.Protocol)
backconfig = &client.BackendConfig{
ServiceName: "trpc.test.helloworld1", // backend service name
Network: "tcp",
Timeout: 1000,
Protocol: "trpc",
Compression: 1,
Password: "xxx",
CACert: "xxx",
}
require.Nil(t, client.RegisterClientConfig("trpc.test.helloworld3", backconfig))
}
func TestRegisterWildcardClient(t *testing.T) {
cfg := client.Config("*")
t.Cleanup(func() {
client.RegisterClientConfig("*", cfg)
})
client.RegisterClientConfig("*", &client.BackendConfig{
DisableServiceRouter: true,
})
ch := make(chan *client.Options, 1)
c := client.New()
ctx, _ := codec.EnsureMessage(context.Background())
require.Nil(t, c.Invoke(ctx, nil, nil, client.WithFilter(
func(ctx context.Context, _, _ interface{}, _ filter.ClientHandleFunc) error {
ch <- client.OptionsFromContext(ctx)
// Skip next.
return nil
})))
opts := <-ch
require.True(t, opts.DisableServiceRouter)
}