grpc-go/service_config_test.go

324 строки
6.2 KiB
Go

/*
*
* 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 (
"reflect"
"testing"
"time"
)
func TestParseLoadBalancer(t *testing.T) {
testcases := []struct {
scjs string
wantSC ServiceConfig
wantErr bool
}{
{
`{
"loadBalancingPolicy": "round_robin",
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
ServiceConfig{
LB: newString("round_robin"),
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(true),
},
},
},
false,
},
{
`{
"loadBalancingPolicy": 1,
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": false
}
]
}`,
ServiceConfig{},
true,
},
}
for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
}
func TestParseWaitForReady(t *testing.T) {
testcases := []struct {
scjs string
wantSC ServiceConfig
wantErr bool
}{
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(true),
},
},
},
false,
},
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": false
}
]
}`,
ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
WaitForReady: newBool(false),
},
},
},
false,
},
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": fall
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"waitForReady": true
}
]
}`,
ServiceConfig{},
true,
},
}
for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
}
func TestPraseTimeOut(t *testing.T) {
testcases := []struct {
scjs string
wantSC ServiceConfig
wantErr bool
}{
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "1s"
}
]
}`,
ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
Timeout: newDuration(time.Second),
},
},
},
false,
},
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "3c"
}
]
}`,
ServiceConfig{},
true,
},
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "3c"
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"timeout": "1s"
}
]
}`,
ServiceConfig{},
true,
},
}
for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
}
func TestPraseMsgSize(t *testing.T) {
testcases := []struct {
scjs string
wantSC ServiceConfig
wantErr bool
}{
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": 1024,
"maxResponseMessageBytes": 2048
}
]
}`,
ServiceConfig{
Methods: map[string]MethodConfig{
"/foo/Bar": {
MaxReqSize: newInt(1024),
MaxRespSize: newInt(2048),
},
},
},
false,
},
{
`{
"methodConfig": [
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": "1024",
"maxResponseMessageBytes": "2048"
},
{
"name": [
{
"service": "foo",
"method": "Bar"
}
],
"maxRequestMessageBytes": 1024,
"maxResponseMessageBytes": 2048
}
]
}`,
ServiceConfig{},
true,
},
}
for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
}