snapshot of https://github.com/grpc/grpc-go.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
Some checks failed
codecov / upload (push) Failing after 13m59s
CodeQL / Analyze (push) Failing after 1s
Testing / vet-proto (push) Failing after 1s
Testing / tests (arm, latest) (push) Waiting to run
Testing / extras (latest) (push) Failing after 1s
Testing / static checks (latest-1) (push) Failing after 1s
Testing / tests (-race, latest) (push) Failing after 1s
Testing / tests (i386, latest) (push) Failing after 1s
Testing / tests (latest) (push) Failing after 1s
Testing / tests (latest-1) (push) Failing after 1s
Lock Threads / lock (push) Failing after 1s
govulncheck / govulncheck (.) (push) Failing after 1s
govulncheck / govulncheck (./cmd/protoc-gen-go-grpc) (push) Failing after 1s
govulncheck / govulncheck (./gcp/observability) (push) Failing after 1s
govulncheck / govulncheck (./security/advancedtls) (push) Failing after 1s
govulncheck / govulncheck (./stats/opencensus) (push) Failing after 1s
Stale bot / stale (push) Failing after 1s
This commit is contained in:
130
internal/serviceconfig/duration.go
Normal file
130
internal/serviceconfig/duration.go
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 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 serviceconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Duration defines JSON marshal and unmarshal methods to conform to the
|
||||
// protobuf JSON spec defined [here].
|
||||
//
|
||||
// [here]: https://protobuf.dev/reference/protobuf/google.protobuf/#duration
|
||||
type Duration time.Duration
|
||||
|
||||
func (d Duration) String() string {
|
||||
return fmt.Sprint(time.Duration(d))
|
||||
}
|
||||
|
||||
// MarshalJSON converts from d to a JSON string output.
|
||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
ns := time.Duration(d).Nanoseconds()
|
||||
sec := ns / int64(time.Second)
|
||||
ns = ns % int64(time.Second)
|
||||
|
||||
var sign string
|
||||
if sec < 0 || ns < 0 {
|
||||
sign, sec, ns = "-", -1*sec, -1*ns
|
||||
}
|
||||
|
||||
// Generated output always contains 0, 3, 6, or 9 fractional digits,
|
||||
// depending on required precision.
|
||||
str := fmt.Sprintf("%s%d.%09d", sign, sec, ns)
|
||||
str = strings.TrimSuffix(str, "000")
|
||||
str = strings.TrimSuffix(str, "000")
|
||||
str = strings.TrimSuffix(str, ".000")
|
||||
return []byte(fmt.Sprintf("\"%ss\"", str)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals b as a duration JSON string into d.
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.HasSuffix(s, "s") {
|
||||
return fmt.Errorf("malformed duration %q: missing seconds unit", s)
|
||||
}
|
||||
neg := false
|
||||
if s[0] == '-' {
|
||||
neg = true
|
||||
s = s[1:]
|
||||
}
|
||||
ss := strings.SplitN(s[:len(s)-1], ".", 3)
|
||||
if len(ss) > 2 {
|
||||
return fmt.Errorf("malformed duration %q: too many decimals", s)
|
||||
}
|
||||
// hasDigits is set if either the whole or fractional part of the number is
|
||||
// present, since both are optional but one is required.
|
||||
hasDigits := false
|
||||
var sec, ns int64
|
||||
if len(ss[0]) > 0 {
|
||||
var err error
|
||||
if sec, err = strconv.ParseInt(ss[0], 10, 64); err != nil {
|
||||
return fmt.Errorf("malformed duration %q: %v", s, err)
|
||||
}
|
||||
// Maximum seconds value per the durationpb spec.
|
||||
const maxProtoSeconds = 315_576_000_000
|
||||
if sec > maxProtoSeconds {
|
||||
return fmt.Errorf("out of range: %q", s)
|
||||
}
|
||||
hasDigits = true
|
||||
}
|
||||
if len(ss) == 2 && len(ss[1]) > 0 {
|
||||
if len(ss[1]) > 9 {
|
||||
return fmt.Errorf("malformed duration %q: too many digits after decimal", s)
|
||||
}
|
||||
var err error
|
||||
if ns, err = strconv.ParseInt(ss[1], 10, 64); err != nil {
|
||||
return fmt.Errorf("malformed duration %q: %v", s, err)
|
||||
}
|
||||
for i := 9; i > len(ss[1]); i-- {
|
||||
ns *= 10
|
||||
}
|
||||
hasDigits = true
|
||||
}
|
||||
if !hasDigits {
|
||||
return fmt.Errorf("malformed duration %q: contains no numbers", s)
|
||||
}
|
||||
|
||||
if neg {
|
||||
sec *= -1
|
||||
ns *= -1
|
||||
}
|
||||
|
||||
// Maximum/minimum seconds/nanoseconds representable by Go's time.Duration.
|
||||
const maxSeconds = math.MaxInt64 / int64(time.Second)
|
||||
const maxNanosAtMaxSeconds = math.MaxInt64 % int64(time.Second)
|
||||
const minSeconds = math.MinInt64 / int64(time.Second)
|
||||
const minNanosAtMinSeconds = math.MinInt64 % int64(time.Second)
|
||||
|
||||
if sec > maxSeconds || (sec == maxSeconds && ns >= maxNanosAtMaxSeconds) {
|
||||
*d = Duration(math.MaxInt64)
|
||||
} else if sec < minSeconds || (sec == minSeconds && ns <= minNanosAtMinSeconds) {
|
||||
*d = Duration(math.MinInt64)
|
||||
} else {
|
||||
*d = Duration(sec*int64(time.Second) + ns)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
86
internal/serviceconfig/duration_test.go
Normal file
86
internal/serviceconfig/duration_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2023 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 serviceconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
rand "math/rand/v2"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Tests both marshalling and unmarshalling of Durations.
|
||||
func TestDuration_MarshalUnmarshal(t *testing.T) {
|
||||
testCases := []struct {
|
||||
json string
|
||||
td time.Duration
|
||||
unmarshalErr error
|
||||
noMarshal bool
|
||||
}{
|
||||
// Basic values.
|
||||
{json: `"1s"`, td: time.Second},
|
||||
{json: `"-100.700s"`, td: -100*time.Second - 700*time.Millisecond},
|
||||
{json: `".050s"`, td: 50 * time.Millisecond, noMarshal: true},
|
||||
{json: `"-.001s"`, td: -1 * time.Millisecond, noMarshal: true},
|
||||
{json: `"-0.200s"`, td: -200 * time.Millisecond},
|
||||
// Positive near / out of bounds.
|
||||
{json: `"9223372036s"`, td: 9223372036 * time.Second},
|
||||
{json: `"9223372037s"`, td: math.MaxInt64, noMarshal: true},
|
||||
{json: `"9223372036.854775807s"`, td: math.MaxInt64},
|
||||
{json: `"9223372036.854775808s"`, td: math.MaxInt64, noMarshal: true},
|
||||
{json: `"315576000000s"`, td: math.MaxInt64, noMarshal: true},
|
||||
{json: `"315576000001s"`, unmarshalErr: fmt.Errorf("out of range")},
|
||||
// Negative near / out of bounds.
|
||||
{json: `"-9223372036s"`, td: -9223372036 * time.Second},
|
||||
{json: `"-9223372037s"`, td: math.MinInt64, noMarshal: true},
|
||||
{json: `"-9223372036.854775808s"`, td: math.MinInt64},
|
||||
{json: `"-9223372036.854775809s"`, td: math.MinInt64, noMarshal: true},
|
||||
{json: `"-315576000000s"`, td: math.MinInt64, noMarshal: true},
|
||||
{json: `"-315576000001s"`, unmarshalErr: fmt.Errorf("out of range")},
|
||||
// Parse errors.
|
||||
{json: `123s`, unmarshalErr: fmt.Errorf("invalid character")},
|
||||
{json: `"5m"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `"5.3.2s"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `"x.3s"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `"3.xs"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `"3.1234567890s"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `".s"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
{json: `"s"`, unmarshalErr: fmt.Errorf("malformed duration")},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
// Seed `got` with a random value to ensure we properly reset it in all
|
||||
// non-error cases.
|
||||
got := Duration(rand.Uint64())
|
||||
err := got.UnmarshalJSON([]byte(tc.json))
|
||||
if (err == nil && time.Duration(got) != tc.td) ||
|
||||
(err != nil) != (tc.unmarshalErr != nil) || !strings.Contains(fmt.Sprint(err), fmt.Sprint(tc.unmarshalErr)) {
|
||||
t.Errorf("UnmarshalJSON of %v = %v, %v; want %v, %v", tc.json, time.Duration(got), err, tc.td, tc.unmarshalErr)
|
||||
}
|
||||
|
||||
if tc.unmarshalErr == nil && !tc.noMarshal {
|
||||
d := Duration(tc.td)
|
||||
got, err := d.MarshalJSON()
|
||||
if string(got) != tc.json || err != nil {
|
||||
t.Errorf("MarshalJSON of %v = %v, %v; want %v, nil", d, string(got), err, tc.json)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
180
internal/serviceconfig/serviceconfig.go
Normal file
180
internal/serviceconfig/serviceconfig.go
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 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 serviceconfig contains utility functions to parse service config.
|
||||
package serviceconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/balancer"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
externalserviceconfig "google.golang.org/grpc/serviceconfig"
|
||||
)
|
||||
|
||||
var logger = grpclog.Component("core")
|
||||
|
||||
// BalancerConfig wraps the name and config associated with one load balancing
|
||||
// policy. It corresponds to a single entry of the loadBalancingConfig field
|
||||
// from ServiceConfig.
|
||||
//
|
||||
// It implements the json.Unmarshaler interface.
|
||||
//
|
||||
// https://github.com/grpc/grpc-proto/blob/54713b1e8bc6ed2d4f25fb4dff527842150b91b2/grpc/service_config/service_config.proto#L247
|
||||
type BalancerConfig struct {
|
||||
Name string
|
||||
Config externalserviceconfig.LoadBalancingConfig
|
||||
}
|
||||
|
||||
type intermediateBalancerConfig []map[string]json.RawMessage
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface.
|
||||
//
|
||||
// It marshals the balancer and config into a length-1 slice
|
||||
// ([]map[string]config).
|
||||
func (bc *BalancerConfig) MarshalJSON() ([]byte, error) {
|
||||
if bc.Config == nil {
|
||||
// If config is nil, return empty config `{}`.
|
||||
return []byte(fmt.Sprintf(`[{%q: %v}]`, bc.Name, "{}")), nil
|
||||
}
|
||||
c, err := json.Marshal(bc.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(fmt.Sprintf(`[{%q: %s}]`, bc.Name, c)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
//
|
||||
// ServiceConfig contains a list of loadBalancingConfigs, each with a name and
|
||||
// config. This method iterates through that list in order, and stops at the
|
||||
// first policy that is supported.
|
||||
// - If the config for the first supported policy is invalid, the whole service
|
||||
// config is invalid.
|
||||
// - If the list doesn't contain any supported policy, the whole service config
|
||||
// is invalid.
|
||||
func (bc *BalancerConfig) UnmarshalJSON(b []byte) error {
|
||||
var ir intermediateBalancerConfig
|
||||
err := json.Unmarshal(b, &ir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var names []string
|
||||
for i, lbcfg := range ir {
|
||||
if len(lbcfg) != 1 {
|
||||
return fmt.Errorf("invalid loadBalancingConfig: entry %v does not contain exactly 1 policy/config pair: %q", i, lbcfg)
|
||||
}
|
||||
|
||||
var (
|
||||
name string
|
||||
jsonCfg json.RawMessage
|
||||
)
|
||||
// Get the key:value pair from the map. We have already made sure that
|
||||
// the map contains a single entry.
|
||||
for name, jsonCfg = range lbcfg {
|
||||
}
|
||||
|
||||
names = append(names, name)
|
||||
builder := balancer.Get(name)
|
||||
if builder == nil {
|
||||
// If the balancer is not registered, move on to the next config.
|
||||
// This is not an error.
|
||||
continue
|
||||
}
|
||||
bc.Name = name
|
||||
|
||||
parser, ok := builder.(balancer.ConfigParser)
|
||||
if !ok {
|
||||
if string(jsonCfg) != "{}" {
|
||||
logger.Warningf("non-empty balancer configuration %q, but balancer does not implement ParseConfig", string(jsonCfg))
|
||||
}
|
||||
// Stop at this, though the builder doesn't support parsing config.
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := parser.ParseConfig(jsonCfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing loadBalancingConfig for policy %q: %v", name, err)
|
||||
}
|
||||
bc.Config = cfg
|
||||
return nil
|
||||
}
|
||||
// This is reached when the for loop iterates over all entries, but didn't
|
||||
// return. This means we had a loadBalancingConfig slice but did not
|
||||
// encounter a registered policy. The config is considered invalid in this
|
||||
// case.
|
||||
return fmt.Errorf("invalid loadBalancingConfig: no supported policies found in %v", names)
|
||||
}
|
||||
|
||||
// MethodConfig defines the configuration recommended by the service providers for a
|
||||
// particular method.
|
||||
type MethodConfig struct {
|
||||
// WaitForReady indicates whether RPCs sent to this method should wait until
|
||||
// the connection is ready by default (!failfast). The value specified via the
|
||||
// gRPC client API will override the value set here.
|
||||
WaitForReady *bool
|
||||
// Timeout is the default timeout for RPCs sent to this method. The actual
|
||||
// deadline used will be the minimum of the value specified here and the value
|
||||
// set by the application via the gRPC client API. If either one is not set,
|
||||
// then the other will be used. If neither is set, then the RPC has no deadline.
|
||||
Timeout *time.Duration
|
||||
// MaxReqSize is the maximum allowed payload size for an individual request in a
|
||||
// stream (client->server) in bytes. The size which is measured is the serialized
|
||||
// payload after per-message compression (but before stream compression) in bytes.
|
||||
// The actual value used is the minimum of the value specified here and the value set
|
||||
// by the application via the gRPC client API. If either one is not set, then the other
|
||||
// will be used. If neither is set, then the built-in default is used.
|
||||
MaxReqSize *int
|
||||
// MaxRespSize is the maximum allowed payload size for an individual response in a
|
||||
// stream (server->client) in bytes.
|
||||
MaxRespSize *int
|
||||
// RetryPolicy configures retry options for the method.
|
||||
RetryPolicy *RetryPolicy
|
||||
}
|
||||
|
||||
// RetryPolicy defines the go-native version of the retry policy defined by the
|
||||
// service config here:
|
||||
// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
|
||||
type RetryPolicy struct {
|
||||
// MaxAttempts is the maximum number of attempts, including the original RPC.
|
||||
//
|
||||
// This field is required and must be two or greater.
|
||||
MaxAttempts int
|
||||
|
||||
// Exponential backoff parameters. The initial retry attempt will occur at
|
||||
// random(0, initialBackoff). In general, the nth attempt will occur at
|
||||
// random(0,
|
||||
// min(initialBackoff*backoffMultiplier**(n-1), maxBackoff)).
|
||||
//
|
||||
// These fields are required and must be greater than zero.
|
||||
InitialBackoff time.Duration
|
||||
MaxBackoff time.Duration
|
||||
BackoffMultiplier float64
|
||||
|
||||
// The set of status codes which may be retried.
|
||||
//
|
||||
// Status codes are specified as strings, e.g., "UNAVAILABLE".
|
||||
//
|
||||
// This field is required and must be non-empty.
|
||||
// Note: a set is used to store this for easy lookup.
|
||||
RetryableStatusCodes map[codes.Code]bool
|
||||
}
|
||||
182
internal/serviceconfig/serviceconfig_test.go
Normal file
182
internal/serviceconfig/serviceconfig_test.go
Normal file
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2020 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 serviceconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"google.golang.org/grpc/balancer"
|
||||
externalserviceconfig "google.golang.org/grpc/serviceconfig"
|
||||
)
|
||||
|
||||
type testBalancerConfigType struct {
|
||||
externalserviceconfig.LoadBalancingConfig `json:"-"`
|
||||
|
||||
Check bool `json:"check"`
|
||||
}
|
||||
|
||||
var testBalancerConfig = testBalancerConfigType{Check: true}
|
||||
|
||||
const (
|
||||
testBalancerBuilderName = "test-bb"
|
||||
testBalancerBuilderNotParserName = "test-bb-not-parser"
|
||||
|
||||
testBalancerConfigJSON = `{"check":true}`
|
||||
)
|
||||
|
||||
type testBalancerBuilder struct {
|
||||
balancer.Builder
|
||||
}
|
||||
|
||||
func (testBalancerBuilder) ParseConfig(js json.RawMessage) (externalserviceconfig.LoadBalancingConfig, error) {
|
||||
if string(js) != testBalancerConfigJSON {
|
||||
return nil, fmt.Errorf("unexpected config json")
|
||||
}
|
||||
return testBalancerConfig, nil
|
||||
}
|
||||
|
||||
func (testBalancerBuilder) Name() string {
|
||||
return testBalancerBuilderName
|
||||
}
|
||||
|
||||
type testBalancerBuilderNotParser struct {
|
||||
balancer.Builder
|
||||
}
|
||||
|
||||
func (testBalancerBuilderNotParser) Name() string {
|
||||
return testBalancerBuilderNotParserName
|
||||
}
|
||||
|
||||
func init() {
|
||||
balancer.Register(testBalancerBuilder{})
|
||||
balancer.Register(testBalancerBuilderNotParser{})
|
||||
}
|
||||
|
||||
func TestBalancerConfigUnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
json string
|
||||
want BalancerConfig
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty json",
|
||||
json: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
// The config should be a slice of maps, but each map should have
|
||||
// exactly one entry.
|
||||
name: "more than one entry for a map",
|
||||
json: `[{"balancer1":"1","balancer2":"2"}]`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no balancer registered",
|
||||
json: `[{"balancer1":"1"},{"balancer2":"2"}]`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "OK",
|
||||
json: fmt.Sprintf("[{%q: %v}]", testBalancerBuilderName, testBalancerConfigJSON),
|
||||
want: BalancerConfig{
|
||||
Name: testBalancerBuilderName,
|
||||
Config: testBalancerConfig,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "first balancer not registered",
|
||||
json: fmt.Sprintf(`[{"balancer1":"1"},{%q: %v}]`, testBalancerBuilderName, testBalancerConfigJSON),
|
||||
want: BalancerConfig{
|
||||
Name: testBalancerBuilderName,
|
||||
Config: testBalancerConfig,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "balancer registered but builder not parser",
|
||||
json: fmt.Sprintf("[{%q: %v}]", testBalancerBuilderNotParserName, testBalancerConfigJSON),
|
||||
want: BalancerConfig{
|
||||
Name: testBalancerBuilderNotParserName,
|
||||
Config: nil,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var bc BalancerConfig
|
||||
if err := bc.UnmarshalJSON([]byte(tt.json)); (err != nil) != tt.wantErr {
|
||||
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if !cmp.Equal(bc, tt.want) {
|
||||
t.Errorf("diff: %v", cmp.Diff(bc, tt.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancerConfigMarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
bc BalancerConfig
|
||||
wantJSON string
|
||||
}{
|
||||
{
|
||||
name: "OK",
|
||||
bc: BalancerConfig{
|
||||
Name: testBalancerBuilderName,
|
||||
Config: testBalancerConfig,
|
||||
},
|
||||
wantJSON: `[{"test-bb": {"check":true}}]`,
|
||||
},
|
||||
{
|
||||
name: "OK config is nil",
|
||||
bc: BalancerConfig{
|
||||
Name: testBalancerBuilderNotParserName,
|
||||
Config: nil, // nil should be marshalled to an empty config "{}".
|
||||
},
|
||||
wantJSON: `[{"test-bb-not-parser": {}}]`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b, err := tt.bc.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
if str := string(b); str != tt.wantJSON {
|
||||
t.Fatalf("got str %q, want %q", str, tt.wantJSON)
|
||||
}
|
||||
|
||||
var bc BalancerConfig
|
||||
if err := bc.UnmarshalJSON(b); err != nil {
|
||||
t.Errorf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if !cmp.Equal(bc, tt.bc) {
|
||||
t.Errorf("diff: %v", cmp.Diff(bc, tt.bc))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user