5 Star 11 Fork 2

Gitee 极速下载/grpc-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grpc/grpc-go
克隆/下载
service_config_test.go 12.12 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
/*
*
* Copyright 2017 gRPC 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 grpc
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/balancer/gracefulswitch"
"google.golang.org/grpc/serviceconfig"
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
)
type parseTestCase struct {
name string
scjs string
wantSC *ServiceConfig
wantErr bool
}
func lbConfigFor(t *testing.T, name string, cfg serviceconfig.LoadBalancingConfig) serviceconfig.LoadBalancingConfig {
if name == "" {
name = "pick_first"
cfg = struct {
serviceconfig.LoadBalancingConfig
}{}
}
d := []map[string]any{{name: cfg}}
strCfg, err := json.Marshal(d)
t.Logf("strCfg = %v", string(strCfg))
if err != nil {
t.Fatalf("Error parsing config: %v", err)
}
parsedCfg, err := gracefulswitch.ParseConfig(strCfg)
if err != nil {
t.Fatalf("Error parsing config: %v", err)
}
return parsedCfg
}
func runParseTests(t *testing.T, testCases []parseTestCase) {
t.Helper()
for i, c := range testCases {
name := c.name
if name == "" {
name = fmt.Sprint(i)
}
t.Run(name, func(t *testing.T) {
scpr := parseServiceConfig(c.scjs, defaultMaxCallAttempts)
var sc *ServiceConfig
sc, _ = scpr.Config.(*ServiceConfig)
if !c.wantErr {
c.wantSC.rawJSONString = c.scjs
}
if c.wantErr != (scpr.Err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, scpr.Err, c.wantSC, c.wantErr)
}
})
}
}
type pbbData struct {
serviceconfig.LoadBalancingConfig
Foo string
Bar int
}
type parseBalancerBuilder struct{}
func (parseBalancerBuilder) Name() string {
return "pbb"
}
func (parseBalancerBuilder) ParseConfig(c json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
d := pbbData{}
if err := json.Unmarshal(c, &d); err != nil {
return nil, err
}
return d, nil
}
func (parseBalancerBuilder) Build(balancer.ClientConn, balancer.BuildOptions) balancer.Balancer {
panic("unimplemented")
}
func init() {
balancer.Register(parseBalancerBuilder{})
}
func (s) TestParseLBConfig(t *testing.T) {
testcases := []parseTestCase{
{
scjs: `{
"loadBalancingConfig": [{"pbb": { "foo": "hi" } }]
}`,
wantSC: &ServiceConfig{
Methods: make(map[string]MethodConfig),
lbConfig: lbConfigFor(t, "pbb", pbbData{Foo: "hi"}),
},
wantErr: false,
},
}
runParseTests(t, testcases)
}
func (s) TestParseNoLBConfigSupported(t *testing.T) {
// We have a loadBalancingConfig field but will not encounter a supported
// policy. The config will be considered invalid in this case.
testcases := []parseTestCase{
{
scjs: `{
"loadBalancingConfig": [{"not_a_balancer1": {} }, {"not_a_balancer2": {}}]
}`,
wantErr: true,
}, {
scjs: `{"loadBalancingConfig": []}`,
wantErr: true,
},
}
runParseTests(t, testcases)
}
func (s) TestParseLoadBalancer(t *testing.T) {
testcases := []parseTestCase{
{
scjs: `{
"loadBalancingPolicy": "round_robin",
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(true),
},
},
lbConfig: lbConfigFor(t, "round_robin", nil),
},
wantErr: false,
},
{
scjs: `{
"loadBalancingPolicy": 1,
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": false
}
]
}`,
wantErr: true,
},
}
runParseTests(t, testcases)
}
func (s) TestParseWaitForReady(t *testing.T) {
testcases := []parseTestCase{
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(true),
},
},
lbConfig: lbConfigFor(t, "", nil),
},
},
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": false
}
]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(false),
},
},
lbConfig: lbConfigFor(t, "", nil),
},
},
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": fall
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
wantErr: true,
},
}
runParseTests(t, testcases)
}
func (s) TestParseTimeOut(t *testing.T) {
testcases := []parseTestCase{
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "1s"
}
]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
Timeout: newDuration(time.Second),
},
},
lbConfig: lbConfigFor(t, "", nil),
},
},
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "3c"
}
]
}`,
wantErr: true,
},
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "3c"
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "1s"
}
]
}`,
wantErr: true,
},
}
runParseTests(t, testcases)
}
func (s) TestParseMsgSize(t *testing.T) {
testcases := []parseTestCase{
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": 1024,
"maxResponseMessageBytes": 2048
}
]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
MaxReqSize: newInt(1024),
MaxRespSize: newInt(2048),
},
},
lbConfig: lbConfigFor(t, "", nil),
},
},
{
scjs: `{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": "1024",
"maxResponseMessageBytes": "2048"
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": 1024,
"maxResponseMessageBytes": 2048
}
]
}`,
wantErr: true,
},
}
runParseTests(t, testcases)
}
func (s) TestParseDefaultMethodConfig(t *testing.T) {
dc := &ServiceConfig{
Methods: map[string]MethodConfig{
"": {WaitForReady: newBool(true)},
},
lbConfig: lbConfigFor(t, "", nil),
}
runParseTests(t, []parseTestCase{
{
scjs: `{
"methodConfig": [{
"name": [{}],
"waitForReady": true
}]
}`,
wantSC: dc,
},
{
scjs: `{
"methodConfig": [{
"name": [{"service": null}],
"waitForReady": true
}]
}`,
wantSC: dc,
},
{
scjs: `{
"methodConfig": [{
"name": [{"service": ""}],
"waitForReady": true
}]
}`,
wantSC: dc,
},
{
scjs: `{
"methodConfig": [{
"name": [{"method": "Bar"}],
"waitForReady": true
}]
}`,
wantErr: true,
},
{
scjs: `{
"methodConfig": [{
"name": [{"service": "", "method": "Bar"}],
"waitForReady": true
}]
}`,
wantErr: true,
},
})
}
func (s) TestParseMethodConfigDuplicatedName(t *testing.T) {
runParseTests(t, []parseTestCase{
{
scjs: `{
"methodConfig": [{
"name": [
{"service": "foo"},
{"service": "foo"}
],
"waitForReady": true
}]
}`,
wantErr: true,
},
})
}
func (s) TestParseRetryPolicy(t *testing.T) {
runParseTests(t, []parseTestCase{
{
name: "valid",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": 2,
"initialBackoff": "2s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantSC: &ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/": {
RetryPolicy: &internalserviceconfig.RetryPolicy{
MaxAttempts: 2,
InitialBackoff: 2 * time.Second,
MaxBackoff: 10 * time.Second,
BackoffMultiplier: 2,
RetryableStatusCodes: map[codes.Code]bool{codes.Unavailable: true},
},
},
},
lbConfig: lbConfigFor(t, "", nil),
},
},
{
name: "negative maxAttempts",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": -1,
"initialBackoff": "2s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantErr: true,
},
{
name: "missing maxAttempts",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"initialBackoff": "2s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantErr: true,
},
{
name: "zero initialBackoff",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": 2,
"initialBackoff": "0s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantErr: true,
},
{
name: "zero maxBackoff",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": 2,
"initialBackoff": "2s",
"maxBackoff": "0s",
"backoffMultiplier": 2,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantErr: true,
},
{
name: "zero backoffMultiplier",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": 2,
"initialBackoff": "2s",
"maxBackoff": "10s",
"backoffMultiplier": 0,
"retryableStatusCodes": ["UNAVAILABLE"]
}
}]
}`,
wantErr: true,
},
{
name: "no retryable codes",
scjs: `{
"methodConfig": [{
"name": [{"service": "foo"}],
"retryPolicy": {
"maxAttempts": 2,
"initialBackoff": "2s",
"maxBackoff": "10s",
"backoffMultiplier": 2,
"retryableStatusCodes": []
}
}]
}`,
wantErr: true,
},
})
}
func newBool(b bool) *bool {
return &b
}
func newDuration(b time.Duration) *time.Duration {
return &b
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/grpc-go.git
git@gitee.com:mirrors/grpc-go.git
mirrors
grpc-go
grpc-go
master

搜索帮助