test(selector): improve coverage (#2320)

* test(selector): improve coverage

* style: gofumpt
pull/2326/head
川桑 2 years ago committed by GitHub
parent b354f185c0
commit 361391732b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 116
      selector/selector_test.go

@ -12,6 +12,8 @@ import (
"github.com/go-kratos/kratos/v2/registry"
)
var errNodeNotMatch = errors.New("node is not match")
type mockWeightedNode struct {
Node
@ -80,6 +82,18 @@ func (b *mockBalancer) Pick(ctx context.Context, nodes []WeightedNode) (selected
return
}
type mockMustErrorBalancerBuilder struct{}
func (b *mockMustErrorBalancerBuilder) Build() Balancer {
return &mockMustErrorBalancer{}
}
type mockMustErrorBalancer struct{}
func (b *mockMustErrorBalancer) Pick(ctx context.Context, nodes []WeightedNode) (selected WeightedNode, done DoneFunc, err error) {
return nil, nil, errNodeNotMatch
}
func TestDefault(t *testing.T) {
builder := DefaultBuilder{
Node: &mockWeightedNodeBuilder{},
@ -107,6 +121,7 @@ func TestDefault(t *testing.T) {
Endpoints: []string{"http://127.0.0.1:9090"},
Metadata: map[string]string{"weight": "10"},
}))
selector.Apply(nodes)
n, done, err := selector.Select(context.Background(), WithNodeFilter(mockFilter("v2.0.0")))
if err != nil {
@ -121,6 +136,9 @@ func TestDefault(t *testing.T) {
if !reflect.DeepEqual("v2.0.0", n.Version()) {
t.Errorf("expect %v, got %v", "v2.0.0", n.Version())
}
if n.Scheme() == "" {
t.Errorf("expect %v, got %v", "", n.Scheme())
}
if n.Address() == "" {
t.Errorf("expect %v, got %v", "", n.Address())
}
@ -135,6 +153,21 @@ func TestDefault(t *testing.T) {
}
done(context.Background(), DoneInfo{})
// peer in ctx
ctx := NewPeerContext(context.Background(), &Peer{
Node: mockWeightedNode{},
})
n, done, err = selector.Select(ctx)
if err != nil {
t.Errorf("expect %v, got %v", ErrNoAvailable, err)
}
if done == nil {
t.Errorf("expect %v, got %v", nil, done)
}
if n == nil {
t.Errorf("expect %v, got %v", nil, n)
}
// no v3.0.0 instance
n, done, err = selector.Select(context.Background(), WithNodeFilter(mockFilter("v3.0.0")))
if !errors.Is(ErrNoAvailable, err) {
@ -172,4 +205,87 @@ func TestDefault(t *testing.T) {
if n != nil {
t.Errorf("expect %v, got %v", nil, n)
}
// without node_filters
n, done, err = selector.Select(context.Background())
if !errors.Is(ErrNoAvailable, err) {
t.Errorf("expect %v, got %v", ErrNoAvailable, err)
}
if done != nil {
t.Errorf("expect %v, got %v", nil, done)
}
if n != nil {
t.Errorf("expect %v, got %v", nil, n)
}
}
func TestWithoutApply(t *testing.T) {
builder := DefaultBuilder{
Node: &mockWeightedNodeBuilder{},
Balancer: &mockBalancerBuilder{},
}
selector := builder.Build()
n, done, err := selector.Select(context.Background())
if !errors.Is(ErrNoAvailable, err) {
t.Errorf("expect %v, got %v", ErrNoAvailable, err)
}
if done != nil {
t.Errorf("expect %v, got %v", nil, done)
}
if n != nil {
t.Errorf("expect %v, got %v", nil, n)
}
}
func TestNoPick(t *testing.T) {
builder := DefaultBuilder{
Node: &mockWeightedNodeBuilder{},
Balancer: &mockMustErrorBalancerBuilder{},
}
var nodes []Node
nodes = append(nodes, NewNode(
"http",
"127.0.0.1:8080",
&registry.ServiceInstance{
ID: "127.0.0.1:8080",
Name: "helloworld",
Version: "v2.0.0",
Endpoints: []string{"http://127.0.0.1:8080"},
Metadata: map[string]string{"weight": "10"},
}))
nodes = append(nodes, NewNode(
"http",
"127.0.0.1:9090",
&registry.ServiceInstance{
ID: "127.0.0.1:9090",
Name: "helloworld",
Version: "v1.0.0",
Endpoints: []string{"http://127.0.0.1:9090"},
Metadata: map[string]string{"weight": "10"},
}))
selector := builder.Build()
selector.Apply(nodes)
n, done, err := selector.Select(context.Background())
if !errors.Is(errNodeNotMatch, err) {
t.Errorf("expect %v, got %v", errNodeNotMatch, err)
}
if done != nil {
t.Errorf("expect %v, got %v", nil, done)
}
if n != nil {
t.Errorf("expect %v, got %v", nil, n)
}
}
func TestGolobal(t *testing.T) {
builder := DefaultBuilder{
Node: &mockWeightedNodeBuilder{},
Balancer: &mockBalancerBuilder{},
}
SetGlobalSelector(&builder)
gBuilder := GlobalSelector()
if gBuilder == nil {
t.Errorf("expect %v, got %v", nil, gBuilder)
}
}

Loading…
Cancel
Save