2015-12-09 19:23:16 +00:00
|
|
|
package selector
|
2015-12-09 00:02:45 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-07 00:04:08 +01:00
|
|
|
"errors"
|
2015-12-09 00:02:45 +00:00
|
|
|
"testing"
|
2015-12-09 19:23:16 +00:00
|
|
|
|
2016-05-07 00:04:08 +01:00
|
|
|
"github.com/micro/go-micro/registry"
|
2015-12-09 19:23:16 +00:00
|
|
|
"github.com/micro/go-micro/registry/mock"
|
2015-12-09 00:02:45 +00:00
|
|
|
)
|
|
|
|
|
2016-05-07 00:04:08 +01:00
|
|
|
func TestDefaultSelector(t *testing.T) {
|
2015-12-09 00:02:45 +00:00
|
|
|
counts := map[string]int{}
|
|
|
|
|
2016-05-03 22:06:19 +01:00
|
|
|
rs := newDefaultSelector(Registry(mock.NewRegistry()))
|
2015-12-09 00:02:45 +00:00
|
|
|
|
2016-02-26 00:09:06 +00:00
|
|
|
next, err := rs.Select("foo")
|
2015-12-09 00:02:45 +00:00
|
|
|
if err != nil {
|
2016-05-03 22:06:19 +01:00
|
|
|
t.Errorf("Unexpected error calling default select: %v", err)
|
2015-12-09 00:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
node, err := next()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected node err, got err: %v", err)
|
|
|
|
}
|
|
|
|
counts[node.Id]++
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:04:08 +01:00
|
|
|
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
|
2016-05-26 18:26:02 +01:00
|
|
|
for i := 0; i < 20; i++ {
|
2016-05-07 00:04:08 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:02:45 +00:00
|
|
|
}
|