mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-14 02:33:03 +02:00
test(selector): improve coverage (#2320)
* test(selector): improve coverage * style: gofumpt
This commit is contained in:
parent
b354f185c0
commit
361391732b
@ -12,6 +12,8 @@ import (
|
|||||||
"github.com/go-kratos/kratos/v2/registry"
|
"github.com/go-kratos/kratos/v2/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errNodeNotMatch = errors.New("node is not match")
|
||||||
|
|
||||||
type mockWeightedNode struct {
|
type mockWeightedNode struct {
|
||||||
Node
|
Node
|
||||||
|
|
||||||
@ -80,6 +82,18 @@ func (b *mockBalancer) Pick(ctx context.Context, nodes []WeightedNode) (selected
|
|||||||
return
|
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) {
|
func TestDefault(t *testing.T) {
|
||||||
builder := DefaultBuilder{
|
builder := DefaultBuilder{
|
||||||
Node: &mockWeightedNodeBuilder{},
|
Node: &mockWeightedNodeBuilder{},
|
||||||
@ -107,6 +121,7 @@ func TestDefault(t *testing.T) {
|
|||||||
Endpoints: []string{"http://127.0.0.1:9090"},
|
Endpoints: []string{"http://127.0.0.1:9090"},
|
||||||
Metadata: map[string]string{"weight": "10"},
|
Metadata: map[string]string{"weight": "10"},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
selector.Apply(nodes)
|
selector.Apply(nodes)
|
||||||
n, done, err := selector.Select(context.Background(), WithNodeFilter(mockFilter("v2.0.0")))
|
n, done, err := selector.Select(context.Background(), WithNodeFilter(mockFilter("v2.0.0")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -121,6 +136,9 @@ func TestDefault(t *testing.T) {
|
|||||||
if !reflect.DeepEqual("v2.0.0", n.Version()) {
|
if !reflect.DeepEqual("v2.0.0", n.Version()) {
|
||||||
t.Errorf("expect %v, got %v", "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() == "" {
|
if n.Address() == "" {
|
||||||
t.Errorf("expect %v, got %v", "", n.Address())
|
t.Errorf("expect %v, got %v", "", n.Address())
|
||||||
}
|
}
|
||||||
@ -135,6 +153,21 @@ func TestDefault(t *testing.T) {
|
|||||||
}
|
}
|
||||||
done(context.Background(), DoneInfo{})
|
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
|
// no v3.0.0 instance
|
||||||
n, done, err = selector.Select(context.Background(), WithNodeFilter(mockFilter("v3.0.0")))
|
n, done, err = selector.Select(context.Background(), WithNodeFilter(mockFilter("v3.0.0")))
|
||||||
if !errors.Is(ErrNoAvailable, err) {
|
if !errors.Is(ErrNoAvailable, err) {
|
||||||
@ -172,4 +205,87 @@ func TestDefault(t *testing.T) {
|
|||||||
if n != nil {
|
if n != nil {
|
||||||
t.Errorf("expect %v, got %v", nil, n)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user