Use ErrNoneAvailable and test blacklisting

This commit is contained in:
Asim 2016-05-07 00:04:08 +01:00
parent 63c6e82192
commit 75f2706fd0
4 changed files with 84 additions and 6 deletions

View File

@ -356,7 +356,7 @@ func (c *cacheSelector) Select(service string, opts ...selector.SelectOption) (s
// if there's nothing left, return // if there's nothing left, return
if len(services) == 0 { if len(services) == 0 {
return nil, selector.ErrNotFound return nil, selector.ErrNoneAvailable
} }
return sopts.Strategy(services), nil return sopts.Strategy(services), nil

View File

@ -71,7 +71,7 @@ func (r *defaultSelector) Select(service string, opts ...SelectOption) (Next, er
// if there's nothing left, return // if there's nothing left, return
if len(services) == 0 { if len(services) == 0 {
return nil, ErrNotFound return nil, ErrNoneAvailable
} }
return sopts.Strategy(services), nil return sopts.Strategy(services), nil

View File

@ -1,12 +1,14 @@
package selector package selector
import ( import (
"errors"
"testing" "testing"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/mock" "github.com/micro/go-micro/registry/mock"
) )
func TestRandomSelector(t *testing.T) { func TestDefaultSelector(t *testing.T) {
counts := map[string]int{} counts := map[string]int{}
rs := newDefaultSelector(Registry(mock.NewRegistry())) rs := newDefaultSelector(Registry(mock.NewRegistry()))
@ -24,5 +26,81 @@ func TestRandomSelector(t *testing.T) {
counts[node.Id]++ 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(&registry.Service{
Name: "test",
Nodes: []*registry.Node{
&registry.Node{
Id: "test-1",
Address: "localhost",
Port: 10001,
},
&registry.Node{
Id: "test-2",
Address: "localhost",
Port: 10002,
},
&registry.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)
}
} }

View File

@ -22,7 +22,7 @@ func Random(services []*registry.Service) Next {
return func() (*registry.Node, error) { return func() (*registry.Node, error) {
if len(nodes) == 0 { if len(nodes) == 0 {
return nil, ErrNotFound return nil, ErrNoneAvailable
} }
i := rand.Int() % len(nodes) i := rand.Int() % len(nodes)
@ -43,7 +43,7 @@ func RoundRobin(services []*registry.Service) Next {
return func() (*registry.Node, error) { return func() (*registry.Node, error) {
if len(nodes) == 0 { if len(nodes) == 0 {
return nil, ErrNotFound return nil, ErrNoneAvailable
} }
mtx.Lock() mtx.Lock()