From 361391732b93dcad750177d90557f69dd94fd431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=9D=E6=A1=91?= <1554694323@qq.com> Date: Mon, 29 Aug 2022 09:27:44 +0800 Subject: [PATCH] test(selector): improve coverage (#2320) * test(selector): improve coverage * style: gofumpt --- selector/selector_test.go | 116 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/selector/selector_test.go b/selector/selector_test.go index f7c1f1bcf..bcc08264b 100644 --- a/selector/selector_test.go +++ b/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", + ®istry.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", + ®istry.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) + } }