Skip to content

Commit

Permalink
add uint test
Browse files Browse the repository at this point in the history
  • Loading branch information
whalecold committed Mar 8, 2024
1 parent d03c4d9 commit a515e38
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
53 changes: 53 additions & 0 deletions core/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {

Check failure on line 335 in core/manager/manager_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
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,
},
})
}
4 changes: 4 additions & 0 deletions core/xdsresource/cds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 18 additions & 1 deletion core/xdsresource/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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,
}
Expand Down
22 changes: 22 additions & 0 deletions xdssuite/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
}

0 comments on commit a515e38

Please sign in to comment.