From a515e388e6c58b79d268bf16aa5a722067c57e02 Mon Sep 17 00:00:00 2001 From: whalecold Date: Fri, 8 Mar 2024 16:59:06 +0800 Subject: [PATCH] add uint test --- core/manager/manager.go | 2 ++ core/manager/manager_test.go | 53 ++++++++++++++++++++++++++++++++++++ core/xdsresource/cds_test.go | 4 +++ core/xdsresource/testutil.go | 19 ++++++++++++- xdssuite/option.go | 22 +++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) diff --git a/core/manager/manager.go b/core/manager/manager.go index 5eed8fe..334815b 100644 --- a/core/manager/manager.go +++ b/core/manager/manager.go @@ -120,6 +120,8 @@ func (m *xdsResourceManager) getFromCache(rType xdsresource.ResourceType, rName return nil, false } +// RegisterCircuitBreaker registers the circuit breaker handler to resourceManager. The config stores in ClusterType xDS resource, +// If the config changed, manager will invoke the handler. func (m *xdsResourceManager) RegisterCircuitBreaker(handler xdsresource.UpdateCircuitbreakCallback) { m.mu.Lock() defer m.mu.Unlock() diff --git a/core/manager/manager_test.go b/core/manager/manager_test.go index 5e496a4..f25d914 100644 --- a/core/manager/manager_test.go +++ b/core/manager/manager_test.go @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/cloudwego/kitex/pkg/circuitbreak" "github.com/stretchr/testify/assert" "github.com/kitex-contrib/xds/core/manager/mock" @@ -308,3 +309,55 @@ func Test_xdsResourceManager_ConcurrentGet(t *testing.T) { } wg.Wait() } + +func TestRegisterCircuitBreaker(t *testing.T) { + m := &xdsResourceManager{ + cache: map[xdsresource.ResourceType]map[string]xdsresource.Resource{ + xdsresource.ClusterType: { + xdsresource.ClusterName1: &xdsresource.ClusterResource{ + OutlierDetection: &xdsresource.OutlierDetection{ + FailurePercentageThreshold: 10, + FailurePercentageRequestVolume: 1001, + }, + }, + xdsresource.ClusterName2: &xdsresource.ClusterResource{ + OutlierDetection: &xdsresource.OutlierDetection{ + FailurePercentageThreshold: 10, + FailurePercentageRequestVolume: 0, + }, + }, + }, + }, + meta: map[xdsresource.ResourceType]map[string]*xdsresource.ResourceMeta{}, + } + + policies := make(map[string]circuitbreak.CBConfig) + var updater = func(configs map[string]circuitbreak.CBConfig) { + policies = configs + } + m.RegisterCircuitBreaker(updater) + assert.Equal(t, policies, map[string]circuitbreak.CBConfig{ + "cluster1": { + Enable: true, + ErrRate: 0.1, + MinSample: 1001, + }, + "cluster2": {}, + }) + + m.UpdateResource(xdsresource.ClusterType, map[string]xdsresource.Resource{ + xdsresource.ClusterName1: &xdsresource.ClusterResource{ + OutlierDetection: &xdsresource.OutlierDetection{ + FailurePercentageThreshold: 1, + FailurePercentageRequestVolume: 100, + }, + }, + }, "latest") + assert.Equal(t, policies, map[string]circuitbreak.CBConfig{ + "cluster1": { + Enable: true, + ErrRate: 0.01, + MinSample: 100, + }, + }) +} diff --git a/core/xdsresource/cds_test.go b/core/xdsresource/cds_test.go index d96441f..c76c94a 100644 --- a/core/xdsresource/cds_test.go +++ b/core/xdsresource/cds_test.go @@ -72,5 +72,9 @@ func TestUnmarshalCDSSuccess(t *testing.T) { assert.Equal(t, cluster.EndpointName, EndpointName1) assert.Equal(t, cluster.DiscoveryType, ClusterDiscoveryTypeEDS) assert.Equal(t, cluster.LbPolicy, ClusterLbRoundRobin) + assert.Equal(t, cluster.OutlierDetection, &OutlierDetection{ + FailurePercentageThreshold: 10, + FailurePercentageRequestVolume: 100, + }) assert.Nil(t, cluster.InlineEndpoints) } diff --git a/core/xdsresource/testutil.go b/core/xdsresource/testutil.go index b5954de..ad576e6 100644 --- a/core/xdsresource/testutil.go +++ b/core/xdsresource/testutil.go @@ -26,6 +26,7 @@ import ( v3thrift_proxy "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/thrift_proxy/v3" v3matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3" "github.com/golang/protobuf/ptypes/any" + "github.com/golang/protobuf/ptypes/wrappers" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/wrapperspb" @@ -289,7 +290,15 @@ var ( EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{ ServiceName: EndpointName1, }, - LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN, + LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN, + OutlierDetection: &v3clusterpb.OutlierDetection{ + FailurePercentageThreshold: &wrappers.UInt32Value{ + Value: 10, + }, + FailurePercentageRequestVolume: &wrappers.UInt32Value{ + Value: 100, + }, + }, LoadAssignment: nil, } Cluster2 = &v3clusterpb.Cluster{ @@ -298,6 +307,14 @@ var ( EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{ ServiceName: EndpointName1, }, + OutlierDetection: &v3clusterpb.OutlierDetection{ + FailurePercentageThreshold: &wrappers.UInt32Value{ + Value: 10, + }, + FailurePercentageRequestVolume: &wrappers.UInt32Value{ + Value: 0, + }, + }, LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN, LoadAssignment: nil, } diff --git a/xdssuite/option.go b/xdssuite/option.go index ee40e08..ceefdc1 100644 --- a/xdssuite/option.go +++ b/xdssuite/option.go @@ -20,6 +20,8 @@ import ( "context" "github.com/bytedance/gopkg/cloud/metainfo" + "github.com/cloudwego/kitex/client" + "github.com/cloudwego/kitex/pkg/xds" ) type routerMetaExtractor func(context.Context) map[string]string @@ -57,3 +59,23 @@ func WithRouterMetaExtractor(routerMetaExtractor routerMetaExtractor) Option { }, } } + +type clientSuite struct { + cOpts []client.Option +} + +func (c *clientSuite) Options() []client.Option { + return c.cOpts +} + +// NewClientSuite client suite for xds handler +func NewClientSuite(opts ...Option) *clientSuite { + cOpts := []client.Option{ + NewCircuitBreaker(), + client.WithXDSSuite(xds.ClientSuite{ + RouterMiddleware: NewXDSRouterMiddleware(opts...), + Resolver: NewXDSResolver(), + }), + } + return &clientSuite{cOpts} +}