micro/registry/memory/memory_test.go

126 lines
2.2 KiB
Go
Raw Normal View History

2019-01-14 18:27:25 +03:00
package memory
2016-03-14 22:15:42 +03:00
import (
"testing"
"github.com/micro/go-micro/registry"
)
var (
testData = map[string][]*registry.Service{
"foo": []*registry.Service{
{
Name: "foo",
Version: "1.0.0",
Nodes: []*registry.Node{
{
Id: "foo-1.0.0-123",
2019-07-08 10:01:42 +03:00
Address: "localhost:9999",
2016-03-14 22:15:42 +03:00
},
{
Id: "foo-1.0.0-321",
2019-07-08 10:01:42 +03:00
Address: "localhost:9999",
2016-03-14 22:15:42 +03:00
},
},
},
{
Name: "foo",
Version: "1.0.1",
Nodes: []*registry.Node{
{
Id: "foo-1.0.1-321",
2019-07-08 10:01:42 +03:00
Address: "localhost:6666",
2016-03-14 22:15:42 +03:00
},
},
},
{
Name: "foo",
Version: "1.0.3",
Nodes: []*registry.Node{
{
Id: "foo-1.0.3-345",
2019-07-08 10:01:42 +03:00
Address: "localhost:8888",
2016-03-14 22:15:42 +03:00
},
},
},
},
"bar": []*registry.Service{
{
Name: "bar",
Version: "default",
Nodes: []*registry.Node{
{
Id: "bar-1.0.0-123",
2019-07-08 10:01:42 +03:00
Address: "localhost:9999",
2016-03-14 22:15:42 +03:00
},
{
Id: "bar-1.0.0-321",
2019-07-08 10:01:42 +03:00
Address: "localhost:9999",
2016-03-14 22:15:42 +03:00
},
},
},
{
Name: "bar",
Version: "latest",
Nodes: []*registry.Node{
{
Id: "bar-1.0.1-321",
2019-07-08 10:01:42 +03:00
Address: "localhost:6666",
2016-03-14 22:15:42 +03:00
},
},
},
},
}
)
2019-01-18 20:29:17 +03:00
func TestMemoryRegistry(t *testing.T) {
2016-03-14 22:15:42 +03:00
m := NewRegistry()
fn := func(k string, v []*registry.Service) {
services, err := m.GetService(k)
if err != nil {
t.Errorf("Unexpected error getting service %s: %v", k, err)
}
if len(services) != len(v) {
t.Errorf("Expected %d services for %s, got %d", len(v), k, len(services))
}
for _, service := range v {
var seen bool
for _, s := range services {
if s.Version == service.Version {
seen = true
break
}
}
if !seen {
t.Errorf("expected to find version %s", service.Version)
}
}
}
// register data
for _, v := range testData {
for _, service := range v {
if err := m.Register(service); err != nil {
t.Errorf("Unexpected register error: %v", err)
}
}
}
// using test data
for k, v := range testData {
fn(k, v)
}
// deregister
for _, v := range testData {
for _, service := range v {
if err := m.Deregister(service); err != nil {
t.Errorf("Unexpected deregister error: %v", err)
}
}
}
}