Add endpoint filter

This commit is contained in:
Asim 2016-04-23 21:24:07 +01:00
parent febe87dfb8
commit ed764ca0b2
2 changed files with 100 additions and 0 deletions

View File

@ -4,6 +4,25 @@ import (
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
// FilterEndpoint is an endpoint based Select Filter which will
// only return services with the endpoint specified.
func FilterEndpoint(name string) Filter {
return func(old []*registry.Service) []*registry.Service {
var services []*registry.Service
for _, service := range old {
for _, ep := range service.Endpoints {
if ep.Name == name {
services = append(services, service)
break
}
}
}
return services
}
}
// FilterLabel is a label based Select Filter which will // FilterLabel is a label based Select Filter which will
// only return services with the label specified. // only return services with the label specified.
func FilterLabel(key, val string) Filter { func FilterLabel(key, val string) Filter {

View File

@ -6,6 +6,87 @@ import (
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
func TestFilterEndpoint(t *testing.T) {
testData := []struct {
services []*registry.Service
endpoint string
count int
}{
{
services: []*registry.Service{
&registry.Service{
Name: "test",
Version: "1.0.0",
Endpoints: []*registry.Endpoint{
&registry.Endpoint{
Name: "Foo.Bar",
},
},
},
&registry.Service{
Name: "test",
Version: "1.1.0",
Endpoints: []*registry.Endpoint{
&registry.Endpoint{
Name: "Baz.Bar",
},
},
},
},
endpoint: "Foo.Bar",
count: 1,
},
{
services: []*registry.Service{
&registry.Service{
Name: "test",
Version: "1.0.0",
Endpoints: []*registry.Endpoint{
&registry.Endpoint{
Name: "Foo.Bar",
},
},
},
&registry.Service{
Name: "test",
Version: "1.1.0",
Endpoints: []*registry.Endpoint{
&registry.Endpoint{
Name: "Foo.Bar",
},
},
},
},
endpoint: "Bar.Baz",
count: 0,
},
}
for _, data := range testData {
filter := FilterEndpoint(data.endpoint)
services := filter(data.services)
if len(services) != data.count {
t.Fatalf("Expected %d services, got %d", data.count, len(services))
}
for _, service := range services {
var seen bool
for _, ep := range service.Endpoints {
if ep.Name == data.endpoint {
seen = true
break
}
}
if seen == false && data.count > 0 {
t.Fatalf("Expected %d services but seen is %t; result %+v", data.count, seen, services)
}
}
}
}
func TestFilterLabel(t *testing.T) { func TestFilterLabel(t *testing.T) {
testData := []struct { testData := []struct {
services []*registry.Service services []*registry.Service