-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnaming_test.go
393 lines (377 loc) · 10.2 KB
/
naming_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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
//
// 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 naming
import (
"os"
"strings"
"testing"
"time"
_ "trpc.group/trpc-go/trpc-naming-polarismesh/registry"
"trpc.group/trpc-go/trpc-go"
"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
plog "github.com/polarismesh/polaris-go/pkg/log"
"github.com/polarismesh/polaris-go/plugin/loadbalancer/ringhash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestSelectorFactory_Setup(t *testing.T) {
cfgstr := `
plugins:
selector:
polarismesh:
address_list: 127.0.0.1:0
enable_servicerouter: true
persistDir: /tmp/polarismesh/backup
log_dir: /tmp/polarismesh/log
debug: false
discovery:
refresh_interval: 10000
`
cfg := trpc.Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
polarisCfg := cfg.Plugins["selector"]["polarismesh"]
pluginFac := &SelectorFactory{}
err = pluginFac.Setup("polarismesh", &polarisCfg)
assert.Nil(t, err)
assert.NotNil(t, pluginFac.GetSDKCtx())
}
func Test_SetupWithConfig(t *testing.T) {
var (
enableServiceRouter = true
persisDir = "/tmp/polarismesh/backup"
logDir = "/tmp/polarismesh/log"
)
require.Nil(t, SetupWithConfig(&Config{
AddressList: "127.0.0.1:0",
EnableServiceRouter: &enableServiceRouter,
PersistDir: &persisDir,
LogDir: &logDir,
Debug: false,
Discovery: DiscoveryConfig{RefreshInterval: 10000},
}))
}
func Test_SetupWithPolarisConfig(t *testing.T) {
var (
enableServiceRouter = true
persisDir = "/tmp/polarismesh/backup"
logDir = "/tmp/polarismesh/log/polaris_config"
address = "not_exist"
)
os.RemoveAll(logDir)
defer os.RemoveAll(logDir)
api.SetLoggersDir(logDir)
// 北极星配置文件
cfg := api.NewConfiguration()
addresses := []string{address}
cfg.GetGlobal().GetServerConnector().SetAddresses(addresses)
require.Nil(t, SetupWithConfig(&Config{
EnableServiceRouter: &enableServiceRouter,
PersistDir: &persisDir,
Debug: false,
Discovery: DiscoveryConfig{RefreshInterval: 10000},
PolarisConfig: cfg,
}))
// 日志异步书写
time.Sleep(3 * time.Second)
startLog, err := os.ReadFile(logDir + "/base/polaris.log")
assert.Equal(t, err, nil)
index := strings.Index(string(startLog), address)
assert.True(t, index >= 0)
}
func Test_newSDKContext(t *testing.T) {
cfgstr := `
address_list: 127.0.0.1:0
report_timeout: 1s
timeout: 1000
debug: false
persistDir: /tmp/polarismesh/backup
join_point: default
service_expire_time: 12h
connect_timeout:
message_timeout: 3s
bind_ip: 127.0.0.1
discovery:
refresh_interval: 10000
circuitbreaker:
checkPeriod: 10s
requestCountAfterHalfOpen: 20
successCountAfterHalfOpen: 18
sleepWindow: 25s
errorCount:
continuousErrorThreshold: 20
metricNumBuckets: 12
metricStatTimeWindow: 2m
errorRate:
metricStatTimeWindow: 1m
metricNumBuckets: 6
requestVolumeThreshold: 12
cluster_service:
discover: xxx.polaris
health_check: yyy.polaris
monitor: zzz.polaris
service_router:
nearby_matchlevel: region
percent_of_min_instances: 0.2
need_return_all_nodes: true
loadbalance:
name: [polaris_ring_hash]
details:
polaris_ring_hash:
vnodeCount: 1024
instance_location: # 注册实例的地址位置信息
region: China
zone: Guangdong
campus: Shenzhen
`
cfg := Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
sdkCtx, err := newSDKContext(&cfg)
assert.Nil(t, err)
// 检查熔断配置
circuitbreaker := sdkCtx.GetConfig().GetConsumer().GetCircuitBreaker()
assert.Equal(t, 10*time.Second, circuitbreaker.GetCheckPeriod())
assert.Equal(t, 20, circuitbreaker.GetRequestCountAfterHalfOpen())
assert.Equal(t, 18, circuitbreaker.GetSuccessCountAfterHalfOpen())
assert.Equal(t, 25*time.Second, circuitbreaker.GetSleepWindow())
errorCount := sdkCtx.GetConfig().GetConsumer().GetCircuitBreaker().GetErrorCountConfig()
assert.Equal(t, 20, errorCount.GetContinuousErrorThreshold())
assert.Equal(t, 12, errorCount.GetMetricNumBuckets())
assert.Equal(t, 2*time.Minute, errorCount.GetMetricStatTimeWindow())
errRate := sdkCtx.GetConfig().GetConsumer().GetCircuitBreaker().GetErrorRateConfig()
assert.Equal(t, 1*time.Minute, errRate.GetMetricStatTimeWindow())
assert.Equal(t, 6, errRate.GetMetricNumBuckets())
assert.Equal(t, 12, errRate.GetRequestVolumeThreshold())
// 检查LocalCache配置
discovery := sdkCtx.GetConfig().GetConsumer().GetLocalCache()
assert.Equal(t, 10000*time.Millisecond, discovery.GetServiceRefreshInterval())
assert.Equal(t, "/tmp/polarismesh/backup", discovery.GetPersistDir())
loadBalanceCfg, ok := sdkCtx.GetConfig().GetConsumer().GetLoadbalancer().
GetPluginConfig(config.DefaultLoadBalancerRingHash).(*ringhash.Config)
require.True(t, ok)
require.Equal(t, 1024, loadBalanceCfg.VnodeCount)
// 其它配置检查
global := sdkCtx.GetConfig().GetGlobal()
assert.Equal(t, 1000*time.Millisecond, global.GetAPI().GetTimeout())
assert.Equal(t, "xxx.polaris", global.GetSystem().GetDiscoverCluster().GetService())
assert.Equal(t, "yyy.polaris", global.GetSystem().GetHealthCheckCluster().GetService())
assert.Equal(t, "zzz.polaris", global.GetSystem().GetMonitorCluster().GetService())
// 检查服务路由配置
serviceRouter := sdkCtx.GetConfig().GetConsumer().GetServiceRouter()
assert.Equal(t, "region", serviceRouter.GetNearbyConfig().GetMatchLevel())
assert.Equal(t, 0.2, serviceRouter.GetPercentOfMinInstances())
reportTimeout := time.Second
assert.Equal(t, &reportTimeout, cfg.ReportTimeout)
// 检查客户端绑定IP配置
assert.Equal(t, "127.0.0.1", sdkCtx.GetConfig().GetGlobal().GetAPI().GetBindIP())
// 检查地域信息
assert.Equal(t, "China", sdkCtx.GetValueContext().GetCurrentLocation().GetLocation().Region)
assert.Equal(t, "Guangdong", sdkCtx.GetValueContext().GetCurrentLocation().GetLocation().Zone)
assert.Equal(t, "Shenzhen", sdkCtx.GetValueContext().GetCurrentLocation().GetLocation().Campus)
}
func Test_getLogLevel(t *testing.T) {
type args struct {
desc string
}
tests := []struct {
name string
args args
want int
}{
{
"convert to debug level",
args{"debug"},
plog.DebugLog,
},
{
"convert to info level",
args{"info"},
plog.InfoLog,
},
{
"convert to warn level",
args{"warn"},
plog.WarnLog,
},
{
"convert to error level",
args{"error"},
plog.ErrorLog,
},
{
"convert to fatal level",
args{"fatal"},
plog.FatalLog,
},
{
"convert to none level",
args{"none"},
plog.NoneLog,
},
{
"convert to default",
args{"default"},
plog.DefaultBaseLogLevel,
},
{
"convert to real log level - case insensitive",
args{"DEBUG"},
plog.DebugLog,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, getLogLevel(tt.args.desc))
})
}
}
func TestConfig_UnmarshalYAML(t *testing.T) {
String := func(v string) *string { return &v }
type args struct {
value *yaml.Node
}
tests := []struct {
name string
args args
wantErr bool
want *Config
}{
{
"unmarshal yaml - log_dir",
args{func() *yaml.Node {
cfgstr := `
plugins:
selector:
polarismesh:
address_list: 127.0.0.1:0
log_dir: /tmp/polarismesh/log
discovery:
refresh_interval: 10000
`
cfg := trpc.Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
polarisCfg := cfg.Plugins["selector"]["polarismesh"]
return &polarisCfg
}()},
false,
&Config{
LogDir: String("/tmp/polarismesh/log"),
Logs: &Logs{
DirPath: "/tmp/polarismesh/log",
Level: "default",
MaxBackups: plog.DefaultRotationMaxBackups,
MaxSize: plog.DefaultRotationMaxSize,
},
AddressList: "127.0.0.1:0",
Discovery: DiscoveryConfig{
RefreshInterval: 10000,
},
},
},
{
"unmarshal yaml - no log config",
args{func() *yaml.Node {
cfgstr := `
plugins:
selector:
polarismesh:
address_list: 127.0.0.1:0
discovery:
refresh_interval: 10000
`
cfg := trpc.Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
polarisCfg := cfg.Plugins["selector"]["polarismesh"]
return &polarisCfg
}()},
false,
&Config{
AddressList: "127.0.0.1:0",
Discovery: DiscoveryConfig{
RefreshInterval: 10000,
},
},
},
{
"unmarshal yaml - logs, with default value",
args{func() *yaml.Node {
cfgstr := `
plugins:
selector:
polarismesh:
address_list: 127.0.0.1:0
logs:
level: debug
discovery:
refresh_interval: 10000
`
cfg := trpc.Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
polarisCfg := cfg.Plugins["selector"]["polarismesh"]
return &polarisCfg
}()},
false,
&Config{
Logs: &Logs{
DirPath: plog.DefaultLogRotationRootDir,
Level: "debug",
MaxBackups: plog.DefaultRotationMaxBackups,
MaxSize: plog.DefaultRotationMaxSize,
},
AddressList: "127.0.0.1:0",
Discovery: DiscoveryConfig{
RefreshInterval: 10000,
},
},
},
{
"unmarshal yaml - no log config",
args{func() *yaml.Node {
cfgstr := `
plugins:
selector:
polarismesh:
address_list: 127.0.0.1:0
discovery:
refresh_interval: 10000
`
cfg := trpc.Config{}
err := yaml.Unmarshal([]byte(cfgstr), &cfg)
assert.Nil(t, err)
polarisCfg := cfg.Plugins["selector"]["polarismesh"]
return &polarisCfg
}()},
false,
&Config{
AddressList: "127.0.0.1:0",
Discovery: DiscoveryConfig{
RefreshInterval: 10000,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{}
err := c.UnmarshalYAML(tt.args.value)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.want, c)
})
}
}