Use ErrNoneAvailable and test blacklisting
This commit is contained in:
parent
63c6e82192
commit
75f2706fd0
2
selector/cache/cache.go
vendored
2
selector/cache/cache.go
vendored
@ -356,7 +356,7 @@ func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (s
|
||||
|
||||
// if there's nothing left, return
|
||||
if len(services) == 0 {
|
||||
return nil, selector.ErrNotFound
|
||||
return nil, selector.ErrNoneAvailable
|
||||
}
|
||||
|
||||
return sopts.Strategy(services), nil
|
||||
|
@ -71,7 +71,7 @@ func (r *defaultSelector) Select(service string, opts ...SelectOption) (Next, er
|
||||
|
||||
// if there's nothing left, return
|
||||
if len(services) == 0 {
|
||||
return nil, ErrNotFound
|
||||
return nil, ErrNoneAvailable
|
||||
}
|
||||
|
||||
return sopts.Strategy(services), nil
|
||||
|
@ -1,12 +1,14 @@
|
||||
package selector
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/registry"
|
||||
"github.com/micro/go-micro/registry/mock"
|
||||
)
|
||||
|
||||
func TestRandomSelector(t *testing.T) {
|
||||
func TestDefaultSelector(t *testing.T) {
|
||||
counts := map[string]int{}
|
||||
|
||||
rs := newDefaultSelector(Registry(mock.NewRegistry()))
|
||||
@ -24,5 +26,81 @@ func TestRandomSelector(t *testing.T) {
|
||||
counts[node.Id]++
|
||||
}
|
||||
|
||||
t.Logf("Random Counts %v", counts)
|
||||
t.Logf("Default Counts %v", counts)
|
||||
}
|
||||
|
||||
func TestBlackList(t *testing.T) {
|
||||
r := mock.NewRegistry()
|
||||
|
||||
r.Register(®istry.Service{
|
||||
Name: "test",
|
||||
Nodes: []*registry.Node{
|
||||
®istry.Node{
|
||||
Id: "test-1",
|
||||
Address: "localhost",
|
||||
Port: 10001,
|
||||
},
|
||||
®istry.Node{
|
||||
Id: "test-2",
|
||||
Address: "localhost",
|
||||
Port: 10002,
|
||||
},
|
||||
®istry.Node{
|
||||
Id: "test-3",
|
||||
Address: "localhost",
|
||||
Port: 10002,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
rs := newDefaultSelector(Registry(r))
|
||||
|
||||
next, err := rs.Select("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
node, err := next()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; i < 4; i++ {
|
||||
rs.Mark("test", node, errors.New("error"))
|
||||
}
|
||||
|
||||
next, err = rs.Select("test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// still expecting 2 nodes
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
node, err = next()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen[node.Id] = true
|
||||
}
|
||||
|
||||
if len(seen) != 2 {
|
||||
t.Fatalf("Expected seen to be 2 %+v", seen)
|
||||
}
|
||||
|
||||
// blacklist all of it
|
||||
for i := 0; i < 9; i++ {
|
||||
node, err = next()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rs.Mark("test", node, errors.New("error"))
|
||||
}
|
||||
|
||||
next, err = rs.Select("test")
|
||||
if err != ErrNoneAvailable {
|
||||
t.Fatalf("Expected %v got %v", ErrNoneAvailable, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ func Random(services []*registry.Service) Next {
|
||||
|
||||
return func() (*registry.Node, error) {
|
||||
if len(nodes) == 0 {
|
||||
return nil, ErrNotFound
|
||||
return nil, ErrNoneAvailable
|
||||
}
|
||||
|
||||
i := rand.Int() % len(nodes)
|
||||
@ -43,7 +43,7 @@ func RoundRobin(services []*registry.Service) Next {
|
||||
|
||||
return func() (*registry.Node, error) {
|
||||
if len(nodes) == 0 {
|
||||
return nil, ErrNotFound
|
||||
return nil, ErrNoneAvailable
|
||||
}
|
||||
|
||||
mtx.Lock()
|
||||
|
Loading…
Reference in New Issue
Block a user